Mercurial > octave
annotate scripts/statistics/mean.m @ 27918:b442ec6dda5c
use centralized file for copyright info for individual contributors
* COPYRIGHT.md: New file.
* In most other files, use "Copyright (C) YYYY-YYYY The Octave Project
Developers" instead of tracking individual names in separate source
files. The motivation is to reduce the effort required to update the
notices each year.
Until now, the Octave source files contained copyright notices that
list individual contributors. I adopted these file-scope copyright
notices because that is what everyone was doing 30 years ago in the
days before distributed version control systems. But now, with many
contributors and modern version control systems, having these
file-scope copyright notices causes trouble when we update copyright
years or refactor code.
Over time, the file-scope copyright notices may become outdated as new
contributions are made or code is moved from one file to
another. Sometimes people contribute significant patches but do not
add a line claiming copyright. Other times, people add a copyright
notice for their contribution but then a later refactoring moves part
or all of their contribution to another file and the notice is not
moved with the code. As a practical matter, moving such notices is
difficult -- determining what parts are due to a particular
contributor requires a time-consuming search through the project
history. Even managing the yearly update of copyright years is
problematic. We have some contributors who are no longer
living. Should we update the copyright dates for their contributions
when we release new versions? Probably not, but we do still want to
claim copyright for the project as a whole.
To minimize the difficulty of maintaining the copyright notices, I
would like to change Octave's sources to use what is described here:
https://softwarefreedom.org/resources/2012/ManagingCopyrightInformation.html
in the section "Maintaining centralized copyright notices":
The centralized notice approach consolidates all copyright
notices in a single location, usually a top-level file.
This file should contain all of the copyright notices
provided project contributors, unless the contribution was
clearly insignificant. It may also credit -- without a copyright
notice -- anyone who helped with the project but did not
contribute code or other copyrighted material.
This approach captures less information about contributions
within individual files, recognizing that the DVCS is better
equipped to record those details. As we mentioned before, it
does have one disadvantage as compared to the file-scope
approach: if a single file is separated from the distribution,
the recipient won't see the contributors' copyright notices.
But this can be easily remedied by including a single
copyright notice in each file's header, pointing to the
top-level file:
Copyright YYYY-YYYY The Octave Project Developers
See the COPYRIGHT file at the top-level directory
of this distribution or at https://octave.org/COPYRIGHT.html.
followed by the usual GPL copyright statement.
For more background, see the discussion here:
https://lists.gnu.org/archive/html/octave-maintainers/2020-01/msg00009.html
Most files in the following directories have been skipped intentinally
in this changeset:
doc
libgui/qterminal
liboctave/external
m4
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Mon, 06 Jan 2020 15:38:17 -0500 |
parents | cc0d942d0e20 |
children | 1891570abac8 |
rev | line source |
---|---|
27918
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
26596
diff
changeset
|
1 ## Copyright (C) 1995-2019 The Octave Project Developers |
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
26596
diff
changeset
|
2 ## |
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
26596
diff
changeset
|
3 ## See the file COPYRIGHT.md in the top-level directory of this distribution |
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
26596
diff
changeset
|
4 ## or <https://octave.org/COPYRIGHT.html/>. |
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
26596
diff
changeset
|
5 ## |
3426 | 6 ## |
3922 | 7 ## This file is part of Octave. |
8 ## | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
24512
diff
changeset
|
9 ## Octave is free software: you can redistribute it and/or modify it |
3922 | 10 ## 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
|
11 ## 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
|
12 ## (at your option) any later version. |
3426 | 13 ## |
3922 | 14 ## Octave is distributed in the hope that it will be useful, but |
3200 | 15 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
16 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
17 ## GNU General Public License for more details. |
3426 | 18 ## |
3200 | 19 ## You should have received a copy of the GNU General Public License |
7016 | 20 ## 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
|
21 ## <https://www.gnu.org/licenses/>. |
3200 | 22 |
3367 | 23 ## -*- texinfo -*- |
20852
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20174
diff
changeset
|
24 ## @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
|
25 ## @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
|
26 ## @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
|
27 ## @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
|
28 ## @deftypefnx {} {} mean (@dots{}, @var{outtype}) |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
29 ## 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
|
30 ## |
d9341b422488
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
31 ## 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
|
32 ## |
3367 | 33 ## @tex |
34 ## $$ {\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
|
35 ## 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
|
36 ## |
3367 | 37 ## @end tex |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7411
diff
changeset
|
38 ## @ifnottex |
3426 | 39 ## |
3367 | 40 ## @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
|
41 ## mean (@var{x}) = SUM_i @var{x}(i) / N |
3367 | 42 ## @end example |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
43 ## |
24985
d85b2485af9e
doc: grammarcheck m-files ahead of 4.4 release.
Rik <rik@octave.org>
parents:
24869
diff
changeset
|
44 ## @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
|
45 ## 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
|
46 ## |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7411
diff
changeset
|
47 ## @end ifnottex |
3367 | 48 ## If @var{x} is a matrix, compute the mean for each column and return them |
49 ## in a row vector. | |
3200 | 50 ## |
20174
d9341b422488
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
51 ## 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
|
52 ## |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
53 ## 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
|
54 ## The following options are recognized: |
3367 | 55 ## |
11595
5ec6aa05638d
Prevent doubled quotes around @table items in Info.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
56 ## @table @asis |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
15467
diff
changeset
|
57 ## @item @qcode{"a"} |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
58 ## Compute the (ordinary) arithmetic mean. [default] |
3367 | 59 ## |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
15467
diff
changeset
|
60 ## @item @qcode{"g"} |
7001 | 61 ## Compute the geometric mean. |
3367 | 62 ## |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
15467
diff
changeset
|
63 ## @item @qcode{"h"} |
3367 | 64 ## Compute the harmonic mean. |
65 ## @end table | |
4022 | 66 ## |
24869
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
67 ## 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
|
68 ## 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
|
69 ## |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
70 ## @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
|
71 ## @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
|
72 ## 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
|
73 ## 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
|
74 ## |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
75 ## @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
|
76 ## 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
|
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 ## @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
|
79 ## 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
|
80 ## 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
|
81 ## |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
82 ## @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
|
83 ## |
20174
d9341b422488
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
84 ## 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
|
85 ## may appear first. |
12575
d0b799dafede
Grammarcheck files for 3.4.1 release.
Rik <octave@nomad.inbox5.com>
parents:
12454
diff
changeset
|
86 ## @seealso{median, mode} |
3367 | 87 ## @end deftypefn |
3426 | 88 |
5428 | 89 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
3456 | 90 ## Description: Compute arithmetic, geometric, and harmonic mean |
3200 | 91 |
24869
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
92 function y = mean (x, varargin) |
4022 | 93 |
24869
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
94 if (nargin < 1 || nargin > 4) |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
95 print_usage (); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
96 endif |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
97 |
12435
0f21f258aa17
mean: also accept logical values
John W. Eaton <jwe@octave.org>
parents:
11595
diff
changeset
|
98 if (! (isnumeric (x) || islogical (x))) |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
99 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
|
100 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
|
101 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
|
102 sz = size (x); |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
103 |
24869
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
104 ## 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
|
105 |
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 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
|
107 ## MEAN_TYPE exist, they must be the first two options |
3200 | 108 |
24869
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 = "default"; |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
110 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
|
111 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
|
112 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
|
113 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
|
114 varargin(end) = []; |
4022 | 115 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
|
116 endif |
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 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
|
119 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
|
120 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
|
121 |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
122 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
|
123 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
|
124 if (! all (dim_mask | mean_type_mask)) |
6046 | 125 print_usage (); |
3200 | 126 endif |
3426 | 127 |
24869
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
128 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
|
129 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
|
130 (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
|
131 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
|
132 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
|
133 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
|
134 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
|
135 endif |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
136 otherwise |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
137 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
|
138 endswitch |
3200 | 139 |
24869
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
140 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
|
141 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
|
142 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
|
143 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
|
144 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
|
145 otherwise |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
146 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
|
147 endswitch |
3426 | 148 |
24869
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
149 ## 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
|
150 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
|
151 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
|
152 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
|
153 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
|
154 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
|
155 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
|
156 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
|
157 else |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
158 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
|
159 endif |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
160 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
|
161 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
|
162 otherwise |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
163 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
|
164 endswitch |
3426 | 165 |
24869
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
166 ## 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
|
167 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
|
168 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
|
169 ## 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
|
170 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
|
171 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
|
172 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
|
173 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
|
174 ## 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
|
175 else |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
176 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
|
177 endif |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
178 otherwise |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
179 ## 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
|
180 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
|
181 endswitch |
3200 | 182 endfunction |
7411 | 183 |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
184 |
7411 | 185 %!test |
186 %! x = -10:10; | |
187 %! y = x'; | |
188 %! 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
|
189 %! assert (mean (x), 0); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
190 %! assert (mean (y), 0); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
191 %! assert (mean (z), [0, 10]); |
7411 | 192 |
15464
930117c97760
mean.m: improve geometric mean algorithm for small numbers. Add test.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14868
diff
changeset
|
193 ## 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
|
194 %!assert (mean (repmat (0.1,1,1000), "g"), 0.1, 20*eps) |
7411 | 195 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
196 %!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
|
197 %!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
|
198 %!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
|
199 %!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
|
200 %!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
|
201 %!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
|
202 %!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
|
203 |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
204 ## 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
|
205 %!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
|
206 %!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
|
207 %!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
|
208 %!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
|
209 %!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
|
210 %!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
|
211 %!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
|
212 %!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
|
213 %!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
|
214 %!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
|
215 |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
216 ## 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
|
217 %!test |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
218 %! 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
|
219 %! 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
|
220 %! 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
|
221 %! 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
|
222 %! |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
223 %! 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
|
224 %! 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
|
225 %! 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
|
226 %! 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
|
227 %! 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
|
228 %! 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
|
229 %! |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
230 %! 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
|
231 %! 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
|
232 %! 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
|
233 %! 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
|
234 %! 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
|
235 %! 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
|
236 %! |
170e8625562a
mean.m: add out_type option to control data type of return value
Carnë Draug <carandraug@octave.org>
parents:
24547
diff
changeset
|
237 %! 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
|
238 %! 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
|
239 %! 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
|
240 %! 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
|
241 %! assert (mean (in, "native"), out) # logical ignores native option |