In collaboration with Karl Meerbergen, we developed expression templates that can be nested, called rich expression templates. The following program shows some examples of rich expression templates:
// File: rich_vector_expr.cpp #include <iostream> #include <boost/numeric/mtl/mtl.hpp> int main(int argc, char* argv[]) { using namespace mtl; typedef std::complex<double> cdouble; dense_vector<cdouble> u(10), v(10); dense_vector<double> w(10), x(10, 4.0); for (int i= 0; i < size(v); i++) v[i]= cdouble(i+1, 10-i), w[i]= 2 * i + 2; // Increment w by x // and assign the sum of--the incremented--w and v to u u= v + (w+= x); std::cout << "u is " << u << "\n"; // w= w * 3; x= 2; v= v + w + x; u= u + v; u+= v+= (w*= 3) + (x= 2); std::cout << "u is " << u << "w is " << w << "\n"; return 0; }
The first example shows the combination of an incremental assignment with a vector addition. The second statement fuses four vector expressions:
Again, all these operations are performed in one loop and each vector element is accessed exactly once.
Return to Vector Expressions Table of Content Proceed to Matrix Expressions
Rich Vector Expressions -- MTL 4 -- Peter Gottschling and Andrew Lumsdaine
-- Generated on 19 May 2009 by Doxygen 1.5.5 -- Copyright 2007 by the Trustees of Indiana University.