changeset 5741:f0955d62224a octave-forge

faster exist() improves performance (especially in covm.m)
author schloegl
date Wed, 10 Jun 2009 12:27:10 +0000
parents 42801cc0cf04
children b2b2a17193c3
files extra/NaN/inst/corrcoef.m extra/NaN/inst/covm.m extra/NaN/inst/naninsttest.m extra/NaN/inst/nantest.m extra/NaN/inst/partcorrcoef.m extra/NaN/inst/sumskipnan.m
diffstat 6 files changed, 73 insertions(+), 54 deletions(-) [+]
line wrap: on
line diff
--- a/extra/NaN/inst/corrcoef.m	Wed Jun 10 08:55:27 2009 +0000
+++ b/extra/NaN/inst/corrcoef.m	Wed Jun 10 12:27:10 2009 +0000
@@ -311,7 +311,7 @@
 % CONFIDENCE INTERVAL
 if isfield(mode,'alpha')
 	alpha = mode.alpha; 
-elseif exist('flag_implicit_significance')==2,
+elseif exist('flag_implicit_significance','file'),
         alpha = flag_implicit_significance;
 else
 	alpha = 0.01;        
@@ -324,9 +324,9 @@
 tmp(tmp<0) = 0;		% prevent tmp<0 i.e. imag(t)~=0 
 t   = R.*sqrt(max(NN-2,0)./tmp);
 
-if exist('t_cdf')>1;
+if exist('t_cdf','file');
         sig = t_cdf(t,NN-2);
-elseif exist('tcdf')>1;
+elseif exist('tcdf','file')>1;
         sig = tcdf(t,NN-2);
 else
         fprintf('CORRCOEF: significance test not completed because of missing TCDF-function\n')
--- a/extra/NaN/inst/covm.m	Wed Jun 10 08:55:27 2009 +0000
+++ b/extra/NaN/inst/covm.m	Wed Jun 10 12:27:10 2009 +0000
@@ -100,8 +100,17 @@
         warning('Covariance is ill-defined, because of too few observations (rows)');
 end;
 
-mexFLAG2 = exist('covm_mex','file');	
-mexFLAG  = exist('sumskipnan_mex','file');	
+
+persistent mexFLAG2; 
+persistent mexFLAG; 
+if isempty(mexFLAG2) 
+	mexFLAG2 = exist('covm_mex','file');	
+end; 
+if isempty(mexFLAG) 
+	mexFLAG = exist('sumskipnan_mex','file');	
+end; 
+
+
 if ~isempty(W)
 	W = W(:);
 	if (r1~=numel(W))
--- a/extra/NaN/inst/naninsttest.m	Wed Jun 10 08:55:27 2009 +0000
+++ b/extra/NaN/inst/naninsttest.m	Wed Jun 10 12:27:10 2009 +0000
@@ -1,6 +1,6 @@
 % NANINSTTEST checks whether the functions from NaN-toolbox have been
-% correctly installed. 
-%
+% correctly installed. 
+%
 % see also: NANTEST
 
 %    This program is free software; you can redistribute it and/or modify
@@ -27,8 +27,8 @@
 
 % run test, k=1: with NaNs, k=2: all NaN's are removed
 % the result of both should be the same. 
-
-FLAG_WARNING = warning;
+
+%FLAG_WARNING = warning;
 warning('off');
 
 funlist = {'sumskipnan','mean','std','var','skewness','kurtosis','sem','median','mad','zscore','coefficient_of_variation','geomean','harmmean','meansq','moment','rms','','corrcoef','rankcorr','spearman','ranks','center','trimean','min','max','tpdf','tcdf','tinv','normpdf','normcdf','norminv','nansum','nanstd','','','','','','','','','','','',''};
@@ -55,7 +55,7 @@
         end;
         if exist('moment')==2,
                 r(15,k)=moment(x,6);
