Mercurial > hg > octave-thorsten
changeset 9284:567e3e4ab74d
fix up examples/@polynomial
author | Robert T. Short <octave@phaselockedsystems.com> |
---|---|
date | Sun, 31 May 2009 21:11:31 -0700 |
parents | 38ad8c99d6a2 |
children | 226f6d001ee2 |
files | ChangeLog examples/@polynomial/Makefile.in examples/@polynomial/display.m examples/@polynomial/end.m examples/@polynomial/get.m examples/@polynomial/mtimes.m examples/@polynomial/polynomial.m examples/@polynomial/polynomial_superiorto.m examples/@polynomial/polyval.m examples/@polynomial/roots.m examples/@polynomial/set.m examples/@polynomial/subsasgn.m examples/@polynomial/subsref.m |
diffstat | 13 files changed, 74 insertions(+), 44 deletions(-) [+] |
line wrap: on
line diff
--- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2009-06-02 Robert T. Short <octave@phaselockedsystems.com> + + * examples/@polynomial: Remove tabs from all functions so + documentation looks right. Fix a bunch of methods + that didn't work. Add a method referred to in the docs + that didn't exist. + 2009-05-26 John W. Eaton <jwe@octave.org> * src/dirfns.cc (Fpathsep): Allow path separator to be set.
--- a/examples/@polynomial/Makefile.in +++ b/examples/@polynomial/Makefile.in @@ -51,6 +51,7 @@ polynomial.m \ polynomial_superiorto.m \ polyval.m \ + roots.m \ set.m \ subsasgn.m \ subsref.m
--- a/examples/@polynomial/display.m +++ b/examples/@polynomial/display.m @@ -5,23 +5,23 @@ for i = 1 : length (a); if (a(i) != 0) if (first) - first = false; + first = false; elseif (a(i) > 0) - fprintf (" +"); + fprintf (" +"); endif if (a(i) < 0) - fprintf (" -"); + fprintf (" -"); endif if (i == 1) - fprintf (" %g", abs (a(i))); + fprintf (" %g", abs (a(i))); elseif (abs(a(i)) != 1) - fprintf (" %g *", abs (a(i))); + fprintf (" %g *", abs (a(i))); endif if (i > 1) - fprintf (" X"); + fprintf (" X"); endif if (i > 2) - fprintf (" ^ %d", i - 1); + fprintf (" ^ %d", i - 1); endif endif endfor @@ -30,4 +30,3 @@ endif fprintf("\n"); endfunction -
--- a/examples/@polynomial/end.m +++ b/examples/@polynomial/end.m @@ -1,11 +1,13 @@ function r = end (obj, index_pos, num_indices) - dv = size (obj.x); - for i = (num_indices + 1) : length (dv) - dv(num_indices) *= dv(i); - endfor - if (index_pos <= length (dv)) - r = dv (index_pos); - elseif - r = 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); + endfunction
--- a/examples/@polynomial/get.m +++ b/examples/@polynomial/get.m @@ -4,10 +4,10 @@ elseif (nargin == 2) if (ischar (f)) switch (f) - case "poly" - s = p.poly; - otherwise - error ("get: invalid property %s", f); + case "poly" + s = p.poly; + otherwise + error ("get: invalid property %s", f); endswitch else error ("get: expecting the property to be a string");
--- a/examples/@polynomial/mtimes.m +++ b/examples/@polynomial/mtimes.m @@ -1,5 +1,3 @@ function y = mtimes (a, b) - ap = double (a); - bp = double (b); - y = polynomial (filter (ap, 1, [bp(:).', zeros(1, length(bp) - 1)])); + y = polynomial (conv (double(a),double(b))); endfunction \ No newline at end of file
--- a/examples/@polynomial/polynomial.m +++ b/examples/@polynomial/polynomial.m @@ -6,23 +6,24 @@ ## @example ## a0 + a1 * x + a2 * x^2 + @dots{} + an * x^n ## @end example +## +## from a vector of coefficients [a0 a1 a2 ... an]. ## @end deftypefn function p = polynomial (a) if (nargin == 0) - p.poly = []; + p.poly = [0]; p = class (p, "polynomial"); elseif (nargin == 1) if (strcmp (class (a), "polynomial")) p = a; elseif (isvector (a) && isreal (a)) - p.poly = a(:)'; + p.poly = a(:).'; p = class (p, "polynomial"); else - error ("polynomial: expecting real or complex vector"); + error ("polynomial: expecting real vector"); endif else print_usage (); endif - superiorto ("double"); endfunction
--- a/examples/@polynomial/polynomial_superiorto.m +++ b/examples/@polynomial/polynomial_superiorto.m @@ -1,15 +1,27 @@ +## -*- texinfo -*- +## @deftypefn {Function File} {} polynomial () +## @deftypefnx {Function File} {} polynomial (@var{a}) +## Creates a polynomial object representing the polynomial +## +## @example +## a0 + a1 * x + a2 * x^2 + @dots{} + an * x^n +## @end example +## +## from a vector of coefficients [a0 a1 a2 ... an]. +## @end deftypefn + function p = polynomial (a) if (nargin == 0) - p.poly = []; + p.poly = [0]; p = class (p, "polynomial"); elseif (nargin == 1) if (strcmp (class (a), "polynomial")) p = a; elseif (isvector (a) && isreal (a)) - p.poly = a(:)'; + p.poly = a(:).'; p = class (p, "polynomial"); else - error ("polynomial: expecting real or complex vector"); + error ("polynomial: expecting real vector"); endif else print_usage ();
--- a/examples/@polynomial/polyval.m +++ b/examples/@polynomial/polyval.m @@ -1,7 +1,7 @@ function [y, dy] = polyval (p, varargin) if (nargout == 2) - [y, dy] = polyval (p.poly, varargin{:}); + [y, dy] = polyval (fliplr(p.poly), varargin{:}); else - y = polyval (p.poly, varargin{:}); + y = polyval (fliplr(p.poly), varargin{:}); endif endfunction
new file mode 100644 --- /dev/null +++ b/examples/@polynomial/roots.m @@ -0,0 +1,3 @@ +function y = roots (p) + y = roots(fliplr(p.poly)); +endfunction \ No newline at end of file
--- a/examples/@polynomial/set.m +++ b/examples/@polynomial/set.m @@ -9,12 +9,12 @@ varargin(1:2) = []; if (ischar (prop) && strcmp (prop, "poly")) if (isvector (val) && isreal (val)) - s.poly = val(:)'; + s.poly = val(:).'; else - error ("set: expecting the value to be a real vector"); + error ("set: expecting the value to be a real vector"); endif else error ("set: invalid property of polynomial class"); endif endwhile -endfunction \ No newline at end of file +endfunction
--- a/examples/@polynomial/subsasgn.m +++ b/examples/@polynomial/subsasgn.m @@ -1,12 +1,20 @@ -function s = subsasgn (s, index, val) +function p = subsasgn (p, index, val) + index.type + index.subs switch (index.type) case "()" - if (! isnumeric (val) || iscomplex(val) ||any (val(:)) >= 2.^ s.m || - any (val(:)) < 0 || any (val(:) != fix(val(:)))) - error ("subsasgn: value must be an array of real integers between 0 and 2.^m - 1"); + ind = index.subs; + if ( (any (ind{:}>length(p.poly))) + || (any (ind{:}<0)) ) + error ("subsasgn: subscript out of range"); endif - s.x = subsasgn (s.x, index, double (val)); + p.poly(ind{:}) = val; case "." - error ("subsagn: can not set properties of a galois field directly"); + fld = index.subs; + if (strcmp (fld, "poly")) + p.poly = val; + else + error ("@polynomial/subsref: invalid property \"%s\"", fld); + endif endswitch endfunction
--- a/examples/@polynomial/subsref.m +++ b/examples/@polynomial/subsref.m @@ -9,10 +9,9 @@ case "." fld = s.subs; if (strcmp (fld, "poly")) - b = a.poly; + b = a.poly; else - error ("@polynomial/subsref: invalid property \"%s\"", - fld); + error ("@polynomial/subsref: invalid property \"%s\"", fld); endif endswitch endfunction