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

Last change on this file since 4077 was 3713, checked in by ansari, 16 years ago

Ajout options afichage cdreal,cdimag,cdphas,cdmod,cdmod2 pour tableaux complexes, Reza 11/12/2009

File size: 15.2 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 "cimage.h" // Pour la prise en charge des Image<T> (Avr 2007)
18#include "pimgadapter.h" // Adaptateur de tableaux PIImage pour Image<T>
19
20#include "fioarr.h"
21
22#include "nobjmgr.h"
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>
54string NOMAdapter_TMatrix<T>::GetDataObjType()
55{
56string type = "TMatrix< ";
57
58TVector<T>* v = dynamic_cast<TVector<T> *>(mMtx);
59if (v != NULL) type = "TVector< ";
60else {
61 Image<T>* img = dynamic_cast<Image<T> *>(mMtx);
62 if (img != NULL) type = "Image< ";
63}
64// type += DecodeTypeIdName(typeid(T).name());
65type += DataTypeInfo<T>::getTypeName();
66type += " > ";
67return(type);
68}
69
70/* --Methode-- */
71template <class T>
72AnyDataObj* NOMAdapter_TMatrix<T>::CloneDataObj(bool share)
73{
74if (mMtx == NULL) return(NULL);
75TVector<T>* v = dynamic_cast<TVector<T> *>(mMtx);
76if (v != NULL) return( new TVector<T>(*v, share) );
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) );
81}
82
83}
84
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 }
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 }
114 else if (opts[0] == "sum") {
115 MuTyV mtv(mMtx->Sum());
116 string s;
117 return mtv.Convert(s);
118 }
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 r_8 nrm2 = mtv.Convert(nrm2); // we force the conversion to double
127 mtv = nrm2;
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 }
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 }
155 else return "TMatrix.Att: rank size/nelts nrow/nrows ncol/ncols sum sumsq norm2 min max minmax info.varname";
156 }
157}
158
159/* --Methode-- */
160template <class T>
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
190 cout << "NOMAdapter_TMatrix<T>::PerformOperation(): Error operation/arguments !" << endl;
191 cout << "...Valid args: row/line r [rowname] , col/column c [rowname]" << endl;
192 return 1;
193}
194
195/* --Methode-- */
196template <class T>
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) {
202 FIO_TArray<T> fio(v);
203 fio.Write(pos, nom);
204}
205else {
206 Image<T>* img = dynamic_cast<Image<T> *>(mMtx);
207 if (img != NULL) {
208 FIO_Image<T> fio(img);
209 fio.Write(pos, nom);
210 }
211 else {
212 FIO_TArray<T> fio(mMtx);
213 fio.Write(pos, nom);
214 }
215}
216return;
217}
218
219/* --Methode-- */
220template <class T>
221void NOMAdapter_TMatrix<T>::Print(ostream& os, int lev)
222{
223if (lev < 3) mMtx->Show(os, false);
224else mMtx->Show(os, true);
225if (lev > 0) mMtx->Print(os, 10*lev);
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 {
235 dopt = "thinline " + dopt;
236 return( new PIYfXDrawer( new POTVectorAdapter<T>(v, false, StringToCmplxDispOption(dopt)), NULL, true) );
237 }
238}
239
240/* --Methode-- */
241template <class T>
242P2DArrayAdapter* NOMAdapter_TMatrix<T>::Get2DArray(string & dopt)
243{
244Image<T>* img = dynamic_cast<Image<T> *>(mMtx);
245if (img != NULL) return ( new ImageAdapter<T>(img, false) );
246 else return ( new POTMatrixAdapter<T>(mMtx, false, StringToCmplxDispOption(dopt)) );
247}
248
249
250// ---- Specialisation pour complexes -----
251DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
252P2DArrayAdapter* NOMAdapter_TMatrix< complex<r_4> >::Get2DArray(string & dopt)
253{
254 return ( new POTMatrixAdapter< complex<r_4> >(mMtx, false, StringToCmplxDispOption(dopt) ) );
255}
256DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
257P2DArrayAdapter* NOMAdapter_TMatrix< complex<r_8> >::Get2DArray(string & dopt)
258{
259 return ( new POTMatrixAdapter< complex<r_8> >(mMtx, false, StringToCmplxDispOption(dopt) ) );
260}
261// -------------------------------------------------------------
262
263/* --Methode-- */
264template <class T>
265NTupleInterface* NOMAdapter_TMatrix<T>::GetNTupleInterface(bool& adel)
266{
267adel = true;
268return( new NTupInt_TMatrix<T>(mMtx) );
269}
270
271
272/* --Methode-- */
273template <class T>
274GeneralFitData* NOMAdapter_TMatrix<T>::GetGeneralFitData(bool& adel
275 ,GeneralFitData::FitErrType errtype,double errscale,double errmin
276 ,int i1,int i2,int j1,int j2)
277{
278adel = false;
279if(!mMtx) return(NULL);
280
281int nx,ny;
282TVector<T>* vec = dynamic_cast<TVector<T> *>(mMtx);
283
284if(vec) { // C'est un TVector
285 nx = vec->NElts();
286 ny = 1;
287} else {
288 nx = mMtx->NRows();
289 ny = mMtx->NCol();
290 if(nx<=0 || ny<=0) return(NULL);
291}
292
293i1 = (i1<0||i1>=nx)? 0: i1;
294i2 = (i2<0||i2>=nx||i2<i1)? nx-1: i2;
295j1 = (j1<0||j1>=ny)? 0: j1;
296j2 = (j2<0||j2>=ny||j2<j1)? ny-1: j2;
297
298GeneralFitData* mGData = NULL;
299
300if(vec) { // C'est un TVector
301 mGData = new GeneralFitData(1,(i2-i1+1),0);
302 adel = true;
303 for(int i=i1;i<=i2;i++) {
304 double x = (double) i;
305 double f = (*vec)(i);
306 double e = 1.;
307 e = GeneralFitData::ComputeError(f,e,errtype,errscale,errmin);
308 mGData->AddData1(x,f,e);
309 }
310} else {
311 mGData = new GeneralFitData(2,(i2-i1+1)*(j2-j1+1),0);
312 adel = true;
313 for(int i=i1;i<=i2;i++) for(int j=j1;j<=j2;j++) {
314 double x = (double) i;
315 double y = (double) j;
316 double f = (*mMtx)(i,j);
317 double e = 1.;
318 e = GeneralFitData::ComputeError(f,e,errtype,errscale,errmin);
319 mGData->AddData2(x,y,f,e);
320 }
321}
322
323return mGData;
324}
325
326template <class T>
327AnyDataObj* NOMAdapter_TMatrix<T>::FitResidusObj(GeneralFit& mfit)
328{
329ArrayFitter<T> arrfit;
330TVector<T>* vec = dynamic_cast<TVector<T> *>(mMtx);
331if(vec) {
332 TVector<T>* v = new TVector<T>(arrfit.FitResidus(*vec, mfit),true);
333 return v;
334} else {
335 TMatrix<T>* m = new TMatrix<T>(arrfit.FitResidus(*mMtx,mfit),true);
336 return m;
337}
338}
339
340template <class T>
341AnyDataObj* NOMAdapter_TMatrix<T>::FitFunctionObj(GeneralFit& mfit)
342{
343ArrayFitter<T> arrfit;
344TVector<T>* vec = dynamic_cast<TVector<T> *>(mMtx);
345if(vec) {
346 TVector<T>* v = new TVector<T>(arrfit.FitFunction(*vec,mfit),true);
347 return v;
348} else {
349 TMatrix<T>* m = new TMatrix<T>(arrfit.FitFunction(*mMtx,mfit),true);
350 return m;
351}
352}
353
354// ---- Specialisation pour complexes -----
355DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
356GeneralFitData* NOMAdapter_TMatrix< complex<r_4> >::GetGeneralFitData(bool& adel
357 ,GeneralFitData::FitErrType errtype,double errscale,double errmin
358 ,int i1,int i2,int j1,int j2)
359{
360 return(NULL);
361}
362
363DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
364AnyDataObj* NOMAdapter_TMatrix< complex<r_4> >::FitResidusObj(GeneralFit& mfit)
365{
366 return(NULL);
367}
368
369DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
370AnyDataObj* NOMAdapter_TMatrix< complex<r_4> >::FitFunctionObj(GeneralFit& mfit)
371{
372 return(NULL);
373}
374
375DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
376GeneralFitData* NOMAdapter_TMatrix< complex<r_8> >::GetGeneralFitData(bool& adel
377 ,GeneralFitData::FitErrType errtype,double errscale,double errmin
378 ,int i1,int i2,int j1,int j2)
379{
380 return(NULL);
381}
382
383DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
384AnyDataObj* NOMAdapter_TMatrix< complex<r_8> >::FitResidusObj(GeneralFit& mfit)
385{
386 return(NULL);
387}
388
389DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
390AnyDataObj* NOMAdapter_TMatrix< complex<r_8> >::FitFunctionObj(GeneralFit& mfit)
391{
392 return(NULL);
393}
394
395// -------------------------------------------------------------
396
397/* --Methode-- */
398template <class T>
399NTupInt_TMatrix<T>::NTupInt_TMatrix(TMatrix<T>* m)
400{
401mMtx = m;
402}
403
404/* --Methode-- */
405template <class T>
406NTupInt_TMatrix<T>::~NTupInt_TMatrix()
407{
408}
409
410/* --Methode-- */
411template <class T>
412sa_size_t NTupInt_TMatrix<T>::NbLines() const
413{
414return( mMtx->NRows()*mMtx->NCols() );
415}
416
417/* --Methode-- */
418template <class T>
419sa_size_t NTupInt_TMatrix<T>::NbColumns() const
420{
421return(8);
422}
423
424/* --Methode-- */
425template <class T>
426r_8* NTupInt_TMatrix<T>::GetLineD(sa_size_t n) const
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[3] = (*mMtx)(i,j);
437 mRet[4] = mRet[2]; mRet[5] = 0.;
438 mRet[6] = mRet[2]; mRet[7] = 0.;
439 }
440return(mRet);
441}
442
443/* --Methode-- */
444template <class T>
445string NTupInt_TMatrix<T>::VarList_C(const char* nx) const
446{
447string nomx;
448if (nx) nomx = nx;
449else nomx = "_xh_";
450string vardec = "double n,r,c,val,real,imag,mod,phas; \n";
451vardec += "n = " + nomx + "[0]; r = " + nomx + "[1]; c = " + nomx + "[2]; \n";
452vardec += "val = " + nomx + "[3]; \n";
453vardec += "real = " + nomx + "[4]; imag = " + nomx + "[5]; \n";
454vardec += "mod = " + nomx + "[6]; phas = " + nomx + "[7]; \n";
455return(vardec);
456}
457
458/* --Methode-- */
459DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
460r_8* NTupInt_TMatrix< complex<r_4> >::GetLineD(sa_size_t n) const
461{
462int i,j;
463if ((n < 0) || (n >= (int)(mMtx->NRows()*mMtx->NCols()) )) {
464 mRet[0] = n;
465 for(i=1; i<8; i++) mRet[i] = 0.;
466}
467else {
468 i = n/mMtx->NCols(); j = n%mMtx->NCols();
469 mRet[0] = n; mRet[1] = i; mRet[2] = j;
470 mRet[4] = (*mMtx)(i,j).real(); mRet[5] = (*mMtx)(i,j).imag();
471 mRet[3] = mRet[6] = sqrt(mRet[4]*mRet[4]+mRet[5]*mRet[5]);
472 mRet[7] = atan2(mRet[5], mRet[4]);
473}
474return(mRet);
475}
476
477DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
478r_8* NTupInt_TMatrix< complex<r_8> >::GetLineD(sa_size_t n) const
479{
480int i,j;
481if ((n < 0) || (n >= (int)(mMtx->NRows()*mMtx->NCols()) )) {
482 mRet[0] = n;
483 for(i=1; i<8; i++) mRet[i] = 0.;
484}
485else {
486 i = n/mMtx->NCols(); j = n%mMtx->NCols();
487 mRet[0] = n; mRet[1] = i; mRet[2] = j;
488 mRet[4] = (*mMtx)(i,j).real(); mRet[5] = (*mMtx)(i,j).imag();
489 mRet[3] = mRet[6] = sqrt(mRet[4]*mRet[4]+mRet[5]*mRet[5]);
490 mRet[7] = atan2(mRet[5], mRet[4]);
491}
492return(mRet);
493}
494
495
496#ifdef __CXX_PRAGMA_TEMPLATES__
497#pragma define_template NOMAdapter_TMatrix<uint_1>
498#pragma define_template NOMAdapter_TMatrix<uint_2>
499#pragma define_template NOMAdapter_TMatrix<uint_4>
500#pragma define_template NOMAdapter_TMatrix<uint_8>
501#pragma define_template NOMAdapter_TMatrix<int_1>
502#pragma define_template NOMAdapter_TMatrix<int_2>
503#pragma define_template NOMAdapter_TMatrix<int_4>
504#pragma define_template NOMAdapter_TMatrix<int_8>
505#pragma define_template NOMAdapter_TMatrix<r_4>
506#pragma define_template NOMAdapter_TMatrix<r_8>
507#pragma define_template NOMAdapter_TMatrix< complex<r_4> >
508#pragma define_template NOMAdapter_TMatrix< complex<r_8> >
509#pragma define_template NTupInt_TMatrix<uint_1>
510#pragma define_template NTupInt_TMatrix<uint_2>
511#pragma define_template NTupInt_TMatrix<uint_4>
512#pragma define_template NTupInt_TMatrix<uint_8>
513#pragma define_template NTupInt_TMatrix<int_1>
514#pragma define_template NTupInt_TMatrix<int_2>
515#pragma define_template NTupInt_TMatrix<int_4>
516#pragma define_template NTupInt_TMatrix<int_8>
517#pragma define_template NTupInt_TMatrix<r_4>
518#pragma define_template NTupInt_TMatrix<r_8>
519#pragma define_template NTupInt_TMatrix< complex<r_4> >
520#pragma define_template NTupInt_TMatrix< complex<r_8> >
521#endif
522
523#if defined(ANSI_TEMPLATES)
524template class NOMAdapter_TMatrix<uint_1>;
525template class NOMAdapter_TMatrix<uint_2>;
526template class NOMAdapter_TMatrix<uint_4>;
527template class NOMAdapter_TMatrix<uint_8>;
528template class NOMAdapter_TMatrix<int_1>;
529template class NOMAdapter_TMatrix<int_2>;
530template class NOMAdapter_TMatrix<int_4>;
531template class NOMAdapter_TMatrix<int_8>;
532template class NOMAdapter_TMatrix<r_4>;
533template class NOMAdapter_TMatrix<r_8>;
534template class NOMAdapter_TMatrix< complex<r_4> >;
535template class NOMAdapter_TMatrix< complex<r_8> >;
536template class NTupInt_TMatrix<uint_1>;
537template class NTupInt_TMatrix<uint_2>;
538template class NTupInt_TMatrix<uint_4>;
539template class NTupInt_TMatrix<uint_8>;
540template class NTupInt_TMatrix<int_1>;
541template class NTupInt_TMatrix<int_2>;
542template class NTupInt_TMatrix<int_4>;
543template class NTupInt_TMatrix<int_8>;
544template class NTupInt_TMatrix<r_4>;
545template class NTupInt_TMatrix<r_8>;
546template class NTupInt_TMatrix< complex<r_4> >;
547template class NTupInt_TMatrix< complex<r_8> >;
548#endif
Note: See TracBrowser for help on using the repository browser.