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

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

forcage conversion Norm2() en double ds NomAdapter array/matrices, Reza 02/10/2007

File size: 14.3 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{
328TVector<T>* vec = dynamic_cast<TVector<T> *>(mMtx);
329if(vec) {
330 TVector<T>* v = new TVector<T>(ObjectFitter::FitResidus(*vec,mfit),true);
331 return v;
332} else {
333 TMatrix<T>* m = new TMatrix<T>(ObjectFitter::FitResidus(*mMtx,mfit),true);
334 return m;
335}
336}
337
338template <class T>
339AnyDataObj* NOMAdapter_TMatrix<T>::FitFunctionObj(GeneralFit& mfit)
340{
341TVector<T>* vec = dynamic_cast<TVector<T> *>(mMtx);
342if(vec) {
343 TVector<T>* v = new TVector<T>(ObjectFitter::FitFunction(*vec,mfit),true);
344 return v;
345} else {
346 TMatrix<T>* m = new TMatrix<T>(ObjectFitter::FitFunction(*mMtx,mfit),true);
347 return m;
348}
349}
350
[1315]351// ---- Specialisation pour complexes -----
[2343]352DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
[1315]353GeneralFitData* NOMAdapter_TMatrix< complex<r_4> >::GetGeneralFitData(bool& adel
354 ,GeneralFitData::FitErrType errtype,double errscale,double errmin
355 ,int i1,int i2,int j1,int j2)
356{
357 return(NULL);
358}
[1207]359
[2343]360DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
[1315]361AnyDataObj* NOMAdapter_TMatrix< complex<r_4> >::FitResidusObj(GeneralFit& mfit)
362{
363 return(NULL);
364}
365
[2343]366DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
[1315]367AnyDataObj* NOMAdapter_TMatrix< complex<r_4> >::FitFunctionObj(GeneralFit& mfit)
368{
369 return(NULL);
370}
371
[2343]372DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
[1315]373GeneralFitData* NOMAdapter_TMatrix< complex<r_8> >::GetGeneralFitData(bool& adel
374 ,GeneralFitData::FitErrType errtype,double errscale,double errmin
375 ,int i1,int i2,int j1,int j2)
376{
377 return(NULL);
378}
379
[2343]380DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
[1315]381AnyDataObj* NOMAdapter_TMatrix< complex<r_8> >::FitResidusObj(GeneralFit& mfit)
382{
383 return(NULL);
384}
385
[2343]386DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
[1315]387AnyDataObj* NOMAdapter_TMatrix< complex<r_8> >::FitFunctionObj(GeneralFit& mfit)
388{
389 return(NULL);
390}
391
[585]392// -------------------------------------------------------------
393
394/* --Methode-- */
395template <class T>
396NTupInt_TMatrix<T>::NTupInt_TMatrix(TMatrix<T>* m)
397{
398mMtx = m;
399}
400
401/* --Methode-- */
402template <class T>
403NTupInt_TMatrix<T>::~NTupInt_TMatrix()
404{
405}
406
407/* --Methode-- */
408template <class T>
[2683]409sa_size_t NTupInt_TMatrix<T>::NbLines() const
[585]410{
411return( mMtx->NRows()*mMtx->NCols() );
412}
413
414/* --Methode-- */
415template <class T>
[2683]416sa_size_t NTupInt_TMatrix<T>::NbColumns() const
[585]417{
418return(8);
419}
420
421/* --Methode-- */
422template <class T>
[2683]423r_8* NTupInt_TMatrix<T>::GetLineD(sa_size_t n) const
[585]424{
425int i,j;
426if ((n < 0) || (n >= (int)(mMtx->NRows()*mMtx->NCols()) )) {
427 mRet[0] = n;
428 for(i=1; i<8; i++) mRet[i] = 0.;
429}
430else {
431 i = n/mMtx->NCols(); j = n%mMtx->NCols();
432 mRet[0] = n; mRet[1] = i; mRet[2] = j;
433 mRet[3] = (*mMtx)(i,j);
434 mRet[4] = mRet[2]; mRet[5] = 0.;
435 mRet[6] = mRet[2]; mRet[7] = 0.;
436 }
437return(mRet);
438}
439
440/* --Methode-- */
441template <class T>
442string NTupInt_TMatrix<T>::VarList_C(const char* nx) const
443{
444string nomx;
445if (nx) nomx = nx;
446else nomx = "_xh_";
447string vardec = "double n,r,c,val,real,imag,mod,phas; \n";
448vardec += "n = " + nomx + "[0]; r = " + nomx + "[1]; c = " + nomx + "[2]; \n";
449vardec += "val = " + nomx + "[3]; \n";
450vardec += "real = " + nomx + "[4]; imag = " + nomx + "[5]; \n";
451vardec += "mod = " + nomx + "[6]; phas = " + nomx + "[7]; \n";
452return(vardec);
453}
454
455/* --Methode-- */
[2343]456DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
[2689]457r_8* NTupInt_TMatrix< complex<r_4> >::GetLineD(sa_size_t n) const
[585]458{
459int i,j;
460if ((n < 0) || (n >= (int)(mMtx->NRows()*mMtx->NCols()) )) {
461 mRet[0] = n;
462 for(i=1; i<8; i++) mRet[i] = 0.;
463}
464else {
465 i = n/mMtx->NCols(); j = n%mMtx->NCols();
466 mRet[0] = n; mRet[1] = i; mRet[2] = j;
467 mRet[4] = (*mMtx)(i,j).real(); mRet[5] = (*mMtx)(i,j).imag();
468 mRet[3] = mRet[6] = sqrt(mRet[4]*mRet[4]+mRet[5]*mRet[5]);
469 mRet[7] = atan2(mRet[5], mRet[4]);
470}
471return(mRet);
472}
473
[2343]474DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
[2689]475r_8* NTupInt_TMatrix< complex<r_8> >::GetLineD(sa_size_t n) const
[585]476{
477int i,j;
478if ((n < 0) || (n >= (int)(mMtx->NRows()*mMtx->NCols()) )) {
479 mRet[0] = n;
480 for(i=1; i<8; i++) mRet[i] = 0.;
481}
482else {
483 i = n/mMtx->NCols(); j = n%mMtx->NCols();
484 mRet[0] = n; mRet[1] = i; mRet[2] = j;
485 mRet[4] = (*mMtx)(i,j).real(); mRet[5] = (*mMtx)(i,j).imag();
486 mRet[3] = mRet[6] = sqrt(mRet[4]*mRet[4]+mRet[5]*mRet[5]);
487 mRet[7] = atan2(mRet[5], mRet[4]);
488}
489return(mRet);
490}
491
492
493#ifdef __CXX_PRAGMA_TEMPLATES__
[2930]494#pragma define_template NOMAdapter_TMatrix<uint_2>
495#pragma define_template NOMAdapter_TMatrix<int_2>
[585]496#pragma define_template NOMAdapter_TMatrix<int_4>
[2930]497#pragma define_template NOMAdapter_TMatrix<int_8>
[585]498#pragma define_template NOMAdapter_TMatrix<r_4>
499#pragma define_template NOMAdapter_TMatrix<r_8>
[1315]500#pragma define_template NOMAdapter_TMatrix< complex<r_4> >
501#pragma define_template NOMAdapter_TMatrix< complex<r_8> >
[2930]502#pragma define_template NTupInt_TMatrix<uint_2>
503#pragma define_template NTupInt_TMatrix<int_2>
[585]504#pragma define_template NTupInt_TMatrix<int_4>
[2930]505#pragma define_template NTupInt_TMatrix<int_8>
[585]506#pragma define_template NTupInt_TMatrix<r_4>
507#pragma define_template NTupInt_TMatrix<r_8>
[719]508#pragma define_template NTupInt_TMatrix< complex<r_4> >
509#pragma define_template NTupInt_TMatrix< complex<r_8> >
[585]510#endif
511
512#if defined(ANSI_TEMPLATES)
[2930]513template class NOMAdapter_TMatrix<uint_2>;
514template class NOMAdapter_TMatrix<int_2>;
[585]515template class NOMAdapter_TMatrix<int_4>;
[2930]516template class NOMAdapter_TMatrix<int_8>;
[585]517template class NOMAdapter_TMatrix<r_4>;
518template class NOMAdapter_TMatrix<r_8>;
[1315]519template class NOMAdapter_TMatrix< complex<r_4> >;
520template class NOMAdapter_TMatrix< complex<r_8> >;
[2930]521template class NTupInt_TMatrix<uint_2>;
522template class NTupInt_TMatrix<int_2>;
[585]523template class NTupInt_TMatrix<int_4>;
[2930]524template class NTupInt_TMatrix<int_8>;
[585]525template class NTupInt_TMatrix<r_4>;
526template class NTupInt_TMatrix<r_8>;
[719]527template class NTupInt_TMatrix< complex<r_4> >;
528template class NTupInt_TMatrix< complex<r_8> >;
[585]529#endif
Note: See TracBrowser for help on using the repository browser.