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

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

1/ Ajout methode Services2NObjMgr::DisplayPoints3DW() et commande plot3dw
2/ Ajout et utilisation methode de verification de nom NamedObjMgr::CheckName()
3/ Changement methode NObjMgrAdapter::GetInfoString() et son utilisation
a travers la methdoe PIACmd::GetVarApp() pour acces de type $objname
4/ Ajout methode NObjMgrAdapter::PerformOperation() , implemente pour
adaptateur TMatrix<T> et TArray<T> pour extraction de ligne/colonne
(des matrices) et plan (slice) des TArray pour Rank()>3 Et mise en place
de la commande objaoper pour appel a PerformOperation()
5/ Petites corrections pour ntline2var vec2var

Reza 2 Juillet 2006

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