This site is version 0.0.1. Most of the code on this site is drafted by an agent and needs review. I apologize for the AI-speak, we are working on rewriting it!
viv-lang.org

Complex arrays & the FFT

Packed complex arrays, and a Fourier transform that works at every length and gives identical output in every engine. Run a cell to see its output beside the expected one.

A complex array

czeros allocates; indexing gives a complex number, and storing a real promotes it.

Viv [1]

A literal with a complex in it

One complex element makes the whole literal a packed complex array; the reals promote.

Viv [2]

Real, imaginary, magnitude

re, im and abs over a complex array give a REAL array of the same shape. abs is what you plot as a spectrum.

Viv [3]

A delta transforms to all ones

A unit impulse at index 0 transforms to all ones, which also confirms that fft does not scale its output.

Viv [4]

A pure tone shows up in two bins

A cosine at frequency 3 on 16 points puts N/2 = 8 into bins 3 and 13. The test checks that each of the other 14 bins is below 1e-12.

Viv [5]

ifft inverts fft

ifft carries the 1/N, so the round trip returns what you started with — the same convention as numpy and FFTW.

Viv [6]

Any length, not just powers of two

Seven is prime. Lengths that are not a power of two go through Bluestein's algorithm, so they cost N log N too and nothing about the call changes.

Viv [7]

A real array is not a complex one

fft refuses a real array and says what to do instead. The two are different types and never mix silently.

Viv [8]

expected error: `fft` needs a complex array…