commit eaa50974f3cebeec8fc2b22210bf19c42c834844
parent 7f4caff66655a560108bbf86c7e05a51cd08a67f
Author: Sebastian <sebastian@sebsite.pw>
Date: Sun, 5 Jun 2022 00:19:52 -0400
math::complex: add some c64 functions
Signed-off-by: Sebastian <sebastian@sebsite.pw>
Diffstat:
1 file changed, 28 insertions(+), 0 deletions(-)
diff --git a/math/complex/complex.ha b/math/complex/complex.ha
@@ -73,17 +73,36 @@ export fn c64to128(z: c64) c128 = (z.0: f64, z.1: f64);
// Truncates a [[c128]] to a [[c64]]. Precision may be lost.
export fn c128to64(z: c128) c64 = (z.0: f32, z.1: f32);
+// Adds two complex numbers
+export fn addc64(a: c64, b: c64) c64 = (a.0 + b.0, a.1 + b.1);
+
// Adds two complex numbers.
export fn addc128(a: c128, b: c128) c128 = (a.0 + b.0, a.1 + b.1);
// Subtracts two complex numbers.
+export fn subc64(a: c64, b: c64) c64 = (a.0 - b.0, a.1 - b.1);
+
+// Subtracts two complex numbers.
export fn subc128(a: c128, b: c128) c128 = (a.0 - b.0, a.1 - b.1);
// Multiplies two complex numbers.
+export fn mulc64(a: c64, b: c64) c64 =
+ (a.0 * b.0 - a.1 * b.1, a.1 * b.0 + a.0 * b.1);
+
+// Multiplies two complex numbers.
export fn mulc128(a: c128, b: c128) c128 =
(a.0 * b.0 - a.1 * b.1, a.1 * b.0 + a.0 * b.1);
// Divides two complex numbers.
+export fn divc64(a: c64, b: c64) c64 = {
+ const denom = b.0 * b.0 + b.1 * b.1;
+ return (
+ (a.0 * b.0 + a.1 * b.1) / denom,
+ (a.1 * b.0 - a.0 * b.1) / denom,
+ );
+};
+
+// Divides two complex numbers.
export fn divc128(a: c128, b: c128) c128 = {
const denom = b.0 * b.0 + b.1 * b.1;
return (
@@ -142,6 +161,9 @@ export fn isnan(z: c128) bool =
export fn logc128(z: c128) c128 = (math::logf64(absc128(z)), argc128(z));
// Negates z.
+export fn negc64(z: c64) c64 = (-z.0, -z.1);
+
+// Negates z.
export fn negc128(z: c128) c128 = (-z.0, -z.1);
// Creates a new [[c128]] from the polar coordinates (r, theta).
@@ -182,6 +204,12 @@ export fn powc128(a: c128, b: c128) c128 = {
// Projects z onto the surface of a Riemann Sphere. If z is finite, it projects
// to itself. If z is infinite, it projects to positive infinity on the real
// axis.
+export fn projc64(z: c64) c64 =
+ if (!isinf(c64to128(z))) z else (math::INF, math::copysignf32(0f32, z.1));
+
+// Projects z onto the surface of a Riemann Sphere. If z is finite, it projects
+// to itself. If z is infinite, it projects to positive infinity on the real
+// axis.
export fn projc128(z: c128) c128 =
if (!isinf(z)) z else (math::INF, math::copysignf64(0f64, z.1));