# Feature or Bug? - Divide by zeros: 1/0 vs \[1\]/\[0\]

**URL:** <https://discourse.julialang.org/t/feature-or-bug-divide-by-zeros-1-0-vs-1-0/22960>\
**Category:** General Usage\
**Tags:** bug\
**Created:** [April 9, 2019, 2:24pm UTC](https://discourse.julialang.org/t/feature-or-bug-divide-by-zeros-1-0-vs-1-0/22960 "2019-04-09T14:24:03Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![bachrathyd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bachrathyd/32/7545_2.png) [@bachrathyd](https://discourse.julialang.org/u/bachrathyd)\
**Post date:** [April 9, 2019, 2:24pm UTC](https://discourse.julialang.org/t/feature-or-bug-divide-by-zeros-1-0-vs-1-0/22960/1 "2019-04-09T14:24:03Z")

</div>

Before I open an Issue, I would like to discuss this question.

The core of the question is the following difference:

```julia
julia> 1 / 0
Inf
julia> [1] / [0]
1×1 Array{Float64,2}:
0.0

```

In my [method](https://github.com/bachrathyd/MDBM.jl) this lead to a problem as discovered [here](https://github.com/bachrathyd/MDBM.jl/issues/16), because I use the left division with a non-square matrix, which can be full of zeros in some special situation.

I supposed that as `a` tends to zero ` [1]/[a]` tends to infinity

```julia
julia> [1] / [0.01]
1×1 Array{Float64,2}:
100.0
julia> [1] / [0.001]
1×1 Array{Float64,2}:
1000.0000000000001

```

So, I supposed that the limit case will provide Inf if `a` is `0.0`.

Furthermore, if squared matrix is used in the division:

```julia
julia> [1 0;0 1]/[1 0; 0 0]
ERROR: SingularException(2)

```

it leads to an error, and I think [0] is squared matrix.

I accept (but I am not happy) that `pinv` provides zeros for non-square matrices:

```julia
julia> [1 0;0 1; 0 0]/[1 0; 0 0; 0 0]
3×3 Array{Float64,2}:
1.0 0.0 0.0
0.0 0.0 0.0
0.0 0.0 0.0

```

`pinv` fullfils all the [definition](https://en.wikipedia.org/wiki/Moore%E2%80%93Penrose_inverse) and in the help it is clarifies:

`For matrices M with floating point elements, it is convenient to compute the pseudoinverse by inverting only singular values greater than rtol * maximum(svdvals(M)).`

I would prefer Inf (or error) if the number of “non-singular values” is smaller then the smaller size of the matix (e.g.: if `det(A * A')=0` ).

Note, that the same is problem can be found for left and right division (`/`,`\`). The help shows, that

```julia
help?> \
...
  \(A, B)
  Matrix division using a polyalgorithm. For input matrices A and B, the result X is such that A*X == B when A is square. 

```

but

```julia
julia> [0]\[1]
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:** [April 9, 2019, 3:04pm UTC](https://discourse.julialang.org/t/feature-or-bug-divide-by-zeros-1-0-vs-1-0/22960/2 "2019-04-09T15:04:12Z")

</div>

> [@bachrathyd](#):
>
> I think [0] is squared matrix.

I think you mean square matrix, but it isn’t. It’s a vector. For square matrices,

```julia
julia> ones(1, 1) / zeros(1, 1)
ERROR: LinearAlgebra.SingularException(1)
Stacktrace:
 [1] ldiv!(::LinearAlgebra.Diagonal{Float64,Array{Float64,1}}, ::Array{Float64,2}) at /home/tamas/src/julia-git/usr/share/julia/stdlib/v1.2/LinearAlgebra/src/diagonal.jl:482
 [2] \(::LinearAlgebra.Diagonal{Float64,Array{Float64,1}}, ::LinearAlgebra.Adjoint{Float64,Array{Float64,2}}) at /home/tamas/src/julia-git/usr/share/julia/stdlib/v1.2/LinearAlgebra/src/diagonal.jl:489
 [3] \(::LinearAlgebra.Adjoint{Float64,Array{Float64,2}}, ::LinearAlgebra.Adjoint{Float64,Array{Float64,2}}) at /home/tamas/src/julia-git/usr/share/julia/stdlib/v1.2/LinearAlgebra/src/generic.jl:962
 [4] /(::Array{Float64,2}, ::Array{Float64,2}) at /home/tamas/src/julia-git/usr/share/julia/stdlib/v1.2/LinearAlgebra/src/generic.jl:978
 [5] top-level scope at REPL[15]:1

```

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [April 9, 2019, 3:10pm UTC](https://discourse.julialang.org/t/feature-or-bug-divide-by-zeros-1-0-vs-1-0/22960/3 "2019-04-09T15:10:48Z")

</div>

It’s a feature, and it’s why I hate assigning linear algebra meaning to cases that could be ambiguous with element wise operation so much.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [April 9, 2019, 3:15pm UTC](https://discourse.julialang.org/t/feature-or-bug-divide-by-zeros-1-0-vs-1-0/22960/4 "2019-04-09T15:15:18Z")

</div>

And for previous discussion/complains of related issues:

[RowVector, pinv, and quasi-division by martinholters · Pull Request #23067 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/23067#discussion_r144685229) .  
[https://github.com/JuliaLang/julia/issues/25431](https://github.com/JuliaLang/julia/issues/25431) .  
[https://github.com/JuliaLang/julia/issues/28827#issuecomment-432414506](https://github.com/JuliaLang/julia/issues/28827#issuecomment-432414506) .

---

<div class="post-metadata">

**Author:** ![bachrathyd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bachrathyd/32/7545_2.png) [@bachrathyd](https://discourse.julialang.org/u/bachrathyd)\
**Post date:** [April 9, 2019, 3:25pm UTC](https://discourse.julialang.org/t/feature-or-bug-divide-by-zeros-1-0-vs-1-0/22960/5 "2019-04-09T15:25:41Z")

</div>

Thank you for the fast reply. Now, I see.  
I have realized that  
`pinv(A)`  
is not the same as  
`inv(a'*a)*a'`  
for matrices A if `rank(A) < minimum(size(A))` .  
In my code now I use the latter one, which behaves as I needed (and it seems to be faster in my case).

---

<div class="post-metadata">

**Author:** ![yha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yha/32/3502_2.png) [@yha](https://discourse.julialang.org/u/yha)\
**Post date:** [April 9, 2019, 7:27pm UTC](https://discourse.julialang.org/t/feature-or-bug-divide-by-zeros-1-0-vs-1-0/22960/6 "2019-04-09T19:27:19Z")

</div>

Isn’t that still a bug, since neither of `([1] / [0]) * [0]` and `[0] * ([0] \ [1])` are `[1]`?  
Seems to contradict what the docs promise, though they don’t say anything explicit about vectors.

---

<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:** [April 10, 2019, 6:16am UTC](https://discourse.julialang.org/t/feature-or-bug-divide-by-zeros-1-0-vs-1-0/22960/7 "2019-04-10T06:16:09Z")

</div>

> [@yha](#):
>
> neither of `([1] / [0]) * [0]` and `[0] * ([0] \ [1])` are `[1]` ?

I am not sure I understand what the issue is. The Moore-Penrose inverse of `[0]` is correctly calculated to be `[0.0]`, the rest follows from that.

---

<div class="post-metadata">

**Author:** ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)\
**Post date:** [April 10, 2019, 8:23am UTC](https://discourse.julialang.org/t/feature-or-bug-divide-by-zeros-1-0-vs-1-0/22960/8 "2019-04-10T08:23:07Z")

</div>

> [@Tamas\_Papp](#):
>
> The Moore-Penrose inverse of `[0]` is correctly calculated to be `[0.0]`

Is there a notion of a Moore-Penrose pseudo-inverse for _vectors_ (i.e. elements of \mathbb{R}^n, as opposed to matrices of \mathcal{M}\_{n,1}(\mathbb{R}))? I would be interested to see any reference explaining how such a thing would be defined.

Even if so, it could be argued that it is inconsistent for the `/` and `\` operators to compute (Moore-Penrose) pseudo-inverses for vectors, but fail with a `SingularException` for singular matrices.

  

> [@yuyichao](#):
>
> It’s a feature, and it’s why I hate assigning linear algebra meaning to cases that could be ambiguous with element wise operation so much.

I agree. I actually used to like this tendency to give many array operators a linear algebra meaning, but a few recent discussions gradually led me to like it less and less.

---

<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:** [April 10, 2019, 8:33am UTC](https://discourse.julialang.org/t/feature-or-bug-divide-by-zeros-1-0-vs-1-0/22960/9 "2019-04-10T08:33:27Z")

</div>

> [@ffevotte](#):
>
> Is there a notion of a Moore-Penrose pseudo-inverse for _vectors_

Not that I am aware of. Here Julia is treating that vector as a single-column matrix, consistently with some other operations.

---

<div class="post-metadata">

**Author:** ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)\
**Post date:** [April 10, 2019, 8:56am UTC](https://discourse.julialang.org/t/feature-or-bug-divide-by-zeros-1-0-vs-1-0/22960/10 "2019-04-10T08:56:19Z")

</div>

But as you noted yourself earlier in this thread, if `[0]` were to be treated as a matrix, it would be of size 1\times 1 (therefore square), and the very same operation would fail with a `SingularException`.

Everything can be considered consistent if `[0]` is considered to be a non-square matrix:

- matrix → inversion has a linear algebra meaning
- non-square → no real inverse can possibly be defined, hence defaulting to a pseudo-inverse

I can understand why vectors could be considered to be non-square matrices in general (i.e. when their size is not 1), and I don’t actually like it very much anymore, but this can be debatable (and has been debated, at length). But in any case, I think that treating a vector of size 1 as a non-square matrix leads to results that are very difficult to interpret. Wouldn’t you agree?

---

<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:** [April 10, 2019, 10:00am UTC](https://discourse.julialang.org/t/feature-or-bug-divide-by-zeros-1-0-vs-1-0/22960/11 "2019-04-10T10:00:58Z")

</div>

See the discussion of

> <https://github.com/JuliaLang/julia/pull/23067>
>
> No tests yet, I first would like feedback whether we actually want (all of) this….
> 
> Demo:
> \`\`\`julia
> julia\> c = \[1; 2\]
> 2-element Array{Int64,1}:
> 1
> 2
> 
> julia\> r = \[3; 4\]'
> 1×2 RowVector{Int64,Array{Int64,1}}:
> 3 4
> 
> julia\> s = r\*c
> 11
> 
> julia\> pinv(c) # returns Matrix on master
> 1×2 RowVector{Float64,Array{Float64,1}}:
> 0.2 0.4
> 
> julia\> pinv(c)\*c # returns Vector on master
> 1.0
> 
> julia\> pinv(r) # throws MethodError on master
> 2-element Array{Float64,1}:
> 0.12
> 0.16
> 
> julia\> s/c # throws MethodError on master
> 1×2 RowVector{Float64,Array{Float64,1}}:
> 2.2 4.4
> 
> julia\> r\\s # throws MethodError on master
> 2-element Array{Float64,1}:
> 1.32
> 1.76
> \`\`\`
> 
> Closes #23028.

for various angles.

Personally, I think that `\` and `/` for anything but division (ie the inverse of `*`) is a bad pun, and would prefer if `pinv` and least squares happened only when the user explicitly asks for them.

---

<div class="post-metadata">

**Author:** ![yha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yha/32/3502_2.png) [@yha](https://discourse.julialang.org/u/yha)\
**Post date:** [April 10, 2019, 1:46pm UTC](https://discourse.julialang.org/t/feature-or-bug-divide-by-zeros-1-0-vs-1-0/22960/12 "2019-04-10T13:46:17Z")

</div>

> [@Tamas\_Papp](#):
>
> I am not sure I understand what the issue is. The Moore-Penrose inverse of `[0]` is correctly calculated to be `[0.0]` , the rest follows from that.

The docs of `\` and `/` do not mention the Moore-Penrose inverse, but instead say that `A\B` is an `X` such `A*X=B`, which is not generally the same as `pinv(A)*B`. So at the very least it’s a documentation issue.  
(I also agree that

> [@Tamas\_Papp](#):
>
> I think that `\` and `/` for anything but division (ie the inverse of `*` ) is a bad pun

)
