source: Sophya/trunk/SophyaLib/TArray/spesqmtx.cc@ 3809

Last change on this file since 3809 was 3809, checked in by ansari, 15 years ago

1/ Ajout fichiers / classes de matrices carrees speciales (DiagonalMatrix<T>,

SymmetricMatrix<T> LowerTriangularMatrix<T>

2/ Suppression fichier triangmtx.h et la classe TriangularMatrix<T>
3/ adaptation array.h et enregistrement handlers PPF

Reza, 26/07/2010

File size: 11.0 KB
Line 
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
25namespace SOPHYA {
26
27//! Default constructor - SpecialSquareMatrix of size 0, SetSize() should be called before the object is used
28template <class T>
29SpecialSquareMatrix<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)
35template <class T>
36SpecialSquareMatrix<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)
44template <class T>
45SpecialSquareMatrix<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
52template <class T>
53void 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
59template <class T>
60TMatrix<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
69template <class T>
70void 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
81template <class T>
82void 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
93template <class T>
94void 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
107template <class T>
108SpecialSquareMatrix<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
118template <class T>
119SpecialSquareMatrix<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
137template <class T>
138SpecialSquareMatrix<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
147template <class T>
148SpecialSquareMatrix<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
157template <class T>
158SpecialSquareMatrix<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 .
169template <class T>
170SpecialSquareMatrix<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
179template <class T>
180SpecialSquareMatrix<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)
190template <class T>
191SpecialSquareMatrix<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)
202template <class T>
203SpecialSquareMatrix<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)
213template <class T>
214SpecialSquareMatrix<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)
224template <class T>
225SpecialSquareMatrix<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*/
240template <class T>
241void 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
253DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
254void 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}
258DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
259void 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
265template <class T>
266ostream& 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} // namespace SOPHYA
275
276///////////////////////////////////////////////////////////////
277#ifdef __CXX_PRAGMA_TEMPLATES__
278#pragma define_template SpecialSquareMatrix<uint_1>
279#pragma define_template SpecialSquareMatrix<uint_2>
280#pragma define_template SpecialSquareMatrix<uint_4>
281#pragma define_template SpecialSquareMatrix<uint_8>
282#pragma define_template SpecialSquareMatrix<int_1>
283#pragma define_template SpecialSquareMatrix<int_2>
284#pragma define_template SpecialSquareMatrix<int_4>
285#pragma define_template SpecialSquareMatrix<int_8>
286#pragma define_template SpecialSquareMatrix<r_4>
287#pragma define_template SpecialSquareMatrix<r_8>
288#pragma define_template SpecialSquareMatrix< complex<r_4> >
289#pragma define_template SpecialSquareMatrix< complex<r_8> >
290#ifdef SO_LDBLE128
291#pragma define_template SpecialSquareMatrix<r_16>
292#pragma define_template SpecialSquareMatrix< complex<r_16> >
293#endif
294#endif
295
296#if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
297namespace SOPHYA {
298template class SpecialSquareMatrix<uint_1>;
299template class SpecialSquareMatrix<uint_2>;
300template class SpecialSquareMatrix<uint_4>;
301template class SpecialSquareMatrix<uint_8>;
302template class SpecialSquareMatrix<int_1>;
303template class SpecialSquareMatrix<int_2>;
304template class SpecialSquareMatrix<int_4>;
305template class SpecialSquareMatrix<int_8>;
306template class SpecialSquareMatrix<r_4>;
307template class SpecialSquareMatrix<r_8>;
308template class SpecialSquareMatrix< complex<r_4> >;
309template class SpecialSquareMatrix< complex<r_8> >;
310#ifdef SO_LDBLE128
311template class SpecialSquareMatrix<r_16>;
312template class SpecialSquareMatrix< complex<r_16> >;
313#endif
314}
315#endif
316
Note: See TracBrowser for help on using the repository browser.