# No method matching \`oneunit(::Type{Any})\` when solving matrix equation

**URL:** <https://discourse.julialang.org/t/no-method-matching-oneunit-type-any-when-solving-matrix-equation/48020>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [October 9, 2020, 8:33am UTC](https://discourse.julialang.org/t/no-method-matching-oneunit-type-any-when-solving-matrix-equation/48020 "2020-10-09T08:33:11Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![kapple](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kapple/32/218915_2.png) [@kapple](https://discourse.julialang.org/u/kapple)\
**Post date:** [October 9, 2020, 8:33am UTC](https://discourse.julialang.org/t/no-method-matching-oneunit-type-any-when-solving-matrix-equation/48020/1 "2020-10-09T08:33:11Z")

</div>

I have some functions trying to solve a matrix equation

```julia
u = A\b

```

inside a function. Upon running the function, I receive the error

```julia
ERROR: MethodError: no method matching oneunit(::Type{Any})

```

What’s strange is, I copy the values of `A` and `b` from the terminal via `@show`, i.e.

```julia
@show A = ...
@show b = ...

```

and run `u = A\b` in a script, and it runs without errors.

What am I doing wrong? What results in the error when run inside the function?

Let me know if you would like the specific context via a standalone Julia script.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [October 9, 2020, 8:52am UTC](https://discourse.julialang.org/t/no-method-matching-oneunit-type-any-when-solving-matrix-equation/48020/2 "2020-10-09T08:52:08Z")

</div>

What’s the type of `A` and `b`?

---

<div class="post-metadata">

**Author:** ![kapple](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kapple/32/218915_2.png) [@kapple](https://discourse.julialang.org/u/kapple)\
**Post date:** [October 9, 2020, 8:55am UTC](https://discourse.julialang.org/t/no-method-matching-oneunit-type-any-when-solving-matrix-equation/48020/3 "2020-10-09T08:55:43Z")

</div>

```julia
typeof(A) = SymTridiagonal{Float64,Array{Float64,1}}
typeof(b) = Array{Any,1}

```

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [October 9, 2020, 9:00am UTC](https://discourse.julialang.org/t/no-method-matching-oneunit-type-any-when-solving-matrix-equation/48020/4 "2020-10-09T09:00:04Z")

</div>

Well there’s your problem - `b` can hold elements of quite literally any type and there is no one definition of `oneunit` for all possible types. I don’t know how you generate `b`, but you’ll probably want to make sure it contains elements of a concrete type, in your case possibly `Float64`.

---

<div class="post-metadata">

**Author:** ![kapple](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kapple/32/218915_2.png) [@kapple](https://discourse.julialang.org/u/kapple)\
**Post date:** [October 9, 2020, 9:04am UTC](https://discourse.julialang.org/t/no-method-matching-oneunit-type-any-when-solving-matrix-equation/48020/5 "2020-10-09T09:04:14Z")

</div>

Thanks! That solved it.

`b` is constructed as a matrix product of `SymTridiagonal{Float64,Array{Float64,1}}` and `Array{Real,1}` and the result was an array of `Any` which… should I think that’s strange?

Anyway, I just wrapped the definition of `b` with `real`, i.e.

```julia
b = real(...)

```

and it fixed it.

Thanks for your help!

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [October 9, 2020, 9:06am UTC](https://discourse.julialang.org/t/no-method-matching-oneunit-type-any-when-solving-matrix-equation/48020/6 "2020-10-09T09:06:27Z")

</div>

Maybe there’s a missing method? If you can share some more code to reproduce this deterministically, you might be able to file an issue to get this fixed (or you’re doing something else weird, who knows :p).

---

<div class="post-metadata">

**Author:** ![kapple](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kapple/32/218915_2.png) [@kapple](https://discourse.julialang.org/u/kapple)\
**Post date:** [October 9, 2020, 9:16am UTC](https://discourse.julialang.org/t/no-method-matching-oneunit-type-any-when-solving-matrix-equation/48020/7 "2020-10-09T09:16:10Z")

</div>

Okay, well here’s a simple replication:

```julia
julia> using LinearAlgebra

julia> A = SymTridiagonal([1., 2., 3.], [4., 5.])
3×3 SymTridiagonal{Float64,Array{Float64,1}}:
 1.0 4.0 ⋅ 
 4.0 2.0 5.0
  ⋅ 5.0 3.0

julia> v = Vector{Real}(undef, 3)
3-element Array{Real,1}:
 #undef
 #undef
 #undef

julia> for n = 1:length(v)
           v[n] = rand(Float64)
       end

julia> typeof(A)
SymTridiagonal{Float64,Array{Float64,1}}

julia> typeof(v)
Array{Real,1}

julia> typeof(A*v)
Array{Any,1}

```

However, when initializing `v` as a vector of `Float64`s, the `Any` issue disappears.

```julia
julia> using LinearAlgebra

julia> A = SymTridiagonal([1., 2., 3.], [4., 5.])
3×3 SymTridiagonal{Float64,Array{Float64,1}}:
 1.0 4.0 ⋅
 4.0 2.0 5.0
  ⋅ 5.0 3.0

julia> v = Vector{Float64}(undef, 3)
3-element Array{Float64,1}:
 1.902534155e-315
 8.06947933e-316
 0.0

julia> for n = 1:length(v)
           v[n] = rand(Float64)
       end

julia> typeof(A)
SymTridiagonal{Float64,Array{Float64,1}}

julia> typeof(v)
Array{Float64,1}

julia> typeof(A*v)
Array{Float64,1}

```

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [October 9, 2020, 9:22am UTC](https://discourse.julialang.org/t/no-method-matching-oneunit-type-any-when-solving-matrix-equation/48020/8 "2020-10-09T09:22:49Z")

</div>

`Real` is an abstract type and all concrete subtypes of `Real` are these (expanded recursively using `subtypes`):

```julia
17-element Array{Any,1}:
 BigFloat
 Float16
 Float32
 Float64
 Irrational
 Bool
 UInt128
 UInt16
 UInt32
 UInt64
 UInt8
 BigInt
 Int128
 Int16
 Int32
 Int64
 Int8

```

Are you sure you want to have `v` be able to hold all of those?

If all you want is a vector of `Float64` of `n` elements, `rand(n)` is what you’re looking for:

```julia
julia> rand(4)
4-element Array{Float64,1}:
 0.18146125094318588
 0.5071153204876462
 0.43918306228459847
 0.5878162664116326

```

---

<div class="post-metadata">

**Author:** ![kapple](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kapple/32/218915_2.png) [@kapple](https://discourse.julialang.org/u/kapple)\
**Post date:** [October 9, 2020, 9:27am UTC](https://discourse.julialang.org/t/no-method-matching-oneunit-type-any-when-solving-matrix-equation/48020/9 "2020-10-09T09:27:24Z")

</div>

I see your point, and the answer is not necessarily yes, but considering things like optimization through type stability, and… other stuff I’m sure exists in efficient and healthy coding in Julia… I suppose the answer is no.

I had the (incorrect) mindset that if I initialize with `Real` then it would well handle anything I feed the vector elements.

> [@Sukera](#):
>
> If all you want is a vector of `Float64` of `n` elements, `rand(n)` is what you’re looking for:

Oh, in my original context, there were specific values to assign the vector in a loop (complicated so I couldn’t use comprehensions (or could I…)) and in my MWE above, I used `rand` to grab any random values, what the values were didn’t matter.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [October 9, 2020, 9:29am UTC](https://discourse.julialang.org/t/no-method-matching-oneunit-type-any-when-solving-matrix-equation/48020/10 "2020-10-09T09:29:42Z")

</div>

> [@kapple](#):
>
> Oh, in my original context, there were specific values to assign the vector in a loop (complicated so I couldn’t use comprehensions (or could I…)) and in my MWE above, I used `rand` to grab any random values, what the values were didn’t matter.

You can always put the hard work of generating those values into a function and call that in the comprehension - in fact, it’ll probably work better than hard-coding `Real` since the compiler can infer exactly what types are actually needed 🙂 (as long as the generating function is somewhat type stable, of course)

* * *

Nevertheless, I think the compiler should be able to infer `Real` here 🤔

---

<div class="post-metadata">

**Author:** ![kapple](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kapple/32/218915_2.png) [@kapple](https://discourse.julialang.org/u/kapple)\
**Post date:** [October 9, 2020, 9:32am UTC](https://discourse.julialang.org/t/no-method-matching-oneunit-type-any-when-solving-matrix-equation/48020/11 "2020-10-09T09:32:52Z")

</div>

> [@Sukera](#):
>
> it’ll probably work better than hard-coding `Real` since the compiler can infer exactly what types are actually needed

Ah, good point. That makes total sense.

> [@Sukera](#):
>
> You can always put the hard work of generating those values into a function and call that in the comprehension

That… is a genius idea. Why didn’t I think of that.

> [@Sukera](#):
>
> Nevertheless, I think the compiler should be able to infer `Real` here 🤔

My original thoughts exactly. But let’s go with specific type specifications, it’s healthier for the code I suppose. 🙂

Thanks again @Sukera 😃

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [October 9, 2020, 9:36am UTC](https://discourse.julialang.org/t/no-method-matching-oneunit-type-any-when-solving-matrix-equation/48020/12 "2020-10-09T09:36:17Z")

</div>

[I’ve opened an issue](https://github.com/JuliaLang/julia/issues/37960).

> [@kapple](#):
>
> My original thoughts exactly. But let’s go with specific type specifications, it’s healthier for the code I suppose. 🙂

Well.. not if it unnecessarily restricts the type, like in your case 😅 If you change your mind or the return type of the function called in your comprehension changes, your code will recompile to fit those types and still be fast. If you keep it abstractly typed, you’ll incur performance overhead from having to actually check the runtime type of the elements.

---

<div class="post-metadata">

**Author:** ![kapple](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kapple/32/218915_2.png) [@kapple](https://discourse.julialang.org/u/kapple)\
**Post date:** [October 9, 2020, 9:42am UTC](https://discourse.julialang.org/t/no-method-matching-oneunit-type-any-when-solving-matrix-equation/48020/13 "2020-10-09T09:42:49Z")

</div>

Sorry you lost me for a bit there.

When you say

> [@Sukera](#):
>
> unnecessarily restricts the type

are you saying that when the matrix-vector product produced a vector of type `Any`, that _that’s_ the restriction?

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [October 9, 2020, 9:49am UTC](https://discourse.julialang.org/t/no-method-matching-oneunit-type-any-when-solving-matrix-equation/48020/14 "2020-10-09T09:49:34Z")

</div>

No, that’s not what I mean 🤔 In fact, “restricts” was the wrong word to use, it’s more like writing `Array{Real,1}` unnecessarily opens your the type of the array? As a result of that, `A*v` (wrongly) infers to `Any`, making everything down the road slower. Since you’re only (presumably) storing a small subset of all possible concrete types of `Real` at any one point, having the container be `Real` instead of e.g. `Union{Float64,Int64}` like it may be as a result of using the `[f(...) for _ in 1:n]` approach limits optimization to things that any `Real` can be. If it were `Union{Float64,Int64}`, the compiler would know that each element was 64 bits long and could make smarter decisions about how to make it fast. Otherwise it would have to take care of the cases where the element is e.g. `Int8`, which is much shorter with only 8 bits.

---

<div class="post-metadata">

**Author:** ![kapple](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kapple/32/218915_2.png) [@kapple](https://discourse.julialang.org/u/kapple)\
**Post date:** [October 9, 2020, 9:52am UTC](https://discourse.julialang.org/t/no-method-matching-oneunit-type-any-when-solving-matrix-equation/48020/15 "2020-10-09T09:52:46Z")

</div>

Ah, okay that makes sense.

I’ve subscribed to the issue you opened, I’m interested to see where this goes. But AFAI understand, what you’re suggesting totally makes sense - get the inference right so that we can continue writing code that will be automatically type stable, instead of becoming `Any`.

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [October 9, 2020, 10:27am UTC](https://discourse.julialang.org/t/no-method-matching-oneunit-type-any-when-solving-matrix-equation/48020/16 "2020-10-09T10:27:51Z")

</div>

> [@Sukera](#):
>
> Nevertheless, I think the compiler should be able to infer `Real` here

It _could_, but it’s a deliberate choice not to: [set default max\_methods to 3 by JeffBezanson · Pull Request #36208 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/36208). In this case,

```julia
julia> methods(+, (Int, Real))
# 7 methods for generic function "+":
[1] +(x::T, y::T) where T<:Union{Int128, Int16, Int32, Int64, Int8, UInt128, UInt16, UInt32, UInt64, UInt8} in Base at int.jl:87
[2] +(c::Union{Int16, Int32, Int64, Int8}, x::BigInt) in Base.GMP at gmp.jl:534
[3] +(c::Union{Int16, Int32, Int64, Int8}, x::BigFloat) in Base.MPFR at mpfr.jl:385
[4] +(a::Integer, b::Integer) in Base at int.jl:919
[5] +(x::T, y::T) where T<:Number in Base at promotion.jl:394
[6] +(y::Integer, x::Rational) in Base at rational.jl:295
[7] +(x::Number, y::Number) in Base at promotion.jl:321

julia> Base.return_types(+, (Int, Real))
7-element Vector{Any}:
 Int64
 BigInt
 BigFloat
 Any
 Union{}
 Rational
 Any

```

Here there are 5 concrete and 2 abstract applicable methods, well over the limit of 3.

---

<div class="post-metadata">

**Author:** ![kapple](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kapple/32/218915_2.png) [@kapple](https://discourse.julialang.org/u/kapple)\
**Post date:** [October 9, 2020, 10:32am UTC](https://discourse.julialang.org/t/no-method-matching-oneunit-type-any-when-solving-matrix-equation/48020/17 "2020-10-09T10:32:15Z")

</div>

I guess I’ll stick to `Float64`ing or `AbstractFloat`ing instead of `Real`ing then.

Thanks! 😃

---

<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:** [October 9, 2020, 10:49am UTC](https://discourse.julialang.org/t/no-method-matching-oneunit-type-any-when-solving-matrix-equation/48020/18 "2020-10-09T10:49:56Z")

</div>

I think that LinearAlgebra methods should work with any type if they contain valid numbers. Cf

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

---

<div class="post-metadata">

**Author:** ![kapple](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kapple/32/218915_2.png) [@kapple](https://discourse.julialang.org/u/kapple)\
**Post date:** [October 9, 2020, 10:56am UTC](https://discourse.julialang.org/t/no-method-matching-oneunit-type-any-when-solving-matrix-equation/48020/19 "2020-10-09T10:56:35Z")

</div>

> [@Tamas\_Papp](#):
>
> I think that LinearAlgebra methods should work with any type if they contain valid numbers.

Intuitively that’s what I initially thought would happen, with the philosophy that it would/should “just work”. But I also accept that there’s more going on within the internals, and will leave it to you guys to figure out a viable solution.

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [October 9, 2020, 2:32pm UTC](https://discourse.julialang.org/t/no-method-matching-oneunit-type-any-when-solving-matrix-equation/48020/20 "2020-10-09T14:32:57Z")

</div>

An alternative to

```julia
v = Vector{Real}(undef, 3)
for n = 1:length(v)
    v[n] = rand(Float64)
end

```

is

```julia
v = [rand(Float64) for n in 1:3]

```

(We’re assuming that `rand(Float64)` is a stand-in for some more complicated expression, possibly a function of `n`, etc.)

The latter syntax not only saves a bit of typing, it will also give a more narrow element type for `v`. In fact, even if you do use the first syntax to generate v, you could re-generate it afterwards using

```julia
v = [v[i] for i in eachindex(v)]

```

and that will narrow down the element type.
