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

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

Ajout decodage nouveaux attributs (min max sumsq norm2) pour tableaux/matrices/vecteurs (${tab.norm2} ${tab.min} ...) - Reza 02/10/2007 "

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