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

Add pseudo-inverse of tensors and enable tranpose for Vec{dim} #139

Open
wants to merge 5 commits 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
2 changes: 1 addition & 1 deletion src/Tensors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ end
###############
# Typealiases #
###############
const Vec{dim, T, M} = Tensor{1, dim, T, dim}
const Vec{dim, T} = Tensor{1, dim, T, dim}

const AllTensors{dim, T} = Union{SymmetricTensor{2, dim, T}, Tensor{2, dim, T},
SymmetricTensor{4, dim, T}, Tensor{4, dim, T},
Expand Down
25 changes: 25 additions & 0 deletions src/math_ops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,31 @@ function Base.inv(t::SymmetricTensor{4, dim, T}) where {dim, T}
frommandel(SymmetricTensor{4, dim}, inv(tomandel(t)))
end

"""
inv(::Tensor{order, dim})

Compute the pseudo-inverse of a tensor.
"""
function LinearAlgebra.pinv(t::Vec{dim, T}) where {dim, T}
LinearAlgebra.Transpose{T, Vec{dim, T}}(t / sum(t.^2))
end

function LinearAlgebra.pinv(t::Tensor{2, dim}) where {dim}
Base.inv(t)
end

function LinearAlgebra.pinv(t::SymmetricTensor{2, dim}) where {dim}
Base.inv(t)
end

function LinearAlgebra.pinv(t::Tensor{4, dim}) where {dim}
Base.inv(t)
end

function LinearAlgebra.pinv(t::SymmetricTensor{4, dim, T}) where {dim, T}
Base.inv(t)
end

Base.:\(S1::SecondOrderTensor, S2::AbstractTensor) = inv(S1) ⋅ S2

"""
Expand Down
8 changes: 1 addition & 7 deletions src/tensor_ops_errors.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
# Remove `*` as infix operator between tensors
function Base.:*(S1::AbstractTensor, S2::AbstractTensor)
error("use `⋅` (`\\cdot`) for single contraction and `⊡` (`\\boxdot`) for double contraction instead of `*`")
end

for f in (:transpose, :adjoint)
@eval function LinearAlgebra.$f(::Vec)
throw(ArgumentError("the (no-op) $($f) is discontinued for `Tensors.Vec`"))
end
end
end
6 changes: 0 additions & 6 deletions test/test_misc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -545,10 +545,4 @@ end
A2 = rand(Tensor{2, 2})
@test_throws DimensionMismatch A + A2
@test_throws DimensionMismatch AA - A

# transpose/adjoint of Vec
x = rand(Vec{3})
@test_throws ArgumentError x'
@test_throws ArgumentError transpose(x)
@test_throws ArgumentError adjoint(x)
end # of testset