Mercurial > octave
annotate scripts/statistics/mean.m @ 26596:cc0d942d0e20
Remove inconsistent warning "Octave:divide-by-zero" (bug #46650).
* libinterp/corefcn/errwarn.h (warn_divide_by_zero),
libinterp/corefcn/errwarn.cc (warn_divide_by_zero): Deprecate function
"warn_divide_by_zero", and leave implementation empty for a few releases.
* libinterp/corefcn/error.cc: Use different id for test.
* examples/code/make_int.cc: No longer advertise "warn_divide_by_zero" in the
examples.
* libinterp/operators/op-*-*.cc: Remove unused header "errwarn.h". Remove check
for scalar zero divisor including the warning.
* libinterp/operators/op-dms-template.cc (gripe_if_zero): Remove function.
* scripts/general/quadgk.m, scripts/general/quadv.m,
scripts/statistics/kurtosis.m, scripts/statistics/mean.m,
scripts/statistics/skewness.m: No longer need to turn off unused warning.
* scripts/help/warning_ids.m: Remove documentation of "Octave:divide-by-zero".
* test/jit.tst, test/mk-sparse-tst.sh, test/prefer.tst: Remove warning
"Octave:divide-by-zero".
author | Kai T. Ohlhus <k.ohlhus@gmail.com> |
---|---|
date | Tue, 22 Jan 2019 14:34:08 +0100 |
parents | 00f796120a6d |
children | b442ec6dda5c |
rev | line source |
---|---|
26376
00f796120a6d
maint: Update copyright dates in all source files.
John W. Eaton <jwe@octave.org>
parents:
26287
diff
changeset
|
1 ## Copyright (C) 1995-2019 Kurt Hornik |
3426 | 2 ## |
3922 | 3 ## This file is part of Octave. |
4 ## | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
24512
diff
changeset
|
5 ## Octave is free software: you can redistribute it and/or modify it |
3922 | 6 ## under the terms of the GNU General Public License as published by |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
24512
diff
changeset
|
7 ## the Free Software Foundation, either version 3 of the License, or |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
8 ## (at your option) any later version. |
3426 | 9 ## |
3922 | 10 ## Octave is distributed in the hope that it will be useful, but |
3200 | 11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
13 ## GNU General Public License for more details. |
3426 | 14 ## |
3200 | 15 ## You should have received a copy of the GNU General Public License |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
24512
diff
changeset
|
17 ## <https://www.gnu.org/licenses/>. |
3200 | 18 |
3367 | 19 ## -*- texinfo -*- |
20852
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20174
diff
changeset
|
20 ## @deftypefn {} {} mean (@var{x}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20174
diff
changeset
|
21 ## @deftypefnx {} {} mean (@var{x}, @var{dim}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20174
diff
changeset
|
22 ## @deftypefnx {} {} mean (@var{x}, @var{opt}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20174
diff
changeset
|
23 ## @deftypefnx {} {} mean (@var{x}, @var{dim}, @var{opt}) |
24869
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
24 ## @deftypefnx {} {} mean (@dots{}, @var{outtype}) |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
25 ## Compute the mean of the elements of the vector @var{x}. |
20174
d9341b422488
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
26 ## |
d9341b422488
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
27 ## The mean is defined as |
d9341b422488
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
28 ## |
3367 | 29 ## @tex |
30 ## $$ {\rm mean}(x) = \bar{x} = {1\over N} \sum_{i=1}^N x_i $$ | |
24511
4f0e6ee6c9b8
Make documentation Sec 26.1 more consistent and Sec 25.4 clearer (bug #52685)
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
23219
diff
changeset
|
31 ## where $N$ is the number of elements of @var{x}. |
4f0e6ee6c9b8
Make documentation Sec 26.1 more consistent and Sec 25.4 clearer (bug #52685)
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
23219
diff
changeset
|
32 ## |
3367 | 33 ## @end tex |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7411
diff
changeset
|
34 ## @ifnottex |
3426 | 35 ## |
3367 | 36 ## @example |
24511
4f0e6ee6c9b8
Make documentation Sec 26.1 more consistent and Sec 25.4 clearer (bug #52685)
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
23219
diff
changeset
|
37 ## mean (@var{x}) = SUM_i @var{x}(i) / N |
3367 | 38 ## @end example |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
39 ## |
24985
d85b2485af9e
doc: grammarcheck m-files ahead of 4.4 release.
Rik <rik@octave.org>
parents:
24869
diff
changeset
|
40 ## @noindent |
24511
4f0e6ee6c9b8
Make documentation Sec 26.1 more consistent and Sec 25.4 clearer (bug #52685)
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
23219
diff
changeset
|
41 ## where @math{N} is the length of the @var{x} vector. |
4f0e6ee6c9b8
Make documentation Sec 26.1 more consistent and Sec 25.4 clearer (bug #52685)
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
23219
diff
changeset
|
42 ## |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7411
diff
changeset
|
43 ## @end ifnottex |
3367 | 44 ## If @var{x} is a matrix, compute the mean for each column and return them |
45 ## in a row vector. | |
3200 | 46 ## |
20174
d9341b422488
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
47 ## If the optional argument @var{dim} is given, operate along this dimension. |
d9341b422488
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
48 ## |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
49 ## The optional argument @var{opt} selects the type of mean to compute. |
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
50 ## The following options are recognized: |
3367 | 51 ## |
11595
5ec6aa05638d
Prevent doubled quotes around @table items in Info.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
52 ## @table @asis |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
15467
diff
changeset
|
53 ## @item @qcode{"a"} |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
54 ## Compute the (ordinary) arithmetic mean. [default] |
3367 | 55 ## |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
15467
diff
changeset
|
56 ## @item @qcode{"g"} |
7001 | 57 ## Compute the geometric mean. |
3367 | 58 ## |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
15467
diff
changeset
|
59 ## @item @qcode{"h"} |
3367 | 60 ## Compute the harmonic mean. |
61 ## @end table | |
4022 | 62 ## |
24869
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
63 ## The optional argument @var{outtype} selects the data type of the |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
64 ## output value. The following options are recognized: |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
65 ## |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
66 ## @table @asis |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
67 ## @item @qcode{"default"} |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
68 ## Output will be of class double unless @var{x} is of class single, |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
69 ## in which case the output will also be single. |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
70 ## |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
71 ## @item @qcode{"double"} |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
72 ## Output will be of class double. |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
73 ## |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
74 ## @item @qcode{"native"} |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
75 ## Output will be the same class as @var{x} unless @var{x} is of class |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
76 ## logical in which case it returns of class double. |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
77 ## |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
78 ## @end table |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
79 ## |
20174
d9341b422488
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
80 ## Both @var{dim} and @var{opt} are optional. If both are supplied, either |
d9341b422488
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
81 ## may appear first. |
12575
d0b799dafede
Grammarcheck files for 3.4.1 release.
Rik <octave@nomad.inbox5.com>
parents:
12454
diff
changeset
|
82 ## @seealso{median, mode} |
3367 | 83 ## @end deftypefn |
3426 | 84 |
5428 | 85 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
3456 | 86 ## Description: Compute arithmetic, geometric, and harmonic mean |
3200 | 87 |
24869
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
88 function y = mean (x, varargin) |
4022 | 89 |
24869
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
90 if (nargin < 1 || nargin > 4) |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
91 print_usage (); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
92 endif |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
93 |
12435
0f21f258aa17
mean: also accept logical values
John W. Eaton <jwe@octave.org>
parents:
11595
diff
changeset
|
94 if (! (isnumeric (x) || islogical (x))) |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
95 error ("mean: X must be a numeric vector or matrix"); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
96 endif |
24869
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
97 nd = ndims (x); |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
98 sz = size (x); |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
99 |
24869
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
100 ## We support too many options... |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
101 |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
102 ## If OUTTYPE is set, it must be the last option. If DIM and |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
103 ## MEAN_TYPE exist, they must be the first two options |
3200 | 104 |
24869
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
105 out_type = "default"; |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
106 if (numel (varargin)) |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
107 maybe_out_type = tolower (varargin{end}); |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
108 if (any (strcmpi (maybe_out_type, {"default", "double", "native"}))) |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
109 out_type = maybe_out_type; |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
110 varargin(end) = []; |
4022 | 111 endif |
24869
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
112 endif |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
113 |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
114 scalars = cellfun (@isscalar, varargin); |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
115 chars = cellfun (@ischar, varargin); |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
116 numerics = cellfun (@isnumeric, varargin); |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
117 |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
118 dim_mask = numerics & scalars; |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
119 mean_type_mask = chars & scalars; |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
120 if (! all (dim_mask | mean_type_mask)) |
6046 | 121 print_usage (); |
3200 | 122 endif |
3426 | 123 |
24869
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
124 switch (nnz (dim_mask)) |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
125 case 0 # Find the first non-singleton dimension |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
126 (dim = find (sz > 1, 1)) || (dim = 1); |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
127 case 1 |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
128 dim = varargin{dim_mask}; |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
129 if (dim != fix (dim) || dim < 1) |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
130 error ("mean: DIM must be an integer and a valid dimension"); |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
131 endif |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
132 otherwise |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
133 print_usage (); |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
134 endswitch |
3200 | 135 |
24869
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
136 switch (nnz (mean_type_mask)) |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
137 case 0 |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
138 mean_type = "a"; |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
139 case 1 |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
140 mean_type = varargin{mean_type_mask}; |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
141 otherwise |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
142 print_usage (); |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
143 endswitch |
3426 | 144 |
24869
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
145 ## The actual mean computation |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
146 n = size (x, dim); |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
147 switch (mean_type) |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
148 case "a" |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
149 y = sum (x, dim) / n; |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
150 case "g" |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
151 if (all (x(:) >= 0)) |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
152 y = exp (sum (log (x), dim) ./ n); |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
153 else |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
154 error ("mean: X must not contain any negative values"); |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
155 endif |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
156 case "h" |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
157 y = n ./ sum (1 ./ x, dim); |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
158 otherwise |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
159 error ("mean: mean type '%s' not recognized", mean_type); |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
160 endswitch |
3426 | 161 |
24869
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
162 ## Convert output as requested |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
163 switch (out_type) |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
164 case "default" |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
165 ## do nothing, the operators already do the right thing |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
166 case "double" |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
167 y = double (y); |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
168 case "native" |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
169 if (islogical (x)) |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
170 ## ignore it, return double anyway |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
171 else |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
172 y = cast (y, class (x)); |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
173 endif |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
174 otherwise |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
175 ## this should have been filtered out during input check, but... |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
176 error ("mean: OUTTYPE '%s' not recognized", out_type); |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
177 endswitch |
3200 | 178 endfunction |
7411 | 179 |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
180 |
7411 | 181 %!test |
182 %! x = -10:10; | |
183 %! y = x'; | |
184 %! z = [y, y+10]; | |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
185 %! assert (mean (x), 0); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
186 %! assert (mean (y), 0); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
187 %! assert (mean (z), [0, 10]); |
7411 | 188 |
15464
930117c97760
mean.m: improve geometric mean algorithm for small numbers. Add test.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14868
diff
changeset
|
189 ## Test small numbers |
930117c97760
mean.m: improve geometric mean algorithm for small numbers. Add test.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14868
diff
changeset
|
190 %!assert (mean (repmat (0.1,1,1000), "g"), 0.1, 20*eps) |
7411 | 191 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
192 %!assert (mean (magic (3), 1), [5, 5, 5]) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
193 %!assert (mean (magic (3), 2), [5; 5; 5]) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
194 %!assert (mean ([2 8], "g"), 4) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
195 %!assert (mean ([4 4 2], "h"), 3) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
196 %!assert (mean (logical ([1 0 1 1])), 0.75) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
197 %!assert (mean (single ([1 0 1 1])), single (0.75)) |
21309
7fbecef105ca
Allow statistics functions to work over non-existent dimension (bug #33523).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
198 %!assert (mean ([1 2], 3), [1 2]) |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
199 |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
200 ## Test input validation |
24869
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
201 %!error <Invalid call to mean. Correct usage is> mean () |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
202 %!error <Invalid call to mean. Correct usage is> mean (1, 2, 3, 4) |
21309
7fbecef105ca
Allow statistics functions to work over non-existent dimension (bug #33523).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
203 %!error <X must be a numeric> mean ({1:5}) |
24869
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
204 %!error <Invalid call to mean. Correct usage is> mean (1, 2, 3) |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
205 %!error <Invalid call to mean. Correct usage is> mean (1, ones (2,2)) |
21309
7fbecef105ca
Allow statistics functions to work over non-existent dimension (bug #33523).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
206 %!error <DIM must be an integer> mean (1, 1.5) |
7fbecef105ca
Allow statistics functions to work over non-existent dimension (bug #33523).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
207 %!error <DIM must be .* a valid dimension> mean (1, 0) |
7fbecef105ca
Allow statistics functions to work over non-existent dimension (bug #33523).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
208 %!error <X must not contain any negative values> mean ([1 -1], "g") |
24869
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
209 %!error <mean type 'b' not recognized> mean (1, "b") |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
210 %!error <Invalid call to mean. Correct usage is> mean (1, 1, "foo") |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
211 |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
212 ## Test outtype option |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
213 %!test |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
214 %! in = [1 2 3]; |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
215 %! out = 2; |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
216 %! assert (mean (in, "default"), mean (in)) |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
217 %! assert (mean (in, "default"), out) |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
218 %! |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
219 %! in = single ([1 2 3]); |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
220 %! out = 2; |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
221 %! assert (mean (in, "default"), mean (in)) |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
222 %! assert (mean (in, "default"), single (out)) |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
223 %! assert (mean (in, "double"), out) |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
224 %! assert (mean (in, "native"), single (out)) |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
225 %! |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
226 %! in = uint8 ([1 2 3]); |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
227 %! out = 2; |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
228 %! assert (mean (in, "default"), mean (in)) |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
229 %! assert (mean (in, "default"), out) |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
230 %! assert (mean (in, "double"), out) |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
231 %! assert (mean (in, "native"), uint8 (out)) |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
232 %! |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
233 %! in = logical ([1 0 1]); |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
234 %! out = 2/3; |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
235 %! assert (mean (in, "default"), mean (in)) |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
236 %! assert (mean (in, "default"), out) |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
237 %! assert (mean (in, "native"), out) # logical ignores native option |