⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 class1.cs

📁 MATLAB C# Book. This book is a great tutorial for C# programmers who use MATLAB to develop applicati
💻 CS
字号:
using System;
using MatrixCalculation ;
using MWComUtil ;

namespace LinearEquationsCOM
{
	class Class1
		{

		[STAThread]
		static void Main(string[] args)
		{
			Console.WriteLine(
			"MATLAB COM Builder for Matrix Computations and Linear Equations") ;

			Class1 obj = new Class1()	;
			
			Console.WriteLine("\n Matrix Multiplication") ;
			obj.MatrixMultiplication()	;

			Console.WriteLine("\n Linear System Equation") ;
			obj.LinearSystemEquations()	;

			Console.WriteLine("\n LU decompression") ;
			obj.LU_decompression()		;
		}

		public void MatrixMultiplication() 
		{

			MatrixCalculationclass obj2 = new MatrixCalculationclass() ;

			double [,] db_A =	{	{ 1.1, 2.2  , 3.3  , 4.4   }	,
									{ 5.5, 6.6  , 7.7  , 8.8   }	,
									{ 9.9, 10.10, 11.11, 12.12 }	} ;

			double [,] db_B = {{ 10, 11}, {12, 13}, {14, 15}, {16, 17} } ;

			int rowA = db_A.GetUpperBound(0) - db_A.GetLowerBound(0) + 1;
			int colA = db_A.GetUpperBound(1) - db_A.GetLowerBound(1) + 1;

			int rowB = db_B.GetUpperBound(0) - db_B.GetLowerBound(0) + 1;
			int colB = db_B.GetUpperBound(1) - db_B.GetLowerBound(1) + 1;

			Object obj_A = (object) db_A  ;
			Object obj_B = (object) db_B  ;

			int rowC = rowA ;
			int colC = colB ;

			Object obj_C = (object) 0 ;
			obj2.mymtimes(1, ref obj_C, obj_A, obj_B) ;

			double [,] db_C = new double[rowC, colC] ;
			int aLength = rowC*colC ;

			Array.Copy((double [,])obj_C, db_C, aLength) ;	
			printMatrix(db_C) ;

		}

		/* ******************************** */

		public void LinearSystemEquations()  
		{
			MatrixCalculationclass obj3 = new MatrixCalculationclass() ;

			/* Solve general linear system equations Ax = b */
			double[,] db_A	 =  {	{1.1,  5.6, 3.3}  ,
									{4.4, 12.3, 6.6}  ,
									{7.7,  8.8, 9.9}  };

			double[] db_vectorb =  { 12.5, 32.2 , 45.6 } ;
			
			Object obj_A = (object) db_A  ;
			Object obj_b = (object) db_vectorb  ;
			Object obj_Transb = (object) 0 ;

			// to get a comlumn vector b
			// note: type of obj_Transb is Double[,]
			obj3.mytranspose(1, ref obj_Transb, obj_b ) ;

			Object obj_x = (object) 0 ;
			obj3.mymldivide(1, ref obj_x, obj_A, obj_Transb) ;

			// note: type of obj_x is Double[,],

			int aLength = db_vectorb.Length ;
			double [,] db_x = new double[aLength, 1] ;
			
			Array.Copy( (double[,])obj_x, db_x, aLength );			
			printMatrix(db_x) ;
			
		}

			/* **************************** */

		public void LU_decompression ()  
		{
			/* find lower and upper matrixes */
			double[,] db_A =  {	{1.1,  5.6, 3.3}  ,
								{4.4, 12.3, 6.6}  ,
								{7.7,  8.8, 9.9}  };

			Object obj_A = (object) db_A ;
			Object obj_L = (object) 0 ;
			Object obj_U = (object) 0 ;
			Object obj_P = (object) 0 ;

			/* call an implemental function */
			MatrixCalculationclass obj4 = new MatrixCalculationclass() ;
			obj4.mylu(3, ref obj_L, ref obj_U, ref obj_P, obj_A);

			int row = db_A.GetUpperBound(0) - db_A.GetLowerBound(0) + 1;
			int col = db_A.GetUpperBound(1) - db_A.GetLowerBound(1) + 1;

			double[,] db_L = new double[row, col] ;
			double[,] db_U = new double[row, col] ;

			int aLength = row*col ;
			Array.Copy( (double[,])obj_L, db_L, aLength );			
			Array.Copy( (double[,])obj_U, db_U, aLength );			

			/* print out */
			Console.WriteLine("\n The lower matrix") ;
			printMatrix(db_L) ;

			Console.WriteLine("\n The upper matrix") ;
			printMatrix(db_U) ;

		}
		/* ******************************** */
		public static void printMatrix(double[,] matrix)  
		{

			int i, j, row, col ;
			row = matrix.GetUpperBound(0) - matrix.GetLowerBound(0) + 1;
			col = matrix.GetUpperBound(1) - matrix.GetLowerBound(1) + 1;


			for (i=0; i<row; i++)  
			{
				for (j=0; j<col; j++) 
				{
					
					Console.Write("{0} \t", matrix.GetValue(i,j).ToString() ) ;

				}
				Console.WriteLine() ;
			}

		}

		/* ******************************** */

	}  // end class
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -