Borderbasix

resolve2.hpp
Go to the documentation of this file.
1 //#include<g2c.h>
2 //struct doublecomplex
3 //{
4 // double r;
5 // double i;
6 //};
7 //typedef double doublereal;
8 //typedef long int logical;
9 //#define MYTYPE long double
10 
12 #include "linalg/clapack.h"
13 #include<complex>
14 #include"linpkg/Lapack.H"
15 #include"linpkg/Eigen.H"
16 #include<stdlib.h>
17 #include<cstdlib>
18 //extern "C" {
19  //bon je sais la decalration ci apres est un peu crade mais bon
20  //entre f2c et g2c tout n'est pas tjrs compatible alors comme ca
21  //meme avec le -Wall on cloue le bec a gcc.. :-0
22 // void ztrsen_(...);
23 int zgebal_(...);
24 // void zgees_(...);
25 // void dgees_(...);
26 void dgees_(char *JOBVS,char *SORT,bool (*SELECT)(double,double),int *N,double *A,int *LDA,int *SDIM,double *WR,double *WI,double *VS, int *LDVS,double *WORK,int *LWORK,double *BWORK,int *info);
27 
28 template<typename T>
29 int zgees_(const char *jobvs,const char *sort,
30  logical (*select)(doublecomplex<T>*),tla_int *n,
31  doublecomplex<T> *a, tla_int *lda, tla_int *sdim, doublecomplex<T> *w,
32  doublecomplex<T> *vs, tla_int *ldvs, doublecomplex<T> *work,
33  tla_int *lwork, T *rwork, logical *bwork, tla_int *info);
34 template<typename T>
35 int ztrsen_(const char *job, const char *compq, logical *select, tla_int
36  *n, doublecomplex<T> *t, tla_int *ldt, doublecomplex<T> *q,
37  tla_int *ldq, doublecomplex<T> *w, tla_int *m, T *s, T *sep,
38  doublecomplex<T> *work, tla_int *lwork, tla_int *info);
39 //}
40 
41 //wrapper au dessus de lapack pour calculer
42 //une decomposition d schur
43 //de la matrice qui doit etre un lapack<double>
44 template<typename Mat>
45 Mat wraplapack_dgees(const Mat &M)
46 {
47  typedef typename Mat::rep_t rept;
48  rept m=M.rep,res;
49  bool (*SELECT)(double,double)=(bool (*)(double,double))1;
50  double *bwork=(double*)malloc(m.nbcol()*sizeof(void (*)(void)));
51  double *wr=(double*)malloc(m.nbrow()*sizeof(double));
52  double *wi=(double*)malloc(m.nbrow()*sizeof(double));
53  Mat Mres(m.nbrow(),m.nbrow());
54  double *work=(double*)malloc(10*sizeof(double)*m.nbrow());
55  int info,n,i,l,lres,lwork,sdim;
56  double* tab =(double*)malloc(m.nbcol()*m.nbrow()*sizeof(double));
57  double* tabres =(double*)malloc(m.nbcol_*m.nbrow_*sizeof(double));
58  n=m.nbrow();l=m.nbrow();lres=m.nbrow();lwork=10*m.nbrow();
59  for(i=0;i<m.nbcol_*m.nbrow_;i++) tab[i]=m.tab_[i];
60  dgees_("V","N",SELECT,&n,tab,&l,&sdim,wr,wi,tabres,&lres,work,
61  &lwork,bwork,&info);
62  (Mres.rep).reserve(m.nbcol_,m.nbcol_);
63  for(i=0;i<m.nbcol_*m.nbrow_;i++) (Mres.rep).tab_[i]=tabres[i];
64  free(wr);
65  free(wi);
66  free(work);
67  free(tab);
68  free(tabres);
69  return Mres;
70 }
71 //fonction qui sert a rien.. :-)
72 
73 //extern "C"{
74 template<typename T>
76 {
77  static long int rep=1;
78  return rep;
79 }
80 //}
81 
82 //la meme que wraplapack_dgees
83 //sauf qu'elle marche sur le matrice du type
84 //lapack<complex<double> >
85 //Si PbS sous linux verifier la fonction dznrm2!!!
86 //Elle est pas compilee de maniere adequate dans la distrib semir
87 template<typename Mat>
88 Mat wraplapack_zgees(Mat M)
89 {
90  typedef typename Mat::rep_t rept;
91  typedef typename Mat::coeff_t::value_type T;
92  rept m=M.rep;
95  malloc(m.nbrow_*sizeof(doublecomplex<T>));
96  T *rwork=(T*)malloc(m.nbrow_*sizeof(T));
97  logical *bwork=(logical*)malloc(m.nbrow_*sizeof(logical));
98  Mat Mres(m.nbrow_,m.nbrow_);
99  doublecomplex<T>* work=
100  (doublecomplex<T>*)malloc(10*sizeof(doublecomplex<T>)*m.nbrow_);
101  tla_int info,n,l,lres,lwork,sdim;
102  unsigned int i;
103  doublecomplex<T>* tab =
104  (doublecomplex<T>*)calloc(m.nbcol_*m.nbrow_,sizeof(doublecomplex<T>));
105  doublecomplex<T>* tabres =
106  (doublecomplex<T>*)calloc(m.nbcol_*m.nbrow_,sizeof(doublecomplex<T>));
107  const char *JOBS="V",*SORT="N";
108 
109  n=m.nbrow_;l=m.nbrow_;lres=m.nbrow_;lwork=5*m.nbrow_;
110 
111  for(i=0;i<m.nbcol_*m.nbrow_;i++) {
112  tab[i].r=m.tab_[i].real();
113  tab[i].i=m.tab_[i].imag();
114  //toto=tab[i].r;
115  }
116  zgees_(JOBS,SORT,SELECT,&n,tab,&l,&sdim,
117  w,tabres,&lres,work,&lwork,rwork,bwork,&info);
118  (Mres.rep).reserve(m.nbcol_,m.nbcol_);//matrice carree exclusivement!!
119  for(i=0;i<m.nbcol_*m.nbrow_;i++)
120  {
121  (Mres.rep).tab_[i]=complex<T>(tab[i].r,tab[i].i);
122  //toto=*(((T*)tabres)+i+2);
123  }
124  //cout<<"info "<<info<<endl;
125  //cout<<Mres<<endl<<endl;
126  for(i=0;i<m.nbcol_*m.nbrow_;i++)
127  {
128  (Mres.rep).tab_[i]=complex<T>(tabres[i].r,tabres[i].i);
129  }
130  free(w);
131  free(rwork);
132  free(work);
133  free(bwork);
134  free(tab);
135  free(tabres);
136  return Mres;
137 }
138 
139 #if 0
140 template<>
141 MatDense<lapack<complex<mpf_class> > >
142  wraplapack_zgees(MatDense<lapack<complex<mpf_class> > >M)
143 {
144  typedef MatDense<lapack<complex<mpf_class> > > Mat;
145  typedef lapack<complex<mpf_class> > rept;
146  typedef mpf_class T;
147  rept m=M.rep;
148  logical (*SELECT)(doublecomplex<T> *)=select_true;
150  malloc(m.nbrow_*sizeof(doublecomplex<T>));
151  T *rwork=(T*)malloc(m.nbrow_*sizeof(T));
152  logical *bwork=(logical*)malloc(m.nbrow_*sizeof(logical));
153  Mat Mres(m.nbrow_,m.nbrow_);
154  doublecomplex<T>* work=
155  (doublecomplex<T>*)new doublecomplex<T>[10*m.nbrow_];
156  tla_int info,n,l,lres,lwork,sdim;
157  unsigned int i;
158  doublecomplex<T>* tab =
159  (doublecomplex<T>*) new doublecomplex<T>[m.nbcol_*m.nbrow_];
160  doublecomplex<T>* tabres = new doublecomplex<T>[m.nbcol_*m.nbrow_];
161  // (doublecomplex<T>*)calloc(m.nbcol_*m.nbrow_,sizeof(doublecomplex<T>));
162  char *JOBS="V",*SORT="N";
163 
164  n=m.nbrow_;l=m.nbrow_;lres=m.nbrow_;lwork=5*m.nbrow_;
165 
166  for(i=0;i<m.nbcol_*m.nbrow_;i++) {
167  tab[i].r=m.tab_[i].real();
168  tab[i].i=m.tab_[i].imag();
169  //toto=tab[i].r;
170  }
171 
172  zgees_(JOBS,SORT,SELECT,&n,tab,&l,&sdim,
173  w,tabres,&lres,work,&lwork,rwork,bwork,&info);
174  (Mres.rep).reserve(m.nbcol_,m.nbcol_);//matrice carree exclusivement!!
175  for(i=0;i<m.nbcol_*m.nbrow_;i++)
176  {
177  (Mres.rep).tab_[i]=complex<T>(tab[i].r,tab[i].i);
178  //toto=*(((T*)tabres)+i+2);
179  }
180  //cout<<"info "<<info<<endl;
181  //cout<<Mres<<endl<<endl;
182  for(i=0;i<m.nbcol_*m.nbrow_;i++)
183  {
184  (Mres.rep).tab_[i]=complex<T>(tabres[i].r,tabres[i].i);
185  }
186  free(w);
187  free(rwork);
188  delete[] work;
189  free(bwork);
190  delete[] tab;
191  delete[] tabres;
192  return Mres;
193 }
194 #endif
195 
196 //debut de l'implementation de l'algo de Corless dans
197 //ISSAC 97
198 
199 //trigo simultanee sur des doubles
200 template<typename Mat>
201 void trigsim(list<Mat> &lmat)
202 {
203  //on supose que la list n'est pas vide!!!
204  Mat Shurvect=wraplapack_dgees(lmat.front());
205  Mat invShurvect;
206  Shurvect.transpose();//on est reel jusque la
207  invShurvect=Shurvect;
208  Shurvect.transpose();
209  typename list<Mat>::iterator iter;
210  for(iter=lmat.begin();iter!=lmat.end();iter++)
211  {
212  Mat tmp=invShurvect*(*iter)*Shurvect;
213  *iter=tmp;
214  }
215 }
216 //un wrap de lapack rien d'important
217 template<typename Mat,typename coeff>
218 void wraplapack_ztrsen(Mat &M,logical * select,coeff *res)
219 {
220  //typedef typename Mat::coeff_t::value_type coeff;
221  const char *JOBS="E",*COMPQ="N";
222  tla_int N=M.nbcol(),LDT=M.nbcol();
224  malloc(M.nbcol()*M.nbcol()*sizeof(doublecomplex<coeff>));
225  doublecomplex<coeff> *Q=NULL;
227  malloc(M.nbcol()*sizeof(doublecomplex<coeff>));
228  tla_int m=1,LDQ=1;
229  coeff S=1,SEP=2;
231  malloc(2*N*N*sizeof(doublecomplex<coeff>));
232  tla_int LWORK=2*N*N,info;
233  //maintenant on initialise le tableau T
234  for(int i=0;i<N*N;i++)
235  {
236  T[i].r=(M.rep).tab_[i].real();
237  T[i].i=(M.rep).tab_[i].imag();
238  }
239  ztrsen_(JOBS,COMPQ,select,&N,T,&LDT,Q,&LDQ,W,&m,&S,&SEP,WORK,&LWORK,&info);
240  *res=S;
241  free(WORK);
242  free(W);
243  free(T);
244 }
245 //evalue le conditionnement non clusterise de
246 //chacunes des valeurs propres de la matrice
247 
248 template<typename T,typename Mat>
249 T cond_number(Mat &Combilin,int i)
250 {
251  //typedef typename Mat::coeff_t::value_type T;
252  T res;
253  logical *select;
254  select=(logical*)calloc(Combilin.nbcol(),sizeof(logical));
255  select[i]=1;
256  wraplapack_ztrsen(Combilin,select,&res);
257  free(select);
258  return res;
259 }
260 //
261 //creation des clusters
262 //utilisation de l'heuritique de Stetter!!
263 //
264 
265 template<typename Mat,typename typcond>
266 int getcluster(Mat &M,typcond *cond)
267 {
268  typedef typename Mat::coeff_t::value_type T;
269  // typedef MYTYPE T;
270  T EPSILON=1e-16;
272  malloc(M.nbcol()*sizeof(doublecomplex<T>));
273  int *digits=(int*)malloc(M.nbcol()*sizeof(int));
274  T normM=0;
275  char tampon1[60],tampon2[60],*rien;
276  int comp=0;
277  for(unsigned i=0;i<M.nbcol();i++)
278  normM=max(sqrt((M(i,i)*conj(M(i,i))).real()),normM);
279  if(normM<EPSILON)
280  {
281  for(unsigned i=0;i<M.nbcol();i++)
282  cond[i]=0;
283  //cout<<"ca foire ici"<<endl;
284  return M.nbcol();
285  }
286  //cout<<"M.nbcol()"<<M.nbcol()<<endl;
287  for(unsigned i=0;i<M.nbcol();i++)
288  {
289  //cout<<"cond[i]"<<cond[i]<<endl;
290  //cout<<"normM"<<normM<<endl;
291  //cout<<EPSILON*normM<<endl;
292  //cout<<1.0/cond[i]<<endl;
293  T lambda(1.0/cond[i]*EPSILON*normM);
294  //cout<<"lada"<<lambda<<endl;
295  T nbdigits=(1.0/lambda);
296  int k;
297  for(k=1;nbdigits>1;k++,nbdigits/=10);//
298  //cout<<"nbdigits "<<nbdigits<<" K "<<k<<endl;;
299  nbdigits=k;
300  digits[i]=k;
301  //rajout des tests de taille apres
302  sprintf(tampon1,"%%.%dLg\0",(k>16)?16:k);
303  //et pas snprintf parce que ca compile pas sur alpha.
304  // cout<<tampon1<<endl;
305  sprintf(tampon2,tampon1,M(i,i).real());
306  //et pas snprintf parce que ca compile pas sur alpha.
307  //cout<<tampon2<<endl;
308  truncnum[i].r=(T)(strtod(tampon2,&rien));
309  sprintf(tampon2,tampon1,M(i,i).imag());
310  //et pas snprintf parce que ca compile pas sur alpha.
311  //cout<<tampon2<<endl;
312  truncnum[i].i=(T)strtod(tampon2,&rien);
313  }
314  for(int i=0;i<M.nbcol();i++)
315  {
316  //if(cond[i]>=0)
317  //{
318  // cout<<"toto"<<endl;
319  cond[i]=-i;
320  cout<<"i"<<i<<"condi"<<cond[i]<<endl;
321  for(int j=0;j<M.nbcol();j++)
322  {
323  double val=1.0;
324  cout<<"digitis_i "<<digits[i]<<" "<<digits[j]<<endl;
325  for(int k=0;k<min(digits[i],digits[j]);k++,val*=10);
326  cout<<"val "<<val<<endl;
327  cout<<"la diference "<<abs(truncnum[j].r-truncnum[i].r)<<endl;
328  cout<<" en complex "<<abs(truncnum[j].i-truncnum[i].i)<<endl;
329  cout<<"prod "<<abs(truncnum[j].r-truncnum[i].r)*val<<endl;
330  if ((abs(truncnum[j].r-truncnum[i].r)*(T)val<(T)1.0)
331  &&(abs(truncnum[j].i-truncnum[i].i)*(T)val<(T)1.0))
332  {
333  typcond stock=cond[j];
334  cout<<"stock"<<stock<<endl;
335  for(int k=0;k<M.nbcol();k++)
336  {
337  cout<<"condk"<< cond[k]<<endl;
338  if(cond[k]==stock) cond[k]=-i;
339  }
340  //on merge les cluster
341  //truncnum[i].r=-1;
342  //cout<<"clustering ok plus d'un"<<endl;
343  }
344  }
345  //}
346  }
347  //cout<<"cond"<<endl;
348  for(int k=0;k<M.nbcol();k++)
349  cout<<cond[k]<<endl;
350  free(truncnum);
351  free(digits);
352  return M.nbcol();//cluster numerotes de -1 a ...
353 }
354 
355 //
356 //appliquation des clusters crees avec la fonction d'avant
357 //
358 
359 template<typename Mat,typename typcond>
360 int docluster(Mat &M,Mat &Shurvect,typcond * cond,int i)
361 {
362  //0n recherche tous les -i dans le tableau cond et apres on
363  //fait un cluster avec eux......
364  typedef typename Mat::coeff_t::value_type T;
365  //typedef MYTYPE T;
366  logical *select=(logical*)malloc(M.nbcol()*sizeof(logical));
367  tla_int N=M.nbcol(),LDT=M.nbcol(),LDQ=M.nbcol();
368  tla_int m,info,LWORK=N*N*2;
369  long flag=0;
370  T *stockcond=(T*)malloc(N*sizeof(T));
372  malloc(sizeof(doublecomplex<T>)*LWORK);
373  T S,SEP;
375  malloc(N*sizeof(doublecomplex<T>));
378  malloc(N*N*sizeof(doublecomplex<T>));
380  malloc(N*N*sizeof(doublecomplex<T>));
381  for(int k=0;k<N*N;k++)
382  {
383  tmpM[k].r=(M.rep).tab_[k].real();
384  tmpM[k].i=(M.rep).tab_[k].imag();
385  }
386  for(int k=0;k<N*N;k++)
387  {
388  tmpQ[k].r=(Shurvect.rep).tab_[k].real();
389  tmpQ[k].i=(Shurvect.rep).tab_[k].imag();
390  }
391  //cout<<"1"<<endl;
392  for(int k=0;k<M.nbcol();k++)
393  {
394  stockW[k].r=M(k,k).real();
395  stockW[k].i=M(k,k).imag();
396  }
397  //cout<<"icisel "<<i<<endl;
398  for(int k=0;k<M.nbcol();k++)
399  {
400  // cout<<"i"<<i<<endl;
401  // cout<<"M"<<M<<endl;
402  // cout<<"select "<<k<<" "<<select[k]<<endl;
403  // cout<<"cond "<<k<<" "<<cond[k]<<endl;
404  select[k]=((cond[k]==-i)?1:0);
405  flag+=select[k];
406  //cout<<"select "<<k<<" "<<select[k]<<endl;
407  }
408  //on fait la selection
409  //cout<<"flag"<<flag<<endl;
410 // cout<<"2"<<endl;
411  if (flag>1)
412  {
413  // cout<<"on fait quelquechose reorde"<<M<<endl;
414  const char *JOBS="B",*COMPQ="V";
415  ztrsen_(JOBS,COMPQ,select,&N,tmpM,&LDT,tmpQ,
416  &LDQ,W,&m,&S,&SEP,WORK,&LWORK,&info);
417  for(int k=0;k<N*N;k++)
418  (M.rep).tab_[k]=complex<T>(tmpM[k].r,tmpM[k].i);
419  for(int k=0;k<N*N;k++)
420  (Shurvect.rep).tab_[k]=complex<T>(tmpQ[k].r,tmpQ[k].i);
421  //cout<<"apres reord"<<M<<endl;
422  //donc maintenant on a reordonne les vp du cluster i
423  //il faut maintenant reconnaitre qui est qui pour
424  //remettre a jour cond
425  char * posbusy=(char*)malloc(N);
426  for(int i=0;i<N;i++)
427  posbusy[i]=0;
428  for(int k=0;k<N;k++)
429  {
430  //on prends pour valeur celle qui minimise la difference
431  int stockpos=0;
432  for(;posbusy[stockpos];stockpos++);
433  T mindiff=abs(
434  (W[stockpos].r-stockW[k].r)*(W[stockpos].r-stockW[k].r)
435  +(W[stockpos].i-stockW[k].i)*(W[stockpos].i-stockW[k].i)
436  );
437  for(int l=0;l<N;l++)
438  if((!posbusy[l])&&(abs((W[l].r-stockW[k].r)*(W[l].r-stockW[k].r)+
439  (W[l].i-stockW[k].i)*(W[l].i-stockW[k].i)<mindiff))
440  )
441  {
442  mindiff=abs((W[l].r-stockW[k].r)*(W[l].r-stockW[k].r)
443  +(W[l].i-stockW[k].i)*(W[l].i-stockW[k].i)
444  );
445  stockpos=l;
446  }
447  posbusy[stockpos]=1;
448  stockcond[stockpos]=(T)cond[k];
449  }
450  free(posbusy);
451  for(int k=0;k<N;k++)
452  cond[k]=stockcond[k];
453  }
454  // cout<<"cool"<<endl;
455  free(select);
456  free(stockcond);
457  free(WORK);
458  free(stockW);
459  free(W);
460  free(tmpM);
461  free(tmpQ);
462  return flag;
463 }
464 
465 //
466 //fonction de reordenement en enbtier
467 //
468 
469 template<typename T,typename Mat>
470 void reorder(Mat &Combilin,Mat &Shurvect,Mat &invShurvect,
471  list<int> &size_clust)
472 {
473  T *cond;
474  int i,nbcluster;
475  cond=new T[Combilin.nbcol()];
476  //(doublereal*)malloc(sizeof(doublereal)*Combilin.nbcol());
477  for(i=0;i<(int)Combilin.nbcol();i++)
478  {
479  cond[i]=cond_number<T>(Combilin,i);
480  //cout<<"coond "<<i<<" "<<cond[i]<<endl;
481  }
482 
483 // cout<<"cond"<< cond<<endl;
484  nbcluster=getcluster(Combilin,cond);
485  // cout<<"nbcluster"<< nbcluster<<endl;
486  // cout<<"combilin"<<Combilin<<endl;
487 
488  for(i=0;i<nbcluster;i++)
489  {
490 // cout<<"i"<<i<<endl;
491 // cout<<"nbcluster"<<nbcluster<<endl;
492  int tmp_size=docluster(Combilin,Shurvect,cond,i);
493  // cout<<"tmp_size"<<tmp_size<<endl;
494  if (tmp_size!=0)
495  {
496  if(tmp_size>1)
497  size_clust.push_back(tmp_size);
498  else
499  size_clust.push_front(tmp_size);
500  }
501  // cout<<"final"<<endl;
502  }
503 // cout<<"sortie"<<endl;
504 // cout<<"d1clu"<<endl;
505  //cout<<"nb cluster "<<size_clust.size()<<endl;
506  //Shurvect.transpose();
507  for(i=0;i<invShurvect.nbrow();i++)
508  for(int j=0;j<invShurvect.nbcol();j++)
509  invShurvect(i,j)=conj(Shurvect(j,i));
510  //Shurvect.transpose();
511  // free(cond);
512  delete[] cond;
513 }
514 
515 //
516 //trigonlisation simultanne d'une liste de matrices
517 //complexe de type lapack<complex<double>>
518 //
519 
520 template<typename Mat>
521 void compltrigsim(list<Mat> &lmat,list<int> &size_clust)
522 {
523  //on supose que la list n'est pas vide!!!
524  typedef typename Mat::coeff_t::value_type T;
525  double sum=0;
526  Mat Shurvect,Combilin,tmp;
527  Mat invShurvect;
528  //<ugly>
529  double *tab=(double*)malloc(lmat.size()*sizeof(double));
530  //<\ugly>
531  typename list<Mat>::iterator iterComb=lmat.begin();
532  for(unsigned i=0;i<lmat.size();i++)
533  tab[i]=rand()% 32003+1;
534  for(unsigned i=0;i<lmat.size();i++)
535  {
536  sum+=tab[i];
537  // cout<<"tab["<<i<<"] "<<tab[i]<<endl;
538  }
539  iterComb++;
540 // cout<<"completrigsim"<<endl;
541  Combilin=lmat.front();
542  for(unsigned i=0;iterComb!=lmat.end();iterComb++,i++)
543  {
544  Combilin+=T(tab[i])*(*iterComb);
545  }
546  Combilin/=T(sum);
547  //cout<<"Combilin"<<endl;
548  Shurvect=wraplapack_zgees(Combilin);
549 // cout<<"apres Combilin"<<endl;
550  Shurvect.transpose();
551  invShurvect=Shurvect;
552  Shurvect.transpose();
553  //tmp=invShurvect*Combilin*Shurvect;
554  //Combilin=tmp;
555  // cout<<"apres trigo "<<Combilin<<endl;
556  for(unsigned i=0;i<invShurvect.nbrow();i++)
557  for(unsigned j=0;j<invShurvect.nbcol();j++)
558  invShurvect(i,j)=conj(invShurvect(i,j));
559 // cout<<"reorder"<<endl;
560  tmp=invShurvect*Combilin*Shurvect;
561  Combilin=tmp;
562  //cout<<"apres trigo2 "<<Combilin<<endl;
563  reorder<T>(Combilin,Shurvect,invShurvect,size_clust);
564  // cout<<"apres trigo3 "<<endl;
565  typename list<Mat>::iterator iter;
566  for(iter=lmat.begin();iter!=lmat.end();iter++)
567  {
568  Mat tmp=invShurvect;
569  tmp*=(*iter);
570  tmp*=Shurvect;
571  *iter=tmp;
572  }
573  // cout<<"apres trigo4 "<<endl;
574 }
575 
576 //
577 //resolution une fois le boulot fait!!
578 //on a plus qu'a lire sur la diagonale
579 //
580 
581 template<typename Mat,typename Complex_Mat>
582 void solvefromcompltrigsim(const list<Mat> &lmat,const list<int> &size_clust,
583  Complex_Mat &res)
584 {
585  int increment,i;
586  typename list<Mat>::const_iterator iter,deb=lmat.begin();
587  typename list<int>::const_reverse_iterator
588  iter_size=size_clust.rbegin();
589 // for( iter=lmat.begin();iter!=lmat.end();iter++)
590 // cout<<*iter<<endl<<"--------------"<<endl;
591 // cout<<"deb->nbrow()"<<deb->nbrow()<<endl;
592 // for( typename list<int>::const_iterator i=size_clust.begin();i!=size_clust.end();i++)
593 // cout<<*i<<" toto "<<endl;
594  for(int i=0;i<deb->nbrow();)
595  {
596  int comp=0;
597  cout<<"Solution: ";
598  for(iter=lmat.begin();iter!=lmat.end();iter++)
599  {
600  // cout<<"deux fois"<<endl;
601  //cout<<"*iter_size"<<*iter_size<<endl;
602  typename list<Mat>::value_type::coeff_t value(0) ,tmp(*iter_size);
603  // cout<<"bug"<<endl;
604  //on calcule la valeur
605 
606  // Segmentation fault dans cette boucle pour des systemes de solutions 0
607  for(int j=0;j<*iter_size;j++) {
608 // cout<<"no puedes"<<endl;
609  value+=(*iter)(i+j,i+j);
610  // cout << "i " << i << " j " << j << " *iter.size() " << (*iter).nbcol() << endl;
611  }
612  //cout << "bug 2" << endl;
613  //cout<<"value"<<value<<endl;
614  value/=tmp;
615  for(int j=i;j<i+*iter_size;j++) {
616  res(j,comp,value);
617  cout <<" "<< res(j,comp,value);
618  }
619  comp++;
620  increment=*iter_size;
621  }
622  std::cout<<std::endl;
623  // cout<<"taille cluster "<<*iter_size<<endl;
624  //i+=increment;
625  i+=*iter_size++;
626  }
627 }
628 //
629 //la meme mais en reel
630 //c plus dur parce que il peut ya vavoir de blocs 2x2
631 //
632 template<typename Mat,typename Complex_Mat>
633 void solvefromtrigsim(const list<Mat> &lmat,Complex_Mat &res)
634 {
635  int increment,i;
636  typename list<Mat>::const_iterator iter,deb=lmat.begin();
637  for(i=0;i<deb->nbrow();)
638  {
639  if ((*deb)(i+1,i)+(*deb)(i,i)==(*deb)(i,i))//cas reel
640  {
641  int comp=0;
642  increment=1;
643  for(iter=lmat.begin();iter!=lmat.end();iter++)
644  {
645  res(i,comp)=(*iter)(i,i);
646  comp++;
647  }
648  }
649  else //cas complex
650  {
651  int comp=0;
652  increment=2;
653  Complex_Mat tmp(2,2),tmpShu(2,2),invtmpShu(2,2);
654  tmp(0,0)=(*deb)(i,i);
655  tmp(1,0)=(*deb)(i+1,i);
656  tmp(1,1)=(*deb)(i+1,i+1);
657  tmp(0,1)=(*deb)(i,i+1);
658  tmpShu=wraplapack_zgees(tmp);
659  tmpShu.transpose();
660  invtmpShu=tmpShu;
661  tmpShu.transpose();
662  invtmpShu(0,0)=conj(invtmpShu(0,0));
663  invtmpShu(1,0)=conj(invtmpShu(1,0));
664  invtmpShu(1,1)=conj(invtmpShu(1,1));
665  invtmpShu(0,1)=conj(invtmpShu(0,1));
666  for(iter=lmat.begin();iter!=lmat.end();iter++)
667  {
668  tmp(0,0)=(*iter)(i,i);
669  tmp(1,0)=(*iter)(i+1,i);
670  tmp(1,1)=(*iter)(i+1,i+1);
671  tmp(0,1)=(*iter)(i,i+1);
672  tmp=invtmpShu*tmp*tmpShu;
673  res(i,comp)=tmp(0,0);
674  res(i+1,comp)=tmp(1,1);
675  comp++;
676  }
677  }
678  i+=increment;
679  }
680 }
681 
682 template<typename Mat>
684 {
685  typedef typename Mat::rep_t rept;
686  typedef typename Mat::coeff_t::value_type T;
687  // typedef long double T;
688  float toto;
689  rept m=M.rep;
690  Mat Mres(m.nbrow(),m.nbrow());
691  int info,n,l,ilo,ihi;
692  unsigned int i;
693  doublecomplex<T>* tab =
694  (doublecomplex<T>*)calloc(m.nbcol_*m.nbrow_,sizeof(doublecomplex<T>));
695  T *scale =
696  (T*)calloc(m.nbcol_,sizeof(T));
697  char *JOBS="B";
698 
699  n=m.nbrow_;l=m.nbrow_;
700 
701  for(i=0;i<m.nbcol_*m.nbrow_;i++) {
702  tab[i].r=m.tab_[i].real();
703  tab[i].i=m.tab_[i].imag();
704 
705  //cout<<"tab["<<2*i<<"]";
706  //cout<<tab[i].r<<" "<<tab[i].i<<endl;
707  toto=tab[i].r;
708  //cout<<"tentative toto"<<toto<<endl;
709  }
710  //cout<<"avant lapack"<<endl;
711  //cout<<"int "<<sizeof(int)<<endl<<"int *"<<sizeof(int*)<<endl;
712  //cout<<"fun "<<sizeof(int (*)(void *))<<endl<<"DOUBLE "<<sizeof(double)<<endl;
713  zgebal_(JOBS,&n,tab,&l,&ilo,&ihi,scale,&info);
714  // cout<<"apres lapack"<<tab<<" info "<<info<<" ilo "<<ilo<<" ihi "<<ihi<<endl;
715  (Mres.rep).reserve(n,n);//matrice carree exclusivement!!
716  for(unsigned int i=0;i<m.nbcol_*m.nbrow_;i++)
717  {
718  (Mres.rep).tab_[i]=complex<double>(tab[i].r,tab[i].i);
719  //cout<<"tab["<<2*i<<"]";
720  //cout<<tab[i].r<<" "<<tab[i].i<<endl;
721  // cout<<"scale "<<scale[i/n]<<endl;
722  }
723  // cout<<"info "<<info<<endl;
724  //cout<<Mres<<endl<<endl;
725 
726 
727  free(tab);
728  free(scale);
729  return Mres;
730 }
731 
732 //
733 //resoud le system en effectuant une decomposition de Schgur simultannee
734 //telle que decrite dans Corless Trager issac 97
735 //
736 
737 template<typename lmat,typename typres>
738 void GetRootMat(lmat &lres,typres &res)
739 {
740  list<int> size_clust;
741  compltrigsim(lres,size_clust);
742  // cout<<"fincomple--------------------------------"<<endl;
743  for(typename lmat::iterator iter=lres.begin();iter!=lres.end();iter++)
744 
745  {
746  cout<<*iter<<endl<<endl;
747  typres tmpres((lres.front()).nbrow(),lres.size());
748  // cout<<"si"<<endl;
749  solvefromcompltrigsim(lres,size_clust,tmpres);
750  // cout<<"no"<<endl;
751 
752  res=tmpres;
753 
754  }
755 }
756 
757 template<typename typres,typename Base>
758 void realsolve2(const list<harewell<QQ> > &lmat,const Base &b,typres &res)
759 {
760  //int i;
761  list<MatDense<lapack<typename typres::coeff_t > > >lres;
762 
763  cout<<"RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR"<<endl;
764  for(list<harewell<QQ> >::const_iterator iter=lmat.begin()
765  ;iter!=lmat.end();iter++)
766  {
767  MatDense<lapack<typename typres::coeff_t > >
768  tmp(iter->nrow,iter->ncol);
769  (tmp.rep).reserve(iter->nrow,iter->ncol);
770  for(int j=0;j<iter->nrow;j++)
771  for(int k=0;k<iter->ncol;k++)
772  {
773  tmp(j,k)=mpq_get_d(&(*iter)(j,k).rep());
774  // tmp(j,k)=&(*iter)(j,k).rep();
775  //tmp(i,k).imag()=0;
776  //fortement sous optimal
777  }
778  lres.push_back(tmp);
779  }
780  cout<<"RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR"<<endl;
781  GetRootMat(lres,res);
782 }
783 
784 template<typename typres, typename Base>
785 void realsolve2(const list<harewell<RR> > &lmat
786  ,const Base &b,typres &res)
787 {
788  // int i;
789  // cout<<"entra aqui"<<endl;
790  typedef typename typres::coeff_t T;//Ca c deja complex
791  list<MatDense<lapack<typename typres::coeff_t > > > lres;
792 
793  for(list<harewell<RR> >::const_iterator iter=lmat.begin()
794  ;iter!=lmat.end();iter++)
795  {
796  MatDense<lapack<typename typres::coeff_t> >
797  tmp(iter->nrow,iter->ncol);
798  (tmp.rep).reserve(iter->nrow,iter->ncol);
799  for(int j=0;j<iter->nrow;j++)
800  for(int k=0;k<iter->ncol;k++)
801  {
802  //char *toto=NULL;
803  char _toto [256]; char* toto= & (_toto[0]);
804  mp_exp_t expon;
805  int sign=1;
806  toto= mpf_get_str(toto,&expon,10,50,(*iter)(j,k).rep());
807  tmp(j,k)=(T)(0);
808  if (toto[0]=='-')
809  {
810  sign=-1;
811  toto[0]='0';
812  }
813  int l;
814  for(l=0;toto[l]!='\0';l++)
815  {
816  tmp(j,k)=(T)(10)*tmp(j,k)+(T)(toto[l]-'0');
817  //cout<<toto<<endl;
818  //cout<<tmp(j,k)<<endl;
819  }
820  // cout<<-l+1+expon-(sign+1)/2<<endl;
821  tmp(j,k)*=(T)(sign*pow(10.0,(int)(-l+1+expon-((sign+1)/2))));
822 
823  // cout<<"toto "<<toto<<endl;
824  // cout<<"tmp(j,k) a la fin "<<tmp(j,k)<<" "
825  // <<mpf_get_d((*iter)(j,k).rep())<<endl;;
826  //free(toto);
827  //tmp(i,k).imag()=0;
828  //fortement sous optimal
829  }
830  lres.push_back(tmp);
831  }
832  GetRootMat(lres,res);
833 }
834 template<typename typcoeff,typename typres, typename Base>
835 void realsolve2(const list<harewell<typcoeff> > &lmat
836  ,const Base &b,typres &res)
837 {
838 
839  int i;
840  list<MatDense<lapack<typename typres::coeff_t > > >lres;
841 
842  for(typename list<harewell<typcoeff> >::const_iterator iter=lmat.begin()
843  ;iter!=lmat.end();iter++)
844  {
845  MatDense<lapack<typename typres::coeff_t > > tmp(iter->nrow
846  ,iter->ncol);
847  (tmp.rep).reserve(iter->nrow,iter->ncol);
848  for(int j=0;j<iter->nrow;j++)
849  for(int k=0;k<iter->ncol;k++)
850  {
851  tmp(j,k)= (*iter)(j,k);
852  //fortement sous optimal
853  }
854  lres.push_back(tmp);
855  }
856  GetRootMat(lres,res);
857 }
858 
859 template<int p,typename typrep,typename typres, typename Base>
860 void realsolve2(const list<harewell<Z<p,typrep> > > &lmat
861  ,const Base &b,typres &res)
862 {
863  res.ncol=0;
864  res.nrow=0;
865 }
866 template<typename typres,
867  typename typlmat,typename Base>
868 void realsolve3(const typlmat &lmat,const Base &b,typres &res)
869 {
870  int i;
871  typedef typename typlmat::value_type::coeff_t::value_type T;
872  list<MatDense<lapack<complex<T > > > >lres;
873 
874  for(typename typlmat::const_iterator iter=lmat.begin()
875  ;iter!=lmat.end();iter++)
876  {
877  MatDense<lapack<complex<T> > > tmp(iter->nrow,iter->ncol);
878  (tmp.rep).reserve(iter->nrow,iter->ncol);
879  for(int j=0;j<iter->nrow;j++)
880  for(int k=0;k<iter->ncol;k++)
881  {
882  tmp(j,k)=mpq_get_d(&(*iter)(j,k).rep());
883  //tmp(i,k).imag()=0;
884  //fortement sous optimal
885  }
886  lres.push_back(tmp);
887  }
888  GetRootMat(lres,res);
889 }
890 #undef MYTYPE
891 
void realsolve2(const list< harewell< QQ > > &lmat, const Base &b, typres &res)
Definition: resolve2.hpp:758
void trigsim(list< Mat > &lmat)
Definition: resolve2.hpp:201
#define max(a, b)
Definition: alp_f2c.H:167
void solvefromcompltrigsim(const list< Mat > &lmat, const list< int > &size_clust, Complex_Mat &res)
Definition: resolve2.hpp:582
void free(void *)
int docluster(Mat &M, Mat &Shurvect, typcond *cond, int i)
Definition: resolve2.hpp:360
int ztrsen_(const char *job, const char *compq, logical *select, tla_int *n, doublecomplex< T > *t, tla_int *ldt, doublecomplex< T > *q, tla_int *ldq, doublecomplex< T > *w, tla_int *m, T *s, T *sep, doublecomplex< T > *work, tla_int *lwork, tla_int *info)
doublereal i
Definition: alp_f2c.H:30
MSKaccmodee MSKint32t MSKint32t MSKint32t MSKrealt value
Definition: mosek.h:2463
void compltrigsim(list< Mat > &lmat, list< int > &size_clust)
Definition: resolve2.hpp:521
MSKint32t MSKint32t MSKint32t MSKrealt * val
Definition: mosek.h:2359
void realsolve3(const typlmat &lmat, const Base &b, typres &res)
Definition: resolve2.hpp:868
long flag
Definition: alp_f2c.H:52
void * malloc(YYSIZE_T)
Scl< T > & pow(const Scl< T > &base, unsigned long exp)
Definition: BC.hpp:1075
int sign(const Scl< T > &b)
Definition: BC.hpp:307
MSKconetypee MSKrealt MSKint32t MSKint32t j
Definition: mosek.h:2421
MSKint32t k
Definition: mosek.h:2713
Definition: Zp.hpp:19
void GetRootMat(lmat &lres, typres &res)
Definition: resolve2.hpp:738
void solvefromtrigsim(const list< Mat > &lmat, Complex_Mat &res)
Definition: resolve2.hpp:633
int getcluster(Mat &M, typcond *cond)
Definition: resolve2.hpp:266
T cond_number(Mat &Combilin, int i)
Definition: resolve2.hpp:249
typedef void(MSKAPI *MSKexitfunc)(MSKuserhandle_t usrptr
doublereal r
Definition: alp_f2c.H:30
Definition: types.hpp:14
Scl< T > & sqrt(const Scl< T > &b)
Definition: BC.hpp:1103
MSKint32t MSKint32t MSKint32t i
Definition: mosek.h:2278
void dgees_(char *JOBVS, char *SORT, bool(*SELECT)(double, double), int *N, double *A, int *LDA, int *SDIM, double *WR, double *WI, double *VS, int *LDVS, double *WORK, int *LWORK, double *BWORK, int *info)
#define abs(x)
Definition: f2c.H:161
int zgebal_(...)
MSKrescodee r
Definition: mosek.h:2321
long int select_true(doublecomplex< T > *toto)
Definition: resolve2.hpp:75
MSKstreamtypee MSKint32t MSKint32t MSKint32t MSKint32t MSKint32t MSKint32t MSKint32t MSKint32t MSKint32t a
Definition: mosek.h:3833
Mat wraplapack_dgees(const Mat &M)
Definition: resolve2.hpp:45
void reorder(Mat &Combilin, Mat &Shurvect, Mat &invShurvect, list< int > &size_clust)
Definition: resolve2.hpp:470
long int logical
Definition: resolve.hpp:8
Mat wraplapack_zgees(Mat M)
Definition: resolve2.hpp:88
Mat wraplapack_zgebal(Mat M)
Definition: resolve2.hpp:683
int zgees_(const char *jobvs, const char *sort, logical(*select)(doublecomplex< T > *), tla_int *n, doublecomplex< T > *a, tla_int *lda, tla_int *sdim, doublecomplex< T > *w, doublecomplex< T > *vs, tla_int *ldvs, doublecomplex< T > *work, tla_int *lwork, T *rwork, logical *bwork, tla_int *info)
Definition: alp_f2c.H:30
void wraplapack_ztrsen(Mat &M, logical *select, coeff *res)
Definition: resolve2.hpp:218
long min(long a, long b)
Definition: workspace.hpp:3
Home  |  Download & InstallContributions