Skip to content

Cofactor function #217

@termi-official

Description

@termi-official

Reminder for me to factor out the cofactor matrix computation

Tensors.jl/src/math_ops.jl

Lines 80 to 132 in 258540f

"""
inv(::SecondOrderTensor)
Computes the inverse of a second order tensor.
# Examples
```jldoctest
julia> A = rand(Tensor{2,3})
3×3 Tensor{2, 3, Float64, 9}:
0.590845 0.460085 0.200586
0.766797 0.794026 0.298614
0.566237 0.854147 0.246837
julia> inv(A)
3×3 Tensor{2, 3, Float64, 9}:
19.7146 -19.2802 7.30384
6.73809 -10.7687 7.55198
-68.541 81.4917 -38.8361
```
"""
@generated function Base.inv(t::Tensor{2, dim}) where {dim}
Tt = get_base(t)
idx(i,j) = compute_index(Tt, i, j)
if dim == 1
ex = :($Tt((dinv, )))
elseif dim == 2
ex = quote
v = get_data(t)
$Tt((v[$(idx(2,2))] * dinv, -v[$(idx(2,1))] * dinv,
-v[$(idx(1,2))] * dinv, v[$(idx(1,1))] * dinv))
end
else # dim == 3
ex = quote
v = get_data(t)
$Tt(((v[$(idx(2,2))]*v[$(idx(3,3))] - v[$(idx(2,3))]*v[$(idx(3,2))]) * dinv,
-(v[$(idx(2,1))]*v[$(idx(3,3))] - v[$(idx(2,3))]*v[$(idx(3,1))]) * dinv,
(v[$(idx(2,1))]*v[$(idx(3,2))] - v[$(idx(2,2))]*v[$(idx(3,1))]) * dinv,
-(v[$(idx(1,2))]*v[$(idx(3,3))] - v[$(idx(1,3))]*v[$(idx(3,2))]) * dinv,
(v[$(idx(1,1))]*v[$(idx(3,3))] - v[$(idx(1,3))]*v[$(idx(3,1))]) * dinv,
-(v[$(idx(1,1))]*v[$(idx(3,2))] - v[$(idx(1,2))]*v[$(idx(3,1))]) * dinv,
(v[$(idx(1,2))]*v[$(idx(2,3))] - v[$(idx(1,3))]*v[$(idx(2,2))]) * dinv,
-(v[$(idx(1,1))]*v[$(idx(2,3))] - v[$(idx(1,3))]*v[$(idx(2,1))]) * dinv,
(v[$(idx(1,1))]*v[$(idx(2,2))] - v[$(idx(1,2))]*v[$(idx(2,1))]) * dinv))
end
end
return quote
$(Expr(:meta, :inline))
dinv = 1 / det(t)
@inbounds return $ex
end
end
into a separate function together with its analytical gradient. We need this one frequently for integration over the reference domain to connect with the normal in the deformed domain (see Nansons formula). Needs to check for performance regressions.

Users will for now probably use the identity $cof(F) = det(F) F^{-T}$, which is arguably less efficient.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions