7The template ROOT::Math::SMatrix class has 4 template parameters which define, at compile time, its properties. These are:
8
9* type of the contained elements, T, for example _float_ or _double_;
10* number of rows;
11* number of columns;
12* representation type (\ref MatRep). This is a class describing the underlined storage model of the Matrix. Presently exists only two types of this class:
13 1. ROOT::Math::MatRepStd for a general nrows x ncols matrix. This class is itself a template on the contained type T, the number of rows and the number of columns. Its data member is an array T[nrows*ncols] containing the matrix data. The data are stored in the row-major C convention. For example, for a matrix, M, of size 3x3, the data \f$ \left[a_0,a_1,a_2,.......,a_7,a_8 \right] \f$ are stored in the following order: \f[ M = \left( \begin{array}{ccc} a_0 & a_1 & a_2 \\ a_3 & a_4 & a_5 \\ a_6 & a_7 & a_8 \end{array} \right) \f]
14 2. ROOT::Math::MatRepSym for a symmetric matrix of size NxN. This class is a template on the contained type and on the symmetric matrix size, N. It has as data member an array of type T of size N*(N+1)/2, containing the lower diagonal block of the matrix. The order follows the lower diagonal block, still in a row-major convention. For example for a symmetric 3x3 matrix the order of the 6 elements \f$ \left[a_0,a_1.....a_5 \right]\f$ is: \f[ M = \left( \begin{array}{ccc} a_0 & a_1 & a_3 \\ a_1 & a_2 & a_4 \\ a_3 & a_4 & a_5 \end{array} \right) \f]
15
16### Creating a matrix
17
18The following constructors are available to create a matrix:
19
20* Default constructor for a zero matrix (all elements equal to zero).
21* Constructor of an identity matrix.
22* Copy constructor (and assignment) for a matrix with the same representation, or from a different one when possible, for example from a symmetric to a general matrix.
23* Constructor (and assignment) from a matrix expression, like D = A*B + C. Due to the expression template technique, no temporary objects are created in this operation. In the case of an operation like A = A*B + C, a temporary object is needed and it is created automatically to store the intermediary result in order to preserve the validity of this operation.
24* Constructor from a generic STL-like iterator copying the data referred by the iterator, following its order. It is both possible to specify the _begin_ and _end_ of the iterator or the _begin_ and the size. In case of a symmetric matrix, it is required only the triangular block and the user can specify whether giving a block representing the lower (default case) or the upper diagonal part.
25* Constructor of a symmetric matrix NxN passing a ROOT::Math::SVector with dimension N*(N+1)/2 containing the lower (or upper) block data elements.
26
27Here are some examples on how to create a matrix. We use _typedef's_ in the following examples to avoid the full C++ names for the matrix classes. Notice that for a general matrix the representation has the default value, ROOT::Math::MatRepStd, and it is not needed to be specified. Furtheremore, for a general square matrix, the number of column may be as well omitted.
28
29<pre>_// typedef definitions used in the following declarations_
80The matrix elements can be set using the _operator()(irow,icol)_, where irow and icol are the row and column indexes or by using the iterator interface. Notice that the indexes start from zero and not from one as in FORTRAN. All the matrix elements can be set also by using the ROOT::Math::SetElements function passing a generic iterator.
81The elements can be accessed by these same methods and also by using the ROOT::Math::SMatrix::apply function. The _apply(i)_ function has exactly the same behavior for general and symmetric matrices, in contrast to the iterator access methods which behave differently (it follows the data order).
82
83<pre>SMatrix33 m;
84m(0,0) = 1; _ // set the element in first row and first column_
85*(m.**begin**()+1) = 2; _// set the second element (0,1)_
86double d[9]={1,2,3,4,5,6,7,8,9};
87m.SetElements(d,d+9); _// set the d[] values in m_
88
89double x = m(2,1); _// return the element in third row and first column_
90x = m.**apply**(7); _// return the 8-th element (row=2,col=1)_
91x = *(m.**begin**()+7); _// return the 8-th element (row=2,col=1)_
92_// symmetric matrices (note the difference in behavior between apply and the iterators)_
93x = *(m.**begin**()+4) _// return the element (row=2,col=1)._
94x = m.**apply**(7); _// returns again the (row=2,col=1) element_
95</pre>
96
97There are methods to place and/or retrieve ROOT::Math::SVector objects as rows or columns in (from) a matrix. In addition one can put (get) a sub-matrix as another ROOT::Math::SMatrix object in a matrix. If the size of the the sub-vector or sub-matrix are larger than the matrix size a static assert ( a compilation error) is produced. The non-const methods are:
98
99<pre>
100
101SMatrix33 m;
102SVector2 v2(1,2);
103_// place a vector of size 2 in the first row starting from element (0,1) : m(0,1) = v2[0]_
104m.**Place_in_row**(v2,0,1);
105_// place the vector in the second column from (0,1) : m(0,1) = v2[0] _
106m.**Place in_col**(v2,0,1);
107SMatrix22 m2;
108_// place the sub-matrix m2 in m starting from the element (1,1) : m(1,1) = m2(0,0) _
109m.**Place_at**(m2,1,1);
110SVector3 v3(1,2,3);
111_// set v3 as the diagonal elements of m : m(i,i) = v3[i] for i=0,1,2_
112m.**SetDiagonal**(v3) </pre>
113
114The const methods retrieving contents (getting slices of a matrix) are:
115
116<pre>a = {1,2,3,4,5,6,7,8,9};
117SMatrix33 m(a,a+9);
118SVector3 irow = m.**Row**(0); _// return as vector the first matrix row_
119SVector3 jcol = m.**Col**(1); _// return as vector the second matrix column_
120_// return a slice of the first row from element (0,1) : r2[0] = m(0,1); r2[1] = m(0,2)_
121SVector2 r2 = m.**SubRow**<SVector2> (0,1);
122_// return a slice of the second column from element (0,1) : c2[0] = m(0,1); c2[1] = m(1,1);_
123SVector2 c2 = m.**SubCol**<SVector2> (1,0);
124_// return a sub-matrix 2x2 with the upper left corner at the values (1,1)_
125SMatrix22 subM = m.**Sub**<SMatrix22> (1,1);
126_// return the diagonal element in a SVector_
127SVector3 diag = m.**Diagonal**();
128_// return the upper(lower) block of the matrix m_