Mercurial > octave
annotate scripts/polynomial/conv.m @ 31191:bb9d776eafac stable
Fix wrong color in PDF printout of some latex strings (bug #62884)
* octave-svgconvert (draw): For "rect" elements only set brush color if
necessary and eventually restore to previous color.
author | Pantxo Diribarne <pantxo.diribarne@gmail.com> |
---|---|
date | Sun, 14 Aug 2022 18:24:07 +0200 |
parents | 796f54d4ddbf |
children | 5d3faba0342e |
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) 1994-2022 The Octave Project Developers |
27918
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
26376
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/>. |
2313 | 7 ## |
8 ## This file is part of Octave. | |
9 ## | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
24023
diff
changeset
|
10 ## Octave is free software: you can redistribute it and/or modify it |
2313 | 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:
24023
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. |
2313 | 14 ## |
15 ## Octave is distributed in the hope that it will be useful, but | |
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. |
2313 | 19 ## |
20 ## You should have received a copy of the GNU General Public License | |
7016 | 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:
24023
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 ######################################################################## |
1025 | 25 |
3368 | 26 ## -*- texinfo -*- |
20852
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
27 ## @deftypefn {} {} conv (@var{a}, @var{b}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
28 ## @deftypefnx {} {} conv (@var{a}, @var{b}, @var{shape}) |
13295
96c7143304d9
conv.m: Simplify algorithm and add more input validation and tests
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
29 ## Convolve two vectors @var{a} and @var{b}. |
3426 | 30 ## |
13295
96c7143304d9
conv.m: Simplify algorithm and add more input validation and tests
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
31 ## When @var{a} and @var{b} are the coefficient vectors of two polynomials, the |
96c7143304d9
conv.m: Simplify algorithm and add more input validation and tests
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
32 ## convolution represents the coefficient vector of the product polynomial. |
11082
4558aad4c41d
conv.m: handle "same" shape argument
John W. Eaton <jwe@octave.org>
parents:
10224
diff
changeset
|
33 ## |
24023
7dec20b876d6
Add documentation for shape "valid" in function "conv" (bug #48218)
Avinoam Kalma <a.kalma@gmail.com>
parents:
23220
diff
changeset
|
34 ## The size of the result is determined by the optional @var{shape} argument |
7dec20b876d6
Add documentation for shape "valid" in function "conv" (bug #48218)
Avinoam Kalma <a.kalma@gmail.com>
parents:
23220
diff
changeset
|
35 ## which takes the following values |
11082
4558aad4c41d
conv.m: handle "same" shape argument
John W. Eaton <jwe@octave.org>
parents:
10224
diff
changeset
|
36 ## |
11084 | 37 ## @table @asis |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
14363
diff
changeset
|
38 ## @item @var{shape} = @qcode{"full"} |
11085
2beacd515e09
Update docstrings for convolution family of functions (conv, conv2, fftconv)
Rik <octave@nomad.inbox5.com>
parents:
11084
diff
changeset
|
39 ## Return the full convolution. (default) |
24023
7dec20b876d6
Add documentation for shape "valid" in function "conv" (bug #48218)
Avinoam Kalma <a.kalma@gmail.com>
parents:
23220
diff
changeset
|
40 ## The result is a vector with length equal to |
7dec20b876d6
Add documentation for shape "valid" in function "conv" (bug #48218)
Avinoam Kalma <a.kalma@gmail.com>
parents:
23220
diff
changeset
|
41 ## @code{length (@var{a}) + length (@var{b}) - 1}. |
11084 | 42 ## |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
14363
diff
changeset
|
43 ## @item @var{shape} = @qcode{"same"} |
11085
2beacd515e09
Update docstrings for convolution family of functions (conv, conv2, fftconv)
Rik <octave@nomad.inbox5.com>
parents:
11084
diff
changeset
|
44 ## Return the central part of the convolution with the same size as @var{a}. |
24023
7dec20b876d6
Add documentation for shape "valid" in function "conv" (bug #48218)
Avinoam Kalma <a.kalma@gmail.com>
parents:
23220
diff
changeset
|
45 ## |
7dec20b876d6
Add documentation for shape "valid" in function "conv" (bug #48218)
Avinoam Kalma <a.kalma@gmail.com>
parents:
23220
diff
changeset
|
46 ## @item @var{shape} = @qcode{"valid"} |
7dec20b876d6
Add documentation for shape "valid" in function "conv" (bug #48218)
Avinoam Kalma <a.kalma@gmail.com>
parents:
23220
diff
changeset
|
47 ## Return only the parts which do not include zero-padded edges. |
24985
d85b2485af9e
doc: grammarcheck m-files ahead of 4.4 release.
Rik <rik@octave.org>
parents:
24534
diff
changeset
|
48 ## The size of the result is |
d85b2485af9e
doc: grammarcheck m-files ahead of 4.4 release.
Rik <rik@octave.org>
parents:
24534
diff
changeset
|
49 ## @code{max (size (@var{a}) - size (@var{b}) + 1, 0)}. |
11082
4558aad4c41d
conv.m: handle "same" shape argument
John W. Eaton <jwe@octave.org>
parents:
10224
diff
changeset
|
50 ## @end table |
4558aad4c41d
conv.m: handle "same" shape argument
John W. Eaton <jwe@octave.org>
parents:
10224
diff
changeset
|
51 ## |
14104
614505385171
doc: Overhaul docstrings for polynomial functions.
Rik <octave@nomad.inbox5.com>
parents:
13295
diff
changeset
|
52 ## @seealso{deconv, conv2, convn, fftconv} |
3368 | 53 ## @end deftypefn |
2311 | 54 |
11082
4558aad4c41d
conv.m: handle "same" shape argument
John W. Eaton <jwe@octave.org>
parents:
10224
diff
changeset
|
55 function y = conv (a, b, shape = "full") |
2325 | 56 |
28789
28de41192f3c
Eliminate unneeded verification of nargin, nargout in m-files.
Rik <rik@octave.org>
parents:
27985
diff
changeset
|
57 if (nargin < 2) |
6046 | 58 print_usage (); |
787 | 59 endif |
60 | |
4030 | 61 if (! (isvector (a) && isvector (b))) |
13295
96c7143304d9
conv.m: Simplify algorithm and add more input validation and tests
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
62 error ("conv: both arguments A and B must be vectors"); |
14347
12c70d00c04e
conv.m: Add "valid" as possible SHAPE parameter (Bug #34893)
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
63 elseif (nargin == 3 && ! any (strcmpi (shape, {"full", "same", "valid"}))) |
12c70d00c04e
conv.m: Add "valid" as possible SHAPE parameter (Bug #34893)
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
64 error ('conv: SHAPE argument must be "full", "same", or "valid"'); |
787 | 65 endif |
66 | |
26209
33913d29bed4
conv.m: Simplify function and improve performance (8X) (bug #55205).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
25054
diff
changeset
|
67 y = conv2 (a(:), b(:), shape); |
13295
96c7143304d9
conv.m: Simplify algorithm and add more input validation and tests
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
68 |
26209
33913d29bed4
conv.m: Simplify function and improve performance (8X) (bug #55205).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
25054
diff
changeset
|
69 if (strcmpi (shape, "full")) |
33913d29bed4
conv.m: Simplify function and improve performance (8X) (bug #55205).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
25054
diff
changeset
|
70 ## Adapt the shape to the longest input argument, if necessary. |
33913d29bed4
conv.m: Simplify function and improve performance (8X) (bug #55205).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
25054
diff
changeset
|
71 if ((length (a) > length (b) && isrow (a)) ... |
33913d29bed4
conv.m: Simplify function and improve performance (8X) (bug #55205).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
25054
diff
changeset
|
72 || (length (a) <= length (b) && isrow (b))) |
33913d29bed4
conv.m: Simplify function and improve performance (8X) (bug #55205).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
25054
diff
changeset
|
73 y = y.'; |
33913d29bed4
conv.m: Simplify function and improve performance (8X) (bug #55205).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
25054
diff
changeset
|
74 endif |
33913d29bed4
conv.m: Simplify function and improve performance (8X) (bug #55205).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
25054
diff
changeset
|
75 elseif (isrow (a)) |
33913d29bed4
conv.m: Simplify function and improve performance (8X) (bug #55205).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
25054
diff
changeset
|
76 ## Adapt the shape to the first input argument, if necessary. |
33913d29bed4
conv.m: Simplify function and improve performance (8X) (bug #55205).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
25054
diff
changeset
|
77 y = y.'; |
787 | 78 endif |
79 | |
80 endfunction | |
7411 | 81 |
13295
96c7143304d9
conv.m: Simplify algorithm and add more input validation and tests
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
82 |
9433
38a0f9dc0ab4
conv.m: fix Matlab incompatibility; new tests
Robert T. Short <octave@phaselockedsystems.com>
parents:
8920
diff
changeset
|
83 %!test |
14347
12c70d00c04e
conv.m: Add "valid" as possible SHAPE parameter (Bug #34893)
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
84 %! x = ones (3,1); |
12c70d00c04e
conv.m: Add "valid" as possible SHAPE parameter (Bug #34893)
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
85 %! y = ones (1,3); |
12c70d00c04e
conv.m: Add "valid" as possible SHAPE parameter (Bug #34893)
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
86 %! b = 2; |
12c70d00c04e
conv.m: Add "valid" as possible SHAPE parameter (Bug #34893)
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
87 %! c = 3; |
12c70d00c04e
conv.m: Add "valid" as possible SHAPE parameter (Bug #34893)
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
88 %! assert (conv (x, x), [1; 2; 3; 2; 1]); |
12c70d00c04e
conv.m: Add "valid" as possible SHAPE parameter (Bug #34893)
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
89 %! assert (conv (y, y), [1, 2, 3, 2, 1]); |
12c70d00c04e
conv.m: Add "valid" as possible SHAPE parameter (Bug #34893)
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
90 %! assert (conv (x, y), [1, 2, 3, 2, 1]); |
12c70d00c04e
conv.m: Add "valid" as possible SHAPE parameter (Bug #34893)
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
91 %! assert (conv (y, x), [1; 2; 3; 2; 1]); |
12c70d00c04e
conv.m: Add "valid" as possible SHAPE parameter (Bug #34893)
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
92 %! assert (conv (c, x), [3; 3; 3]); |
12c70d00c04e
conv.m: Add "valid" as possible SHAPE parameter (Bug #34893)
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
93 %! assert (conv (c, y), [3, 3, 3]); |
12c70d00c04e
conv.m: Add "valid" as possible SHAPE parameter (Bug #34893)
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
94 %! assert (conv (x, c), [3; 3; 3]); |
12c70d00c04e
conv.m: Add "valid" as possible SHAPE parameter (Bug #34893)
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
95 %! assert (conv (y, c), [3, 3, 3]); |
12c70d00c04e
conv.m: Add "valid" as possible SHAPE parameter (Bug #34893)
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
96 %! assert (conv (b, c), 6); |
11085
2beacd515e09
Update docstrings for convolution family of functions (conv, conv2, fftconv)
Rik <octave@nomad.inbox5.com>
parents:
11084
diff
changeset
|
97 |
14347
12c70d00c04e
conv.m: Add "valid" as possible SHAPE parameter (Bug #34893)
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
98 %!shared a,b |
8158
15e4a450bf84
conv.m: Correct row/col orientation of output
Ben Abbott <bpabbott@mac.com>
parents:
7411
diff
changeset
|
99 %!test |
14347
12c70d00c04e
conv.m: Add "valid" as possible SHAPE parameter (Bug #34893)
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
100 %! a = 1:10; |
12c70d00c04e
conv.m: Add "valid" as possible SHAPE parameter (Bug #34893)
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
101 %! b = 1:3; |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14352
diff
changeset
|
102 %!assert (size (conv (a,b)), [1, numel(a)+numel(b)-1]) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14352
diff
changeset
|
103 %!assert (size (conv (b,a)), [1, numel(a)+numel(b)-1]) |
11082
4558aad4c41d
conv.m: handle "same" shape argument
John W. Eaton <jwe@octave.org>
parents:
10224
diff
changeset
|
104 |
13295
96c7143304d9
conv.m: Simplify algorithm and add more input validation and tests
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
105 %!test |
14347
12c70d00c04e
conv.m: Add "valid" as possible SHAPE parameter (Bug #34893)
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
106 %! a = (1:10).'; |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14352
diff
changeset
|
107 %!assert (size (conv (a,b)), [numel(a)+numel(b)-1, 1]) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14352
diff
changeset
|
108 %!assert (size (conv (b,a)), [numel(a)+numel(b)-1, 1]) |
14347
12c70d00c04e
conv.m: Add "valid" as possible SHAPE parameter (Bug #34893)
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
109 |
12c70d00c04e
conv.m: Add "valid" as possible SHAPE parameter (Bug #34893)
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
110 %!test |
12c70d00c04e
conv.m: Add "valid" as possible SHAPE parameter (Bug #34893)
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
111 %! a = 1:10; |
12c70d00c04e
conv.m: Add "valid" as possible SHAPE parameter (Bug #34893)
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
112 %! b = (1:3).'; |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14352
diff
changeset
|
113 %!assert (size (conv (a,b)), [1, numel(a)+numel(b)-1]) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14352
diff
changeset
|
114 %!assert (size (conv (b,a)), [1, numel(a)+numel(b)-1]) |
9433
38a0f9dc0ab4
conv.m: fix Matlab incompatibility; new tests
Robert T. Short <octave@phaselockedsystems.com>
parents:
8920
diff
changeset
|
115 |
8158
15e4a450bf84
conv.m: Correct row/col orientation of output
Ben Abbott <bpabbott@mac.com>
parents:
7411
diff
changeset
|
116 %!test |
14347
12c70d00c04e
conv.m: Add "valid" as possible SHAPE parameter (Bug #34893)
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
117 %! a = 1:10; |
12c70d00c04e
conv.m: Add "valid" as possible SHAPE parameter (Bug #34893)
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
118 %! b = 1:3; |
12c70d00c04e
conv.m: Add "valid" as possible SHAPE parameter (Bug #34893)
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
119 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14352
diff
changeset
|
120 %!assert (conv (a,b,"full"), conv (a,b)) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14352
diff
changeset
|
121 %!assert (conv (b,a,"full"), conv (b,a)) |
11124 | 122 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14352
diff
changeset
|
123 %!assert (conv (a,b,"same"), [4, 10, 16, 22, 28, 34, 40, 46, 52, 47]) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14352
diff
changeset
|
124 %!assert (conv (b,a,"same"), [28, 34, 40]) |
11085
2beacd515e09
Update docstrings for convolution family of functions (conv, conv2, fftconv)
Rik <octave@nomad.inbox5.com>
parents:
11084
diff
changeset
|
125 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14352
diff
changeset
|
126 %!assert (conv (a,b,"valid"), [10, 16, 22, 28, 34, 40, 46, 52]) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14352
diff
changeset
|
127 %!assert (conv (b,a,"valid"), zeros (1,0)) |
14347
12c70d00c04e
conv.m: Add "valid" as possible SHAPE parameter (Bug #34893)
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
128 |
11082
4558aad4c41d
conv.m: handle "same" shape argument
John W. Eaton <jwe@octave.org>
parents:
10224
diff
changeset
|
129 |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
130 ## 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
|
131 %!error <Invalid call> conv (1) |
14347
12c70d00c04e
conv.m: Add "valid" as possible SHAPE parameter (Bug #34893)
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
132 %!error <A and B must be vectors> conv ([1, 2; 3, 4], 3) |
12c70d00c04e
conv.m: Add "valid" as possible SHAPE parameter (Bug #34893)
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
133 %!error <A and B must be vectors> conv (3, [1, 2; 3, 4]) |
12c70d00c04e
conv.m: Add "valid" as possible SHAPE parameter (Bug #34893)
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
134 %!error <SHAPE argument must be> conv (2, 3, "INVALID_SHAPE") |