-        end;
+        end;
         if exist('rms')==2,
                 r(16,k)=rms(x);
         end;
@@ -81,51 +81,51 @@
         	r(23,k)=trimean(x);
         end;
         r(24,k)=min(x);
-        r(25,k)=max(x);
-        
-        if exist('tpdf')==2, 
+        r(25,k)=max(x);
+        
+        if exist('tpdf')==2, 
                 fun='tpdf'; 
         elseif exist('t_pdf')==2,
-                fun='t_pdf';
-        end;
-        r(26,k) = k+isnan(feval(fun,x(2),4));	        
-        
-        if exist('tcdf')==2, 
+                fun='t_pdf';
+        end;
+        r(26,k) = k+isnan(feval(fun,x(2),4));	        
+        
+        if exist('tcdf')==2, 
                 fun='tcdf'; 
         elseif exist('t_cdf')==2,
-                fun='t_cdf';
-        end;
-        try, 
-                r(27,k) = k*(~isnan(feval(fun,nan,4)));	        
-        catch
-                r(27,k) = k;	
-        end;
-        
-        if exist('tinv')==2, 
+                fun='t_cdf';
+        end;
+        try, 
+                r(27,k) = k*(~isnan(feval(fun,nan,4)));	        
+        catch
+                r(27,k) = k;	
+        end;
+        
+        if exist('tinv')==2, 
                 fun='tinv'; 
         elseif exist('t_inv')==2,
-                fun='t_inv';
-        end;
-        r(28,k) = k*(~isnan(feval(fun,NaN,4)));	        
-        
-        if exist('normpdf')==2, 
+                fun='t_inv';
+        end;
+        r(28,k) = k*(~isnan(feval(fun,NaN,4)));	        
+        
+        if exist('normpdf')==2, 
                 fun='normpdf'; 
         elseif exist('normal_pdf')==2,
-                fun='normal_pdf';
-        end;
-        r(29,k) = (feval(fun,k,k,0)~=Inf)*k;	        
-        if exist('normcdf')==2, 
+                fun='normal_pdf';
+        end;
+        r(29,k) = (feval(fun,k,k,0)~=Inf)*k;	        
+        if exist('normcdf')==2, 
                 fun='normcdf'; 
         elseif exist('normal_cdf')==2,
-                fun='normal_cdf';
-        end;
-        r(30,k) = feval(fun,4,4,0);	        
-        if exist('norminv')==2, 
+                fun='normal_cdf';
+        end;
+        r(30,k) = feval(fun,4,4,0);	        
+        if exist('norminv')==2, 
                 fun='norminv'; 
         elseif exist('normal_inv')==2,
-                fun='normal_inv';
-        end;
-        r(31,k) = k*any(isnan(feval(fun,[0,1],4,0)));	        
+                fun='normal_inv';
+        end;
+        r(31,k) = k*any(isnan(feval(fun,[0,1],4,0)));	        
         if exist('nansum')==2,
         	r(32,k)=k*isnan(nansum(nan));
         end;
@@ -138,7 +138,7 @@
 tmp = abs(r(:,1)-r(:,2))<eps;
 
 q = zeros(1,5);
-
+
 % output
 if all(tmp) & all(~q),
         fprintf(1,'NANINSTTEST successful - your NaN-tools are correctly installed\n');
@@ -148,7 +148,7 @@
 	fprintf(1,'  - %s\n',funlist{find(~tmp)});
 	fprintf(1,'Its recommended to install the NaN-toolbox.\n');
 end;
-
-warning(FLAG_WARNING);
+
+%warning(FLAG_WARNING);
 
 
--- a/extra/NaN/inst/nantest.m	Wed Jun 10 08:55:27 2009 +0000
+++ b/extra/NaN/inst/nantest.m	Wed Jun 10 12:27:10 2009 +0000
@@ -106,6 +106,13 @@
         end;
 end;
 
