← Back to team overview

dolfin team mailing list archive

Re: operator() or operator[]

 

On Mon, Apr 14, 2008 at 11:45:27AM +0100, Garth N. Wells wrote:
> 
> 
> Anders Logg wrote:
> > On Fri, Apr 11, 2008 at 02:24:13PM +0200, Dag Lindbo wrote:
> >> Anders Logg wrote:
> >>> On Fri, Apr 11, 2008 at 01:34:24PM +0200, Martin Sandve Alnæs wrote:
> >>>> 2008/4/11, Anders Logg <logg@xxxxxxxxx>:
> >>>>> On Fri, Apr 11, 2008 at 12:23:15PM +0100, Garth N. Wells wrote:
> >>>>>  >
> >>>>>  >
> >>>>>  > Anders Logg wrote:
> >>>>>  > > Which one do we want to use for element access in vectors and
> >>>>>  > > matrices. It looks like operator() is now implemented for uBlasVector,
> >>>>>  > > but isn't operator[] more natural to use?
> >>>>>  > >
> >>>>>  >
> >>>>>  > Much the same to me. We've used (.,.) for matrices, so it seems natural
> >>>>>  > to use (.) for vectors.
> >>>>>  >
> >>>>>  > Garth
> >>>>>
> >>>>>
> >>>>> We could use [] for matrices also I guess.
> >>>>>
> >>>>>  Then it will look the same as numpy.
> >>>> It's useful to index vectors with [], since the code will be similar
> >>>> for regular arrays.
> >>> I forgot something important, namely that operator[] expects exactly
> >>> one argument, so we can't do A[i, j] for matrices.
> >>>
> >>> Maybe then it's better to have () both for vectors and matrices?
> >>>
> >> Whatever you do with matrices, I don't have an opinion.
> >>
> >> However, I think operator[] should be present for vectors. One can argue 
> >> that it does not _need_ to be there, but the fact is that it _is_ in the 
> >> 0.7.2 interface (it worked until yesterday for uBlasVector, but it was 
> >> maybe handled by uBlas itself?).
> >>
> >> /Dag
> > 
> > Other opinions? Should we have both?
> > 
> > Since we have A(i, j), it will be expected that x(i) works.
> > 
> > But it will also be expected that x[i] works (since it works in numpy
> > and it has worked before for uBlasVector).
> > 
> 
> Having both is fine with me. It can be a little confusing in the code 
> because one might expect that a(2) and a[2] do different things.
> 
> Garth

After some discussion here, the new suggestion is to use [] everywhere
it is possible (both vectors and matrices, both C++ and Python) except
in the one case where it is not possible, namely for indexing matrices
in C++, which must be done by A(i, j).

-- 
Anders


References