HILA
Loading...
Searching...
No Matches
cmplx.h File Reference

Definition of Complex types. More...

#include "plumbing/defs.h"
Include dependency graph for cmplx.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  Complex< T >
 Complex definition. More...
 
struct  hila::is_complex_or_arithmetic< T >
 hila::is_complex_or_arithmetic<T>::value More...
 
class  Imaginary_t< T >
 Imaginary type, used to represent purely imaginary numbers. More...
 

Namespaces

namespace  hila
 Implement hila::swap for gauge fields.
 

Functions

template<typename T >
real (const Complex< T > &a)
 Return real value of Complex number.
 
template<typename T >
imag (const Complex< T > &a)
 Retrun imaginary value of Complex number.
 
template<typename T >
Complex< T > polar (T r, T arg)
 Return complex number given by polar representation.
 
template<typename T1 , typename T2 , typename Tr = hila::type_plus<T1, T2>>
Complex< Tr > operator+ (const Complex< T1 > &a, const Complex< T2 > &b)
 Addition operator Complex + Complex.
 
template<typename T , typename A , std::enable_if_t< hila::is_arithmetic< A >::value, int > = 0>
auto operator+ (const Complex< T > &c, const A &a)
 Addition operator Complex + Scalar.
 
template<typename T , typename A , std::enable_if_t< hila::is_arithmetic< A >::value, int > = 0>
auto operator+ (const A &a, const Complex< T > &c)
 Addition operator Scalar + Complex.
 
template<typename T1 , typename T2 , typename Tr = hila::type_plus<T1, T2>>
Complex< Tr > operator- (const Complex< T1 > &a, const Complex< T2 > &b)
 Subtraction operator Complex - Complex.
 
template<typename T , typename A , std::enable_if_t< hila::is_arithmetic< A >::value, int > = 0>
auto operator- (const Complex< T > &c, const A &a)
 Subtraction operator Complex - Scalar.
 
template<typename T , typename A , std::enable_if_t< hila::is_arithmetic< A >::value, int > = 0>
auto operator- (const A &a, const Complex< T > &c)
 Subtraction operator Scalar - Complex.
 
template<typename T1 , typename T2 , typename Tr = hila::type_mul<T1, T2>>
Complex< Tr > operator* (const Complex< T1 > &a, const Complex< T2 > &b)
 Multiplication operator Complex * Complex.
 
template<typename T , typename A , std::enable_if_t< hila::is_arithmetic< A >::value, int > = 0>
auto operator* (const Complex< T > &c, const A &a)
 Multiplication operator Complex * Scalar This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
template<typename T , typename A , std::enable_if_t< hila::is_arithmetic< A >::value, int > = 0>
auto operator* (const A &a, const Complex< T > &c)
 Multiplication operator Scalar * Complex This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
template<typename T1 , typename T2 , typename Tr = hila::type_mul<T1, T2>>
Complex< Tr > operator/ (const Complex< T1 > &a, const Complex< T2 > &b)
 Division operator Complex / Complex.
 
template<typename T , typename A , std::enable_if_t< hila::is_arithmetic< A >::value, int > = 0>
auto operator/ (const Complex< T > &c, const A &a)
 Division operator Complex / Scalar.
 
template<typename T , typename A , std::enable_if_t< hila::is_arithmetic< A >::value, int > = 0>
auto operator/ (const A &a, const Complex< T > &c)
 Division operator Scalar / Complex.
 
template<typename T >
Complex< T > mul_add (const Complex< T > &a, const Complex< T > &b, const Complex< T > &c)
 Multiply add with Complex numbers.
 
template<typename A , typename B >
bool operator== (const Complex< A > &a, const Complex< B > &b)
 Compare equality of two complex numbers.
 
template<typename A , typename B , std::enable_if_t< hila::is_arithmetic< B >::value, int > = 0>
bool operator== (const Complex< A > &a, const B b)
 Compare equality of Complex and scalar.
 
