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

Fix: Complex SubArray times real Matrix #29246

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 3 additions & 1 deletion stdlib/LinearAlgebra/src/matmul.jl
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,14 @@ function (*)(A::AbstractMatrix, B::AbstractMatrix)
mul!(similar(B, TS, (size(A,1), size(B,2))), A, B)
end
mul!(C::StridedMatrix{T}, A::StridedVecOrMat{T}, B::StridedVecOrMat{T}) where {T<:BlasFloat} = gemm_wrapper!(C, 'N', 'N', A, B)
# Complex Matrix times real matrix: We use that it is generally faster to reinterpret the
# first matrix as a real matrix and carry out real matrix matrix multiply
for elty in (Float32,Float64)
@eval begin
function mul!(C::StridedMatrix{Complex{$elty}}, A::StridedVecOrMat{Complex{$elty}}, B::StridedVecOrMat{$elty})
Afl = reinterpret($elty, A)
Cfl = reinterpret($elty, C)
gemm_wrapper!(Cfl, 'N', 'N', Afl, B)
mul!(Cfl, Afl, B)
return C
end
end
Expand Down
31 changes: 31 additions & 0 deletions stdlib/LinearAlgebra/test/matmul.jl
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,37 @@ end
@test *(Asub, adjoint(Asub)) == *(Aref, adjoint(Aref))
end

@testset "Complex matrix x real MatOrVec etc (issue #29224)" for T1 in (Float32,Float64)
for T2 in (Float32,Float64)
for arg1_real in (true,false)
@testset "Combination $T1 $T2 $arg1_real $arg2_real" for arg2_real in (true,false)
A0 = reshape(Vector{T1}(1:25),5,5) .+
(arg1_real ? 0 : 1im*reshape(Vector{T1}(-3:21),5,5))
A = view(A0,1:2,1:2)
Copy link
Member

@andreasnoack andreasnoack Sep 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe also add a test with view(A0,:,1:2) to test the code path that dispatches to BLAS.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

B = Matrix{T2}([1.0 3.0; -1.0 2.0]).+
(arg2_real ? 0 : 1im*Matrix{T2}([3.0 4; -1 10]))
AB_correct = copy(A)*B
AB = A*B; # view times matrix
@test AB ≈ AB_correct
A1 = view(A0,:,1:2) # rectangular view times matrix
@test A1*B ≈ copy(A1)*B
B1 = view(B,1:2,1:2);
AB1 = A*B1; # view times view
@test AB1 ≈ AB_correct
x = Vector{T2}([1.0;10.0]) .+ (arg2_real ? 0 : 1im*Vector{T2}([3;-1]))
Ax_exact = copy(A)*x
Ax = A*x # view times vector
@test Ax ≈ Ax_exact
x1 = view(x,1:2)
Ax1 = A*x1 # view times viewed vector
@test Ax1 ≈ Ax_exact
@test copy(A)*x1 ≈ Ax_exact # matrix times viewed vector
end
end
end
end


@testset "issue #15286" begin
A = reshape(map(Float64, 1:20), 5, 4)
C = zeros(8, 8)
Expand Down