# Sparse with default value "Inf"

**URL:** <https://discourse.julialang.org/t/sparse-with-default-value-inf/35110>\
**Category:** General Usage\
**Tags:** question, sparse\
**Created:** [February 25, 2020, 12:04pm UTC](https://discourse.julialang.org/t/sparse-with-default-value-inf/35110 "2020-02-25T12:04:36Z")\
**Posts on this page:** 8\
**Page:** 2

<div class="post-metadata">

**Author:** ![abraunst](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraunst/32/6880_2.png) [@abraunst](https://discourse.julialang.org/u/abraunst)\
**Post date:** [February 28, 2020, 1:48pm UTC](https://discourse.julialang.org/t/sparse-with-default-value-inf/35110/23 "2020-02-28T13:48:32Z")

</div>

Try with

```julia
struct Weight <: Number
           w::Float64
end

```

For some reason you can find the following line in `stdlib/SparseArrays/src/higherorderfns.jl`:

```julia
@inline _iszero(x) = x == 0
@inline _iszero(x::Number) = Base.iszero(x)

```

---

<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:** [February 28, 2020, 1:54pm UTC](https://discourse.julialang.org/t/sparse-with-default-value-inf/35110/24 "2020-02-28T13:54:56Z")

</div>

This is not heavily tested, lacks convenience features, bounds checks, and various optimizations, but may get you started:

```julia
struct DefaultArray{T,N} <: AbstractArray{T,N}
    default::T
    size::NTuple{N,Int}
    elements::Dict{NTuple{N,Int},T}
end

function DefaultArray(default::T, size::NTuple{N,Int}) where {T,N}
    DefaultArray(default, size, Dict{NTuple{N,Int},T}())
end

Base.size(A::DefaultArray) = A.size

Base.getindex(A::DefaultArray, I::Int...) = get(A.elements, I, A.default)

Base.setindex!(A::DefaultArray, v, I::Int...) = A.elements[I] = v

```

```julia
julia> A = DefaultArray(Inf, (2,2))
2×2 DefaultArray{Float64,2}:
 Inf Inf
 Inf Inf

julia> A[1,2] = 3.0
3.0

julia> A
2×2 DefaultArray{Float64,2}:
 Inf 3.0
 Inf Inf

```

(Even after years of using Julia, it never ceases to amaze me how easy it is to get a fairly fast plug-in prototype in \<10 LOC.)

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [February 28, 2020, 2:15pm UTC](https://discourse.julialang.org/t/sparse-with-default-value-inf/35110/25 "2020-02-28T14:15:31Z")

</div>

You could perhaps store the inverse of all entries and then when you access a zero it will be infinite 🤔

---

<div class="post-metadata">

**Author:** ![Duane\_Wilson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/duane_wilson/32/9506_2.png) [@Duane\_Wilson](https://discourse.julialang.org/u/Duane_Wilson)\
**Post date:** [February 28, 2020, 8:26pm UTC](https://discourse.julialang.org/t/sparse-with-default-value-inf/35110/26 "2020-02-28T20:26:15Z")

</div>

I’m not 100% sure why mapped arrays doesn’t work here. I might be missing something

```julia
using MappedArrays
using SparseArrays

function f(x)
    if x == 0.0
        return Inf
    elseif x === NaN
        return 0.0
    else
        return x
    end
end 

function finv(y)
    if y == Inf
        return 0.0
    elseif y === 0.0
        return NaN
    else
        return y
    end
end 
A = spzeros(3, 3)
mA = mappedarray(f, finv, A)

```

```julia
julia> mA
3×3 mappedarray(f, finv, ::SparseMatrixCSC{Float64,Int64}) with eltype Float64:
 Inf Inf Inf
 Inf Inf Inf
 Inf Inf Inf

julia> mA .= Inf
3×3 mappedarray(f, finv, ::SparseMatrixCSC{Float64,Int64}) with eltype Float64:
 Inf Inf Inf
 Inf Inf Inf
 Inf Inf Inf

julia> nnz(mA.data)
0

julia> mA .-= 0
3×3 mappedarray(f, finv, ::SparseMatrixCSC{Float64,Int64}) with eltype Float64:
 Inf Inf Inf
 Inf Inf Inf
 Inf Inf Inf

julia> nnz(mA.data)
0

julia> mA[1, 2] = 3; mA[1,1] = 0
julia> mA
3×3 mappedarray(f, finv, ::SparseMatrixCSC{Float64,Int64}) with eltype Float64:
   0.0 3.0 Inf
 Inf Inf Inf
 Inf Inf Inf

julia> nnz(mA.data)
2

```

Unless you are looking for something different.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [February 29, 2020, 12:10am UTC](https://discourse.julialang.org/t/sparse-with-default-value-inf/35110/27 "2020-02-29T00:10:43Z")

</div>

It doesn’t surprise me that Mathematica allows this, but I’m not aware of any numerical sparse matrix libraries (UMFPACK, SuiteSparse, ARPACK, etc.) that support it. It would be a large amount of work to support the full range of efficient operations on a sparse matrix type that deviates from the standard CSC representation. Work that Mathematica has done, or course, but it’s not like we can use that.

---

<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:** [February 29, 2020, 12:52am UTC](https://discourse.julialang.org/t/sparse-with-default-value-inf/35110/28 "2020-02-29T00:52:57Z")

</div>

I perfectly understand @StefanKarpinski, via this thread i discovered that just a tiny bit of work on my part can turn into a working solution that does not imply augmenting julia itself. Altough via the 0 \to \infty monstrosity,i could see that a compressed array like structure similar to SparseArrays, without the linear algebra could be truly useful, it would also benefit Graph Algorithms.

at the moment i’m using @Tamas_Papp solution to move forward, if some more efficient sparse array like structure comes along i can also switch (since DefaultArray behaves like an Array 😃 ), thank you man!

---

<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:** [February 29, 2020, 6:23am UTC](https://discourse.julialang.org/t/sparse-with-default-value-inf/35110/29 "2020-02-29T06:23:32Z")

</div>

You are welcome.

If this is genuinely useful and otherwise not covered by any existing packages, please consider packaging it up and registering (all you need is [bounds checks](https://docs.julialang.org/en/v1/devdocs/boundscheck/) which I did not implement, and some tests).

---

<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:** [February 29, 2020, 2:39pm UTC](https://discourse.julialang.org/t/sparse-with-default-value-inf/35110/30 "2020-02-29T14:39:44Z")

</div>

i already made a repo

> **[GitHub - francescoalemanno/DefaultArrays.jl: Julia array type supporting a...](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 ...

obviously i will work on it alongside the real problem i’m solving

[Previous page](https://discourse.julialang.org/t/sparse-with-default-value-inf/35110.md?page=1)
