I would like to understand how expression templates work in Eigen. I understood that the sum of two dynamical double vectors is performed by something which looks like this :
CwiseBinaryOp< internal::scalar_sum_op<double>, VectorXd const, VectorXd const > operator+(VectorXd const & lhs, VectorXd const & rhs);
I also understood how the difference of two vectors is implemented.
I have two questions.
1. How does the product of a vector by a scalar work?
I have noticed that
CwiseBinaryOp< internal::scalar_product_op<double>, VectorXd const, VectorXd const >
exists but I have the feeling that it is only designed to perform componentwise operations between two vectors. Does it mean that the product of a vector by a scalar correspond to a unary operator, say
CwiseUnaryOp< internal::scalar_product_op<double>, VectorXd const, VectorXd const > ?
2. Can template expressions be built from mixed operations?
For example, in an expression like
x = u + (2*v + (v-w))
is it true that these operations are performed in a nested way like this?
v-w
leads to the construction of an instance of E12*v
leads to the construction of an instance of E22*v + (v-w)
leads to the construction of an instance of E3u + (2*v + (u-w))
leads to the construction of an instance of E4x = u + (2*v + (v-w))
calls
the constructor
VectorXd(E4 const &);
or the overloading
VectorXd & operator=(E4 const &);
which evaluates the tree built from the previous steps, with the following aliases:
using diff = internal::scalar_difference_op<double>;
using prod = internal::scalar_product_op<double>;
using sum = internal::scalar_sum_op<double>;
using E1 = CwiseBinaryOp< diff, VectorXd const, VectorXd const >;
using E2 = CwiseUnaryOp< prod, VectorXd const >;
using E3 = CwiseBinaryOp< sum, E1 const, E2 const >;