From 7903a4611830b252b87b23e367374b6939bd1ec2 Mon Sep 17 00:00:00 2001 From: Glen Whitney Date: Thu, 24 Apr 2025 20:52:43 -0700 Subject: [PATCH] feat: add cis and fix indistinguishable --- src/complex/__test__/type.spec.js | 4 ++++ src/complex/relational.js | 4 ++-- src/complex/type.js | 5 ++++- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/complex/__test__/type.spec.js b/src/complex/__test__/type.spec.js index b85797f..6ea0875 100644 --- a/src/complex/__test__/type.spec.js +++ b/src/complex/__test__/type.spec.js @@ -32,4 +32,8 @@ describe('complex type operations', () => { assert(!assoc(b, cplx(false, true))) assert(!assoc(cplx(0, 1), b)) }) + it('computes cis of an angle', () => { + assert(math.equal(math.cis(0), 1)) + assert(math.equal(math.cis(Math.PI/3), cplx(0.5, Math.sqrt(3)/2))) + }) }) diff --git a/src/complex/relational.js b/src/complex/relational.js index 4d6a8af..a8c58f3 100644 --- a/src/complex/relational.js +++ b/src/complex/relational.js @@ -13,11 +13,11 @@ export const indistinguishable = match( return Returns( BooleanT, (w, z) => same(w.re, z.re) && same(w.im, z.im)) } - const [RT, AT] = T + const [[RT, AT]] = T const same = math.indistinguishable.resolve([WComp, ZComp, RT, AT]) return Returns( BooleanT, - (w, z, [rT, aT]) => { + (w, z, [[rT, aT]]) => { return same(w.re, z.re, rT, aT) && same(w.im, z.im, rT, aT) }) }) diff --git a/src/complex/type.js b/src/complex/type.js index fbe7d8e..e83aabb 100644 --- a/src/complex/type.js +++ b/src/complex/type.js @@ -49,4 +49,7 @@ export const associate = match([Complex, Complex], (math, [W, Z]) => { const iz = mult(iZ, z) return eqM(w, iz) || eqNM(w, negM(iz)) }) -}) +}) + +export const cis = match(NumberT, Returns(Complex(NumberT), t => ({ + re: Math.cos(t), im: Math.sin(t)})))