JuliaLang / julia

The Julia Programming Language
https://julialang.org/
MIT License
45.77k stars 5.49k forks source link

Let `//` support matrix arguments #52870

Open putianyi889 opened 10 months ago

putianyi889 commented 10 months ago

Since // supports Complex{Integer} and / supports Matrix{Rational}, it's intuitive that // should support Matrix{Integer} as well.

A workaround for now is (A // 1) / (B // 1).

julia> rand(1:9,3,3) // 1 / (rand(1:9,3,3) // 1)
3×3 Matrix{Rational{Int64}}:
 -85//31   53//31   121//31
  14//31   -8//31    18//31
 197//93  -55//93  -163//93
mikmoore commented 10 months ago

I don't find this intuitive, so I'll ask for clarification: you want A // B == (A // 1) / (B // 1) in your example?

I disagree. In my opinion, // should construct a Rational (which AbstractMatrix{<:Rational} is not). I think it's a big stretch (and an unnecessary pun) to make it do matrix right-division.

putianyi889 commented 10 months ago

you want A // B == (A // 1) / (B // 1) in your example?

I'd say A // B == (A // one(eltype(A))) / (B // one(eltype(B))), but you get the point

// should construct a Rational (which AbstractMatrix{<:Rational}

There is a case where it doesn't.

https://github.com/JuliaLang/julia/blob/5b6a94da5af35a4aa91759cac2f8db7669a6ec2a/base/rational.jl#L93

nsajko commented 10 months ago

There is a case where it doesn't.

IMO that method is different than this, because the method implements the idea of scalar multiplication, which is a simpler operation than matrix multiplication with inverse. The semantics you propose feel nonobvious.