Skip to content

Commit

Permalink
Add left division operator for matrix and vector
Browse files Browse the repository at this point in the history
  • Loading branch information
KeitaNakamura committed Feb 17, 2021
1 parent 5afe3e5 commit 6ff20ce
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/ops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,19 @@ end
frommandel(SymmetricFourthOrderTensor{dim}, inv(tomandel(x)))
end

@generated function Base.:\(A::AbstractSquareTensor{dim}, b::AbstractVec{dim}) where {dim}
exps_A = [:(Tuple(A)[$i]) for i in independent_indices(A)]
exps_b = [:(Tuple(b)[$i]) for i in independent_indices(b)]
quote
@_inline_meta
@inbounds begin
SA = SMatrix{dim, dim}($(exps_A...))
Sb = SVector{dim}($(exps_b...))
end
Vec{dim}(Tuple(SA \ Sb))
end
end

"""
cross(x::Vec{3}, y::Vec{3}) -> Vec{3}
cross(x::Vec{2}, y::Vec{2}) -> Vec{3}
Expand Down
9 changes: 9 additions & 0 deletions test/ops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,15 @@ end
@test (@inferred inv(y))::typeof(y) y one(y)
end
end
@testset "solve" begin
for T in (Float32, Float64), dim in 1:4
A = rand(SecondOrderTensor{dim, T})
S = rand(SymmetricSecondOrderTensor{dim, T})
b = rand(Vec{dim, T})
@test (@inferred A \ b)::Vec{dim, T} |> Array Array(A) \ Array(b)
@test (@inferred S \ b)::Vec{dim, T} |> Array Array(S) \ Array(b)
end
end
@testset "cross" begin
for T in (Float32, Float64), dim in 1:3
x = rand(Vec{dim, T})
Expand Down

0 comments on commit 6ff20ce

Please sign in to comment.