← Back to team overview

dolfin team mailing list archive

Re: Vector to numpy array and vice verse

 

How would you do this? The way the dolfin.Function class is created
destroys the documentation of constructor signatures, which in my
opinion is really really bad.

You could just add:

def as_vector(x):
    v = Vector(len(x))
    v.set(x)
    return v

then we can do
x = zeros(10)
v = as_vector(x)

and the dolfin.Function constructor can just as easily do this step.

-- 
Martin


2008/8/8 Johan Hake <hake@xxxxxxxxx>:
> Hello!
>
> You can easy get the the values of a dolfin vector in python, by calling
> Vector.array(). A numpy array is returned. But you can't do the other way
> around, i.e, initialize a dolfin Vector in python with a numpy array.
> Or you can but its done in two stages:
>
>  na = ones(10,'d')
>  dv = Vector(len(na))
>  dv.set(na)
>
> It would be straight forward to extend the Vector interface, together with all
> the different variants of Vectors, so one can do
>
>  na = ones(10,'d')
>  dv = Vector(na)
>
> I think this is a convinient way of initializing a Vector in dolfin, do we
> want this?
>
> With this extension of the Vector interface we can extend the Function
> constructor in python to also take a numpy array with values, instead of only
> a Vector (this is what I really want).
>
> What do you say? Any considerations conserning the upcomming revamp of
> Function and LA interface concerning parallelity?
>
> Johan
> _______________________________________________
> DOLFIN-dev mailing list
> DOLFIN-dev@xxxxxxxxxx
> http://www.fenics.org/mailman/listinfo/dolfin-dev
>


Follow ups

References