# \[ANN\] DefaultArrays

**URL:** <https://discourse.julialang.org/t/ann-defaultarrays/35679>\
**Category:** Package Announcements\
**Created:** [March 7, 2020, 8:44am UTC](https://discourse.julialang.org/t/ann-defaultarrays/35679 "2020-03-07T08:44:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![francesco.alemanno](https://avatars.discourse-cdn.com/v4/letter/f/e8c25b/32.png) [@francesco.alemanno](https://discourse.julialang.org/u/francesco.alemanno)\
**Post date:** [March 7, 2020, 8:44am UTC](https://discourse.julialang.org/t/ann-defaultarrays/35679/1 "2020-03-07T08:44:10Z")

</div>

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

> **[GitHub - francescoalemanno/DefaultArrays.jl: Julia array type supporting a default value,...](https://github.com/francescoalemanno/DefaultArrays.jl)**
>
> Julia array type supporting a default value, useful for storing very sparse information in a space efficient manner, the internal design uses "Dict" for storage, thanks to Tamas K. Papp @ https://github.com/tpapp

### Installation:

Since this package is now registered do:

```julia
using Pkg; pkg"add DefaultArrays";

```

### Example:

It can be used like any other common julia array:

```julia
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"](https://discourse.julialang.org/t/sparse-with-default-value-inf/35110)

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [March 7, 2020, 12:44pm UTC](https://discourse.julialang.org/t/ann-defaultarrays/35679/2 "2020-03-07T12:44:52Z")

</div>

Nice! Thanks for making a package out of this.

---

<div class="post-metadata">

**Author:** ![francesco.alemanno](https://avatars.discourse-cdn.com/v4/letter/f/e8c25b/32.png) [@francesco.alemanno](https://discourse.julialang.org/u/francesco.alemanno)\
**Post date:** [March 7, 2020, 1:42pm UTC](https://discourse.julialang.org/t/ann-defaultarrays/35679/3 "2020-03-07T13:42:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [March 7, 2020, 6:01pm UTC](https://discourse.julialang.org/t/ann-defaultarrays/35679/4 "2020-03-07T18:01:14Z")

</div>

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](https://github.com/JuliaLang/julia/issues/10410#issuecomment-77451003)).

---

<div class="post-metadata">

**Author:** ![francesco.alemanno](https://avatars.discourse-cdn.com/v4/letter/f/e8c25b/32.png) [@francesco.alemanno](https://discourse.julialang.org/u/francesco.alemanno)\
**Post date:** [March 7, 2020, 8:26pm UTC](https://discourse.julialang.org/t/ann-defaultarrays/35679/5 "2020-03-07T20:26:08Z")

</div>

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 😃

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
