Hacker News new | ask | show | jobs
by drewm1980 2634 days ago
Me and the company I am at are on the fence with Rust because the type system still can't do Eigen but probably will eventually. I worry all of the current array libraries for Rust will go obsolete in a year or ten and Rust will only then start building up a convincing ecosystem for numerical computing.
2 comments

Yes, this was our main worry as well. At first we switched to Rust only for the lower level code, the parts that interfaces with sensors and the motors, that do the data logging, our inter process communication library, etc. The part with the control algorithms (where all the interesting linear algebra code is) was still in C++ until recently.

For all the small vectors and matrices, things like Vector3 and Matrix3x3 from nalgebra work fine. A few generic things we had to change from statically sized to dynamically sized arrays/matrices, but since these were rather big already, the difference in performance was neglegible. The type safety was maintained by directly wrapping those in a generic struct where the template parameter describes the contents of the matrix or vector. Such template parameter is basically a struct where the members describe the meaning of all the columns/rows of the related vector/matrix. A custom derive proc macro generated the conversions to and from this type (which are all optimized away by the compiler). It looks something like this:

    struct Foo<T: VectorLike> {
        matrix: DynanamicallyAllocatedMatrix<f64>,
    }

    impl<T: VectorLike> Foo<T> {
        fn new() {
            Foo { matrix: DynanamicallyAllocatedMatrix::zeros(T::N, T::N) }
        }
        fn get_diagonal(&self) -> T { ... }
        ...
    }
We were already doing something like this in C++, but with some hacky preprocessor macros instead of Rust's procedural macros.

In the end, keeping track of the meaning of the entire vector/matrix in the type system provides us with even a lot more safety than only keeping the size of them in the type system. And it has the advantage that you don't need const generics, since you're keeping track of whole types, and not just numbers. The downside is that we have to maintain a few procedural macro implementations.

Getting const generics finished up is on this year’s roadmap.
Cool! I knew it was being worked on; I look it up every few months but keep forgetting the name. RFC 2000 const generics. Any talk yet of a standard ndarray type once that lands? C++ doesn't have one and numeric library compatibility really suffers. Python has one (the buffer interface basically blessed numpy arrays). At our shop python is our glue language mainly because you can pass data between libraries and expect it to work and work efficiently (without unnecessary copies).
Yeah, it’s taken a lot of work to get it going, It we might even have an implementation soonish!

I don’t know how soon ndarray will adopt it. We’ll just have to see.