I have several vectors (<dolfin.cpp.la.Vector at…) and I need to create a matrix<dolfin.cpp.la.Matrix at…) from them like this (every column of the matrix will be a vector):
A = [vector1, vector2, vector3, …]
I would like to know how to do that with these dolfin object types.
Maybe it is a non-sense question. I am in a python environment with those dolfin objects. I tried “as_matrix” but did not work
I would appreciate a hint about that.
As always, you should second-guess any procedure that results in a dense matrix, but, if it’s really necessary, one approach could be the following:
from dolfin import *
from petsc4py import PETSc
from numpy import array
# Get some dolfin.cpp.la.Vector objects:
mesh = UnitIntervalMesh(10)
V = FunctionSpace(mesh,"DG",0)
v = TestFunction(V)
v1 = assemble(Constant(1.0)*v*dx)
v2 = assemble(Constant(2.0)*v*dx)
v3 = assemble(Constant(3.0)*v*dx)
# Put them all into a NumPy matrix:
npMat = array([v1.get_local(),
v2.get_local(),
v3.get_local()]).transpose()
print(npMat)
# Create a PETSc dense matrix, with the
# petsc4py API, then wrap that as a
# dolfin.cpp.la.PETScMatrix object:
petMat = PETSc.Mat()
petMat.createDense(npMat.shape,array=npMat)
petMat.setUp()
mat = PETScMatrix(petMat)
print(mat.array())
These two are solving in very different time, when FEniCS matrix K is solved in a second but the assembled mat needs minutes. Could you let me know where I am doing wrong?