📄 example.cs
字号:
using System;
using System.Runtime.InteropServices;
using UtilityMatlabCompilerVer4 ;
namespace ExampleUsingMatlab
{
public class Example
{
//constructor
public Example()
{
matrixcomputationslibInitialize();
}
public void CleanUp()
{
matrixcomputationslibTerminate();
}
/* declare dll functions */
[ DllImport( "matrixcomputationslib.dll ", CallingConvention = CallingConvention.Cdecl)]
public static extern void matrixcomputationslibInitialize();
[ DllImport( "matrixcomputationslib.dll ", CallingConvention = CallingConvention.Cdecl)]
public static extern void matrixcomputationslibTerminate();
[ DllImport( "matrixcomputationslib.dll ", CallingConvention = CallingConvention.Cdecl)]
public static extern void mlfMydet(int nargout, ref IntPtr y, IntPtr a);
[ DllImport( "matrixcomputationslib.dll ", CallingConvention = CallingConvention.Cdecl)]
public static extern void mlfMyinv(int nargout, ref IntPtr y, IntPtr a);
[ DllImport( "matrixcomputationslib.dll ", CallingConvention = CallingConvention.Cdecl)]
public static extern void mlfMyminus(int nargout, ref IntPtr y, IntPtr a, IntPtr b);
[ DllImport( "matrixcomputationslib.dll ", CallingConvention = CallingConvention.Cdecl)]
public static extern void mlfMymtimes(int nargout, ref IntPtr y, IntPtr a, IntPtr b);
[ DllImport( "matrixcomputationslib.dll ", CallingConvention = CallingConvention.Cdecl)]
public static extern void mlfMyplus(int nargout, ref IntPtr y, IntPtr a, IntPtr b);
[ DllImport( "matrixcomputationslib.dll ", CallingConvention = CallingConvention.Cdecl)]
public static extern void mlfMytranspose(int nargout, ref IntPtr y, IntPtr x);
/* end dll functions */
[STAThread]
static void Main(string[] args)
{
Example obj = new Example() ;
Console.WriteLine("Matrix Computations") ;
Console.WriteLine("Matrix addition") ;
obj.addMatrix() ;
Console.WriteLine("Matrix subtraction") ;
obj.subtractMatrix() ;
Console.WriteLine("Matrix multiplication") ;
obj.multipleMatrix();
Console.WriteLine("Matrix determinant") ;
obj.determinantMatrix() ;
Console.WriteLine("Inverse matrix") ;
obj.inverseMatrix() ;
Console.WriteLine("Transpose matrix") ;
obj.transposeMatrix() ;
obj.CleanUp();
}
/* ***************************************** */
public void addMatrix()
{
double [,]A = {{ 1.1, 2.2, 3.3} , {4.4, 5.5, 6.6} , {7.7, 8.8, 9.9} } ;
double [,]B = {{ 11 , 12 , 13 } , {14 , 15 , 16 } , {17 , 18 , 19 } } ;
/* declare mxArray variables */
IntPtr mx_A = (IntPtr)null ;
IntPtr mx_B = (IntPtr)null ;
IntPtr mx_C = (IntPtr)null ;
/* convert Cs matrix to mxArray */
mx_A = MatlabCSharp.double2mxArray_matrixReal(A) ;
mx_B = MatlabCSharp.double2mxArray_matrixReal(B) ;
/* call an implemental function */
mlfMyplus(1, ref mx_C, mx_A, mx_B );
/* convert back to Cs double */
double [,] C = MatlabCSharp.mxArray2double_matrixReal(mx_C) ;
/* print out */
MatlabCSharp.printMatrix(C) ;
}
/* ***************************************** */
public void subtractMatrix()
{
double [,]A = {{ 1.1, 2.2, 3.3} , {4.4, 5.5, 6.6} , {7.7, 8.8, 9.9} } ;
double [,]B = {{ 11 , 12 , 13 } , {14 , 15 , 16 } , {17 , 18 , 19 } } ;
/* declare mxArray variables */
IntPtr mx_A = (IntPtr)null ;
IntPtr mx_B = (IntPtr)null ;
IntPtr mx_C = (IntPtr)null ;
/* convert Cs matrix to mxArray */
mx_A = MatlabCSharp.double2mxArray_matrixReal(A) ;
mx_B = MatlabCSharp.double2mxArray_matrixReal(B) ;
/* call an implemental function */
mlfMyminus(1, ref mx_C, mx_A, mx_B );
/* convert back to Cs double */
double [,] C = MatlabCSharp.mxArray2double_matrixReal(mx_C) ;
/* print out */
MatlabCSharp.printMatrix(C) ;
}
/* ***************************************** */
public void multipleMatrix()
{
double [,]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 [,]B = {{ 10, 11}, {12, 13}, {14, 15}, {16, 17} } ;
/* declare mxArray variables */
IntPtr mx_A = (IntPtr)null ;
IntPtr mx_B = (IntPtr)null ;
IntPtr mx_C = (IntPtr)null ;
/* convert Cs matrix to mxArray */
mx_A = MatlabCSharp.double2mxArray_matrixReal(A) ;
mx_B = MatlabCSharp.double2mxArray_matrixReal(B) ;
/* call an implemental function */
mlfMymtimes(1, ref mx_C, mx_A, mx_B );
/* convert back to Cs double */
double [,] C = MatlabCSharp.mxArray2double_matrixReal(mx_C) ;
/* print out */
MatlabCSharp.printMatrix(C) ;
}
/* ***************************************** */
public void determinantMatrix()
{
double [,]A = {{ 1.1, 2.2, 3.3}, {7.7, 4.4, 9.9} , {4.4, 5.5, 8.8} } ;
/* declare mxArray variables */
IntPtr mx_A = (IntPtr)null ;
IntPtr mx_detA = (IntPtr)null ;
/* convert Cs matrix to mxArray */
mx_A = MatlabCSharp.double2mxArray_matrixReal(A) ;
/* call an implemental function */
mlfMydet(1, ref mx_detA, mx_A );
/* convert back to Cs double */
double detA = MatlabCSharp.mxArray2double_scalarReal(mx_detA) ;
/* print out */
Console.WriteLine( detA.ToString() ) ;
}
/* ***************************************** */
public void inverseMatrix()
{
double [,]A = {{ -1 , 1 , 2} , {3 , -1 , 1} , {-1 , 3 , 4} } ;
/* declare mxArray variables */
IntPtr mx_A = (IntPtr)null ;
IntPtr mx_inverseA = (IntPtr)null ;
/* convert Cs matrix to mxArray */
mx_A = MatlabCSharp.double2mxArray_matrixReal(A) ;
/* call an implemental function */
mlfMyinv(1, ref mx_inverseA, mx_A );
/* convert back to Cs double */
double[,] inverseA = MatlabCSharp.mxArray2double_matrixReal(mx_inverseA) ;
/* print out */
MatlabCSharp.printMatrix(inverseA) ;
}
/* ***************************************** */
public void transposeMatrix()
{
double [,] A = {{ -1 , 1 , 2} , {3 , -1 , 1} , {-1 , 3 , 4} } ;
/* declare mxArray variables */
IntPtr mx_A = (IntPtr)null ;
IntPtr mx_transposeA = (IntPtr)null ;
/* convert Cs matrix to mxArray */
mx_A = MatlabCSharp.double2mxArray_matrixReal(A) ;
/* call an implemental function */
mlfMytranspose(1, ref mx_transposeA, mx_A );
/* convert back to Cs double */
double[,] transposeA = MatlabCSharp.mxArray2double_matrixReal(mx_transposeA) ;
/* print out */
MatlabCSharp.printMatrix(transposeA) ;
}
} // end class
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -