[ANN] LengthFreeStaticMatrices.jl - drop length parameters for static matrices

In Electrum.jl, another package I maintain, I have a pervasive need to include SMatrix instances as part of data strucutures. However, this leads to a very annoying problem: because Julia cannot infer the length type parameter automatically, I have to carry it everywhere if I need a composite type to be a pure bits type (needed for storing structs inline in a vector, for instance). Even if this isn’t an important consideration, it can be annoying to have an extra type parameter corresponding to the length of the backing tuple.

To solve this issue, I created LengthFreeStaticMatrices.jl, which exports LSMatrix{M,N,T}, lacking the final length parameter. This is accomplished by storing the data in an NTuple{N,NTuple{M,T}}. It also exports the alias LSSqMatrix{N,T} for square matrices.

The LSMatrix type exported by this package is intended to work exactly like StaticArrays.SMatrix, and any failure to do so is a bug – please file a bug report for any noted deviations! Performance should also be very similar, if not the same. From my very brief testing, it has been essentially identical, but my use cases are pretty specific, so I wouldn’t be surprised if there are some performance issues I haven’t seen yet.

12 Likes

Fascinating solution! I really hate having to lug around type parameters which are calculable from the others! It’s like: why do we even have computers except to multiply small integers together :smile: ?

There’s plenty of ugly things in Core of course to make the sausage, but for me this is the ugliest Thing-I-Must-Deal-With in Julia, and why I (and everyone else) gave up trying to make a StaticBitArrays.jl package. Chunksize, array size, chunklength… all functions of eachother, all needing a dangling parameter of their own. Perhaps when I fully understand how this code works I will give it another shot.

I’m too dumb to understand why the problem is so hard. All I know is that it is apparently VERY HARD INDEED, if it’s prefereable to write and maintain the SArrays code as is. Great and inspiring work getting this to go!

1 Like

There’s been talk in the Julia Github issues tab of potential language features that could provide solutions, including some notion of generated structs. Hopefully, in the future, some language-level innovation will render this package pointless.

I should also note that extending this to higher dimensional arrays would require manual implementation for each dimension as far as I know, so that’s why this is just a package for matrices.

1 Like