1 namespace Eigen { 2 3 /** \page TopicFunctionTakingEigenTypes Writing Functions Taking Eigen Types as Parameters 4 5 Eigen's use of expression templates results in potentially every expression being of a different type. If you pass such an expression to a function taking a parameter of type Matrix, your expression will implicitly be evaluated into a temporary Matrix, which will then be passed to the function. This means that you lose the benefit of expression templates. Concretely, this has two drawbacks: 6 \li The evaluation into a temporary may be useless and inefficient; 7 \li This only allows the function to read from the expression, not to write to it. 8 9 Fortunately, all this myriad of expression types have in common that they all inherit a few common, templated base classes. By letting your function take templated parameters of these base types, you can let them play nicely with Eigen's expression templates. 10 11 <b>Table of contents</b> 12 - \ref TopicFirstExamples 13 - \ref TopicPlainFunctionsWorking 14 - \ref TopicPlainFunctionsFailing 15 - \ref TopicResizingInGenericImplementations 16 - \ref TopicSummary 17 18 \section TopicFirstExamples Some First Examples 19 20 This section will provide simple examples for different types of objects Eigen is offering. Before starting with the actual examples, we need to recapitulate which base objects we can work with (see also \ref TopicClassHierarchy). 21 22 \li MatrixBase: The common base class for all dense matrix expressions (as opposed to array expressions, as opposed to sparse and special matrix classes). Use it in functions that are meant to work only on dense matrices. 23 \li ArrayBase: The common base class for all dense array expressions (as opposed to matrix expressions, etc). Use it in functions that are meant to work only on arrays. 24 \li DenseBase: The common base class for all dense matrix expression, that is, the base class for both \c MatrixBase and \c ArrayBase. It can be used in functions that are meant to work on both matrices and arrays. 25 \li EigenBase: The base class unifying all types of objects that can be evaluated into dense matrices or arrays, for example special matrix classes such as diagonal matrices, permutation matrices, etc. It can be used in functions that are meant to work on any such general type. 26 27 <b> %EigenBase Example </b><br/><br/> 28 Prints the dimensions of the most generic object present in Eigen. It coulde be any matrix expressions, any dense or sparse matrix and any array. 29 <table class="example"> 30 <tr><th>Example:</th><th>Output:</th></tr> 31 <tr><td> 32 \include function_taking_eigenbase.cpp 33 </td> 34 <td> 35 \verbinclude function_taking_eigenbase.out 36 </td></tr></table> 37 <b> %DenseBase Example </b><br/><br/> 38 Prints a sub-block of the dense expression. Accepts any dense matrix or array expression, but no sparse objects and no special matrix classes such as DiagonalMatrix. 39 \code 40 template <typename Derived> 41 void print_block(const DenseBase<Derived>& b, int x, int y, int r, int c) 42 { 43 std::cout << "block: " << b.block(x,y,r,c) << std::endl; 44 } 45 \endcode 46 <b> %ArrayBase Example </b><br/><br/> 47 Prints the maximum coefficient of the array or array-expression. 48 \code 49 template <typename Derived> 50 void print_max_coeff(const ArrayBase<Derived> &a) 51 { 52 std::cout << "max: " << a.maxCoeff() << std::endl; 53 } 54 \endcode 55 <b> %MatrixBase Example </b><br/><br/> 56 Prints the inverse condition number of the given matrix or matrix-expression. 57 \code 58 template <typename Derived> 59 void print_inv_cond(const MatrixBase<Derived>& a) 60 { 61 const typename JacobiSVD<typename Derived::PlainObject>::SingularValuesType& 62 sing_vals = a.jacobiSvd().singularValues(); 63 std::cout << "inv cond: " << sing_vals(sing_vals.size()-1) / sing_vals(0) << std::endl; 64 } 65 \endcode 66 <b> Multiple templated arguments example </b><br/><br/> 67 Calculate the Euclidean distance between two points. 68 \code 69 template <typename DerivedA,typename DerivedB> 70 typename DerivedA::Scalar squaredist(const MatrixBase<DerivedA>& p1,const MatrixBase<DerivedB>& p2) 71 { 72 return (p1-p2).squaredNorm(); 73 } 74 \endcode 75 Notice that we used two template parameters, one per argument. This permits the function to handle inputs of different types, e.g., 76 \code 77 squaredist(v1,2*v2) 78 \endcode 79 where the first argument \c v1 is a vector and the second argument \c 2*v2 is an expression. 80 <br/><br/> 81 82 These examples are just intended to give the reader a first impression of how functions can be written which take a plain and constant Matrix or Array argument. They are also intended to give the reader an idea about the most common base classes being the optimal candidates for functions. In the next section we will look in more detail at an example and the different ways it can be implemented, while discussing each implementation's problems and advantages. For the discussion below, Matrix and Array as well as MatrixBase and ArrayBase can be exchanged and all arguments still hold. 83 84 \section TopicPlainFunctionsWorking In which cases do functions taking plain Matrix or Array arguments work? 85 86 Let's assume one wants to write a function computing the covariance matrix of two input matrices where each row is an observation. The implementation of this function might look like this 87 \code 88 MatrixXf cov(const MatrixXf& x, const MatrixXf& y) 89 { 90 const float num_observations = static_cast<float>(x.rows()); 91 const RowVectorXf x_mean = x.colwise().sum() / num_observations; 92 const RowVectorXf y_mean = y.colwise().sum() / num_observations; 93 return (x.rowwise() - x_mean).transpose() * (y.rowwise() - y_mean) / num_observations; 94 } 95 \endcode 96 and contrary to what one might think at first, this implementation is fine unless you require a genric implementation that works with double matrices too and unless you do not care about temporary objects. Why is that the case? Where are temporaries involved? How can code as given below compile? 97 \code 98 MatrixXf x,y,z; 99 MatrixXf C = cov(x,y+z); 100 \endcode 101 In this special case, the example is fine and will be working because both parameters are declared as \e const references. The compiler creates a temporary and evaluates the expression x+z into this temporary. Once the function is processed, the temporary is released and the result is assigned to C. 102 103 \b Note: Functions taking \e const references to Matrix (or Array) can process expressions at the cost of temporaries. 104 105 \section TopicPlainFunctionsFailing In which cases do functions taking a plain Matrix or Array argument fail? 106 107 Here, we consider a slightly modified version of the function given above. This time, we do not want to return the result but pass an additional non-const paramter which allows us to store the result. A first naive implementation might look as follows. 108 \code 109 // Note: This code is flawed! 110 void cov(const MatrixXf& x, const MatrixXf& y, MatrixXf& C) 111 { 112 const float num_observations = static_cast<float>(x.rows()); 113 const RowVectorXf x_mean = x.colwise().sum() / num_observations; 114 const RowVectorXf y_mean = y.colwise().sum() / num_observations; 115 C = (x.rowwise() - x_mean).transpose() * (y.rowwise() - y_mean) / num_observations; 116 } 117 \endcode 118 When trying to execute the following code 119 \code 120 MatrixXf C = MatrixXf::Zero(3,6); 121 cov(x,y, C.block(0,0,3,3)); 122 \endcode 123 the compiler will fail, because it is not possible to convert the expression returned by \c MatrixXf::block() into a non-const \c MatrixXf&. This is the case because the compiler wants to protect you from writing your result to a temporary object. In this special case this protection is not intended -- we want to write to a temporary object. So how can we overcome this problem? 124 125 The solution which is preferred at the moment is based on a little \em hack. One needs to pass a const reference to the matrix and internally the constness needs to be cast away. The correct implementation for C98 compliant compilers would be 126 \code 127 template <typename Derived, typename OtherDerived> 128 void cov(const MatrixBase<Derived>& x, const MatrixBase<Derived>& y, MatrixBase<OtherDerived> const & C) 129 { 130 typedef typename Derived::Scalar Scalar; 131 typedef typename internal::plain_row_type<Derived>::type RowVectorType; 132 133 const Scalar num_observations = static_cast<Scalar>(x.rows()); 134 135 const RowVectorType x_mean = x.colwise().sum() / num_observations; 136 const RowVectorType y_mean = y.colwise().sum() / num_observations; 137 138 const_cast< MatrixBase<OtherDerived>& >(C) = 139 (x.rowwise() - x_mean).transpose() * (y.rowwise() - y_mean) / num_observations; 140 } 141 \endcode 142 The implementation above does now not only work with temporary expressions but it also allows to use the function with matrices of arbitrary floating point scalar types. 143 144 \b Note: The const cast hack will only work with templated functions. It will not work with the MatrixXf implementation because it is not possible to cast a Block expression to a Matrix reference! 145 146 147 148 \section TopicResizingInGenericImplementations How to resize matrices in generic implementations? 149 150 One might think we are done now, right? This is not completely true because in order for our covariance function to be generically applicable, we want the follwing code to work 151 \code 152 MatrixXf x = MatrixXf::Random(100,3); 153 MatrixXf y = MatrixXf::Random(100,3); 154 MatrixXf C; 155 cov(x, y, C); 156 \endcode 157 This is not the case anymore, when we are using an implementation taking MatrixBase as a parameter. In general, Eigen supports automatic resizing but it is not possible to do so on expressions. Why should resizing of a matrix Block be allowed? It is a reference to a sub-matrix and we definitely don't want to resize that. So how can we incorporate resizing if we cannot resize on MatrixBase? The solution is to resize the derived object as in this implementation. 158 \code 159 template <typename Derived, typename OtherDerived> 160 void cov(const MatrixBase<Derived>& x, const MatrixBase<Derived>& y, MatrixBase<OtherDerived> const & C_) 161 { 162 typedef typename Derived::Scalar Scalar; 163 typedef typename internal::plain_row_type<Derived>::type RowVectorType; 164 165 const Scalar num_observations = static_cast<Scalar>(x.rows()); 166 167 const RowVectorType x_mean = x.colwise().sum() / num_observations; 168 const RowVectorType y_mean = y.colwise().sum() / num_observations; 169 170 MatrixBase<OtherDerived>& C = const_cast< MatrixBase<OtherDerived>& >(C_); 171 172 C.derived().resize(x.cols(),x.cols()); // resize the derived object 173 C = (x.rowwise() - x_mean).transpose() * (y.rowwise() - y_mean) / num_observations; 174 } 175 \endcode 176 This implementation is now working for parameters being expressions and for parameters being matrices and having the wrong size. Resizing the expressions does not do any harm in this case unless they actually require resizing. That means, passing an expression with the wrong dimensions will result in a run-time error (in debug mode only) while passing expressions of the correct size will just work fine. 177 178 \b Note: In the above discussion the terms Matrix and Array and MatrixBase and ArrayBase can be exchanged and all arguments still hold. 179 180 \section TopicSummary Summary 181 182 - To summarize, the implementation of functions taking non-writable (const referenced) objects is not a big issue and does not lead to problematic situations in terms of compiling and running your program. However, a naive implementation is likely to introduce unnecessary temporary objects in your code. In order to avoid evaluating parameters into temporaries, pass them as (const) references to MatrixBase or ArrayBase (so templatize your function). 183 184 - Functions taking writable (non-const) parameters must take const references and cast away constness within the function body. 185 186 - Functions that take as parameters MatrixBase (or ArrayBase) objects, and potentially need to resize them (in the case where they are resizable), must call resize() on the derived class, as returned by derived(). 187 */ 188 } 189