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

How to read data from class Matrix?

0 votes

Hi, I am new here. Could I ask how to read out the matrix in class Matrix? For example, in the following code, if I want to read the data in A after assembling, what should I do?

So thanks for the helps!

from dolfin import * 
import numpy
# Create mesh and define function space
nx = ny = 2
mesh = UnitSquare(nx, ny)
V = FunctionSpace(mesh, 'Lagrange', 1)
dt = 0.3      # time step
# Define variational problem
u = TrialFunction(V)
v = TestFunction(V)
a = u*v*dx + dt*inner(nabla_grad(u), nabla_grad(v))*dx
A = assemble(a)   # assemble
asked Jan 15, 2014 by Hrunx FEniCS Novice (910 points)

What do you mean by 'read the data'? Do you want to print the matrix to the screen?

2 Answers

+1 vote
 
Best answer

You can get a NumPy array representation by running A.array().

answered Jan 15, 2014 by johannr FEniCS Expert (17,350 points)
selected Jan 20, 2014 by Hrunx
+1 vote

If you want to avoid densification of the sparse operator matrix, you would have to use the uBLAS backend.

import scipy.sparse as sps
parameters.linear_algebra_backend = "uBLAS"
rows, cols, values = A.data()
Aa = sps.csr_matrix((values, cols, rows))
answered Jan 15, 2014 by meigel FEniCS User (1,520 points)
compute inverse of 2D matrix
Query on "parameters.linear_algebra_backend = "uBLAS""
...