template<typename A , typename B , std::enable_if_t< hila::is_arithmetic< A >::value, int > = 0>
bool operator== (const A a, const Complex< B > &b)
 Compare equality of Scalar and Complex.
 
template<typename A , typename B >
bool operator!= (const Complex< A > &a, const Complex< B > &b)
 Compare non-equality of two complex numbers.
 
template<typename A , typename B , std::enable_if_t< hila::is_arithmetic< B >::value, int > = 0>
bool operator!= (const Complex< A > &a, const B b)
 Compare non-equality of Complex number and Scalar.
 
template<typename A , typename B , std::enable_if_t< hila::is_arithmetic< A >::value, int > = 0>
bool operator!= (const A a, const Complex< B > &b)
 Compare non-equality of Scalar and Complex number.
 
template<typename T >
abs (const Complex< T > &a)
 Return absolute value of Complex number.
 
template<typename T >
arg (const Complex< T > &a)
 Return argument of Complex number.
 
template<typename T >
Complex< T > conj (const Complex< T > &val)
 Return conjugate of Complex number.
 
template<typename T >
Complex< T > dagger (const Complex< T > &val)
 Return dagger of Complex number.
 
template<typename T >
auto squarenorm (const Complex< T > &val)
 Return Squarenorm of Complex number.
 
template<typename T >
std::ostream & operator<< (std::ostream &strm, const Complex< T > &A)
 Print a complex value as (re,im)
 
template<typename T >
std::string hila::to_string (const Complex< T > &A, int prec=8, char separator=' ')
 Return Complex number as std::string.
 
template<typename T >
std::string hila::prettyprint (const Complex< T > &A, int prec=8)
 Return well formatted Complex number as std::string.
 
constexpr Imaginary_t< double > I (1.0)
 Imaginary unit I - global variable.
 
template<typename T >
Complex< T > exp (const Complex< T > z)
 \(\exp(z)\)
 
template<typename T , std::enable_if_t< hila::is_arithmetic< T >::value, int > = 0>
Complex< T > expi (T a)
 \(\exp(i\cdot x)\)
 
template<typename T >
Complex< T > log (Complex< T > z)
 \(\log{z}\)
 
template<typename T >
Complex< T > sqrt (Complex< T > z)
 \(\sqrt{z}\)
 
template<typename T >
Complex< T > cbrt (Complex< T > z)
 \(\sqrt[3]{z}\)
 
template<typename A , typename B >
auto pow (Complex< A > z, Complex< B > p)
 pow(z.p) = \(z^p\) = \(exp(p*log(z))\)
 
template<typename T >
Complex< T > sin (Complex< T > z)
 
template<typename T >
Complex< T > cos (Complex< T > z)
 
template<typename T >
Complex< T > tan (Complex< T > z)
 \(\tan(z) = \frac{\sin(z)}{\cos(z)}\) - rely on optimizer to simplify
 
template<typename T >
Complex< T > sinh (Complex< T > z)
 
template<typename T >
Complex< T > cosh (Complex< T > z)
 
template<typename T >
Complex< T > tanh (Complex< T > z)
 \(\tanh(z)\)
 
template<typename T >
Complex< T > atan (Complex< T > z)
 \(\arctan(z)\)
 
template<typename T >
Complex< T > asin (Complex< T > z)
 \(\arcsin(z)\)
 
template<typename T >
Complex< T > acos (Complex< T > z)
 \(\arccos(z)\)
 
template<typename T >
Complex< T > atanh (Complex< T > z)
 \(\text{artanh}(z)\)
 
template<typename T >
Complex< T > asinh (Complex< T > z)
 \(\text{arsinh}(z)\)
 
template<typename T >
Complex< T > acosh (Complex< T > z)
 \(\text{arcosh}(z)\)
 
constexpr Imaginary_t< double > operator""_i (long double a)
 

Detailed Description

Definition of Complex types.

