Complex Manual

About

maxon::Complex is a MAXON API class used to represent complex numbers and deliver standard mathematical operations. A complex number is a number that can be expressed in the form a + bi, where a and b are real numbers, and i is a solution of the equation x^2 = -1, which is called an imaginary number because there is no real number that satisfies this equation. For the complex number a + bi, a is called the real part, and b is called the imaginary part. Usually complex numbers are represented in a 2D plane known as "complex plane" or "Argand plane" with the abscissa representing the real part and the ordinate representing the imaginary part.

Creation and initialization

A maxon::Complex instance can be created on the stack and initialized through the proper method using a numerical data type as template:

// This example allocates a few maxon::Complex objects showing the different allocation methods.
// just allocate an zero-valued complex object
const maxon::Complex<maxon::Float> zeroComplex;
// just allocate a complex object defining the real part
const maxon::Complex<maxon::Float> realOnlyComplex(1.4);
// just allocate a complex object defining both real and imaginary part
const maxon::Complex<maxon::Float> fullComplex(1.4, 4);
Definition: complex.h:25

Standard operations

The maxon::Complex class delivers a set of standard mathematical operators to perform calculation with complex numbers. The operators delivered are:

// This example shows how to perform simple mathematical operations.
// just allocate a complex object defining the real part
const maxon::Complex<maxon::Float> complexA(2);
// just allocate a complex object defining both real and imaginery part
// sum A to B
complexB += complexA;
// allocate a new complex number and assign the difference between B and A
const maxon::Complex<maxon::Float> complexC = complexB - complexA;
// allocate a new complex number and assign the product between A and C
const maxon::Complex<maxon::Float> complexD = complexA * complexC;
// scale the complexB by a given amount
complexB = complexB * 10;

Get and Set operations

The maxon::Complex class is provided with a number of get methods to retrieve useful data from a maxon::Complex instance:

// This example shows how to use the "get" methods provided with maxon::Complex.
// just allocate a complex object defining the real part
const maxon::Complex<maxon::Float> complexA(2, 2);
// get the length of the vector representing the complex number on the Argand plane
const maxon::Float lengthValue = complexA.GetLength();
// get the angle (in radians) of the vector representing the complex number on the Argand plane
const maxon::Float angleValue = complexA.GetPhase();
// get the squared length of the vector representing the complex number on the Argand plane
const maxon::Float squaredlengthValue = complexA.GetSquaredLength();
// get the normalized vector representing the complex number on the Argand plane
const maxon::Complex<maxon::Float> normalized = complexA.GetNormalized();
// get the complex conjugate of the vector representing the complex number on the Argand plane
const maxon::Complex<maxon::Float> conjugate = complexA.GetConjugate();
// get the sqrt of the complex number
const maxon::Complex<maxon::Float> sqrt = complexA.GetSqrt();
// get the natural log of the complex number
// get the division of the complex number by the passed value
// NOTE: the result is stored in the calling instance
const maxon::Complex<maxon::Float> divisor(2, 4);
complexB.GetDivision(divisor) iferr_return;
Complex GetNormalized() const
Definition: complex.h:299
Complex GetSqrt() const
Definition: complex.h:312
Complex GetConjugate() const
Definition: complex.h:307
Result< Complex > GetLog() const
Definition: complex.h:323
Float64 Float
Definition: apibase.h:197
#define iferr_return
Definition: resultbase.h:1519

The maxon::Complex class is provided with a number of "set" methods to define a maxon::Complex instance:

// This example shows how to use the "set" methods provided with maxon::Complex.
// just allocate a few zero-valued complex objects
// set the length of the vector representing the complex number on the Argand plane
complexA.SetLength(3);
// set the angle of the vector representing the complex number on the Argand plane
complexA.SetPhase(-PI05);
// set the length and angle of the vector representing the complex number on the Argand plane
complexB.SetPolar(3, PI05);
// set a complex number according to e^(i*x) where x is the passed value
complexC.SetExp(5);
void SetPolar(const T len, const T phase)
Initialize Complex number by given polar coordinates.
Definition: complex.h:272
void SetExp(const T x)
set Complex number according to e^(i*x)
Definition: complex.h:278
void SetLength(const T len)
Set polar length (magnitude) in the Argand plane.
Definition: complex.h:260
void SetPhase(const T phase)
Set polar phase (angle) in the Argand plane.
Definition: complex.h:267
static constexpr Float64 PI05
floating point constant: 0.5 * PI
Definition: apibasemath.h:124

Conversion

A maxon::Complex instance can be converted to maxon::String using:

// This example shows how to convert a maxon::Complex to maxon::String.
// just allocate a complex object
const maxon::Complex<maxon::Float> complexA(1.45, -0.4);
const maxon::String stringComplex = complexA.ToString(nullptr);
DiagnosticOutput(diagIDString + "complexA: @", complexA);
DiagnosticOutput(diagIDString + "complexA.ToString(): @", stringComplex);
String ToString(const FormatStatement *formatStatement=nullptr) const
Definition: complex.h:358
Definition: string.h:1235
#define DiagnosticOutput(formatString,...)
Definition: debugdiagnostics.h:176

Utilities

A maxon::Complex instance can be read from and written to disk by serializing the data contained using the conventional functions.

// This example shows how to store and retrieve a maxon::Complex from a file.
const maxon::Complex<maxon::Float> savedComplex(-1, 3);
// file URL
const maxon::Url url = (targetFolder + "complex.txt"_s)iferr_return;
const maxon::Id fileID("net.maxonexample.complex");
// save to file
// read from file
maxon::ReadDocument(url, fileID, loadedComplex) iferr_return;
Definition: apibaseid.h:253
Definition: url.h:952
std::enable_if< GetCollectionKind< T >::value==COLLECTION_KIND::ARRAY, Result< void > >::type ReadDocument(const Url &url, const Id &id, T &object, const DataDictionary &dict=DataDictionary())
Definition: io.h:35
Result< void > WriteDocument(const Url &url, OPENSTREAMFLAGS flags, const Id &id, const T &object, IOFORMAT format=IOFORMAT::DEFAULT, const DataDictionary &dict=DataDictionary())
Definition: io.h:67
@ NONE
No flags set.

Further Reading