Skip to content

Commit 38ae167

Browse files
Sebastien Poncesponce
authored andcommitted
Made Complex.hpp up to date in python example
1 parent e8d69f4 commit 38ae167

File tree

2 files changed

+106
-65
lines changed

2 files changed

+106
-65
lines changed

code/python/Complex.hpp

Lines changed: 104 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,110 @@
1+
#include <ostream>
2+
#include <cmath>
3+
14
template <typename T=float>
25
class Complex_t {
36
public:
4-
Complex_t(T r, T i);
5-
Complex_t(T r);
6-
Complex_t();
7-
8-
T real() const {return m_r;};
9-
T imaginary() const {return m_i;};
10-
11-
Complex_t operator+(const Complex_t& other) const;
12-
Complex_t operator-(const Complex_t& other) const;
13-
Complex_t operator*(const Complex_t& other) const;
14-
Complex_t operator*(const T factor) const;
15-
Complex_t operator/(const T dividend) const;
16-
Complex_t& operator+=(const Complex_t& other);
17-
bool operator<(const Complex_t& a) const;
7+
Complex_t() = default;
8+
Complex_t(T r, T i) : m_r(r), m_i(i) {}
9+
10+
T real() const { return m_r; }
11+
T imaginary() const { return m_i; }
12+
13+
T norm_sqr() const {
14+
return m_r * m_r + m_i * m_i;
15+
}
16+
17+
T norm() const {
18+
return std::sqrt(norm_sqr());
19+
}
20+
21+
Complex_t& operator+=(const Complex_t& other) {
22+
m_r += other.m_r;
23+
m_i += other.m_i;
24+
return *this;
25+
}
26+
27+
Complex_t& operator-=(const Complex_t& other) {
28+
m_r -= other.m_r;
29+
m_i -= other.m_i;
30+
return *this;
31+
}
32+
33+
Complex_t& operator*=(const Complex_t& other) {
34+
const auto r = m_r * other.m_r - m_i * other.m_i;
35+
const auto i = m_r * other.m_i + m_i * other.m_r;
36+
m_r = r;
37+
m_i = i;
38+
return *this;
39+
}
40+
41+
Complex_t& operator*=(T factor) {
42+
m_r *= factor;
43+
m_i *= factor;
44+
return *this;
45+
}
46+
47+
Complex_t& operator/=(const Complex_t& other) {
48+
const T ns = other.norm_sqr();
49+
const auto r = (m_r * other.m_r + m_i * other.m_i) / ns;
50+
const auto i = (m_i * other.m_r - m_r * other.m_i) / ns;
51+
m_r = r;
52+
m_i = i;
53+
return *this;
54+
}
55+
56+
Complex_t& operator/=(T divisor) {
57+
m_r /= divisor;
58+
m_i /= divisor;
59+
return *this;
60+
}
61+
62+
friend Complex_t operator+(Complex_t a, const Complex_t& b) {
63+
return a += b;
64+
}
65+
66+
friend Complex_t operator-(Complex_t a, const Complex_t& b) {
67+
return a -= b;
68+
}
69+
70+
friend Complex_t operator*(Complex_t a, const Complex_t& b) {
71+
return a *= b;
72+
}
73+
74+
friend Complex_t operator*(Complex_t c, T factor) {
75+
return c *= factor;
76+
}
77+
78+
friend Complex_t operator*(T factor, Complex_t c) {
79+
return c *= factor;
80+
}
81+
82+
friend Complex_t operator/(Complex_t a, Complex_t b) {
83+
return a /= b;
84+
}
85+
86+
friend Complex_t operator/(Complex_t c, T divisor) {
87+
return c /= divisor;
88+
}
89+
90+
friend Complex_t operator/(T dividend, const Complex_t& c) {
91+
return Complex_t(dividend, 0) / c;
92+
}
93+
94+
friend bool operator==(const Complex_t& a, const Complex_t& b) {
95+
return a.m_r == b.m_r && a.m_i == b.m_i;
96+
}
97+
98+
friend bool operator<(const Complex_t& a, const Complex_t& b) {
99+
return a.norm_sqr() < b.norm_sqr();
100+
}
101+
102+
friend std::ostream& operator<<(std::ostream& os, const Complex_t<T>& c) {
103+
return os << "(" << c.real() << ", " << c.imaginary() << ")";
104+
}
105+
18106
private:
19-
T m_r, m_i;
107+
T m_r{}, m_i{};
20108
};
21109

22-
typedef Complex_t<> Complex;
23-
24-
template <typename T>
25-
Complex_t<T>::Complex_t() {}
26-
27-
template <typename T>
28-
Complex_t<T>::Complex_t(T r) : m_r(r), m_i(0) {}
29-
30-
template <typename T>
31-
Complex_t<T>::Complex_t(T r, T i) : m_r(r), m_i(i) {}
32-
33-
template <typename T>
34-
Complex_t<T> Complex_t<T>::operator+(const Complex_t<T>& other) const {
35-
return Complex_t(m_r + other.m_r, m_i + other.m_i);
36-
}
37-
38-
template <typename T>
39-
Complex_t<T> Complex_t<T>::operator-(const Complex_t<T>& other) const {
40-
return Complex_t(m_r - other.m_r, m_i - other.m_i);
41-
}
42-
43-
template <typename T>
44-
Complex_t<T> Complex_t<T>::operator*(const Complex_t<T>& other) const {
45-
return Complex_t(m_r*other.m_r - m_i*other.m_i,
46-
m_r*other.m_i + m_i*other.m_r);
47-
}
48-
49-
template <typename T>
50-
Complex_t<T> Complex_t<T>::operator*(const T factor) const {
51-
return Complex_t(m_r*factor, m_i*factor);
52-
}
53-
54-
template <typename T>
55-
Complex_t<T> Complex_t<T>::operator/(const T dividend) const {
56-
return Complex_t(m_r/dividend, m_i/dividend);
57-
}
58-
59-
template <typename T>
60-
Complex_t<T>& Complex_t<T>::operator+=(const Complex_t<T>& other) {
61-
m_r += other.m_r;
62-
m_i += other.m_i;
63-
return *this;
64-
}
65-
66-
template <typename T>
67-
bool Complex_t<T>::operator<(const Complex_t<T>& other) const {
68-
return (m_r*m_r+m_i*m_i) < (other.m_r*other.m_r+other.m_i*other.m_i);
69-
}
110+
using Complex = Complex_t<>;

code/python/mandel.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#include "mandel.hpp"
22

33
int mandel(const Complex &a) {
4-
Complex z = 0;
4+
Complex z{0, 0};
55
for (int n = 1; n < 100; n++) {
66
z = z*z + a;
7-
if (Complex(2) < z) {
7+
if (Complex{2, 0} < z) {
88
return n;
99
}
1010
}

0 commit comments

Comments
 (0)