[scilab-Users] can index starts from zero

Calixte Denizet calixte at contrib.scilab.org
Mon Oct 25 12:55:59 CEST 2010


Le lundi 25 octobre 2010 à 12:47 +0200, Samuel GOUGEON a écrit :
> Hello Calixte,
> 
> ----- Message d'origine -----
> De : Calixte Denizet
> Date : 25/10/2010 12:27:
> >
> > Hello,
> >
> > There is a workaround: you can wrap your array in a mlist:
> >
> > a=[4 3 2 1];
> > A=mlist(['Carray' 'array'],a);
> > // overload extraction
> > deff('y=%Carray_e(i,x)','y=x.array(i+1)');
> > // overload insertion
> > deff('z=%s_i_Carray(i,x,y)','y.array(i+1)=x;z=y;');
> > // overload print
> > deff('%Carray_p(x)','disp(x.array)');
> >
> > A(0) returns 4 and A(0)=5 inserts 5 at the first place. Have a look at
> > the help page about overloading and mlist.
> 
> The price is that then you also have to redefine all usual operators such that
> A*real, real*A, A+real, real+A, A+A, A*A, A>real, etc etc.
> 
> -->A*2
>      !--error 144
> Undefined operation for the given operands.
> check or define function %Carray_m_s for overloading.
> 
> -->A+1
>      !--error 144
> Undefined operation for the given operands.
> check or define function %Carray_a_s for overloading.
> 
> So yes, i agree that it is technically possible by defining a specific type
> + ALL needed functions -- not only insertion and extraction -- (and some
> of them like find() that is often useful are not (yet?) overloadable)
> However, it is a bit heavy, and decrease portability.
> A quite big price just for a shift-by-one ;)
> 

You're right and this workaround is time-consuming since overloaded
extraction for example is made in macro rather than in a built-in
function...

This workaround is not fully satisfying but it could be useful...
depending on what you want to do.

Calixte 

> Regards
> Samuel
> 





More information about the users mailing list