ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/REPOS_ERICCA/magic/lib/outil/src/ot_tenseur.cpp
Revision: 80
Committed: Thu Apr 10 15:19:20 2008 UTC (17 years, 1 month ago) by souaissa
Original Path: magic/lib/outil/outil/src/ot_tenseur.cpp
File size: 10735 byte(s)
Error occurred while calculating annotation data.
Log Message:

File Contents

# Content
1 //---------------------------------------------------------------------------
2 #include "gestionversion.h"
3
4 #pragma hdrstop
5
6 #include "ot_tenseur.h"
7
8 #include <iomanip>
9 #include <conio.h>
10 #include <stdio.h>
11 #include <assert.h>
12 //---------------------------------------------------------------------------
13
14 #pragma package(smart_init)
15
16
17
18
19 OT_TENSEUR::OT_TENSEUR()
20 {
21 Dim1=0;
22 Dim2=0;
23 data=0;
24 }
25
26 OT_TENSEUR::OT_TENSEUR(int n)
27 {
28 Dim1=n;
29 Dim2=n;
30 double2 zero(0.);
31 data =new double2[n*n];
32 for(int i=0;i<Dim1*Dim2;i++) data[i]=zero;
33 }
34
35 OT_TENSEUR::OT_TENSEUR(int n,int m)
36 {
37 Dim1=n;
38 Dim2=m;
39 double2 zero(0.);
40 data =new double2[n*m];
41 for(int i=0;i<Dim1*Dim2;i++) data[i]=zero;
42 }
43
44
45
46 OT_TENSEUR:: OT_TENSEUR(vector<OT_VECTEUR_4DD>& v)
47 {
48
49 Dim1=v.size();
50 Dim2=Dim1;
51 int Dim= Dim1*Dim2 ;
52 data=new double2[Dim];
53 for(int i=0;i<Dim1;i++)
54 {
55 OT_VECTEUR_4DD z1=v[i];
56 for (int j=0;j<Dim2;j++)
57 {
58 OT_VECTEUR_4DD z2=v[j];
59 double2 prscal=z1*z2;
60 double2 val= prscal.get_fabs();
61 if(val.get_x()<=val.get_dx()) {
62 prscal=0.;
63 prscal.set_dx(val.get_dx());
64 }
65 data[i*Dim2+j]=prscal;
66 }
67 }
68
69
70 }
71
72 OT_TENSEUR::OT_TENSEUR(vector<OT_VECTEUR_4DD>&v1,vector<OT_VECTEUR_4DD>&v2)
73 {
74
75 Dim1=v1.size();
76 Dim2=v2.size();
77 int Dim= Dim1*Dim2 ;
78 data=new double2[Dim];
79 for(int i=0;i<Dim1;i++)
80 {
81 OT_VECTEUR_4DD z1=v1[i];
82 for (int j=0;j<Dim2;j++)
83 {
84 OT_VECTEUR_4DD z2=v2[j];
85 double2 prscal=z1*z2;
86 double2 val= prscal.get_fabs();
87 if(val.get_x()<=val.get_dx()) {
88 prscal=0.;
89 prscal.set_dx(val.get_dx());
90 }
91 data[i*Dim2+j]=prscal;
92 }
93 }
94
95
96 }
97
98
99 OT_TENSEUR::~OT_TENSEUR()
100 {
101 Dim1=0;
102 Dim2=0;
103
104 if (data!=NULL) delete [] data;
105 }
106
107
108 OT_TENSEUR::OT_TENSEUR(const OT_TENSEUR& ot)
109 {
110 Dim1=ot.Dim1;
111 Dim2=ot.Dim2;
112 data=new double2[Dim1*Dim2];
113
114 for(int i=0;i<Dim1;i++)
115 {
116 for (int j=0;j<Dim2;j++)
117 {
118 data[i*Dim2+j]=ot(i,j);
119 }
120 }
121 }
122
123
124 OT_TENSEUR& OT_TENSEUR:: operator =(const OT_TENSEUR& ot)
125 {
126
127 if(this==&ot) return *this ;
128
129 if(data) delete [] data;
130
131 Dim1=ot.Dim1;
132 Dim2=ot.Dim2;
133 data=new double2[Dim1*Dim2];
134
135 for(int i=0;i<Dim1;i++) {
136 for (int j=0;j<Dim2;j++) {
137 data[i*Dim2+j]=ot.data[i*Dim2+j];
138 }
139 }
140
141
142 return *this;
143 }
144
145 int operator==( OT_TENSEUR& t1, OT_TENSEUR&t2)
146 {
147 if(t1.Get_NbRows()!=t2.Get_NbRows()||t1.Get_NbCols()!=t2.Get_NbCols()) return 0;
148 else {
149 for(int i=0;i<t1.Get_NbRows();i++)
150 {
151 for(int j=0;j<t1.Get_NbCols();j++)
152 {
153 double2 val1=t1(i,j);
154 double2 val2=t2(i,j);
155 if(val1==val2) continue;
156 else return 0;
157 }
158
159 }
160 }
161 return 1;
162 }
163
164
165
166 double2&OT_TENSEUR:: operator()(int i,int j)
167 {
168 assert(i<Dim1&&j<Dim2);
169 double2 prciseion=0.0;
170 if(data[i*Dim2+j].get_fabs()<=prciseion)
171 {
172 data[i*Dim2+j]=0.;
173
174 }
175 return data[i*Dim2+j];
176 }
177
178
179
180 double2& OT_TENSEUR:: operator()(int i,int j)const
181 {
182 assert(i<Dim1&&j<Dim2);
183 double2 prciseion=0.0;
184 if(data[i*Dim2+j].get_fabs()<=prciseion)
185 {
186 data[i*Dim2+j]=0.;
187
188 }
189 return data[i*Dim2+j];
190 }
191
192
193
194 int OT_TENSEUR:: Get_NbRows()const
195 {
196 return Dim1;
197 }
198
199
200
201 int OT_TENSEUR:: Get_NbCols()const
202 {
203 return Dim2;
204 }
205
206
207
208 int OT_TENSEUR:: est_til_equivalent( OT_TENSEUR& tns)
209 {
210 vector<int> check;
211
212 for(int i=0;i<tns.Get_NbRows();i++)
213 {
214
215 vector<double2> lig1;
216 for(int ix1=0;ix1<Dim2;ix1++) lig1.insert(lig1.end(),data[i*Dim2+ix1]);
217
218 for(int j=0;j<Dim2;j++)
219 {
220 int p=-1;
221 if(!existe(check,j,p))
222 {
223 vector<int>trouve;
224 vector<double2>valeur;
225 vector<double2> lig2;
226 for(int ix2=0;ix2<Dim2;ix2++) lig2.insert(lig2.end(),tns(j,ix2));
227 for(int r=0;r<Dim2;r++)
228 {
229 double2 val=lig1[r];
230 val=val.get_fabs();
231
232 int indx1=-1;
233
234 for(int s=0;s<Dim2;s++)
235 { int pos1;
236 double2 vall=lig2[s];
237 vall=vall.get_fabs();
238
239 if (val==vall&&!existe(trouve,s,indx1))
240 {
241 trouve.insert(trouve.end(),s);
242 valeur.insert(valeur.end(),val);
243 break;
244 }
245
246 }
247
248 }
249 int t_trouve=trouve.size();
250 if (t_trouve==Dim2)
251 {
252 check.insert(check.end(),j);
253 break;
254
255 }
256
257 }
258 }
259 continue;
260 }
261 int t_chek=check.size();
262 if (t_chek==Dim1)return 1;
263 else return 0;
264
265 }
266
267
268
269 template <class T> int OT_TENSEUR:: existe(const vector<T>& vct,T val,int& idx)
270 {
271
272 if(val<0) val=-val;
273 for(unsigned int i=0;i<vct.size();i++)
274 {
275 double x=vct[i];
276 if(x<0) val=-val;
277 if(x==val){
278 idx=i; return 1; }
279 }
280 idx=-1;
281 return 0;
282 }
283
284
285 OT_TENSEUR operator+( OT_TENSEUR& ot1, OT_TENSEUR& ot2)
286 {
287 assert (ot1.Get_NbRows()==ot2.Get_NbRows()&&ot1.Get_NbCols()==ot2.Get_NbCols());
288 OT_TENSEUR res(ot1.Get_NbRows(),ot1.Get_NbCols());
289 for(int i=0;i<ot1.Get_NbRows();i++)
290 {
291 for(int j=0;j<ot2.Get_NbCols();j++)
292 {
293 res(i,j)=ot1(i,j)+ot2(i,j);
294 }
295 }
296
297 return res;
298 }
299
300
301 OT_TENSEUR operator*( double2& coef, OT_TENSEUR& ot)
302 {
303 OT_TENSEUR T=ot;
304 for(int i=0;i<ot.Get_NbRows();i++)
305 for(int j=0;j<ot.Get_NbCols();j++)
306
307 T(i,j)=coef*T(i,j);
308
309 return T;
310
311 }
312
313 OT_TENSEUR operator*(OT_TENSEUR& ot , double2& coef)
314 {
315 return coef*ot;
316 }
317
318 OT_TENSEUR OT_TENSEUR:: transpose()
319 {
320 OT_TENSEUR TMP(Dim2,Dim1);
321 for(int i=0;i<Dim1;i++) {
322 for(int j=0;j<Dim2;j++)
323 {
324 TMP(j,i)=(*this)(i,j); // ot(i,j);
325 }
326 }
327 return TMP;
328 }
329
330 OT_TENSEUR operator*( OT_TENSEUR& ot1, OT_TENSEUR& ot2)
331 {
332 int dim1=ot1.Get_NbRows();
333 int dim3= ot1.Get_NbCols();
334 int dim2=ot2.Get_NbCols();
335 OT_TENSEUR TMP (dim1,dim2);
336
337 for(int i=0;i<dim1;i++)
338 {
339 for(int j=0;j<dim2;j++)
340 {
341 for(int k=0;k<dim3;k++)
342 TMP(i,j)=TMP(i,j)+ot1(i,k)*ot2(k,j);
343 }
344 }
345 return TMP;
346 }
347
348 std::ostream& operator<<(std::ostream& os, OT_TENSEUR& tns){
349 os<<"------------------------------------------------------------------------------------"<<endl;
350 os<<"# TENSEUR: DIM1= "<< tns.Get_NbRows()<<" , DIM2: "<<tns.Get_NbCols()<<" #"<<endl;
351 os<<"------------------------------------------------------------------------------------"<<endl;
352 for(int i=0;i<tns.Get_NbRows();i++) {
353 for(int j=0;j<tns.Get_NbCols();j++) {
354 os<<setw(18)<<tns(i,j);
355 }
356 os<<endl;
357 }
358 os<<"------------------------------------------------------------------------------------"<<endl;
359 return os;
360 }
361
362
363
364 void OT_TENSEUR::get_orthogonalisation(OT_TENSEUR& a,OT_VECTEUR_4DD& d,OT_TENSEUR& v,int n,int&nrot)
365 {
366
367 static const int ITMAX=50;
368 double2 zro=0.0;
369 int n1=n-1,n2=1/(n*n),ip1,iq1;// ,ip,j,i,iq// n=3,
370 double2 tresh,theta,tau,t,sm,s,h,g,c;
371 double2 *b=new double2[n]; // OT_VECTEUR_3D b,z;
372 double2 *z=new double2[n];
373
374 for(int ip=0;ip<n;ip++)
375 {
376 for(int iq=0;iq<n;iq++)
377 //v[ip*n+iq]=0.0;
378 v(ip,iq)=0.0;
379 v(ip,ip)=1.0;
380 b[(unsigned int)ip]=d[(unsigned int)ip]=a(ip,ip);
381 z[(unsigned int)ip]=0.0;
382 }
383 nrot=0;
384 for(int i=1;i<=ITMAX;i++)
385 {
386 sm=0.0;
387 for(int ip=0;ip<n1;ip++)
388 for(int iq=ip+1;iq<n;iq++)
389 sm =sm+(a(ip,iq)).get_fabs();
390 if(sm==zro)
391 return;
392 if(i<4)
393 tresh=0.2*sm*n2;
394 else
395 tresh=0.0;
396 for(int ip=0;ip<n1;ip++)
397 for(int iq=ip+1;iq<n;iq++)
398 {
399 g=100*(a(ip,iq)).get_fabs();
400 if(i>4&&((d[(unsigned int)ip]+g).get_fabs()==d[(unsigned int)ip].get_fabs())&&((d[(unsigned int)iq])+g).get_fabs()==d[(unsigned int)iq].get_fabs())
401 a(ip,iq)=0.0;
402 else if((a(ip,iq)).get_fabs()>tresh)
403 {
404 h=d[(unsigned int)iq]-d[(unsigned int)ip];
405 if(((h).get_fabs()+g)==(h).get_fabs())
406 t=a(ip,iq)/h;
407 else
408 {
409 theta=0.5*h/a(ip,iq);
410 t=1.0/((theta).get_fabs()+sqrt(1.0+theta*theta));
411 if(theta<zro)t=zro-t;
412 }
413 c=1.0/sqrt(1+t*t);
414 s=t*c;
415 tau=s/(1.0+c);
416 h=t*a(ip,iq);
417 z[(unsigned int)ip]=z[(unsigned int)ip]-h;
418 z[(unsigned int)iq]=z[(unsigned int)iq]+h;
419 d[(unsigned int)ip]=d[(unsigned int)ip]-h;
420 d[(unsigned int)iq]=d[(unsigned int)iq]+h;
421 a(ip,iq)=0.0;
422 ip1=ip-1;
423 iq1=iq-1;
424 for(int j=0;j<=ip1;j++)
425 {
426 g=a(j,ip);
427 h=a(j,iq);
428 a(j,ip)=g-s*(h+g*tau);
429 a(j,iq)=h+s*(g-h*tau);
430 }
431 for(int j=ip+1;j<=iq1;j++)
432 {
433 g=a(ip,j);
434 h=a(j,iq);
435 a(ip,j)=g-s*(h+g*tau);
436 a(j,iq)=h+s*(g-h*tau);
437 }
438 for(int j=iq+1;j<n;j++)
439 {
440 g=a(ip,j);
441 h=a(iq,j);
442 a(ip,j)=g-s*(h+g*tau);
443 a(iq,j)=h+s*(g-h*tau);
444 }
445 for(int j=0;j<n;j++)
446 {
447 g=v(j,ip);
448 h=v(j,iq);
449 v(j,ip)=g-s*(h+g*tau);
450 v(j,iq)=h+s*(g-h*tau);
451 }
452 ++nrot;
453 }
454 }
455 for(int ip=0;ip<n;ip++)
456 {
457 b[(unsigned int)ip]=b[(unsigned int)ip]+z[(unsigned int)ip];
458 d[(unsigned int)ip]=b[(unsigned int)ip];
459 z[(unsigned int)ip]=0.0;
460 }
461 }
462
463
464 delete [] b;
465 delete [] z;
466
467 }
468
469
470
471
472 int OT_TENSEUR::listes_equivalentes(std::vector<double2>& list1,std::vector<double2>& list2,std::vector<unsigned int>& indx)
473 {
474
475
476 int compt=0;
477 int pos=-1;
478 int exist=0;
479
480 for(unsigned int i=0;i<list1.size();i++)
481 {
482 double2 val=list1[i];
483
484 if(pos==-1)
485 {
486 for(unsigned int k=0;k< list2.size();k++)
487 {
488 if(list2[k]==val){
489 indx.insert(indx.end(),k);
490 pos=k;
491 compt++;
492 break;
493 }
494 }
495 }
496
497 else
498 {
499 for(unsigned int r=0;r<list2.size();r++)
500 {
501 for(unsigned int j=0;j<indx.size();j++)
502 {
503 exist=0;
504 if (r==indx[j])
505 {
506 exist=1;
507 break;
508 }
509 }
510 if(list2[r]==val&&!exist)
511 {
512 indx.insert(indx.end(),r);
513 pos=r;
514 compt++;
515 break;
516 }
517 }
518 }
519
520 }
521
522 if (compt==list1.size())return 1;
523 else return 0;
524
525 }
526
527
528
529
530
531