source: Sophya/trunk/SophyaPI/PIext/nomtmatvecadapter.cc@ 3033

Last change on this file since 3033 was 3033, checked in by ansari, 19 years ago

Quelques corrections/ajout ds methode NObjMgrAdapter::GetInfoString() pour TArray/TMatrix et NTuple/DataTable , Reza 17/7/2006

File size: 12.0 KB
Line 
1#include "sopnamsp.h"
2#include "machdefs.h"
3#include <stdlib.h>
4#include <typeinfo>
5#include <iostream>
6#include <string>
7#include <complex>
8
9#include "datatype.h"
10
11#include "tvector.h"
12#include "objfitter.h"
13#include "nomtmatvecadapter.h"
14#include "piyfxdrw.h"
15#include "pitvmaad.h"
16
17#include "fioarr.h"
18
19#include "nobjmgr.h"
20
21//----------------------------------------------------------------
22// Class Adaptateur d'objet (Pour NamedObjMgr) d'objet TMatrix<T>
23//----------------------------------------------------------------
24
25
26/* --Methode-- */
27template <class T>
28NOMAdapter_TMatrix<T>::NOMAdapter_TMatrix(TMatrix<T>* o)
29 : NObjMgrAdapter(o)
30{
31mMtx = o;
32}
33
34/* --Methode-- */
35template <class T>
36NOMAdapter_TMatrix<T>::~NOMAdapter_TMatrix()
37{
38}
39
40/* --Methode-- */
41template <class T>
42NObjMgrAdapter* NOMAdapter_TMatrix<T>::Clone(AnyDataObj* o)
43{
44TMatrix<T>* m = dynamic_cast<TMatrix<T> *>(o);
45if (m) return ( new NOMAdapter_TMatrix<T>(m) );
46return ( new NObjMgrAdapter(o) );
47}
48
49/* --Methode-- */
50template <class T>
51string NOMAdapter_TMatrix<T>::GetDataObjType()
52{
53string type = "TMatrix< ";
54
55TVector<T>* v = dynamic_cast<TVector<T> *>(mMtx);
56if (v != NULL) type = "TVector< ";
57
58// type += DecodeTypeIdName(typeid(T).name());
59type += DataTypeInfo<T>::getTypeName();
60type += " > ";
61return(type);
62}
63
64/* --Methode-- */
65template <class T>
66AnyDataObj* NOMAdapter_TMatrix<T>::CloneDataObj(bool share)
67{
68if (mMtx == NULL) return(NULL);
69TVector<T>* v = dynamic_cast<TVector<T> *>(mMtx);
70if (v != NULL) return( new TVector<T>(*v, share) );
71else return ( new TMatrix<T>(*mMtx, share) );
72}
73
74/* --Methode-- */
75template <class T>
76string NOMAdapter_TMatrix<T>::GetInfoString(vector<string>& opts)
77{
78 if (opts.size() == 0) return mMtx->InfoString();
79 else {
80 if (opts[0] == "rank") return string("2");
81 else if (opts[0] == "sizes") {
82 char buff[64];
83 TVector<T>* v = dynamic_cast<TVector<T> *>(mMtx);
84 if (v != NULL) sprintf(buff, "%ld", (long)mMtx->Size());
85 else sprintf(buff, "%ld %ld", (long)mMtx->NRows(), (long)mMtx->NCols());
86 return string(buff);
87 }
88 else if ((opts[0] == "size") || (opts[0] == "nelts")) {
89 char buff[32];
90 sprintf(buff, "%ld", (long)mMtx->Size());
91 return string(buff);
92 }
93 else if ((opts[0] == "nrow") || (opts[0] == "nrows")) {
94 char buff[32];
95 sprintf(buff, "%ld", (long)mMtx->NRows());
96 return string(buff);
97 }
98 else if ((opts[0] == "ncol") || (opts[0] == "ncols")) {
99 char buff[32];
100 sprintf(buff, "%ld", (long)mMtx->NCols());
101 return string(buff);
102 }
103 else if (opts[0] == "sum") {
104 MuTyV mtv(mMtx->Sum());
105 string s;
106 return mtv.Convert(s);
107 }
108 else return mMtx->InfoString();
109 }
110}
111
112/* --Methode-- */
113template <class T>
114int NOMAdapter_TMatrix<T>::PerformOperation(vector<string>& opts)
115{
116 bool ok = false;
117 if (opts.size() >= 2) {
118 int_4 kk = atoi(opts[1].c_str());
119 TVector<T> * vrc = new TVector<T>;
120 char buff[40];
121 if ((opts[0] == "row") || (opts[0] == "line")) {
122 vrc->Share(mMtx->Row((sa_size_t)kk));
123 ok = true;
124 sprintf(buff,"row_%ld",(long)kk);
125 cout << "PerformOperation(): Extracting Row(" << kk << ") from matrix" << endl;
126 }
127 else if ((opts[0] == "col") || (opts[0] == "column")) {
128 vrc->Share(mMtx->Column((sa_size_t)kk));
129 ok = true;
130 sprintf(buff,"col_%ld",(long)kk);
131 cout << "PerformOperation(): Extracting Column(" << kk << ") from matrix" << endl;
132 }
133 if (ok) {
134 string nvrc;
135 if (opts.size() > 2) nvrc = opts[2];
136 else nvrc = buff;
137 NamedObjMgr omg;
138 omg.AddObj(vrc, nvrc, true);
139 return 0;
140 }
141 }
142
143 cout << "NOMAdapter_TMatrix<T>::PerformOperation(): Error operation/arguments !" << endl;
144 return 1;
145}
146
147/* --Methode-- */
148template <class T>
149void NOMAdapter_TMatrix<T>::SavePPF(POutPersist& pos, string const & nom)
150{
151if (mMtx == NULL) return;
152TVector<T>* v = dynamic_cast<TVector<T> *>(mMtx);
153if (v != NULL) {
154 FIO_TArray<T> fio(v);
155 fio.Write(pos, nom);
156 }
157else {
158 FIO_TArray<T> fio(mMtx);
159 fio.Write(pos, nom);
160 }
161}
162
163/* --Methode-- */
164template <class T>
165void NOMAdapter_TMatrix<T>::Print(ostream& os, int lev)
166{
167if (lev < 3) mMtx->Show(os, false);
168else mMtx->Show(os, true);
169if (lev > 0) mMtx->Print(os, 10*lev);
170}
171
172/* --Methode-- */
173template <class T>
174PIDrawer* NOMAdapter_TMatrix<T>::GetDrawer(string & dopt)
175{
176TVector<T>* v = dynamic_cast<TVector<T> *>(mMtx);
177if (v == NULL) return(NULL);
178else {
179 dopt = "thinline " + dopt;
180 return( new PIYfXDrawer( new POTVectorAdapter<T>(v, false), NULL, true) );
181 }
182}
183
184/* --Methode-- */
185template <class T>
186P2DArrayAdapter* NOMAdapter_TMatrix<T>::Get2DArray(string &)
187{
188return ( new POTMatrixAdapter<T>(mMtx, false) );
189}
190
191/* --Methode-- */
192template <class T>
193NTupleInterface* NOMAdapter_TMatrix<T>::GetNTupleInterface(bool& adel)
194{
195adel = true;
196return( new NTupInt_TMatrix<T>(mMtx) );
197}
198
199
200/* --Methode-- */
201template <class T>
202GeneralFitData* NOMAdapter_TMatrix<T>::GetGeneralFitData(bool& adel
203 ,GeneralFitData::FitErrType errtype,double errscale,double errmin
204 ,int i1,int i2,int j1,int j2)
205{
206adel = false;
207if(!mMtx) return(NULL);
208
209int nx,ny;
210TVector<T>* vec = dynamic_cast<TVector<T> *>(mMtx);
211
212if(vec) { // C'est un TVector
213 nx = vec->NElts();
214 ny = 1;
215} else {
216 nx = mMtx->NRows();
217 ny = mMtx->NCol();
218 if(nx<=0 || ny<=0) return(NULL);
219}
220
221i1 = (i1<0||i1>=nx)? 0: i1;
222i2 = (i2<0||i2>=nx||i2<i1)? nx-1: i2;
223j1 = (j1<0||j1>=ny)? 0: j1;
224j2 = (j2<0||j2>=ny||j2<j1)? ny-1: j2;
225
226GeneralFitData* mGData = NULL;
227
228if(vec) { // C'est un TVector
229 mGData = new GeneralFitData(1,(i2-i1+1),0);
230 adel = true;
231 for(int i=i1;i<=i2;i++) {
232 double x = (double) i;
233 double f = (*vec)(i);
234 double e = 1.;
235 e = GeneralFitData::ComputeError(f,e,errtype,errscale,errmin);
236 mGData->AddData1(x,f,e);
237 }
238} else {
239 mGData = new GeneralFitData(2,(i2-i1+1)*(j2-j1+1),0);
240 adel = true;
241 for(int i=i1;i<=i2;i++) for(int j=j1;j<=j2;j++) {
242 double x = (double) i;
243 double y = (double) j;
244 double f = (*mMtx)(i,j);
245 double e = 1.;
246 e = GeneralFitData::ComputeError(f,e,errtype,errscale,errmin);
247 mGData->AddData2(x,y,f,e);
248 }
249}
250
251return mGData;
252}
253
254template <class T>
255AnyDataObj* NOMAdapter_TMatrix<T>::FitResidusObj(GeneralFit& mfit)
256{
257TVector<T>* vec = dynamic_cast<TVector<T> *>(mMtx);
258if(vec) {
259 TVector<T>* v = new TVector<T>(ObjectFitter::FitResidus(*vec,mfit),true);
260 return v;
261} else {
262 TMatrix<T>* m = new TMatrix<T>(ObjectFitter::FitResidus(*mMtx,mfit),true);
263 return m;
264}
265}
266
267template <class T>
268AnyDataObj* NOMAdapter_TMatrix<T>::FitFunctionObj(GeneralFit& mfit)
269{
270TVector<T>* vec = dynamic_cast<TVector<T> *>(mMtx);
271if(vec) {
272 TVector<T>* v = new TVector<T>(ObjectFitter::FitFunction(*vec,mfit),true);
273 return v;
274} else {
275 TMatrix<T>* m = new TMatrix<T>(ObjectFitter::FitFunction(*mMtx,mfit),true);
276 return m;
277}
278}
279
280// ---- Specialisation pour complexes -----
281DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
282GeneralFitData* NOMAdapter_TMatrix< complex<r_4> >::GetGeneralFitData(bool& adel
283 ,GeneralFitData::FitErrType errtype,double errscale,double errmin
284 ,int i1,int i2,int j1,int j2)
285{
286 return(NULL);
287}
288
289DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
290AnyDataObj* NOMAdapter_TMatrix< complex<r_4> >::FitResidusObj(GeneralFit& mfit)
291{
292 return(NULL);
293}
294
295DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
296AnyDataObj* NOMAdapter_TMatrix< complex<r_4> >::FitFunctionObj(GeneralFit& mfit)
297{
298 return(NULL);
299}
300
301DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
302GeneralFitData* NOMAdapter_TMatrix< complex<r_8> >::GetGeneralFitData(bool& adel
303 ,GeneralFitData::FitErrType errtype,double errscale,double errmin
304 ,int i1,int i2,int j1,int j2)
305{
306 return(NULL);
307}
308
309DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
310AnyDataObj* NOMAdapter_TMatrix< complex<r_8> >::FitResidusObj(GeneralFit& mfit)
311{
312 return(NULL);
313}
314
315DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
316AnyDataObj* NOMAdapter_TMatrix< complex<r_8> >::FitFunctionObj(GeneralFit& mfit)
317{
318 return(NULL);
319}
320
321// -------------------------------------------------------------
322
323/* --Methode-- */
324template <class T>
325NTupInt_TMatrix<T>::NTupInt_TMatrix(TMatrix<T>* m)
326{
327mMtx = m;
328}
329
330/* --Methode-- */
331template <class T>
332NTupInt_TMatrix<T>::~NTupInt_TMatrix()
333{
334}
335
336/* --Methode-- */
337template <class T>
338sa_size_t NTupInt_TMatrix<T>::NbLines() const
339{
340return( mMtx->NRows()*mMtx->NCols() );
341}
342
343/* --Methode-- */
344template <class T>
345sa_size_t NTupInt_TMatrix<T>::NbColumns() const
346{
347return(8);
348}
349
350/* --Methode-- */
351template <class T>
352r_8* NTupInt_TMatrix<T>::GetLineD(sa_size_t n) const
353{
354int i,j;
355if ((n < 0) || (n >= (int)(mMtx->NRows()*mMtx->NCols()) )) {
356 mRet[0] = n;
357 for(i=1; i<8; i++) mRet[i] = 0.;
358}
359else {
360 i = n/mMtx->NCols(); j = n%mMtx->NCols();
361 mRet[0] = n; mRet[1] = i; mRet[2] = j;
362 mRet[3] = (*mMtx)(i,j);
363 mRet[4] = mRet[2]; mRet[5] = 0.;
364 mRet[6] = mRet[2]; mRet[7] = 0.;
365 }
366return(mRet);
367}
368
369/* --Methode-- */
370template <class T>
371string NTupInt_TMatrix<T>::VarList_C(const char* nx) const
372{
373string nomx;
374if (nx) nomx = nx;
375else nomx = "_xh_";
376string vardec = "double n,r,c,val,real,imag,mod,phas; \n";
377vardec += "n = " + nomx + "[0]; r = " + nomx + "[1]; c = " + nomx + "[2]; \n";
378vardec += "val = " + nomx + "[3]; \n";
379vardec += "real = " + nomx + "[4]; imag = " + nomx + "[5]; \n";
380vardec += "mod = " + nomx + "[6]; phas = " + nomx + "[7]; \n";
381return(vardec);
382}
383
384/* --Methode-- */
385DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
386r_8* NTupInt_TMatrix< complex<r_4> >::GetLineD(sa_size_t n) const
387{
388int i,j;
389if ((n < 0) || (n >= (int)(mMtx->NRows()*mMtx->NCols()) )) {
390 mRet[0] = n;
391 for(i=1; i<8; i++) mRet[i] = 0.;
392}
393else {
394 i = n/mMtx->NCols(); j = n%mMtx->NCols();
395 mRet[0] = n; mRet[1] = i; mRet[2] = j;
396 mRet[4] = (*mMtx)(i,j).real(); mRet[5] = (*mMtx)(i,j).imag();
397 mRet[3] = mRet[6] = sqrt(mRet[4]*mRet[4]+mRet[5]*mRet[5]);
398 mRet[7] = atan2(mRet[5], mRet[4]);
399}
400return(mRet);
401}
402
403DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
404r_8* NTupInt_TMatrix< complex<r_8> >::GetLineD(sa_size_t n) const
405{
406int i,j;
407if ((n < 0) || (n >= (int)(mMtx->NRows()*mMtx->NCols()) )) {
408 mRet[0] = n;
409 for(i=1; i<8; i++) mRet[i] = 0.;
410}
411else {
412 i = n/mMtx->NCols(); j = n%mMtx->NCols();
413 mRet[0] = n; mRet[1] = i; mRet[2] = j;
414 mRet[4] = (*mMtx)(i,j).real(); mRet[5] = (*mMtx)(i,j).imag();
415 mRet[3] = mRet[6] = sqrt(mRet[4]*mRet[4]+mRet[5]*mRet[5]);
416 mRet[7] = atan2(mRet[5], mRet[4]);
417}
418return(mRet);
419}
420
421
422#ifdef __CXX_PRAGMA_TEMPLATES__
423#pragma define_template NOMAdapter_TMatrix<uint_2>
424#pragma define_template NOMAdapter_TMatrix<int_2>
425#pragma define_template NOMAdapter_TMatrix<int_4>
426#pragma define_template NOMAdapter_TMatrix<int_8>
427#pragma define_template NOMAdapter_TMatrix<r_4>
428#pragma define_template NOMAdapter_TMatrix<r_8>
429#pragma define_template NOMAdapter_TMatrix< complex<r_4> >
430#pragma define_template NOMAdapter_TMatrix< complex<r_8> >
431#pragma define_template NTupInt_TMatrix<uint_2>
432#pragma define_template NTupInt_TMatrix<int_2>
433#pragma define_template NTupInt_TMatrix<int_4>
434#pragma define_template NTupInt_TMatrix<int_8>
435#pragma define_template NTupInt_TMatrix<r_4>
436#pragma define_template NTupInt_TMatrix<r_8>
437#pragma define_template NTupInt_TMatrix< complex<r_4> >
438#pragma define_template NTupInt_TMatrix< complex<r_8> >
439#endif
440
441#if defined(ANSI_TEMPLATES)
442template class NOMAdapter_TMatrix<uint_2>;
443template class NOMAdapter_TMatrix<int_2>;
444template class NOMAdapter_TMatrix<int_4>;
445template class NOMAdapter_TMatrix<int_8>;
446template class NOMAdapter_TMatrix<r_4>;
447template class NOMAdapter_TMatrix<r_8>;
448template class NOMAdapter_TMatrix< complex<r_4> >;
449template class NOMAdapter_TMatrix< complex<r_8> >;
450template class NTupInt_TMatrix<uint_2>;
451template class NTupInt_TMatrix<int_2>;
452template class NTupInt_TMatrix<int_4>;
453template class NTupInt_TMatrix<int_8>;
454template class NTupInt_TMatrix<r_4>;
455template class NTupInt_TMatrix<r_8>;
456template class NTupInt_TMatrix< complex<r_4> >;
457template class NTupInt_TMatrix< complex<r_8> >;
458#endif
Note: See TracBrowser for help on using the repository browser.