mathematica c语言混合编程的问题 大神求救
目的是算出cos(x)5次方的程序,整个程序为,结果却不对,调试了很长时间也没有搞定,向大神求助了#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mathlink.h"
double f(MLINK lp,double x);
int main()
{
int argc=4;
char*argv ={"-linkname", "F:\\Program Files\\Wolfram Research\\Mathematica\\8.0", "-linkmode", "launch", "NULL"};//指定连接方式
MLEnvironment env;//mathlink的环境变量
MLINK lp;
env=MLInitialize(NULL);//初始化
if(env==NULL)
{
printf("initialization error!\n");
return 1;
}
lp=MLOpen(argc, argv);
if(lp==NULL)
{
printf("open error!\n");
return 1;
}
for(int i=1;i<=10;i++)
printf("cos(%d)= %lf\n",i,f(lp,i));//计算f(1)至f(10)
MLClose(lp);
MLDeinitialize(env);
//printf("Hello World!\n");
return 0;
}
double f(MLINK lp, double x)
{
double res;
int pkt;
static int done=0;
if(!done)
{
//char* expression="cos(x)^5";
MLPutFunction(lp,"EvaluatrPacket",1);
MLPutFunction(lp,"ToExpression",1);
MLPutString(lp,"cos(x)^5");
MLEndPacket(lp);
while((pkt=MLNextPacket(lp),pkt)&&pkt!=RETURNPKT);
MLNewPacket(lp);
done=1;
}
MLPutFunction(lp,"EvaluatePacket",1);
MLPutFunction(lp,"N",1);
MLPutFunction(lp,"F",1);
MLPutReal(lp,x);
MLEndPacket(lp);
while((pkt=MLNextPacket(lp))&&pkt!=RETURNPKT);
MLNewPacket(lp);
MLGetReal(lp, &res);
return res;
}
我觉得是不是因为在mathematica上面还要写个接口,提供c数据交换的接口 这么大个论坛没人了么?
页:
[1]