← Back to team overview

dolfin team mailing list archive

Re: [HG DOLFIN] Added slicing capabilities for GenericVector interface in PyDOLFIN:

 

On Friday 17 April 2009 09:47:27 Anders Logg wrote:
> On Fri, Apr 17, 2009 at 09:25:16AM +0200, Johan Hake wrote:
> > On Friday 17 April 2009 09:20:20 DOLFIN wrote:
> > > One or more new changesets pushed to the primary dolfin repository.
> > > A short summary of the last three changesets is included below.
> > >
> > > changeset:   6003:010f9db3ecb7456374a12e3510ff1c93847d5f6b
> > > tag:         tip
> > > user:        "Johan Hake <hake@xxxxxxxxx>"
> > > date:        Fri Apr 17 09:20:16 2009 +0200
> > > files:       ChangeLog dolfin/swig/dolfin_la_post.i
> > > dolfin/swig/dolfin_la_pre.i test/unit/la/python/test.py description:
> > > Added slicing capabilities for GenericVector interface in PyDOLFIN:
> > >
> > >    We can now e.g. do things like:
> > >       v = Vector(10)
> > >       v[:] = 1.0
> > >       v[0:10:2] = 2.0
> > >       v[0:10:2] = v[1:10:2]
> > >       v[[0,3,4,6,9]] = v[1:10:2]
> > >
> > >    in the PyDOLFIN interface. A sliced Vector returns a copy of the old
> > > Vector. I have added a bunch of tests that covers some user cases, but
> > > please report any missbehaviours.
> >
> > This is a nice feature that makes python programming more smooth. The
> > implementation is done totally in an extended c++ layer of the proxy
> > classes in swig.
> >
> > There are some code duplications, which probably could be eliminated, and
> > it does not support numpy arrays on the right hand side, but this could
> > be added in the future.
> >
> > Cheers!
> >
> > Johan
>
> That's really cool. Can you do the same for matrices?

Done, at least for getitem. We can now do:

  inds = [0,2,4,-1]
  M = A[inds,inds]

The returned matrix is sparse and only have non-zero values. Setitem should be 
doable along the same alley. I use the set and get functions in the 
GenericVector interface so nothing is done in the DOLFIN class. This does not 
work for EpetraMatrix as resize() is not implemented.

The vector interface now implements the sequence interface, most notable:

  v.__contains__, __iter__, __len__

and comparison operators for scalars and GenericVectors, returning a boolean 
numpy array. This can be used for operations like:

  v[v<0] = 0

Filling a vector can now be done by:

  v[:] = 10; v[:] = u; v[:] = a

where u is a GenericVector of the same size and a numpy array of the same 
size.


Cheers

Johan


References