
Complex numbers in JavaScript - megalodon
https://beta.observablehq.com/@mateogianolio/complex-numbers-in-javascript
======
stared
Thinking for some time about refactoring Quantum Game
([https://github.com/stared/quantum-game](https://github.com/stared/quantum-
game)), including its complex tensor operation part (plus some "boring but
important" like jspm.io -> webpack, actually using Vue or React vs the no-
framework mess, etc). If anyone interested in collaboration, I am happy to
talk!

~~~
s-macke
You might as well use WebAssembly for the task. C and C++ have good native
complex number support.

~~~
gct
They don't really, they tried to glue IEEE semantics onto their complex
numbers, so they're largely broken in the modern era of auto-vectorizing
compilers.

~~~
s-macke
Interesting. Under what circumstances are they broken?. Inf, NaN?

~~~
gct
Here's an article on it that was posted awhile back:
[https://medium.com/@smcallis_71148/complex-arithmetic-is-
com...](https://medium.com/@smcallis_71148/complex-arithmetic-is-
complicated-873ec0c69fc5)

The gist is that you can get Nan+jNan for some operations where you wanted
"infinity", so to do things their way, you have to insert a NaN check after
multiplication for example. This isn't too bad by itself (it's just a bit
mask), but it completely borks the vectorizer.

------
compumike
I like how you moved to Complex16Array for reduced memory overhead and
improving CPU cache performance. I'd probably take a close look at your get
method's "return new Complex16(...)" call and see if those object
instantiations are consuming a lot of CPU time and memory churn on their own.

We use quite a bit of complex numbers in Javascript under the hood within
CircuitLab for doing frequency-domain circuit analysis of electronic circuits
like filters and amplifiers. In that mode, a circuit simulator is essentially
solving a system of complex-valued equations using a sparse matrix solver that
accepts complex numbers, so the complex arithmetic routines are some of the
potential performance hot spots. You'd want to unroll a lot of the
intermediate manipulation without creating a lot of new objects in the middle!
I've written a short complex numbers tutorial here:
[https://www.circuitlab.com/textbook/complex-
numbers/](https://www.circuitlab.com/textbook/complex-numbers/)

~~~
megalodon
Yes, I decided to add the instantiations for readability. It would be more
efficient in a production setting to do the operations directly on the raw
data.

Thank you for the link!

------
chrishenn
Cool!

I once wrote a much hackier version of complex arithmetic in JavaScript [0].
It was to support a visualization of the complex derivative [1].

I found it useful to think of a complex number as a point in R^2 that operates
under some different rules. I used Mathematica's `ComplexExpand` to translate
from traditional notation for complex numbers [2].

[0]: [https://github.com/chnn/multivariable-derivative-
viz/blob/ma...](https://github.com/chnn/multivariable-derivative-
viz/blob/master/app/utils/complex-numbers.js)

[1]:
[http://people.reed.edu/~ormsbyk/projectproject/assets/posts/...](http://people.reed.edu/~ormsbyk/projectproject/assets/posts/multivariable-
derivative/multivariable-derivative-viz/)

[2]:
[https://reference.wolfram.com/language/ref/ComplexExpand.htm...](https://reference.wolfram.com/language/ref/ComplexExpand.html)

------
javajosh
Not much to say about the complex implementation itself; seems pretty good,
and it's as 'bare-metal' as you can get in browser javascript. It's not a
great abstraction, and very little error checking.

However, I _really_ like this literate programming style. You've got live
editing, and legible output, in the form of little TeX blocks. It's a really
nice way to define, describe, and teach little libraries like this. Well done.

------
jwilk
Why Complex _16_? What does 16 mean here?

~~~
megalodon
16 bytes of memory for one complex number! I could've called it Complex128,
but that’s a mouthful.

