This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

compute inverse of 2D matrix

+3 votes

Hi,

I am using FEniCS with the Python interface and I need to compute the inverse of Green-Cauchy tensor in 2D :

mesh = RectangleMesh(0.0,0.0,1.1,2.1,20,60)
V = VectorFunctionSpace(mesh, "Lagrange", 1)
u = Function(V)
I = Identity(2)
F = I + grad(u)
C = F.T*F

I thought about using the classical formula for computing the inverse of a 2D-matrix :
\(A =\begin{pmatrix} a &b \\ c & d \end{pmatrix}\)
\(A^{-1}=\frac{1}{det(A)}\begin{pmatrix} d & -b \\ -c & a \end{pmatrix}\)

What is the right syntax to access \(C(i,j)\) ? To build a new tensor \(C^{-1}\) ? Or is there a faster way (using spicy ?) to compute the inverse of a matrix ?

Thank you in advance !

related to an answer for: How to read data from class Matrix?
closed with the note: I reopened the question by mistake :-//
asked Jan 22, 2015 by Claire L FEniCS User (2,120 points)
closed Mar 30, 2016 by Claire L

1 Answer

+3 votes
 
Best answer

Hi, consider the following snippet

from dolfin import *
import numpy as np

mesh = UnitSquareMesh(3, 3)
V = VectorFunctionSpace(mesh, 'CG', 1)

u = Expression(('x[0]*x[1]', 'x[1]*x[1]'), domain=mesh)
I = Identity(2)
F = I + grad(u)
C = F.T*F

# Convenient inverse
Cinv0 = inv(C)
# Less convenient inverse
Cinv1 = as_tensor([[C[1, 1], -C[0, 1]], [-C[1, 0], C[0, 0]]])/det(C)

# Assemble the tensor fields
T = TensorFunctionSpace(mesh, 'CG', 1)
C0 = project(Cinv0, T)
C1 = project(Cinv1, T)

# Check that the fields are equal
assert np.allclose(C0.vector().array(), C1.vector().array()) 
answered Jan 22, 2015 by MiroK FEniCS Expert (80,920 points)
selected Mar 9, 2015 by Claire L

Ok, Thanks !

...