# Should scalar divided by matrix be allowed?

**URL:** https://discourse.julialang.org/t/should-scalar-divided-by-matrix-be-allowed/23502
**Category:** Internals & Design
**Created:** [April 25, 2019, 10:07am UTC](https://discourse.julialang.org/t/should-scalar-divided-by-matrix-be-allowed/23502 "2019-04-25T10:07:15Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![bsxfan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bsxfan/32/6914_2.png) [@bsxfan](https://discourse.julialang.org/u/bsxfan)
#### Post date: [April 25, 2019, 10:07am UTC](https://discourse.julialang.org/t/should-scalar-divided-by-matrix-be-allowed/23502/1 "2019-04-25T10:07:15Z")

</div>

Given a square (invertible) matrix `D` and a scalar `s`, the operations `s/D` and `D\s` are not allowed in Julia. I’d like to give some arguments both in favour and against allowing these operations. Some related discussion can be found [here](https://discourse.julialang.org/t/scalar-divided-by-range-or-array/8196/6) and [here](https://github.com/JuliaLang/julia/issues/25431). (My interest in this question stems from our current work on a macro that allows a convenient syntax for calling inplace operations such as `BLAS.trsm`.)

Although `s\D` and `D/s` are disallowed, all of the following _can_ be done successfully in Julia:

```julia
using LinearAlgebra
D = randn(3,3); N = randn(3,3); s = randn()
inv(D)*s
D \ (s*N)
D \ 2N
D \ N*s
s*inv(D)
N*s/D

```

however, the following variants _fail_:

```julia
D \ s*N
D \ 2*N

```

because they are parsed from left to right and attempt `D\s` before multiplying by `N`.

As pointed out in the discussions cited above, _a scalar can be divided by a vector_, which results in (a multiple of) the pseudoinverse of the vector. That is, the following _can_ be done successfully:

```julia
v = randn(3)
@assert s/v ≈ s*pinv(v)
@assert v'\s ≈ s*pinv(v')

```

However, the following variants _fail_:

```julia
v \ s
s / v'
s / randn(2,3)

```

even though all of the above denominators do have pseudoinverses (the pseudoinverse always exists).

My argument in favour of more generally allowing the division of scalars by matrices is therefore one of _consistency_.

However, there is also a good argument for the case against: In general, `D\(s*N)` is both faster and more accurate than `inv(D)*s*N`. If `D\s` were to be implemented as `s*inv(D)`, or `s*pinv(D)`, then `D\s*N` would end up doing the slower, inaccurate calculation. (The MATLAB editor will usually warn the programmer about attempts to multiply inverses.)

I think therefore, for fast accurate Julia, scalar divided by matrix should remain disallowed and perhaps to improve consistency, scalar / vector should be deprecated.

---

<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 25, 2019, 11:33am UTC](https://discourse.julialang.org/t/should-scalar-divided-by-matrix-be-allowed/23502/2 "2019-04-25T11:33:24Z")

</div>

> [@bsxfan](#):
>
> My argument in favour of more generally allowing the division of scalars by matrices is therefore one of _consistency_ .

In the name of consistency, instead of extending it, I would suggest removing the pseudoinverse `/` magic for vectors. It is a bad pun, and just causes confusion. Cf

> [@Feature or Bug? - Divide by zeros: 1/0 vs \[1\]/\[0\]](https://discourse.julialang.org/t/feature-or-bug-divide-by-zeros-1-0-vs-1-0/22960):
>
> Before I open an Issue, I would like to discuss this question. The core of the question is the following difference: 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\> [1] / [0.01] 1×1 Array{Float64,2}: 100.0 julia\> [1] / [0.001] 1×1 Array{Float64,2…

Users who want a pseudoinverse should explicitly ask for it.

Also, are you aware of `LinearAlgebra.I`? Eg `(2*I)/randn(3,3)` etc.

---

<div class="post-metadata">

### Author: ![bsxfan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bsxfan/32/6914_2.png) [@bsxfan](https://discourse.julialang.org/u/bsxfan)
#### Post date: [April 25, 2019, 11:48am UTC](https://discourse.julialang.org/t/should-scalar-divided-by-matrix-be-allowed/23502/3 "2019-04-25T11:48:18Z")

</div>

@Tamas_Papp, Please see the last paragraph in my post—we are in agreement about deprecation of scalar divided by vector. But thanks also for the other example, which reinforces the case for this deprecation.

---

<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 25, 2019, 11:58am UTC](https://discourse.julialang.org/t/should-scalar-divided-by-matrix-be-allowed/23502/4 "2019-04-25T11:58:27Z")

</div>

Thanks, I missed that. The way your post is organized is a bit confusing: first you argue for something we don’t have, then conclude we don’t need it.

---

<div class="post-metadata">

### Author: ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)
#### Post date: [April 27, 2019, 8:36pm UTC](https://discourse.julialang.org/t/should-scalar-divided-by-matrix-be-allowed/23502/5 "2019-04-27T20:36:48Z")

</div>

Agree 💯 as someone who was never a Matlab user the behaviour of \ is strange and inconsistent: it’s annoying that it returns a wrong answer instead of erroring out or returning `NanN/Inf` when you really want the inverse, and it’s not not even always the pseudo-inverse: sometimes it does error out when you _do_ want the pseudo-inverse!
