how to create a matrix with given entries?

Asked by Doug Arnold

This seems like a simple question, but I'm stuck. Suppose I want to create
a dolfin Matrix object (with the default PETSc backend), with a given array.
E.g., suppose I want a matrix object M so that M.array() returns

array([[ 0., 0.],
       [ 7., 0.]])

or so that M.array() is equal to some other numpy array I have created.

Can someone supply the python code?

Thanks -- Doug

Question information

Language:
English Edit question
Status:
Answered
For:
DOLFIN Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Marie Rognes (meg-simula) said :
#1

Try:

--

from dolfin import *
from numpy import array

A = PETScMatrix()
A.resize(2, 2)
pos = array([0, 1], dtype='I')
A.set(array([[0.0, 0.0], [7.0, 0.0]]), pos, pos)
A.apply("insert")
print A.array()

--

and take a look at

    demo/undocumented/simple/python/demo_simple.py

Can you help with this problem?

Provide an answer of your own, or ask Doug Arnold for more information if necessary.

To post a message you must log in.