-------------------------------------------------------
myTrace.c
-------------------------------------------------------
#include<stdio.h> #include<string.h> #include "mathlink.h" extern void myTrace( void ); void myTrace( void ) { int i,j,n; double *matrix; int *dims; char **heads; int rank; if( !MLGetReal64Array(stdlink,&matrix,&dims,&heads,&rank) ){ // unable to read data printf(" MLGetReal64Array error reading data \n"); }; n = dims[0]; double resum=0.; double imsum=0.; double sum[2]; int outdims[1]; char **outheads; int outrank = 1; for(i=0;i<n;i++){ resum = resum + matrix[2*i*n + 2*i]; imsum = imsum + matrix[2*i*n + 2*i + 1]; } MLReleaseReal64Array(stdlink,matrix,dims,heads,rank); MLPutFunction(stdlink,"Complex",2); MLPutFloat(stdlink,resum); MLPutFloat(stdlink,imsum); } #if WINDOWS_MATHLINK #if __BORLANDC__ #pragma argsused #endif int PASCAL WinMain( HINSTANCE hinstCurrent, HINSTANCE hinstPrevious, LPSTR lpszCmdLine, int nCmdShow) { char buff[512]; char FAR * buff_start = buff; char FAR * argv[32]; char FAR * FAR * argv_end = argv + 32; hinstPrevious = hinstPrevious; /* suppress warning */ if( !MLInitializeIcon( hinstCurrent, nCmdShow)) return 1; MLScanString( argv, &argv_end, &lpszCmdLine, &buff_start); return MLMain( (int)(argv_end - argv), argv); } #else int main(int argc, char* argv[]) { return MLMain(argc, argv); } #endif
-----------------------------------------------------
myTrace.tm
-----------------------------------------------------
:Begin: :Function: myTrace :Pattern: myTrace[ L_List] :Arguments: { L } :ArgumentTypes: { Manual } :ReturnType: Manual :End: :Evaluate: myTrace::usage = "myTrace[M] gives the trace of complex matrix M"
------------------------------------------------------
Makefile
------------------------------------------------------
MPREP = /usr/bin/mprep CXX = /usr/bin/c++ myTrace : myTracetm.c myTrace.c ${CXX} myTracetm.c myTrace.c -o myTrace -lML64i3 -lm -lpthread -lrt -lstdc++ myTracetm.c : myTrace.tm ${MPREP} myTrace.tm -o $@
---------------------------------------------------
myTrace.nb
---------------------------------------------------
link = Install["./myTrace"] (m = {{1., 2., 1.}, {2., 2., 2.}, {3., 3., 3.}} + I) // MatrixForm myTrace[m]
No comments:
Post a Comment