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

Last change on this file since 3263 was 3222, checked in by ansari, 18 years ago

++ Prise en charge des objets Image<T> par l'adaptateur des TMatrix NOMAdapter_TMatrix ds nomtmatvecadapter.cc, Reza 12/04/2007

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