← Back to team overview

dolfin team mailing list archive

Re: Mapping Array to Python

 

On Thursday 04 March 2010 10:48:35 Anders Logg wrote:
> On Wed, Feb 24, 2010 at 06:12:29PM -0800, Johan Hake wrote:
> > On Tuesday 23 February 2010 11:48:05 Anders Logg wrote:
> > > I thought that Array should be automagically mapped to Numpy arrays in
> > > Python. This doesn't happen for the new component() function I just
> > > added to FunctionSpace.
> > >
> > > Test by
> > >
> > >   mesh = UnitSquare(4, 4)
> > >   V = VectorFunctionSpace(mesh, "CG", 1)
> > >   print V.sub(0).component()
> >
> > No they are not.
> >
> > I think we landed on doing this on case to case. You can just call
> > V.sub(0).component().array().
> >
> > The blueprint on the array typemap is not closed. There still exist some
> > questions we need to sort out, including (I think) this one.
> >
> > I cannot come up with a good example just now why we shouldn't just wrap
> > all out Arrays to numpy arrays, but I think it had something to do with
> > ownership and possibilities of returning an array that potentially might
> > be resized.
> 
> Could you try wrapping all Arrays by default?
> 
> It would be useful at least for
> 
>   FunctionSpace::component()
>   TimeSeries::vector_times()
>   TimeSeries::mesh_times()
> 
> All of these return an Array. It would be natural for these to return
> a Python list (or tuple) in Python.

Ok.

All return by value Arrays, can safely be mapped to a list, a tuple or a NumPy 
array. When I get time I can add this typemap. Which Python type to choose 
might differ from method to method though. I guess the examples you gave above 
a tuple is preferred.

I do not have the whole overview of the different ways methods in DOLFIN 
return data through Arrays, so it is difficult to construct a general rule. It 
would be good to get the overview so we can establish one and eventually close 
the blueprint.

Johan

> The first one returns a const reference. If it helps I can change it
> so it returns by value.
> 
> The other two return an Array by value.
> 
> Perhaps the natural thing would be for return-by-value Arrays to map
> to Python tuples?
> 
> --
> Anders
> 



References