comparison scripts/statistics/base/moment.m @ 21309:7fbecef105ca

Allow statistics functions to work over non-existent dimension (bug #33523). * var.m, center.m, kurtosis.m, mean.m, meansq.m, median.m, moment.m, skewness.m, std.m: Remove check for input dimension being within the range of ndims (x). Add BIST tests for new behavior and update input validation tests.
author Rik <rik@octave.org>
date Fri, 19 Feb 2016 21:27:03 -0800
parents 516bb87ea72e
children bac0d6f07a3e
comparison
equal deleted inserted replaced
21308:c53bfd6d8e08 21309:7fbecef105ca
159 sz = size (x); 159 sz = size (x);
160 if (need_dim) 160 if (need_dim)
161 ## Find the first non-singleton dimension. 161 ## Find the first non-singleton dimension.
162 (dim = find (sz > 1, 1)) || (dim = 1); 162 (dim = find (sz > 1, 1)) || (dim = 1);
163 else 163 else
164 if (! (isscalar (dim) && dim == fix (dim)) || ! (1 <= dim && dim <= nd)) 164 if (! (isscalar (dim) && dim == fix (dim) && dim > 0))
165 error ("moment: DIM must be an integer and a valid dimension"); 165 error ("moment: DIM must be an integer and a valid dimension");
166 endif 166 endif
167 endif 167 endif
168 168
169 n = sz(dim); 169 n = size (x, dim);
170 170
171 if (! any (type == "r")) 171 if (! any (type == "r"))
172 x = center (x, dim); 172 x = center (x, dim);
173 endif 173 endif
174 if (any (type == "a")) 174 if (any (type == "a"))
188 %!assert (moment (x,1,"a"), mean (abs (center (x))), eps) 188 %!assert (moment (x,1,"a"), mean (abs (center (x))), eps)
189 %!assert (moment (x,1,"r"), mean (x), eps) 189 %!assert (moment (x,1,"r"), mean (x), eps)
190 %!assert (moment (x,1,"ar"), mean (abs (x)), eps) 190 %!assert (moment (x,1,"ar"), mean (abs (x)), eps)
191 191
192 %!assert (moment (single ([1 2 3]), 1, "r"), single (2)) 192 %!assert (moment (single ([1 2 3]), 1, "r"), single (2))
193
194 %!assert (moment (1, 2, 4), 0)
193 195
194 ## Test input validation 196 ## Test input validation
195 %!error moment () 197 %!error moment ()
196 %!error moment (1) 198 %!error moment (1)
197 %!error moment (1, 2, 3, 4, 5) 199 %!error moment (1, 2, 3, 4, 5)
200 %!error <P must be a numeric scalar> moment (1, true) 202 %!error <P must be a numeric scalar> moment (1, true)
201 %!error <P must be a numeric scalar> moment (1, ones (2,2)) 203 %!error <P must be a numeric scalar> moment (1, ones (2,2))
202 %!error <TYPE must be a string> moment (1, 2, 3, 4) 204 %!error <TYPE must be a string> moment (1, 2, 3, 4)
203 %!error <DIM must be an integer and a valid dimension> moment (1, 2, ones (2,2)) 205 %!error <DIM must be an integer and a valid dimension> moment (1, 2, ones (2,2))
204 %!error <DIM must be an integer and a valid dimension> moment (1, 2, 1.5) 206 %!error <DIM must be an integer and a valid dimension> moment (1, 2, 1.5)
205 %!error <DIM must be an integer and a valid dimension> moment (1, 2, 4) 207
206