/* example.cpp , example file for mathexpr.cpp 2.0 Copyright (c) 1997-2000 Yann OLLIVIER Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include #include "mathexpr.h" // Compilation : // g++ mathexpr.cpp example.cpp -lm int main() { // The number that the variable "x" will point to double x; // Creates a variable named "x" and which value will be x RVar xvar ( "x" , &x ); // Asks for a fomula depending on the variable x, e.g. "sin 2x" char s[500]=""; printf("Enter a formula depending on the variable x:\n"); gets(s); // Creates an operation with this formula. The operation depends on one // variable, which is xvar; the third argument is an array of pointers // to variables; the previous argument is its size RVar* vararray[1]; vararray[0]=&xvar; ROperation op ( s, 1, vararray ); // Affects (indirectly) a value to xvar x=3; // Printfs the value of the formula for x=3; printf("%s = %G for x=3\n\n", op.Expr(), op.Val() ); // Creates a function name which can be used in later functions to refer to the operation op(x) RFunction f (op, &xvar); f.SetName("f"); // Creates a second variable named y, and a formula depending on both x and y double y; RVar yvar ( "y" , &y ); RVar* vararray2[2]; // table of variables containing the adresses of xvar and yvar vararray2[0]=&xvar; vararray2[1]=&yvar; // Asks for a formula using x, y and the already-defined function f, e.g. x+f(3y) printf("Enter a formula using x, y and the function f(x): x -> %s that you just entered, e.g. x+f(3y) :\n", op.Expr() ); gets(s); RFunction* funcarray[1]; funcarray[0]=&f; ROperation op2 ( (char*)s , 2 , vararray2 , 1, funcarray ); // vararray2 is a RVar* array with two elements // funcarray is a RFunction* array with one element y=5; printf("Value for x=3, y=5 : %G\n", op2.Val() ); // Turns the last expression into a function of x and y RFunction g(op2, 2, vararray2); g.SetName("g"); // Here is another way to do it double z,t; RVar zvar("z", &z), tvar("t", &t); ROperation op3,zop,top; zop=zvar; top=tvar; // constructs, from a variable, the operation returning its value op3=g( (zop+top, top^2) ); // Ready-to-use ; needs two pairs of ( ) // Now op3 contains the operation op2 with x replaced with z+t, and y replaced with t^2 z=5;t=7; printf("\nLet g be the function g : x,y -> %s\n", op2.Expr() ); printf("Value of %s for z=5,t=7:\n %G\n", op3.Expr(), op3.Val() ); ROperation dopdt = op3.Diff(tvar); // Computes the derivative of op3 w.r.t t printf("Value of d/dt (g(z+t,t^2)) = %s for z=5,t=7:\n %G\n", dopdt.Expr(), dopdt.Val() ); return 0; }