This file contains definitions and methods for Complex numbers and Imaginary type.

‍NOTE: All overloads for operators +,-,/,* are not documented separately since there exists a function for each combinations of scalar,imaginary and complex number representations. All versions are documented in the ComplexComplex definitions.

Definition in file cmplx.h.

Function Documentation

◆ abs()

template<typename T >
T abs ( const Complex< T > &  a)
inline

Return absolute value of Complex number.

Wrapper around Complex::abs

Template Parameters
TArithmetic type of a
Parameters
aComplex number to get abs from
Returns
T

Definition at line 1187 of file cmplx.h.

◆ arg()

template<typename T >
T arg ( const Complex< T > &  a)
inline

Return argument of Complex number.

Wrapper around Complex::arg

Template Parameters
TArithmetic type of a
Parameters
aComplex number to get arg from
Returns
T

Definition at line 1199 of file cmplx.h.

◆ conj()

template<typename T >
Complex< T > conj ( const Complex< T > &  val)
inline

Return conjugate of Complex number.

Wrapper around Complex::conj

Template Parameters
TArithmetic type of a
Parameters
aComplex number to get conjugate from
Returns
Complex<T>

Definition at line 1211 of file cmplx.h.

◆ cos()

template<typename T >
Complex< T > cos ( Complex< T >  z)
inline

\begin{align}\cos(z) &= \cos(\Re{z})\cos(i \Im(z)) - \sin(\Re{z})\sin(i \Im{z}) \\ &= \cos(\Re{z})\cosh(\Im(z)) - i \sin(\Re{z})\sinh(\Im(z))\end{align}

Definition at line 1476 of file cmplx.h.

◆ cosh()

template<typename T >
Complex< T > cosh ( Complex< T >  z)
inline

cosh(z) = cosh(re)cosh(i im) - sinh(re)sinh(i im) = cosh(re)cos(im) - i sinh(re)sin(im)

Definition at line 1496 of file cmplx.h.

◆ dagger()

template<typename T >
Complex< T > dagger ( const Complex< T > &  val)
inline

Return dagger of Complex number.

Wrapper around Complex::conj

Template Parameters
TArithmetic type of a
Parameters
aComplex number to get conjugate from
Returns
Complex<T>

Definition at line 1223 of file cmplx.h.

◆ I()

constexpr Imaginary_t< double > I ( 1.  0)
constexpr

Imaginary unit I - global variable.

◆ imag()

template<typename T >
T imag ( const Complex< T > &  a)
inline

Retrun imaginary value of Complex number.

Wrapper around Complex::imag

Template Parameters
TArithmetic type of a
Parameters
aComplex number to get imaginary value from
Returns
T

Definition at line 803 of file cmplx.h.

◆ mul_add()

template<typename T >
Complex< T > mul_add ( const Complex< T > &  a,
const Complex< T > &  b,
const Complex< T > &  c 
)
inline

Multiply add with Complex numbers.

Defined as

mul_add(a,b,c) = a*b + c;
Template Parameters
TArithmetic type of a,b and c
Parameters
aComplex number to multiply
bComplex number to multiply
cComplex number to add to result of multiplication
Returns
Complex<T>

Definition at line 1049 of file cmplx.h.

◆ operator!=() [1/3]

template<typename A , typename B , std::enable_if_t< hila::is_arithmetic< A >::value, int > = 0>
bool operator!= ( const A  a,
const Complex< B > &  b 
)
inline

Compare non-equality of Scalar and Complex number.

Negation of operator==()

Template Parameters
AArithmetic type of a
BArithmetic type of b
Parameters
aScalar to compare
bComplex number to compare
Returns
true if values are not arithmetically equal

Definition at line 1158 of file cmplx.h.

◆ operator!=() [2/3]

template<typename A , typename B , std::enable_if_t< hila::is_arithmetic< B >::value, int > = 0>
bool operator!= ( const Complex< A > &  a,
const B  b 
)
inline

