# HG changeset patch # User Rik # Date 1452730884 28800 # Node ID 73ab962bc52dcb091b2ad0ef058a816b95cdfc8f # Parent 759fcdf3666db41da1105127924dfe5e15121a60 doc: Use newer coding conventions in examples/code directory. * FIRfilter.m, FIRfilter_aggregation.m, display.m, subsasgn.m, subsref.m, display.m, end.m, get.m, numel.m, plot.m, polynomial.m, polynomial_superiorto.m, polyval.m, roots.m, set.m, subsasgn.m, subsref.m, celldemo.cc, fortrandemo.cc, funcdemo.cc, globaldemo.cc, oct_demo.cc, paramdemo.cc, stringdemo.cc: Use newer coding conventions in examples/code directory. diff -r 759fcdf3666d -r 73ab962bc52d examples/code/@FIRfilter/FIRfilter.m --- a/examples/code/@FIRfilter/FIRfilter.m Wed Jan 13 17:28:42 2016 +1100 +++ b/examples/code/@FIRfilter/FIRfilter.m Wed Jan 13 16:21:24 2016 -0800 @@ -6,15 +6,17 @@ function f = FIRfilter (p) - f.polynomial = []; + if (nargin > 1) + print_usage (); + endif + if (nargin == 0) p = @polynomial ([1]); - elseif (nargin == 1) - if (!isa (p, "polynomial")) - error ("@FIRfilter: expecting polynomial as input argument"); - endif - else - print_usage (); + elseif (! isa (p, "polynomial")) + error ("@FIRfilter: expecting polynomial as input argument"); endif + + f.polynomial = []; f = class (f, "FIRfilter", p); + endfunction diff -r 759fcdf3666d -r 73ab962bc52d examples/code/@FIRfilter/FIRfilter_aggregation.m --- a/examples/code/@FIRfilter/FIRfilter_aggregation.m Wed Jan 13 17:28:42 2016 +1100 +++ b/examples/code/@FIRfilter/FIRfilter_aggregation.m Wed Jan 13 16:21:24 2016 -0800 @@ -6,16 +6,20 @@ function f = FIRfilter (p) + if (nargin > 1) + print_usage (); + endif + if (nargin == 0) f.polynomial = @polynomial ([1]); - elseif (nargin == 1) - if (isa (p, "polynomial")) - f.polynomial = p; - else + else + if (! isa (p, "polynomial")) error ("FIRfilter: expecting polynomial as input argument"); endif - else - print_usage (); + + f.polynomial = p; endif + f = class (f, "FIRfilter"); + endfunction diff -r 759fcdf3666d -r 73ab962bc52d examples/code/@FIRfilter/display.m --- a/examples/code/@FIRfilter/display.m Wed Jan 13 17:28:42 2016 +1100 +++ b/examples/code/@FIRfilter/display.m Wed Jan 13 16:21:24 2016 -0800 @@ -3,4 +3,3 @@ display (f.polynomial); endfunction - diff -r 759fcdf3666d -r 73ab962bc52d examples/code/@FIRfilter/subsasgn.m --- a/examples/code/@FIRfilter/subsasgn.m Wed Jan 13 17:28:42 2016 +1100 +++ b/examples/code/@FIRfilter/subsasgn.m Wed Jan 13 16:21:24 2016 -0800 @@ -1,14 +1,16 @@ function out = subsasgn (f, index, val) + switch (index.type) case "." fld = index.subs; - if (strcmp (fld, "polynomial")) - out = f; - out.polynomial = val; - else - error ("@FIRfilter/subsasgn: invalid property \"%s\"", fld); + if (! strcmp (fld, "polynomial")) + error ('@FIRfilter/subsasgn: invalid property "%s"', fld); endif + out = f; + out.polynomial = val; + otherwise error ("@FIRfilter/subsasgn: Invalid index type") endswitch + endfunction diff -r 759fcdf3666d -r 73ab962bc52d examples/code/@FIRfilter/subsref.m --- a/examples/code/@FIRfilter/subsref.m Wed Jan 13 17:28:42 2016 +1100 +++ b/examples/code/@FIRfilter/subsref.m Wed Jan 13 16:21:24 2016 -0800 @@ -1,16 +1,20 @@ function out = subsref (f, x) + switch (x.type) case "()" n = f.polynomial; out = filter (n.poly, 1, x.subs{1}); + case "." fld = x.subs; - if (strcmp (fld, "polynomial")) - out = f.polynomial; - else - error ("@FIRfilter/subsref: invalid property \"%s\"", fld); + if (! strcmp (fld, "polynomial")) + error ('@FIRfilter/subsref: invalid property "%s"', fld); endif + out = f.polynomial; + otherwise error ("@FIRfilter/subsref: invalid subscript type for FIR filter"); + endswitch + endfunction diff -r 759fcdf3666d -r 73ab962bc52d examples/code/@polynomial/display.m --- a/examples/code/@polynomial/display.m Wed Jan 13 17:28:42 2016 +1100 +++ b/examples/code/@polynomial/display.m Wed Jan 13 16:21:24 2016 -0800 @@ -1,32 +1,35 @@ function display (p) + a = p.poly; first = true; - fprintf ("%s =", inputname (1)); + printf ("%s =", inputname (1)); for i = 1 : length (a); if (a(i) != 0) if (first) first = false; elseif (a(i) > 0) - fprintf (" +"); + printf (" +"); endif if (a(i) < 0) - fprintf (" -"); + printf (" -"); endif if (i == 1) - fprintf (" %g", abs (a(i))); + printf (" %g", abs (a(i))); elseif (abs(a(i)) != 1) - fprintf (" %g *", abs (a(i))); + printf (" %g *", abs (a(i))); endif if (i > 1) - fprintf (" X"); + printf (" X"); endif if (i > 2) - fprintf (" ^ %d", i - 1); + printf (" ^ %d", i - 1); endif endif endfor + if (first) - fprintf (" 0"); + printf (" 0"); endif - fprintf ("\n"); + printf ("\n"); + endfunction diff -r 759fcdf3666d -r 73ab962bc52d examples/code/@polynomial/end.m --- a/examples/code/@polynomial/end.m Wed Jan 13 17:28:42 2016 +1100 +++ b/examples/code/@polynomial/end.m Wed Jan 13 16:21:24 2016 -0800 @@ -1,7 +1,7 @@ function r = end (obj, index_pos, num_indices) if (num_indices != 1) - error ("polynomial object may only have one index") + error ("polynomial object may only have one index"); endif r = length (obj.poly) - 1; diff -r 759fcdf3666d -r 73ab962bc52d examples/code/@polynomial/get.m --- a/examples/code/@polynomial/get.m Wed Jan 13 17:28:42 2016 +1100 +++ b/examples/code/@polynomial/get.m Wed Jan 13 16:21:24 2016 -0800 @@ -1,18 +1,22 @@ function s = get (p, f) + + if (nargin < 1 || nargin > 2) + print_usage (); + endif + if (nargin == 1) s.poly = p.poly; - elseif (nargin == 2) - if (ischar (f)) - switch (f) - case "poly" - s = p.poly; - otherwise - error ("@polynomial/get: invalid property %s", f); - endswitch - else + else + if (! ischar (f)) error ("@polynomial/get: expecting the property to be a string"); endif - else - print_usage (); + + switch (f) + case "poly" + s = p.poly; + otherwise + error ("@polynomial/get: invalid property %s", f); + endswitch endif + endfunction diff -r 759fcdf3666d -r 73ab962bc52d examples/code/@polynomial/numel.m --- a/examples/code/@polynomial/numel.m Wed Jan 13 17:28:42 2016 +1100 +++ b/examples/code/@polynomial/numel.m Wed Jan 13 16:21:24 2016 -0800 @@ -1,7 +1,4 @@ function n = numel (obj, idx) - - # always produce an array. - n = 1; - + n = 1; # always produce an array. endfunction diff -r 759fcdf3666d -r 73ab962bc52d examples/code/@polynomial/plot.m --- a/examples/code/@polynomial/plot.m Wed Jan 13 17:28:42 2016 +1100 +++ b/examples/code/@polynomial/plot.m Wed Jan 13 16:21:24 2016 -0800 @@ -1,4 +1,5 @@ function h = plot (p, varargin) + n = 128; rmax = max (abs (roots (p.poly))); x = [0 : (n - 1)] / (n - 1) * 2.2 * rmax - 1.1 * rmax; @@ -7,4 +8,5 @@ else plot (x, p(x), varargin{:}); endif + endfunction diff -r 759fcdf3666d -r 73ab962bc52d examples/code/@polynomial/polynomial.m --- a/examples/code/@polynomial/polynomial.m Wed Jan 13 17:28:42 2016 +1100 +++ b/examples/code/@polynomial/polynomial.m Wed Jan 13 16:21:24 2016 -0800 @@ -12,19 +12,23 @@ ## @end deftypefn function p = polynomial (a) + + if (nargin > 1) + print_usage (); + endif + if (nargin == 0) p.poly = [0]; p = class (p, "polynomial"); - elseif (nargin == 1) + else if (strcmp (class (a), "polynomial")) p = a; elseif (isvector (a) && isreal (a)) - p.poly = a(:).'; + p.poly = a(:).'; # force row vector p = class (p, "polynomial"); else error ("@polynomial: expecting real vector"); endif - else - print_usage (); endif + endfunction diff -r 759fcdf3666d -r 73ab962bc52d examples/code/@polynomial/polynomial_superiorto.m --- a/examples/code/@polynomial/polynomial_superiorto.m Wed Jan 13 17:28:42 2016 +1100 +++ b/examples/code/@polynomial/polynomial_superiorto.m Wed Jan 13 16:21:24 2016 -0800 @@ -12,20 +12,25 @@ ## @end deftypefn function p = polynomial (a) + + if (nargin > 1) + print_usage (); + endif + if (nargin == 0) p.poly = [0]; p = class (p, "polynomial"); - elseif (nargin == 1) + else if (strcmp (class (a), "polynomial")) p = a; elseif (isvector (a) && isreal (a)) - p.poly = a(:).'; + p.poly = a(:).'; # force row vector p = class (p, "polynomial"); else error ("@polynomial: expecting real vector"); endif - else - print_usage (); endif + superiorto ("double"); + endfunction diff -r 759fcdf3666d -r 73ab962bc52d examples/code/@polynomial/polyval.m --- a/examples/code/@polynomial/polyval.m Wed Jan 13 17:28:42 2016 +1100 +++ b/examples/code/@polynomial/polyval.m Wed Jan 13 16:21:24 2016 -0800 @@ -1,7 +1,9 @@ function [y, dy] = polyval (p, varargin) + if (nargout == 2) [y, dy] = polyval (fliplr (p.poly), varargin{:}); else y = polyval (fliplr (p.poly), varargin{:}); endif + endfunction diff -r 759fcdf3666d -r 73ab962bc52d examples/code/@polynomial/roots.m --- a/examples/code/@polynomial/roots.m Wed Jan 13 17:28:42 2016 +1100 +++ b/examples/code/@polynomial/roots.m Wed Jan 13 16:21:24 2016 -0800 @@ -1,3 +1,3 @@ function y = roots (p) - y = roots(fliplr(p.poly)); -endfunction \ No newline at end of file + y = roots (fliplr (p.poly)); +endfunction diff -r 759fcdf3666d -r 73ab962bc52d examples/code/@polynomial/set.m --- a/examples/code/@polynomial/set.m Wed Jan 13 17:28:42 2016 +1100 +++ b/examples/code/@polynomial/set.m Wed Jan 13 16:21:24 2016 -0800 @@ -1,20 +1,21 @@ function s = set (p, varargin) - s = p; - if (length (varargin) < 2 || rem (length (varargin), 2) != 0) + + if (numel (varargin) < 2 || rem (numel (varargin), 2) != 0) error ("@polynomial/set: expecting property/value pairs"); endif - while (length (varargin) > 1) + + s = p; + while (numel (varargin) > 1) prop = varargin{1}; - val = varargin{2}; + val = varargin{2}; varargin(1:2) = []; - if (ischar (prop) && strcmp (prop, "poly")) - if (isvector (val) && isreal (val)) - s.poly = val(:).'; - else - error ("@polynomial/set: expecting the value to be a real vector"); - endif - else + 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"); endif + + s.poly = val(:).'; # force row vector endwhile + endfunction diff -r 759fcdf3666d -r 73ab962bc52d examples/code/@polynomial/subsasgn.m --- a/examples/code/@polynomial/subsasgn.m Wed Jan 13 17:28:42 2016 +1100 +++ b/examples/code/@polynomial/subsasgn.m Wed Jan 13 16:21:24 2016 -0800 @@ -1,35 +1,38 @@ function p = subsasgn (p, s, val) - if (length (s) < 1) + + if (isempty (s)) error ("@polynomial/subsasgn: needs index"); endif + switch (s(1).type) + case "{}" ind = s(1).subs; if (numel (ind) != 1) error ("@polynomial/subsasgn: need exactly one index"); + elseif (numel (s) != 1) + error ("@polynomial/subsasgn: chained subscripts not allowed for {}"); + endif + + if (isnumeric (ind{1})) + p.poly(ind{1}+1) = val; else - if (length (s) == 1) - if (isnumeric (ind{1})) - p.poly(ind{1}+1) = val; - else - p.poly(ind{1}) = val; - endif - else - error ("@polynomial/subsasgn: chained subscripts not allowed for {}"); - endif + p.poly(ind{1}) = val; endif + case "." fld = s(1).subs; - if (strcmp (fld, "poly")) - if (length (s) == 1) - p.poly = val; - else - p.poly = subsasgn (p.poly, s(2:end), val); - endif - else + if (! strcmp (fld, "poly")) error ("@polynomial/subsasgn: invalid property \"%s\"", fld); endif + if (numel (s) == 1) + p.poly = val; + else + p.poly = subsasgn (p.poly, s(2:end), val); + endif + otherwise error ("@polynomial/subsasgn: invalid subscript type"); endswitch + endfunction diff -r 759fcdf3666d -r 73ab962bc52d examples/code/@polynomial/subsref.m --- a/examples/code/@polynomial/subsref.m Wed Jan 13 17:28:42 2016 +1100 +++ b/examples/code/@polynomial/subsref.m Wed Jan 13 16:21:24 2016 -0800 @@ -1,37 +1,44 @@ function b = subsref (a, s) + if (isempty (s)) error ("@polynomial/subsref: missing index"); endif + switch (s(1).type) + case "()" ind = s(1).subs; if (numel (ind) != 1) error ("@polynomial/subsref: need exactly one index"); - else - b = polyval (fliplr (a.poly), ind{1}); endif + b = polyval (fliplr (a.poly), ind{1}); + case "{}" ind = s(1).subs; if (numel (ind) != 1) error ("@polynomial/subsref: need exactly one index"); + endif + + if (isnumeric (ind{1})) + b = a.poly(ind{1}+1); else - if (isnumeric (ind{1})) - b = a.poly(ind{1}+1); - else - b = a.poly(ind{1}); - endif + b = a.poly(ind{1}); endif + case "." fld = s.subs; - if (strcmp (fld, "poly")) - b = a.poly; - else + if (! strcmp (fld, "poly")) error ("@polynomial/subsref: invalid property \"%s\"", fld); endif + b = a.poly; + otherwise error ("@polynomial/subsref: invalid subscript type"); + endswitch + if (numel (s) > 1) b = subsref (b, s(2:end)); endif + endfunction diff -r 759fcdf3666d -r 73ab962bc52d examples/code/celldemo.cc --- a/examples/code/celldemo.cc Wed Jan 13 17:28:42 2016 +1100 +++ b/examples/code/celldemo.cc Wed Jan 13 16:21:24 2016 -0800 @@ -3,13 +3,14 @@ DEFUN_DLD (celldemo, args, , "Cell Demo") { - octave_value_list retval; - if (args.length () != 1) print_usage (); Cell c = args(0).cell_value (); + octave_value_list retval; + retval.resize (c.numel ()); // faster code by pre-declaring size + for (octave_idx_type i = 0; i < c.numel (); i++) { retval(i) = c(i); // using operator syntax diff -r 759fcdf3666d -r 73ab962bc52d examples/code/fortrandemo.cc --- a/examples/code/fortrandemo.cc Wed Jan 13 17:28:42 2016 +1100 +++ b/examples/code/fortrandemo.cc Wed Jan 13 16:21:24 2016 -0800 @@ -10,8 +10,6 @@ DEFUN_DLD (fortrandemo, args, , "Fortran Demo") { - octave_value_list retval; - if (args.length () != 1) print_usage (); @@ -25,8 +23,5 @@ F77_XFCN (fortransub, FORTSUB, (na, av, ctmp F77_CHAR_ARG_LEN (128))); - retval(1) = std::string (ctmp); - retval(0) = a; - - return retval; + return ovl (a, std::string (ctmp)); } diff -r 759fcdf3666d -r 73ab962bc52d examples/code/funcdemo.cc --- a/examples/code/funcdemo.cc Wed Jan 13 17:28:42 2016 +1100 +++ b/examples/code/funcdemo.cc Wed Jan 13 16:21:24 2016 -0800 @@ -3,8 +3,6 @@ DEFUN_DLD (funcdemo, args, nargout, "Function Demo") { - octave_value_list retval; - int nargin = args.length (); if (nargin < 2) @@ -15,6 +13,8 @@ for (octave_idx_type i = nargin - 1; i > 0; i--) newargs(i-1) = args(i); + octave_value_list retval; + if (args(0).is_function_handle () || args(0).is_inline_function ()) { octave_function *fcn = args(0).function_value (); diff -r 759fcdf3666d -r 73ab962bc52d examples/code/globaldemo.cc --- a/examples/code/globaldemo.cc Wed Jan 13 17:28:42 2016 +1100 +++ b/examples/code/globaldemo.cc Wed Jan 13 16:21:24 2016 -0800 @@ -2,11 +2,11 @@ DEFUN_DLD (globaldemo, args, , "Global Demo") { - octave_value retval; - if (args.length () != 1) print_usage (); + octave_value retval; + std::string s = args(0).string_value (); octave_value tmp = get_global_value (s, true); diff -r 759fcdf3666d -r 73ab962bc52d examples/code/oct_demo.cc --- a/examples/code/oct_demo.cc Wed Jan 13 17:28:42 2016 +1100 +++ b/examples/code/oct_demo.cc Wed Jan 13 16:21:24 2016 -0800 @@ -46,17 +46,20 @@ // 3) The number of output arguments // 4) A string to use as help text if 'help ' is entered. // -// Note below that the third parameter (nargout) of DEFUN_DLD is not used, -// so it is omitted from the list of arguments in order to avoid a warning -// from gcc about an unused function parameter. +// Note below that the third parameter (nargout) of DEFUN_DLD is not used. -DEFUN_DLD (oct_demo, args, , +DEFUN_DLD (oct_demo, args, /* nargout */, "[...] = oct_demo (...)\n\ \n\ -Print a greeting followed by the values of all the arguments passed.\n\ +Print a greeting followed by the values of all input arguments.\n\ +\n\ Return all arguments in reverse order.") { - // The list of values to return. See the declaration in oct-obj.h + // The inputs to this are available in the variable named args. + + int nargin = args.length (); + + // The list of values to return. See the declaration in ovl.h. octave_value_list retval; @@ -64,10 +67,6 @@ octave_stdout << "Hello, world!\n"; - // The inputs to this function are available in args. - - int nargin = args.length (); - // The octave_value_list class is a zero-based array of octave_value objects. // The declaration for the octave_value class is in the file ov.h. // The print() method will send its output to octave_stdout, diff -r 759fcdf3666d -r 73ab962bc52d examples/code/paramdemo.cc --- a/examples/code/paramdemo.cc Wed Jan 13 17:28:42 2016 +1100 +++ b/examples/code/paramdemo.cc Wed Jan 13 16:21:24 2016 -0800 @@ -27,5 +27,5 @@ if (m.all_integers (min_val, max_val)) octave_stdout << " includes only integers in [-10,10]\n"; - return octave_value (); + return octave_value_list (); } diff -r 759fcdf3666d -r 73ab962bc52d examples/code/stringdemo.cc --- a/examples/code/stringdemo.cc Wed Jan 13 17:28:42 2016 +1100 +++ b/examples/code/stringdemo.cc Wed Jan 13 16:21:24 2016 -0800 @@ -2,11 +2,11 @@ DEFUN_DLD (stringdemo, args, , "String Demo") { - octave_value_list retval; - if (args.length () != 1) print_usage (); + octave_value_list retval; + charMatrix ch = args(0).char_matrix_value (); retval(1) = octave_value (ch, '\''); // Single Quote String