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

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

Introduction et gestion du flag preprocesseur NEED_EXT_DECL_TEMP pour declaration extern des classes template avec instantiation explicite (pb dynamic_cast sur Mac OS 10.6), Reza 05/08/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#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
26namespace SOPHYA {
27
28//! Default constructor - SpecialSquareMatrix of size 0, SetSize() should be called before the object is used
29template <class T>
30SpecialSquareMatrix<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)
36template <class T>
37SpecialSquareMatrix<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)
45template <class T>
46SpecialSquareMatrix<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
53template <class T>
54void 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
60template <class T>
61TMatrix<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
70template <class T>
71void 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
82template <class T>
83void 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
94template <class T>
95void 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
108template <class T>
109SpecialSquareMatrix<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
119template <class T>
120SpecialSquareMatrix<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
138template <class T>
139SpecialSquareMatrix<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
148template <class T>
149SpecialSquareMatrix<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
158template <class T>
159SpecialSquareMatrix<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 .
170template <class T>
171SpecialSquareMatrix<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
180template <class T>
181SpecialSquareMatrix<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)
191template <class T>
192SpecialSquareMatrix<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)
203template <class T>
204SpecialSquareMatrix<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)
214template <class T>
215SpecialSquareMatrix<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)
225template <class T>
226SpecialSquareMatrix<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*/
241template <class T>
242void 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
254DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
255void 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}
259DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
260void 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
266template <class T>
267ostream& 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)
296template class SpecialSquareMatrix<uint_1>;
297template class SpecialSquareMatrix<uint_2>;
298template class SpecialSquareMatrix<uint_4>;
299template class SpecialSquareMatrix<uint_8>;
300template class SpecialSquareMatrix<int_1>;
301template class SpecialSquareMatrix<int_2>;
302template class SpecialSquareMatrix<int_4>;
303template class SpecialSquareMatrix<int_8>;
304template class SpecialSquareMatrix<r_4>;
305template class SpecialSquareMatrix<r_8>;
306template class SpecialSquareMatrix< complex<r_4> >;
307template class SpecialSquareMatrix< complex<r_8> >;
308#ifdef SO_LDBLE128
309template class SpecialSquareMatrix<r_16>;
310template class SpecialSquareMatrix< complex<r_16> >;
311#endif
312#endif
313
314} // namespace SOPHYA
315
Note: See TracBrowser for help on using the repository browser.