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