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