r/rust•
YatoRust

const-alg and array-vec - a safe const generics implementation of N-D linear algebra

This library is very similar to aljabar (in fact it was inspired by it). It improves upon it by giving panic safety and elementwise access (edit: latest update of aljabar does have these properties, but it still leaks on panics, whereas const-alg does not leak on panics). Also it gives iterators over rows and columns!

This is backed by my second crate, array-vec, which is a minimal fixed capacity vector backed by an array.

https://crates.io/crates/const-alg

https://crates.io/crates/array-vec

You can create a matrix very simply, just do

use const_alg::Matrix;

Matrix(
    [[0, 1, 2],
     [3, 4, 5]]
)

and that will create a new 2x3 matrix!

I also have element access, iterators over both rows and columns of the matrix, as well as some functionality to do global operations on the matrix, like mapping to another matrix and combining two matricies together!

Posted Jun 29, 2019, 5:24 AM UTC · edited

208
GuamPirate•

Cool! Aljabar actually has element wise access by virtue of Vectors derefing their arrays. It also took me a bit but I managed to change the implementation to use MaybeUninit, so it should be panic safe now

6
YatoRustOP•

Cool, last time I checked I didn't see that, but it's nice that it's there now!

1
YatoRustOP•

I looked through your code again, and wow, much better than when I first saw it. One small thing, you will still leak memory on panics, but I don't think that matters much because mostly matrices deal with Copy types

1
matthieum•

I love all these new experiments around const generics; it takes time and practice to refine an API, so thanks to all the trailblazers for spending their valuable time on this.

5
YatoRustOP•

Thank you

2
cbarrick•

This is cool.

I've been working on a (toy for now) tensor library, and I considered const generics. It seems obvious at first that const generics should work well with linear algebra.

But then I realized how limited and bloated it would turn out. For the former, as soon as you start loading data from disk, you're going to need dynamic shapes. For the latter, it doesn't make sense to monomorphize every matrix routine over every shape you use in a program. (Also, BLAS and LAPACK already use dynamic shapes.) I'm now firmly in the dynamic shape camp.

1
GuamPirate•

I think it depends on what your use case is. Fixed size vectors/matrices are a must for video game programming.

5
cbarrick•

That makes a lot of sense. My use case is scientific comp, and I hadn't thought about it that way.

2