# HG changeset patch
# User Markus Bergholz <markuman+octave@gmail.com>
# Date 1411278149 -7200
#      Sun Sep 21 07:42:29 2014 +0200
# Node ID c1a9a6a81c4b78de8b9f3797e36c326858232a86
# Parent  81df0dca633633d529ac12e9d00a6cfa3d98b767
reworking center, iqr, kurtosis, mean, meansq, median and mode when operating on trailing singleton dimension (#43038)

diff -r 81df0dca6336 -r c1a9a6a81c4b scripts/statistics/base/center.m
--- a/scripts/statistics/base/center.m	Sun Sep 21 01:26:17 2014 +0200
+++ b/scripts/statistics/base/center.m	Sun Sep 21 07:42:29 2014 +0200
@@ -1,3 +1,4 @@
+## Copyright (C) 2014 Markus Bergholz
 ## Copyright (C) 1995-2013 Kurt Hornik
 ## Copyright (C) 2009 VZLU Prague
 ##
@@ -49,10 +50,12 @@
     ## Find the first non-singleton dimension.
     (dim = find (sz > 1, 1)) || (dim = 1);
   else
-    if (!(isscalar (dim) && dim == fix (dim))
-        || !(1 <= dim && dim <= nd))
-      error ("center: DIM must be an integer and a valid dimension");
+
+    if (is_singleton_stats (x, dim) == 1)
+      retval = x;
+      return;
     endif
+  
   endif
 
   n = sz(dim);
@@ -74,6 +77,7 @@
 %!assert (center (ones (3,2,0,2, "single")), zeros (3,2,0,2, "single"))
 %!assert (center (magic (3)), [3,-4,1;-2,0,2;-1,4,-3])
 %!assert (center ([1 2 3; 6 5 4], 2), [-1 0 1; 1 0 -1])
+%!assert (center (1,3), 1)
 
 %% Test input validation
 %!error center ()
@@ -81,5 +85,4 @@
 %!error center (1, ones (2,2))
 %!error center (1, 1.5)
 %!error center (1, 0)
-%!error center (1, 3)
 
diff -r 81df0dca6336 -r c1a9a6a81c4b scripts/statistics/base/iqr.m
--- a/scripts/statistics/base/iqr.m	Sun Sep 21 01:26:17 2014 +0200
+++ b/scripts/statistics/base/iqr.m	Sun Sep 21 07:42:29 2014 +0200
@@ -1,3 +1,4 @@
+## Copyright (C) 2014 Markus Bergholz
 ## Copyright (C) 1995-2013 Kurt Hornik
 ##
 ## This file is part of Octave.
@@ -50,10 +51,12 @@
     ## Find the first non-singleton dimension.
     (dim = find (sz > 1, 1)) || (dim = 1);
   else
-    if (!(isscalar (dim) && dim == fix (dim))
-        || !(1 <= dim && dim <= nd))
-      error ("iqr: DIM must be an integer and a valid dimension");
-    endif
+
+    if (is_singleton_stats (x, dim) == 1)
+      y = x;
+      return;
+    endif  
+  
   endif
 
   ## This code is a bit heavy, but is needed until empirical_inv
@@ -89,10 +92,10 @@
 %%! x = [1:100];
 %%! n = iqr (x, 0:10);
 %%! assert (n, [repmat(100, 1, 10), 1]);
+%%! assert (iqr (1:10,3), 1:10)
 
 %!error iqr ()
 %!error iqr (1, 2, 3)
 %!error iqr (1)
 %!error iqr (['A'; 'B'])
-%!error iqr (1:10, 3)
 
diff -r 81df0dca6336 -r c1a9a6a81c4b scripts/statistics/base/kurtosis.m
--- a/scripts/statistics/base/kurtosis.m	Sun Sep 21 01:26:17 2014 +0200
+++ b/scripts/statistics/base/kurtosis.m	Sun Sep 21 07:42:29 2014 +0200
@@ -1,3 +1,4 @@
+## Copyright (C) 2014 Markus Bergholz
 ## Copyright (C) 2013 Julien Bect
 ## Copyright (C) 1996-2013 John W. Eaton
 ##
@@ -103,9 +104,12 @@
     ## Find the first non-singleton dimension.
     (dim = find (sz > 1, 1)) || (dim = 1);
   else
-    if (! (isscalar (dim) && dim == fix (dim)) || ! (1 <= dim && dim <= nd))
-      error ("kurtosis: DIM must be an integer and a valid dimension");
-    endif
+
+    if (is_singleton_stats (x, dim) == 1)
+      y = x;
+      return;
+    endif  
+  
   endif
 
   n = sz(dim);
@@ -146,6 +150,7 @@
 ## Test behaviour on single input
 %!assert (kurtosis (single ([1:5 10])), single (2.9786513), eps ("single"))
 %!assert (kurtosis (single ([1 2]), 0), single (NaN))
+%!assert (kurtosis (1,0,3), 1)
 
 ## Verify no "divide-by-zero" warnings
 %!test
@@ -168,4 +173,3 @@
 %!error <DIM must be an integer> kurtosis (1, [], ones (2,2))
 %!error <DIM must be an integer> kurtosis (1, [], 1.5)
 %!error <DIM must be .* a valid dimension> kurtosis (1, [], 0)
-%!error <DIM must be .* a valid dimension> kurtosis (1, [], 3)
diff -r 81df0dca6336 -r c1a9a6a81c4b scripts/statistics/base/mean.m
--- a/scripts/statistics/base/mean.m	Sun Sep 21 01:26:17 2014 +0200
+++ b/scripts/statistics/base/mean.m	Sun Sep 21 07:42:29 2014 +0200
@@ -1,3 +1,4 @@
+## Copyright (C) 2014 Markus Bergholz
 ## Copyright (C) 1995-2013 Kurt Hornik
 ##
 ## This file is part of Octave.
@@ -102,10 +103,12 @@
     ## Find the first non-singleton dimension.
     (dim = find (sz > 1, 1)) || (dim = 1);
   else
-    if (!(isscalar (dim) && dim == fix (dim))
-      || !(1 <= dim && dim <= nd))
-      error ("mean: DIM must be an integer and a valid dimension");
+  
+    if (is_singleton_stats (x, dim) == 1)
+      y = x;
+      return;
     endif
+    
   endif
 
   n = sz(dim);
@@ -144,6 +147,7 @@
 %!assert (mean ([4 4 2], "h"), 3)
 %!assert (mean (logical ([1 0 1 1])), 0.75)
 %!assert (mean (single ([1 0 1 1])), single (0.75))
+%!assert (mean (1,3), 1)
 
 %% Test input validation
 %!error mean ()
@@ -153,6 +157,5 @@
 %!error mean (1, ones (2,2))
 %!error mean (1, 1.5)
 %!error mean (1, 0)
-%!error mean (1, 3)
 %!error mean (1, "b")
 
diff -r 81df0dca6336 -r c1a9a6a81c4b scripts/statistics/base/meansq.m
--- a/scripts/statistics/base/meansq.m	Sun Sep 21 01:26:17 2014 +0200
+++ b/scripts/statistics/base/meansq.m	Sun Sep 21 07:42:29 2014 +0200
@@ -1,3 +1,4 @@
+## Copyright (C) 2014 Markus Bergholz
 ## Copyright (C) 1995-2013 Kurt Hornik
 ## Copyright (C) 2009 Jaroslav Hajek
 ##
@@ -62,10 +63,12 @@
     ## Find the first non-singleton dimension.
     (dim = find (sz > 1, 1)) || (dim = 1);
   else
-    if (!(isscalar (dim) && dim == fix (dim))
-        || !(1 <= dim && dim <= nd))
-      error ("mean: DIM must be an integer and a valid dimension");
-    endif
+
+    if (is_singleton_stats (x, dim) == 1)
+      y = x;
+      return;
+    endif  
+  
   endif
 
   y = sumsq (x, dim) / sz(dim);
@@ -77,6 +80,7 @@
 %!assert (meansq (single (1:5)), single (11))
 %!assert (meansq (magic (4)), [94.5, 92.5, 92.5, 94.5])
 %!assert (meansq (magic (4), 2), [109.5; 77.5; 77.5; 109.5])
+%!assert (meansq (1,3), 1)
 
 %% Test input validation
 %!error meansq ()
@@ -85,5 +89,4 @@
 %!error meansq (1, ones (2,2))
 %!error meansq (1, 1.5)
 %!error meansq (1, 0)
-%!error meansq (1, 3)
 
diff -r 81df0dca6336 -r c1a9a6a81c4b scripts/statistics/base/median.m
--- a/scripts/statistics/base/median.m	Sun Sep 21 01:26:17 2014 +0200
+++ b/scripts/statistics/base/median.m	Sun Sep 21 07:42:29 2014 +0200
@@ -1,3 +1,4 @@
+## Copyright (C) 2014 Markus Bergholz
 ## Copyright (C) 1996-2013 John W. Eaton
 ## Copyright (C) 2009-2010 VZLU Prague
 ##
@@ -69,10 +70,12 @@
     ## Find the first non-singleton dimension.
     (dim = find (sz > 1, 1)) || (dim = 1);
   else
-    if (!(isscalar (dim) && dim == fix (dim))
-        || !(1 <= dim && dim <= nd))
-      error ("median: DIM must be an integer and a valid dimension");
-    endif
+
+    if (is_singleton_stats (x, dim) == 1)
+      retval = x;
+      return;
+    endif  
+  
   endif
 
   n = sz(dim);
@@ -111,6 +114,7 @@
 %! y = sort (b, 3);
 %!assert (median (a, 4), x(:, :, :, 3));
 %!assert (median (b, 3), (y(:, :, 3, :) + y(:, :, 4, :))/2);
+%!assert (median (1,3), 1)
 
 %% Test input validation
 %!error median ()
diff -r 81df0dca6336 -r c1a9a6a81c4b scripts/statistics/base/mode.m
--- a/scripts/statistics/base/mode.m	Sun Sep 21 01:26:17 2014 +0200
+++ b/scripts/statistics/base/mode.m	Sun Sep 21 07:42:29 2014 +0200
@@ -1,3 +1,4 @@
+## Copyright (C) 2014 Markus Bergholz
 ## Copyright (C) 2007-2013 David Bateman
 ##
 ## This file is part of Octave.
@@ -49,10 +50,12 @@
     ## Find the first non-singleton dimension.
     (dim = find (sz > 1, 1)) || (dim = 1);
   else
-    if (!(isscalar (dim) && dim == fix (dim))
-        || !(1 <= dim && dim <= nd))
-      error ("mode: DIM must be an integer and a valid dimension");
+ 
+     if (is_singleton_stats (x, dim) == 1)
+      m = x;
+      return;
     endif
+ 
   endif
 
   sz2 = sz;
@@ -154,6 +157,7 @@
 %! assert (c{1}, [1; 2; 3]);
 %! assert (c{2}, [1; 2; 3]);
 %! assert (c{3}, [1; 2; 3]);
+%! assert (mode (1,3), 1);
 
 %% Test input validation
 %!error mode ()
@@ -163,5 +167,4 @@
 %!error mode (1, ones (2,2))
 %!error mode (1, 1.5)
 %!error mode (1, 0)
-%!error mode (1, 3)
 
diff -r 81df0dca6336 -r c1a9a6a81c4b scripts/statistics/base/private/is_singleton_stats.m
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scripts/statistics/base/private/is_singleton_stats.m	Sun Sep 21 07:42:29 2014 +0200
@@ -0,0 +1,11 @@
+# DIM must be an integer and a valid dimension + 
+# dim > ndims(x) check (#43038)
+
+function ret = is_singleton_stats(x, dim) 
+  if (!(isscalar (dim) && dim == fix (dim))
+    || !(1 <= dim))
+    error ("DIM must be an integer and a valid dimension");
+  else
+    ret = (dim > ndims (x) || size (x, dim) == 1);
+  end
+endfunction
\ No newline at end of file
