Math/Stats library Numeric ported to Matrex

MatrexNumerix - Numeric stats library ported to (fast) nif based matrix library Matrex for fast stats and data-science.

I ported the Numerix math and statistics library to use the Matrex library. A couple of convenience functions have been added to Matrex, primarily Matrex.from_list/1 which produces a “vector” (just 1 row matrex with function guards). These can then be properly dotted as vectors by using the Matrex.inner_dot/2 function. This enables most “vector” based numerical algorithms to be encoded without getting transposes mixed up.

On the Numerix side the port contains almost all of the features of the original library. The test framework has been switched to plain exunit.

A new feature that’s been added is a 1d fast Fourier transform (FFT) which produces results comparable to NumPy’s fft module. It supports complex numbers by using a 2xN matrix size (e.g. real and imaginary components are stored in separate rows side by side). It’d be possible to generalize it to 2d (or more) for image FFT’s, if anyone’s interested.

One last note is that a polynomial fitting function has been added to Matrex.Algorithms.fit_poly/3 which supports fitting polynomial coefficients for a given x and y vector using gradient descent. Sometimes this produces better results than least squares.

8 Likes

Is Matrex the go to math and statistics library for Elixir? I could not find a way to use FFT as mentioned in this post. Any guidance on how to use FFT with Matrex?

Matrex is mostly for linear algebra and such. Numerix is probably the closest to a “math and statistics” Elixir has currently (that I know of). So I’d ported Numerix to Matrex, and added a few extras (like the FFT).

See matrex_numerix/test/fft_test.exs at master · elcritch/matrex_numerix · GitHub for how to use it. It should be back-portable to Numerix or Nx. Note it’s probably not fast.

Note, the to_freq_and_amplitude is important to put the data in a recognizable frequency power form.

MatrexNumerix currently uses a forked version of Matrex that supports vectors (aka rank 1 tensors).

The FFT in theory should be portable to Nx as well.

1 Like

Much appreciate the response. That helps.