I would like to assemble an identity matrix to interact with others assembled matrices. Second, How can I create a diagonal matrix that has ones on specific entries, to again interact with other assembled matrices?
Thanks
Hi, the identity matrix can be assembled if you take this answer as starting point. That is
from dolfin import * mesh = UnitSquareMesh(10, 10) V = FunctionSpace(mesh, 'CG', 1) u = TrialFunction(V) v = TestFunction(V) form = inner(u, v)*dx I = assemble(form) I.zero() I.set_diagonal(interpolate(Constant(1), V).vector())
To control how the diagonal looks you need to appropriately assemble GenericVector with which you set the diagonal of your matrix. Can you give example of where you want the ones to be located?
GenericVector
I just tried the above. I figured that the following
I.set_diagonal(interpolate(Constant(1), V).vector())
needs to be replaced by
I.ident_zeros()
This can be verified by typing the following on the python interpretter
I.array()
This gives the following output
array([[ 1., 0., 0., ..., 0., 0., 0.], [ 0., 1., 0., ..., 0., 0., 0.], [ 0., 0., 1., ..., 0., 0., 0.], ..., [ 0., 0., 0., ..., 1., 0., 0.], [ 0., 0., 0., ..., 0., 1., 0.], [ 0., 0., 0., ..., 0., 0., 1.]])
The original snippet does produce an identity matrix with DOLFIN 1.4.0.
oops!... I forgot to mention that I was using
V = VectorFunctionSpace(mesh, 'CG', 1)
May be there is a way of using the original method for this case as well. But for now my problem is solved.
Thanks!
The following code generates an NxN identity matrix I very efficiently.
N = 100 mesh = UnitIntervalMesh(N) V = FunctionSpace(mesh, "DG", 0) u = TrialFunction(V) v = TestFunction(V) I = assemble(u*v*dx)*N