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

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