Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

`\(::UniformScaling, ::Diagonal{T,Array{T,1}})` returns dense `Matrix` when `T` is abstract #37359

Open
StevenWhitaker opened this issue Sep 2, 2020 · 2 comments · May be fixed by #37504
Open

`\(::UniformScaling, ::Diagonal{T,Array{T,1}})` returns dense `Matrix` when `T` is abstract #37359

StevenWhitaker opened this issue Sep 2, 2020 · 2 comments · May be fixed by #37504

Comments

@StevenWhitaker
Copy link

@StevenWhitaker StevenWhitaker commented Sep 2, 2020

As stated in the title, \(::UniformScaling, ::Diagonal{T,Array{T,1}}) returns a dense Matrix when T is abstract. It seems that, ideally, a Diagonal matrix should be returned (as is the case when T is concrete). (This is for the case when T <: Number is true; I haven't looked at what happens when T <: Number is false.)

julia> versioninfo()
Julia Version 1.5.1
Commit 697e782ab8 (2020-08-25 20:08 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-9.0.1 (ORCJIT, skylake)
Environment:
  JULIA_NUM_THREADS = 8

julia> I \ Diagonal(ones(Integer, 4)) # T abstract --> returns Array
4×4 Array{Float64,2}:
 1.0  0.0
 0.0  1.0  0.0
 0.0  0.0  1.0  0.0
 0.0  0.0  1.0

julia> I \ Diagonal(ones(Int, 4)) # T concrete --> returns Diagonal
4×4 Diagonal{Float64,Array{Float64,1}}:
 1.0        
     1.0        
         1.0    
         1.0
@martinholters
Copy link
Member

@martinholters martinholters commented Sep 3, 2020

Fix should be as simple as:

--- a/stdlib/LinearAlgebra/src/diagonal.jl
+++ b/stdlib/LinearAlgebra/src/diagonal.jl
@@ -174,6 +174,7 @@ end
 (*)(x::Number, D::Diagonal) = Diagonal(x * D.diag)
 (*)(D::Diagonal, x::Number) = Diagonal(D.diag * x)
 (/)(D::Diagonal, x::Number) = Diagonal(D.diag / x)
+(\)(x::Number, D::Diagonal) = Diagonal(x \ D.diag)
 
 function (*)(Da::Diagonal, Db::Diagonal)
     nDa, mDb = size(Da, 2), size(Db, 1)

If someone wants to try this out and make a PR out of it - go for it!

tantei3 added a commit to tantei3/julia that referenced this issue Sep 10, 2020
tantei3 added a commit to tantei3/julia that referenced this issue Sep 10, 2020
tantei3 added a commit to tantei3/julia that referenced this issue Sep 10, 2020
@dkarrasch
Copy link
Member

@dkarrasch dkarrasch commented Sep 11, 2020

While @mbauman, maybe there is something else to be improved here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

3 participants
You can’t perform that action at this time.

Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant