From 45ad49bad53e8487ed204050cd74913d0c9a4262 Mon Sep 17 00:00:00 2001 From: Lorenzo Moneta <Lorenzo.Moneta@cern.ch> Date: Mon, 5 Dec 2005 16:33:47 +0000 Subject: [PATCH] add stl iterator interface in matrices. Cannoot add constructor Vector(begin,end) since it conflicts with Vector( val1, val2). Add also const methods for inverse and determinant. Rename enumeration variables to be consistent with ROOT naming conventions git-svn-id: http://root.cern.ch/svn/root/trunk@13491 27541ba8-7e3a-0410-8455-c3a389f83636 --- smatrix/Module.mk | 3 + smatrix/inc/LinkDef.h | 5 +- smatrix/inc/Math/Dsfact.h | 6 +- smatrix/inc/Math/Expression.h | 11 +-- smatrix/inc/Math/MatrixFunctions.h | 12 ++-- smatrix/inc/Math/SMatrix.h | 105 +++++++++++++++++++++++++---- smatrix/inc/Math/SMatrix.icc | 84 ++++++++++++++++++++++- smatrix/inc/Math/SVector.h | 52 +++++++++++++- smatrix/inc/Math/SVector.icc | 67 ++++++++++++++++-- smatrix/test/testSMatrix.cxx | 60 +++++++++++++---- 10 files changed, 355 insertions(+), 50 deletions(-) diff --git a/smatrix/Module.mk b/smatrix/Module.mk index fece247518f..700d7515526 100644 --- a/smatrix/Module.mk +++ b/smatrix/Module.mk @@ -88,3 +88,6 @@ distclean-smatrix: clean-smatrix @rm -rf include/Math distclean:: distclean-smatrix + +test-smatrix: all-smatrix + @cd $(SMATRIXDIR)/test; make \ No newline at end of file diff --git a/smatrix/inc/LinkDef.h b/smatrix/inc/LinkDef.h index 5e50a4863f2..f1db7fe4cb0 100644 --- a/smatrix/inc/LinkDef.h +++ b/smatrix/inc/LinkDef.h @@ -1,4 +1,4 @@ -// @(#)root/smatrix:$Name: $:$Id: LinkDef.h,v 1.1 2005/10/28 15:58:38 moneta Exp $ +// @(#)root/smatrix:$Name: $:$Id: LinkDef.h,v 1.1 2005/11/24 16:03:42 brun Exp $ // Authors: L. Moneta 2005 @@ -28,4 +28,7 @@ #pragma link C++ class ROOT::Math::SVector<double,5>+; + + + #endif diff --git a/smatrix/inc/Math/Dsfact.h b/smatrix/inc/Math/Dsfact.h index abd62806115..bffd399b913 100644 --- a/smatrix/inc/Math/Dsfact.h +++ b/smatrix/inc/Math/Dsfact.h @@ -1,4 +1,4 @@ -// @(#)root/smatrix:$Name: $:$Id: Dsfact.hv 1.0 2005/11/24 12:00:00 moneta Exp $ +// @(#)root/smatrix:$Name: $:$Id: Dsfact.h,v 1.1 2005/11/24 16:03:42 brun Exp $ // Authors: T. Glebe, L. Moneta 2005 #ifndef ROOT_Math_Dsfact @@ -57,11 +57,11 @@ bool Dsfact(Matrix& rhs, typename Matrix::value_type& det) { #ifdef XXX const typename Matrix::value_type* A = rhs.Array(); - typename Matrix::value_type array[Matrix::size]; + typename Matrix::value_type array[Matrix::kSize]; typename Matrix::value_type* a = array; // copy contents of matrix to working place - for(unsigned int i=0; i<Matrix::size; ++i) { + for(unsigned int i=0; i<Matrix::kSize; ++i) { array[i] = A[i]; } #endif diff --git a/smatrix/inc/Math/Expression.h b/smatrix/inc/Math/Expression.h index 87daf893f35..b14fdd9e8ac 100644 --- a/smatrix/inc/Math/Expression.h +++ b/smatrix/inc/Math/Expression.h @@ -1,4 +1,4 @@ -// @(#)root/smatrix:$Name: $:$Id: Expression.h,v 1.1 2005/11/24 16:03:42 brun Exp $ +// @(#)root/smatrix:$Name: $:$Id: Expression.h,v 1.2 2005/11/30 16:00:41 rdm Exp $ // Authors: T. Glebe, L. Moneta 2005 #ifndef ROOT_Math_Expression @@ -80,9 +80,9 @@ public: // use enumerations enum { /// - rows = D, + kRows = D, /// - cols = D2 + kCols = D2 }; #endif @@ -99,10 +99,11 @@ public: } else { os << "[ "; for (unsigned int i=0; i < D; ++i) { - for (unsigned int j=0; j < D2; ++j) { + unsigned int d2 = D2; // to avoid some annoying warnings in case of vectors (D2 = 0) + for (unsigned int j=0; j < d2; ++j) { os << std::setw(12) << apply(i*D2+j); if ((!((j+1)%12)) && (j < D2-1)) - os << std::endl << " ..."; + os << std::endl << " ..."; } if (i != D - 1) os << std::endl << " "; diff --git a/smatrix/inc/Math/MatrixFunctions.h b/smatrix/inc/Math/MatrixFunctions.h index ad2c93b9bcb..fe0dc295c49 100644 --- a/smatrix/inc/Math/MatrixFunctions.h +++ b/smatrix/inc/Math/MatrixFunctions.h @@ -1,4 +1,4 @@ -// @(#)root/smatrix:$Name: $:$Id: MatrixFunctions.hv 1.0 2005/11/24 12:00:00 moneta Exp $ +// @(#)root/smatrix:$Name: $:$Id: MatrixFunctions.h,v 1.1 2005/11/24 16:03:42 brun Exp $ // Authors: T. Glebe, L. Moneta 2005 #ifndef ROOT_Math_MatrixFunctions @@ -114,7 +114,7 @@ struct meta_col_dot { template <class Matrix, class Vector> static inline typename Matrix::value_type f(const Matrix& lhs, const Vector& rhs, const unsigned int offset) { - return lhs.apply(Matrix::cols*I+offset) * rhs.apply(I) + + return lhs.apply(Matrix::kCols*I+offset) * rhs.apply(I) + meta_col_dot<I-1>::f(lhs,rhs,offset); } }; @@ -251,8 +251,8 @@ struct meta_matrix_dot { template <class MatrixA, class MatrixB> static inline typename MatrixA::value_type f(const MatrixA& lhs, const MatrixB& rhs, const unsigned int offset) { - return lhs.apply(offset/MatrixB::cols*MatrixA::cols + I) * - rhs.apply(MatrixB::cols*I + offset%MatrixB::cols) + + return lhs.apply(offset/MatrixB::kCols*MatrixA::kCols + I) * + rhs.apply(MatrixB::kCols*I + offset%MatrixB::kCols) + meta_matrix_dot<I-1>::f(lhs,rhs,offset); } }; @@ -266,8 +266,8 @@ struct meta_matrix_dot<0> { template <class MatrixA, class MatrixB> static inline typename MatrixA::value_type f(const MatrixA& lhs, const MatrixB& rhs, const unsigned int offset) { - return lhs.apply(offset/MatrixB::cols*MatrixA::cols) * - rhs.apply(offset%MatrixB::cols); + return lhs.apply(offset/MatrixB::kCols*MatrixA::kCols) * + rhs.apply(offset%MatrixB::kCols); } }; diff --git a/smatrix/inc/Math/SMatrix.h b/smatrix/inc/Math/SMatrix.h index f7adaf532b4..7a2419382e1 100644 --- a/smatrix/inc/Math/SMatrix.h +++ b/smatrix/inc/Math/SMatrix.h @@ -1,4 +1,4 @@ -// @(#)root/smatrix:$Name: $:$Id: SMatrix.hv 1.0 2005/11/24 12:00:00 moneta Exp $ +// @(#)root/smatrix:$Name: $:$Id: SMatrix.h,v 1.1 2005/11/24 16:03:42 brun Exp $ // Authors: T. Glebe, L. Moneta 2005 #ifndef ROOT_Math_SMatrix @@ -71,9 +71,18 @@ public: /** @name --- Typedefs --- */ /// typedef T value_type; + + /** STL iterator interface. */ + typedef T* iterator; + + /** STL const_iterator interface. */ + typedef const T* const_iterator; /** @name --- Constructors --- */ - /// + + /** + Default constructor: + */ SMatrix(); /// SMatrix(const SMatrix<T,D1,D2>& rhs); @@ -81,6 +90,19 @@ public: template <class A> SMatrix(const Expr<A,T,D1,D2>& rhs); + // new constructs using STL iterator interface + /** + * Constructor with STL iterator interface. The data will be copied into the matrix + */ + template<class InputIterator> + SMatrix(InputIterator begin, InputIterator end); + + /** + * Constructor with STL iterator interface. The data will be copied into the matrix + */ + template<class InputIterator> + SMatrix(InputIterator begin, unsigned int size); + // skip this methods (they are too ambigous) #ifdef OLD_IMPL /// 2nd arg: set only diagonal? @@ -110,19 +132,19 @@ public: #ifdef OLD_IMPL /// return no. of matrix rows - static const unsigned int rows = D1; + static const unsigned int kRows = D1; /// return no. of matrix columns - static const unsigned int cols = D2; + static const unsigned int kCols = D2; /// return no of elements: rows*columns - static const unsigned int size = D1*D2; + static const unsigned int kSize = D1*D2; #else enum { /// return no. of matrix rows - rows = D1, + kRows = D1, /// return no. of matrix columns - cols = D2, + kCols = D2, /// return no of elements: rows*columns - size = D1*D2 + kSize = D1*D2 }; #endif /** @name --- Access functions --- */ @@ -133,6 +155,21 @@ public: /// return pointer to internal array T* Array(); + // STL interface + + /** STL iterator interface. */ + iterator begin(); + + /** STL iterator interface. */ + iterator end(); + + /** STL const_iterator interface. */ + const_iterator begin() const; + + /** STL const_iterator interface. */ + const_iterator end() const; + + /** @name --- Operators --- */ /// element wise comparison bool operator==(const T& rhs) const; @@ -199,16 +236,58 @@ public: #endif /** @name --- Expert functions --- */ - /// invert symmetric, pos. def. Matrix via Dsinv + + /** + invert symmetric, pos. def. Matrix via Dsinv. + This method change the current matrix + */ bool Sinvert(); + + /** + invert symmetric, pos. def. Matrix via Dsinv. + This method returns a new matrix. In case the inversion fails + the current matrix is returned + */ + SMatrix<T,D1,D2> Sinverse() const; + /** determinant of symmetrc, pos. def. Matrix via Dsfact. \textbf{Note:} this - will destroy the contents of the Matrix!*/ + will destroy the contents of the Matrix! + */ bool Sdet(T& det); - /// invert square Matrix via Dinv + + /** determinant of symmetrc, pos. def. Matrix via Dsfact. \textbf{Note:} + this method will preserve the contents of the Matrix! + */ + bool Sdet2(T& det) const; + + + /** + invert square Matrix via Dinv. + This method change the current matrix + */ bool Invert(); - /** determinant of square Matrix via Dfact. \textbf{Note:} this will destroy - the contents of the Matrix! */ + + /** + invert square Matrix via Dinv. + This method returns a new matrix. In case the inversion fails + the current matrix is returned + */ + SMatrix<T,D1,D2> Inverse() const; + + /** + determinant of square Matrix via Dfact. \textbf{Note:} this will destroy + the contents of the Matrix! + */ bool Det(T& det); + + /** + determinant of square Matrix via Dfact. \textbf{Note:} this will preserve + the content of the Matrix! + */ + bool Det2(T& det) const; + + + /// place a vector in a Matrix row template <unsigned int D> SMatrix<T,D1,D2>& Place_in_row(const SVector<T,D>& rhs, diff --git a/smatrix/inc/Math/SMatrix.icc b/smatrix/inc/Math/SMatrix.icc index 0c306ba0f8d..f3d6768d665 100644 --- a/smatrix/inc/Math/SMatrix.icc +++ b/smatrix/inc/Math/SMatrix.icc @@ -1,4 +1,4 @@ -// @(#)root/smatrix:$Name: $:$Id: SMatrix.iccv 1.0 2005/11/24 12:00:00 moneta Exp $ +// @(#)root/smatrix:$Name: $:$Id: SMatrix.icc,v 1.1 2005/11/24 16:03:42 brun Exp $ // Authors: T. Glebe, L. Moneta 2005 #ifndef ROOT_Math_SMatrix_icc @@ -42,6 +42,8 @@ #include "Math/Dinv.h" #include "Math/Functions.h" +#include "Math/StaticCheck.h" + namespace ROOT { @@ -72,8 +74,30 @@ template <class A> SMatrix<T,D1,D2>::SMatrix(const Expr<A,T,D1,D2>& rhs) { operator=(rhs); } - + + +//============================================================================== +// New Constructors from STL interfaces +//============================================================================== +template <class T, unsigned int D1, unsigned int D2> +template <class InputIterator> +SMatrix<T,D1,D2>::SMatrix(InputIterator begin, InputIterator end) { + assert( std::distance(begin,end) <= kSize); + std::copy(begin,end, fArray); +} + +template <class T, unsigned int D1, unsigned int D2> +template <class InputIterator> +SMatrix<T,D1,D2>::SMatrix(InputIterator begin, unsigned int size) { + assert( size <= kSize); + std::copy(begin,begin+size,fArray); +} + + #ifdef OLD_IMPL +//============================================================================= +// old constructors (disabled) +//============================================================================ template <class T, unsigned int D1, unsigned int D2> SMatrix<T,D1,D2>::SMatrix(const T& rhs, const bool diagonal) { @@ -378,32 +402,64 @@ bool SMatrix<T,D1,D2>::operator<(const Expr<A,T,D1,D2>& rhs) const { //============================================================================== template <class T, unsigned int D1, unsigned int D2> inline bool SMatrix<T,D1,D2>::Sinvert() { + STATIC_CHECK( D1 == D2,SMatrix_not_square); return Dsinv<T,D1,D1>(fArray); } +template <class T, unsigned int D1, unsigned int D2> +inline SMatrix<T,D1,D2> SMatrix<T,D1,D2>::Sinverse() const { + SMatrix<T,D1,D2> tmp(*this); + tmp.Sinvert(); + return tmp; +} + + + //============================================================================== // sdet //============================================================================== template <class T, unsigned int D1, unsigned int D2> inline bool SMatrix<T,D1,D2>::Sdet(T& det) { + STATIC_CHECK( D1 == D2,SMatrix_not_square); return Dsfact<SMatrix<T,D1,D1>, D1, D1>(*this,det); } +template <class T, unsigned int D1, unsigned int D2> +inline bool SMatrix<T,D1,D2>::Sdet2(T& det) const { + SMatrix<T,D1,D2> tmp(*this); + return tmp.Sdet(det); +} + //============================================================================== // invert //============================================================================== template <class T, unsigned int D1, unsigned int D2> inline bool SMatrix<T,D1,D2>::Invert() { + STATIC_CHECK( D1 == D2,SMatrix_not_square); return Inverter<D2>::Dinv(*this); } +template <class T, unsigned int D1, unsigned int D2> +inline SMatrix<T,D1,D2> SMatrix<T,D1,D2>::Inverse() const { + SMatrix<T,D1,D2> tmp(*this); + tmp.Invert(); + return tmp; +} + //============================================================================== // det //============================================================================== template <class T, unsigned int D1, unsigned int D2> inline bool SMatrix<T,D1,D2>::Det(T& det) { + STATIC_CHECK( D1 == D2,SMatrix_not_square); return Dfact<SMatrix<T,D1,D1>, D1, D1>(*this,det); } +template <class T, unsigned int D1, unsigned int D2> +inline bool SMatrix<T,D1,D2>::Det2(T& det) const { + SMatrix<T,D1,D2> tmp(*this); + return tmp.Det(det); +} + //============================================================================== // place_in_row @@ -587,6 +643,30 @@ inline T& SMatrix<T,D1,D2>::operator()(unsigned int i, unsigned int j) { return fArray[i*D2+j]; } +//============================================================================== +// STL interface +//============================================================================== +template <class T, unsigned int D1, unsigned int D2> +inline T * SMatrix<T,D1,D2>::begin() { + return fArray; +} + +template <class T, unsigned int D1, unsigned int D2> +inline T * SMatrix<T,D1,D2>::end() { + return fArray + kSize; +} + +template <class T, unsigned int D1, unsigned int D2> +inline const T * SMatrix<T,D1,D2>::begin() const { + return fArray; +} + +template <class T, unsigned int D1, unsigned int D2> +inline const T * SMatrix<T,D1,D2>::end() const { + return fArray + kSize; +} + + } // namespace Math diff --git a/smatrix/inc/Math/SVector.h b/smatrix/inc/Math/SVector.h index 57ea4909d34..4596cde8493 100644 --- a/smatrix/inc/Math/SVector.h +++ b/smatrix/inc/Math/SVector.h @@ -1,4 +1,4 @@ -// @(#)root/smatrix:$Name: $:$Id: SVector.hv 1.0 2005/11/24 12:00:00 moneta Exp $ +// @(#)root/smatrix:$Name: $:$Id: SVector.h,v 1.1 2005/11/24 16:03:42 brun Exp $ // Authors: T. Glebe, L. Moneta 2005 #ifndef ROOT_Math_SVector @@ -68,8 +68,19 @@ public: /// typedef T value_type; + /** STL iterator interface. */ + typedef T* iterator; + + /** STL const_iterator interface. */ + typedef const T* const_iterator; + /** @name --- Constructors --- */ - /// + + + /** @name --- Constructors --- */ + /** + Default constructor: vector filled with zero values + */ SVector(); /// template <class A> @@ -82,8 +93,28 @@ public: /// $D1\le D-1$ required! template <unsigned int D1> SVector(const T& a1, const SVector<T,D1>& rhs); + + // new constructs using STL iterator interface + // skip - need to solve the ambiguities +#ifdef LATER + /** + * Constructor with STL iterator interface. The data will be copied into the vector + */ + template<class InputIterator> + explicit SVector(InputIterator begin, InputIterator end); + + /** + * Constructor with STL iterator interface. The data will be copied into the vector + */ + template<class InputIterator> + explicit SVector(InputIterator begin, unsigned int size); + +#else + // if you use iterator this is not necessary + /// fill from array, len must be equal to D! SVector(const T* a, unsigned int len); +#endif /// SVector(const T& rhs); /// @@ -113,6 +144,8 @@ public: const T& a5, const T& a6, const T& a7, const T& a8, const T& a9, const T& a10); + + /// SVector<T,D>& operator=(const T& rhs); /// @@ -129,6 +162,21 @@ public: /// return pointer to internal array T* Array(); + // STL interface + + /** STL iterator interface. */ + iterator begin(); + + /** STL iterator interface. */ + iterator end(); + + /** STL const_iterator interface. */ + const_iterator begin() const; + + /** STL const_iterator interface. */ + const_iterator end() const; + + /** @name --- Operators --- */ /// element wise comparison bool operator==(const T& rhs) const; diff --git a/smatrix/inc/Math/SVector.icc b/smatrix/inc/Math/SVector.icc index ed07b07af7e..3fcae4bf1f4 100644 --- a/smatrix/inc/Math/SVector.icc +++ b/smatrix/inc/Math/SVector.icc @@ -1,4 +1,4 @@ -// @(#)root/smatrix:$Name: $:$Id: SVector.iccv 1.0 2005/11/24 12:00:00 moneta Exp $ +// @(#)root/smatrix:$Name: $:$Id: SVector.icc,v 1.1 2005/11/24 16:03:42 brun Exp $ // Authors: T. Glebe, L. Moneta 2005 #ifndef ROOT_Math_SVector_icc @@ -34,6 +34,8 @@ #include <assert.h> +#include "Math/StaticCheck.h" + namespace ROOT { namespace Math { @@ -62,6 +64,7 @@ SVector<T,D>::SVector(const SVector<T,D>& rhs) { template <class T, unsigned int D> template <unsigned int D1> SVector<T,D>::SVector(const SVector<T,D1>& rhs) { + STATIC_CHECK( D >= D1,SVector_dimension_too_small); for(unsigned int i=0; i<D1; ++i) fArray[i] = rhs.fArray[i]; } @@ -70,41 +73,71 @@ template <class T, unsigned int D> template <unsigned int D1> SVector<T,D>::SVector(const T& a1, const SVector<T,D1>& rhs) { fArray[0] = a1; + STATIC_CHECK( D >= D1+1,SVector_dimension_too_small); for(unsigned int i=0; i<D1; ++i) fArray[i+1] = rhs[i]; } + template <class T, unsigned int D> -SVector<T,D>::SVector(const T* a, unsigned int len) { - assert(len == D); +SVector<T,D>::SVector(const T& rhs) { for(unsigned int i=0; i<D; ++i) - fArray[i] = a[i]; + fArray[i] = rhs; +} + + +//============================================================================== +// New Constructors from STL interfaces +//============================================================================== + +#ifdef LATER +template <class T, unsigned int D> +template <class InputIterator> +SVector<T,D>::SVector(InputIterator begin, InputIterator end) { + assert( std::distance(begin,end) <= D); + std::copy(begin, end, fArray); } template <class T, unsigned int D> -SVector<T,D>::SVector(const T& rhs) { +template <class InputIterator> +SVector<T,D>::SVector(InputIterator begin, unsigned int size) { + assert( size <= D); + std::copy(begin, begin+size, fArray); +} + +#else + +template <class T, unsigned int D> +SVector<T,D>::SVector(const T* a, unsigned int len) { + assert(len == D); for(unsigned int i=0; i<D; ++i) - fArray[i] = rhs; + fArray[i] = a[i]; } +#endif + template <class T, unsigned int D> SVector<T,D>::SVector(const T& a1, const T& a2) { + STATIC_CHECK( D >= 2,SVector_dimension_too_small); fArray[0] = a1; fArray[1] = a2; } template <class T, unsigned int D> SVector<T,D>::SVector(const T& a1, const T& a2, const T& a3) { + STATIC_CHECK( D >= 3,SVector_dimension_too_small); fArray[0] = a1; fArray[1] = a2; fArray[2] = a3; } template <class T, unsigned int D> SVector<T,D>::SVector(const T& a1, const T& a2, const T& a3, const T& a4) { + STATIC_CHECK( D >= 4,SVector_dimension_too_small); fArray[0] = a1; fArray[1] = a2; fArray[2] = a3; fArray[3] = a4; } template <class T, unsigned int D> SVector<T,D>::SVector(const T& a1, const T& a2, const T& a3, const T& a4, const T& a5) { + STATIC_CHECK( D >= 5,SVector_dimension_too_small); fArray[0] = a1; fArray[1] = a2; fArray[2] = a3; fArray[3] = a4; fArray[4] = a5; } @@ -112,6 +145,7 @@ SVector<T,D>::SVector(const T& a1, const T& a2, const T& a3, const T& a4, template <class T, unsigned int D> SVector<T,D>::SVector(const T& a1, const T& a2, const T& a3, const T& a4, const T& a5, const T& a6) { + STATIC_CHECK( D >= 6,SVector_dimension_too_small); fArray[0] = a1; fArray[1] = a2; fArray[2] = a3; fArray[3] = a4; fArray[4] = a5; fArray[5] = a6; } @@ -119,6 +153,7 @@ SVector<T,D>::SVector(const T& a1, const T& a2, const T& a3, const T& a4, template <class T, unsigned int D> SVector<T,D>::SVector(const T& a1, const T& a2, const T& a3, const T& a4, const T& a5, const T& a6, const T& a7) { + STATIC_CHECK( D >= 7,SVector_dimension_too_small); fArray[0] = a1; fArray[1] = a2; fArray[2] = a3; fArray[3] = a4; fArray[4] = a5; fArray[5] = a6; fArray[6] = a7; } @@ -126,6 +161,7 @@ SVector<T,D>::SVector(const T& a1, const T& a2, const T& a3, const T& a4, template <class T, unsigned int D> SVector<T,D>::SVector(const T& a1, const T& a2, const T& a3, const T& a4, const T& a5, const T& a6, const T& a7, const T& a8) { + STATIC_CHECK( D >= 8,SVector_dimension_too_small); fArray[0] = a1; fArray[1] = a2; fArray[2] = a3; fArray[3] = a4; fArray[4] = a5; fArray[5] = a6; fArray[6] = a7; fArray[7] = a8; } @@ -134,6 +170,7 @@ template <class T, unsigned int D> SVector<T,D>::SVector(const T& a1, const T& a2, const T& a3, const T& a4, const T& a5, const T& a6, const T& a7, const T& a8, const T& a9) { + STATIC_CHECK( D >= 9,SVector_dimension_too_small); fArray[0] = a1; fArray[1] = a2; fArray[2] = a3; fArray[3] = a4; fArray[4] = a5; fArray[5] = a6; fArray[6] = a7; fArray[7] = a8; fArray[8] = a9; @@ -143,6 +180,7 @@ template <class T, unsigned int D> SVector<T,D>::SVector(const T& a1, const T& a2, const T& a3, const T& a4, const T& a5, const T& a6, const T& a7, const T& a8, const T& a9, const T& a10) { + STATIC_CHECK( D >= 10,SVector_dimension_too_small); fArray[0] = a1; fArray[1] = a2; fArray[2] = a3; fArray[3] = a4; fArray[4] = a5; fArray[5] = a6; fArray[6] = a7; fArray[7] = a8; fArray[8] = a9; fArray[9] = a10; @@ -432,6 +470,23 @@ inline const T* SVector<T,D>::Array() const { return fArray; } template <class T, unsigned int D> inline T* SVector<T,D>::Array() { return fArray; } + +//============================================================================== +// STL interface +//============================================================================== +template <class T, unsigned int D> +inline T* SVector<T,D>::begin() { return fArray; } + +template <class T, unsigned int D> +inline const T* SVector<T,D>::begin() const { return fArray; } + +template <class T, unsigned int D> +inline T* SVector<T,D>::end() { return fArray + Dim(); } + +template <class T, unsigned int D> +inline const T* SVector<T,D>::end() const { return fArray + Dim(); } + + //============================================================================== // Operators //============================================================================== diff --git a/smatrix/test/testSMatrix.cxx b/smatrix/test/testSMatrix.cxx index f67f987de22..fbd4564211d 100644 --- a/smatrix/test/testSMatrix.cxx +++ b/smatrix/test/testSMatrix.cxx @@ -3,6 +3,7 @@ #include "Math/SMatrix.h" #include <iostream> +#include <vector> using namespace ROOT::Math; @@ -10,24 +11,56 @@ using std::cout; using std::endl; +//#define TEST_STATIC_CHECK // for testing compiler failures (static check) + #define XXX int test1() { SVector<float,3> x(4,5,6); - SVector<float,2> y(2,3); - cout << "x: " << x << endl; - cout << "y: " << y << endl; - - SMatrix<float,4,3> A; - SMatrix<float,2,2> B; - - A.Place_in_row(y, 1, 1); - A.Place_in_col(x + 2, 1, 0); - A.Place_at(B , 2, 1); - cout << "A: " << endl << A << endl; + SVector<float,2> y(2.0,3.0); + cout << "x: " << x << endl; + cout << "y: " << y << endl; + + float yy1=2.; float yy2 = 3; + SVector<float,2> y2(yy1,yy2); + + + SMatrix<float,4,3> A; + SMatrix<float,2,2> B; + + A.Place_in_row(y, 1, 1); + A.Place_in_col(x + 2, 1, 0); + A.Place_at(B , 2, 1); + cout << "A: " << endl << A << endl; + +#ifdef TEST_STATIC_CHECK + // create a vector of size 2 from 3 arguments + SVector<float, 2> v(1,2,3); +#endif + + // test STL interface + + //double p[2] = {1,2}; + double m[4] = {1,2,3,4}; + + //SVector<float, 2> sp(p,2); + SMatrix<float, 2,2> sm(m,4); + + //cout << "sp: " << endl << sp << endl; + cout << "sm: " << endl << sm << endl; + + //std::vector<float> vp(sp.begin(), sp.end() ); + std::vector<float> vm(sm.begin(), sm.end() ); + + //SVector<float, 2> sp2(vp.begin(),vp.end()); + //SVector<float, 2> sp2(vp.begin(),vp.size()); + SMatrix<float, 2,2> sm2(vm.begin(),vm.end()); + + //if ( sp2 != sp) { cout << "Test STL interface for SVector failed" << endl; return -1; } + if ( sm2 != sm) { cout << "Test STL interface for SMatrix failed" << endl; return -1; } - return 0; + return 0; } @@ -211,6 +244,9 @@ int test8() { return 0; } + + + #define TEST(N) \ itest = N; \ if (test##N() == 0) std::cout << " Test " << itest << " OK " << std::endl; \ -- GitLab