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

define dot between AbstractMatrix and UniformScaling #40250

Merged
merged 5 commits into from
Apr 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ Standard library changes
* OpenBLAS is updated to 0.3.13. ([#39216])
* SuiteSparse is updated to 5.8.1. ([#39455])
* `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