Skip to content

Commit

Permalink
define dot between AbstractMatrix and UniformScaling (JuliaLang#40250)
Browse files Browse the repository at this point in the history
* define dot between AbstractMatrix and UniformScaling

* fix equality check

* fix for complex

* add news entry
  • Loading branch information
lkapelevich authored and ElOceanografo committed May 4, 2021
1 parent 0ed4c1b commit 09ff96f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ Standard library changes
* SuiteSparse is updated to 5.8.1. ([#39455])
* The shape of an `UpperHessenberg` matrix is preserved under certain arithmetic operations, e.g. when multiplying or dividing by an `UpperTriangular` matrix. ([#40039])
* `cis(A)` now supports matrix arguments ([#40194]).
* `dot` now supports `UniformScaling` with `AbstractMatrix` ([#40250]).

#### Markdown

Expand Down
3 changes: 3 additions & 0 deletions stdlib/LinearAlgebra/src/uniformscaling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,9 @@ Array(s::UniformScaling, dims::Dims{2}) = Matrix(s, dims)
Diagonal{T}(s::UniformScaling, m::Integer) where {T} = Diagonal{T}(fill(T(s.λ), m))
Diagonal(s::UniformScaling, m::Integer) = Diagonal{eltype(s)}(s, m)

dot(A::AbstractMatrix, J::UniformScaling) = dot(tr(A), J.λ)
dot(J::UniformScaling, A::AbstractMatrix) = dot(J.λ, tr(A))

dot(x::AbstractVector, J::UniformScaling, y::AbstractVector) = dot(x, J.λ, y)
dot(x::AbstractVector, a::Number, y::AbstractVector) = sum(t -> dot(t[1], a, t[2]), zip(x, y))
dot(x::AbstractVector, a::Union{Real,Complex}, y::AbstractVector) = a*dot(x, y)
Expand Down
14 changes: 14 additions & 0 deletions stdlib/LinearAlgebra/test/uniformscaling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,20 @@ end
@test I(3) == [1 0 0; 0 1 0; 0 0 1]
end

@testset "dot" begin
A = randn(3, 3)
λ = randn()
J = UniformScaling(λ)
@test dot(A, J) dot(J, A)
@test dot(A, J) tr(A' * J)

A = rand(ComplexF64, 3, 3)
λ = randn() + im * randn()
J = UniformScaling(λ)
@test dot(A, J) conj(dot(J, A))
@test dot(A, J) tr(A' * J)
end

@testset "generalized dot" begin
x = rand(-10:10, 3)
y = rand(-10:10, 3)
Expand Down

0 comments on commit 09ff96f

Please sign in to comment.