# Scalar divided by range or array

**URL:** <https://discourse.julialang.org/t/scalar-divided-by-range-or-array/8196>\
**Category:** General Usage\
**Tags:** question, bug, array\
**Created:** [January 6, 2018, 10:41am UTC](https://discourse.julialang.org/t/scalar-divided-by-range-or-array/8196 "2018-01-06T10:41:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mlhetland](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mlhetland/32/283_2.png) [@mlhetland](https://discourse.julialang.org/u/mlhetland)\
**Post date:** [January 6, 2018, 10:41am UTC](https://discourse.julialang.org/t/scalar-divided-by-range-or-array/8196/1 "2018-01-06T10:41:15Z")

</div>

Mostly, it seems that using scalars and arithmetic operations with arrays and ranges gives me what I suspect. However, dividing a scalar by an array is not defined, whereas the elementwise version is (as opposed to other operators, where both versions work). And for ranges, it _does_ work, but gives what seems to me to be the wrong answer, whether I use the elementwise version or not.

That is:

```julia
julia> [1, 2, 3] / 1
3-element Array{Float64,1}:
 1.0
 2.0
 3.0
julia> 1 / [1, 2, 3]
ERROR: MethodError: no method matching /(::Int64, ::Array{Int64,1})
...
julia> 1 ./ [1, 2, 3]
3-element Array{Float64,1}:
 1.0
 0.5
 0.333333
julia> 1 / 1:3
1.0:1.0:3.0
julia> 1 ./ 1:3
1.0:1.0:3.0

```

To clarify, my expectation would be that `1 / [1, 2, 3]` would yield the same result as `1 ./ [1, 2, 3]`, and that applying either `/` or `./` to a range would yield the same result whether I used `collect` before or after.

Are these expectations unfounded, or is this perhaps a bug (or simply some versions of the operators, with the range reverting to a default not intended for this purpose)?

---

<div class="post-metadata">

**Author:** ![mlhetland](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mlhetland/32/283_2.png) [@mlhetland](https://discourse.julialang.org/u/mlhetland)\
**Post date:** [January 6, 2018, 10:43am UTC](https://discourse.julialang.org/t/scalar-divided-by-range-or-array/8196/2 "2018-01-06T10:43:26Z")

</div>

Aaaaaah … I see my error wrt. the range, now. I guess it’s just a matter of precedence xD But the question about elementwise array division remains, I guess.

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [January 6, 2018, 11:47am UTC](https://discourse.julialang.org/t/scalar-divided-by-range-or-array/8196/3 "2018-01-06T11:47:34Z")

</div>

In v0.7 it returns some nonsense:

```julia
julia> 1/[1,2,3]
1×3 Transpose{Float64,Array{Float64,1}}:
 0.0714286 0.142857 0.214286

```

Opened an issue: [https://github.com/JuliaLang/julia/issues/25431](https://github.com/JuliaLang/julia/issues/25431).

Edited: I was wrong, it is not nonsense, it is actually the pseudoinverse, see the issue for more details.

---

<div class="post-metadata">

**Author:** ![andyferris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andyferris/32/235_2.png) [@andyferris](https://discourse.julialang.org/u/andyferris)\
**Post date:** [January 6, 2018, 11:54am UTC](https://discourse.julialang.org/t/scalar-divided-by-range-or-array/8196/4 "2018-01-06T11:54:29Z")

</div>

Yes great question.

Elementwise operations on containers _must_ use the elementwise (broadcasted) version like `./` and `.+` etc.

We include `array + array` (`-` too) and `scalar * array` (also division by scalar) _only_ because these “whole container” operations make sense for vector spaces in linear algebra.

We’ve been deprecating all the other methods in favor of the “dot” version slowly over several revisions. One that slipped through was a buggy `scalar / range` which has been deprecated in Julia v0.7.

---

<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:** [January 6, 2018, 3:39pm UTC](https://discourse.julialang.org/t/scalar-divided-by-range-or-array/8196/5 "2018-01-06T15:39:57Z")

</div>

> [@mohamed82008](#):
>
> it is not nonsense, it is actually the pseudoinverse, see the issue for more details.

It is not nonsense, but definitely surprising. I missed the original PR, so I guess it is too late to argue against this now. On the one hand I appreciate the mathematical elegance, OTOH this looks like something that can be invoked accidentally very easily (eg missing a `.` in `./`, etc). It may be surprising to new users of the language who don’t expect this.

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [January 6, 2018, 4:39pm UTC](https://discourse.julialang.org/t/scalar-divided-by-range-or-array/8196/6 "2018-01-06T16:39:49Z")

</div>

> [@Tamas\_Papp](#):
>
> OTOH this looks like something that can be invoked accidentally very easily (eg missing a . in ./, etc). It may be surprising to new users of the language who don’t expect this.

Exactly!

I can see this might be a logical consequence of treating scalars and 1x1 matrices as equivalents though. For example, when one wants to solve the system x^TA=b where A happens to be a single column vector (matrix) and b is therefore in this shady area of being a 1x1 matrix in some contexts and a scalar in others. In that case, right “dividing” by A on both sides might be a mathematically elegant way to get the solution for x. So there seems to be some rationale behind it but it’s rather subtle at first sight. Obviously, 1 element vectors are also in the same shady club.
