← Back to team overview

dolfin team mailing list archive

Re: x[:] syntax for DOLFIN vectors

 

On Thursday December 22 2011 10:33:20 Garth N. Wells wrote:
> On 21 Dec 2011, at 18:33, Johan Hake <johan.hake@xxxxxxxxx> wrote:
> > On Wednesday December 21 2011 18:00:23 Garth N. Wells wrote:
> >> What happens behind the scenes when for
> >> 
> >>   x = Vector(100)
> >> 
> >> ones does
> >> 
> >>  x[:]
> >> 
> >> ?
> > 
> > That depends if x[:] is on the lhs or on the rhs. If it is on the rhs a
> > copy will be returned. This is a slice convention in Python.
> > 
> >  l0 = range(5)
> >  l1 = l[:]
> >  l1[0] = 1
> > 
> > The last statement will change l1 but not l0. I would expect the same for
> > dolfin vectors.
> > 
> > If it is on lhs and the rhs is either a GeneriVector or a scalar,
> > x._assign(rhs) will be envoked.
> 
> What if I do
> 
>   x[:] += 1.0

Then x.__getslice__ will be envoked. This will return a copy of x and that 
copy will get 1.0 added to it. 

I suggest you do:

  x += 1.0

To get this wrapped to Python you need to first rename operator+= to (for 
example) _shift. Then expand __iadd__, and __isub__ in la_post.i, with check 
for getting a scalar. If true call self._shift. You can also use _shift for 
__isub__, and ignore operator-=.

Johan

> ?
> 
> Garth
> 
> > The relevant methods in la_post.i are:
> >  def __getslice__(...):
> > 
> >  def __setslize__(...):
> > Johan
> > 
> >> I've added GenericVector operator+= and -= for doubles, and would like
> >> to be sure that this can be used from Python without unnecessary
> >> copies being made.
> >> 
> >> Garth
> >> 
> >> _______________________________________________
> >> Mailing list: https://launchpad.net/~dolfin
> >> Post to     : dolfin@xxxxxxxxxxxxxxxxxxx
> >> Unsubscribe : https://launchpad.net/~dolfin
> >> More help   : https://help.launchpad.net/ListHelp
> 
> _______________________________________________
> Mailing list: https://launchpad.net/~dolfin
> Post to     : dolfin@xxxxxxxxxxxxxxxxxxx
> Unsubscribe : https://launchpad.net/~dolfin
> More help   : https://help.launchpad.net/ListHelp


References