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

Cofactor function #217

Open
termi-official opened this issue May 22, 2024 · 0 comments
Open

Cofactor function #217

termi-official opened this issue May 22, 2024 · 0 comments

Comments

@termi-official
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant