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

Last change on this file since 3039 was 3039, checked in by ansari, 19 years ago

Ajout texte de description pour $objname.--- et objaoper , Reza 19/7/2006

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