# Function zero() with type"Any"

**URL:** <https://discourse.julialang.org/t/function-zero-with-type-any/27007>\
**Category:** General Usage\
**Tags:** question\
**Created:** [July 30, 2019, 11:56pm UTC](https://discourse.julialang.org/t/function-zero-with-type-any/27007 "2019-07-30T23:56:22Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dota](https://avatars.discourse-cdn.com/v4/letter/d/ec9cab/32.png) [@Dota](https://discourse.julialang.org/u/Dota)\
**Post date:** [July 30, 2019, 11:56pm UTC](https://discourse.julialang.org/t/function-zero-with-type-any/27007/1 "2019-07-30T23:56:22Z")

</div>

Hi!

Nowadays I’m learning about Julia, mainly to solve non-linear optimization problem. Because of the low efficiency of dense matrix, I transfer some of the matrix into sparse matrix. When I’m trying to multiply two matrix with type"Any", an error trough out:  
ERROR: MethodError: no method matching zero(::Type{Any})

It seems that function zero() cannot have type “Any” as input, but I don’t understand why and how to solve this problem.

---

<div class="post-metadata">

**Author:** ![Dota](https://avatars.discourse-cdn.com/v4/letter/d/ec9cab/32.png) [@Dota](https://discourse.julialang.org/u/Dota)\
**Post date:** [July 31, 2019, 12:01am UTC](https://discourse.julialang.org/t/function-zero-with-type-any/27007/2 "2019-07-31T00:01:27Z")

</div>

> Blockquote  
> julia\> matrix  
> 2×2 SparseMatrixCSC{Any,Int64} with 3 stored entries:  
> [1, 1] = 1  
> [2, 1] = 1  
> [2, 2] = 1  
> julia\> matrix=Any[1 1;1 2]  
> 2×2 Array{Any,2}:  
> 1 1  
> 1 2  
> julia\> matrix-matrix  
> ERROR: MethodError: no method matching zero(::Type{Any})  
> Closest candidates are:  
> zero(::Type{Union{Missing, T}}) where T at missing.jl:87  
> zero(::Type{LibGit2.GitHash}) at C:\Users\julia\AppData\Local\Julia-1.1.1\share\julia\stdlib\v1.1\LibGit2\src\oid.jl:220  
> zero(::Type{Pkg.Resolve.VersionWeights.VersionWeight}) at C:\Users\julia\AppData\Local\Julia-1.1.1\share\julia\stdlib\v1.1\Pkg\src\resolve\VersionWeights.jl:19

It seems that while operating sparse matrix, function zero() is used, in missing.jl, I find that:

> Blockquote  
> for f in (:(Base.zero), :(Base.one), :(Base.oneunit))  
> @eval function $(f)(::Type{Union{T, Missing}}) where T  
> T === Any && throw(MethodError($f, (Any,))) # To prevent StackOverflowError  
> $f(T)  
> end  
> end

I try to delete that ,but it doesn’t work

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [July 31, 2019, 12:05am UTC](https://discourse.julialang.org/t/function-zero-with-type-any/27007/3 "2019-07-31T00:05:02Z")

</div>

> [@Dota](#):
>
> matrix=Any[1 1;1 2]

Why not write `matrix = [1 1;1 2]`?

```julia

julia> matrix = [1 1;1 2]
2×2 Array{Int64,2}:
 1 1
 1 2

julia> matrix - matrix
2×2 Array{Int64,2}:
 0 0
 0 0

```

---

<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:** [July 31, 2019, 7:26am UTC](https://discourse.julialang.org/t/function-zero-with-type-any/27007/4 "2019-07-31T07:26:28Z")

</div>

While using `Any` as an element type is probably not good style unless you have a good reason, the methods should work, so this looks like a bug. Probably non-numeric types should just use `0` here.

Please make a self-contained MWE and open an issue.

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [July 31, 2019, 8:41am UTC](https://discourse.julialang.org/t/function-zero-with-type-any/27007/5 "2019-07-31T08:41:26Z")

</div>

> [@Dota](#):
>
> matrix=Any[1 1;1 2]

It appears they do work (Julia v1.1.1)

```julia
julia> matrix=Any[1 1;1 2]
2×2 Array{Any,2}:
 1 1
 1 2

julia> matrix - matrix
2×2 Array{Int64,2}:
 0 0
 0 0

```

---

<div class="post-metadata">

**Author:** ![Dota](https://avatars.discourse-cdn.com/v4/letter/d/ec9cab/32.png) [@Dota](https://discourse.julialang.org/u/Dota)\
**Post date:** [July 31, 2019, 4:48pm UTC](https://discourse.julialang.org/t/function-zero-with-type-any/27007/8 "2019-07-31T16:48:06Z")

</div>

yeah it works in this situation… but in my problem I try to use sparse matrix to improve the operating speed, it seems like something wrong with sparse matrix in type “Any”

---

<div class="post-metadata">

**Author:** ![Dota](https://avatars.discourse-cdn.com/v4/letter/d/ec9cab/32.png) [@Dota](https://discourse.julialang.org/u/Dota)\
**Post date:** [July 31, 2019, 4:48pm UTC](https://discourse.julialang.org/t/function-zero-with-type-any/27007/9 "2019-07-31T16:48:39Z")

</div>

yeah… actually I’m using JuMP to do nonlinear optimization, my input is a 1\*144 matrix, in the value function some entries of the matrix is constant, e.g., zero, but some others are `VariableRef` , so I need to use `Any`

---

<div class="post-metadata">

**Author:** ![Dota](https://avatars.discourse-cdn.com/v4/letter/d/ec9cab/32.png) [@Dota](https://discourse.julialang.org/u/Dota)\
**Post date:** [July 31, 2019, 4:50pm UTC](https://discourse.julialang.org/t/function-zero-with-type-any/27007/10 "2019-07-31T16:50:51Z")

</div>

I really need them in my question, I need to build a matrix with some entries are variable, thank you for your advice!

---

<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:** [July 31, 2019, 7:09pm UTC](https://discourse.julialang.org/t/function-zero-with-type-any/27007/11 "2019-07-31T19:09:14Z")

</div>

> [@Tamas\_Papp](#):
>
> Probably non-numeric types should just use `0` here.

`zero(Any)` should definitely not be `0`, and throwing an error is the right thing in the absence of a generic additive-identity object. See also [#8092](https://github.com/JuliaLang/julia/issues/8092), [#4808](https://github.com/JuliaLang/julia/issues/4808), and [#27766](https://github.com/JuliaLang/julia/issues/27766), for example.

That being said, it should be possible to perform a matrix multiplication or subtraction for non-empty arrays without calling `zero` on the element type, by instead using the first element of the matrices to initialize the sums.

---

<div class="post-metadata">

**Author:** ![cchderrick](https://avatars.discourse-cdn.com/v4/letter/c/ecd19e/32.png) [@cchderrick](https://discourse.julialang.org/u/cchderrick)\
**Post date:** [July 31, 2019, 8:38pm UTC](https://discourse.julialang.org/t/function-zero-with-type-any/27007/12 "2019-07-31T20:38:56Z")

</div>

> [@Dota](#):
>
> Blockquote  
> julia\> matrix  
> 2×2 SparseMatrixCSC{Any,Int64} with 3 stored entries:  
> [1, 1] = 1  
> [2, 1] = 1  
> [2, 2] = 1  
> julia\> matrix=Any[1 1;1 2]  
> 2×2 Array{Any,2}:  
> 1 1  
> 1 2  
> julia\> matrix-matrix  
> ERROR: MethodError: no method matching zero(::Type{Any})  
> Closest candidates are:  
> zero(::Type{Union{Missing, T}}) where T at missing.jl:87  
> zero(::Type{LibGit2.GitHash}) at C:\Users\julia\AppData\Local\Julia-1.1.1\share\julia\stdlib\v1.1\LibGit2\src\oid.jl:220  
> zero(::Type{Pkg.Resolve.VersionWeights.VersionWeight}) at C:\Users\julia\AppData\Local\Julia-1.1.1\share\julia\stdlib\v1.1\Pkg\src\resolve\VersionWeights.jl:19

I can’t reproduce? Not sure what is the problem.  
I am on Julia 1.0.3, by the way.

```julia-repl
julia> using SparseArrays

julia> A = sparse([1,2,2],[1,1,2],[1,1,1])
2×2 SparseMatrixCSC{Int64,Int64} with 3 stored entries:
  [1, 1] = 1
  [2, 1] = 1
  [2, 2] = 1

julia> B=Any[1 1;1 2]
2×2 Array{Any,2}:
 1 1
 1 2

julia> A-B
2×2 Array{Int64,2}:
 0 -1
 0 -1

julia> A-A
2×2 SparseMatrixCSC{Int64,Int64} with 0 stored entries

julia> B-B
2×2 Array{Int64,2}:
 0 0
 0 0

```

how did you make this?  
`SparseMatrixCSC{Any,Int64}`, a {Any, Int64}?

Edit:  
oh

```julia-repl
julia> C = sparse([1,2,2],[1,1,2],Any[1,1,1])
2×2 SparseMatrixCSC{Any,Int64} with 3 stored entries:
  [1, 1] = 1
  [2, 1] = 1
  [2, 2] = 1

julia> A-C
ERROR: MethodError: no method matching zero(::Type{Any})

```

---

<div class="post-metadata">

**Author:** ![Dota](https://avatars.discourse-cdn.com/v4/letter/d/ec9cab/32.png) [@Dota](https://discourse.julialang.org/u/Dota)\
**Post date:** [July 31, 2019, 8:47pm UTC](https://discourse.julialang.org/t/function-zero-with-type-any/27007/13 "2019-07-31T20:47:23Z")

</div>

right！But I really need sparse matrix with type `Any`, I don’t know how to solve this problem…

---

<div class="post-metadata">

**Author:** ![Dota](https://avatars.discourse-cdn.com/v4/letter/d/ec9cab/32.png) [@Dota](https://discourse.julialang.org/u/Dota)\
**Post date:** [August 1, 2019, 12:00am UTC](https://discourse.julialang.org/t/function-zero-with-type-any/27007/14 "2019-08-01T00:00:12Z")

</div>

thank you for your reply! I’m sorry but I don’t fully understand how to use the first element of the matrices to initialize the sums, Could you show more details or give a simple example? thank you very much!

---

<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:** [August 1, 2019, 4:29am UTC](https://discourse.julialang.org/t/function-zero-with-type-any/27007/15 "2019-08-01T04:29:29Z")

</div>

> [@Dota](#):
>
> But I really need sparse matrix with type `Any`

Why?

> [@Dota](#):
>
> I don’t fully understand how to use the first element of the matrices to initialize the sums

I think this was a suggestion as a fix for the implementation of the relevant methods, not something the user would do.

---

<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:** [August 1, 2019, 12:38pm UTC](https://discourse.julialang.org/t/function-zero-with-type-any/27007/16 "2019-08-01T12:38:21Z")

</div>

> [@Dota](#):
>
> some entries of the matrix is constant, e.g., zero, but some others are `VariableRef` , so I need to use `Any`

You could use `Union{Real,VariableRef}` and then define `Base.zero(::Type{Union,VariableRef}) = 0`.

Arguably, JuMP should define `AbstractJuMPScalar <: Real` (or maybe `<: Number`) to help with this sort of problem (`AbstractJuMPScalar` is the supertype of `VariableRef`).

---

<div class="post-metadata">

**Author:** ![leah](https://avatars.discourse-cdn.com/v4/letter/l/c89c15/32.png) [@leah](https://discourse.julialang.org/u/leah)\
**Post date:** [March 2, 2020, 6:56pm UTC](https://discourse.julialang.org/t/function-zero-with-type-any/27007/17 "2020-03-02T18:56:46Z")

</div>

I have a similar problem, but my input file is a biom file  
julia\> netw\_results = learn\_network(data\_path, meta\_data\_path, sensitive=true, heterogeneous=false, alpha= 0.05)

ERROR: MethodError: no method matching zero(::Type{Any})

Thanks in advance
