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