1 \page SMatrixDoc SMatrix
Class Properties
3 The
template ROOT::Math::SMatrix class has 4
template parameters which define, at compile time, its properties. These are:
5 *
type of the contained elements,
T,
for example _float_ or _double_;
8 * representation
type (\ref MatRep). This is
a class describing the underlined storage
model of the Matrix. Presently exists only two types of
this class:
9 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]
10 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]
14 The following constructors are available to create
a matrix:
16 * Default constructor
for a zero matrix (all elements
equal to zero).
17 * Constructor of an identity matrix.
18 *
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.
19 * 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.
20 * 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.
21 * 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.
23 Here 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.
26 // typedef definitions used in the following declarations
27 typedef ROOT::Math::SMatrix<double,3> SMatrix33;
28 typedef ROOT::Math::SMatrix<double,2> SMatrix22;
29 typedef ROOT::Math::SMatrix<double,3,3,ROOT::Math::MatRepSym<double,3> > SMatrixSym3;
30 typedef ROOT::Math::SVector>double,2> SVector2;
31 typedef ROOT::Math::SVector>double,3> SVector3;
32 typedef ROOT::Math::SVector>double,6> SVector6;
34 SMatrix33 m0; // create a zero 3x3 matrix
35 // create an 3x3 identity matrix
36 SMatrix33 i = ROOT::Math::SMatrixIdentity();
37 double a[9] = {1,2,3,4,5,6,7,8,9}; // input matrix data
38 SMatrix33 m(a,9); // create a matrix using the a[] data
39 // this will produce the 3x3 matrix
46 Example to create a symmetric matrix from an _std::vector_:
49 std::vector<double> v(6);
50 for (int i = 0; i<6; ++i) v[i] = double(i+1);
51 SMatrixSym3 s(v.begin(),v.end())
52 // this will produce the symmetric matrix
57 // create a a general matrix from a symmetric matrix. The opposite will not compile
62 Example to create a symmetric matrix from a ROOT::Math::SVector contining the lower/upper data block:
65 ROOT::Math::SVectorr<double, 6> v(1,2,3,4,5,6);
66 SMatrixSym3 s1(v); // lower block (default)
67 // this will produce the symmetric matrix
72 SMatrixSym3 s2(v,false); // upper block
73 // this will produce the symmetric matrix
80 ### Accessing and Setting Methods
82 The 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.
83 The 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).
87 m(0,0) = 1; // set the element in first row and first column
88 *(m.**begin**()+1) = 2; // set the second element (0,1)
89 double d[9]={1,2,3,4,5,6,7,8,9};
90 m.SetElements(d,d+9); // set the d[] values in m
92 double x = m(2,1); // return the element in third row and first column
93 x = m.**apply**(7); // return the 8-th element (row=2,col=1)
94 x = *(m.**begin**()+7); // return the 8-th element (row=2,col=1)
95 // symmetric matrices (note the difference in behavior between apply and the iterators)
96 x = *(m.**begin**()+4) // return the element (row=2,col=1).
97 x = m.**apply**(7); // returns again the (row=2,col=1) element
101 There 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:
108 // place a vector of size 2 in the first row starting from element (0,1) : m(0,1) = v2[0]
109 m.**Place_in_row**(v2,0,1);
110 // place the vector in the second column from (0,1) : m(0,1) = v2[0]
111 m.**Place in_col**(v2,0,1);
113 // place the sub-matrix m2 in m starting from the element (1,1) : m(1,1) = m2(0,0)
114 m.**Place_at**(m2,1,1);
116 // set v3 as the diagonal elements of m : m(i,i) = v3[i] for i=0,1,2
117 m.**SetDiagonal**(v3)
121 The const methods retrieving contents (getting slices of a matrix) are:
124 a = {1,2,3,4,5,6,7,8,9};
126 SVector3 irow = m.**Row**(0); // return as vector the first matrix row
127 SVector3 jcol = m.**Col**(1); // return as vector the second matrix column
128 // return a slice of the first row from element (0,1) : r2[0] = m(0,1); r2[1] = m(0,2)
129 SVector2 r2 = m.**SubRow**<SVector2> (0,1);
130 // return a slice of the second column from element (0,1) : c2[0] = m(0,1); c2[1] = m(1,1);
131 SVector2 c2 = m.**SubCol**<SVector2> (1,0);
132 // return a sub-matrix 2x2 with the upper left corner at the values (1,1)
133 SMatrix22 subM = m.**Sub**<SMatrix22> (1,1);
134 // return the diagonal element in a SVector
135 SVector3 diag = m.**Diagonal**();
136 // return the upper(lower) block of the matrix m
137 SVector6 vub = m.**UpperBlock**(); // vub = [ 1, 2, 3, 5, 6, 9 ]
138 SVector6 vlb = m.**LowerBlock**(); // vlb = [ 1, 4, 5, 7, 8, 9 ]
142 ### Linear Algebra Functions
144 Only limited linear algebra functionality is available for SMatrix. It is possible
145 for squared matrices NxN, to find the inverse or to calculate the determinant.
146 Different inversion algorithms are used if the matrix is smaller than 6x6 or if it
147 is symmetric. In the case of a small matrix, a faster direct inversion is used.
148 For a large (N > 6) symmetric matrix the Bunch-Kaufman diagonal pivoting method
149 is used while for a large (N > 6) general matrix an LU factorization is performed
150 using the same algorithm as in the CERNLIB routine
151 [dinv](https://cern-tex.web.cern.ch/cern-tex/shortwrupsdir/f010/top.html).
154 // Invert a NxN matrix. The inverted matrix replace the existing one and returns if the result is successful
155 bool ret = m.**Invert**()
156 // return the inverse matrix of m. If the inversion fails ifail is different than zero
158 mInv = m.**Inverse**(ifail);
162 The determinant of a square matrix can be obtained as follows:
166 // calculate the determinant modifying the matrix content. Returns if the calculation was successful
167 bool ret = m.**Det**(det);
168 // calculate the determinant using a temporary matrix but preserving the matrix content
169 bool ret = n.**Det2**(det);
173 For additional Matrix functionality see the \ref MatVecFunctions page
bool equal(double d1, double d2, double stol=10000)
MatRepSym Matrix storage representation for a symmetric matrix of dimension NxN This class is a templ...
SMatrix: a generic fixed size D1 x D2 Matrix class.
Expression wrapper class for Matrix objects.
void Copy(void *source, void *dest)
SVector: a generic fixed size Vector class.