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

[WIP] add lmul! #33

Open
wants to merge 1 commit 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: 19 additions & 13 deletions src/functions_math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ end


for (NA, NB) in ((1,2), (2,1), (2,2)) #Vector * Vector, is not allowed
@eval function Base.:*(a::NamedDimsArray{A,T,$NA}, b::NamedDimsArray{B,S,$NB}) where {A,B,T,S}
L = matrix_prod_names(A,B)
data = *(parent(a), parent(b))
return NamedDimsArray{L}(data)
for func in (:(Base.:*), :(LinearAlgebra.lmul!))
@eval function $func(a::NamedDimsArray{A,T,$NA}, b::NamedDimsArray{B,S,$NB}) where {A,B,T,S}
L = matrix_prod_names(A,B)
data = $func(parent(a), parent(b))
return NamedDimsArray{L}(data)
end
end
end

Expand All @@ -40,23 +42,27 @@ It defines the various overloads for `Base.:*` that are required.
It should be used at the top level of a module.
"""
macro declare_matmul(MatrixT, VectorT=nothing)
codes = Expr(:block)
dim_combos = VectorT === nothing ? ((2,2),) : ((1,2), (2,1), (2,2))
codes = map(dim_combos) do (NA, NB)

for (NA, NB) in dim_combos
TA_named = :(NamedDims.NamedDimsArray{<:Any, <:Any, $NA})
TB_named = :(NamedDims.NamedDimsArray{<:Any, <:Any, $NB})
TA_other = (VectorT, MatrixT)[NA]
TB_other = (VectorT, MatrixT)[NB]

quote
function Base.:*(a::$TA_named, b::$TB_other)
return *(a, NamedDims.NamedDimsArray{NamedDims.names(b)}(b))
end
function Base.:*(a::$TA_other, b::$TB_named)
return *(NamedDims.NamedDimsArray{NamedDims.names(a)}(a), b)
for func in (:(Base.:*), :(LinearAlgebra.lmul!))
code = quote
function $func(a::$TA_named, b::$TB_other)
return $func(a, NamedDims.NamedDimsArray{NamedDims.names(b)}(b))
end
function $func(a::$TA_other, b::$TB_named)
return $func(NamedDims.NamedDimsArray{NamedDims.names(a)}(a), b)
end
end
push!(codes.args, code)
end
end
return esc(Expr(:block, codes...))
return esc(codes)
end

@declare_matmul(AbstractMatrix, AbstractVector)
Expand Down
11 changes: 11 additions & 0 deletions test/functions_math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,14 @@ end
@test names(nda * x) == (:a, :_)
end
end


@testset "mldivide" begin
outputs = NamedDimsArray{(:variates, :observations)}(randn(3, 10))
inputs = NamedDimsArray{(:features, :observations)}(rand(4, 10))

# outputs = mapping * inputs
mapping = outputs / inputs

@test names(mapping) == (:variates, :features)
end