Re: MathLink MLGetRealArray
- To: mathgroup at smc.vnet.net
- Subject: [mg21974] Re: MathLink MLGetRealArray
- From: Jens-Peer Kuska <kuska at informatik.uni-leipzig.de>
- Date: Mon, 7 Feb 2000 13:02:27 -0500 (EST)
- Organization: Universitaet Leipzig
- References: <87iph4$3vb@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Hi Johannes,
a) look at the decalaration of MLGetRealArray(), x is a pointer to a
pointer to double
and *not* as you use in your example a pointer to a pointer to a
pointer to double.
b) you must read the values in your C program exactly as you pass them
from Mathematica.
I append the source that compile/and run fine on Unix and on my
Windows box.
so you have to read the y list first, than the x-Matrix and than the
kernelWidth
c) I have several MathLink programs but I have never used
MLGetRealArray(). Since you can't
allocate a 2d array in C dynamical you must pass a 1d array and use
the index calculation
as usual. So, it is not very usefull to pass a multi-dimensional
array. I personal
pass the matrix dimensions typical in separate arguments and use a
Flatten[] array.
For the way back Matrix-> Mathematica I typical pass a 1d array with
some Partition[] calls
arround.
d) seven nice MathLink programs, explained and with help full packages
are just a
book store away from you, don't delay order
"Mathematica und C in der modernen Theoretischen Physik" von
Jens-Peer Kuska
ISBN3-540-61489-3, Springer 1997
the programs do allmost everything that on can do with MathLink,
calls to Mathematica
passing arrays (compex and real ones) solving ordinary differential
equations with
MathLink ... it is soo nice
c) You can look to my MathGL3d source code, it has total 19000 lines of
C/MathLink and Mathematica
code with allmost every dirty trick
Hope that helps
Jens
Johannes Ludsteck wrote:
>
> Dear Group Members,
> I am trying to get a matrix from Mathematica into a c program via
> MathLink. When I compile the code below (with Borland C++ 5.0), I
> get an Warning message Suspicious pointer conversion in line...,
> which relates to depthx. However, the variable depthx works fine.
>
> The problem is with the array x. If I try to access the elements in x,
> for example
> temp = x[0][0]; temp = temp * temp;
>
> then the program crashes.
> Any suggestions. It would help me too if someone could provide C
> program, written in connection with mathlink.
>
> Thank you,
> Johannes Ludsteck
--------------FC4D6D82E3605F9D6EFFE3D5
filename="test.tm"
#include <windows.h>
#include "mathlink.h"
void kerndens(void)
{ double *y,kernelWidth;
long n;
double *x, temp;
long *dimsx, depthx;
char **headsx;
MLGetRealList(stdlink,&y,&n);
MLGetRealArray(stdlink, &x, &dimsx, &headsx, &depthx);
MLGetReal(stdlink,&kernelWidth);
MLPutReal(stdlink, x[0]);
MLDisownRealList(stdlink,y,n);
MLDisownRealArray(stdlink, x, dimsx, headsx, depthx);
}
#ifdef __WIN32__
int WINAPI WinMain(
HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nShowCmd) {
char buff[512];
char FAR *buff_start=buff;
char FAR *argv[32];
char FAR * FAR * argv_end=argv+32;
if(!MLInitializeIcon(hInstance,nShowCmd)) return 1;
MLScanString(argv,&argv_end,&lpCmdLine,&buff_start);
return MLMain(argv_end-argv,argv);
}
#else
int main(int argc, char *argv[]) {
return MLMain(argc,argv);
}
#endif
:Begin:
:Function: kerndens
:Pattern: kerndens[y_List,x_?MatrixQ,kernelWidth_Real]
:Arguments: {y,x,kernelWidth}
:ArgumentTypes: {Manual}
:ReturnType: Manual
:End: