1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/** @file
*****************************************************************************
Implementation of complex domain data type.
*****************************************************************************
* @author This file is part of libff, developed by SCIPR Lab
* and contributors (see AUTHORS).
* @copyright MIT license (see LICENSE file)
*****************************************************************************/
#include <cmath>
#include <complex>
#include <math.h>
#include <../trusted_libff/libff/algebra/fields/bigint.hpp>
#include <../trusted_libff/libff/common/double.hpp>
namespace libff {
const double PI = 3.141592653589793238460264338328L;
Double::Double()
{
val = std::complex<double>(0, 0);
}
Double::Double(double real)
{
val = std::complex<double>(real, 0);
}
Double::Double(double real, double imag)
{
val = std::complex<double>(real, imag);
}
Double::Double(std::complex<double> num)
{
val = num;
}
unsigned Double::add_cnt = 0;
unsigned Double::sub_cnt = 0;
unsigned Double::mul_cnt = 0;
unsigned Double::inv_cnt = 0;
Double Double::operator+(const Double &other) const
{
#ifdef PROFILE_OP_COUNTS
++add_cnt;
#endif
return Double(val + other.val);
}
Double Double::operator-(const Double &other) const
{
#ifdef PROFILE_OP_COUNTS
++sub_cnt;
#endif
return Double(val - other.val);
}
Double Double::operator*(const Double &other) const
{
#ifdef PROFILE_OP_COUNTS
++mul_cnt;
#endif
return Double(val * other.val);
}
Double Double::operator-() const
{
if (val.imag() == 0) return Double(-val.real());
return Double(-val.real(), -val.imag());
}
Double& Double::operator+=(const Double &other)
{
#ifdef PROFILE_OP_COUNTS
++add_cnt;
#endif
this->val = std::complex<double>(val + other.val);
return *this;
}
Double& Double::operator-=(const Double &other)
{
#ifdef PROFILE_OP_COUNTS
++sub_cnt;
#endif
this->val = std::complex<double>(val - other.val);
return *this;
}
Double& Double::operator*=(const Double &other)
{
#ifdef PROFILE_OP_COUNTS
++mul_cnt;
#endif
this->val *= std::complex<double>(other.val);
return *this;
}
bool Double::operator==(const Double &other) const
{
return (std::abs(val.real() - other.val.real()) < 0.000001)
&& (std::abs(val.imag() - other.val.imag()) < 0.000001);
}
bool Double::operator!=(const Double &other) const
{
return Double(val) == other ? 0 : 1;
}
bool Double::operator<(const Double &other) const
{
return (val.real() < other.val.real());
}
bool Double::operator>(const Double &other) const
{
return (val.real() > other.val.real());
}
Double Double::operator^(const libff::bigint<1> power) const
{
return Double(pow(val, power.as_ulong()));
}
Double Double::operator^(const size_t power) const
{
return Double(pow(val, power));
}
Double Double::inverse() const
{
#ifdef PROFILE_OP_COUNTS
++inv_cnt;
#endif
return Double(std::complex<double>(1) / val);
}
libff::bigint<1> Double::as_bigint() const
{
return libff::bigint<1>(val.real());
}
unsigned long Double::as_ulong() const
{
return round(val.real());
}
Double Double::squared() const
{
return Double(val * val);
}
Double Double::one()
{
return Double(1);
}
Double Double::zero()
{
return Double(0);
}
Double Double::random_element()
{
return Double(std::rand() % 1001);
}
Double Double::geometric_generator()
{
return Double(2);
}
Double Double::arithmetic_generator()
{
return Double(1);
}
Double Double::multiplicative_generator = Double(2);
} // libff