src/thirdparty/mathexpr/example_c.cpp

Go to the documentation of this file.
00001 /*
00002 
00003 example.cpp , example file for mathexpr.cpp 2.0
00004 
00005 Copyright Yann OLLIVIER 1997-2000
00006 
00007 This software may be freely distributed as is, including this whole notice.
00008 
00009 It may be modified, but any modification should include this whole notice as 
00010 is and a description of the changes made.
00011 
00012 This software may not be sold.
00013 
00014 This software or any modified version of it may be freely used in free
00015 programs. The program should include a copy of this whole notice.
00016 If you want to use it in a program you sell, contact me
00017 
00018 This software comes with absolutely no warranty.
00019 
00020 */
00021 
00022 #include <stdio.h>
00023 #include "mathexpr_c.h"
00024 
00025 // Compilation :
00026 // g++ mathexpr_c.cpp example_c.cpp
00027 // (A simple call to gcc does not work with double_complex.h)
00028 
00029 int main()
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 }

Generated on Sat Mar 15 22:55:57 2008 for Armagetron Advanced by  doxygen 1.5.4