From: <sch...@us...> - 2009-03-11 11:31:08
|
Revision: 5603 http://octave.svn.sourceforge.net/octave/?rev=5603&view=rev Author: schloegl Date: 2009-03-11 10:37:04 +0000 (Wed, 11 Mar 2009) Log Message: ----------- significant speed improvement Modified Paths: -------------- trunk/octave-forge/extra/NaN/inst/meansq.m trunk/octave-forge/extra/NaN/inst/nanstd.m trunk/octave-forge/extra/NaN/inst/std.m trunk/octave-forge/extra/NaN/inst/var.m Added Paths: ----------- trunk/octave-forge/extra/NaN/inst/sumsq.m Modified: trunk/octave-forge/extra/NaN/inst/meansq.m =================================================================== --- trunk/octave-forge/extra/NaN/inst/meansq.m 2009-03-11 08:27:24 UTC (rev 5602) +++ trunk/octave-forge/extra/NaN/inst/meansq.m 2009-03-11 10:37:04 UTC (rev 5603) @@ -1,4 +1,4 @@ -function o=meansq(i,DIM) +function o=meansq(x,DIM) % MEANSQ calculates the mean of the squares % % y = meansq(x,DIM) @@ -30,21 +30,18 @@ % along with this program; If not, see <http://www.gnu.org/licenses/>. -% original Copyright by: KH <Kur...@ci...> -% -% Copyright (C) 2000-2003 by Alois Schloegl <a.s...@ie...> -% $Revision$ +% Copyright (C) 2000-2003,2009 by Alois Schloegl <a.s...@ie...> % $Id$ +% This is part of the NaN-toolbox for Octave and Matlab +% see also: http://hci.tugraz.at/schloegl/matlab/NaN/ -x = real(i).^2+imag(i).^2; - if nargin<2, - [o,N] = sumskipnan(x); + [o,N,ssq] = sumskipnan(x); else - [o,N] = sumskipnan(x,DIM); + [o,N,ssq] = sumskipnan(x,DIM); end; -o = o./N; +o = ssq./N; Modified: trunk/octave-forge/extra/NaN/inst/nanstd.m =================================================================== --- trunk/octave-forge/extra/NaN/inst/nanstd.m 2009-03-11 08:27:24 UTC (rev 5602) +++ trunk/octave-forge/extra/NaN/inst/nanstd.m 2009-03-11 10:37:04 UTC (rev 5603) @@ -1,4 +1,4 @@ -function [y] = nanstd(i,FLAG,DIM) +function [y] = nanstd(x,FLAG,DIM) % NANSTD same as STD but ignores NaN's. % NANSTD is OBSOLETE; use NaN/STD instead. NANSTD is included % to fix a bug in alternative implementations and to @@ -17,8 +17,8 @@ % % see also: SUM, SUMSKIPNAN, NANSUM, STD -% Copyright (C) 2000-2003,2006,2008 by Alois Schloegl <a.s...@ie...> % $Id$ +% Copyright (C) 2000-2003,2006,2008,2009 by Alois Schloegl <a.s...@ie...> % This is part of the NaN-toolbox. For more details see % http://www.dpmi.tu-graz.ac.at/~schloegl/matlab/NaN/ @@ -47,7 +47,14 @@ if isempty(DIM), DIM=1; end; end; -[y,n] = sumskipnan(center(i,DIM).^2,DIM); +[y,n,ssq] = sumskipnan(x,DIM); +if all(ssq(:).*n(:) > 2*(y(:).^2)) + %% rounding error is neglectable + y = ssq - y.*y./n; +else + %% rounding error is not neglectable + [y,n] = sumskipnan(center(x,DIM).^2,DIM); +end; if (FLAG==1) y = sqrt(y)./n; % normalize with N Modified: trunk/octave-forge/extra/NaN/inst/std.m =================================================================== --- trunk/octave-forge/extra/NaN/inst/std.m 2009-03-11 08:27:24 UTC (rev 5602) +++ trunk/octave-forge/extra/NaN/inst/std.m 2009-03-11 10:37:04 UTC (rev 5603) @@ -43,9 +43,8 @@ % You should have received a copy of the GNU General Public License % along with this program; If not, see <http://www.gnu.org/licenses/>. -% $Revision$ % $Id$ -% Copyright (C) 2000-2003, 2006 by Alois Schloegl <a.s...@ie...> +% Copyright (C) 2000-2003, 2006,2009 by Alois Schloegl <a.s...@ie...> % This is part of the NaN-toolbox for Octave and Matlab % see also: http://hci.tugraz.at/schloegl/matlab/NaN/ @@ -57,8 +56,17 @@ if isempty(DIM), DIM=1; end; end; -[y,n] = sumskipnan(center(x,DIM).^2,DIM); +[y,n,ssq] = sumskipnan(x,DIM); +if all(ssq(:).*n(:) > 2*(y(:).^2)) + %% rounding error is neglectable + y = ssq - y.*y./n; +else + %% rounding error is not neglectable + [y,n] = sumskipnan(center(x,DIM).^2,DIM); +end; + + if nargin<2, opt = 0; end; Added: trunk/octave-forge/extra/NaN/inst/sumsq.m =================================================================== --- trunk/octave-forge/extra/NaN/inst/sumsq.m (rev 0) +++ trunk/octave-forge/extra/NaN/inst/sumsq.m 2009-03-11 10:37:04 UTC (rev 5603) @@ -0,0 +1,50 @@ +function [o]=sumsq(x,DIM) +% SUMSQ calculates the sum of squares. +% +% [y] = sumsq(x [, DIM]) +% +% DIM dimension +% N STD of N-th dimension +% default or []: first DIMENSION, with more than 1 element +% +% y estimated standard deviation +% +% features: +% - can deal with NaN's (missing values) +% - dimension argument also in Octave +% - compatible to Matlab and Octave +% +% see also: RMS, SUMSKIPNAN, MEAN, VAR, MEANSQ, +% +% +% References(s): + + +% This program is free software; you can redistribute it and/or modify +% it under the terms of the GNU General Public License as published by +% the Free Software Foundation; either version 2 of the License, or +% (at your option) any later version. +% +% This program is distributed in the hope that it will be useful, +% but WITHOUT ANY WARRANTY; without even the implied warranty of +% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +% GNU General Public License for more details. +% +% You should have received a copy of the GNU General Public License +% along with this program; If not, see <http://www.gnu.org/licenses/>. + +% $Id$ +% Copyright (C) 2009 by Alois Schloegl <a.s...@ie...> +% This is part of the NaN-toolbox for Octave and Matlab +% see also: http://hci.tugraz.at/schloegl/matlab/NaN/ + +if nargin<2, + DIM = []; +end; +if isempty(DIM), + DIM = min(find(size(x)>1)); + if isempty(DIM), DIM=1; end; +end; + +[s,n,o] = sumskipnan(x,DIM); + Modified: trunk/octave-forge/extra/NaN/inst/var.m =================================================================== --- trunk/octave-forge/extra/NaN/inst/var.m 2009-03-11 08:27:24 UTC (rev 5602) +++ trunk/octave-forge/extra/NaN/inst/var.m 2009-03-11 10:37:04 UTC (rev 5603) @@ -33,9 +33,10 @@ % You should have received a copy of the GNU General Public License % along with this program; If not, see <http://www.gnu.org/licenses/>. -% $Revision$ % $Id$ -% Copyright (C) 2000-2003,2006 by Alois Schloegl <a.s...@ie...> +% Copyright (C) 2000-2003,2006,2009 by Alois Schloegl <a.s...@ie...> +% This is part of the NaN-toolbox for Octave and Matlab +% see also: http://hci.tugraz.at/schloegl/matlab/NaN/ if nargin>1, if ~isempty(opt) & opt~=0, @@ -53,7 +54,14 @@ if isempty(DIM), DIM=1; end; end; -[y,n] = sumskipnan(center(x,DIM).^2,DIM); +[y,n,ssq] = sumskipnan(x,DIM); +if all(ssq(:).*n(:) > 2*(y(:).^2)) + %% rounding error is neglectable + y = ssq - y.*y./n; +else + %% rounding error is not neglectable + [y,n] = sumskipnan(center(x,DIM).^2,DIM); +end; if (opt~=1) n = max(n-1,0); % in case of n=0 and n=1, the (biased) variance, STD and STE are INF This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |