How to create a Matrix ( from Vectors (

Hi all:

I have several vectors (< at…) and I need to create a 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.

Thank you so much and regards

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 :frowning:
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 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(),

# Create a PETSc dense matrix, with the
# petsc4py API, then wrap that as a
# object:
petMat = PETSc.Mat()
mat = PETScMatrix(petMat)


Thank you so much kamensky, that does the trick!!! :clap:

Hi Kamensky,

Thank you for the answer and it is very helpful. I am trying to do something like this to add some forces to the system manually:

add forces

F0 = assemble(RHS)
F = as_backend_type(F0)

assemble matrix

K = PETScMatrix()
assemble(LHS, tensor=K)
K_array = K.array()
petMat = PETSc.Mat()
mat = PETScMatrix(petMat)

solve my matrix and FEniCS matrix:

solve(mat, u.vector(), F, “bicgstab”, “amg”)
solve(K, u.vector(), F, “bicgstab”, “amg”)

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?

Many thanks!