[ANN] DefaultArrays

Hey everyone,
this package implements an array type which supports a default value, it might be useful for storing very sparse information in a space efficient manner, the internal design uses “Dict” for storage

Installation:

Since this package is now registered do:

using Pkg; pkg"add DefaultArrays";

Example:

It can be used like any other common julia array:

M=DefaultArray(0.0,100,100) #0.0 is the default value
M.=rand([zeros(50);1],100,100)

for i in eachnondefault(M) # eachnondefault iterates over non default indices of M
    M[i]=rand()
end
Q=sin.(M)

sum(Q)  #->  some random value
length(Q.elements) #-> MUCH less space occupied than 100*100 = 10000

original idea of Tamas K. Papp: Sparse with default value "Inf"

7 Likes

Nice! Thanks for making a package out of this.

Hey Tamas, my pleasure! you really solved a problem of mine with that idea!

Why not store these kind of arrays as A = S + c * I, where S is the ordinary sparse array (with lots zeros), c the desired scalar “default” value, and I the Julian identity matrix? (With the non-zero values of S corrected by -c.)

In this form it would be easy to get efficient matrix-ops.

(Just found that this was originally suggested by @StefanKarpinski in the old issue, https://github.com/JuliaLang/julia/issues/10410#issuecomment-77451003).

hey Cossio, thank you for your interest, @StefanKarpinski 's approach is the best if you handle finite numerical values, unfortunately it breaks down if you need to use “Inf” as a default value, and it obviously won’t work if you want an array of “MyCoolType” with some default instance of such type.
This package is meant to work in EVERY possible case, not to be the most efficient for finite floats :smiley:

Altough i think that a sparse array type of the kind you are describing could be very useful, maybe i will try to wip it up the next weekend