[3809] | 1 | // --- Special Square matrices base class
|
---|
| 2 | // This code is part of the SOPHYA library
|
---|
| 3 | // (C) Univ. Paris-Sud (C) LAL-IN2P3/CNRS (C) IRFU-CEA
|
---|
| 4 | // (C) R. Ansari, C.Magneville 2009
|
---|
| 5 |
|
---|
[3831] | 6 | #define SPESQMTX_CC_BFILE // avoid extern template declarations
|
---|
[3809] | 7 | #include "spesqmtx.h"
|
---|
| 8 | #include "pexceptions.h"
|
---|
| 9 | #include "tmatrix.h"
|
---|
| 10 |
|
---|
[3838] | 11 | namespace SOPHYA {
|
---|
[3809] | 12 | /*!
|
---|
[3838] | 13 | \class SpecialSquareMatrix
|
---|
[3809] | 14 | \ingroup TArray
|
---|
| 15 | \brief The base class for representing and manipulating special square matrices
|
---|
| 16 | Diagonal matrix, Symmetric matrix, Triangular matrix
|
---|
[3838] | 17 | This class implements the common operations for special square matrices. Only objects
|
---|
[3809] | 18 |
|
---|
| 19 | from the sub-classes (DiagonalMatrix<T>, LowerTriangularMatrix<T>, SymmetricMatrix<T>)
|
---|
| 20 | can be instanciated. Theses classes offer similar functionalities to the TArray<T> / TMatrix<T>
|
---|
| 21 | classes, like reference sharing and arithmetic operations. However, no sub-matrix extraction
|
---|
| 22 | method is provided.
|
---|
[3838] | 23 |
|
---|
| 24 | \code
|
---|
| 25 | // Create and initialize a diagonal matrix
|
---|
| 26 | DiagonalMatrix<double> diag(3);
|
---|
| 27 | diag(0,0)=1.; diag(1,1)=2.; diag(2,2)=3.;
|
---|
| 28 | // use of multiplication by a constant operator
|
---|
| 29 | diag *= 10.;
|
---|
| 30 | // check the diagonal matrix
|
---|
| 31 | cout << diag << diag.ConvertToStdMatrix() << endl;
|
---|
| 32 | // define and initialize a general matrix
|
---|
| 33 | TMatrix<double> mx(3,3);
|
---|
| 34 | mx=RegularSequence();
|
---|
| 35 | cout << mx;
|
---|
| 36 | // check the result of a matrix mutiplication
|
---|
| 37 | cout << mx*diag;
|
---|
| 38 | \endcode
|
---|
[3809] | 39 | */
|
---|
| 40 |
|
---|
| 41 | //! Default constructor - SpecialSquareMatrix of size 0, SetSize() should be called before the object is used
|
---|
| 42 | template <class T>
|
---|
| 43 | SpecialSquareMatrix<T>::SpecialSquareMatrix(SpSqMtxType typ)
|
---|
| 44 | : mType(typ) , mNrows(0) , mInfo(NULL)
|
---|
| 45 | {
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | //! Instanciate a SpecialSquareMatrix matrix from the number of rows (rowSize must be > 0)
|
---|
| 49 | template <class T>
|
---|
| 50 | SpecialSquareMatrix<T>::SpecialSquareMatrix(sa_size_t rowSize, SpSqMtxType typ)
|
---|
| 51 | : mType(typ) , mNrows(rowSize) , mInfo(NULL)
|
---|
| 52 | {
|
---|
| 53 | if (rowSize < 1)
|
---|
| 54 | throw ParmError("SpecialSquareMatrix<T>::SpecialSquareMatrix(rsz) rsz <= 0");
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | //! Copy constructor (possibility of sharing datas)
|
---|
| 58 | template <class T>
|
---|
| 59 | SpecialSquareMatrix<T>::SpecialSquareMatrix(SpecialSquareMatrix<T> const & a, bool share)
|
---|
| 60 | : mType(a.mType) , mNrows(a.mNrows) , mElems(a.mElems, share) , mInfo(NULL)
|
---|
| 61 | {
|
---|
| 62 | if (a.mInfo) mInfo = new DVList(*(a.mInfo));
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | //! Sets or change the matrix size, specifying the new number of rows
|
---|
| 66 | template <class T>
|
---|
| 67 | void SpecialSquareMatrix<T>::SetSize(sa_size_t rowSize)
|
---|
| 68 | {
|
---|
| 69 | throw ForbiddenError("SpecialSquareMatrix<T>::SetSize() Only sub-classes should be instanciated");
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | //! Return the object as a standard (TMatrix<T>) object
|
---|
| 73 | template <class T>
|
---|
| 74 | TMatrix<T> SpecialSquareMatrix<T>::ConvertToStdMatrix() const
|
---|
| 75 | {
|
---|
| 76 | throw ForbiddenError("SpecialSquareMatrix<T>::ConvertToStdMatrix() Only sub-classes should be instanciated");
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 |
|
---|
| 80 |
|
---|
| 81 |
|
---|
| 82 | //! Clone (duplicate) the SpecialSquareMatrix<T> matrix \b a
|
---|
| 83 | template <class T>
|
---|
| 84 | void SpecialSquareMatrix<T>::Clone(SpecialSquareMatrix<T> const & a)
|
---|
| 85 | {
|
---|
| 86 | if ((mType != C_UndefinedMatrix) && (a.mType != mType))
|
---|
| 87 | throw TypeMismatchExc("SpecialSquareMatrix<T>::Clone()- Different type matrices");
|
---|
| 88 | mType = a.mType;
|
---|
| 89 | mNrows = a.mNrows;
|
---|
| 90 | mElems.Clone(a.mElems);
|
---|
| 91 | if (a.mInfo) mInfo = new DVList(*(a.mInfo));
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | //! Share the data with \b a
|
---|
| 95 | template <class T>
|
---|
| 96 | void SpecialSquareMatrix<T>::Share(SpecialSquareMatrix<T> const & a)
|
---|
| 97 | {
|
---|
| 98 | if ((mType != C_UndefinedMatrix) && (a.mType != mType))
|
---|
| 99 | throw TypeMismatchExc("SpecialSquareMatrix<T>::Share()- Different type matrices");
|
---|
| 100 | mType = a.mType;
|
---|
| 101 | mNrows= a.mNrows;
|
---|
| 102 | mElems.Share(a.mElems);
|
---|
| 103 | if (a.mInfo) mInfo = new DVList(*(a.mInfo));
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | //! Clone or share the data. Data is shared if \b is flagged as temporary
|
---|
| 107 | template <class T>
|
---|
| 108 | void SpecialSquareMatrix<T>::CloneOrShare(SpecialSquareMatrix<T> const & a)
|
---|
| 109 | {
|
---|
| 110 | if ((mType != C_UndefinedMatrix) && (a.mType != mType))
|
---|
| 111 | throw TypeMismatchExc("SpecialSquareMatrix<T>::CloneOrShare()- Different type matrices");
|
---|
| 112 | mType = a.mType;
|
---|
| 113 | mNrows = a.mNrows;
|
---|
| 114 | mElems.CloneOrShare(a.mElems);
|
---|
| 115 | if (a.mInfo) mInfo = new DVList(*(a.mInfo));
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 |
|
---|
| 119 |
|
---|
| 120 | //! Set all matrix elements to the value \b a
|
---|
| 121 | template <class T>
|
---|
| 122 | SpecialSquareMatrix<T>& SpecialSquareMatrix<T>::SetCst(T a)
|
---|
| 123 | {
|
---|
| 124 | if (mElems.Size() < 1)
|
---|
| 125 | throw SzMismatchError("SpecialSquareMatrix<T>::SetCst(T )- (this) not allocated !");
|
---|
| 126 | mElems = a;
|
---|
| 127 | return (*this);
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 |
|
---|
| 131 | //! Element by element copy method
|
---|
| 132 | template <class T>
|
---|
| 133 | SpecialSquareMatrix<T>& SpecialSquareMatrix<T>::Set(SpecialSquareMatrix<T> const & a)
|
---|
| 134 | {
|
---|
| 135 | if (a.mType != mType)
|
---|
| 136 | throw TypeMismatchExc("SpecialSquareMatrix<T>::Set(a)- Different type matrices");
|
---|
| 137 | if (a.mElems.Size() < 1)
|
---|
| 138 | throw SzMismatchError("SpecialSquareMatrix<T>::Set(a)- Object a not allocated !");
|
---|
| 139 | if (mElems.Size() < 1) {
|
---|
| 140 | mNrows = a.mNrows;
|
---|
| 141 | mElems.CloneOrShare(a.mElems);
|
---|
| 142 | return (*this);
|
---|
| 143 | }
|
---|
| 144 | if (mNrows != a.mNrows)
|
---|
| 145 | throw SzMismatchError("SpecialSquareMatrix<T>::Set(a)- NRows() != a.NRows() ! ");
|
---|
| 146 | mElems = a.mElems;
|
---|
| 147 | return (*this);
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | //! Set the matrix elements using the values from the sequence \b seq
|
---|
| 151 | template <class T>
|
---|
| 152 | SpecialSquareMatrix<T>& SpecialSquareMatrix<T>::SetSeq(Sequence const & seq)
|
---|
| 153 | {
|
---|
| 154 | if (mElems.Size() < 1)
|
---|
| 155 | throw SzMismatchError("SpecialSquareMatrix<T>::SetSeq()- (this) not allocated !");
|
---|
| 156 | for(size_t k=0; k<mElems.Size(); k++) seq(k).Convert(mElems(k));
|
---|
| 157 | return (*this);
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | //! Add a constant value (\b b ) to all matrix elements
|
---|
| 161 | template <class T>
|
---|
| 162 | SpecialSquareMatrix<T>& SpecialSquareMatrix<T>::AddCst(T b)
|
---|
| 163 | {
|
---|
| 164 | if (mElems.Size() < 1)
|
---|
| 165 | throw SzMismatchError("SpecialSquareMatrix<T>::AddCst(T )- (this) not allocated !");
|
---|
| 166 | mElems += b;
|
---|
| 167 | return (*this);
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | //! Subtract a constant value (\b b ) from all matrix elements. fginv==true -> elem = b-elem
|
---|
| 171 | template <class T>
|
---|
| 172 | SpecialSquareMatrix<T>& SpecialSquareMatrix<T>::SubCst(T b, bool fginv)
|
---|
| 173 | {
|
---|
| 174 | if (mElems.Size() < 1)
|
---|
| 175 | throw SzMismatchError("SpecialSquareMatrix<T>::Subst(T )- (this) not allocated !");
|
---|
| 176 | if (fginv)
|
---|
| 177 | mElems = mElems.Sub(b, true);
|
---|
| 178 | else mElems -= b;
|
---|
| 179 | return (*this);
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | //! Multiply all matrix elements by a constant value \b b .
|
---|
| 183 | template <class T>
|
---|
| 184 | SpecialSquareMatrix<T>& SpecialSquareMatrix<T>::MulCst(T b)
|
---|
| 185 | {
|
---|
| 186 | if (mElems.Size() < 1)
|
---|
| 187 | throw SzMismatchError("SpecialSquareMatrix<T>::MulCst(T )- (this) not allocated !");
|
---|
| 188 | mElems *= b;
|
---|
| 189 | return (*this);
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | //! Divide all matrix elements by a constant value \b b . if fginv==true elem = b / elem
|
---|
| 193 | template <class T>
|
---|
| 194 | SpecialSquareMatrix<T>& SpecialSquareMatrix<T>::DivCst(T b, bool fginv)
|
---|
| 195 | {
|
---|
| 196 | if (mElems.Size() < 1)
|
---|
| 197 | throw SzMismatchError("SpecialSquareMatrix<T>::DivCst(T )- (this) not allocated !");
|
---|
| 198 | if (fginv) mElems = mElems.Div(b, true);
|
---|
| 199 | else mElems /= b;
|
---|
| 200 | return (*this);
|
---|
| 201 | }
|
---|
| 202 |
|
---|
| 203 | //! Element by element addition : elem(l,m) = elem(l,m) + b.elem(l,m)
|
---|
| 204 | template <class T>
|
---|
| 205 | SpecialSquareMatrix<T>& SpecialSquareMatrix<T>::AddElt(SpecialSquareMatrix<T> const& b)
|
---|
| 206 | {
|
---|
| 207 | if (b.mType != mType)
|
---|
| 208 | throw TypeMismatchExc("SpecialSquareMatrix<T>::AddElt() Different type matrices");
|
---|
| 209 | if ((mNrows < 1)||(mNrows != b.mNrows)||(mElems.Size() < 1)||(mElems.Size() != b.mElems.Size()))
|
---|
| 210 | throw SzMismatchError("SpecialSquareMatrix<T>::AddElt()- Not allocated or different sizes !");
|
---|
| 211 | mElems += b.mElems;
|
---|
| 212 | return (*this);
|
---|
| 213 | }
|
---|
| 214 |
|
---|
| 215 | //! Element by element subtraction : elem(l,m) = elem(l,m) - b.elem(l,m)
|
---|
| 216 | template <class T>
|
---|
| 217 | SpecialSquareMatrix<T>& SpecialSquareMatrix<T>::SubElt(SpecialSquareMatrix<T> const& b)
|
---|
| 218 | {
|
---|
| 219 | if (b.mType != mType)
|
---|
| 220 | throw TypeMismatchExc("SpecialSquareMatrix<T>::SubElt() Different type matrices");
|
---|
| 221 | if ((mNrows < 1)||(mNrows != b.mNrows)||(mElems.Size() < 1)||(mElems.Size() != b.mElems.Size()))
|
---|
| 222 | throw SzMismatchError("SpecialSquareMatrix<T>::SubElt()- Not allocated or different sizes !");
|
---|
| 223 | mElems -= b.mElems;
|
---|
| 224 | return (*this);
|
---|
| 225 | }
|
---|
| 226 | //! Element by element multiplication : elem(l,m) = elem(l,m) * b.elem(l,m)
|
---|
| 227 | template <class T>
|
---|
| 228 | SpecialSquareMatrix<T>& SpecialSquareMatrix<T>::MulElt(SpecialSquareMatrix<T> const& b)
|
---|
| 229 | {
|
---|
| 230 | if (b.mType != mType)
|
---|
| 231 | throw TypeMismatchExc("SpecialSquareMatrix<T>::MulElt() Different type matrices");
|
---|
| 232 | if ((mNrows < 1)||(mNrows != b.mNrows)||(mElems.Size() < 1)||(mElems.Size() != b.mElems.Size()))
|
---|
| 233 | throw SzMismatchError("SpecialSquareMatrix<T>::MulElt()- Not allocated or different sizes !");
|
---|
| 234 | mElems *= b.mElems;
|
---|
| 235 | return (*this);
|
---|
| 236 | }
|
---|
| 237 | //! Element by element divison : elem(l,m) = elem(l,m) / b.elem(l,m)
|
---|
| 238 | template <class T>
|
---|
| 239 | SpecialSquareMatrix<T>& SpecialSquareMatrix<T>::DivElt(SpecialSquareMatrix<T> const& b)
|
---|
| 240 | {
|
---|
| 241 | if (b.mType != mType)
|
---|
| 242 | throw TypeMismatchExc("SpecialSquareMatrix<T>::DivElt() Different type matrices");
|
---|
| 243 | if ((mNrows < 1)||(mNrows != b.mNrows)||(mElems.Size() < 1)||(mElems.Size() != b.mElems.Size()))
|
---|
| 244 | throw SzMismatchError("SpecialSquareMatrix<T>::DivElt()- Not allocated or different sizes !");
|
---|
| 245 | mElems /= b.mElems;
|
---|
| 246 | return (*this);
|
---|
| 247 | }
|
---|
| 248 |
|
---|
| 249 |
|
---|
| 250 | //! Return the minimum and the maximum values of the SpecialSquare Matrix elements
|
---|
| 251 | /*!
|
---|
| 252 | This method generates an exception (\c MathExc) if called for complex matrices
|
---|
| 253 | */
|
---|
| 254 | template <class T>
|
---|
| 255 | void SpecialSquareMatrix<T>::MinMax(T& min, T& max) const
|
---|
| 256 | {
|
---|
| 257 | const T * pe = mElems.Begin();
|
---|
| 258 | min = pe[0];
|
---|
| 259 | max = pe[0];
|
---|
| 260 | for(size_t k=0; k<mElems.Size(); k++) {
|
---|
| 261 | if (pe[k]<min) min = pe[k];
|
---|
| 262 | else if (pe[k]>max) max = pe[k];
|
---|
| 263 | }
|
---|
| 264 | return;
|
---|
| 265 | }
|
---|
| 266 |
|
---|
| 267 | DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
|
---|
| 268 | void SpecialSquareMatrix< complex<r_4> >::MinMax(complex<r_4>& min, complex<r_4>& max) const
|
---|
| 269 | {
|
---|
| 270 | throw MathExc("SpecialSquareMatrix< complex<r_4> >::MinMax(...) - No order in complex");
|
---|
| 271 | }
|
---|
| 272 | DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
|
---|
| 273 | void SpecialSquareMatrix< complex<r_8> >::MinMax(complex<r_8>& min, complex<r_8>& max) const
|
---|
| 274 | {
|
---|
| 275 | throw MathExc("SpecialSquareMatrix< complex<r_4> >::MinMax(...) - No order in complex");
|
---|
| 276 | }
|
---|
| 277 |
|
---|
| 278 | //! Short ASCII representatio of the object pushed to stream \b os
|
---|
| 279 | template <class T>
|
---|
| 280 | ostream& SpecialSquareMatrix<T>::Show(ostream& os) const
|
---|
| 281 | {
|
---|
| 282 | os << typeid(*this).name() << " < " << typeid(T).name()
|
---|
| 283 | << " > NRow=" << mNrows << " NbElem<>0 : " << Size() << endl;
|
---|
| 284 | return os;
|
---|
| 285 | }
|
---|
| 286 |
|
---|
| 287 |
|
---|
| 288 | ///////////////////////////////////////////////////////////////
|
---|
| 289 | #ifdef __CXX_PRAGMA_TEMPLATES__
|
---|
| 290 | #pragma define_template SpecialSquareMatrix<uint_1>
|
---|
| 291 | #pragma define_template SpecialSquareMatrix<uint_2>
|
---|
| 292 | #pragma define_template SpecialSquareMatrix<uint_4>
|
---|
| 293 | #pragma define_template SpecialSquareMatrix<uint_8>
|
---|
| 294 | #pragma define_template SpecialSquareMatrix<int_1>
|
---|
| 295 | #pragma define_template SpecialSquareMatrix<int_2>
|
---|
| 296 | #pragma define_template SpecialSquareMatrix<int_4>
|
---|
| 297 | #pragma define_template SpecialSquareMatrix<int_8>
|
---|
| 298 | #pragma define_template SpecialSquareMatrix<r_4>
|
---|
| 299 | #pragma define_template SpecialSquareMatrix<r_8>
|
---|
| 300 | #pragma define_template SpecialSquareMatrix< complex<r_4> >
|
---|
| 301 | #pragma define_template SpecialSquareMatrix< complex<r_8> >
|
---|
| 302 | #ifdef SO_LDBLE128
|
---|
| 303 | #pragma define_template SpecialSquareMatrix<r_16>
|
---|
| 304 | #pragma define_template SpecialSquareMatrix< complex<r_16> >
|
---|
| 305 | #endif
|
---|
| 306 | #endif
|
---|
| 307 |
|
---|
| 308 | #if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
|
---|
| 309 | template class SpecialSquareMatrix<uint_1>;
|
---|
| 310 | template class SpecialSquareMatrix<uint_2>;
|
---|
| 311 | template class SpecialSquareMatrix<uint_4>;
|
---|
| 312 | template class SpecialSquareMatrix<uint_8>;
|
---|
| 313 | template class SpecialSquareMatrix<int_1>;
|
---|
| 314 | template class SpecialSquareMatrix<int_2>;
|
---|
| 315 | template class SpecialSquareMatrix<int_4>;
|
---|
| 316 | template class SpecialSquareMatrix<int_8>;
|
---|
| 317 | template class SpecialSquareMatrix<r_4>;
|
---|
| 318 | template class SpecialSquareMatrix<r_8>;
|
---|
| 319 | template class SpecialSquareMatrix< complex<r_4> >;
|
---|
| 320 | template class SpecialSquareMatrix< complex<r_8> >;
|
---|
| 321 | #ifdef SO_LDBLE128
|
---|
| 322 | template class SpecialSquareMatrix<r_16>;
|
---|
| 323 | template class SpecialSquareMatrix< complex<r_16> >;
|
---|
| 324 | #endif
|
---|
| 325 | #endif
|
---|
| 326 |
|
---|
[3824] | 327 | } // namespace SOPHYA
|
---|
| 328 |
|
---|