help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

is a piece of code in 'polyval' really needed ?


From: Sergei Steshenko
Subject: is a piece of code in 'polyval' really needed ?
Date: Thu, 18 Aug 2011 08:31:36 -0700 (PDT)

Hello,

looking at 'polyval' implementation in octave-3.4.2 I see this:

     42 function [y, dy] = polyval (p, x, s, mu)
     43
     44   if (nargin < 2 || nargin > 4 || (nargout == 2 && nargin < 3))
     45     print_usage ();
     46   endif
     47
     48   if (nargin < 3)
     49     s = [];
     50   endif
     51
     52   if (! (isvector (p) || isempty (p)))
     53     error ("polyval: first argument must be a vector");
     54   endif
     55
     56   if (nargin > 3)
     57     x = (x - mu(1)) / mu(2);
     58   endif
     59
     60   if (isempty (x))
     61     y = [];
     62     return;
     63   endif
     64
     65   if (length (p) == 0)
     66     y = p;
     67     return;
     68   endif
     69
     70   n = length (p) - 1;
     71   y = p(1) * ones (size (x));
     72   for i = 2:n+1
     73     y = y .* x + p(i);
     74   endfor
     75
     76   if (nargout == 2)
     77     ## Note: the F-Distribution is generally considered to be 
single-sided.
     78     ## http://www.itl.nist.gov/div898/handbook/eda/section3/eda3673.htm
     79     ##   t = finv (1-alpha, s.df, s.df);
     80     ##   dy = t * sqrt (1 + sumsq (A/s.R, 2)) * s.normr / sqrt (s.df)
     81     ## If my inference is correct, then t must equal 1 for polyval.
     82     ## This is because finv (0.5, n, n) = 1.0 for any n.
     83     k = numel (x);
     84     A = (x(:) * ones (1, n+1)) .^ (ones (k, 1) * (n:-1:0));
     85     dy = sqrt (1 + sumsq (A/s.R, 2)) * s.normr / sqrt (s.df);
     86     dy = reshape (dy, size (x));
     87   endif
     88
     89 endfunction
.

Is this:

     65   if (length (p) == 0)
     66     y = p;
     67     return;
     68   endif

really needed ? My point is that, if I understand correctly,

     52   if (! (isvector (p) || isempty (p)))

check won't let code on lines ##65 .. 68 to be executed.

'polyval' is a ubiquitous function, so it should be as fast as
possible - if the 'if' in question is unneeded, it makes the
function slower for no good reason.

And shouldn't 'polyval' because of this be implemented in
C++ ?

Thanks,
  Sergei.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]