Skip to main content

Numeric Types

All numeric types in Mathematics.NET implement the IComplex interface. Unlike INumberBase, this interface defines the method Conjugate, which makes operations that accept either complex or real numbers as parameters more robust.

Complex Numbers

Because the complex number type in this library shares the name with .NET's own implementation, use the following statement to avoid conflicts:

using Complex = Mathematics.NET.Core.Complex;

With that, to create complex numbers, we can simply write

Complex z = new(3, 4); // 3+i4
Complex w = 2 // 2

To get the real or imaginary part of a complex number, we can use the properties Re and Im, respectively.

Important Methods

The following are some important methods related to complex numbers. Note that this method works for real and rational numbers as well.

Conjugate

As mentioned before, one important method is Conjugate.

z.Conjugate(); // 3-i4

Magnitude

The magnitude of a complex number is defined as z=x2+y2|z| = \sqrt{x^2+y^2}, where z=x+iyz=x+iy. This is implemented as the property, Magnitude.

z.Magnitude; // 5

Phase

The phase of a complex number is defined as argz=arctany/x\arg{z} = \arctan{y/x}, where z=x+iyz=x+iy. This is implemented as the property, Phase.

z.Phase; // 0.9272952180016122

Real Numbers

Real numbers are represented by the real type and function as expected. This type implements the IReal interface, which inherits from IComplex, and contains additional methods such as Max and Min. To create one, simply write

Real x = 1;

Rational Numbers

Rational numbers are represented by the rational type. This type implements the IRational interface, which inherits from IReal, and contains addition mathods such as Reciprocate and GCD.

To create rational numbers, we can write

Rational<int> p = new(3, 5); // 3/5
Rational<int> q = 2; // 2

Rationals accept any backing type that implements the IBinaryInteger and ISignedNumber interfaces. This includes BigInteger, which should be used carefully due to its performance implications.

To get the numerator and denominator of a rational number, we can use the properties Num and Den, respectively.

Important Methods

These methods are specific to rational numbers.

Reduce

The Reduce method simplifies rational numbers by dividing the numerator and denominator by their greatest common divisor.

Rational<int> p = new(6, 9);
Rational<int>.Reduce(p); // 2/3
note

Operations involving rational numbers automatically reduce their results.

Reciprocate

The Reciprocate method returns the reciprocal of a rational number.

Rational<int> p = new(3, 5);
Rational<int>.Reciprocate(p); // 5/3

Fields

For rational numbers in Mathematics.NET, Nan, PositiveInfinity, and NegativeInfinity are represented in the following ways:

Rational<int>.Nan; // 0/0
Rational<int>.PositiveInfinity; // 1/0
Rational<int>.NegativeInfinity; // -1/0