src/thirdparty/mathexpr/example_c.cpp File Reference

#include <stdio.h>
#include "mathexpr_c.h"

Include dependency graph for example_c.cpp:

Go to the source code of this file.

Functions

int main ()


Function Documentation

int main (  ) 

Definition at line 29 of file example_c.cpp.

References COperation::Diff(), COperation::DiffConj(), double_complex, COperation::Expr(), PrettyPrint(), CFunction::SetName(), COperation::Val(), x, and z.

00030 {
00031 
00032     // The number that the variable "x" will point to
00033     double_complex x;
00034     // Creates a variable named "x" and which value will be x
00035     CVar xvar ( "x" , &x );
00036 
00037     // Asks for a fomula depending on the variable x, e.g. "sin 2x"
00038     char s[500]="";
00039     printf("Enter a formula depending on the variable x:\n");
00040     gets(s);
00041 
00042     // Creates an operation with this formula. The operation depends on one
00043     // variable, which is xvar ; the third argument is an array of pointers
00044     // to variables; the previous argument is its size
00045     CVar* vararray[1]; vararray[0]=&xvar;
00046     COperation op ( s, 1, vararray );
00047 
00048     // Affects (indirectly) a value to xvar
00049     x=3;
00050     // Printfs the value of the formula for x=3;
00051     printf("%s = %s for x=3\n\n", op.Expr(), PrettyPrint(op.Val()) );
00052 
00053     // Creates a function name which can be used in later functions to refer to the operation op(x)
00054     CFunction f (op, &xvar); f.SetName("f");
00055 
00056     // Creates a second variable named y, and a formula depending on both x and y
00057     double_complex y;
00058     CVar yvar ( "y" , &y );
00059     CVar* vararray2[2]; // table of variables containing the adresses of xvar and yvar
00060     vararray2[0]=&xvar; vararray2[1]=&yvar;
00061 
00062     // Asks for a formula using x, y and the already-defined function f, e.g. x+f(3y)
00063     printf("Enter a formula using x, y and the function f: x |-> %s that you just entered :\n", op.Expr());
00064     gets(s);
00065     CFunction* funcarray[1]; funcarray[0]=&f;
00066     COperation op2 ( (char*)s , 2 , vararray2 , 1, funcarray );
00067     // vararray2 is a CVar* array with two elements
00068     // funcarray is a CFunction* array with one element
00069     y=5;
00070     printf("Value for x=3, y=5 : %s\n", PrettyPrint(op2.Val()) );
00071 
00072     // Turns the last expression into a function of x and y
00073     CFunction g(op2, 2, vararray2); g.SetName("g");
00074 
00075     // Here is another way to do it
00076     double_complex z,t;
00077     CVar zvar("z", &z), tvar("t", &t);
00078     COperation op3,zop,top;
00079     zop=zvar; top=tvar; // constructs, from a variable, the operation returning its value
00080 
00081     op3=g( (zop+top, top^2) ); // Ready-to-use ; needs two pairs of ( )
00082     // Now op3 contains the operation op2 with x replaced with z+t, and y replaced with t^2
00083 
00084     z=5;t=7;
00085     printf("\nLet g be the function g : x,y |-> %s\n", op2.Expr());
00086     printf("Value of %s for z=5,t=7:\n %s\n", op3.Expr(), PrettyPrint(op3.Val()) );
00087 
00088     COperation dopdt=op3.Diff(tvar); // Computes the derivative of op3 w.r.t t
00089     printf("Value of d/dt (g(z+t,t^2)) = %s for z=5,t=7:\n %s\n", dopdt.Expr(), PrettyPrint(dopdt.Val()) );
00090     COperation dopdtbar=op3.DiffConj(tvar); // Computes the derivative of op3 w.r.t the conjugate of t
00091     printf("Value of d/dtbar (g(z+t,t^2)) = %s for z=5,t=7:\n %s\n", dopdtbar.Expr(), PrettyPrint(dopdtbar.Val()) );
00092 
00093     return 0;
00094 }

Here is the call graph for this function:


Generated on Sat Mar 15 23:07:55 2008 for Armagetron Advanced by  doxygen 1.5.4