HepLib
CppFormat.cpp
Go to the documentation of this file.
1 
6 #include "SD.h"
7 #include "cln/cln.h"
8 
9 namespace HepLib::SD {
10 
11  GINAC_IMPLEMENT_PRINT_CONTEXT(CppFormat, print_csrc_cl_N)
12 
13  const CppFormat & CppFormat::operator << (const basic & v) const {
14  v.print(*this);
15  return *this;
16  }
17  const CppFormat & CppFormat::operator << (const ex & v) const {
18  v.print(*this);
19  return *this;
20  }
21  const CppFormat & CppFormat::operator << (const lst & v) const {
22  v.print(*this);
23  return *this;
24  }
25  const CppFormat & CppFormat::operator<<(std::ostream& (*v)(std::ostream&)) const {
26  s << v;
27  return *this;
28  }
29 
30  void CppFormat::print_integer(const CppFormat & c, const cln::cl_I & x) {
31  const int max_cln_int = 536870911; // 2^29-1
32  if (x >= cln::cl_I(-max_cln_int) && x <= cln::cl_I(max_cln_int)) {
33  if(c.suffix=="MP") c.s << "mpREAL(";
34  c.s << cln::cl_I_to_int(x);
35  if(c.suffix=="MP") c.s << ")";
36  else c.s << ".0" << c.suffix;
37  } else {
38  print_real(c, cln::cl_float(x));
39  }
40  }
41 
42  void CppFormat::print_real(const CppFormat & c, const cln::cl_R & x) {
43  if (cln::instanceof(x, cln::cl_I_ring)) {
44  print_integer(c, cln::the<cln::cl_I>(x));
45  } else if (cln::instanceof(x, cln::cl_RA_ring)) {
46  const cln::cl_I numer = cln::numerator(cln::the<cln::cl_RA>(x));
47  const cln::cl_I denom = cln::denominator(cln::the<cln::cl_RA>(x));
48  if (cln::plusp(x)) {
49  c.s << "(";
50  print_integer(c, numer);
51  c.s << "/";
52  print_integer(c, denom);
53  c.s << ")";
54  } else {
55  c.s << "(-(";
56  print_integer(c, -numer);
57  c.s << "/";
58  print_integer(c, denom);
59  c.s << "))";
60  }
61  } else {
62  if(c.suffix=="MP") c.s << "mpREAL(" << c.MQuote;
63  cln::cl_print_flags ourflags;
64  ourflags.default_float_format = cln::float_format(cln::the<cln::cl_F>(x));
65  cln::print_real(c.s, ourflags, x);
66  if(c.suffix=="MP") c.s << c.MQuote << ")";
67  else c.s << c.suffix;
68  }
69  }
70 
71  void CppFormat::print_numeric(const numeric & p, const CppFormat & c, unsigned level) {
72  if (p.is_real()) {
73  print_real(c, cln::the<cln::cl_R>(p.to_cl_N()));
74  } else {
75  if(c.suffix=="D") {
76  c.s << "complex<double>(";
77  print_real(c, cln::realpart(p.to_cl_N()));
78  c.s << ",";
79  print_real(c, cln::imagpart(p.to_cl_N()));
80  c.s << ")";
81  } else if(c.suffix=="L") {
82  c.s << "complex<long double>(";
83  print_real(c, cln::realpart(p.to_cl_N()));
84  c.s << ",";
85  print_real(c, cln::imagpart(p.to_cl_N()));
86  c.s << ")";
87  } else if(c.suffix=="Q") {
88  c.s << "(";
89  print_real(c, cln::realpart(p.to_cl_N()));
90  c.s << "+(";
91  print_real(c, cln::imagpart(p.to_cl_N()));
92  c.s << ")*1.Qi)";
93  } else if(c.suffix=="MP") {
94  c.s << "complex<mpREAL>(";
95  print_real(c, cln::realpart(p.to_cl_N()));
96  c.s << ",";
97  print_real(c, cln::imagpart(p.to_cl_N()));
98  c.s << ")";
99  } else {
100  throw Error("CppFormat: suffix is Wrong.");
101  }
102  }
103  }
104 
105  CppFormat::CppFormat(ostream &os, const string & s, unsigned opt) : print_csrc_cl_N(os, opt), suffix(s) { }
106 
107 }
108 
109 
110 
SecDec header file.
class to export GiNaC expression to cpp format
Definition: SD.h:301
const CppFormat & operator<<(const T &v) const
Definition: SD.h:308
string suffix
Definition: SD.h:305
CppFormat(ostream &os, const string &s="L", unsigned opt=0)
Definition: CppFormat.cpp:105
namespace for Numerical integration with Sector Decomposition method
Definition: AsyMB.cpp:10