Nice example of a shape-safe linear algebra API in...
# mathematics
b
Nice example of a shape-safe linear algebra API in Rust: https://www.nalgebra.org/vectors_and_matrices/
a
Well, it is possible to do, and even with almost zero overhead. If you really want, I can create a PoC using kmath structures in a separate module. I am still not convinced that this feature is really needed, but it could be done without breaking anything.
b
I really want this feature... there's a toy implementation I've been playing around with here: https://github.com/breandan/kotlingrad/blob/master/src/main/kotlin/edu/umontreal/kotlingrad/dependent/MatDemo.kt The idea is that if the user provide a type, it should be easy to check/infer the result type of most matrix operations at compile time. And it's completely optional, so if it's the shape is not specified, then it defaults to dynamic construction with a runtime exception on incompatible shapes.
It would be nice if Kotlin had real type level integers, but they can be enumerated up to a fixed constant like this does: https://github.com/paholg/typenum
a
OK, I have a bit of free time today, I will try to draw a PoC and get back to you. The type-safe functionality will be added as a thin layer on top of existing ndstrucures.
If it works, you can then add a functionality from your project
b
Sure, the strategy I found works best is something like this (this example is mostly self-contained if you want to play with it): https://github.com/breandan/kotlingrad/blob/master/src/main/kotlin/edu/umontreal/kotlingrad/samples/ToyExample.kt#L273:L292 Feel free to ping me if you want feedback, happy to help
a
yeah, I am doing something similar.