Skip to content

Promote hcat vcat #158

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

Merged
merged 3 commits into from
May 11, 2017
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
6 changes: 3 additions & 3 deletions src/linalg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ end

return quote
@_inline_meta
@inbounds return similar_type(a, Size($Snew))(tuple($(exprs...)))
@inbounds return similar_type(a, promote_type(eltype(a), eltype(b)), Size($Snew))(tuple($(exprs...)))
end
end
# TODO make these more efficient
Expand All @@ -129,7 +129,7 @@ end

return quote
@_inline_meta
@inbounds return similar_type(a, Size($Snew))(tuple($(exprs...)))
@inbounds return similar_type(a, promote_type(eltype(a), eltype(b)), Size($Snew))(tuple($(exprs...)))
end
end
# TODO make these more efficient
Expand Down Expand Up @@ -297,8 +297,8 @@ end
end
end

# TODO same for `RowVector`?
@inline Size(::Union{RowVector{T, SA}, Type{RowVector{T, SA}}}) where {T, SA <: StaticArray} = Size(1, Size(SA)[1])
@inline Size(::Union{RowVector{T, CA}, Type{RowVector{T, CA}}} where CA <: ConjVector{<:Any, SA}) where {T, SA <: StaticArray} = Size(1, Size(SA)[1])
Copy link
Member Author

Choose a reason for hiding this comment

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

Oops. This line of code was meant for #157. I suggest we review all changes here and just merge both (or this).

Copy link
Member

Choose a reason for hiding this comment

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

I'll review it all here.

@inline Size(::Union{Symmetric{T,SA}, Type{Symmetric{T,SA}}}) where {T,SA<:StaticArray} = Size(SA)
@inline Size(::Union{Hermitian{T,SA}, Type{Hermitian{T,SA}}}) where {T,SA<:StaticArray} = Size(SA)

Expand Down
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, <: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, <: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, <: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
3 changes: 3 additions & 0 deletions test/linalg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@

@test @inferred(vcat(SVector(1),SVector(2),SVector(3),SVector(4))) === SVector(1,2,3,4)
@test @inferred(hcat(SVector(1),SVector(2),SVector(3),SVector(4))) === SMatrix{1,4}(1,2,3,4)

vcat(SVector(1.0f0), SVector(1.0)) === SVector(1.0, 1.0)
hcat(SVector(1.0f0), SVector(1.0)) === SMatrix{1,2}(1.0, 1.0)
end

@testset "normalization" begin
Expand Down
9 changes: 9 additions & 0 deletions test/matrix_multiply.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@
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 = SVector(1, 2)
v3 = SVector(3, 4)
@test v2 * v3' === @SMatrix [3 4; 6 8]

v4 = SVector(1+0im, 2+0im)
v5 = SVector(3+0im, 4+0im)
@test v4 * v5' === @SMatrix [3+0im 4+0im; 6+0im 8+0im]
end

@testset "Matrix-matrix" begin
Expand Down