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

📄 example.cs

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

namespace MatlabCSharpExample
{
	class Example
	{
		public Example()
		{
			eigenlibInitialize();	
		}

		public void CleanUp()
		{
			eigenlibTerminate();	
		}

/* declare dll functions */

		[ DllImport( "eigenlib.dll ", CallingConvention = CallingConvention.Cdecl)] 
		public static extern void eigenlibInitialize(); 

		[ DllImport( "eigenlib.dll ", CallingConvention = CallingConvention.Cdecl)] 
		public static extern void eigenlibTerminate(); 

		[ DllImport( "eigenlib.dll ", CallingConvention = CallingConvention.Cdecl)] 
		public static extern void mlfMyeig(int nargout, ref IntPtr V, ref IntPtr D, IntPtr A);


/* end dll functions */

		[STAThread]
		static void Main(string[] args)
		{
			Console.WriteLine(" ") ;

			Example obj = new Example() ; 

			Console.WriteLine("Eigenvalues and eigenvectors" ) ;
			obj.EigValueVector() ;

			obj.CleanUp() ;
		}


		/* **************************** */
		public void EigValueVector()  
		{

			double[,] A = {{ 0, -6, -1} , {6, 2, -16} ,  {-5, 20, -10} } ;

			int row = 3 ;
			int col = 3 ;
			int i, j ;

			// declare mx_Array variables //
			IntPtr mx_A			   = (IntPtr) null  ;
			IntPtr mx_eigenvectors = (IntPtr) null  ;
			IntPtr mx_eigenvalues  = (IntPtr) null  ;


			/* convert Cs double to mxArray */
			mx_A = MatlabCSharp.double2mxArray_matrixReal(A) ;

			/* call an implemental function */
			mlfMyeig(2, ref mx_eigenvectors, ref mx_eigenvalues, mx_A);

			/* convert back to Cs double  */
			double[,] db_eigenvectorsReal = new double [row, col] ;
			double[,] db_eigenvectorsImag = new double [row, col] ;

			double[,] db_eigenvaluesReal = new double [row, col] ;
			double[,] db_eigenvaluesImag = new double [row, col] ;

			MatlabCSharp.mxArray2double_matrixComplex(
				mx_eigenvectors, ref db_eigenvectorsReal, ref db_eigenvectorsImag ) ;

			MatlabCSharp.mxArray2double_matrixComplex(
				mx_eigenvalues , ref db_eigenvaluesReal , ref db_eigenvaluesImag ) ;

			/* print out */
			Console.WriteLine("Eigenvalues of the matrix A : " ) ;

			for (i=0; i<row; i++)  
			{
				Console.Write( db_eigenvaluesReal.GetValue(i,i) +  " + "  ) ;
				Console.Write( db_eigenvaluesImag.GetValue(i,i) +  "i"  + "\n"  ) ;
			}
			
			Console.Write("\n") ;


			Console.WriteLine("Eigenvectors of the matrix A : " ) ;

			for (j=0; j<col; j++ ) 
			{
				Console.WriteLine("Eigenvector {0} is : ", (j+1).ToString()  ) ;

				for (i=0; i<row; i++)  
				{

					Console.Write( db_eigenvectorsReal.GetValue(i,j) + " + "  ) ;
					Console.Write( db_eigenvectorsImag.GetValue(i,j) + "i" + "\n") ;
				}

				Console.Write("\n") ;
			}

			Console.Write("\n") ;
			
		}
	} // end class 
}

⌨️ 快捷键说明

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