# 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:** 20
**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: [February 25, 2020, 12:04pm UTC](https://discourse.julialang.org/t/sparse-with-default-value-inf/35110/1 "2020-02-25T12:04:37Z")

</div>

Hi guys i’m working on a problem which requires solving many linear assignment problems (LAP) and i wonder if it’s possible to declare a sparse matrix object in Julia which defaults to “Inf” rather than 0, as this would turn into a massive optimization in storage for the nature of my problem.

So, is there any way to declare a Sparse Array which defaults to some other value?

Thank you 🙂

---

<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 25, 2020, 12:34pm UTC](https://discourse.julialang.org/t/sparse-with-default-value-inf/35110/2 "2020-02-25T12:34:37Z")

</div>

You could create your own type to do this.

Perhaps for prototyping, you could just use a `Dict` to store which values are not `-Inf`, so that the bookkeeping is easier than CSC — if I understand correctly, you will not be needing any linear algebra anyway.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [February 25, 2020, 12:36pm UTC](https://discourse.julialang.org/t/sparse-with-default-value-inf/35110/3 "2020-02-25T12:36:14Z")

</div>

See the discussion in

[https://github.com/JuliaLang/julia/issues/10410](https://github.com/JuliaLang/julia/issues/10410)

In particular, as suggested in that issue, you can easily create your own type wrapping `SparseArray` that returns a different default (or use some other data structure as suggested above).

---

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [February 25, 2020, 12:53pm UTC](https://discourse.julialang.org/t/sparse-with-default-value-inf/35110/4 "2020-02-25T12:53:22Z")

</div>

Maybe you could store `1./A`, so that your `Inf`s are 0s?

---

<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 25, 2020, 3:52pm UTC](https://discourse.julialang.org/t/sparse-with-default-value-inf/35110/5 "2020-02-25T15:52:36Z")

</div>

Even the approach I described there wouldn’t work with a default value of `Inf` since you can’t add anything to `Inf` and get a finite value.

---

<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 26, 2020, 6:17pm UTC](https://discourse.julialang.org/t/sparse-with-default-value-inf/35110/6 "2020-02-26T18:17:21Z")

</div>

i’m working with Dict’s in the end.  
it is quite sad though, as it is pretty common to use a different default for sparse matrixes.

---

<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 26, 2020, 7:54pm UTC](https://discourse.julialang.org/t/sparse-with-default-value-inf/35110/7 "2020-02-26T19:54:15Z")

</div>

> [@francesco.alemanno](#):
>
> it is quite sad though, as it is pretty common to use a different default for sparse matrixes.

Really? What sparse matrix library supports this?

---

<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 26, 2020, 9:01pm UTC](https://discourse.julialang.org/t/sparse-with-default-value-inf/35110/8 "2020-02-26T21:01:06Z")

</div>

You can also define your own type that redefines `zero` and operations (this idea is from @andreasnoack originally I think), e.g. something like:

```julia
struct SemiRing{T,A,M,Z,O} <: Number
        val::T
end

Base.:+(x::SemiRing{T, A, M, Z, O}, y::SemiRing{T, A, M, Z, O}) where {T, A, M, Z, O} = SemiRing{T, A, M, Z, O}(A(x.val, y.val))
Base.:*(x::SemiRing{T, A, M, Z, O}, y::SemiRing{T, A, M, Z, O}) where {T, A, M, Z, O} = SemiRing{T, A, M, Z, O}(M(x.val, y.val))
Base.zero(::Type{SemiRing{T, A, M, Z, O}}) where {T, A, M, Z, O} = SemiRing{T, A, M, Z, O}(Z)
Base.zero(::SemiRing{T, A, M, Z, O}) where {T, A, M, Z, O} = SemiRing{T, A, M, Z, O}(Z)
Base.one(::Type{SemiRing{T, A, M, Z, O}}) where {T, A, M, Z, O} = SemiRing{T, A, M, Z, O}(O)
Base.one(::SemiRing{T, A, M, Z, O}) where {T, A, M, Z, O} = SemiRing{T, A, M, Z, O}(O)

```

then you use it as

```julia
S = SemiRing{Float64,max,+,-Inf,0.0}
using SparseArrays
x=sparse(1:10, 1:10, ones(S,10))

```

---

<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 27, 2020, 12:40pm UTC](https://discourse.julialang.org/t/sparse-with-default-value-inf/35110/9 "2020-02-27T12:40:26Z")

</div>

@StefanKarpinski  
this for example

> **[SparseArray—Wolfram Language Documentation](https://reference.wolfram.com/language/ref/SparseArray.html)**
>
> SparseArray\[{pos1 -\> v1, pos2 -\> v2, ...}\] yields a sparse array with all elements zero except for values vi at positions posi. SparseArray\[list\] yields a sparse array version of list. SparseArray\[data, {d1, d2, ...}\] yields a sparse array...

@abraunst  
Thank you so much, i already prototyped most of the code needed, and it is using dictionaries, for now i’m focusing on correctness, as soon as i finish prototyping the whole algorithm i’ll try to make it work on Sparse SemiRings

---

<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 27, 2020, 12:58pm UTC](https://discourse.julialang.org/t/sparse-with-default-value-inf/35110/10 "2020-02-27T12:58:11Z")

</div>

One can think of `<:AbstractMatrix` types as

1. a rectangular table of data
2. a matrix (ie as understood in linear algebra)

It is my understanding that the SparseArrays library targets the latter, so I am curious what the application is for this use case. Do you have some examples and references?

If it is not about linear algebra, just a kind of compressed storage, then it would make sense to define a subtype of `AbstractArray`, along the lines of various packages in

> **[JuliaArrays](https://github.com/JuliaArrays)**
>
> Custom array types (and utilities for building array types) for Julia - JuliaArrays

---

<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 27, 2020, 1:54pm UTC](https://discourse.julialang.org/t/sparse-with-default-value-inf/35110/11 "2020-02-27T13:54:00Z")

</div>

> Do you have some examples and references?

The applications in linear programming are endless, for instance, my problem is solving many very large linear assignment problems (with some modifications w.r.t the various hungarian methods already at disposal) which have many blocking costs (typical size: 200000x200000 matrix with ~ 99% of the values == Inf),

> If it is not about linear algebra, just a kind of compressed storage, then it would make sense to define a subtype of `AbstractArray` , along the lines of various packages in

I would not do any matmul or factorization on this matrices, the things i would use are the broadcasting machinery, sliced views, indexing, masked indexing.

that’s all, if i had the time and the knowledge, i would make package, and maybe i will try to do it after i finish working out the actual algorithms 🙂 for my work  
thank you all

---

<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 27, 2020, 2:00pm UTC](https://discourse.julialang.org/t/sparse-with-default-value-inf/35110/12 "2020-02-27T14:00:56Z")

</div>

> [@francesco.alemanno](#):
>
> applications in linear programming are endless

I am not an expert in those algorithms, but I would assume that they make some use of the sparsity information directly, so it might make sense to just provide that in the interface.

Eg given a Ax \le b constraint, A could be provided as `(row, column, value)` triplets for _finite_ entries with the understanding that it is \infty everywhere else, instead of going to the somewhat roundabout solution of wrapping that information in a matrix type, _then_ extracting it.

---

<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 28, 2020, 10:00am UTC](https://discourse.julialang.org/t/sparse-with-default-value-inf/35110/13 "2020-02-28T10:00:24Z")

</div>

Okay guys, i’m reworking my code now to use Sparse Matrices, containing a custom type, which allows me to redefine zero and get past the original issue,  
Thank you again @abraunst

Now my other issue, is that if i broadcast some calculation involving the elements which are not stored, even though this calculation produces a “0” this zero is stored in the sparse matrix, i do not want to use dropzeros!, is there some way to avoid storing “0” after a broadcast?

because i know for sure that all the operations i allow on this custom sparse type, will never affect "0"s

by the way i want to thank everybody, every answer has slowly steered me in the right direction, thank you guys

---

<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, 10:50am UTC](https://discourse.julialang.org/t/sparse-with-default-value-inf/35110/14 "2020-02-28T10:50:44Z")

</div>

Can you produce a MWE? For instance, following my example

```julia
julia> S = SemiRing{Float64,max,+,-Inf,0.0}
SemiRing{Float64,max,+,-Inf,0.0}
julia> x=sparse(1:10, 1:10, ones(S,10));
julia> (t->S(-Inf)).(x)
10×10 SparseMatrixCSC{Any,Int64} with 0 stored entries

```

seems to work as expected.

EDIT: the `eltype` being `Any` seems a bug?

---

<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 28, 2020, 11:56am UTC](https://discourse.julialang.org/t/sparse-with-default-value-inf/35110/16 "2020-02-28T11:56:43Z")

</div>

MWE, i hope that there is a way to solve this 🙂, thank you again

```julia
struct Weight
    w::Float64
end
import Base.-
Base.:-(x::Weight, y::T) where {T<:Number} = Weight(x.w-y)
Base.:-(y::T,x::Weight) where {T<:Number} = Weight(y-x.w)
import Base.convert
Base.convert(::Type{Weight}, y::T) where {T<:Number} = Weight(y)
import Base.zero
Base.zero(::Type{Weight}) = Weight(Inf)
Base.zero(::Weight) = Weight(Inf)

#TESTS
using SparseArrays
A=spzeros(Weight,10,10)
@show typeof(A)
A.=Inf #Works
snumzeros=size(A.nzval)
@show snumzeros
@show typeof(A)
A.-=0 #ALLOCATES UNNECESSARY ZEROS
snumzeros=size(A.nzval)

@show snumzeros
@show typeof(A)
;

```

```
typeof(A) = SparseMatrixCSC{Weight,Int64}
snumzeros = (0,)
typeof(A) = SparseMatrixCSC{Weight,Int64}
snumzeros = (100,)
typeof(A) = SparseMatrixCSC{Weight,Int64}

```

---

<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, 12:29pm UTC](https://discourse.julialang.org/t/sparse-with-default-value-inf/35110/17 "2020-02-28T12:29:44Z")

</div>

Would MappedArrays.jl work for this?

> **[GitHub - JuliaArrays/MappedArrays.jl: Lazy in-place transformations of arrays](https://github.com/JuliaArrays/MappedArrays.jl)**
>
> Lazy in-place transformations of arrays. Contribute to JuliaArrays/MappedArrays.jl development by creating an account on GitHub.

---

<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 28, 2020, 12:42pm UTC](https://discourse.julialang.org/t/sparse-with-default-value-inf/35110/18 "2020-02-28T12:42:01Z")

</div>

Unfortunately it wont help

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [February 28, 2020, 12:57pm UTC](https://discourse.julialang.org/t/sparse-with-default-value-inf/35110/20 "2020-02-28T12:57:22Z")

</div>

> [@francesco.alemanno](#):
>
> `A=spzeros(Weight,10,10)`

I seem to recall that our suggestion was the other way around: define a custom array type that wraps around a `SparseMatrixCSC`, not a `SparseMatrixCSC` that contains a custom number type.

> [@francesco.alemanno](#):
>
> `Base.zero(::Type{Weight}) = Weight(Inf)`

`zero` is supposed to be the additive identity. Defining a method that fundamentally changes the meaning of this function will break all generic code that uses `zero`, and will be a continuing headache.

---

<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 28, 2020, 1:25pm UTC](https://discourse.julialang.org/t/sparse-with-default-value-inf/35110/21 "2020-02-28T13:25:12Z")

</div>

> I seem to recall that our suggestion was the other way around: define a custom array type that wraps around a `SparseMatrixCSC` , not a `SparseMatrixCSC` that contains a custom number type.

i see the deep uglyness in asserting W(\infty)=0, unfortunately i do not know how to start doing what you suggested.

I still want a Sparse Matrix, which is sparsified w.r.t. a custom value.

---

<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:33pm UTC](https://discourse.julialang.org/t/sparse-with-default-value-inf/35110/22 "2020-02-28T13:33:35Z")

</div>

> [@stevengj](#):
>
> `zero` is supposed to be the additive identity

I think it is not wrong for a user-defined type (in which the addition can be redefined, e.g. as `min`).

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