Compare non-equality of Complex number and Scalar.

Negation of operator==()

Template Parameters
AArithmetic type of a
BArithmetic type of b
Parameters
aComplex number to compare
bScalar to compare
Returns
true if values are not arithmetically equal

Definition at line 1142 of file cmplx.h.

◆ operator!=() [3/3]

template<typename A , typename B >
bool operator!= ( const Complex< A > &  a,
const Complex< B > &  b 
)
inline

Compare non-equality of two complex numbers.

Negation of operator==()

Template Parameters
AArithmetic type of a
BArithmetic type of b
Parameters
aComplex number to compare
bComplex number to compare
Returns
true if values are not arithmetically equal

Definition at line 1126 of file cmplx.h.

◆ operator""_i()

constexpr Imaginary_t< double > operator""_i ( long double  a)
constexpr

Operators to implement imaginary unit 1_i, enablig expressions 3 + 2_i etc. This is defined as an user-defined literal, which requires an underscore.

Definition at line 1548 of file cmplx.h.

◆ operator*() [1/3]

template<typename T , typename A , std::enable_if_t< hila::is_arithmetic< A >::value, int > = 0>
auto operator* ( const A &  a,
const Complex< T > &  c 
)
inline

Multiplication operator Scalar * Complex This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Multiplication between Complex and scalar is defined in the usual way

\begin{align}z &= x + iy \in \mathbb{C}, a \in \mathbb{R} \\ z * a &= (x\cdot a + iy\cdot a)\end{align}

Template Parameters
TArithmetic type of c
AArithmetic type of a
Parameters
cComplex number to multiply
aScalar to multiply
Returns
auto

Definition at line 967 of file cmplx.h.

◆ operator*() [2/3]

template<typename T , typename A , std::enable_if_t< hila::is_arithmetic< A >::value, int > = 0>
auto operator* ( const Complex< T > &  c,
const A &  a 
)
inline

Multiplication operator Complex * Scalar This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Multiplication between Complex and scalar is defined in the usual way

\begin{align}z &= x + iy \in \mathbb{C}, a \in \mathbb{R} \\ z * a &= (x\cdot a + iy\cdot a)\end{align}

Template Parameters
TArithmetic type of c
AArithmetic type of a
Parameters
cComplex number to multiply
aScalar to multiply
Returns
auto

Definition at line 950 of file cmplx.h.

◆ operator*() [3/3]

template<typename T1 , typename T2 , typename Tr = hila::type_mul<T1, T2>>
Complex< Tr > operator* ( const Complex< T1 > &  a,
const Complex< T2 > &  b 
)
inline

Multiplication operator Complex * Complex.

Defined in the usual way

\begin{align}z &= x + iy, w = x' + iy' \in \mathbb{C} \\ z w &= (x + iy)(x' + iy') = (xx'-yy') + i(xy' + yx')\end{align}

Template Parameters
T1Arithmetic type of a
T2Arithmetic type of b
TrResulting type after multiplication
Parameters
a
b
Returns
Complex<Tr>

Definition at line 933 of file cmplx.h.

◆ operator+() [1/3]

template<typename T , typename A , std::enable_if_t< hila::is_arithmetic< A >::value, int > = 0>
auto operator+ ( const A &  a,
const Complex< T > &  c 
)
inline

Addition operator Scalar + Complex.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Template Parameters
TArithmetic type of c
AArithmetic type of a
Parameters
aScalar to sum
cComplex number to sum
Returns
auto

Definition at line 870 of file cmplx.h.

◆ operator+() [2/3]

template<typename T , typename A , std::enable_if_t< hila::is_arithmetic< A >::value, int > = 0>
auto operator+ ( const Complex< T > &  c,
const A &  a 
)
inline

Addition operator Complex + Scalar.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Defined in the usual way

Template Parameters
TArithmetic type of c
AArithmetic type of a
Parameters
cComplex number to sum
aScalar to sum
Returns
auto

