forked from Whirligig231/number-field-factorization
-
Notifications
You must be signed in to change notification settings - Fork 0
/
complex.cpp
executable file
·132 lines (104 loc) · 2.88 KB
/
complex.cpp
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
#include "complex.h"
complex::complex() {
this->real = 0;
this->imag = 0;
}
complex::complex(int real) {
this->imag = 0;
this->real = mpf_class(real);
}
complex::complex(mpf_class real) {
this->imag = 0;
this->real = real;
}
complex::complex(mpf_class real, mpf_class imag) {
this->real = real;
this->imag = imag;
}
complex::complex(const complex &other) {
this->real = other.real;
this->imag = other.imag;
}
mpf_class complex::get_real() const {
return this->real;
}
mpf_class complex::get_imag() const {
return this->imag;
}
complex &complex::operator=(mpf_class value) {
this->real = value;
this->imag = 0;
return *this;
}
complex &complex::operator=(const complex &other) {
this->real = other.real;
this->imag = other.imag;
return *this;
}
bool complex::operator==(const complex &other) const {
return (this->real == other.real) && (this->imag == other.imag);
}
bool complex::operator!=(const complex &other) const {
return (this->real != other.real) || (this->imag != other.imag);
}
complex &complex::operator+=(const complex &other) {
this->real += other.real;
this->imag += other.imag;
return *this;
}
complex &complex::operator-=(const complex &other) {
this->real -= other.real;
this->imag -= other.imag;
return *this;
}
complex &complex::operator*=(const complex &other) {
mpf_class temp = this->real;
this->real = this->real * other.real - this->imag * other.imag;
this->imag = temp * other.imag + this->imag * other.real;
return *this;
}
complex &complex::operator/=(const complex &other) {
return (*this) *= other.inv();
}
complex complex::operator+(const complex &other) const {
return complex(*this) += other;
}
complex complex::operator-(const complex &other) const {
return complex(*this) -= other;
}
complex complex::operator*(const complex &other) const {
return complex(*this) *= other;
}
complex complex::operator/(const complex &other) const {
return complex(*this) /= other;
}
complex complex::operator-() const {
return complex(-this->real, -this->imag);
}
complex complex::inv() const {
return complex(
this->real / (this->real*this->real + this->imag*this->imag),
-this->imag / (this->real*this->real + this->imag*this->imag)
);
}
complex complex::conjugate() const {
return complex(this->real, -this->imag);
}
mpf_class complex::norm() const {
return ((*this) * this->conjugate()).get_real();
}
std::ostream &operator<<(std::ostream &os, const complex &m) {
return os << m.real << " + " << m.imag << "i";
}
complex util<complex>::zero() {
return complex(0, 0);
}
complex util<complex>::zero(const complex &reference) {
return complex(0, 0);
}
complex util<complex>::one(const complex &reference) {
return complex(1, 0);
}
complex util<complex>::from_int(int n, const complex &reference) {
return complex(n, 0);
}