[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Patch for OOP example.
From: |
Robert T. Short |
Subject: |
Patch for OOP example. |
Date: |
Sun, 31 May 2009 21:17:36 -0700 |
User-agent: |
Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.21) Gecko/20090402 SeaMonkey/1.1.16 |
Attached is a patch to the polynomial object example. This fixes
several things.
- Tabs in the source files made the documentation goofy.
- There were several methods that didn't work. They seem to work now.
- Some simplifications.
How is this going to work without savannah? My tip is out of date and
will probably get worse.
Should we just hold these patches?
Bob
--
Robert T. Short, Ph.D.
PhaseLocked Systems
# HG changeset patch
# User Robert T. Short <address@hidden>
# Date 1243829491 25200
# Node ID 97e9fcb1a36e70652a865c7cee966614339ecbfc
# Parent e5fb8a804aa1e107e79e5203978a3a4bbdbda4f7
* examples/@polynomial: removed tabs from all functions so
documentation would look right. Fixed a bunch of methods
that didn't work. Added a method referred to in the docs
that didn't exist.
diff -r e5fb8a804aa1 -r 97e9fcb1a36e examples/@polynomial/display.m
--- a/examples/@polynomial/display.m Sun May 24 10:23:07 2009 -0700
+++ b/examples/@polynomial/display.m Sun May 31 21:11:31 2009 -0700
@@ -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
-
diff -r e5fb8a804aa1 -r 97e9fcb1a36e examples/@polynomial/end.m
--- a/examples/@polynomial/end.m Sun May 24 10:23:07 2009 -0700
+++ b/examples/@polynomial/end.m Sun May 31 21:11:31 2009 -0700
@@ -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
diff -r e5fb8a804aa1 -r 97e9fcb1a36e examples/@polynomial/get.m
--- a/examples/@polynomial/get.m Sun May 24 10:23:07 2009 -0700
+++ b/examples/@polynomial/get.m Sun May 31 21:11:31 2009 -0700
@@ -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");
diff -r e5fb8a804aa1 -r 97e9fcb1a36e examples/@polynomial/mtimes.m
--- a/examples/@polynomial/mtimes.m Sun May 24 10:23:07 2009 -0700
+++ b/examples/@polynomial/mtimes.m Sun May 31 21:11:31 2009 -0700
@@ -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
diff -r e5fb8a804aa1 -r 97e9fcb1a36e examples/@polynomial/polynomial.m
--- a/examples/@polynomial/polynomial.m Sun May 24 10:23:07 2009 -0700
+++ b/examples/@polynomial/polynomial.m Sun May 31 21:11:31 2009 -0700
@@ -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
diff -r e5fb8a804aa1 -r 97e9fcb1a36e
examples/@polynomial/polynomial_superiorto.m
--- a/examples/@polynomial/polynomial_superiorto.m Sun May 24 10:23:07
2009 -0700
+++ b/examples/@polynomial/polynomial_superiorto.m Sun May 31 21:11:31
2009 -0700
@@ -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 ();
diff -r e5fb8a804aa1 -r 97e9fcb1a36e examples/@polynomial/polyval.m
--- a/examples/@polynomial/polyval.m Sun May 24 10:23:07 2009 -0700
+++ b/examples/@polynomial/polyval.m Sun May 31 21:11:31 2009 -0700
@@ -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
diff -r e5fb8a804aa1 -r 97e9fcb1a36e examples/@polynomial/roots.m
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/@polynomial/roots.m Sun May 31 21:11:31 2009 -0700
@@ -0,0 +1,3 @@
+function y = roots (p)
+ y = roots(fliplr(p.poly));
+endfunction
\ No newline at end of file
diff -r e5fb8a804aa1 -r 97e9fcb1a36e examples/@polynomial/set.m
--- a/examples/@polynomial/set.m Sun May 24 10:23:07 2009 -0700
+++ b/examples/@polynomial/set.m Sun May 31 21:11:31 2009 -0700
@@ -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
diff -r e5fb8a804aa1 -r 97e9fcb1a36e examples/@polynomial/subsasgn.m
--- a/examples/@polynomial/subsasgn.m Sun May 24 10:23:07 2009 -0700
+++ b/examples/@polynomial/subsasgn.m Sun May 31 21:11:31 2009 -0700
@@ -1,12 +1,18 @@
-function s = subsasgn (s, index, val)
+function p = subsasgn (p, index, val)
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
diff -r e5fb8a804aa1 -r 97e9fcb1a36e examples/@polynomial/subsref.m
--- a/examples/@polynomial/subsref.m Sun May 24 10:23:07 2009 -0700
+++ b/examples/@polynomial/subsref.m Sun May 31 21:11:31 2009 -0700
@@ -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
- Patch for OOP example.,
Robert T. Short <=
- Re: Patch for OOP example., Rob Mahurin, 2009/06/01
- Re: Patch for OOP example., Jaroslav Hajek, 2009/06/01
- Re: Patch for OOP example., Robert T. Short, 2009/06/01
- Re: Patch for OOP example., Jaroslav Hajek, 2009/06/02
- Re: Patch for OOP example., John W. Eaton, 2009/06/02
- Re: Patch for OOP example., Jaroslav Hajek, 2009/06/02
- Re: Patch for OOP example., John W. Eaton, 2009/06/02
- Re: Patch for OOP example., Jaroslav Hajek, 2009/06/02
- Re: Patch for OOP example., Robert T. Short, 2009/06/02