mathmatic c++的几个通用例程

来源:互联网 发布:hyper v 连不上网络 编辑:程序博客网 时间:2024/05/01 19:16

1)假设我们要求一个指数函数e^2的值指数函数的值,我们可以如下处理:

image

MLActivate(lp);

double result;
MLINK ml = MLLoopbackOpen(ep, &errno2);   //打开一个环回  
//将表达式e^3放入这个环回连接上  
MLPutFunction(ml,"N",1);   
MLPutFunction(ml, "Exp",1);     
MLPutInteger(ml, 3);  
MLTransferExpression(lp,ml);      //向Mathematica发送数据   
MLEndPacket(lp);  
while( (pkt = MLNextPacket( lp), pkt) && pkt != RETURNPKT) {    
     MLNewPacket( lp);      
     if (MLError( lp))         
          exit(3);
}  
MLTransferExpression(ml,lp);  
     //从Mathematica接收数据  
     //从环回连接上得到一个表达式  
     MLGetReal(ml, &result);  
     printf("The result is: %f /n",result); 
MLClose(ml); //关闭这个环回

/* quit Mathematica */

MLPutFunction( lp, "Exit", 0);

2)外部程序同内核两者如何保持同步?

外部程序同Mathematica之间的数据都是以表达式的形式传递的。例如,"5+6"应该转化为表达式Plus[5,6]才能为内核识别,同样内核向外部程序返回Mathemati
ca表达式。用户的外部程序要能够处理这些表达式。外部程序如何判断Kernel是否处理完毕,以作进一步的处理:例如,将结果打印出来。下面的代码段通过判断MLNextPacket的返回值是否为RETURNPKT来同步内核的处理过程。
while ((p = MLNextPacket(link)) && p != RETURNPKT)
MLNewPacket(link);
下表列出了MLNextPacket可能的返回值及其意义:

image

一下这段代码是对lp返回的信息的一个获取

static void read_and_print_expression( MLINK lp){ 
    kcharp_ct  s;  
    int n; 

    long i, len;  
    double r;   
    static int indent; 
    switch( MLGetNext( lp)) { 
  case MLTKSYM:     
    MLGetSymbol( lp, &s);        printf( "%s ", s);     
    MLDisownSymbol( lp, s);        break;   
  case MLTKSTR:        MLGetString( lp, &s);       
    printf( "/"%s/" ", s);        MLDisownString( lp, s);        break;  
  case MLTKINT:
    MLGetInteger( lp, &n);        printf( "%d ", n);        break;  
  case MLTKREAL:     
    MLGetReal( lp, &r);        printf( "%g ", r);        break;  
  case MLTKFUNC:        indent += 3; //对返回MLTKFUNC的处理相对复杂一些:输出程序先调用MLGetArgCount得到了该复合
                                                        //函数的参数个数,然后递归调用read_and_print_expression本身将每个参数输出。注意,indent是控制缩进格式的变量;
    printf( "/n %*.*s", indent, indent, "");        if( MLGetArgCount( lp, &len) == 0){              exit(4);       }
    else{            read_and_print_expression( lp);            printf( "[");            for( i = 1; i <= len; ++i){ 
        read_and_print_expression( lp);                if( i != len) printf( ", ");            }            printf( "]");        }
    indent -= 3;        break;  
  case MLTKERROR:    default:          exit(5); 
    }
}

3)计算一个代数方程式ax+b=0,其中x是方程的变元。

image

image

int main( int argc, char* argv[])

{

     char* s="3+6";  
     char* q="(x-1)^2==0";
     int pkt; 
     double result;
     init_and_openlink( argc, argv);
     MLActivate(lp);
    //Solve[a*x + b == 0, x]
      printf( "Computing... /n");     
      MLPutFunction(lp,"Solve", 2);    
      MLPutFunction(lp,"Equal", 2);     
      MLPutFunction(lp,"Plus", 2);       
      MLPutFunction(lp,"Times",2);        
      MLPutSymbol(lp,"a");          
      MLPutSymbol(lp,"x");           
      MLPutSymbol(lp,"b");         
      MLPutInteger(lp,0);   
      MLPutSymbol(lp,"x");  
      MLEndPacket(lp);  
      while( (pkt = MLNextPacket( lp), pkt) && pkt != RETURNPKT)
      {     
      MLNewPacket( lp);       
      if (MLError( lp))          
         exit(3);  
      }  
      read_and_print_expression(lp); 

    /* quit Mathematica */

    MLPutFunction( lp, "Exit", 0);

    return 0;

}

image

 

4)表达式:

w=x+5y-8z

MLPutFunction(lp,"Plus", 3); //目标函数w=x+5y-8z
MLPutSymbol(lp,"x");
MLPutFunction(lp,"Times", 2);
MLPutInteger(lp,5);
MLPutSymbol(lp,"y");
MLPutFunction(lp,"Times", 2);
MLPutInteger(lp,-8);
MLPutSymbol(lp,"z");

 

image

展开式:

Cell[BoxData[

RowBox[{"DSolve", "[",

RowBox[{

RowBox[{

RowBox[{

SuperscriptBox["y", "/[Prime]",

MultilineFunction->None], "[", "x", "]"}], "==",

RowBox[{"a", " ",

RowBox[{"y", "[", "x", "]"}]}]}], ",",

RowBox[{"y", "[", "x", "]"}], ",", "x"}], "]"}]], "Input"]

image

原创粉丝点击