Example: Basic Handling
ViennaMath defines a set of types for representing mathematical primitives. Most importantly, the type viennamath::variable models a mathematical variable, i.e. it evaluates to a value of a vector at some later stage:
Run Time Evaluations
viennamath::variable x(0); // represents zeroth entry in a vector viennamath::variable y(1); // represents first entry in a vector viennamath::variable z(2); // represents second entry in a vector viennamath::expr f = x + y - z; // a compound expression std::vector<double> p(3); // a vector for evaluation p[0] = 1.0; p[1] = 2.0; p[2] = 4.0; // evaluations std::cout << f << std::endl; //print the symbolic expression f std::cout << viennamath::eval(f, p) << std::endl; //prints -1.0 std::cout << viennamath::eval(f * y, viennamath::make_vector(1.0, 2.0, 4.0) // convenience function ) << std::endl; //prints -2.0 std::cout << viennamath::eval(viennamath::sqrt(x+y), p); //prints 1.73
The code snippet above uses a pure runtime evaluation. However, if the involved numerical constants are integers (or possibly fractional numbers) and known at compile time, an evaluation can also be carried out at compile time. This also requires the use of dedicated compile time representations of variables and constants. In addition, note that the resulting compile time expressions cannot be assigned to a common expression object, unless the exact type is specified.
Compile Time Evaluations
viennamath::ct_variable<0> x; // represents zeroth entry in a vector viennamath::ct_variable<1> y; // represents first entry in a vector viennamath::ct_variable<2> z; // represents second entry in a vector viennamath::ct_constant<1> c1; // compile time representation of '1' viennamath::ct_constant<2> c2; // compile time representation of '2' viennamath::ct_constant<4> c4; // compile time representation of '4' // evaluations std::cout << x + y - z << std::endl; //print the symbolic expression std::cout << viennamath::eval(x+y-z, viennamath::make_vector(c1, c2, c4) ) << std::endl; //prints -1 (evaluated at compile time)