Skip to content

Fix & test * with matrix-of-adjoint #1304

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
32 changes: 32 additions & 0 deletions src/matrix_multiply_add.jl
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,38 @@
return _mul!(TSize(dest), mul_parent(dest), Size(A), Size(B), A, B, NoMulAdd{TMul, TDest}())
end

@static if Base.VERSION < v"1.11"
let
function mulfix!(dest, A, B)
axdest, axA, axB = axes(dest), axes(A), axes(B)
axA[2] == axB[1] || throw(DimensionMismatch("Tried to multiply arrays with axes $axA and $axB"))
axdest == (axA[1], axB[2]) || throw(DimensionMismatch("Tried to multiply arrays with axes $axA and $axB and assign to array with axes $axdest"))
fill!(dest, zero(eltype(dest)))
# This is not maximally efficient, but it produces the right answer on older Julia versions.
for ij in CartesianIndices(dest)
for k in axA[2]
dest[ij] += A[ij[1], k] * B[k, ij[2]]
end
end

Check warning on line 263 in src/matrix_multiply_add.jl

View check run for this annotation

Codecov / codecov/patch

src/matrix_multiply_add.jl#L262-L263

Added lines #L262 - L263 were not covered by tests
return dest
end
function LinearAlgebra.mul!(

Check warning on line 266 in src/matrix_multiply_add.jl

View check run for this annotation

Codecov / codecov/patch

src/matrix_multiply_add.jl#L266

Added line #L266 was not covered by tests
dest::AbstractMatrix{<:StaticMatrix},
A::LinearAlgebra.AbstractTriangular{<:StaticVector},
B::Adjoint{Adjoint{Float64, V}, <:AbstractMatrix{V}}
) where V<:StaticVector
mulfix!(dest, A, B)

Check warning on line 271 in src/matrix_multiply_add.jl

View check run for this annotation

Codecov / codecov/patch

src/matrix_multiply_add.jl#L271

Added line #L271 was not covered by tests
end
function LinearAlgebra.mul!(

Check warning on line 273 in src/matrix_multiply_add.jl

View check run for this annotation

Codecov / codecov/patch

src/matrix_multiply_add.jl#L273

Added line #L273 was not covered by tests
dest::AbstractMatrix{<:StaticMatrix},
A::AbstractMatrix{<:StaticVector},
B::Adjoint{Adjoint{Float64, V}, <:AbstractMatrix{V}}
) where V<:StaticVector
mulfix!(dest, A, B)
end
end
end

"""
multiplied_dimension(A, B)

Expand Down
12 changes: 11 additions & 1 deletion test/matrix_multiply_add.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using StaticArrays
using LinearAlgebra
using BenchmarkTools
using Random
using Test

macro test_noalloc(ex)
Expand Down Expand Up @@ -226,7 +227,7 @@ function test_wrappers_for_size(N, test_block)
A_block = rand(SMatrix{N,N,SMatrix{2,2,Int,4}})
B_block = rand(SMatrix{N,N,SMatrix{2,2,Int,4}})
bv_block = rand(SVector{N,SMatrix{2,2,Int,4}})

# matrix-vector
for wrapper in mul_add_wrappers
# LinearAlgebra can't handle these
Expand All @@ -252,3 +253,12 @@ end
test_wrappers_for_size(8, false)
test_wrappers_for_size(16, false)
end

@testset "Adjoints and covariances" begin
X = [randn(SVector{3,Float64}) for _ in CartesianIndices((1:2, 1:3))]
μ = sum(X; dims=2)/size(X, 2)
ΔX = X .- μ
@test (ΔX*ΔX')[1, 1] ≈ sum(dx * dx' for dx in ΔX[1, :])
B = [randn(SVector{3,Float64}) for _ in CartesianIndices((1:2, 1:2))]
@test_throws DimensionMismatch ΔX * B'
end
Loading