Skip to content

Commit

Permalink
Added sqrtm to compute matrix square root
Browse files Browse the repository at this point in the history
  • Loading branch information
Sad-Abd committed May 10, 2024
1 parent 6f5d542 commit d992ae8
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/matadi/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,4 +267,17 @@ def unimodular(T):
This operation preserves the isochoric (volume-preserving) part of the tensor
while removing the volumetric part.
"""
return (det(T) ** (-1 / 3)) * T
return (det(T) ** (-1 / 3)) * T

def sqrtm(C, eps = 8e-5):
"""
Compute the matrix square root of a tensor C using eigendecomposition.
"""
w = eigvals(C, eps=eps)
eye = SX.eye(3)

M1 = (C - w[1] * eye) * (C - w[2] * eye) / (w[0] - w[1]) / (w[0] - w[2])
M2 = (C - w[2] * eye) * (C - w[0] * eye) / (w[1] - w[2]) / (w[1] - w[0])
M3 = (C - w[0] * eye) * (C - w[1] * eye) / (w[2] - w[0]) / (w[2] - w[1])

return sqrt(w[0]) * M1 + sqrt(w[1]) * M2 + sqrt(w[2]) * M3

0 comments on commit d992ae8

Please sign in to comment.