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