Back: Programming
Previous: Compiling functions, timing
mcc fac.c a.out -linkname 'math -mathlink'
#include#include #include #include "mathlink.h" void error(); static int read_and_print_expression(); static int read_and_print_atom(); static int read_and_print_function(); int main(argc, argv) int argc; char* argv[]; { char buf[BUFSIZ]; MLINK lp; printf( "\n\nInteger to factor: "); scanf( "%s", buf); lp = MLOpen( argc, argv); if(lp == NULL) return 1; /* Send FactorInteger[n]. */ MLPutFunction( lp, "FactorInteger", 1L); MLPutNext( lp, MLTKINT); MLPutSize( lp, strlen(buf)); MLPutData( lp, buf, strlen(buf)); MLEndPacket( lp); /* skip any packets before the first ReturnPacket */ while( MLNextPacket( lp) != RETURNPKT ) { MLNewPacket( lp); if( MLError( lp)) error( lp); } read_and_print_expression( lp); printf( "\n"); MLPutFunction(lp, "Exit", 0L); MLClose( lp); return 0; } static int read_and_print_expression( lp) MLINK lp; { int tag; switch (tag = MLGetNext( lp)) { case MLTKSYM: case MLTKSTR: case MLTKINT: case MLTKREAL: return read_and_print_atom( lp, tag); case MLTKFUNC: return (read_and_print_function( lp)); case MLTKERROR: default: return 0; } } static int read_and_print_atom( lp, tag) MLINK lp; int tag; { long len, gotlen; char buf[BUFSIZ]; if (tag == MLTKSTR) putchar('"'); while( MLBytesToGet( lp, &len) && len > 0 ) { if (len > BUFSIZ-1) len = BUFSIZ-1; if( MLGetData( lp, buf, len, &gotlen)){ buf[gotlen] = '\0'; printf("%s", buf); } } if (tag == MLTKSTR) putchar('"'); putchar(' '); return MLError(lp) == MLEOK; } static int read_and_print_function( lp) MLINK lp; { long len, i; static indent; if ( ! MLGetArgCount( lp, &len)) return 0; indent += 3; printf( "\n%*.*s", indent, indent, ""); if( read_and_print_expression( lp) == 0) return 0; printf( "["); for( i = 1; i <= len; ++i) { if( read_and_print_expression( lp) == 0) return 0; if( i < len) printf( ", "); } printf( "]"); indent -= 3; return 1; } void error( lp) MLINK lp; { if (MLError( lp)) { fprintf( stderr, "Error detected by MathLink: %s.\n", MLErrorMessage( lp)); }else{ fprintf( stderr, "Error detected by this program.\n"); } MLClose( lp); exit( 1); }
Then, during a Mathematica session, you install the external program with Install and use the function with the form specified in the template file.
#include <mathlink.h>
char *dayoweek(d,m,y)
int d,m,y;
{ int C,K,Z;
m -= 2; if (m <= 0) { m += 12; y -= 1; }
C = y/100; K = y%100;
Z = ((26*m - 2)/10 + d + K + K/4 + C/4 - 2*C) % 7 ;
if (Z == 0) return("Sunday.");
if (Z == 1) return("Monday.");
if (Z == 2) return("Tuesday.");
if (Z == 3) return("Wednesday.");
if (Z == 4) return("Thursday.");
if (Z == 5) return("Friday.");
if (Z == 6) return("Saturday.");
}
int main() { return MLMain(0,0); }
:Begin:
:Function: dayoweek
:Pattern: DayOfWeek[d_Integer, m_Integer, y_Integer]
:Arguments: { d, m, y }
:ArgumentTypes: { Integer, Integer, Integer }
:ReturnType: String
:End:
mcc dayofweek.c dayofweek.tm
link = Install["a.out"]; LinkPatterns[link] DayOfWeek[ 1, 11, 1995 ] Wednesday.