# HG changeset patch # User Jaroslav Hajek # Date 1243926409 -7200 # Node ID 226f6d001ee2d28abd7518f65257145edfc4d35b # Parent 567e3e4ab74debb5753c14eb50c7925ca8fd9fac further improve the polynomial example, fix indexing diff --git a/ChangeLog b/ChangeLog --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2009-06-02 Jaroslav Hajek + + * examples/@polynomial/subsref.m: Allow chained subscripts, + fix behavior according to docs. + * examples/@polynomial/subsasgn.m: Allow chained subscripts, + fix behavior according to docs. + 2009-06-02 Robert T. Short * examples/@polynomial: Remove tabs from all functions so diff --git a/doc/ChangeLog b/doc/ChangeLog --- a/doc/ChangeLog +++ b/doc/ChangeLog @@ -1,3 +1,8 @@ +2009-06-02 Jaroslav Hajek + + * interpreter/oop.txi: Update docs of polynomial class, mention + chained indexing. + 2009-05-25 Rik * interpreter/Makefile.in: Add texmf.cnf to list of distributed files diff --git a/doc/interpreter/oop.txi b/doc/interpreter/oop.txi --- a/doc/interpreter/oop.txi +++ b/doc/interpreter/oop.txi @@ -264,7 +264,7 @@ @DOCSTRING(subsref) For example we might decide that indexing with "()" evaluates the -polynomial and indexing with "@{@}" returns the @var{n}-th coefficient. +polynomial and indexing with "@{@}" returns the @var{n}-th coefficient (of @var{n}-th power). In this case the @code{subsref} method of our polynomial class might look like @polynomialfile{subsref.m} @@ -274,6 +274,11 @@ @DOCSTRING(subsasgn) +Note that the @code{subsref} and @code{subsasgn} methods always receive the +whole index chain, while they usually handle only the first element. It is the +responsibility of these methods to handle the rest of the chain (if needed), +usually by forwarding it again to @code{subsref} or @code{subsasgn}. + If you wish to use the @code{end} keyword in subscripted expressions of an object, then the user needs to define the @code{end} method for the class. diff --git a/examples/@polynomial/end.m b/examples/@polynomial/end.m --- a/examples/@polynomial/end.m +++ b/examples/@polynomial/end.m @@ -1,13 +1,9 @@ function r = end (obj, index_pos, num_indices) - if ( num_indices!=1 ) + if (num_indices != 1) error ("polynomial object may only have one index") endif - if ( (index_pos<1) || (index_pos>length(obj.poly)) ) - error ("subscript out of range") - end - - r = length(obj.poly); + r = length (obj.poly); endfunction diff --git a/examples/@polynomial/subsasgn.m b/examples/@polynomial/subsasgn.m --- a/examples/@polynomial/subsasgn.m +++ b/examples/@polynomial/subsasgn.m @@ -1,20 +1,31 @@ -function p = subsasgn (p, index, val) - index.type - index.subs - switch (index.type) - case "()" - ind = index.subs; - if ( (any (ind{:}>length(p.poly))) - || (any (ind{:}<0)) ) - error ("subsasgn: subscript out of range"); +function p = subsasgn (p, s, val) + if (length (s) < 1) + error ("polynomial: needs index"); + endif + switch (s(1).type) + case "{}" + ind = s(1).subs; + if (numel (ind) != 1) + error ("polynomial: need exactly one index"); + else + if (length (s) == 1) + p.poly(ind{1}+1) = val; + else + error ("polynomial: chained subscripts not allowed for {}"); + endif endif - p.poly(ind{:}) = val; case "." - fld = index.subs; + fld = s(1).subs; if (strcmp (fld, "poly")) - p.poly = val; + if (length (s) == 1) + p.poly = val; + else + p.poly = subsasgn (p.poly, s(2:end), val); + endif else error ("@polynomial/subsref: invalid property \"%s\"", fld); endif + otherwise + error ("invalid subscript type"); endswitch endfunction diff --git a/examples/@polynomial/subsref.m b/examples/@polynomial/subsref.m --- a/examples/@polynomial/subsref.m +++ b/examples/@polynomial/subsref.m @@ -1,11 +1,22 @@ function b = subsref (a, s) - switch s.type + if (isempty (s)) + error ("polynomial: missing index"); + endif + switch (s(1).type) case "()" - ind = s.subs; - b = polyval (fliplr(a.poly), ind{:}); + ind = s(1).subs; + if (numel (ind) != 1) + error ("polynomial: need exactly one index"); + else + b = polyval (fliplr (a.poly), ind{1}); + endif case "{}" - ind = s.subs; - b = polynomial (a.poly(ind{:})); + ind = s(1).subs; + if (numel (ind) != 1) + error ("polynomial: need exactly one index"); + else + b = a.poly(ind{1}+1); + endif case "." fld = s.subs; if (strcmp (fld, "poly")) @@ -13,5 +24,10 @@ else error ("@polynomial/subsref: invalid property \"%s\"", fld); endif + otherwise + error ("invalid subscript type"); endswitch + if (numel (s) > 1) + b = subsref (b, s(2:end)); + endif endfunction