Skip to content

Commit

Permalink
Merge pull request #642 from Marcos-cat/unity
Browse files Browse the repository at this point in the history
experimental negate subscripts as roots of unity
  • Loading branch information
kaikalii authored Dec 31, 2024
2 parents a7e5534 + 62fb0f8 commit 7f140b7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/compile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2055,6 +2055,24 @@ code:
.map(|_| self.primitive(prim, span.clone()))
.collect()
}
Primitive::Neg => {
self.subscript_experimental(prim, &span);
if n == 0 {
self.add_error(span.clone(), "Cannot have the 0th root of unity");
}
// Ensure that common cases are exact
let root_of_unity = match n {
1 | -1 => crate::Complex::ONE,
2 | -2 => -crate::Complex::ONE,
4 => crate::Complex::I,
-4 => -crate::Complex::I,
_ => (crate::Complex::I * (std::f64::consts::TAU / n as f64)).exp(),
};
Node::from_iter([
Node::new_push(root_of_unity),
self.primitive(Primitive::Mul, span),
])
}
Primitive::Sqrt => {
if n == 0 {
self.add_error(span.clone(), "Cannot take 0th root");
Expand Down
7 changes: 7 additions & 0 deletions tests/optimized.ua
Original file line number Diff line number Diff line change
Expand Up @@ -252,3 +252,10 @@ F ← ⬚10(/+◌1⊞(ׯ))
⍤⤙≍ ⊃⧅≥⧅(∘≥) 4 ⇡3
⍤⤙≍ ⊃⧅≠⧅(∘≠) ¯1 ⇡4
⍤⤙≍ ⊃⧅<⧅(∘<) ¯1 ⇡4

# Experimental!
⍤⤙≍ ℂ5 0 ¯₄ 5
⍤⤙≍ ℂ¯π 0 °¯₄ π
⍤⤙≍ ℂ0 5 ⁅₁₀ ⍥₃¯₃ 5
⍤⤙≍ ℂ0 5 ⁅₁₀ ⍥₃¯₋₃ 5
⍤⤙≍ ℂ0 5 ⁅₁₀ ⍥₇¯₇ 5

0 comments on commit 7f140b7

Please sign in to comment.