Mercurial > octave-nkf
annotate src/DLD-FUNCTIONS/conv2.cc @ 10367:173e10268080
avoid indexing nonexistent elements in sparse diag
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Sat, 27 Feb 2010 16:05:16 -0500 |
parents | 12884915a8e4 |
children | d2849dbcc858 |
rev | line source |
---|---|
5819 | 1 /* |
2 | |
8920 | 3 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 |
7017 | 4 Andy Adler |
5819 | 5 |
6 This file is part of Octave. | |
7 | |
8 Octave is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
5819 | 12 |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
5819 | 21 |
22 */ | |
23 | |
24 #ifdef HAVE_CONFIG_H | |
25 #include <config.h> | |
26 #endif | |
27 | |
28 #include "defun-dld.h" | |
29 #include "error.h" | |
30 #include "oct-obj.h" | |
31 #include "utils.h" | |
32 | |
33 enum Shape { SHAPE_FULL, SHAPE_SAME, SHAPE_VALID }; | |
34 | |
35 #if !defined (CXX_NEW_FRIEND_TEMPLATE_DECL) | |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
36 extern MArray<double> |
10367
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10350
diff
changeset
|
37 conv2 (const MArray<double>&, const MArray<double>&, const MArray<double>&, |
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10350
diff
changeset
|
38 Shape); |
5819 | 39 |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
40 extern MArray<Complex> |
10367
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10350
diff
changeset
|
41 conv2 (const MArray<Complex>&, const MArray<Complex>&, |
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10350
diff
changeset
|
42 const MArray<Complex>&, Shape); |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
43 |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
44 extern MArray<float> |
10367
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10350
diff
changeset
|
45 conv2 (const MArray<float>&, const MArray<float>&, const MArray<float>&, |
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10350
diff
changeset
|
46 Shape); |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
47 |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
48 extern MArray<FloatComplex> |
10367
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10350
diff
changeset
|
49 conv2 (const MArray<FloatComplex>&, const MArray<FloatComplex>&, |
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10350
diff
changeset
|
50 const MArray<FloatComplex>&, Shape); |
5819 | 51 #endif |
52 | |
53 template <class T> | |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
54 MArray<T> |
10367
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10350
diff
changeset
|
55 conv2 (const MArray<T>& R, const MArray<T>& C, const MArray<T>& A, Shape ishape) |
5819 | 56 { |
10367
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10350
diff
changeset
|
57 octave_idx_type Rn = R.length (); |
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10350
diff
changeset
|
58 octave_idx_type Cm = C.length (); |
5819 | 59 octave_idx_type Am = A.rows (); |
60 octave_idx_type An = A.columns (); | |
61 | |
62 // Calculate the size of the output matrix: | |
63 // in order to stay Matlab compatible, it is based | |
64 // on the third parameter if it's separable, and the | |
65 // first if it's not | |
66 | |
67 octave_idx_type outM = 0; | |
68 octave_idx_type outN = 0; | |
69 octave_idx_type edgM = 0; | |
70 octave_idx_type edgN = 0; | |
71 | |
72 switch (ishape) | |
73 { | |
74 case SHAPE_FULL: | |
75 outM = Am + Cm - 1; | |
76 outN = An + Rn - 1; | |
77 edgM = Cm - 1; | |
78 edgN = Rn - 1; | |
79 break; | |
80 | |
81 case SHAPE_SAME: | |
82 outM = Am; | |
83 outN = An; | |
84 // Follow the Matlab convention (ie + instead of -) | |
85 edgM = (Cm - 1) /2; | |
86 edgN = (Rn - 1) /2; | |
87 break; | |
88 | |
89 case SHAPE_VALID: | |
90 outM = Am - Cm + 1; | |
91 outN = An - Rn + 1; | |
92 if (outM < 0) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
93 outM = 0; |
5819 | 94 if (outN < 0) |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
95 outN = 0; |
5819 | 96 edgM = edgN = 0; |
97 break; | |
98 | |
99 default: | |
100 error ("conv2: invalid value of parameter ishape"); | |
101 } | |
102 | |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
103 MArray<T> O (outM, outN); |
5819 | 104 |
105 // X accumulates the 1-D conv for each row, before calculating | |
106 // the convolution in the other direction | |
107 // There is no efficiency advantage to doing it in either direction | |
108 // first | |
109 | |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
110 MArray<T> X (An, 1); |
5819 | 111 |
112 for (octave_idx_type oi = 0; oi < outM; oi++) | |
113 { | |
114 for (octave_idx_type oj = 0; oj < An; oj++) | |
115 { | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
116 T sum = 0; |
5819 | 117 |
10367
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10350
diff
changeset
|
118 octave_idx_type ci = Cm - 1 - std::max (0, edgM-oi); |
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10350
diff
changeset
|
119 octave_idx_type ai = std::max (0, oi-edgM); |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
120 const T* Ad = A.data() + ai + Am*oj; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
121 const T* Cd = C.data() + ci; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
122 for ( ; ci >= 0 && ai < Am; ci--, Cd--, ai++, Ad++) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
123 sum += (*Ad) * (*Cd); |
5819 | 124 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
125 X(oj) = sum; |
5819 | 126 } |
127 | |
128 for (octave_idx_type oj = 0; oj < outN; oj++) | |
129 { | |
130 T sum = 0; | |
131 | |
10367
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10350
diff
changeset
|
132 octave_idx_type rj = Rn - 1 - std::max (0, edgN-oj); |
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10350
diff
changeset
|
133 octave_idx_type aj = std::max (0, oj-edgN); |
5819 | 134 const T* Xd = X.data() + aj; |
135 const T* Rd = R.data() + rj; | |
136 | |
137 for ( ; rj >= 0 && aj < An; rj--, Rd--, aj++, Xd++) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
138 sum += (*Xd) * (*Rd); |
5819 | 139 |
10367
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10350
diff
changeset
|
140 O(oi,oj) = sum; |
5819 | 141 } |
142 } | |
143 | |
144 return O; | |
145 } | |
146 | |
147 #if !defined (CXX_NEW_FRIEND_TEMPLATE_DECL) | |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
148 extern MArray<double> |
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
149 conv2 (MArray<double>&, MArray<double>&, Shape); |
5819 | 150 |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
151 extern MArray<Complex> |
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
152 conv2 (MArray<Complex>&, MArray<Complex>&, Shape); |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
153 |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
154 extern MArray<float> |
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
155 conv2 (MArray<float>&, MArray<float>&, Shape); |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
156 |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
157 extern MArray<FloatComplex> |
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
158 conv2 (MArray<FloatComplex>&, MArray<FloatComplex>&, Shape); |
5819 | 159 #endif |
160 | |
161 template <class T> | |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
162 MArray<T> |
10367
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10350
diff
changeset
|
163 conv2 (const MArray<T>& A, const MArray<T>& B, Shape ishape) |
5819 | 164 { |
165 // Convolution works fastest if we choose the A matrix to be | |
166 // the largest. | |
167 | |
168 // Here we calculate the size of the output matrix, | |
169 // in order to stay Matlab compatible, it is based | |
170 // on the third parameter if it's separable, and the | |
171 // first if it's not | |
172 | |
173 // NOTE in order to be Matlab compatible, we give argueably | |
174 // wrong sizes for 'valid' if the smallest matrix is first | |
175 | |
176 octave_idx_type Am = A.rows (); | |
177 octave_idx_type An = A.columns (); | |
178 octave_idx_type Bm = B.rows (); | |
179 octave_idx_type Bn = B.columns (); | |
180 | |
181 octave_idx_type outM = 0; | |
182 octave_idx_type outN = 0; | |
183 octave_idx_type edgM = 0; | |
184 octave_idx_type edgN = 0; | |
185 | |
186 switch (ishape) | |
187 { | |
188 case SHAPE_FULL: | |
189 outM = Am + Bm - 1; | |
190 outN = An + Bn - 1; | |
191 edgM = Bm - 1; | |
192 edgN = Bn - 1; | |
193 break; | |
194 | |
195 case SHAPE_SAME: | |
196 outM = Am; | |
197 outN = An; | |
198 edgM = (Bm - 1) /2; | |
199 edgN = (Bn - 1) /2; | |
200 break; | |
201 | |
202 case SHAPE_VALID: | |
203 outM = Am - Bm + 1; | |
204 outN = An - Bn + 1; | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
205 if (outM < 0) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
206 outM = 0; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
207 if (outN < 0) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
208 outN = 0; |
5819 | 209 edgM = edgN = 0; |
210 break; | |
211 } | |
212 | |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
213 MArray<T> O (outM, outN); |
5819 | 214 |
10367
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10350
diff
changeset
|
215 T *Od = O.fortran_vec (); |
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10350
diff
changeset
|
216 |
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10350
diff
changeset
|
217 for (octave_idx_type oj = 0; oj < outN; oj++) |
5819 | 218 { |
10367
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10350
diff
changeset
|
219 octave_idx_type aj0 = std::max (0, oj-edgN); |
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10350
diff
changeset
|
220 octave_idx_type bj0 = Bn - 1 - std::max (0, edgN-oj); |
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10350
diff
changeset
|
221 |
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10350
diff
changeset
|
222 for (octave_idx_type oi = 0; oi < outM; oi++) |
5819 | 223 { |
224 T sum = 0; | |
225 | |
10367
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10350
diff
changeset
|
226 octave_idx_type bi0 = Bm - 1 - std::max (0, edgM-oi); |
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10350
diff
changeset
|
227 octave_idx_type ai0 = std::max (0, oi-edgM); |
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10350
diff
changeset
|
228 |
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10350
diff
changeset
|
229 for (octave_idx_type aj = aj0, bj = bj0; bj >= 0 && aj < An; |
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10350
diff
changeset
|
230 bj--, aj++) |
5819 | 231 { |
10367
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10350
diff
changeset
|
232 const T* Ad = A.data () + ai0 + Am*aj; |
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10350
diff
changeset
|
233 const T* Bd = B.data () + bi0 + Bm*bj; |
5819 | 234 |
10367
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10350
diff
changeset
|
235 for (octave_idx_type ai = ai0, bi = bi0; bi >= 0 && ai < Am; |
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10350
diff
changeset
|
236 bi--, ai++) |
5819 | 237 { |
10367
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10350
diff
changeset
|
238 sum += (*Ad++) * (*Bd--); |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
239 // Comment: it seems to be 2.5 x faster than this: |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
240 // sum+= A(ai,aj) * B(bi,bj); |
5819 | 241 } |
242 } | |
243 | |
10367
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10350
diff
changeset
|
244 *Od++ = sum; |
5819 | 245 } |
246 } | |
247 | |
248 return O; | |
249 } | |
250 | |
251 /* | |
252 %!test | |
253 %! b = [0,1,2,3;1,8,12,12;4,20,24,21;7,22,25,18]; | |
254 %! assert(conv2([0,1;1,2],[1,2,3;4,5,6;7,8,9]),b); | |
7814
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
255 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
256 %!test |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
257 %! b = single([0,1,2,3;1,8,12,12;4,20,24,21;7,22,25,18]); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
258 %! assert(conv2(single([0,1;1,2]),single([1,2,3;4,5,6;7,8,9])),b); |
5819 | 259 */ |
260 | |
261 DEFUN_DLD (conv2, args, , | |
262 "-*- texinfo -*-\n\ | |
263 @deftypefn {Loadable Function} {y =} conv2 (@var{a}, @var{b}, @var{shape})\n\ | |
264 @deftypefnx {Loadable Function} {y =} conv2 (@var{v1}, @var{v2}, @var{M}, @var{shape})\n\ | |
265 \n\ | |
266 Returns 2D convolution of @var{a} and @var{b} where the size\n\ | |
267 of @var{c} is given by\n\ | |
268 \n\ | |
269 @table @asis\n\ | |
10367
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10350
diff
changeset
|
270 @item @var{shape} = 'full'\n\ |
5819 | 271 returns full 2-D convolution\n\ |
10367
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10350
diff
changeset
|
272 @item @var{shape} = 'same'\n\ |
5819 | 273 same size as a. 'central' part of convolution\n\ |
10367
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10350
diff
changeset
|
274 @item @var{shape} = 'valid'\n\ |
5819 | 275 only parts which do not include zero-padded edges\n\ |
276 @end table\n\ | |
277 \n\ | |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
278 By default @var{shape} is 'full'. When the third argument is a matrix\n\ |
5819 | 279 returns the convolution of the matrix @var{M} by the vector @var{v1}\n\ |
280 in the column direction and by vector @var{v2} in the row direction\n\ | |
281 @end deftypefn") | |
282 { | |
283 octave_value retval; | |
284 octave_value tmp; | |
285 int nargin = args.length (); | |
10367
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10350
diff
changeset
|
286 std::string shape = "full"; //default |
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10350
diff
changeset
|
287 bool separable = false; |
5819 | 288 Shape ishape; |
289 | |
290 if (nargin < 2) | |
291 { | |
5823 | 292 print_usage (); |
5819 | 293 return retval; |
294 } | |
295 else if (nargin == 3) | |
296 { | |
5822 | 297 if (args(2).is_string ()) |
5819 | 298 shape = args(2).string_value (); |
299 else | |
300 separable = true; | |
301 } | |
302 else if (nargin >= 4) | |
303 { | |
304 separable = true; | |
305 shape = args(3).string_value (); | |
306 } | |
307 | |
308 if (shape == "full") | |
309 ishape = SHAPE_FULL; | |
310 else if (shape == "same") | |
311 ishape = SHAPE_SAME; | |
312 else if (shape == "valid") | |
313 ishape = SHAPE_VALID; | |
314 else | |
315 { | |
5822 | 316 error ("conv2: shape type not valid"); |
5823 | 317 print_usage (); |
5819 | 318 return retval; |
319 } | |
320 | |
321 if (separable) | |
322 { | |
323 // If user requests separable, check first two params are vectors | |
324 | |
325 if (! (1 == args(0).rows () || 1 == args(0).columns ()) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
326 || ! (1 == args(1).rows () || 1 == args(1).columns ())) |
5819 | 327 { |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
328 print_usage (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
329 return retval; |
5819 | 330 } |
331 | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
332 if (args(0).is_single_type () || |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
333 args(1).is_single_type () || |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
334 args(2).is_single_type ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
335 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
336 if (args(0).is_complex_type () |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
337 || args(1).is_complex_type () |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
338 || args(2).is_complex_type ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
339 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
340 FloatComplexColumnVector v1 (args(0).float_complex_vector_value ()); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
341 FloatComplexColumnVector v2 (args(1).float_complex_vector_value ()); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
342 FloatComplexMatrix a (args(2).float_complex_matrix_value ()); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
343 FloatComplexMatrix c (conv2 (v1, v2, a, ishape)); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
344 if (! error_state) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
345 retval = c; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
346 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
347 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
348 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
349 FloatColumnVector v1 (args(0).float_vector_value ()); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
350 FloatColumnVector v2 (args(1).float_vector_value ()); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
351 FloatMatrix a (args(2).float_matrix_value ()); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
352 FloatMatrix c (conv2 (v1, v2, a, ishape)); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
353 if (! error_state) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
354 retval = c; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
355 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
356 } |
5819 | 357 else |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
358 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
359 if (args(0).is_complex_type () |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
360 || args(1).is_complex_type () |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
361 || args(2).is_complex_type ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
362 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
363 ComplexColumnVector v1 (args(0).complex_vector_value ()); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
364 ComplexColumnVector v2 (args(1).complex_vector_value ()); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
365 ComplexMatrix a (args(2).complex_matrix_value ()); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
366 ComplexMatrix c (conv2 (v1, v2, a, ishape)); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
367 if (! error_state) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
368 retval = c; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
369 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
370 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
371 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
372 ColumnVector v1 (args(0).vector_value ()); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
373 ColumnVector v2 (args(1).vector_value ()); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
374 Matrix a (args(2).matrix_value ()); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
375 Matrix c (conv2 (v1, v2, a, ishape)); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
376 if (! error_state) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
377 retval = c; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
378 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
379 } |
5819 | 380 } // if (separable) |
381 else | |
382 { | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
383 if (args(0).is_single_type () || |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
384 args(1).is_single_type ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
385 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
386 if (args(0).is_complex_type () |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
387 || args(1).is_complex_type ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
388 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
389 FloatComplexMatrix a (args(0).float_complex_matrix_value ()); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
390 FloatComplexMatrix b (args(1).float_complex_matrix_value ()); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
391 FloatComplexMatrix c (conv2 (a, b, ishape)); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
392 if (! error_state) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
393 retval = c; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
394 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
395 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
396 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
397 FloatMatrix a (args(0).float_matrix_value ()); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
398 FloatMatrix b (args(1).float_matrix_value ()); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
399 FloatMatrix c (conv2 (a, b, ishape)); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
400 if (! error_state) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
401 retval = c; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
402 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
403 } |
5819 | 404 else |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
405 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
406 if (args(0).is_complex_type () |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
407 || args(1).is_complex_type ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
408 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
409 ComplexMatrix a (args(0).complex_matrix_value ()); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
410 ComplexMatrix b (args(1).complex_matrix_value ()); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
411 ComplexMatrix c (conv2 (a, b, ishape)); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
412 if (! error_state) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
413 retval = c; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
414 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
415 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
416 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
417 Matrix a (args(0).matrix_value ()); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
418 Matrix b (args(1).matrix_value ()); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
419 Matrix c (conv2 (a, b, ishape)); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
420 if (! error_state) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
421 retval = c; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
422 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
423 } |
5819 | 424 |
425 } // if (separable) | |
426 | |
427 return retval; | |
428 } | |
429 | |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
430 template MArray<double> |
10367
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10350
diff
changeset
|
431 conv2 (const MArray<double>&, const MArray<double>&, const MArray<double>&, |
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10350
diff
changeset
|
432 Shape); |
5819 | 433 |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
434 template MArray<double> |
10367
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10350
diff
changeset
|
435 conv2 (const MArray<double>&, const MArray<double>&, Shape); |
5819 | 436 |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
437 template MArray<Complex> |
10367
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10350
diff
changeset
|
438 conv2 (const MArray<Complex>&, const MArray<Complex>&, |
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10350
diff
changeset
|
439 const MArray<Complex>&, Shape); |
5819 | 440 |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
441 template MArray<Complex> |
10367
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10350
diff
changeset
|
442 conv2 (const MArray<Complex>&, const MArray<Complex>&, Shape); |