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

Last change on this file since 3666 was 3661, checked in by cmv, 16 years ago
  • ajout des TArray/TMatrix/TVector <uint_1> et <int_1>
  • cet ajout n'a pas ete porte dans Image<T>
  • correction petit bug:

inline int_4 Convert(int_2& x) const {...}
-> inline int_2 Convert(int_2& x) const {...}

cmv 23/10/2009

File size: 15.0 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 }
[3333]119 else if (opts[0] == "sumsq") {
120 MuTyV mtv(mMtx->SumSq());
121 string s;
122 return mtv.Convert(s);
123 }
124 else if (opts[0] == "norm2") {
125 MuTyV mtv(mMtx->Norm2());
[3335]126 r_8 nrm2 = mtv.Convert(nrm2); // we force the conversion to double
127 mtv = nrm2;
[3333]128 string s;
129 return mtv.Convert(s);
130 }
131 else if ((opts[0] == "min")||(opts[0] == "max")||(opts[0] == "minmax")) {
132 T amin, amax;
133 MuTyV mtv;
134 string s;
135 mMtx->MinMax(amin, amax);
136 if (opts[0] == "minmax") {
137 mtv = amin; mtv.Convert(s);
138 s += " ";
139 string mms;
140 mtv = amax; mtv.Convert(mms);
141 s += mms;
142 }
143 else {
144 if (opts[0] == "min") mtv = amin;
145 else if (opts[0] == "max") mtv = amax;
146 mtv.Convert(s);
147 }
148 return s;
149 }
[3221]150 else if (opts[0] == "info") { // Acces aux valeurs stockes ds le DVList Info()
151 if (opts.size() < 2) return string("");
152 else if (mMtx->Info().HasKey(opts[1]) == false) return string("");
153 else return mMtx->Info().GetS(opts[1]);
154 }
[3333]155 else return "TMatrix.Att: rank size/nelts nrow/nrows ncol/ncols sum sumsq norm2 min max minmax info.varname";
[2999]156 }
157}
[1321]158
159/* --Methode-- */
160template <class T>
[2999]161int NOMAdapter_TMatrix<T>::PerformOperation(vector<string>& opts)
162{
163 bool ok = false;
164 if (opts.size() >= 2) {
165 int_4 kk = atoi(opts[1].c_str());
166 TVector<T> * vrc = new TVector<T>;
167 char buff[40];
168 if ((opts[0] == "row") || (opts[0] == "line")) {
169 vrc->Share(mMtx->Row((sa_size_t)kk));
170 ok = true;
171 sprintf(buff,"row_%ld",(long)kk);
172 cout << "PerformOperation(): Extracting Row(" << kk << ") from matrix" << endl;
173 }
174 else if ((opts[0] == "col") || (opts[0] == "column")) {
175 vrc->Share(mMtx->Column((sa_size_t)kk));
176 ok = true;
177 sprintf(buff,"col_%ld",(long)kk);
178 cout << "PerformOperation(): Extracting Column(" << kk << ") from matrix" << endl;
179 }
180 if (ok) {
181 string nvrc;
182 if (opts.size() > 2) nvrc = opts[2];
183 else nvrc = buff;
184 NamedObjMgr omg;
185 omg.AddObj(vrc, nvrc, true);
186 return 0;
187 }
188 }
189
[3039]190 cout << "NOMAdapter_TMatrix<T>::PerformOperation(): Error operation/arguments !" << endl;
191 cout << "...Valid args: row/line r [rowname] , col/column c [rowname]" << endl;
[2999]192 return 1;
193}
194
195/* --Methode-- */
196template <class T>
[585]197void NOMAdapter_TMatrix<T>::SavePPF(POutPersist& pos, string const & nom)
198{
199if (mMtx == NULL) return;
200TVector<T>* v = dynamic_cast<TVector<T> *>(mMtx);
201if (v != NULL) {
[810]202 FIO_TArray<T> fio(v);
[585]203 fio.Write(pos, nom);
[3221]204}
[585]205else {
[3221]206 Image<T>* img = dynamic_cast<Image<T> *>(mMtx);
207 if (img != NULL) {
208 FIO_Image<T> fio(img);
209 fio.Write(pos, nom);
[585]210 }
[3221]211 else {
212 FIO_TArray<T> fio(mMtx);
213 fio.Write(pos, nom);
214 }
[585]215}
[3221]216return;
217}
[585]218
219/* --Methode-- */
220template <class T>
[2975]221void NOMAdapter_TMatrix<T>::Print(ostream& os, int lev)
[585]222{
[2975]223if (lev < 3) mMtx->Show(os, false);
224else mMtx->Show(os, true);
225if (lev > 0) mMtx->Print(os, 10*lev);
[585]226}
227
228/* --Methode-- */
229template <class T>
230PIDrawer* NOMAdapter_TMatrix<T>::GetDrawer(string & dopt)
231{
232TVector<T>* v = dynamic_cast<TVector<T> *>(mMtx);
233if (v == NULL) return(NULL);
234else {
[1971]235 dopt = "thinline " + dopt;
[585]236 return( new PIYfXDrawer( new POTVectorAdapter<T>(v, false), NULL, true) );
237 }
238}
239
240/* --Methode-- */
241template <class T>
242P2DArrayAdapter* NOMAdapter_TMatrix<T>::Get2DArray(string &)
243{
[3221]244Image<T>* img = dynamic_cast<Image<T> *>(mMtx);
245if (img != NULL) return ( new ImageAdapter<T>(img, false) );
246else return ( new POTMatrixAdapter<T>(mMtx, false) );
[585]247}
248
[3221]249// ---- Specialisation pour complexes -----
250DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
251P2DArrayAdapter* NOMAdapter_TMatrix< complex<r_4> >::Get2DArray(string &)
252{
253return ( new POTMatrixAdapter< complex<r_4> >(mMtx, false) );
254}
255DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
256P2DArrayAdapter* NOMAdapter_TMatrix< complex<r_8> >::Get2DArray(string &)
257{
258return ( new POTMatrixAdapter< complex<r_8> >(mMtx, false) );
259}
260// -------------------------------------------------------------
261
[585]262/* --Methode-- */
263template <class T>
264NTupleInterface* NOMAdapter_TMatrix<T>::GetNTupleInterface(bool& adel)
265{
266adel = true;
267return( new NTupInt_TMatrix<T>(mMtx) );
268}
269
270
[1207]271/* --Methode-- */
272template <class T>
273GeneralFitData* NOMAdapter_TMatrix<T>::GetGeneralFitData(bool& adel
274 ,GeneralFitData::FitErrType errtype,double errscale,double errmin
275 ,int i1,int i2,int j1,int j2)
276{
277adel = false;
278if(!mMtx) return(NULL);
[585]279
[1207]280int nx,ny;
281TVector<T>* vec = dynamic_cast<TVector<T> *>(mMtx);
282
283if(vec) { // C'est un TVector
284 nx = vec->NElts();
285 ny = 1;
286} else {
287 nx = mMtx->NRows();
288 ny = mMtx->NCol();
289 if(nx<=0 || ny<=0) return(NULL);
290}
291
292i1 = (i1<0||i1>=nx)? 0: i1;
293i2 = (i2<0||i2>=nx||i2<i1)? nx-1: i2;
294j1 = (j1<0||j1>=ny)? 0: j1;
295j2 = (j2<0||j2>=ny||j2<j1)? ny-1: j2;
296
297GeneralFitData* mGData = NULL;
298
299if(vec) { // C'est un TVector
300 mGData = new GeneralFitData(1,(i2-i1+1),0);
301 adel = true;
302 for(int i=i1;i<=i2;i++) {
303 double x = (double) i;
304 double f = (*vec)(i);
305 double e = 1.;
306 e = GeneralFitData::ComputeError(f,e,errtype,errscale,errmin);
307 mGData->AddData1(x,f,e);
308 }
309} else {
310 mGData = new GeneralFitData(2,(i2-i1+1)*(j2-j1+1),0);
311 adel = true;
312 for(int i=i1;i<=i2;i++) for(int j=j1;j<=j2;j++) {
313 double x = (double) i;
314 double y = (double) j;
315 double f = (*mMtx)(i,j);
316 double e = 1.;
317 e = GeneralFitData::ComputeError(f,e,errtype,errscale,errmin);
318 mGData->AddData2(x,y,f,e);
319 }
320}
321
322return mGData;
323}
324
325template <class T>
326AnyDataObj* NOMAdapter_TMatrix<T>::FitResidusObj(GeneralFit& mfit)
327{
[3528]328ArrayFitter<T> arrfit;
[1207]329TVector<T>* vec = dynamic_cast<TVector<T> *>(mMtx);
330if(vec) {
[3528]331 TVector<T>* v = new TVector<T>(arrfit.FitResidus(*vec, mfit),true);
[1207]332 return v;
333} else {
[3528]334 TMatrix<T>* m = new TMatrix<T>(arrfit.FitResidus(*mMtx,mfit),true);
[1207]335 return m;
336}
337}
338
339template <class T>
340AnyDataObj* NOMAdapter_TMatrix<T>::FitFunctionObj(GeneralFit& mfit)
341{
[3528]342ArrayFitter<T> arrfit;
[1207]343TVector<T>* vec = dynamic_cast<TVector<T> *>(mMtx);
344if(vec) {
[3528]345 TVector<T>* v = new TVector<T>(arrfit.FitFunction(*vec,mfit),true);
[1207]346 return v;
347} else {
[3528]348 TMatrix<T>* m = new TMatrix<T>(arrfit.FitFunction(*mMtx,mfit),true);
[1207]349 return m;
350}
351}
352
[1315]353// ---- Specialisation pour complexes -----
[2343]354DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
[1315]355GeneralFitData* NOMAdapter_TMatrix< complex<r_4> >::GetGeneralFitData(bool& adel
356 ,GeneralFitData::FitErrType errtype,double errscale,double errmin
357 ,int i1,int i2,int j1,int j2)
358{
359 return(NULL);
360}
[1207]361
[2343]362DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
[1315]363AnyDataObj* NOMAdapter_TMatrix< complex<r_4> >::FitResidusObj(GeneralFit& mfit)
364{
365 return(NULL);
366}
367
[2343]368DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
[1315]369AnyDataObj* NOMAdapter_TMatrix< complex<r_4> >::FitFunctionObj(GeneralFit& mfit)
370{
371 return(NULL);
372}
373
[2343]374DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
[1315]375GeneralFitData* NOMAdapter_TMatrix< complex<r_8> >::GetGeneralFitData(bool& adel
376 ,GeneralFitData::FitErrType errtype,double errscale,double errmin
377 ,int i1,int i2,int j1,int j2)
378{
379 return(NULL);
380}
381
[2343]382DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
[1315]383AnyDataObj* NOMAdapter_TMatrix< complex<r_8> >::FitResidusObj(GeneralFit& mfit)
384{
385 return(NULL);
386}
387
[2343]388DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
[1315]389AnyDataObj* NOMAdapter_TMatrix< complex<r_8> >::FitFunctionObj(GeneralFit& mfit)
390{
391 return(NULL);
392}
393
[585]394// -------------------------------------------------------------
395
396/* --Methode-- */
397template <class T>
398NTupInt_TMatrix<T>::NTupInt_TMatrix(TMatrix<T>* m)
399{
400mMtx = m;
401}
402
403/* --Methode-- */
404template <class T>
405NTupInt_TMatrix<T>::~NTupInt_TMatrix()
406{
407}
408
409/* --Methode-- */
410template <class T>
[2683]411sa_size_t NTupInt_TMatrix<T>::NbLines() const
[585]412{
413return( mMtx->NRows()*mMtx->NCols() );
414}
415
416/* --Methode-- */
417template <class T>
[2683]418sa_size_t NTupInt_TMatrix<T>::NbColumns() const
[585]419{
420return(8);
421}
422
423/* --Methode-- */
424template <class T>
[2683]425r_8* NTupInt_TMatrix<T>::GetLineD(sa_size_t n) const
[585]426{
427int i,j;
428if ((n < 0) || (n >= (int)(mMtx->NRows()*mMtx->NCols()) )) {
429 mRet[0] = n;
430 for(i=1; i<8; i++) mRet[i] = 0.;
431}
432else {
433 i = n/mMtx->NCols(); j = n%mMtx->NCols();
434 mRet[0] = n; mRet[1] = i; mRet[2] = j;
435 mRet[3] = (*mMtx)(i,j);
436 mRet[4] = mRet[2]; mRet[5] = 0.;
437 mRet[6] = mRet[2]; mRet[7] = 0.;
438 }
439return(mRet);
440}
441
442/* --Methode-- */
443template <class T>
444string NTupInt_TMatrix<T>::VarList_C(const char* nx) const
445{
446string nomx;
447if (nx) nomx = nx;
448else nomx = "_xh_";
449string vardec = "double n,r,c,val,real,imag,mod,phas; \n";
450vardec += "n = " + nomx + "[0]; r = " + nomx + "[1]; c = " + nomx + "[2]; \n";
451vardec += "val = " + nomx + "[3]; \n";
452vardec += "real = " + nomx + "[4]; imag = " + nomx + "[5]; \n";
453vardec += "mod = " + nomx + "[6]; phas = " + nomx + "[7]; \n";
454return(vardec);
455}
456
457/* --Methode-- */
[2343]458DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
[2689]459r_8* NTupInt_TMatrix< complex<r_4> >::GetLineD(sa_size_t n) const
[585]460{
461int i,j;
462if ((n < 0) || (n >= (int)(mMtx->NRows()*mMtx->NCols()) )) {
463 mRet[0] = n;
464 for(i=1; i<8; i++) mRet[i] = 0.;
465}
466else {
467 i = n/mMtx->NCols(); j = n%mMtx->NCols();
468 mRet[0] = n; mRet[1] = i; mRet[2] = j;
469 mRet[4] = (*mMtx)(i,j).real(); mRet[5] = (*mMtx)(i,j).imag();
470 mRet[3] = mRet[6] = sqrt(mRet[4]*mRet[4]+mRet[5]*mRet[5]);
471 mRet[7] = atan2(mRet[5], mRet[4]);
472}
473return(mRet);
474}
475
[2343]476DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
[2689]477r_8* NTupInt_TMatrix< complex<r_8> >::GetLineD(sa_size_t n) const
[585]478{
479int i,j;
480if ((n < 0) || (n >= (int)(mMtx->NRows()*mMtx->NCols()) )) {
481 mRet[0] = n;
482 for(i=1; i<8; i++) mRet[i] = 0.;
483}
484else {
485 i = n/mMtx->NCols(); j = n%mMtx->NCols();
486 mRet[0] = n; mRet[1] = i; mRet[2] = j;
487 mRet[4] = (*mMtx)(i,j).real(); mRet[5] = (*mMtx)(i,j).imag();
488 mRet[3] = mRet[6] = sqrt(mRet[4]*mRet[4]+mRet[5]*mRet[5]);
489 mRet[7] = atan2(mRet[5], mRet[4]);
490}
491return(mRet);
492}
493
494
495#ifdef __CXX_PRAGMA_TEMPLATES__
[3661]496#pragma define_template NOMAdapter_TMatrix<uint_1>
[2930]497#pragma define_template NOMAdapter_TMatrix<uint_2>
[3528]498#pragma define_template NOMAdapter_TMatrix<uint_4>
499#pragma define_template NOMAdapter_TMatrix<uint_8>
[3661]500#pragma define_template NOMAdapter_TMatrix<int_1>
[2930]501#pragma define_template NOMAdapter_TMatrix<int_2>
[585]502#pragma define_template NOMAdapter_TMatrix<int_4>
[2930]503#pragma define_template NOMAdapter_TMatrix<int_8>
[585]504#pragma define_template NOMAdapter_TMatrix<r_4>
505#pragma define_template NOMAdapter_TMatrix<r_8>
[1315]506#pragma define_template NOMAdapter_TMatrix< complex<r_4> >
507#pragma define_template NOMAdapter_TMatrix< complex<r_8> >
[3661]508#pragma define_template NTupInt_TMatrix<uint_1>
[2930]509#pragma define_template NTupInt_TMatrix<uint_2>
[3528]510#pragma define_template NTupInt_TMatrix<uint_4>
511#pragma define_template NTupInt_TMatrix<uint_8>
[3661]512#pragma define_template NTupInt_TMatrix<int_1>
[2930]513#pragma define_template NTupInt_TMatrix<int_2>
[585]514#pragma define_template NTupInt_TMatrix<int_4>
[2930]515#pragma define_template NTupInt_TMatrix<int_8>
[585]516#pragma define_template NTupInt_TMatrix<r_4>
517#pragma define_template NTupInt_TMatrix<r_8>
[719]518#pragma define_template NTupInt_TMatrix< complex<r_4> >
519#pragma define_template NTupInt_TMatrix< complex<r_8> >
[585]520#endif
521
522#if defined(ANSI_TEMPLATES)
[3661]523template class NOMAdapter_TMatrix<uint_1>;
[2930]524template class NOMAdapter_TMatrix<uint_2>;
[3528]525template class NOMAdapter_TMatrix<uint_4>;
526template class NOMAdapter_TMatrix<uint_8>;
[3661]527template class NOMAdapter_TMatrix<int_1>;
[2930]528template class NOMAdapter_TMatrix<int_2>;
[585]529template class NOMAdapter_TMatrix<int_4>;
[2930]530template class NOMAdapter_TMatrix<int_8>;
[585]531template class NOMAdapter_TMatrix<r_4>;
532template class NOMAdapter_TMatrix<r_8>;
[1315]533template class NOMAdapter_TMatrix< complex<r_4> >;
534template class NOMAdapter_TMatrix< complex<r_8> >;
[3661]535template class NTupInt_TMatrix<uint_1>;
[2930]536template class NTupInt_TMatrix<uint_2>;
[3528]537template class NTupInt_TMatrix<uint_4>;
538template class NTupInt_TMatrix<uint_8>;
[3661]539template class NTupInt_TMatrix<int_1>;
[2930]540template class NTupInt_TMatrix<int_2>;
[585]541template class NTupInt_TMatrix<int_4>;
[2930]542template class NTupInt_TMatrix<int_8>;
[585]543template class NTupInt_TMatrix<r_4>;
544template class NTupInt_TMatrix<r_8>;
[719]545template class NTupInt_TMatrix< complex<r_4> >;
546template class NTupInt_TMatrix< complex<r_8> >;
[585]547#endif
Note: See TracBrowser for help on using the repository browser.