Definition at line 856 of file cmplx.h.

◆ operator+() [3/3]

template<typename T1 , typename T2 , typename Tr = hila::type_plus<T1, T2>>
Complex< Tr > operator+ ( const Complex< T1 > &  a,
const Complex< T2 > &  b 
)
inline

Addition operator Complex + Complex.

Addition between Complex numbers is defined in the usual way

Template Parameters
T1Arithmetic type of a
T2Arithmetic type of b
TrResulting type after addition
Parameters
a
b
Returns
Complex<Tr>

Definition at line 839 of file cmplx.h.

◆ operator-() [1/3]

template<typename T , typename A , std::enable_if_t< hila::is_arithmetic< A >::value, int > = 0>
auto operator- ( const A &  a,
const Complex< T > &  c 
)
inline

Subtraction operator Scalar - Complex.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Template Parameters
TArithmetic type of c
AArithmetic type of a
Parameters
aScalar to subtract
cComplex number to subtract
Returns
auto

Definition at line 914 of file cmplx.h.

◆ operator-() [2/3]

template<typename T , typename A , std::enable_if_t< hila::is_arithmetic< A >::value, int > = 0>
auto operator- ( const Complex< T > &  c,
const A &  a 
)
inline

Subtraction operator Complex - Scalar.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Defined in the usual way

Template Parameters
TArithmetic type of c
AArithmetic type of a
Parameters
cComplex number to subtract
aScalar to subtract
Returns
auto

Definition at line 900 of file cmplx.h.

◆ operator-() [3/3]

template<typename T1 , typename T2 , typename Tr = hila::type_plus<T1, T2>>
Complex< Tr > operator- ( const Complex< T1 > &  a,
const Complex< T2 > &  b 
)
inline

Subtraction operator Complex - Complex.

Subtraction between Complex numbers is defined in the usual way

Template Parameters
T1Arithmetic type of a
T2Arithmetic type of b
TrResulting type after subtraction
Parameters
a
b
Returns
Complex<Tr>

Definition at line 885 of file cmplx.h.

◆ operator/() [1/3]

template<typename T , typename A , std::enable_if_t< hila::is_arithmetic< A >::value, int > = 0>
auto operator/ ( const A &  a,
const Complex< T > &  c 
)
inline

Division operator Scalar / Complex.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Defined in the usual way

\begin{align} z=x + iy &\in \mathbb{C}, a \in \mathbb{R} \\ \frac{a}{z} &= \frac{az^*}{|z|^2} \end{align}

Template Parameters
TArithmetic type of c
AArithmetic type of a
Parameters
cComplex number to divide
aScalar to divide with
Returns
auto

Definition at line 1030 of file cmplx.h.

◆ operator/() [2/3]

template<typename T , typename A , std::enable_if_t< hila::is_arithmetic< A >::value, int > = 0>
auto operator/ ( const Complex< T > &  c,
const A &  a 
)
inline

Division operator Complex / Scalar.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Defined in the usual way

\begin{align} z=x + iy &\in \mathbb{C}, a \in \mathbb{R} \\ \frac{z}{a} &= \frac{x}{a} + i\cdot \frac{y}{a} \end{align}

Template Parameters
TArithmetic type of c
AArithmetic type of a
Parameters
cComplex number to divide
aScalar to divide with
Returns
auto

Definition at line 1011 of file cmplx.h.

◆ operator/() [3/3]

template<typename T1 , typename T2 , typename Tr = hila::type_mul<T1, T2>>
Complex< Tr > operator/ ( const Complex< T1 > &  a,
const Complex< T2 > &  b 
)
inline

Division operator Complex / Complex.

Defined in the usual way

\begin{align} a,b &\in \mathbb{C} \\ \frac{a}{b} &= \frac{a\cdot b^{*}}{|b|^2} \end{align}

