Mercurial > jwe > octave
changeset 21070:fd97ed44f2da
doc: Use more coding conventions in Object Oriented reference example code.
* examples/code/@FIRfilter/FIRfilter.m,
examples/code/@FIRfilter/FIRfilter_aggregation.m,
examples/code/@FIRfilter/subsasgn.m, examples/code/@FIRfilter/subsref.m,
examples/code/@polynomial/display.m, examples/code/@polynomial/double.m,
examples/code/@polynomial/get.m, examples/code/@polynomial/mtimes.m,
examples/code/@polynomial/numel.m, examples/code/@polynomial/polynomial.m,
examples/code/@polynomial/polynomial_superiorto.m,
examples/code/@polynomial/polyval.m, examples/code/@polynomial/roots.m,
examples/code/@polynomial/set.m, examples/code/@polynomial/subsasgn.m,
examples/code/@polynomial/subsref.m:
Remove "Function File" from deftypefn. Use names of variables in error
messages. Surround invalid items in error messages with single quotes.
Use meaningful variable names.
author | Rik <rik@octave.org> |
---|---|
date | Thu, 14 Jan 2016 09:59:07 -0800 |
parents | a1aadf619e3c |
children | f25c14056b7c |
files | examples/code/@FIRfilter/FIRfilter.m examples/code/@FIRfilter/FIRfilter_aggregation.m examples/code/@FIRfilter/subsasgn.m examples/code/@FIRfilter/subsref.m examples/code/@polynomial/display.m examples/code/@polynomial/double.m examples/code/@polynomial/get.m examples/code/@polynomial/mtimes.m examples/code/@polynomial/numel.m examples/code/@polynomial/polynomial.m examples/code/@polynomial/polynomial_superiorto.m examples/code/@polynomial/polyval.m examples/code/@polynomial/roots.m examples/code/@polynomial/set.m examples/code/@polynomial/subsasgn.m examples/code/@polynomial/subsref.m |
diffstat | 16 files changed, 59 insertions(+), 57 deletions(-) [+] |
line wrap: on
line diff
--- a/examples/code/@FIRfilter/FIRfilter.m Thu Jan 14 08:51:44 2016 -0800 +++ b/examples/code/@FIRfilter/FIRfilter.m Thu Jan 14 09:59:07 2016 -0800 @@ -1,6 +1,6 @@ ## -*- texinfo -*- -## @deftypefn {Function File} {} FIRfilter () -## @deftypefnx {Function File} {} FIRfilter (@var{p}) +## @deftypefn {} {} FIRfilter () +## @deftypefnx {} {} FIRfilter (@var{p}) ## Create a FIR filter with polynomial @var{p} as coefficient vector. ## @end deftypefn @@ -13,7 +13,7 @@ if (nargin == 0) p = @polynomial ([1]); elseif (! isa (p, "polynomial")) - error ("@FIRfilter: expecting polynomial as input argument"); + error ("@FIRfilter: P must be a polynomial object"); endif f.polynomial = [];
--- a/examples/code/@FIRfilter/FIRfilter_aggregation.m Thu Jan 14 08:51:44 2016 -0800 +++ b/examples/code/@FIRfilter/FIRfilter_aggregation.m Thu Jan 14 09:59:07 2016 -0800 @@ -1,6 +1,6 @@ ## -*- texinfo -*- -## @deftypefn {Function File} {} FIRfilter () -## @deftypefnx {Function File} {} FIRfilter (@var{p}) +## @deftypefn {} {} FIRfilter () +## @deftypefnx {} {} FIRfilter (@var{p}) ## Create a FIR filter with polynomial @var{p} as coefficient vector. ## @end deftypefn @@ -14,7 +14,7 @@ f.polynomial = @polynomial ([1]); else if (! isa (p, "polynomial")) - error ("FIRfilter: expecting polynomial as input argument"); + error ("@FIRfilter: P must be a polynomial object"); endif f.polynomial = p;
--- a/examples/code/@FIRfilter/subsasgn.m Thu Jan 14 08:51:44 2016 -0800 +++ b/examples/code/@FIRfilter/subsasgn.m Thu Jan 14 09:59:07 2016 -0800 @@ -1,13 +1,13 @@ -function out = subsasgn (f, index, val) +function fout = subsasgn (f, index, val) switch (index.type) case "." fld = index.subs; if (! strcmp (fld, "polynomial")) - error ('@FIRfilter/subsasgn: invalid property "%s"', fld); + error ("@FIRfilter/subsasgn: invalid property '%s'", fld); endif - out = f; - out.polynomial = val; + fout = f; + fout.polynomial = val; otherwise error ("@FIRfilter/subsasgn: Invalid index type")
--- a/examples/code/@FIRfilter/subsref.m Thu Jan 14 08:51:44 2016 -0800 +++ b/examples/code/@FIRfilter/subsref.m Thu Jan 14 09:59:07 2016 -0800 @@ -1,16 +1,17 @@ -function out = subsref (f, x) +function r = subsref (f, x) switch (x.type) + case "()" n = f.polynomial; - out = filter (n.poly, 1, x.subs{1}); + r = filter (n.poly, 1, x.subs{1}); case "." fld = x.subs; if (! strcmp (fld, "polynomial")) - error ('@FIRfilter/subsref: invalid property "%s"', fld); + error ("@FIRfilter/subsref: invalid property '%s'", fld); endif - out = f.polynomial; + r = f.polynomial; otherwise error ("@FIRfilter/subsref: invalid subscript type for FIR filter");
--- a/examples/code/@polynomial/display.m Thu Jan 14 08:51:44 2016 -0800 +++ b/examples/code/@polynomial/display.m Thu Jan 14 09:59:07 2016 -0800 @@ -1,22 +1,23 @@ function display (p) + printf ("%s =", inputname (1)); + a = p.poly; first = true; - printf ("%s =", inputname (1)); for i = 1 : length (a); if (a(i) != 0) if (first) first = false; - elseif (a(i) > 0) + elseif (a(i) > 0 || isnan (a(i))) printf (" +"); endif if (a(i) < 0) printf (" -"); endif if (i == 1) - printf (" %g", abs (a(i))); - elseif (abs(a(i)) != 1) - printf (" %g *", abs (a(i))); + printf (" %.5g", abs (a(i))); + elseif (abs (a(i)) != 1) + printf (" %.5g *", abs (a(i))); endif if (i > 1) printf (" X");
--- a/examples/code/@polynomial/double.m Thu Jan 14 08:51:44 2016 -0800 +++ b/examples/code/@polynomial/double.m Thu Jan 14 09:59:07 2016 -0800 @@ -1,3 +1,3 @@ -function b = double (a) - b = a.poly; +function a = double (p) + a = p.poly; endfunction
--- a/examples/code/@polynomial/get.m Thu Jan 14 08:51:44 2016 -0800 +++ b/examples/code/@polynomial/get.m Thu Jan 14 09:59:07 2016 -0800 @@ -1,21 +1,21 @@ -function s = get (p, f) +function val = get (p, prop) if (nargin < 1 || nargin > 2) print_usage (); endif if (nargin == 1) - s.poly = p.poly; + val.poly = p.poly; else - if (! ischar (f)) - error ("@polynomial/get: expecting the property to be a string"); + if (! ischar (prop)) + error ("@polynomial/get: PROPERTY must be a string"); endif - switch (f) + switch (prop) case "poly" - s = p.poly; + val = p.poly; otherwise - error ("@polynomial/get: invalid property %s", f); + error ("@polynomial/get: invalid PROPERTY '%s'", prop); endswitch endif
--- a/examples/code/@polynomial/mtimes.m Thu Jan 14 08:51:44 2016 -0800 +++ b/examples/code/@polynomial/mtimes.m Thu Jan 14 09:59:07 2016 -0800 @@ -1,3 +1,3 @@ -function y = mtimes (a, b) - y = polynomial (conv (double (a), double (b))); +function p = mtimes (a, b) + p = polynomial (conv (double (a), double (b))); endfunction
--- a/examples/code/@polynomial/numel.m Thu Jan 14 08:51:44 2016 -0800 +++ b/examples/code/@polynomial/numel.m Thu Jan 14 09:59:07 2016 -0800 @@ -1,4 +1,3 @@ function n = numel (obj, idx) n = 1; # always produce an array. endfunction -
--- a/examples/code/@polynomial/polynomial.m Thu Jan 14 08:51:44 2016 -0800 +++ b/examples/code/@polynomial/polynomial.m Thu Jan 14 09:59:07 2016 -0800 @@ -1,6 +1,6 @@ ## -*- texinfo -*- -## @deftypefn {Function File} {} polynomial () -## @deftypefnx {Function File} {} polynomial (@var{a}) +## @deftypefn {} {} polynomial () +## @deftypefnx {} {} polynomial (@var{a}) ## Create a polynomial object representing the polynomial ## ## @example @@ -23,11 +23,11 @@ else if (strcmp (class (a), "polynomial")) p = a; - elseif (isvector (a) && isreal (a)) + elseif (isreal (a) && isvector (a)) p.poly = a(:).'; # force row vector p = class (p, "polynomial"); else - error ("@polynomial: expecting real vector"); + error ("@polynomial: A must be a real vector"); endif endif
--- a/examples/code/@polynomial/polynomial_superiorto.m Thu Jan 14 08:51:44 2016 -0800 +++ b/examples/code/@polynomial/polynomial_superiorto.m Thu Jan 14 09:59:07 2016 -0800 @@ -1,6 +1,6 @@ ## -*- texinfo -*- -## @deftypefn {Function File} {} polynomial () -## @deftypefnx {Function File} {} polynomial (@var{a}) +## @deftypefn {} {} polynomial () +## @deftypefnx {} {} polynomial (@var{a}) ## Create a polynomial object representing the polynomial ## ## @example @@ -23,11 +23,11 @@ else if (strcmp (class (a), "polynomial")) p = a; - elseif (isvector (a) && isreal (a)) + elseif (isreal (a) && isvector (a)) p.poly = a(:).'; # force row vector p = class (p, "polynomial"); else - error ("@polynomial: expecting real vector"); + error ("@polynomial: A must be a real vector"); endif endif
--- a/examples/code/@polynomial/polyval.m Thu Jan 14 08:51:44 2016 -0800 +++ b/examples/code/@polynomial/polyval.m Thu Jan 14 09:59:07 2016 -0800 @@ -1,6 +1,6 @@ function [y, dy] = polyval (p, varargin) - if (nargout == 2) + if (nargout > 1) [y, dy] = polyval (fliplr (p.poly), varargin{:}); else y = polyval (fliplr (p.poly), varargin{:});
--- a/examples/code/@polynomial/roots.m Thu Jan 14 08:51:44 2016 -0800 +++ b/examples/code/@polynomial/roots.m Thu Jan 14 09:59:07 2016 -0800 @@ -1,3 +1,3 @@ -function y = roots (p) - y = roots (fliplr (p.poly)); +function r = roots (p) + r = roots (fliplr (p.poly)); endfunction
--- a/examples/code/@polynomial/set.m Thu Jan 14 08:51:44 2016 -0800 +++ b/examples/code/@polynomial/set.m Thu Jan 14 09:59:07 2016 -0800 @@ -1,21 +1,21 @@ -function s = set (p, varargin) +function pout = set (p, varargin) if (numel (varargin) < 2 || rem (numel (varargin), 2) != 0) - error ("@polynomial/set: expecting property/value pairs"); + error ("@polynomial/set: expecting PROPERTY/VALUE pairs"); endif - s = p; + pout = p; while (numel (varargin) > 1) prop = varargin{1}; val = varargin{2}; varargin(1:2) = []; if (! ischar (prop) || ! strcmp (prop, "poly")) - error ("@polynomial/set: invalid property of polynomial class"); - elseif (! (isvector (val) && isreal (val))) - error ("@polynomial/set: expecting the value to be a real vector"); + error ("@polynomial/set: invalid PROPERTY for polynomial class"); + elseif (! (isreal (val) && isvector (val))) + error ("@polynomial/set: VALUE must be a real vector"); endif - s.poly = val(:).'; # force row vector + pout.poly = val(:).'; # force row vector endwhile endfunction
--- a/examples/code/@polynomial/subsasgn.m Thu Jan 14 08:51:44 2016 -0800 +++ b/examples/code/@polynomial/subsasgn.m Thu Jan 14 09:59:07 2016 -0800 @@ -23,7 +23,7 @@ case "." fld = s(1).subs; if (! strcmp (fld, "poly")) - error ("@polynomial/subsasgn: invalid property \"%s\"", fld); + error ("@polynomial/subsasgn: invalid property '%s'", fld); endif if (numel (s) == 1) p.poly = val; @@ -33,6 +33,7 @@ otherwise error ("@polynomial/subsasgn: invalid subscript type"); + endswitch endfunction
--- a/examples/code/@polynomial/subsref.m Thu Jan 14 08:51:44 2016 -0800 +++ b/examples/code/@polynomial/subsref.m Thu Jan 14 09:59:07 2016 -0800 @@ -1,4 +1,4 @@ -function b = subsref (a, s) +function r = subsref (a, s) if (isempty (s)) error ("@polynomial/subsref: missing index"); @@ -11,7 +11,7 @@ if (numel (ind) != 1) error ("@polynomial/subsref: need exactly one index"); endif - b = polyval (fliplr (a.poly), ind{1}); + r = polyval (fliplr (a.poly), ind{1}); case "{}" ind = s(1).subs; @@ -20,17 +20,17 @@ endif if (isnumeric (ind{1})) - b = a.poly(ind{1}+1); + r = a.poly(ind{1}+1); else - b = a.poly(ind{1}); + r = a.poly(ind{1}); endif case "." fld = s.subs; if (! strcmp (fld, "poly")) - error ("@polynomial/subsref: invalid property \"%s\"", fld); + error ("@polynomial/subsref: invalid property '%s'", fld); endif - b = a.poly; + r = a.poly; otherwise error ("@polynomial/subsref: invalid subscript type"); @@ -38,7 +38,7 @@ endswitch if (numel (s) > 1) - b = subsref (b, s(2:end)); + r = subsref (r, s(2:end)); endif endfunction