← Back to team overview

dolfin team mailing list archive

Re: access to CSR data in python

 

On Monday 16 February 2009 23:29:48 N wrote:
> Take a simple example like
>
> from dolfin import *
> mesh = UnitSquare(4, 4)
> V = FunctionSpace(mesh, "CG", 1)
> v = TestFunction(V)
> u = TrialFunction(V)
> a = dot(grad(v), grad(u))*dx
> A = assemble(a)
>
> The matrix A (with boost as the linear algebra base) gives the following
> for attribute data: data(self) ->
> std::tr1::tuple<(p.q(const).std::size_t,p.q(const).std::size_t,p.q(const).d
>ouble,int)>
>
>     Return pointers to underlying compressed storage data. See
>     GenericMatrix for documentation.
>
>
> How can this be used to gain access to the column data, row pointer, and
> data elements in the matrix?  I would like to have access to this data in
> Numpy arrays without copying the data.  Is this possible?

You now can. Sorry that I did not made it to the release ;)

You can now do:

  >>> rows, cols, values = A.data()

Here rows, cols and values are numpy arrays based on pointers to the 
underlaying data structures in c++.

It works for the linear algebra backends that supports the feature in C++, 
uBLAS and MTL4. Error is raised for the other backends.

Also added direct access to Vector data through the same data() function:

  >>> values = v.data()

This is similare to v.array(), but with no copying. This feature is also only 
supported for the uBLAS and MTL4 backends.

Cheers!

Johan


Follow ups

References