Borderbasix

Complex.hpp
Go to the documentation of this file.
1 /*********************************************************************
2 * This file is part of the source code of BORDERBASIX software. *
3 * (C) B. Mourrain, INRIA *
4 **********************************************************************/
5 /*
6  Complex.hpp
7  definitions of two functions :
8  Root_Unity : returns a n- root of unity
9  pow : power function to a complex
10 
11  complex<Scl<MP_FLOAT> > specialization
12 
13 */
14 
15 #ifndef INCLUDED_COMPLEXH
16 #define INCLUDED_COMPLEXH
17 #include <complex.h>
18 #define COMPLEX complex<double>
19 
20 #ifdef _GMP
21 #include "Scl_mpf.hpp"
22 #endif //
23 
25 
26 template <class T> struct Real {
28 };
29 
30 #ifdef _GMP
31 template <> struct Real<complex<Scl<MP_FLOAT> > > {
32  typedef Scl<MP_FLOAT> TYPE;
33  static inline void assign(complex<Scl<MP_FLOAT> > & a, const complex<Scl<MP_FLOAT> > & b) {a = b ;};
34  static inline Scl<MP_FLOAT> absolute(const complex<Scl<MP_FLOAT> > & a)
35  {return sqrt(pow(a.real(),2)+pow(a.imag(),2)); }
36  };
37 #endif //
38 
39 template <> struct Real<double> {
40  typedef double TYPE;
41  static inline void assign(double & a, const complex<double> & b) { a = b.real() ;};
42  static inline double absolute(const double & a) { return abs(a); }
43 };
44 
45 #ifdef _GMP
46 struct MP_FLOAT;
47 template <> struct Real<Scl<MP_FLOAT> > {
48  typedef Scl<MP_FLOAT> TYPE;
49  static inline void assign(Scl<MP_FLOAT> & a, const complex<Scl<MP_FLOAT> > & b) {a = b.real() ;};
50  static inline Scl<MP_FLOAT> absolute(const Scl<MP_FLOAT> & a) { return abs(a); }
51  };
52 #endif //
53 
54 template <class T> struct Real<complex<T> > {
55  typedef T TYPE;
56  static inline void assign(complex<T> & a, const complex<T> & b) { a = b ;};
57  static inline double absolute(const complex<T> & a) { return abs(a); }
58 };
59 
60 template <> struct Real<int> {
61  typedef int TYPE;
62  static inline void assign(complex<int> & a, const complex<int> & b) { a = b.real() ;};
63  //static inline double absolute(const complex<int> & a) { return (double) abs(a); }
64 };
65 
66 template <class T> inline void __div_1(T& a, T& b, const T& c, const T& d)
67 {
68  T r(d);r/=c;
69  T s(d); s*=r; s+=c;
70  T x(b); x*=r; x+=a;x/=s;
71  a*=r;
72  b -= a; b /= s;
73  a = x;
74 }
75 //---------------------------------------------------------------------------------------------------
76 /*
77  complex<Scl<MP_FLOAT> > specialization
78 */
79 
80 #ifdef _GMP
81 template <>
82 inline complex<Scl<MP_FLOAT> >& complex<Scl<MP_FLOAT> >::operator /=(const complex<Scl<MP_FLOAT> > &y) {
83  __div_1(re,im , y.real(), y.imag());
84  return *this;
85 }
86 
87 template<> inline complex<Scl<MP_FLOAT> > pow(const complex<Scl<MP_FLOAT> >& z, int k) {
88  double mod = sqrt(norm(complex<double>(ToDouble(z.real()),ToDouble(z.imag()))));
89  if (k == 0)
90  return complex<Scl<MP_FLOAT> > (Scl<MP_FLOAT>(1), Scl<MP_FLOAT>(0));
91  else
92  if (k == -1)
93  return complex<Scl<MP_FLOAT> >(z.real(),Scl<MP_FLOAT>(-1.0*ToDouble(z.imag())/mod));
94  else {
95  double argument = k * arg(complex<double>(ToDouble(z.real()),ToDouble(z.imag())));
96  mod = pow(mod,k);
97  return complex<Scl<MP_FLOAT> > (mod*cos(argument), mod*sin(argument));
98  }
99 }
100 
101 inline complex<Scl<MP_FLOAT> >
102 operator / (const complex<Scl<MP_FLOAT> >& x, double y) {
103  Scl<MP_FLOAT> d(y);
104  return complex<Scl<MP_FLOAT> >(x.real()/d,x.imag()/d); }
105 
106 inline complex<Scl<MP_FLOAT> >
107 operator / (const complex<Scl<MP_FLOAT> >& x,const complex<Scl<MP_FLOAT> > y) {
108  complex<Scl<MP_FLOAT> > res(x);
109  res /= y;
110  return complex<Scl<MP_FLOAT> >(res.real(),res.imag()); }
111 
112 ostream & operator<<(ostream & os, const complex<Scl<MP_FLOAT> > & z){
113  os<<"("<< z.real()<< ","<<z.imag()<<")";
114  return(os);
115  }
116 #endif //
117 //-----------------------------------------------------------------------------------------------------
118 
119 template<class T> inline void Root_Unity(complex<T> &c, int n) {
120  c = complex<T>(cos(8.0*atan(1.0)/n), sin(8.0*atan(1.0)/n));
121 }
122 
123 template<class T> inline complex<T> pow(const complex<T>& z, int k) {
124  T mod = sqrt(norm(z));
125  if (k == 0) return complex<T> (1, T(0));
126  else
127  if (k == -1)
128  return complex<T>(z.real(), -(z.imag())/mod);
129  else {
130  double argument = k * arg(z);
131  mod = pow(mod,k);
132  return complex<T> (mod*cos(argument), mod*sin(argument));
133  }
134 }
135 
136 //---------------------------------------------------------------------------------------------------
137 /*template<class T>
138 inline boolean operator < (const complex<T>& a,const complex<T> & b)
139 {
140  return norm(a) < norm(b);
141  }*/
142 
143 #endif // // !INCLUDED_COMPLEXH
144 
void Root_Unity(complex< T > &c, int n)
Definition: Complex.hpp:119
static void assign(complex< int > &a, const complex< int > &b)
Definition: Complex.hpp:62
static void assign(double &a, const complex< double > &b)
Definition: Complex.hpp:41
complex< T > pow(const complex< T > &z, int k)
Definition: Complex.hpp:123
Scl< T > & operator/(const Scl< T > &b1, const Scl< T > &b2)
Definition: BC.hpp:890
MSKcallbackcodee MSKsoltypee MSKprostae MSKsolstae MSKstakeye MSKstakeye MSKstakeye MSKrealt MSKrealt MSKrealt * y
Definition: mosek.h:2689
MSKaccmodee MSKint32t MSKsoltypee MSKstakeye MSKrealt * x
Definition: mosek.h:3209
Definition: Complex.hpp:24
MSKint32t k
Definition: mosek.h:2713
int TYPE
Definition: Complex.hpp:61
Definition: Complex.hpp:26
Definition: Scl.hpp:26
Scl< T > & sqrt(const Scl< T > &b)
Definition: BC.hpp:1103
MSKrealt * c
Definition: mosek.h:2678
#define abs(x)
Definition: f2c.H:161
MSKrescodee r
Definition: mosek.h:2321
MSKstreamtypee MSKint32t MSKint32t MSKint32t MSKint32t MSKint32t MSKint32t MSKint32t MSKint32t MSKint32t a
Definition: mosek.h:3833
static double absolute(const complex< T > &a)
Definition: Complex.hpp:57
T TYPE
Definition: Complex.hpp:55
static double absolute(const double &a)
Definition: Complex.hpp:42
void __div_1(T &a, T &b, const T &c, const T &d)
Definition: Complex.hpp:66
double TYPE
Definition: Complex.hpp:40
static void assign(complex< T > &a, const complex< T > &b)
Definition: Complex.hpp:56
Warning_Value_Type_Not_Defined TYPE
Definition: Complex.hpp:27
Home  |  Download & InstallContributions