Skip to content

Capture complex outer product #157

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

Closed
wants to merge 1 commit into from
Closed
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
14 changes: 14 additions & 0 deletions src/matrix_multiply.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ promote_matprod{T1,T2}(::Type{T1}, ::Type{T2}) = typeof(zero(T1)*zero(T2) + zero
@inline *(A::StaticMatrix, B::StaticMatrix) = _A_mul_B(Size(A), Size(B), A, B)
@inline *(A::StaticVector, B::StaticMatrix) = *(reshape(A, Size(Size(A)[1], 1)), B)
@inline *(A::StaticVector, B::RowVector{<:Any, <:StaticVector}) = _A_mul_B(Size(A), Size(B), A, B)
@inline *(A::StaticVector, B::RowVector{<:Any, CV} where CV <: ConjVector{<:Any, <:StaticVector}) = _A_mul_B(Size(A), Size(B), A, B)

@inline A_mul_B!(dest::StaticVecOrMat, A::StaticMatrix, B::StaticVector) = _A_mul_B!(Size(dest), dest, Size(A), Size(B), A, B)
@inline A_mul_B!(dest::StaticVecOrMat, A::StaticMatrix, B::StaticMatrix) = _A_mul_B!(Size(dest), dest, Size(A), Size(B), A, B)
@inline A_mul_B!(dest::StaticVecOrMat, A::StaticVector, B::StaticMatrix) = A_mul_B!(dest, reshape(A, Size(Size(A)[1], 1)), B)
@inline A_mul_B!(dest::StaticVecOrMat, A::StaticVector, B::RowVector{<:Any, <:StaticVector}) = _A_mul_B!(Size(dest), dest, Size(A), Size(B), A, B)
@inline A_mul_B!(dest::StaticVecOrMat, A::StaticVector, B::RowVector{<:Any, CV} where CV <: ConjVector{<:Any, <:StaticVector}) = _A_mul_B!(Size(dest), dest, Size(A), Size(B), A, B)

#@inline *{TA<:Base.LinAlg.BlasFloat,Tb}(A::StaticMatrix{TA}, b::StaticVector{Tb})

Expand Down Expand Up @@ -93,6 +95,18 @@ end
end
end

# complex outer product
@generated function _A_mul_B(::Size{sa}, ::Size{sb}, a::StaticVector{<: Any, Ta}, b::RowVector{Tb, CV} where CV <: ConjVector{<:Any, <:StaticVector}) where {sa, sb, Ta, Tb}
newsize = (sa[1], sb[2])
exprs = [:(a[$i]*b[$j]) for i = 1:sa[1], j = 1:sb[2]]

return quote
@_inline_meta
T = promote_op(*, Ta, Tb)
@inbounds return similar_type(b, T, Size($newsize))(tuple($(exprs...)))
end
end

@generated function _A_mul_B(Sa::Size{sa}, Sb::Size{sb}, a::StaticMatrix{<:Any, <:Any, Ta}, b::StaticMatrix{<:Any, <:Any, Tb}) where {sa, sb, Ta, Tb}
# Heuristic choice for amount of codegen
if sa[1]*sa[2]*sb[2] <= 8*8*8
Expand Down
7 changes: 7 additions & 0 deletions test/matrix_multiply.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@
m = @SMatrix [1 2 3 4]
v = @SVector [1, 2]
@test @inferred(v*m) === @SMatrix [1 2 3 4; 2 4 6 8]

# Outer product
v2 = zeros(SVector{3, Int})
@test v2 * v2' === zeros(SMatrix{3, 3, Int})

v3 = zeros(SVector{3, Complex{Int}})
@test v3 * v3' === zeros(SMatrix{3, 3, Complex{Int}})
end

@testset "Matrix-matrix" begin
Expand Down