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

@@ -9,8 +9,8 @@ union Complex
Real[<2>] v;
}
const Complex IDENTITY = { 1, 0 };
const Complex IMAGINARY = { 0, 1 };
macro Complex Complex.add(self, Complex b) => Complex { .v = self.v + b.v };
macro Complex Complex.add_each(self, Real b) => Complex { .v = self.v + b };
macro Complex Complex.sub(self, Complex b) => Complex { .v = self.v - b.v };
@@ -22,3 +22,10 @@ macro Complex Complex.div(self, Complex b)
Real div = b.v.dot(b.v);
return Complex{ (self.r * b.r + self.c * b.c) / div, (self.c * b.r - self.r * b.c) / div };
}
macro Complex Complex.inverse(self)
{
Real sqr = self.v.dot(self.v);
return Complex{ self.r / sqr, -self.c / sqr };
}
macro Complex Complex.conjugate(self) => Complex { .r = self.r, .c = -self.c };
macro bool Complex.equals(self, Complex b) => self.v == b.v;