Mercurial > hg > octave-lyh
changeset 9285:226f6d001ee2
further improve the polynomial example, fix indexing
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Tue, 02 Jun 2009 09:06:49 +0200 |
parents | 567e3e4ab74d |
children | c2248cc4821a |
files | ChangeLog doc/ChangeLog doc/interpreter/oop.txi examples/@polynomial/end.m examples/@polynomial/subsasgn.m examples/@polynomial/subsref.m |
diffstat | 6 files changed, 64 insertions(+), 24 deletions(-) [+] |
line wrap: on
line diff
--- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2009-06-02 Jaroslav Hajek <highegg@gmail.com> + + * 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 <octave@phaselockedsystems.com> * examples/@polynomial: Remove tabs from all functions so
--- a/doc/ChangeLog +++ b/doc/ChangeLog @@ -1,3 +1,8 @@ +2009-06-02 Jaroslav Hajek <highegg@gmail.com> + + * interpreter/oop.txi: Update docs of polynomial class, mention + chained indexing. + 2009-05-25 Rik <rdrider0-list@yahoo.com> * interpreter/Makefile.in: Add texmf.cnf to list of distributed files
--- 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.
--- 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
--- 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
--- 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