+q(9) = isreal(double([2+3i]));
+if q(9)
+	printf('DOUBLE rejects imaginary part\n-> this can affect SUMSKIPNAN\n');
+end; 
+
+
+if 0,
 %%%%% MOD 
 if exist('mod')>1,
         if (mod(5,0))~=0,
@@ -130,6 +137,7 @@
                 fprintf(1,'WARNING: REM(x,INF) returns NaN.\n');
         end;
 end;
+end; 
 
 %%%%% NANSUM(NAN) - this test addresses a problem in Matlab 5.3, 6.1 & 6.5
 if exist('nansum')==2,
@@ -208,7 +216,6 @@
 %(roots([5,0,0])-[0;0])
 %(roots([2,-10,12])-[3;2])
 %(roots([2e-37,-2,2])-[1e37;1])
-
 %%%%% check nan/nan   %% this test addresses a problem in Matlab 5.3, 6.1 & 6.5
 p    = 4;
 tmp1 = repmat(nan,p)/repmat(nan,p);
@@ -248,6 +255,8 @@
 
 tmp  = [tmp1;tmp2;tmp3;tmp4;tmp5;tmp6;tmp7;tmp8];
 
+
+
 %warning(FLAG_WARNING); 
 
 
--- a/extra/NaN/inst/partcorrcoef.m	Wed Jun 10 08:55:27 2009 +0000
+++ b/extra/NaN/inst/partcorrcoef.m	Wed Jun 10 12:27:10 2009 +0000
@@ -47,7 +47,7 @@
 % [1] http://www.tufts.edu/~gdallal/partial.htm
 % [2] http://www.nag.co.uk/numeric/fl/manual/pdf/G02/g02byf.pdf
 
-%       $Id: corrcoef.m 5608 2009-03-12 16:46:34Z schloegl $
+%       $Id$
 %       Copyright (C) 2000-2002,2009 by Alois Schloegl <a.schloegl@ieee.org>	
 %       This function is part of the NaN-toolbox
 %       http://hci.tu-graz.ac.at/~schloegl/matlab/NaN/
@@ -112,9 +112,9 @@
 tmp(tmp<0) = 0;		% prevent tmp<0 i.e. imag(t)~=0 
 t   = R.*sqrt(max(NN-2,0)./tmp);
 
-if exist('t_cdf')>1;
+if exist('t_cdf','file')
         sig = t_cdf(t,NN-2);
-elseif exist('tcdf')>1;
+elseif exist('tcdf','file')
         sig = tcdf(t,NN-2);
 else
         fprintf('Warning CORRCOEF: significance test not completed because of missing TCDF-function\n')
--- a/extra/NaN/inst/sumskipnan.m	Wed Jun 10 08:55:27 2009 +0000
+++ b/extra/NaN/inst/sumskipnan.m	Wed Jun 10 12:27:10 2009 +0000
@@ -105,12 +105,14 @@
 	return; 
 end; 	
 
-if ~isempty(W) && (size(x,DIM)~=numel(W))
+if (length(size(x))<DIM)
+	error('SUMSKIPNAN: DIM argument larger than number of dimensions of x');
+elseif ~isempty(W) && (size(x,DIM)~=numel(W))
 	error('SUMSKIPNAN: size of weight vector does not match size(x,DIM)');
 end; 
 
 %% mex and oct files expect double
-x = double(x); 
+x = double(x);
 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 % use Matlab-MEX function when available  
@@ -124,7 +126,7 @@
 	if isempty(FLAG_NANS_OCCURED),
 		FLAG_NANS_OCCURED = logical(0);  % default value 
 	end;
-	
+
 	if (nargout<2),
 		o = sumskipnan_mex(real(x),DIM,FLAG_NANS_OCCURED,W);
 		if (~isreal(x))
@@ -158,7 +160,6 @@
 	end; 	
 end; 
 
-
 if ~isempty(W) 
 	error('weighted sumskipnan requires sumskipnan_mex');
 end;