mikera / vectorz

Fast and flexible numerical library for Java featuring N-dimensional arrays

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add possibility for sparse default value different than zero

icarocd opened this issue · comments

In SparseRowMatrix and SparseColumnMatrix, zero is assumed as the sparse value.
However, there are many applications were the default value if 1 instead, as in some symmetric distance matrices for example.
Although it is possible to handle this limitation by inverting the logic of the application to treat the way the data structure is used, the possibility I am suggesting is simple to implement and very useful.

Good idea, I also thought about this before.

Ultimately I decided that the performance tradeoff / extra complexity wasn't worth it in the main sparse classes (SparseRowMatrix and SparseColumnMatrix). Assuming zero as the sparse value keeps things simple and enables many optimisations.

However it could make sense as a separate class / pair of classes, so leaving this open as an enhancement idea. Could also be implemented using a new kind of sparse vector that support non-zero default values

Another interesting idea would be a "SparseMaskMatrix" that allows you to mask any elements of an array and let other elements fall through to a "background" matrix. Would allow lightweight "edit layers" over large immutable arrays, for example.