math: update complex numbers

Add inverse, conjugate, and equals functions to the Complex numbers. Add
an IMAGINARY constant to represent the imaginary unit. Also, add unit
tests for different types.
This commit is contained in:
Koni Marti
2024-12-05 19:22:10 +01:00
committed by Christoffer Lerno
parent e67e9d3bbf
commit c5a727aa9b
2 changed files with 67 additions and 1 deletions

View File

@@ -0,0 +1,59 @@
module math_tests @test;
import math_tests::complex;
def ComplexDouble = ComplexType(<double>) @local;
def ComplexInt = ComplexType(<int>) @local;
module math_tests::complex(<ElementType>) @test;
import std::math;
def ComplexType = Complex(<ElementType>);
fn void! complex_mul_imaginary()
{
ComplexType i = complex::IMAGINARY(<ElementType>);
assert(i.mul(i).equals(ComplexType{-1, 0}));
assert(i.mul(i).mul(i).equals(ComplexType{0, -1}));
}
fn void! complex_add()
{
ComplexType a = {3, 4};
ComplexType b = {1, 2};
assert(a.add(b).equals(ComplexType{4, 6}));
assert(a.add_each(1).equals(ComplexType{4, 5}));
}
fn void! complex_sub()
{
ComplexType a = {3, 4};
ComplexType b = {1, 2};
assert(a.sub(b).equals(ComplexType{2, 2}));
assert(a.sub_each(1).equals(ComplexType{2, 3}));
}
fn void! complex_scale()
{
ComplexType a = {2, 1};
assert(a.scale(2).equals(ComplexType{4, 2}));
}
fn void! complex_conjugate()
{
ComplexType a = {3, 4};
assert(a.conjugate().equals(ComplexType{3, -4}));
}
fn void! complex_inverse() @if(types::is_float(ElementType))
{
ComplexType a = {3, 4};
assert(a.inverse().mul(a).equals(complex::IDENTITY(<ElementType>)));
}
fn void! complex_div() @if(types::is_float(ElementType))
{
ComplexType a = {2, 5};
ComplexType b = {4, -1};
assert(a.div(b).equals(ComplexType{3.0/17.0, 22.0/17.0}));
}