Julia: 1 / [1,2,3] returns nonsense

Created on 6 Jan 2018  Â·  9Comments  Â·  Source: JuliaLang/julia

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

julia> versioninfo()
Julia Version 0.7.0-DEV.3234
Commit 2cc82d29e1* (2018-01-02 11:44 UTC)
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
  WORD_SIZE: 64
  BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Haswell)
  LAPACK: libopenblas64_
  LIBM: libopenlibm
  LLVM: libLLVM-3.9.1 (ORCJIT, skylake)

Most helpful comment

I know about pinv, but I never expected it show up in that context, i.e. dividing a scalar by a vector. I am also not sure how many people who know about pinv would expect it to pop up in such a context. So I wonder if this is the most intuitive/correct behavior, might be a trade-off between the 2.

All 9 comments

You're looking at the Moore-Penrose pseudoinverse:

julia> (1/[1,2,3]) * [1,2,3]
0.9999999999999999

https://docs.julialang.org/en/stable/stdlib/linalg/#Base.LinAlg.pinv

The way to find out out about these things is by calling

julia> @less 1 / [1,2,3]

which will point you to pinv. The documentation for that explains what you are looking at.

Thanks for opening an issue, @mohamed82008 ! @andreasnoack is right in closing this, though.

I know about pinv, but I never expected it show up in that context, i.e. dividing a scalar by a vector. I am also not sure how many people who know about pinv would expect it to pop up in such a context. So I wonder if this is the most intuitive/correct behavior, might be a trade-off between the 2.

It's pretty surprising to me too. @martinholters, why was scalar / matrix defined as scalar * pinv(matrix) in #23067? That PR was mostly about rowvector, but don't understand how that affects scalar/matrix operations.

There is no matrix here. It is ([1,2,3]' \ 1)', i.e. it involves what used to be a rowvector and a scalar. It solves the underdetermined system x1 + 2x2 + 3x3 = 1 and then transposes the result.

@andreasnoack, we didn't used to allow solving systems by rowvector \ scalar. In all previous Julia versions, [1 2 3] \ 1 threw an error. You had to write [1 2 3] \ [1] or [1, 2, 3]' \ [1] if you wanted to solve the underdetermined system.

I realize that we can assign this a meaning, but I'm not sure we should. An expression like this seems more likely to be a user error...

Reason for allowing it is that rowvector*vector gives a scalar and it seems natural for the inverse operation - the division - to work, at least in the sense it works for matrices.

I see. To put it another way, you want to have associativity (a' \ b') * c ≈ a' \ (b' * c) for three vectors a,b,c.

Tracking down this thread to figure out exactly what 1 / [1,2,3] was doing took a long time for me today. Is this noted anywhere in the official docs? Also the example of using @less that @tkluck gave doesn't work for this particular example any more.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

arshpreetsingh picture arshpreetsingh  Â·  3Comments

omus picture omus  Â·  3Comments

manor picture manor  Â·  3Comments

Keno picture Keno  Â·  3Comments

tkoolen picture tkoolen  Â·  3Comments