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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5819
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
1 /*
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
2
8920
eb63fbe60fab update copyright notices
John W. Eaton <jwe@octave.org>
parents: 7814
diff changeset
3 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
7017
a1dbe9d80eee [project @ 2007-10-12 21:27:11 by jwe]
jwe
parents: 7016
diff changeset
4 Andy Adler
5819
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
5
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
6 This file is part of Octave.
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
7
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
8 Octave is free software; you can redistribute it and/or modify it
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
9 under the terms of the GNU General Public License as published by the
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 5823
diff changeset
10 Free Software Foundation; either version 3 of the License, or (at your
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 5823
diff changeset
11 option) any later version.
5819
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
12
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
13 Octave is distributed in the hope that it will be useful, but WITHOUT
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
16 for more details.
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
17
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
18 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: 5823
diff changeset
19 along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 5823
diff changeset
20 <http://www.gnu.org/licenses/>.
5819
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
21
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
22 */
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
23
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
24 #ifdef HAVE_CONFIG_H
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
25 #include <config.h>
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
26 #endif
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
27
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
28 #include "defun-dld.h"
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
29 #include "error.h"
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
30 #include "oct-obj.h"
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
31 #include "utils.h"
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
32
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
33 enum Shape { SHAPE_FULL, SHAPE_SAME, SHAPE_VALID };
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
34
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
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
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
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
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
51 #endif
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
52
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
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
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
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
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
59 octave_idx_type Am = A.rows ();
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
60 octave_idx_type An = A.columns ();
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
61
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
62 // Calculate the size of the output matrix:
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
63 // in order to stay Matlab compatible, it is based
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
64 // on the third parameter if it's separable, and the
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
65 // first if it's not
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
66
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
67 octave_idx_type outM = 0;
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
68 octave_idx_type outN = 0;
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
69 octave_idx_type edgM = 0;
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
70 octave_idx_type edgN = 0;
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
71
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
72 switch (ishape)
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
73 {
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
74 case SHAPE_FULL:
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
75 outM = Am + Cm - 1;
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
76 outN = An + Rn - 1;
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
77 edgM = Cm - 1;
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
78 edgN = Rn - 1;
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
79 break;
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
80
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
81 case SHAPE_SAME:
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
82 outM = Am;
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
83 outN = An;
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
84 // Follow the Matlab convention (ie + instead of -)
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
85 edgM = (Cm - 1) /2;
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
86 edgN = (Rn - 1) /2;
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
87 break;
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
88
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
89 case SHAPE_VALID:
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
90 outM = Am - Cm + 1;
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
91 outN = An - Rn + 1;
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
92 if (outM < 0)
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
93 outM = 0;
5819
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
94 if (outN < 0)
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
95 outN = 0;
5819
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
96 edgM = edgN = 0;
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
97 break;
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
98
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
99 default:
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
100 error ("conv2: invalid value of parameter ishape");
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
101 }
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
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
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
104
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
105 // X accumulates the 1-D conv for each row, before calculating
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
106 // the convolution in the other direction
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
107 // There is no efficiency advantage to doing it in either direction
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
108 // first
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
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
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
111
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
112 for (octave_idx_type oi = 0; oi < outM; oi++)
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
113 {
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
114 for (octave_idx_type oj = 0; oj < An; oj++)
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
115 {
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
116 T sum = 0;
5819
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
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
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
124
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
125 X(oj) = sum;
5819
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
126 }
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
127
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
128 for (octave_idx_type oj = 0; oj < outN; oj++)
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
129 {
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
130 T sum = 0;
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
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
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
134 const T* Xd = X.data() + aj;
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
135 const T* Rd = R.data() + rj;
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
136
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
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
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
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
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
141 }
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
142 }
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
143
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
144 return O;
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
145 }
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
146
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
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
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
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
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
159 #endif
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
160
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
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
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
164 {
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
165 // Convolution works fastest if we choose the A matrix to be
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
166 // the largest.
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
167
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
168 // Here we calculate the size of the output matrix,
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
169 // in order to stay Matlab compatible, it is based
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
170 // on the third parameter if it's separable, and the
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
171 // first if it's not
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
172
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
173 // NOTE in order to be Matlab compatible, we give argueably
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
174 // wrong sizes for 'valid' if the smallest matrix is first
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
175
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
176 octave_idx_type Am = A.rows ();
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
177 octave_idx_type An = A.columns ();
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
178 octave_idx_type Bm = B.rows ();
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
179 octave_idx_type Bn = B.columns ();
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
180
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
181 octave_idx_type outM = 0;
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
182 octave_idx_type outN = 0;
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
183 octave_idx_type edgM = 0;
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
184 octave_idx_type edgN = 0;
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
185
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
186 switch (ishape)
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
187 {
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
188 case SHAPE_FULL:
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
189 outM = Am + Bm - 1;
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
190 outN = An + Bn - 1;
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
191 edgM = Bm - 1;
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
192 edgN = Bn - 1;
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
193 break;
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
194
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
195 case SHAPE_SAME:
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
196 outM = Am;
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
197 outN = An;
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
198 edgM = (Bm - 1) /2;
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
199 edgN = (Bn - 1) /2;
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
200 break;
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
201
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
202 case SHAPE_VALID:
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
203 outM = Am - Bm + 1;
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
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
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
209 edgM = edgN = 0;
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
210 break;
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
211 }
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
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
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
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
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
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
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
223 {
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
224 T sum = 0;
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
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
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
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
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
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
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
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
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
241 }
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
242 }
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
243
10367
173e10268080 avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents: 10350
diff changeset
244 *Od++ = sum;
5819
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
245 }
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
246 }
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
247
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
248 return O;
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
249 }
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
250
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
251 /*
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
252 %!test
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
253 %! b = [0,1,2,3;1,8,12,12;4,20,24,21;7,22,25,18];
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
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
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
259 */
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
260
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
261 DEFUN_DLD (conv2, args, ,
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
262 "-*- texinfo -*-\n\
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
263 @deftypefn {Loadable Function} {y =} conv2 (@var{a}, @var{b}, @var{shape})\n\
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
264 @deftypefnx {Loadable Function} {y =} conv2 (@var{v1}, @var{v2}, @var{M}, @var{shape})\n\
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
265 \n\
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
266 Returns 2D convolution of @var{a} and @var{b} where the size\n\
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
267 of @var{c} is given by\n\
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
268 \n\
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
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
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
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
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
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
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
275 only parts which do not include zero-padded edges\n\
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
276 @end table\n\
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
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
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
279 returns the convolution of the matrix @var{M} by the vector @var{v1}\n\
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
280 in the column direction and by vector @var{v2} in the row direction\n\
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
281 @end deftypefn")
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
282 {
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
283 octave_value retval;
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
284 octave_value tmp;
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
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
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
288 Shape ishape;
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
289
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
290 if (nargin < 2)
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
291 {
5823
080c08b192d8 [project @ 2006-05-19 05:32:17 by jwe]
jwe
parents: 5822
diff changeset
292 print_usage ();
5819
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
293 return retval;
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
294 }
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
295 else if (nargin == 3)
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
296 {
5822
4fdc2515ebad [project @ 2006-05-18 19:31:36 by jwe]
jwe
parents: 5819
diff changeset
297 if (args(2).is_string ())
5819
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
298 shape = args(2).string_value ();
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
299 else
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
300 separable = true;
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
301 }
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
302 else if (nargin >= 4)
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
303 {
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
304 separable = true;
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
305 shape = args(3).string_value ();
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
306 }
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
307
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
308 if (shape == "full")
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
309 ishape = SHAPE_FULL;
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
310 else if (shape == "same")
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
311 ishape = SHAPE_SAME;
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
312 else if (shape == "valid")
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
313 ishape = SHAPE_VALID;
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
314 else
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
315 {
5822
4fdc2515ebad [project @ 2006-05-18 19:31:36 by jwe]
jwe
parents: 5819
diff changeset
316 error ("conv2: shape type not valid");
5823
080c08b192d8 [project @ 2006-05-19 05:32:17 by jwe]
jwe
parents: 5822
diff changeset
317 print_usage ();
5819
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
318 return retval;
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
319 }
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
320
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
321 if (separable)
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
322 {
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
323 // If user requests separable, check first two params are vectors
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
324
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
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
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
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
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
330 }
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
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
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
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
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
380 } // if (separable)
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
381 else
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
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
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
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
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
424
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
425 } // if (separable)
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
426
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
427 return retval;
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
428 }
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
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
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
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
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
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
e54c11df0524 [project @ 2006-05-17 20:34:52 by jwe]
jwe
parents:
diff changeset
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);