| 26 | // ---------------------------------------------------------------------------------------- |
| 27 | |
| 28 | int main() |
| 29 | { |
| 30 | |
| 31 | // Declare some variables used below |
| 32 | matrix<double,3,1> y; |
| 33 | matrix<double,3,3> M; |
| 34 | matrix<double> x; |
| 35 | |
| 36 | // set all elements to 1 |
| 37 | y = 1; |
| 38 | M = 1; |
| 39 | |
| 40 | |
| 41 | // ------------------------- Template Expressions ----------------------------- |
| 42 | // Now I will discuss the "template expressions" technique and how it is |
| 43 | // used in the matrix object. First consider the following expression: |
| 44 | x = y + y; |
| 45 | |
| 46 | /* |
| 47 | Normally this expression results in machine code that looks, at a high |
| 48 | level, like the following: |
| 49 | temp = y + y; |
| 50 | x = temp |
| 51 | |
| 52 | Temp is a temporary matrix returned by the overloaded + operator. |
| 53 | temp then contains the result of adding y to itself. The assignment |
| 54 | operator copies the value of temp into x and temp is then destroyed while |
| 55 | the blissful C++ user never sees any of this. |
| 56 | |
| 57 | This is, however, totally inefficient. In the process described above |
| 58 | you have to pay for the cost of constructing a temporary matrix object |
| 59 | and allocating its memory. Then you pay the additional cost of copying |
| 60 | it over to x. It also gets worse when you have more complex expressions |
| 61 | such as x = round(y + y + y + M*y) which would involve the creation and copying |
| 62 | of 5 temporary matrices. |
| 63 | |
| 64 | All these inefficiencies are removed by using the template expressions |
| 65 | technique. The basic idea is as follows, instead of having operators and |
| 66 | functions return temporary matrix objects you return a special object that |
| 67 | represents the expression you wish to perform. |
| 68 | |
| 69 | So consider the expression x = y + y again. With dlib::matrix what happens |
| 70 | is the expression y+y returns a matrix_exp object instead of a temporary matrix. |
| 71 | The construction of a matrix_exp does not allocate any memory or perform any |
| 72 | computations. The matrix_exp however has an interface that looks just like a |
| 73 | dlib::matrix object and when you ask it for the value of one of its elements |
| 74 | it computes that value on the spot. Only in the assignment operator does |
| 75 | someone ask the matrix_exp for these values so this avoids the use of any |
| 76 | temporary matrices. Thus the statement x = y + y is equivalent to the following |
| 77 | code: |
| 78 | // loop over all elements in y matrix |
| 79 | for (long r = 0; r < y.nr(); ++r) |
| 80 | for (long c = 0; c < y.nc(); ++c) |
| 81 | x(r,c) = y(r,c) + y(r,c); |
| 82 | |
| 83 | |
| 84 | This technique works for expressions of arbitrary complexity. So if you typed |
| 85 | x = round(y + y + y + M*y) it would involve no temporary matrices being created |
nothing calls this directly
no test coverage detected