blob: 57ec64219ba6b5666544d97fcc049d2e062b8542 [file] [log] [blame]
Carlos Becker9d440052010-06-25 20:16:12 -04001namespace Eigen {
2
Benoit Jacob08c17c42010-07-01 20:29:13 -04003/** \page TutorialArrayClass Tutorial page 3 - The %Array class and coefficient-wise operations
Carlos Becker9d440052010-06-25 20:16:12 -04004 \ingroup Tutorial
5
Benoit Jacobe078bb22010-06-26 14:00:00 -04006\li \b Previous: \ref TutorialMatrixArithmetic
Carlos Becker82e2e8b2010-06-28 18:42:09 +01007\li \b Next: \ref TutorialBlockOperations
Benoit Jacobe078bb22010-06-26 14:00:00 -04008
Carlos Becker82e2e8b2010-06-28 18:42:09 +01009This tutorial aims to provide an overview and explanations on how to use
10Eigen's \link ArrayBase Array \endlink class
Carlos Becker9d440052010-06-25 20:16:12 -040011
12\b Table \b of \b contents
Benoit Jacob08c17c42010-07-01 20:29:13 -040013 - \ref TutorialArrayClassIntro
14 - \ref TutorialArrayClassTypes
15 - \ref TutorialArrayClassAccess
16 - \ref TutorialArrayClassAddSub
17 - \ref TutorialArrayClassMult
18 - \ref TutorialArrayClassConvert
Carlos Becker9d440052010-06-25 20:16:12 -040019
Benoit Jacob08c17c42010-07-01 20:29:13 -040020\section TutorialArrayClassIntro What is the Array class?
21
22The Array class provides general-purpose arrays, as opposed to the Matrix class which
23is intended for linear algebra. Furthermore, the Array class provides an easy way to
24perform coefficient-wise operations, which might not have a linear algebraic meaning,
Carlos Becker82e2e8b2010-06-28 18:42:09 +010025such as adding a constant to every coefficient in the array or multiplying two arrays coefficient-wise.
26
Carlos Becker9d440052010-06-25 20:16:12 -040027
Benoit Jacob08c17c42010-07-01 20:29:13 -040028\section TutorialArrayClassTypes Array types
29Array is a class template taking the same template parameters as Matrix.
30As with with, the first 3 template parameters are mandatory:
Carlos Becker9d440052010-06-25 20:16:12 -040031\code
Benoit Jacob08c17c42010-07-01 20:29:13 -040032Array<typename Scalar, int RowsAtCompileTime, int ColsAtCompileTime>
Carlos Becker9d440052010-06-25 20:16:12 -040033\endcode
Benoit Jacob08c17c42010-07-01 20:29:13 -040034And the last 3 template parameters are optional. Since this is exactly the same as for Matrix,
35we won't reexplain it and just refer to \ref TutorialMatrixClass "this page".
Carlos Becker9d440052010-06-25 20:16:12 -040036
Benoit Jacob08c17c42010-07-01 20:29:13 -040037Eigen also provides typedefs for some common cases, in a way that is similar to the Matrix typedefs
38but with some slight differences, as the word "array" is used for both 1-dimensional and 2-dimensional arrays.
39We adopt that convention that typedefs of the form ArrayNt stand for 1-dimensional arrays, where N and t are
40as in the Matrix typedefs explained on \ref TutorialMatrixClass "this page". For 2-dimensional arrays, we
41use typedefs of the form ArrayNNt. Some examples are shown in the following table:
Carlos Becker9d440052010-06-25 20:16:12 -040042
43<table class="tutorial_code" align="center">
Benoit Jacob08c17c42010-07-01 20:29:13 -040044
45 <tr>
46 <td align="center">\b Type </td>
47 <td align="center">\b Typedef </td>
48 </tr>
49
50 <tr>
51 <td> \code Array<float,Dynamic,1> \endcode </td>
52 <td> \code ArrayXf \endcode </td>
53 </tr>
54
55 <tr>
56 <td> \code Array<float,3,1> \endcode </td>
57 <td> \code Array3f \endcode </td>
58 </tr>
59
60 <tr>
61 <td> \code Array<double,Dynamic,Dynamic> \endcode </td>
62 <td> \code ArrayXXd \endcode </td>
63 </tr>
64
65 <tr>
66 <td> \code Array<double,3,3> \endcode </td>
67 <td> \code Array33d \endcode </td>
68 </tr>
69
Carlos Becker9d440052010-06-25 20:16:12 -040070</table>
71
72
Benoit Jacob08c17c42010-07-01 20:29:13 -040073\section TutorialArrayClassAccess Accessing values inside an Array
74Write and read access to coefficients of an array expression is done in the same way as with matrix expressions.
75For example:
Carlos Becker9d440052010-06-25 20:16:12 -040076
Benoit Jacob08c17c42010-07-01 20:29:13 -040077\include Tutorial_ArrayClass_accessors.cpp
Carlos Becker82e2e8b2010-06-28 18:42:09 +010078Output:
Benoit Jacob08c17c42010-07-01 20:29:13 -040079\verbinclude Tutorial_ArrayClass_accessors.out
Carlos Becker9d440052010-06-25 20:16:12 -040080
Carlos Becker82e2e8b2010-06-28 18:42:09 +010081For more information about the comma initializer, refer to \ref TutorialAdvancedInitialization "this page".
Carlos Becker9d440052010-06-25 20:16:12 -040082
83
Benoit Jacob08c17c42010-07-01 20:29:13 -040084\section TutorialArrayClassAddSub Addition and substraction
Carlos Becker9d440052010-06-25 20:16:12 -040085
Carlos Becker9d440052010-06-25 20:16:12 -040086
Benoit Jacob08c17c42010-07-01 20:29:13 -040087Adding and substracting two arrays has the same result as for Matrices.
88This is valid as long as both arrays have the same sizes:
Carlos Becker82e2e8b2010-06-28 18:42:09 +010089
90\include Tutorial_ArrayClass_addition.cpp
Carlos Becker82e2e8b2010-06-28 18:42:09 +010091Output:
92\verbinclude Tutorial_ArrayClass_addition.out
93
Benoit Jacob08c17c42010-07-01 20:29:13 -040094It is also allowed to add a scalar to each coefficient in an Array,
95providing a functionality that was not available for Matrix objects:
Carlos Becker82e2e8b2010-06-28 18:42:09 +010096
97\include Tutorial_ArrayClass_addition_scalar.cpp
Carlos Becker82e2e8b2010-06-28 18:42:09 +010098Output:
99\verbinclude Tutorial_ArrayClass_addition_scalar.out
100
101
Benoit Jacob08c17c42010-07-01 20:29:13 -0400102\subsection TutorialArrayClassMult Array multiplication
Carlos Becker82e2e8b2010-06-28 18:42:09 +0100103
Benoit Jacob08c17c42010-07-01 20:29:13 -0400104First of all, of course you can multiply an array by a scalar, this works in the same way as matrices. Where arrays
105are fundamentally different from matrices, is when you multiply two arrays together. While Matrices interprete
106multiplication as matrix product, arrays interprete multiplication as coefficient-wise product. For example:
Carlos Becker82e2e8b2010-06-28 18:42:09 +0100107
108\include Tutorial_ArrayClass_mult.cpp
Carlos Becker82e2e8b2010-06-28 18:42:09 +0100109Output:
110\verbinclude Tutorial_ArrayClass_mult.out
111
112
Carlos Becker9d440052010-06-25 20:16:12 -0400113
Benoit Jacob08c17c42010-07-01 20:29:13 -0400114\section TutorialArrayClassConvert Converting between array and matrix expressions
Carlos Becker9d440052010-06-25 20:16:12 -0400115
Benoit Jacob08c17c42010-07-01 20:29:13 -0400116It is possible to wrap a matrix expression as an array expression and conversely. This gives access to all operations
117regardless of the choice of declaring objects as arrays or as matrices.
118
119\link MatrixBase Matrix expressions \endlink have a \link MatrixBase::array() .array() \endlink method that
120'converts' them into \link ArrayBase array expressions \endlink, so that coefficient-wise operations
121can be applied easily. Conversely, \link ArrayBase array expressions \endlink
122have a \link ArrayBase::matrix() .matrix() \endlink method. As with all Eigen expression abstractions,
123this doesn't have any runtime cost (provided that you let your compiler optimize).
124
Carlos Becker82e2e8b2010-06-28 18:42:09 +0100125Both \link MatrixBase::array() .array() \endlink and \link ArrayBase::matrix() .matrix() \endlink
Benoit Jacob08c17c42010-07-01 20:29:13 -0400126can be used as \b rvalues and as \b lvalues.
Carlos Becker9d440052010-06-25 20:16:12 -0400127
Benoit Jacob08c17c42010-07-01 20:29:13 -0400128Mixing matrices and arrays in an expression is forbidden with Eigen. However,
Carlos Becker82e2e8b2010-06-28 18:42:09 +0100129it is easy to convert from one to the other with \link MatrixBase::array() .array() \endlink and
Benoit Jacob08c17c42010-07-01 20:29:13 -0400130\link ArrayBase::matrix() .matrix() \endlink.
Carlos Becker9d440052010-06-25 20:16:12 -0400131
Benoit Jacob08c17c42010-07-01 20:29:13 -0400132On the other hand, assigning a matrix expression to an array expression is allowed.
133
134\subsection TutorialArrayClassInteropMatrix Matrix to array example
135The following example shows how to use array operations on a Matrix object by employing
136the \link MatrixBase::array() .array() \endlink method:
Carlos Becker9d440052010-06-25 20:16:12 -0400137
Carlos Becker82e2e8b2010-06-28 18:42:09 +0100138<table class="tutorial_code"><tr><td>
139\include Tutorial_ArrayClass_interop_matrix.cpp
140</td>
141<td>
142Output:
143\verbinclude Tutorial_ArrayClass_interop_matrix.out
144</td></tr></table>
145
146
Benoit Jacob08c17c42010-07-01 20:29:13 -0400147\subsection TutorialArrayClassInteropArray Array to matrix example
148The following example shows how to use matrix operations with an Array
149object by employing the \link ArrayBase::matrix() .matrix() \endlink method:
Carlos Becker82e2e8b2010-06-28 18:42:09 +0100150
151<table class="tutorial_code"><tr><td>
152\include Tutorial_ArrayClass_interop_array.cpp
153</td>
154<td>
155Output:
156\verbinclude Tutorial_ArrayClass_interop_array.out
157</td></tr></table>
158
Benoit Jacob08c17c42010-07-01 20:29:13 -0400159\subsection TutorialArrayClassInteropCombination Example with combinations of .array() and .matrix()
160Here is a more advanced example:
Carlos Becker82e2e8b2010-06-28 18:42:09 +0100161
162<table class="tutorial_code"><tr><td>
163\include Tutorial_ArrayClass_interop.cpp
164</td>
165<td>
166Output:
167\verbinclude Tutorial_ArrayClass_interop.out
168</td></tr></table>
169
Benoit Jacob4d4a23c2010-06-30 10:11:55 -0400170\li \b Next: \ref TutorialBlockOperations
Carlos Becker9d440052010-06-25 20:16:12 -0400171
Benoit Jacob08c17c42010-07-01 20:29:13 -0400172*/
173
Carlos Becker9d440052010-06-25 20:16:12 -0400174}