annotate scripts/general/trapz.m @ 31200:075443476dfb stable

nextpow2.m: Add bug number tag to regression BIST tests (bug #62947) * nextpow2.m: Add '<*62947>' to BIST tests for bug #62947.
author Rik <rik@octave.org>
date Wed, 24 Aug 2022 08:55:14 -0700
parents 796f54d4ddbf
children 597f3ee61a48
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
1 ########################################################################
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
2 ##
30564
796f54d4ddbf update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
3 ## Copyright (C) 2000-2022 The Octave Project Developers
27918
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 26601
diff changeset
4 ##
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
5 ## See the file COPYRIGHT.md in the top-level directory of this
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
6 ## distribution or <https://octave.org/copyright/>.
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
7 ##
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
8 ## This file is part of Octave.
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
9 ##
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23827
diff changeset
10 ## Octave is free software: you can redistribute it and/or modify it
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
11 ## 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: 23827
diff changeset
12 ## 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
13 ## (at your option) any later version.
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
14 ##
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
15 ## Octave is distributed in the hope that it will be useful, but
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
18 ## GNU General Public License for more details.
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
19 ##
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
20 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
21 ## 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: 23827
diff changeset
22 ## <https://www.gnu.org/licenses/>.
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
23 ##
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
24 ########################################################################
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
25
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
26 ## -*- texinfo -*-
20852
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20617
diff changeset
27 ## @deftypefn {} {@var{q} =} trapz (@var{y})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20617
diff changeset
28 ## @deftypefnx {} {@var{q} =} trapz (@var{x}, @var{y})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20617
diff changeset
29 ## @deftypefnx {} {@var{q} =} trapz (@dots{}, @var{dim})
12612
16cca721117b doc: Update all documentation for chapter on Numerical Integration
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
30 ##
16cca721117b doc: Update all documentation for chapter on Numerical Integration
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
31 ## Numerically evaluate the integral of points @var{y} using the trapezoidal
16cca721117b doc: Update all documentation for chapter on Numerical Integration
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
32 ## method.
20158
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
33 ##
12612
16cca721117b doc: Update all documentation for chapter on Numerical Integration
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
34 ## @w{@code{trapz (@var{y})}} computes the integral of @var{y} along the first
20158
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
35 ## non-singleton dimension. When the argument @var{x} is omitted an equally
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
36 ## spaced @var{x} vector with unit spacing (1) is assumed.
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
37 ## @code{trapz (@var{x}, @var{y})} evaluates the integral with respect to the
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
38 ## spacing in @var{x} and the values in @var{y}. This is useful if the points
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
39 ## in @var{y} have been sampled unevenly.
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
40 ##
12612
16cca721117b doc: Update all documentation for chapter on Numerical Integration
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
41 ## If the optional @var{dim} argument is given, operate along this dimension.
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
42 ##
20158
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
43 ## Application Note: If @var{x} is not specified then unit spacing will be
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
44 ## used. To scale the integral to the correct value you must multiply by the
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
45 ## actual spacing value (deltaX). As an example, the integral of @math{x^3}
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
46 ## over the range [0, 1] is @math{x^4/4} or 0.25. The following code uses
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
47 ## @code{trapz} to calculate the integral in three different ways.
12612
16cca721117b doc: Update all documentation for chapter on Numerical Integration
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
48 ##
16cca721117b doc: Update all documentation for chapter on Numerical Integration
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
49 ## @example
16cca721117b doc: Update all documentation for chapter on Numerical Integration
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
50 ## @group
16cca721117b doc: Update all documentation for chapter on Numerical Integration
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
51 ## x = 0:0.1:1;
16cca721117b doc: Update all documentation for chapter on Numerical Integration
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
52 ## y = x.^3;
26601
de358b26dc88 trapz.m: Restore documentation comments lost in f6730533820e.
Rik <rik@octave.org>
parents: 26600
diff changeset
53 ## ## No scaling
12612
16cca721117b doc: Update all documentation for chapter on Numerical Integration
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
54 ## q = trapz (y)
26600
f6730533820e doc: clean up doc example blocks for accuracy and consistent formatting
Mike Miller <mtmiller@octave.org>
parents: 26376
diff changeset
55 ## @result{} q = 2.5250
26601
de358b26dc88 trapz.m: Restore documentation comments lost in f6730533820e.
Rik <rik@octave.org>
parents: 26600
diff changeset
56 ## ## Approximation to integral by scaling
12612
16cca721117b doc: Update all documentation for chapter on Numerical Integration
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
57 ## q * 0.1
26600
f6730533820e doc: clean up doc example blocks for accuracy and consistent formatting
Mike Miller <mtmiller@octave.org>
parents: 26376
diff changeset
58 ## @result{} 0.25250
26601
de358b26dc88 trapz.m: Restore documentation comments lost in f6730533820e.
Rik <rik@octave.org>
parents: 26600
diff changeset
59 ## ## Same result by specifying @var{x}
12642
f96b9b9f141b doc: Periodic grammarcheck and spellcheck of documentation.
Rik <octave@nomad.inbox5.com>
parents: 12612
diff changeset
60 ## trapz (x, y)
26600
f6730533820e doc: clean up doc example blocks for accuracy and consistent formatting
Mike Miller <mtmiller@octave.org>
parents: 26376
diff changeset
61 ## @result{} 0.25250
12612
16cca721117b doc: Update all documentation for chapter on Numerical Integration
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
62 ## @end group
16cca721117b doc: Update all documentation for chapter on Numerical Integration
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
63 ## @end example
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
64 ##
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
65 ## @seealso{cumtrapz}
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
66 ## @end deftypefn
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
67
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
68 function z = trapz (x, y, dim)
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
69
28789
28de41192f3c Eliminate unneeded verification of nargin, nargout in m-files.
Rik <rik@octave.org>
parents: 27984
diff changeset
70 if (nargin < 1)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5820
diff changeset
71 print_usage ();
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
72 endif
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
73
12667
d8aff843a9e9 Accept input x vector with y matrix for trapz,cumtrapz (bug #33292).
Rik <octave@nomad.inbox5.com>
parents: 12642
diff changeset
74 have_xy = have_dim = false;
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
75
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
76 if (nargin == 3)
12667
d8aff843a9e9 Accept input x vector with y matrix for trapz,cumtrapz (bug #33292).
Rik <octave@nomad.inbox5.com>
parents: 12642
diff changeset
77 have_xy = true;
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
78 have_dim = true;
12667
d8aff843a9e9 Accept input x vector with y matrix for trapz,cumtrapz (bug #33292).
Rik <octave@nomad.inbox5.com>
parents: 12642
diff changeset
79 elseif (nargin == 2)
25575
b09ff039e6ff trapz.m: Overhaul function to fix errors with different orientations of X and Y (bug #54277).
Rik <rik@octave.org>
parents: 25054
diff changeset
80 if (isscalar (y) && ! isscalar (x))
b09ff039e6ff trapz.m: Overhaul function to fix errors with different orientations of X and Y (bug #54277).
Rik <rik@octave.org>
parents: 25054
diff changeset
81 have_dim = true;
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
82 dim = y;
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
83 else
12667
d8aff843a9e9 Accept input x vector with y matrix for trapz,cumtrapz (bug #33292).
Rik <octave@nomad.inbox5.com>
parents: 12642
diff changeset
84 have_xy = true;
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
85 endif
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
86 endif
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
87
12667
d8aff843a9e9 Accept input x vector with y matrix for trapz,cumtrapz (bug #33292).
Rik <octave@nomad.inbox5.com>
parents: 12642
diff changeset
88 if (have_xy)
d8aff843a9e9 Accept input x vector with y matrix for trapz,cumtrapz (bug #33292).
Rik <octave@nomad.inbox5.com>
parents: 12642
diff changeset
89 nd = ndims (y);
d8aff843a9e9 Accept input x vector with y matrix for trapz,cumtrapz (bug #33292).
Rik <octave@nomad.inbox5.com>
parents: 12642
diff changeset
90 sz = size (y);
d8aff843a9e9 Accept input x vector with y matrix for trapz,cumtrapz (bug #33292).
Rik <octave@nomad.inbox5.com>
parents: 12642
diff changeset
91 else
d8aff843a9e9 Accept input x vector with y matrix for trapz,cumtrapz (bug #33292).
Rik <octave@nomad.inbox5.com>
parents: 12642
diff changeset
92 nd = ndims (x);
d8aff843a9e9 Accept input x vector with y matrix for trapz,cumtrapz (bug #33292).
Rik <octave@nomad.inbox5.com>
parents: 12642
diff changeset
93 sz = size (x);
d8aff843a9e9 Accept input x vector with y matrix for trapz,cumtrapz (bug #33292).
Rik <octave@nomad.inbox5.com>
parents: 12642
diff changeset
94 endif
d8aff843a9e9 Accept input x vector with y matrix for trapz,cumtrapz (bug #33292).
Rik <octave@nomad.inbox5.com>
parents: 12642
diff changeset
95
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
96 if (! have_dim)
10690
35adf2a71f3f Use common code block to find first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents: 10549
diff changeset
97 ## Find the first non-singleton dimension.
12667
d8aff843a9e9 Accept input x vector with y matrix for trapz,cumtrapz (bug #33292).
Rik <octave@nomad.inbox5.com>
parents: 12642
diff changeset
98 (dim = find (sz > 1, 1)) || (dim = 1);
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
99 else
12667
d8aff843a9e9 Accept input x vector with y matrix for trapz,cumtrapz (bug #33292).
Rik <octave@nomad.inbox5.com>
parents: 12642
diff changeset
100 if (!(isscalar (dim) && dim == fix (dim))
d8aff843a9e9 Accept input x vector with y matrix for trapz,cumtrapz (bug #33292).
Rik <octave@nomad.inbox5.com>
parents: 12642
diff changeset
101 || !(1 <= dim && dim <= nd))
d8aff843a9e9 Accept input x vector with y matrix for trapz,cumtrapz (bug #33292).
Rik <octave@nomad.inbox5.com>
parents: 12642
diff changeset
102 error ("trapz: DIM must be an integer and a valid dimension");
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
103 endif
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
104 endif
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
105
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
106 n = sz(dim);
25575
b09ff039e6ff trapz.m: Overhaul function to fix errors with different orientations of X and Y (bug #54277).
Rik <rik@octave.org>
parents: 25054
diff changeset
107 idx1 = idx2 = {':'}(ones (nd, 1)); # repmat ({':'}, [nd, 1]), but faster
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
108 idx1{dim} = 2 : n;
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
109 idx2{dim} = 1 : (n - 1);
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
110
12667
d8aff843a9e9 Accept input x vector with y matrix for trapz,cumtrapz (bug #33292).
Rik <octave@nomad.inbox5.com>
parents: 12642
diff changeset
111 if (! have_xy)
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
112 z = 0.5 * sum (x(idx1{:}) + x(idx2{:}), dim);
25575
b09ff039e6ff trapz.m: Overhaul function to fix errors with different orientations of X and Y (bug #54277).
Rik <rik@octave.org>
parents: 25054
diff changeset
113 elseif (isscalar (x))
b09ff039e6ff trapz.m: Overhaul function to fix errors with different orientations of X and Y (bug #54277).
Rik <rik@octave.org>
parents: 25054
diff changeset
114 z = x * 0.5 * sum (y(idx1{:}) + y(idx2{:}), dim);
b09ff039e6ff trapz.m: Overhaul function to fix errors with different orientations of X and Y (bug #54277).
Rik <rik@octave.org>
parents: 25054
diff changeset
115 elseif (isvector (x))
b09ff039e6ff trapz.m: Overhaul function to fix errors with different orientations of X and Y (bug #54277).
Rik <rik@octave.org>
parents: 25054
diff changeset
116 if (length (x) != n)
b09ff039e6ff trapz.m: Overhaul function to fix errors with different orientations of X and Y (bug #54277).
Rik <rik@octave.org>
parents: 25054
diff changeset
117 error ("trapz: length of X and length of Y along DIM must match");
b09ff039e6ff trapz.m: Overhaul function to fix errors with different orientations of X and Y (bug #54277).
Rik <rik@octave.org>
parents: 25054
diff changeset
118 endif
b09ff039e6ff trapz.m: Overhaul function to fix errors with different orientations of X and Y (bug #54277).
Rik <rik@octave.org>
parents: 25054
diff changeset
119 ## Reshape spacing vector x to point along dimension DIM
b09ff039e6ff trapz.m: Overhaul function to fix errors with different orientations of X and Y (bug #54277).
Rik <rik@octave.org>
parents: 25054
diff changeset
120 shape = ones (nd, 1);
b09ff039e6ff trapz.m: Overhaul function to fix errors with different orientations of X and Y (bug #54277).
Rik <rik@octave.org>
parents: 25054
diff changeset
121 shape(dim) = n;
b09ff039e6ff trapz.m: Overhaul function to fix errors with different orientations of X and Y (bug #54277).
Rik <rik@octave.org>
parents: 25054
diff changeset
122 x = reshape (x, shape);
b09ff039e6ff trapz.m: Overhaul function to fix errors with different orientations of X and Y (bug #54277).
Rik <rik@octave.org>
parents: 25054
diff changeset
123 z = 0.5 * sum (diff (x) .* (y(idx1{:}) + y(idx2{:})), dim);
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
124 else
25575
b09ff039e6ff trapz.m: Overhaul function to fix errors with different orientations of X and Y (bug #54277).
Rik <rik@octave.org>
parents: 25054
diff changeset
125 if (! size_equal (x, y))
b09ff039e6ff trapz.m: Overhaul function to fix errors with different orientations of X and Y (bug #54277).
Rik <rik@octave.org>
parents: 25054
diff changeset
126 error ("trapz: X and Y must have same shape");
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
127 endif
25575
b09ff039e6ff trapz.m: Overhaul function to fix errors with different orientations of X and Y (bug #54277).
Rik <rik@octave.org>
parents: 25054
diff changeset
128 z = 0.5 * sum (diff (x, 1, dim) .* (y(idx1{:}) + y(idx2{:})), dim);
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
129 endif
21758
ffad2baa90f7 maint: Use newlines to make code more readable.
Rik <rik@octave.org>
parents: 20852
diff changeset
130
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
131 endfunction
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
132
12667
d8aff843a9e9 Accept input x vector with y matrix for trapz,cumtrapz (bug #33292).
Rik <octave@nomad.inbox5.com>
parents: 12642
diff changeset
133
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
134 %!assert (trapz (1:5), 12)
25575
b09ff039e6ff trapz.m: Overhaul function to fix errors with different orientations of X and Y (bug #54277).
Rik <rik@octave.org>
parents: 25054
diff changeset
135 %!assert (trapz (1, 1:5), 12)
b09ff039e6ff trapz.m: Overhaul function to fix errors with different orientations of X and Y (bug #54277).
Rik <rik@octave.org>
parents: 25054
diff changeset
136 %!assert (trapz (0.5, 1:5), 6)
23827
3d67c7b99546 allow trapz to work on mixed row/column vectors (bug #33292)
John W. Eaton <jwe@octave.org>
parents: 23220
diff changeset
137 %!assert (trapz ([1:5], [1:5]), 12)
3d67c7b99546 allow trapz to work on mixed row/column vectors (bug #33292)
John W. Eaton <jwe@octave.org>
parents: 23220
diff changeset
138 %!assert (trapz ([1:5], [1:5]'), 12)
3d67c7b99546 allow trapz to work on mixed row/column vectors (bug #33292)
John W. Eaton <jwe@octave.org>
parents: 23220
diff changeset
139 %!assert (trapz ([1:5]', [1:5]'), 12)
3d67c7b99546 allow trapz to work on mixed row/column vectors (bug #33292)
John W. Eaton <jwe@octave.org>
parents: 23220
diff changeset
140 %!assert (trapz ([1:5]', [1:5]), 12)
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
141 %!assert (trapz (0:0.5:2,1:5), 6)
25575
b09ff039e6ff trapz.m: Overhaul function to fix errors with different orientations of X and Y (bug #54277).
Rik <rik@octave.org>
parents: 25054
diff changeset
142 %!assert (trapz ([1:5;1:5].', 1), [12, 12])
b09ff039e6ff trapz.m: Overhaul function to fix errors with different orientations of X and Y (bug #54277).
Rik <rik@octave.org>
parents: 25054
diff changeset
143 %!assert (trapz ([1:5;1:5], 2), [12; 12])
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
144 %!assert (trapz (repmat (reshape (1:5,1,1,5),2,2), 3), [12 12; 12 12])
25575
b09ff039e6ff trapz.m: Overhaul function to fix errors with different orientations of X and Y (bug #54277).
Rik <rik@octave.org>
parents: 25054
diff changeset
145 %!assert (trapz ([0:0.5:2;1:5].', [1:5;1:5].', 1), [6, 12])
b09ff039e6ff trapz.m: Overhaul function to fix errors with different orientations of X and Y (bug #54277).
Rik <rik@octave.org>
parents: 25054
diff changeset
146 %!assert (trapz ([0:0.5:2;1:5], [1:5;1:5], 2), [6; 12])
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
147 %!assert (trapz (repmat (reshape ([0:0.5:2],1,1,5),2,2), ...
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
148 %! repmat (reshape (1:5,1,1,5),2,2), 3), [6 6; 6 6])
25575
b09ff039e6ff trapz.m: Overhaul function to fix errors with different orientations of X and Y (bug #54277).
Rik <rik@octave.org>
parents: 25054
diff changeset
149 %!assert (trapz (0:0.5:2, [(1:5)', (1:5)']), [6, 6])
b09ff039e6ff trapz.m: Overhaul function to fix errors with different orientations of X and Y (bug #54277).
Rik <rik@octave.org>
parents: 25054
diff changeset
150 %!assert (trapz (0:0.5:2, [(1:5); (1:5)], 2), [6; 6])
b09ff039e6ff trapz.m: Overhaul function to fix errors with different orientations of X and Y (bug #54277).
Rik <rik@octave.org>
parents: 25054
diff changeset
151 %!assert (trapz (0:0.5:2, repmat (reshape (1:5,1,1,5),2,2),3), [6 6; 6 6])
b09ff039e6ff trapz.m: Overhaul function to fix errors with different orientations of X and Y (bug #54277).
Rik <rik@octave.org>
parents: 25054
diff changeset
152 %!assert <*54277> (trapz (ones (1,3), 1), zeros (1,3))
b09ff039e6ff trapz.m: Overhaul function to fix errors with different orientations of X and Y (bug #54277).
Rik <rik@octave.org>
parents: 25054
diff changeset
153 %!assert <*54277> (trapz (ones (3,1), 2), zeros (3,1))
b09ff039e6ff trapz.m: Overhaul function to fix errors with different orientations of X and Y (bug #54277).
Rik <rik@octave.org>
parents: 25054
diff changeset
154
b09ff039e6ff trapz.m: Overhaul function to fix errors with different orientations of X and Y (bug #54277).
Rik <rik@octave.org>
parents: 25054
diff changeset
155 ## Test input validation
28886
d8318c12d903 test: remove unnecessary BIST tests in m-files checking for excessive number of inputs.
Rik <rik@octave.org>
parents: 28789
diff changeset
156 %!error <Invalid call> trapz ()
25575
b09ff039e6ff trapz.m: Overhaul function to fix errors with different orientations of X and Y (bug #54277).
Rik <rik@octave.org>
parents: 25054
diff changeset
157 %!error <DIM must be an integer> trapz (1, 2, [1 2])
b09ff039e6ff trapz.m: Overhaul function to fix errors with different orientations of X and Y (bug #54277).
Rik <rik@octave.org>
parents: 25054
diff changeset
158 %!error <DIM must be an integer> trapz (1, 2, 1.5)
b09ff039e6ff trapz.m: Overhaul function to fix errors with different orientations of X and Y (bug #54277).
Rik <rik@octave.org>
parents: 25054
diff changeset
159 %!error <DIM must be .* a valid dimension> trapz (1, 2, 0)
b09ff039e6ff trapz.m: Overhaul function to fix errors with different orientations of X and Y (bug #54277).
Rik <rik@octave.org>
parents: 25054
diff changeset
160 %!error <length of X and length of Y.*must match> trapz ([1 2], [1 2 3])
b09ff039e6ff trapz.m: Overhaul function to fix errors with different orientations of X and Y (bug #54277).
Rik <rik@octave.org>
parents: 25054
diff changeset
161 %!error <X and Y must have same shape> trapz (ones (2,3), ones (2,4))