4#ifndef ROOT_Math_MatrixFunctions
5#define ROOT_Math_MatrixFunctions
52 template <
class T,
unsigned int D>
59template <
class T,
unsigned int D1,
unsigned int D2,
class R>
60SVector<T,D1>
operator*(
const SMatrix<T,D1,D2,R>& rhs,
const SVector<T,D2>& lhs)
63 for(
unsigned int i=0; i<D1; ++i) {
64 const unsigned int rpos = i*D2;
65 for(
unsigned int j=0; j<D2; ++j) {
66 tmp[i] += rhs.apply(rpos+j) * lhs.apply(j);
81template <
unsigned int I>
83 template <
class A,
class B>
84 static inline typename A::value_type
f(
const A& lhs,
const B& rhs,
85 const unsigned int offset) {
96 template <
class A,
class B>
97 static inline typename A::value_type
f(
const A& lhs,
const B& rhs,
98 const unsigned int offset) {
99 return lhs.apply(offset) * rhs.apply(0);
106template <
class Matrix,
class Vector,
unsigned int D2>
110 typedef typename Vector::value_type
T;
120 inline typename Matrix::value_type
apply(
unsigned int i)
const {
127 return rhs_.IsInUse(p);
140template <
unsigned int I>
142 template <
class Matrix,
class Vector>
143 static inline typename Matrix::value_type
f(
const Matrix& lhs,
const Vector& rhs,
144 const unsigned int offset) {
145 return lhs.apply(Matrix::kCols*
I+offset) * rhs.apply(
I) +
156 template <
class Matrix,
class Vector>
157 static inline typename Matrix::value_type
f(
const Matrix& lhs,
const Vector& rhs,
158 const unsigned int offset) {
159 return lhs.apply(offset) * rhs.apply(0);
171template <
class Vector,
class Matrix,
unsigned int D1>
175 typedef typename Vector::value_type
T;
184 inline typename Matrix::value_type
apply(
unsigned int i)
const {
191 return lhs_.IsInUse(p);
209template <
class T,
unsigned int D1,
unsigned int D2,
class R>
219template <
class A,
class T,
unsigned int D1,
unsigned int D2,
class R>
220inline VecExpr<VectorMatrixRowOp<SMatrix<T,D1,D2,R>, VecExpr<A,T,D2>, D2>,
T, D1>
229template <
class A,
class T,
unsigned int D1,
unsigned int D2,
class R>
230inline VecExpr<VectorMatrixRowOp<Expr<A,T,D1,D2,R>, SVector<T,D2>, D2>,
T, D1>
239template <
class A,
class B,
class T,
unsigned int D1,
unsigned int D2,
class R>
240inline VecExpr<VectorMatrixRowOp<Expr<A,T,D1,D2,R>, VecExpr<B,T,D2>, D2>,
T, D1>
249template <
class T,
unsigned int D1,
unsigned int D2,
class R>
250inline VecExpr<VectorMatrixColOp<SVector<T,D1>, SMatrix<T,D1,D2,R>, D1>,
T, D2>
259template <
class A,
class T,
unsigned int D1,
unsigned int D2,
class R>
260inline VecExpr<VectorMatrixColOp<SVector<T,D1>, Expr<A,T,D1,D2,R>, D1>,
T, D2>
269template <
class A,
class T,
unsigned int D1,
unsigned int D2,
class R>
270inline VecExpr<VectorMatrixColOp<VecExpr<A,T,D1>, SMatrix<T,D1,D2,R>, D1>,
T, D2>
279template <
class A,
class B,
class T,
unsigned int D1,
unsigned int D2,
class R>
280inline VecExpr<VectorMatrixColOp<VecExpr<A,T,D1>, Expr<B,T,D1,D2,R>, D1>,
T, D2>
289template <
unsigned int I>
292 template <
class MatrixA,
class MatrixB>
293 static inline typename MatrixA::value_type
f(
const MatrixA& lhs,
295 const unsigned int offset) {
296 return lhs.apply(offset/MatrixB::kCols*MatrixA::kCols +
I) *
297 rhs.apply(MatrixB::kCols*
I + offset%MatrixB::kCols) +
302 template <
class MatrixA,
class MatrixB>
303 static inline typename MatrixA::value_type
g(
const MatrixA& lhs,
307 return lhs(i,
I) * rhs(
I , j) +
319 template <
class MatrixA,
class MatrixB>
320 static inline typename MatrixA::value_type
f(
const MatrixA& lhs,
322 const unsigned int offset) {
323 return lhs.apply(offset/MatrixB::kCols*MatrixA::kCols) *
324 rhs.apply(offset%MatrixB::kCols);
328 template <
class MatrixA,
class MatrixB>
329 static inline typename MatrixA::value_type
g(
const MatrixA& lhs,
331 unsigned int i,
unsigned int j) {
332 return lhs(i,0) * rhs(0,j);
345template <
class MatrixA,
class MatrixB,
class T,
unsigned int D>
365 return lhs_.IsInUse(p) ||
rhs_.IsInUse(p);
384template <
class T,
unsigned int D1,
unsigned int D,
unsigned int D2,
class R1,
class R2>
385inline Expr<MatrixMulOp<SMatrix<T,D1,D,R1>,
SMatrix<T,D,D2,R2>,
T,D>,
T, D1, D2,
typename MultPolicy<T,R1,R2>::RepType>
388 return Expr<MatMulOp,
T,D1,D2,
395template <
class A,
class T,
unsigned int D1,
unsigned int D,
unsigned int D2,
class R1,
class R2>
396inline Expr<MatrixMulOp<SMatrix<T,D1,D,R1>, Expr<A,T,D,D2,R2>,
T,D>,
T, D1, D2,
typename MultPolicy<T,R1,R2>::RepType>
398 typedef MatrixMulOp<SMatrix<T,D1,D,R1>,
Expr<A,T,D,D2,R2>,
T,D> MatMulOp;
399 return Expr<MatMulOp,
T,D1,D2,
406template <
class A,
class T,
unsigned int D1,
unsigned int D,
unsigned int D2,
class R1,
class R2>
407inline Expr<MatrixMulOp<Expr<A,T,D1,D,R1>, SMatrix<T,D,D2,R2>,
T,D>,
T, D1, D2,
typename MultPolicy<T,R1,R2>::RepType>
409 typedef MatrixMulOp<Expr<A,T,D1,D,R1>,
SMatrix<T,D,D2,R2>,
T,D> MatMulOp;
410 return Expr<MatMulOp,
T,D1,D2,
417template <
class A,
class B,
class T,
unsigned int D1,
unsigned int D,
unsigned int D2,
class R1,
class R2>
418inline Expr<MatrixMulOp<Expr<A,T,D1,D,R1>, Expr<B,T,D,D2,R2>,
T,D>,
T, D1, D2,
typename MultPolicy<T,R1,R2>::RepType>
420 typedef MatrixMulOp<Expr<A,T,D1,D,R1>,
Expr<B,T,D,D2,R2>,
T,D> MatMulOp;
430template <
class MatrixA,
class MatrixB,
unsigned int D>
434 MatrixMulOp(
const MatrixA& lhs,
const MatrixB& rhs) :
441 inline typename MatrixA::value_type
apply(
unsigned int i)
const {
454template <
class T,
unsigned int D1,
unsigned int D,
unsigned int D2,
class R1,
class R2>
455inline Expr<MatrixMulOp<SMatrix<T,D1,D,R1>, SMatrix<T,D,D2,R2>, D>,
T, D1, D2,
typename MultPolicy<T,R1,R2>::RepType>
456 operator*(
const SMatrix<T,D1,D,R1>& lhs,
const SMatrix<T,D,D2,R2>& rhs) {
457 typedef MatrixMulOp<SMatrix<T,D1,D,R1>, SMatrix<T,D,D2,R2>, D> MatMulOp;
458 return Expr<MatMulOp,T,D1,D2,typename MultPolicy<T,R1,R2>::RepType>(MatMulOp(lhs,rhs));
464template <
class A,
class T,
unsigned int D1,
unsigned int D,
unsigned int D2,
class R1,
class R2>
465inline Expr<MatrixMulOp<SMatrix<T,D1,D,R1>, Expr<A,T,D,D2,R2>, D>,
T, D1, D2,
typename MultPolicy<T,R1,R2>::RepType>
466 operator*(
const SMatrix<T,D1,D,R1>& lhs,
const Expr<A,T,D,D2,R2>& rhs) {
467 typedef MatrixMulOp<SMatrix<T,D1,D,R1>, Expr<A,T,D,D2,R2>, D> MatMulOp;
468 return Expr<MatMulOp,T,D1,D2,typename MultPolicy<T,R1,R2>::RepType>(MatMulOp(lhs,rhs));
474template <
class A,
class T,
unsigned int D1,
unsigned int D,
unsigned int D2,
class R1,
class R2>
475inline Expr<MatrixMulOp<Expr<A,T,D1,D,R1>, SMatrix<T,D,D2,R2>, D>,
T, D1, D2,
typename MultPolicy<T,R1,R2>::RepType>
476 operator*(
const Expr<A,T,D1,D,R1>& lhs,
const SMatrix<T,D,D2,R2>& rhs) {
477 typedef MatrixMulOp<Expr<A,T,D1,D,R1>, SMatrix<T,D,D2,R2>, D> MatMulOp;
478 return Expr<MatMulOp,T,D1,D2,typename MultPolicy<T,R1,R2>::RepType>(MatMulOp(lhs,rhs));
484template <
class A,
class B,
class T,
unsigned int D1,
unsigned int D,
unsigned int D2,
class R1,
class R2>
485inline Expr<MatrixMulOp<Expr<A,T,D1,D,R1>, Expr<B,T,D,D2,R2>, D>,
T, D1, D2,
typename MultPolicy<T,R1,R2>::RepType>
486 operator*(
const Expr<A,T,D1,D,R1>& lhs,
const Expr<B,T,D,D2,R2>& rhs) {
487 typedef MatrixMulOp<Expr<A,T,D1,D,R1>, Expr<B,T,D,D2,R2>, D> MatMulOp;
488 return Expr<MatMulOp,T,D1,D2,typename MultPolicy<T,R1,R2>::RepType>(MatMulOp(lhs,rhs));
500template <
class Matrix,
class T,
unsigned int D1,
unsigned int D2=D1>
512 return rhs_.apply( (i%D1)*D2 + i/D1);
519 return rhs_.IsInUse(p);
536template <
class T,
unsigned int D1,
unsigned int D2,
class R>
537inline Expr<TransposeOp<SMatrix<T,D1,D2,R>,
T,D1,D2>,
T, D2, D1,
typename TranspPolicy<T,D1,D2,R>::RepType>
547template <
class A,
class T,
unsigned int D1,
unsigned int D2,
class R>
548inline Expr<TransposeOp<Expr<A,T,D1,D2,R>,
T,D1,D2>,
T, D2, D1,
typename TranspPolicy<T,D1,D2,R>::RepType>
556#ifdef ENABLE_TEMPORARIES_TRANSPOSE
562template <
class T,
unsigned int D1,
unsigned int D2,
class R>
563inline SMatrix< T, D2, D1, typename TranspPolicy<T,D1,D2,R>::RepType>
564 Transpose(
const SMatrix<T,D1,D2, R>& rhs) {
565 typedef TransposeOp<SMatrix<T,D1,D2,R>,
T,D1,D2> MatTrOp;
567 return SMatrix< T, D2, D1, typename TranspPolicy<T,D1,D2,R>::RepType>
568 ( Expr<MatTrOp, T, D2, D1, typename TranspPolicy<T,D1,D2,R>::RepType>(MatTrOp(rhs)) );
574template <
class A,
class T,
unsigned int D1,
unsigned int D2,
class R>
575inline SMatrix< T, D2, D1, typename TranspPolicy<T,D1,D2,R>::RepType>
576 Transpose(
const Expr<A,T,D1,D2,R>& rhs) {
577 typedef TransposeOp<Expr<A,T,D1,D2,R>,
T,D1,D2> MatTrOp;
579 return SMatrix< T, D2, D1, typename TranspPolicy<T,D1,D2,R>::RepType>
580 ( Expr<MatTrOp, T, D2, D1, typename TranspPolicy<T,D1,D2,R>::RepType>(MatTrOp(rhs)) );
590template <
class T,
unsigned int D,
class R>
591inline T Product(
const SMatrix<T,D,D,R>& lhs,
const SVector<T,D>& rhs) {
592 return Dot(rhs, lhs * rhs);
598template <
class T,
unsigned int D,
class R>
599inline T Product(
const SVector<T,D>& lhs,
const SMatrix<T,D,D,R>& rhs) {
600 return Dot(lhs, rhs * lhs);
606template <
class A,
class T,
unsigned int D,
class R>
607inline T Product(
const SMatrix<T,D,D,R>& lhs,
const VecExpr<A,T,D>& rhs) {
608 return Dot(rhs, lhs * rhs);
614template <
class A,
class T,
unsigned int D,
class R>
615inline T Product(
const VecExpr<A,T,D>& lhs,
const SMatrix<T,D,D,R>& rhs) {
616 return Dot(lhs, rhs * lhs);
622template <
class A,
class T,
unsigned int D,
class R>
623inline T Product(
const SVector<T,D>& lhs,
const Expr<A,T,D,D,R>& rhs) {
624 return Dot(lhs, rhs * lhs);
630template <
class A,
class T,
unsigned int D,
class R>
631inline T Product(
const Expr<A,T,D,D,R>& lhs,
const SVector<T,D>& rhs) {
632 return Dot(rhs, lhs * rhs);
638template <
class A,
class B,
class T,
unsigned int D,
class R>
639inline T Product(
const Expr<A,T,D,D,R>& lhs,
const VecExpr<B,T,D>& rhs) {
640 return Dot(rhs, lhs * rhs);
646template <
class A,
class B,
class T,
unsigned int D,
class R>
647inline T Product(
const VecExpr<A,T,D>& lhs,
const Expr<B,T,D,D,R>& rhs) {
648 return Dot(lhs, rhs * lhs);
662template <
class T,
unsigned int D,
class R>
664 return Dot(rhs, lhs * rhs);
670template <
class T,
unsigned int D,
class R>
672 return Dot(lhs, rhs * lhs);
678template <
class A,
class T,
unsigned int D,
class R>
680 return Dot(rhs, lhs * rhs);
686template <
class A,
class T,
unsigned int D,
class R>
688 return Dot(lhs, rhs * lhs);
694template <
class A,
class T,
unsigned int D,
class R>
696 return Dot(lhs, rhs * lhs);
702template <
class A,
class T,
unsigned int D,
class R>
704 return Dot(rhs, lhs * rhs);
710template <
class A,
class B,
class T,
unsigned int D,
class R>
712 return Dot(rhs, lhs * rhs);
718template <
class A,
class B,
class T,
unsigned int D,
class R>
720 return Dot(lhs, rhs * lhs);
735template <
class T,
unsigned int D1,
unsigned int D2,
class R>
736inline SMatrix<T,D1,D1,MatRepSym<T,D1> >
Similarity(
const SMatrix<T,D1,D2,R>& lhs,
const SMatrix<
T,D2,D2,
MatRepSym<T,D2> >& rhs) {
749template <
class A,
class T,
unsigned int D1,
unsigned int D2,
class R>
750inline SMatrix<T,D1,D1,MatRepSym<T,D1> >
Similarity(
const Expr<A,T,D1,D2,R>& lhs,
const SMatrix<
T,D2,D2,
MatRepSym<T,D2> >& rhs) {
764template <
class T,
unsigned int D1>
765inline SMatrix<T,D1,D1,MatRepSym<T,D1> >
Similarity(
const SMatrix<
T,D1,D1,MatRepSym<T,D1> >& lhs,
const SMatrix<
T,D1,D1,MatRepSym<T,D1> >& rhs) {
766 SMatrix<T,D1,D1, MatRepStd<T,D1,D1> > tmp = lhs * rhs;
767 typedef SMatrix<T,D1,D1,MatRepSym<T,D1> > SMatrixSym;
785template <
class T,
unsigned int D1,
unsigned int D2,
class R>
786inline SMatrix<T,D2,D2,MatRepSym<T,D2> >
SimilarityT(
const SMatrix<T,D1,D2,R>& lhs,
const SMatrix<
T,D1,D1,
MatRepSym<T,D1> >& rhs) {
799template <
class A,
class T,
unsigned int D1,
unsigned int D2,
class R>
800inline SMatrix<T,D2,D2,MatRepSym<T,D2> >
SimilarityT(
const Expr<A,T,D1,D2,R>& lhs,
const SMatrix<
T,D1,D1,
MatRepSym<T,D1> >& rhs) {
834template <
class Vector1,
class Vector2>
846 inline typename Vector1::value_type
apply(
unsigned int i)
const {
849 inline typename Vector1::value_type
operator() (
unsigned int i,
unsigned j)
const {
850 return lhs_.apply(i) *
rhs_.apply(j);
853 inline bool IsInUse (
const typename Vector1::value_type * )
const {
881template <
class T,
unsigned int D1,
unsigned int D2>
891 template <
class T,
unsigned int D1,
unsigned int D2,
class A>
892inline Expr<TensorMulOp<VecExpr<A,T,D1>, SVector<T,D2> >,
T, D1, D2 >
901 template <
class T,
unsigned int D1,
unsigned int D2,
class A>
902inline Expr<TensorMulOp<SVector<T,D1>, VecExpr<A,T,D2> >,
T, D1, D2 >
912 template <
class T,
unsigned int D1,
unsigned int D2,
class A,
class B>
913inline Expr<TensorMulOp<VecExpr<A,T,D1>, VecExpr<B,T,D2> >,
T, D1, D2 >
926template <
class T,
unsigned int D1,
unsigned int D2>
927inline SMatrix<T,D1,D2,MatRepStd<T, D1, D2>>
TensorProd(
const SVector<T,D1>& lhs,
const SVector<T,D2>& rhs) {
928 SMatrix<T,D1,D2,MatRepStd<T, D1, D2>> tmp;
929 for (
unsigned int i=0; i< D1; ++i)
930 for (
unsigned int j=0; j< D2; ++j) {
931 tmp(i,j) = lhs[i]*rhs[j];
939template <
class T,
unsigned int D1,
unsigned int D2,
class A>
940inline SMatrix<T,D1,D2,MatRepStd<T, D1, D2>>
TensorProd(
const VecExpr<A,T,D1>& lhs,
const SVector<T,D2>& rhs) {
941 SMatrix<T,D1,D2,MatRepStd<T, D1, D2>> tmp;
942 for (
unsigned int i=0; i< D1; ++i)
943 for (
unsigned int j=0; j< D2; ++j)
944 tmp(i,j) = lhs.apply(i) * rhs.apply(j);
951template <
class T,
unsigned int D1,
unsigned int D2,
class A>
952inline SMatrix<T,D1,D2, MatRepStd<T, D1, D2>>
TensorProd(
const SVector<T,D1>& lhs,
const VecExpr<A,T,D2>& rhs) {
953 SMatrix<T,D1,D2,MatRepStd<T, D1, D2>> tmp;
954 for (
unsigned int i=0; i< D1; ++i)
955 for (
unsigned int j=0; j< D2; ++j)
956 tmp(i,j) = lhs.apply(i) * rhs.apply(j);
965template <
class T,
unsigned int D1,
unsigned int D2,
class A,
class B>
966inline SMatrix<T,D1,D2,MatRepStd<T, D1, D2>>
TensorProd(
const VecExpr<A,T,D1>& lhs,
const VecExpr<B,T,D2>& rhs) {
967 SMatrix<T,D1,D2,MatRepStd<T, D1, D2>> tmp;
968 for (
unsigned int i=0; i< D1; ++i)
969 for (
unsigned int j=0; j< D2; ++j)
970 tmp(i,j) = lhs.apply(i) * rhs.apply(j);
982template <
class T,
unsigned int D>
985 return decomp.
Solve(vec);
990template <
class T,
unsigned int D>
995 ifail = (ok) ? 0 : -1;
header file containing the templated implementation of matrix inversion routines for use with ROOT's ...
static Double_t Product(const Double_t *x, const Float_t *y)
Product.
class to compute the Cholesky decomposition of a matrix
bool Solve(V &rhs) const
solves a linear system for the given right hand side
Expression wrapper class for Matrix objects.
MatRepSym Matrix storage representation for a symmetric matrix of dimension NxN This class is a templ...
Class for Matrix-Matrix multiplication.
bool IsInUse(const T *p) const
MatrixMulOp(const MatrixA &lhs, const MatrixB &rhs)
T operator()(unsigned int i, unsigned j) const
T apply(unsigned int i) const
calc
SVector: a generic fixed size Vector class.
Class for Tensor Multiplication (outer product) of two vectors giving a matrix.
bool IsInUse(const typename Vector1::value_type *) const
TensorMulOp(const Vector1 &lhs, const Vector2 &rhs)
Vector1::value_type apply(unsigned int i) const
Vector2::kSize is the number of columns in the resulting matrix.
Vector1::value_type operator()(unsigned int i, unsigned j) const
Class for Transpose Operations.
T operator()(unsigned int i, unsigned j) const
T apply(unsigned int i) const
bool IsInUse(const T *p) const
TransposeOp(const Matrix &rhs)
Expression wrapper class for Vector objects.
Class for Vector-Matrix multiplication.
VectorMatrixColOp(const Vector &lhs, const Matrix &rhs)
bool IsInUse(const T *p) const
Matrix::value_type apply(unsigned int i) const
calc
VectorMatrixRowOp(const Matrix &lhs, const Vector &rhs)
bool IsInUse(const T *p) const
Matrix::value_type apply(unsigned int i) const
calc
T Similarity(const SMatrix< T, D, D, R > &lhs, const SVector< T, D > &rhs)
Similarity Vector - Matrix Product: v^T * A * v returning a scalar value of type T .
Expr< TransposeOp< SMatrix< T, D1, D2, R >, T, D1, D2 >, T, D2, D1, typename TranspPolicy< T, D1, D2, R >::RepType > Transpose(const SMatrix< T, D1, D2, R > &rhs)
Matrix Transpose B(i,j) = A(j,i) returning a matrix expression.
SMatrix< T, D2, D2, MatRepSym< T, D2 > > SimilarityT(const SMatrix< T, D1, D2, R > &lhs, const SMatrix< T, D1, D1, MatRepSym< T, D1 > > &rhs)
Transpose Similarity Matrix Product : B = U^T * A * U for A symmetric returning a symmetric matrix ex...
Expr< TensorMulOp< SVector< T, D1 >, SVector< T, D2 > >, T, D1, D2 > TensorProd(const SVector< T, D1 > &lhs, const SVector< T, D2 > &rhs)
Tensor Vector Product : M(i,j) = v(i) * v(j) returning a matrix expression.
T Dot(const SVector< T, D > &lhs, const SVector< T, D > &rhs)
Vector dot product.
Namespace for new Math classes and functions.
bool SolveChol(SMatrix< T, D, D, MatRepSym< T, D > > &mat, SVector< T, D > &vec)
AxisAngle operator*(RotationX const &r1, AxisAngle const &r2)
Multiplication of an axial rotation by an AxisAngle.
static void Evaluate(SMatrix< T, D, D, MatRepSym< T, D > > &lhs, const Expr< A, T, D, D, R > &rhs)
assign a symmetric matrix from an expression
MatRepStd< T, N1, N2 > RepType
MatRepStd< T, N2, N1 > RepType