Template Parameters
T1Arithmetic type of a
T2Arithmetic type of b
TrResulting type after division
Parameters
aComplex number to divide
bComplex number to divide with
Returns
Complex<Tr>

Definition at line 992 of file cmplx.h.

◆ operator<<()

template<typename T >
std::ostream & operator<< ( std::ostream &  strm,
const Complex< T > &  A 
)

Print a complex value as (re,im)

Stream operator

Print Complex number as "A.re A.im"

Template Parameters
TArithmetic type of A
Parameters
strm
AComplex number to print
Returns
std::ostream&

Definition at line 1253 of file cmplx.h.

◆ operator==() [1/3]

template<typename A , typename B , std::enable_if_t< hila::is_arithmetic< A >::value, int > = 0>
bool operator== ( const A  a,
const Complex< B > &  b 
)
inline

Compare equality of Scalar and Complex.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Two numbers are equal, if the arithmetic values are equal: thus, complex and real comparison (a + i b) == a is true if b == 0.

Template Parameters
AArithmetic type of a
BArithmetic type of b
Parameters
bComplex number to compare
aScalar to compare
Returns
true if values compare to equal

Definition at line 1110 of file cmplx.h.

◆ operator==() [2/3]

template<typename A , typename B , std::enable_if_t< hila::is_arithmetic< B >::value, int > = 0>
bool operator== ( const Complex< A > &  a,
const B  b 
)
inline

Compare equality of Complex and scalar.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Two numbers are equal, if the arithmetic values are equal: thus, complex and real comparison (a + i b) == a is true if b == 0.

Template Parameters
AArithmetic type of a
BArithmetic type of b
Parameters
aComplex number to compare
bScalar to compare
Returns
true if values compare to equal

Definition at line 1091 of file cmplx.h.

◆ operator==() [3/3]

template<typename A , typename B >
bool operator== ( const Complex< A > &  a,
const Complex< B > &  b 
)
inline

Compare equality of two complex numbers.

Two numbers are equal, if the real and imaginary components are respectively equal

Template Parameters
AArithmetic type of a
BArithmetic type of b
Parameters
aComplex number to compare
bComplex number to compare
Returns
true if values compare to equal

Definition at line 1072 of file cmplx.h.

◆ polar()

template<typename T >
Complex< T > polar ( r,
arg 
)

Return complex number given by polar representation.

Same as Complex::polar

Template Parameters
TArithmetic type r and arg
Parameters
rRadial component of Complex number
argArgument of Complex number
Returns
Complex<T>

Definition at line 816 of file cmplx.h.

◆ real()

template<typename T >
T real ( const Complex< T > &  a)
inline

Return real value of Complex number.

Wrapper around Complex::real

Template Parameters
TArithmetic type of a
Parameters
aComplex number to get real value from
Returns
T

Definition at line 791 of file cmplx.h.

◆ sin()

template<typename T >
Complex< T > sin ( Complex< T >  z)
inline

\begin{align}sin(z) &= \sin(\Re(z) + i \Im(z)) \\ &= \sin(\Re(z))\cos(i \Im(z)) + \cos(\Re(z))\sin(i \Im(z)) \\ &= \sin(\Re(z)) \cosh(\Im(z)) + i \cos(\Re(z)) \sinh(\Im(z)) \end{align}

Definition at line 1469 of file cmplx.h.

◆ sinh()

template<typename T >
Complex< T > sinh ( Complex< T >  z)
inline

sinh(z) = sinh(re)cosh(i im) + cosh(re)sinh(i im) = sinh(re)cos(im) + i cosh(re)sin(im)

Definition at line 1489 of file cmplx.h.

◆ squarenorm()

template<typename T >
auto squarenorm ( const Complex< T > &  val)
inline

Return Squarenorm of Complex number.

Wrapper around Complex::squarenorm

Template Parameters
TArithmetic type of val
Parameters
valComplex number to compute squarenorm of
Returns
T

Definition at line 1235 of file cmplx.h.