6 #ifndef THEORETICA_COMPLEX_H
7 #define THEORETICA_COMPLEX_H
9 #ifndef THEORETICA_NO_PRINT
16 #include "../core/error.h"
17 #include "../core/real_analysis.h"
25 template<
typename Type = real>
44 :
a(real_part),
b(imag_part) {}
65 template<
typename T = Type>
74 inline Type
Re()
const {
98 inline Type
Im()
const {
129 return a *
a +
b *
b;
177 return (
a >= 0) ? ((Type) 0) : ((Type)
PI);
182 return (
b >= 0) ? ((Type)
PI / 2.0) : ((Type) -
PI / 2.0);
252 return (*
this =
operator*(z.
inverse()));
318 return (
a == z.
a) && (
b == z.
b);
324 return !(*
this == z);
373 #ifndef THEORETICA_NO_PRINT
378 std::stringstream res;
384 res << (
b >= 0 ?
" + " :
" - ");
Complex number in algebraic form .
Definition: complex.h:26
complex operator*(const complex &z) const
Multiply two complex numbers.
Definition: complex.h:215
complex & operator+=(const complex &z)
Add a complex number to this one.
Definition: complex.h:230
bool operator!=(const complex &z) const
Check whether two complex numbers are not the same.
Definition: complex.h:323
static complex rotor(Type rad)
Construct a complex number representing a rotation of <rad> radians in 2 dimensions.
Definition: complex.h:330
complex & operator=(const complex &z)
Assignment operator.
Definition: complex.h:57
Type & Re()
Get the real part of the complex number.
Definition: complex.h:80
complex & operator+=(Type k)
Add a real number to the complex number.
Definition: complex.h:287
friend Type Re(const complex &z)
Extract the real part of the complex number.
Definition: complex.h:86
std::string to_string() const
Convert the complex number to string representation.
Definition: complex.h:376
Type a
Real part.
Definition: complex.h:30
friend Type & Im(complex &z)
Extract the imaginary part of the complex number.
Definition: complex.h:116
complex operator-() const
Get the opposite of the complex number.
Definition: complex.h:203
complex(Type real_part, Type imag_part)
Construct a complex number from its real and complex parts.
Definition: complex.h:43
complex operator+(const complex &z) const
Add two complex numbers.
Definition: complex.h:197
complex & operator/=(Type k)
Divide the complex number by a real number.
Definition: complex.h:305
complex & operator/=(const complex &z)
Divide the complex number by another.
Definition: complex.h:251
bool operator==(const complex &z) const
Check whether two complex numbers are the same.
Definition: complex.h:317
Type sqr_norm() const
Compute the square norm of the complex number.
Definition: complex.h:128
complex()
Default constructor, initializes the number to zero.
Definition: complex.h:38
complex & operator-=(const complex &z)
Subtract a complex number from this one.
Definition: complex.h:236
friend Type Im(const complex &z)
Extract the imaginary part of the complex number.
Definition: complex.h:110
complex(const complex &z)
Copy constructor.
Definition: complex.h:53
complex operator+() const
Identity (for consistency)
Definition: complex.h:191
complex & operator*=(const complex &z)
Multiply the complex number by another.
Definition: complex.h:242
complex operator/(const complex &z) const
Divide two complex numbers.
Definition: complex.h:224
complex inverse() const
Compute the inverse of the complex number.
Definition: complex.h:140
Type Re() const
Get the real part of the complex number.
Definition: complex.h:74
complex operator*(Type k) const
Multiply the complex number by a real number.
Definition: complex.h:269
complex(Type real_part)
Construct a complex number from a real number, with zero imaginary part.
Definition: complex.h:49
friend Type & Re(complex &z)
Extract the real part of the complex number.
Definition: complex.h:92
Type b
Imaginary part.
Definition: complex.h:33
complex operator/(Type k) const
Divide the complex number by a real number.
Definition: complex.h:275
static complex i()
Imaginary unit.
Definition: complex.h:336
complex operator-(Type k) const
Subtract a real number from the complex number.
Definition: complex.h:263
Type arg() const
Get the argument of the complex number.
Definition: complex.h:173
Type norm() const
Compute the norm of the complex number.
Definition: complex.h:134
friend std::ostream & operator<<(std::ostream &out, const complex &obj)
Stream the complex number in string representation to an output stream (std::ostream)
Definition: complex.h:403
complex operator-(const complex &z) const
Subtract two complex numbers.
Definition: complex.h:209
complex & operator-=(Type k)
Subtract a real number from the complex number.
Definition: complex.h:293
complex operator+(Type k) const
Add a real number to the complex number.
Definition: complex.h:257
complex & invert()
Invert the complex number.
Definition: complex.h:154
Type & Im()
Get the imaginary part of the complex number.
Definition: complex.h:104
Type Im() const
Get the imaginary part of the complex number.
Definition: complex.h:98
complex & operator*=(Type k)
Multiply the complex number by a real number.
Definition: complex.h:299
complex & operator=(const std::array< T, 2 > &v)
Assignment operator from a 2D array.
Definition: complex.h:66
complex conjugate() const
Compute the conjugate of the complex number.
Definition: complex.h:122
#define TH_MATH_ERROR(F_NAME, VALUE, EXCEPTION)
TH_MATH_ERROR is a macro which throws exceptions or modifies errno (depending on which compiling opti...
Definition: error.h:219
std::string string(size_t length)
Generate a random string made of human-readable ASCII characters.
Definition: random.h:102
Main namespace of the library which contains all functions and objects.
Definition: algebra.h:27
double real
A real number, defined as a floating point type.
Definition: constants.h:188
dual2 sqrt(dual2 x)
Compute the square root of a second order dual number.
Definition: dual2_functions.h:54
dual2 abs(dual2 x)
Compute the absolute value of a second order dual number.
Definition: dual2_functions.h:183
real atan2(real y, real x)
Compute the 2 argument arctangent.
Definition: real_analysis.h:1099
dual2 cos(dual2 x)
Compute the cosine of a second order dual number.
Definition: dual2_functions.h:82
constexpr real MACH_EPSILON
Machine epsilon for the real type.
Definition: constants.h:197
dual2 sin(dual2 x)
Compute the sine of a second order dual number.
Definition: dual2_functions.h:70
constexpr real PI
The Pi mathematical constant.
Definition: constants.h:206
real nan()
Return a quiet NaN number in floating point representation.
Definition: error.h:54