Clearmatics Libff  0.1
C++ library for Finite Fields and Elliptic Curves
double.cpp
Go to the documentation of this file.
1 
12 #include <cmath>
13 #include <complex>
15 #include <libff/common/double.hpp>
16 #include <math.h>
17 
18 namespace libff
19 {
20 
21 Double::Double() { val = std::complex<double>(0, 0); }
22 
23 Double::Double(double real) { val = std::complex<double>(real, 0); }
24 
25 Double::Double(double real, double imag)
26 {
27  val = std::complex<double>(real, imag);
28 }
29 
30 Double::Double(std::complex<double> num) { val = num; }
31 
32 unsigned Double::add_cnt = 0;
33 unsigned Double::sub_cnt = 0;
34 unsigned Double::mul_cnt = 0;
35 unsigned Double::inv_cnt = 0;
36 
37 Double Double::operator+(const Double &other) const
38 {
39 #ifdef PROFILE_OP_COUNTS
40  ++add_cnt;
41 #endif
42 
43  return Double(val + other.val);
44 }
45 
46 Double Double::operator-(const Double &other) const
47 {
48 #ifdef PROFILE_OP_COUNTS
49  ++sub_cnt;
50 #endif
51 
52  return Double(val - other.val);
53 }
54 
55 Double Double::operator*(const Double &other) const
56 {
57 #ifdef PROFILE_OP_COUNTS
58  ++mul_cnt;
59 #endif
60 
61  return Double(val * other.val);
62 }
63 
65 {
66  if (val.imag() == 0)
67  return Double(-val.real());
68 
69  return Double(-val.real(), -val.imag());
70 }
71 
73 {
74 #ifdef PROFILE_OP_COUNTS
75  ++add_cnt;
76 #endif
77 
78  this->val = std::complex<double>(val + other.val);
79  return *this;
80 }
81 
83 {
84 #ifdef PROFILE_OP_COUNTS
85  ++sub_cnt;
86 #endif
87 
88  this->val = std::complex<double>(val - other.val);
89  return *this;
90 }
91 
93 {
94 #ifdef PROFILE_OP_COUNTS
95  ++mul_cnt;
96 #endif
97 
98  this->val *= std::complex<double>(other.val);
99  return *this;
100 }
101 
102 bool Double::operator==(const Double &other) const
103 {
104  return (std::abs(val.real() - other.val.real()) < 0.000001) &&
105  (std::abs(val.imag() - other.val.imag()) < 0.000001);
106 }
107 
108 bool Double::operator!=(const Double &other) const
109 {
110  return Double(val) == other ? 0 : 1;
111 }
112 
113 bool Double::operator<(const Double &other) const
114 {
115  return (val.real() < other.val.real());
116 }
117 
118 bool Double::operator>(const Double &other) const
119 {
120  return (val.real() > other.val.real());
121 }
122 
124 {
125  return Double(pow(val, power.as_ulong()));
126 }
127 
128 Double Double::operator^(const size_t power) const
129 {
130  return Double(pow(val, power));
131 }
132 
134 {
135 #ifdef PROFILE_OP_COUNTS
136  ++inv_cnt;
137 #endif
138 
139  return Double(std::complex<double>(1) / val);
140 }
141 
143 {
144  return libff::bigint<1>(val.real());
145 }
146 
147 unsigned long Double::as_ulong() const { return round(val.real()); }
148 
149 Double Double::squared() const { return Double(val * val); }
150 
151 Double Double::one() { return Double(1); }
152 
153 Double Double::zero() { return Double(0); }
154 
155 Double Double::random_element() { return Double(std::rand() % 1001); }
156 
158 
160 
162 
163 } // namespace libff
libff::Double::operator-=
Double & operator-=(const Double &other)
Definition: double.cpp:82
libff::Double::operator*
Double operator*(const Double &other) const
Definition: double.cpp:55
libff::Double::operator!=
bool operator!=(const Double &other) const
Definition: double.cpp:108
libff
Definition: ffi.cpp:8
libff::Double::zero
static Double zero()
Definition: double.cpp:153
bigint.hpp
libff::Double::random_element
static Double random_element()
Definition: double.cpp:155
libff::Double::inverse
Double inverse() const
Definition: double.cpp:133
libff::Double::val
std::complex< double > val
Definition: double.hpp:24
libff::Double::geometric_generator
static Double geometric_generator()
Definition: double.cpp:157
libff::Double::operator*=
Double & operator*=(const Double &other)
Definition: double.cpp:92
libff::Double::operator==
bool operator==(const Double &other) const
Definition: double.cpp:102
libff::Double::operator+
Double operator+(const Double &other) const
Definition: double.cpp:37
libff::Double::as_bigint
libff::bigint< 1 > as_bigint() const
Definition: double.cpp:142
libff::Double::arithmetic_generator
static Double arithmetic_generator()
Definition: double.cpp:159
libff::Double::mul_cnt
static unsigned mul_cnt
Definition: double.hpp:36
libff::Double
Definition: double.hpp:21
libff::Double::operator<
bool operator<(const Double &other) const
Definition: double.cpp:113
libff::Double::as_ulong
unsigned long as_ulong() const
Definition: double.cpp:147
libff::Double::operator^
Double operator^(const libff::bigint< 1 > power) const
Definition: double.cpp:123
libff::Double::add_cnt
static unsigned add_cnt
Definition: double.hpp:34
libff::Double::squared
Double squared() const
Definition: double.cpp:149
libff::Double::operator-
Double operator-() const
Definition: double.cpp:64
libff::bigint
Definition: bigint.hpp:20
libff::Double::multiplicative_generator
static Double multiplicative_generator
Definition: double.hpp:68
libff::Double::one
static Double one()
Definition: double.cpp:151
libff::Double::inv_cnt
static unsigned inv_cnt
Definition: double.hpp:37
libff::power
FieldT power(const FieldT &base, const bigint< m > &exponent)
double.hpp
libff::Double::Double
Double()
Definition: double.cpp:21
libff::Double::operator>
bool operator>(const Double &other) const
Definition: double.cpp:118
libff::Double::sub_cnt
static unsigned sub_cnt
Definition: double.hpp:35
libff::Double::operator+=
Double & operator+=(const Double &other)
Definition: double.cpp:72