Content  


Example: Compile Time Integration

Besides differentiation routines and numerical integration, ViennaMath offers the analytic integration of polynomials at compile time. Thus, costly integration at runtime can be avoided if the integration domain and the integrand is already known at compile time. An example of such a case are finite element methods, where the entries in local element matrices stem from such integrals and are typically computed by hand. With ViennaMath, the compiler can do the job:

Compile Time Integration
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<0> c0; // compile time representation of '0'
viennamath::ct_constant<1> c1; // compile time representation of '1'
 
// integrate x*x over [0,1] with respect to x:
std::cout << viennamath::integrate( viennamath::make_interval(c0, c1),
                                    x * x,
                                    x) << std::endl; //prints 1/3
// integrate x*y over [0,1] with respect to x:
std::cout << viennamath::integrate( viennamath::make_interval(c0, c1),
                                    x * y,
                                    x) << std::endl; //prints y/2
 
// Integration can also be nested:
// integrate x*y over the triangle with vertices (0,0), (1,0) and (0,1)
std::cout << viennamath::integrate(viennamath::make_interval(c0, c1),
                                   viennamath::integrate(viennamath::make_interval(0, c1-x),
                                                         x * y,
                                                         y),
                                   x) << std::endl; //prints 1/24