Help With MathLink
- To: mathgroup at smc.vnet.net
- Subject: [mg28951] Help With MathLink
- From: "Ersek, Ted R" <ErsekTR at navair.navy.mil>
- Date: Mon, 21 May 2001 00:43:38 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
You may remember the RootSearch program I discussed several weeks ago. So
far I only had a little time to work on it .... I want to do most of the
work in C and I am having trouble with MathLink. I will make the program
more elegant once I get it working.
In the Mathematica I define:
In[1]:=
f = Sin; (* Eventually Rootsearch will automatically define f in a
privat context. *)
SampleFunction[x_]:= With[ {y=N[f[x]] },
If[ MachineNumberQ[y], {x, y, 1.0}, {x, 99.0, 0.0} ]
]
(*************************)
Then I get the following results:
In[2]:= Install[ "rootsearch" ]
Out[2]=
LinkObject[.\rootsearch.exe,2,2]
In[3]:= RootSearch[ Sin[x], {x, 2.0, 3.0}]
Out[3]=
{{0. , 0. , 0. }, {0. , 0. , 0. }}
I expected the following output instead.
{{2., 0.909297, 1. },{3., 0.14112, 1. }}
Please help me get it working. I can email you the related files if it
helps.
Thank you in advance,
Ted Ersek
----------------------------------
My MathLink templete is:
/* ----------------------- */
:Begin:
:Function: root_search
:Pattern: RootSearch[func_,{x_Symbol,x0_Real,x1_Real}]
:Arguments: { x0, x1}
:ArgumentTypes: { Real, Real }
:ReturnType: Manual
:End:
/* ------------------------- */
My source code is:
/* ------------------------- */
#include "mathlink.h"
#include "stdlib.h"
#include "stdio.h"
void sample_function( double x, double *result);
extern void root_search(double x0, double x1);
void root_search(double x0, double x1)
{
int total_samples=0;
int dimensions[2];
char *heads[2];
long depth=2;
double *value_array;
value_array=(double*)calloc(24,sizeof(double));
sample_function(x0,value_array);
total_samples++;
sample_function(x1,value_array+3);
total_samples++;
dimensions[0]=2;
dimensions[1]=3;
heads[0]=heads[1]="List";
MLPutRealArray(stdlink,value_array,dimensions,heads,depth);
return;
}
void sample_function( double x, double *result )
{
long three=3;
MLPutFunction(stdlink,"EvaluatePacket",1);
MLPutFunction(stdlink,"SampleFunction",1);
MLPutDouble(stdlink,x);
MLEndPacket(stdlink);
MLNextPacket(stdlink);
MLGetRealList(stdlink,&result,&three);
MLDisownRealList(stdlink,result,three);
return;
}