Borderbasix

io.hpp
Go to the documentation of this file.
1 #ifndef ALREADY_io
2 #define ALREADY_io
3 
5 #include<stdio.h>
6 #include<iostream>
7 #include<iomanip>
8 #include<fstream>
9 
10 using std::setprecision;
11 
12 template<typename typmat>
13 void affdebug2(const typmat &m)
14 {
15  cout<<"[";
16  for(int i=0;i<m.nrow;i++)
17  {
18  cout<<"[";
19  for(int j=0;j<m.ncol-1;j++)
20  {
21  if (m(i,j)!=0) {printf("*");}
22  else {printf(" ");}
23  }
24 
25  printf("]\n");
26  }
27  cout<<"]"<<endl;
28 }
29 template<typename typmat>
30 void affdebug(const typmat &m)
31 {
32  int i;
33  cout<<setprecision(6);
34  cout<<"nrow "<<m.nrow<<" ncol "<<m.ncol<<endl;
35  cout<<"[";
36  for(i=0;i<m.nrow-1;i++)
37  {
38  int j;
39  cout<<"[";
40  for(j=0;j<m.ncol-1;j++)
41  {
42  // cout<<" i "<<i<<" j "<<j<<endl;
43  cout<<m(i,j)<<",";
44  }
45  cout<<m(i,j)<<"],"<<endl;
46  }
47  {
48  int j;
49  cout<<"[";
50  for(j=0;j<m.ncol-1;j++)
51  cout<<m(i,j)<<",";
52  cout<<m(i,j)<<"]"<<endl;
53  }
54  cout<<"]"<<endl<<setprecision(16);
55 }
56 
57 template <template<class T> class typmat, class C>
58 void raw_printmatfile(const typmat<C> & m,const char * name) {
59  int i;
60  int j;
61  std::ofstream sfic(name);
62  sfic.unsetf(std::ios::floatfield);
63  sfic.precision(16);
64  //fprintf(fic,"%s:=[",name);
65 
66  for(i=0;i<m.nrow-1;i++) {
67  int k;
68  //fprintf(fic,"[");
69  for(k=0;k<m.ncol-1;k++) {
70  //fprintf(fic,"%.16f,",m(i,j));
71  sfic << m(i,k) << "\n";
72  }
73  //mpq_out_str(fic,10,&(m(i,j).rep()));
74  //fprintf(fic,"%.16f],\n",m(i,j));
75  sfic << m(i,k) << "\n" ;
76  }
77  for(j=0;j<m.ncol-1;j++) {
78  //mpq_out_str(fic,10,&(m(i,j).rep()));
79  //fprintf(fic,"%.16f,",m(i,j));
80  sfic << m(i,j) << "\n";
81  }
82  //mpq_out_str(fic,10,&(m(i,j).rep()));
83  //fprintf(fic,"%.16f]\n];\n",m(i,j));
84  sfic << m(i,j);
85 
86  sfic.close();
87 }
88 
89 
90 template<template<typename T> class typmat,typename toto >
91 void printmatfile(const typmat<toto> & m,const char * name,FILE * fic)
92 {}
93 #define _COMPLEX_
94 #ifdef _COMPLEX_
95 #include<complex>
96 using std::complex;
97 
98 template<template<typename T> class typmat >
99 void printmatfile(const typmat<complex<double> > & m,const char * name,FILE * fic)
100 {int i;
101  fprintf(fic,"%s:=[",name);
102  for(i=0;i<m.nrow-1;i++)
103  {
104  int j;
105  fprintf(fic,"[");
106  for(j=0;j<m.ncol-1;j++)
107  {
108  fprintf(fic,"%.16f+(%.16f)*I,",m(i,j).real(),m(i,j).imag());
109  }
110  //mpq_out_str(fic,10,&(m(i,j).rep()));
111  fprintf(fic,"%.16f+(%.16f)*I],\n",m(i,j).real(),m(i,j).imag());
112  }
113  {
114  int j;
115  fprintf(fic,"[");
116  for(j=0;j<m.ncol-1;j++)
117  {
118  //mpq_out_str(fic,10,&(m(i,j).rep()));
119  fprintf(fic,"%.16f+(%.16f)*I,",m(i,j).real(),m(i,j).imag());
120  }
121  //mpq_out_str(fic,10,&(m(i,j).rep()));
122  fprintf(fic,"%.16f+(%.16f)*I]\n];\n",m(i,j).real(),m(i,j).imag());
123  }
124 }
125 
126 template<template<typename T> class typmat >
127 void printmatfile(const typmat<complex<long double> > & m,const char * name,
128  FILE * fic)
129 {int i;
130  if(m.nrow==0) return;
131  fprintf(fic,"%s:=[",name);
132  for(i=0;i<m.nrow-1;i++)
133  {
134  int j;
135  fprintf(fic,"[");
136  for(j=0;j<m.ncol-1;j++)
137  {
138  fprintf(fic,"%.16Lf+(%.16Lf)*I,",m(i,j).real(),m(i,j).imag());
139  }
140  //mpq_out_str(fic,10,&(m(i,j).rep()));
141  fprintf(fic,"%.16Lf+(%.16Lf)*I],\n",m(i,j).real(),m(i,j).imag());
142  }
143  {
144  int j;
145  fprintf(fic,"[");
146  for(j=0;j<m.ncol-1;j++)
147  {
148  //mpq_out_str(fic,10,&(m(i,j).rep()));
149  fprintf(fic,"%.16Lf+(%.16Lf)*I,",m(i,j).real(),m(i,j).imag());
150  }
151  //mpq_out_str(fic,10,&(m(i,j).rep()));
152  fprintf(fic,"%.16Lf+(%.16Lf)*I]\n];\n",m(i,j).real(),m(i,j).imag());
153  }
154 }
155 
156 template<template<typename T> class typmat >
157 void printmatfile(const typmat<complex<RR> > & m,const char * name,FILE * fic)
158 {
159  int i;
160  fprintf(fic,"%s:=[",name);
161  for(i=0;i<m.nrow-1;i++)
162  {
163  int j;
164  fprintf(fic,"[");
165  for(j=0;j<m.ncol-1;j++)
166  {
167  mpf_out_str(fic,10,NBDIGITS,m(i,j).real().rep());
168  fprintf(fic,"+I*(");
169  mpf_out_str(fic,10,NBDIGITS,m(i,j).imag().rep());
170  fprintf(fic,"),");
171  }
172  //mpq_out_str(fic,10,&(m(i,j).rep()));
173  mpf_out_str(fic,10,NBDIGITS,(m(i,j).real().rep()));
174  fprintf(fic,"+I*(");
175  mpf_out_str(fic,10,NBDIGITS,m(i,j).imag().rep());
176  fprintf(fic,")],");
177 
178  //fprintf(fic,"%.16f+(%.16f)*I],\n",m(i,j).real(),m(i,j).imag());
179  }
180  {
181  int j;
182  fprintf(fic,"[");
183  for(j=0;j<m.ncol-1;j++)
184  {
185  //mpq_out_str(fic,10,&(m(i,j).rep()));
186  mpf_out_str(fic,10,NBDIGITS,(m(i,j).real().rep()));
187  fprintf(fic,"+I*(");
188  mpf_out_str(fic,10,NBDIGITS,m(i,j).imag().rep());
189  fprintf(fic,"),");
190 
191  //fprintf(fic,"%.16f+(%.16f)*I,",m(i,j).real(),m(i,j).imag());
192  }
193  //mpq_out_str(fic,10,&(m(i,j).rep()));
194  //fprintf(fic,"%.16f+(%.16f)*I]\n];\n",m(i,j).real(),m(i,j).imag());
195  mpf_out_str(fic,10,NBDIGITS,(m(i,j).real().rep()));
196  fprintf(fic,"+I*(");
197  mpf_out_str(fic,10,NBDIGITS,m(i,j).imag().rep());
198  fprintf(fic,")]\n];\n");
199 
200  }
201 }
202 
203 template<template<typename T> class typmat >
204 void printmatfilescilab(const typmat<complex<double> > & m,const char * name,FILE * fic)
205 {int i;
206  fprintf(fic,"%s=[",name);
207  for(i=0;i<m.nrow-1;i++)
208  {
209  int j;
210  fprintf(fic,"");
211  for(j=0;j<m.ncol-1;j++)
212  {
213  fprintf(fic,"%.16e+(%.16e)*I,",m(i,j).real(),m(i,j).imag());
214  }
215  //mpq_out_str(fic,10,&(m(i,j).rep()));
216  fprintf(fic,"%.16e+(%.16e)*I;\n",m(i,j).real(),m(i,j).imag());
217  }
218  {
219  int j;
220  fprintf(fic,"");
221  for(j=0;j<m.ncol-1;j++)
222  {
223  //mpq_out_str(fic,10,&(m(i,j).rep()));
224  fprintf(fic,"%.16e+(%.16e)*I,",m(i,j).real(),m(i,j).imag());
225  }
226  //mpq_out_str(fic,10,&(m(i,j).rep()));
227  fprintf(fic,"%.16e+(%.16e)*I\n];\n",m(i,j).real(),m(i,j).imag());
228  }
229 }
230 
231 template<template<typename T> class typmat >
232 void printmatfilescilab(const typmat<complex<long double> > & m,const char * name,FILE * fic)
233 {int i;
234  fprintf(fic,"%s=[",name);
235  for(i=0;i<m.nrow-1;i++)
236  {
237  int j;
238  fprintf(fic,"");
239  for(j=0;j<m.ncol-1;j++)
240  {
241  fprintf(fic,"%.32Le+(%.32Le)*I,",m(i,j).real(),m(i,j).imag());
242  }
243  //mpq_out_str(fic,10,&(m(i,j).rep()));
244  fprintf(fic,"%.32Le+(%.32Le)*I;\n",m(i,j).real(),m(i,j).imag());
245  }
246  {
247  int j;
248  fprintf(fic,"");
249  for(j=0;j<m.ncol-1;j++)
250  {
251  //mpq_out_str(fic,10,&(m(i,j).rep()));
252  fprintf(fic,"%.32Le+(%.32Le)*I,",m(i,j).real(),m(i,j).imag());
253  }
254  //mpq_out_str(fic,10,&(m(i,j).rep()));
255  fprintf(fic,"%.32Le+(%.32Le)*I\n];\n",m(i,j).real(),m(i,j).imag());
256  }
257 }
258 #endif
259 
260 template<template<typename T> class typmat >
261 void printmatfile(const typmat<double> & m,const char * name,FILE * fic)
262 {
263  int i;
264  fprintf(fic,"%s:=[",name);
265  for(i=0;i<m.nrow-1;i++)
266  {
267  int j;
268  fprintf(fic,"[");
269  for(j=0;j<m.ncol-1;j++)
270  {
271  fprintf(fic,"%.16f,",m(i,j));
272  }
273  //mpq_out_str(fic,10,&(m(i,j).rep()));
274  fprintf(fic,"%.16f],\n",m(i,j));
275  }
276  {
277  int j;
278  fprintf(fic,"[");
279  for(j=0;j<m.ncol-1;j++)
280  {
281  //mpq_out_str(fic,10,&(m(i,j).rep()));
282  fprintf(fic,"%.16f,",m(i,j));
283  }
284  //mpq_out_str(fic,10,&(m(i,j).rep()));
285  fprintf(fic,"%.16f]\n];\n",m(i,j));
286  }
287 }
288 
289 template<template<typename T> class typmat >
290 void printmatfile(const typmat<long double> & m,const char * name,FILE * fic)
291 {
292  int i;
293  fprintf(fic,"%s:=[",name);
294  for(i=0;i<m.nrow-1;i++)
295  {
296  int j;
297  fprintf(fic,"[");
298  for(j=0;j<m.ncol-1;j++)
299  {
300  fprintf(fic,"%.16Lf,",m(i,j));
301  }
302  //mpq_out_str(fic,10,&(m(i,j).rep()));
303  fprintf(fic,"%.16Lf],\n",m(i,j));
304  }
305  {
306  int j;
307  fprintf(fic,"[");
308  for(j=0;j<m.ncol-1;j++)
309  {
310  //mpq_out_str(fic,10,&(m(i,j).rep()));
311  fprintf(fic,"%.16Lf,",m(i,j));
312  }
313  //mpq_out_str(fic,10,&(m(i,j).rep()));
314  fprintf(fic,"%.16Lf]\n];\n",m(i,j));
315  }
316 }
317 
318 #ifdef MON_PREMIER
319 template<template<typename T> class typmat ,typename TT>
320 void printmatfile(const typmat<Z<MON_PREMIER,TT> > & m,const char * name,FILE * fic)
321 {
322  int i;
323  fprintf(fic,"%s:=[",name);
324  for(i=0;i<m.nrow-1;i++)
325  {
326  int j;
327  fprintf(fic,"[");
328  for(j=0;j<m.ncol-1;j++)
329  {
330  fprintf(fic,"%d,",m(i,j).rep);
331  }
332  //mpq_out_str(fic,10,&(m(i,j).rep()));
333  fprintf(fic,"%d],\n",m(i,j).rep);
334  }
335  {
336  int j;
337  fprintf(fic,"[");
338  for(j=0;j<m.ncol-1;j++)
339  {
340  //mpq_out_str(fic,10,&(m(i,j).rep()));
341  fprintf(fic,"%d,",m(i,j).rep);
342  }
343  //mpq_out_str(fic,10,&(m(i,j).rep()));
344  fprintf(fic,"%d]\n];\n",m(i,j).rep);
345  }
346 }
347 #endif
348 
349 template<template<typename T> class typmat >
350 void printmatfile(const typmat<Scl<MPQ> > & m,const char * name,FILE * fic)
351 {
352  int i;
353  fprintf(fic,"%s:=[",name);
354  for(i=0;i<m.nrow-1;i++)
355  {
356  int j;
357  fprintf(fic,"[");
358  for(j=0;j<m.ncol-1;j++)
359  {
360  mpq_out_str(fic,10,&(m(i,j).rep()));
361  fprintf(fic,",");
362  }
363  mpq_out_str(fic,10,&(m(i,j).rep()));
364  fprintf(fic,"],\n");
365  }
366  {
367  int j;
368  fprintf(fic,"[");
369  for(j=0;j<m.ncol-1;j++)
370  {
371  mpq_out_str(fic,10,&(m(i,j).rep()));
372  fprintf(fic,",");
373  }
374  mpq_out_str(fic,10,&(m(i,j).rep()));
375  fprintf(fic,"]\n];\n");
376  }
377 }
378 
379 
380 template<template<typename T> class typmat >
381 void printmatfile_pequan(const typmat<Scl<MPF> > & m,FILE * fic)
382 {
383  int i,j;
384  fprintf(fic,"%d %d\n",m.nrow,m.ncol);
385  for(i=0;i<m.nrow;i++)
386  {
387  for(j=0;j<m.ncol;j++)
388  {
389  fprintf(fic,"%d %d ",i,j);
390  mpf_out_str(fic,10,NBDIGITS,(m(i,j).rep()));
391  fprintf(fic,"\n");
392  }
393  }
394 }
395 
396 template<template<typename T> class typmat >
397 void printmatfile(const typmat<Scl<MPF> > & m,char * name,FILE * fic)
398 {
399  int i;
400  fprintf(fic,"%s:=[",name);
401  for(i=0;i<m.nrow-1;i++)
402  {
403  int j;
404  fprintf(fic,"[");
405  for(j=0;j<m.ncol-1;j++)
406  {
407  mpf_out_str(fic,10,NBDIGITS,(m(i,j).rep()));
408  fprintf(fic,",");
409  }
410  mpf_out_str(fic,10,NBDIGITS,(m(i,j).rep()));
411  fprintf(fic,"],\n");
412  }
413  {
414  int j;
415  fprintf(fic,"[");
416  for(j=0;j<m.ncol-1;j++)
417  {
418  mpf_out_str(fic,10,NBDIGITS,(m(i,j).rep()));
419  fprintf(fic,",");
420  }
421  mpf_out_str(fic,10,NBDIGITS,(m(i,j).rep()));
422  fprintf(fic,"]\n];\n");
423  }
424 }
425 
426 // read the
427 //content of the file whose name is name
428 //INPUT : lists of polynomials in BORDERBASIX format
429 //OUTPUT : lists of polynomials given appended with polynomials read
430 // in file <name>
431 //
432 template <typename typP>
433 void readfile(typP &toto,const char *name)
434 {
435 
436  typedef typename typP::value_type pol;
437  pol p;
438  FILE *fic=fopen(name,"r");
439  char * chaine=(char*)malloc(1000000001);
440  for (;fgets(chaine,1000000000,fic)!=NULL;)
441  {
442  // printf("chainelue %s\n",chaine);
443  // cout<<"flush"<<endl;
444  fflush(stdout);
445  // cout<<"despuesfflush"<<endl;
446  //cout<<pol(chaine)<<endl;
447  toto.push_back(p=pol(chaine));
448  // cout<<"g pushe "<<p<<endl;
449  }
450  fclose(fic);
451  free(chaine);
452 }
453 
454 template <typename typP>
455 void readfilemin(typP &toto,int &numeq, int &numineq,char *name)
456 {
457 
458  typedef typename typP::value_type pol;
459  pol p;
460  //int indposi=0;
461  std::ifstream f(name);
462  f>>numeq;
463  f>>numeq;
464  f>>numineq;
465  //FILE *fic=fopen(name,"r");
466  //cout<<"open"<<endl;
467  //char * chaine=(char*)malloc(10000001);
468  char c;
469  std::string chaine;
470  c = f.get();
471  while (f.good()) {
472  c = f.get();
473  while (c != ';' && f.good()) {
474  chaine.push_back(c);
475  c = f.get();
476  }
477  chaine.push_back(';');
478  c = f.get();
479  if(chaine.size()>1) {
480  mdebug()<<"pol"<< chaine;
481  toto.push_back(p=pol(chaine.c_str()));
482  }
483  chaine.resize(0);
484  }
485 
486 // for (;fgets(chaine,10000000,f.)!=NULL;)//!cin.eof();)
487 // {
488 // mdebug() << "chainelue"<<chaine;
489 // // cout<<"flush"<<endl;
490 // //fflush(stdout);
491 // //cout<<"despuesfflush"<<endl;
492 // //cout<<"pol "<< pol(chaine)<<endl;
493 // if (indposi==0)
494 // numeq=atoi(chaine);
495 // else if (indposi==1)
496 // numineq=atoi(chaine);
497 // else {
498 // cout<<"pol "<< pol(chaine)<<endl;
499 // toto.push_back(p=pol(chaine));
500 // }
501 // // cout<<"numineqdentro "<<numineq<<endl;
502 // indposi++;
503 // }
504 // fclose(fic);
505 // free(chaine);
506 }
507 
508 
509 void printcoeff(FILE *fic, double c)
510 {
511  fprintf(fic,"%f",c);
512 }
513 
514 void printcoeff(FILE *fic, long double c)
515 {
516  fprintf(fic,"%Lf",c);
517 }
518 
519 void printcoeff(FILE *fic, Scl<MPQ> c)
520 {
521  mpq_out_str(fic,10,&c.data);
522 }
523 
524 void printcoeff(FILE *fic, Scl<MPF> c)
525 {
526  mpf_out_str(fic,10,30,c.data);
527 }
528 #ifdef __GMP_PLUSPLUS__
529 void printcoeff(FILE *fic, mpf_class c)
530 {
531  mpf_out_str(fic,10,30,c.get_mpf_t());
532 }
533 #endif
534 
535 #ifdef MON_PREMIER
536 template<typename T>
537 void printcoeff(FILE *fic, Z<MON_PREMIER,T> c)
538 {
539  fprintf(fic,"%d",c.rep);
540 }
541 #endif
542 
543 void printmon(FILE *fic, mon m)
544 {
545  printcoeff(fic,m.GetCoeff());
546  for(int i=0;i<=lvar(m.rep);i++)
547  {
548  if (m.GetDegree(i)>=1)
549  {
550  fprintf(fic,"*x%d",i);
551  if (m.GetDegree(i)>1)
552  fprintf(fic,"^%d",(int)m.GetDegree(i));
553  }
554  }
555 }
556 
557 template<typename typdump>
558 void printdump(FILE *fic, const typdump &dump)
559 {
560  fprintf(fic,"#begin: dump\n");
561  for(typename typdump::const_iterator iter=dump.begin();
562  iter!=dump.end();iter++)
563  {
564  fprintf(fic,"k: %d\n",iter->k);
565  fprintf(fic,"size: %d",iter->size);
566  for(int i=0;i<iter->size;i++)
567  {
568  printmon(fic,iter->nf[i].ind);
569  fprintf(fic,"\n");
570  for(int j=0;j<iter->nf[i].size;j++)
571  {
572  if(j>0)
573  fprintf(fic,"+");
574  fprintf(fic,"(");
575  printcoeff(fic,iter->nf[i].nf[j]);
576  fprintf(fic,")*");
577  printmon(fic,monofindex(j,iter->nf[i]));
578 
579  }
580  fprintf(fic,"\n");
581  }
582  }
583  fprintf(fic,"#end: dump\n");
584 }
585 
586 template <typename typdump>
587 void readdump(FILE *fic,typdump &toto)
588 {
589  typedef typename typdump::value_type pol;
590  pol p;
591  char * chaine=(char*)malloc(10000001);
592  for (;fgets(chaine,10000000,fic)!=NULL;)
593  {
594  printf("chainelue %s\n",chaine);fflush(stdout);
595  toto.push_back(p=pol(chaine));
596  cout<<"g pushe "<<p<<endl;
597  }
598  free(chaine);
599 }
600 #endif //ALREADY_io
mon monofindex(int j, pol< mon, T > &p)
Definition: pol.hpp:210
void affdebug(const typmat &m)
Definition: io.hpp:30
void readdump(FILE *fic, typdump &toto)
Definition: io.hpp:587
int lvar(const dynamicexp< X, E > &A)
Definition: dynamicexp.hpp:178
void free(void *)
void affdebug2(const typmat &m)
Definition: io.hpp:13
T data
Definition: Scl.hpp:34
C GetCoeff() const
Definition: Monom.hpp:67
void * malloc(YYSIZE_T)
void readfile(typP &toto, const char *name)
Definition: io.hpp:433
void raw_printmatfile(const typmat< C > &m, const char *name)
Definition: io.hpp:58
MSKconetypee MSKrealt MSKint32t MSKint32t j
Definition: mosek.h:2421
void printdump(FILE *fic, const typdump &dump)
Definition: io.hpp:558
Definition: mdebug.hpp:11
MSKint32t k
Definition: mosek.h:2713
R rep
Definition: Monom.hpp:30
Definition: Zp.hpp:19
void printmatfile_pequan(const typmat< Scl< MPF > > &m, FILE *fic)
Definition: io.hpp:381
Definition: Scl.hpp:26
void printmatfile(const typmat< toto > &m, const char *name, FILE *fic)
Definition: io.hpp:91
exponent_t GetDegree() const
Definition: Monom.hpp:70
MSKrealt * c
Definition: mosek.h:2678
MSKint32t MSKint32t MSKint32t i
Definition: mosek.h:2278
void printmon(FILE *fic, mon m)
Definition: io.hpp:543
void printmatfilescilab(const typmat< complex< double > > &m, const char *name, FILE *fic)
Definition: io.hpp:204
T rep
Definition: Zp.hpp:25
#define NBDIGITS
Definition: solver_bb_floating.cpp:104
void printcoeff(FILE *fic, double c)
Definition: io.hpp:509
#define pol
Definition: pol2ter.hpp:3
Multivariate monomials.
Definition: Monom.hpp:21
void readfilemin(typP &toto, int &numeq, int &numineq, char *name)
Definition: io.hpp:455
MSKint32t MSKint32t char * name
Definition: mosek.h:2853
Home  |  Download & InstallContributions