Template Class Complex#

Class Documentation#

template<typename T = double>
class Complex#

A class representing a complex number of the form \(a + bi\), where \(a\) and \(b\) are real numbers.

This class represents a complex number of the form \(a + bi\), where \(a\) and \(b\) are real numbers. The class is templated, allowing the user to specify the type of the real and imaginary components. The default type is double.

Template Parameters

T – The type of the real and imaginary components

Public Types

using Scalar = typename typetraits::TypeInfo<T>::Scalar#

Public Functions

inline Complex()#

Default constructor.

Create a new complex number. Both the real and imaginary components are set to zero

template<typename R>
inline explicit Complex(const R &realVal)#

Construct a complex number from a real number.

Create a complex number, setting only the real component. The imaginary component is initialized to zero

Template Parameters

R – The type of the real component

Parameters

realVal – The real component

template<typename R, typename I>
inline Complex(const R &realVal, const I &imagVal)#

Construct a complex number from real and imaginary components.

Create a new complex number where both the real and imaginary parts are set from the passed parameters

Template Parameters
  • R – The type of the real component

  • I – The type of the imaginary component

Parameters
  • realVal – The real component

  • imagVal – The imaginary component

inline Complex(const Complex<T> &other)#

Complex number copy constructor.

Parameters

other – The complex number to copy

inline Complex(Complex<T> &&other) noexcept#

Complex number move constructor.

Parameters

other – The complex number to move

template<typename Other>
inline Complex(const Complex<Other> &other)#

Construct a complex number from another complex number with a different type.

Template Parameters

Other – Type of the components of the other complex number

Parameters

other – The complex number to copy

inline explicit Complex(const std::complex<T> &other)#

Construct a complex number from a std::complex.

Parameters

other – The std::complex value to copy

inline auto operator=(const Complex<T> &other) -> Complex<T>&#

Complex number assignment operator.

Parameters

other – The value to assign

Returns

*this

inline void real(const T &val)#

Assign to the real component.

Set the real component of this complex number to val

Parameters

val – The value to assign

inline void imag(const T &val)#

Assign to the imaginary component.

Set the imaginary component of this complex number to val

Parameters

val – The value to assign

inline auto real() const -> const T&#

Access the real component.

Returns a const reference to the real component of this complex number

Returns

Real component

inline auto imag() const -> const T&#

Access the imaginary component.

Returns a const reference to the imaginary component of this complex number

Returns

Imaginary component

inline auto real() -> T&#

Access the real component.

Returns a reference to the real component of this complex number. Since this is a reference type, it can be assigned to

Returns

Real component

inline auto imag() -> T&#

Access the imaginary component.

Returns a reference to the imaginary component of this complex number. Since this is a reference type, it can be assigned to

Returns

imaginary component

inline auto operator=(const T &other) -> Complex&#

Complex number assigment operator.

Set the real component of this complex number to other, and the imaginary component to 0

Parameters

other

Returns

*this

template<typename Other>
inline auto operator=(const Complex<Other> &other) -> Complex&#

Complex number assigment operator.

Assign another complex number to this one, copying the real and imaginary components

Template Parameters

Other – The type of the other complex number

Parameters

otherComplex number to assign

Returns

*this

inline auto operator+=(const T &other) -> Complex&#

Inplace addition.

Add a scalar value to the real component of this imaginary number

Parameters

other – Scalar value to add

Returns

*this

inline auto operator-=(const T &other) -> Complex&#

Inplace subtraction.

Subtract a scalar value from the real component of this imaginary number

Parameters

other – Scalar value to subtract

Returns

*this

inline auto operator*=(const T &other) -> Complex&#

Inplace multiplication.

Multiply both the real and imaginary components of this complex number by a scalar

Parameters

other – Scalar value to multiply by

Returns

*this

inline auto operator/=(const T &other) -> Complex&#

Inplace division.

Divide both the real and imaginary components of this complex number by a scalar

Parameters

other – Scalar value to divide by

Returns

*this

inline auto operator+=(const Complex &other) -> Complex&#

Inplace addition.

Add a complex number to this one

Parameters

otherComplex number to add

Returns

*this

inline auto operator-=(const Complex &other) -> Complex&#

Inplace subtraction.

Subtract a complex number from this one

Parameters

otherComplex number to subtract

Returns

*this

inline auto operator*=(const Complex &other) -> Complex&#

Inplace multiplication.

Multiply this complex number by another one

Parameters

otherComplex number to multiply by

Returns

*this

inline auto operator/=(const Complex &other) -> Complex&#

Inplace division.

Divide this complex number by another one

Parameters

otherComplex number to divide by

Returns

*this

template<typename To>
inline explicit operator To() const#

Cast to scalar types.

Cast this complex number to a scalar type. This will extract only the real component.

Template Parameters

To – Type to cast to

Returns

Scalar

template<typename To>
inline explicit operator Complex<To>() const#

Cast to a complex number with a different scalar type.

Cast the real and imaginary components of this complex number to a different type and return the result as a new complex number

Template Parameters

To – Scalar type to cast to

Returns

Complex number

template<typename T_, typename Char, typename Ctx>
inline void str(const fmt::formatter<T_, Char> &format, Ctx &ctx) const#

Public Static Functions

static inline constexpr auto size() -> size_t#

Protected Functions

template<typename Other>
inline void _add(const Complex<Other> &other)#

Add a complex number to this one.

Template Parameters

Other – Scalar type of the other complex number

Parameters

other – Other complex number

template<typename Other>
inline void _sub(const Complex<Other> &other)#

Subtract a complex number from this one.

Template Parameters

Other – Scalar type of the other complex number

Parameters

other – Other complex number

template<typename Other>
inline void _mul(const Complex<Other> &other)#

Multiply this complex number by another one.

Template Parameters

Other – Scalar type of the other complex number

Parameters

other – Other complex number

template<typename Other>
inline void _div(const Complex<Other> &other)#

Divide this complex number by another one.

Template Parameters

Other – Scalar type of the other complex number

Parameters

other – Other complex number