Mercurial > octave-nkf
annotate src/DLD-FUNCTIONS/__convn__.cc @ 10154:40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 20 Jan 2010 17:33:41 -0500 |
parents | 30b952e90c29 |
children | 12884915a8e4 |
rev | line source |
---|---|
7640 | 1 /* |
2 | |
3 Copyright (C) 2008 Soren Hauberg | |
4 | |
5 This file is part of Octave. | |
6 | |
7 Octave is free software; you can redistribute it and/or modify it | |
8 under the terms of the GNU General Public License as published by the | |
9 Free Software Foundation; either version 3 of the License, or (at your | |
10 option) any later version. | |
11 | |
12 Octave is distributed in the hope that it will be useful, but WITHOUT | |
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 for more details. | |
16 | |
17 You should have received a copy of the GNU General Public License | |
18 along with Octave; see the file COPYING. If not, see | |
19 <http://www.gnu.org/licenses/>. | |
20 | |
21 */ | |
22 | |
23 #ifdef HAVE_CONFIG_H | |
24 #include <config.h> | |
25 #endif | |
26 | |
27 #include <algorithm> | |
28 | |
29 #include "dNDArray.h" | |
30 #include "CNDArray.h" | |
31 | |
32 #include "defun-dld.h" | |
33 | |
7660 | 34 template <class T1, class T2> |
35 class | |
36 octave_convn_traits | |
37 { | |
38 public: | |
39 // The return type for a T1 by T2 convn operation. | |
40 typedef T1 TR; | |
41 }; | |
42 | |
43 #define OCTAVE_CONVN_TRAIT(T1, T2, T3) \ | |
44 template<> \ | |
45 class octave_convn_traits <T1, T2> \ | |
46 { \ | |
47 public: \ | |
48 typedef T3 TR; \ | |
49 } | |
50 | |
51 OCTAVE_CONVN_TRAIT (NDArray, NDArray, NDArray); | |
52 OCTAVE_CONVN_TRAIT (ComplexNDArray, NDArray, ComplexNDArray); | |
53 OCTAVE_CONVN_TRAIT (NDArray, ComplexNDArray, ComplexNDArray); | |
54 OCTAVE_CONVN_TRAIT (ComplexNDArray, ComplexNDArray, ComplexNDArray); | |
55 | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7661
diff
changeset
|
56 OCTAVE_CONVN_TRAIT (FloatNDArray, FloatNDArray, FloatNDArray); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7661
diff
changeset
|
57 OCTAVE_CONVN_TRAIT (FloatComplexNDArray, FloatNDArray, FloatComplexNDArray); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7661
diff
changeset
|
58 OCTAVE_CONVN_TRAIT (FloatNDArray, FloatComplexNDArray, FloatComplexNDArray); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7661
diff
changeset
|
59 OCTAVE_CONVN_TRAIT (FloatComplexNDArray, FloatComplexNDArray, FloatComplexNDArray); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7661
diff
changeset
|
60 |
7640 | 61 // FIXME -- this function should maybe be available in liboctave? |
7660 | 62 template <class MTa, class MTb> |
7640 | 63 octave_value |
7660 | 64 convn (const MTa& a, const MTb& b) |
7640 | 65 { |
66 octave_value retval; | |
67 | |
68 // Get sizes | |
69 const octave_idx_type ndims = a.ndims (); | |
70 const octave_idx_type b_numel = b.numel (); | |
71 | |
72 const dim_vector a_size = a.dims (); | |
73 const dim_vector b_size = b.dims (); | |
74 | |
75 if (ndims != b.ndims ()) | |
76 { | |
77 error ("__convn__: first and second argument must have same dimensionality"); | |
78 return retval; | |
79 } | |
80 | |
81 // Allocate output | |
82 dim_vector out_size (a_size); | |
83 for (octave_idx_type n = 0; n < ndims; n++) | |
7929 | 84 out_size(n) = std::max (a_size(n) - b_size(n) + 1, |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
85 static_cast<octave_idx_type> (0)); |
7640 | 86 |
7660 | 87 typedef typename octave_convn_traits<MTa, MTb>::TR MTout; |
88 | |
89 MTout out (out_size); | |
7640 | 90 |
91 const octave_idx_type out_numel = out.numel (); | |
92 | |
93 // Iterate over every element of 'out'. | |
94 dim_vector idx_dim (ndims); | |
95 | |
96 Array<octave_idx_type> a_idx (idx_dim); | |
97 Array<octave_idx_type> b_idx (idx_dim); | |
98 Array<octave_idx_type> out_idx (idx_dim, 0); | |
99 | |
100 for (octave_idx_type i = 0; i < out_numel; i++) | |
101 { | |
102 OCTAVE_QUIT; | |
103 | |
104 // For each neighbour | |
7660 | 105 typename MTout::element_type sum = 0; |
7640 | 106 |
107 for (octave_idx_type n = 0; n < ndims; n++) | |
108 b_idx(n) = 0; | |
109 | |
110 for (octave_idx_type j = 0; j < b_numel; j++) | |
111 { | |
112 for (octave_idx_type n = 0; n < ndims; n++) | |
113 a_idx(n) = out_idx(n) + (b_size(n) - 1 - b_idx(n)); | |
114 | |
115 sum += a(a_idx) * b(b_idx); | |
116 | |
117 b.increment_index (b_idx, b_size); | |
118 } | |
119 | |
120 // Compute filter result | |
121 out(out_idx) = sum; | |
122 | |
123 // Prepare for next iteration | |
124 out.increment_index (out_idx, out_size); | |
125 } | |
126 | |
127 return out; | |
128 } | |
129 | |
130 DEFUN_DLD (__convn__, args, , | |
131 "-*- texinfo -*-\n\ | |
132 @deftypefn {Loadable Function} {} __convn__(@var{a}, @var{b})\n\ | |
133 Undocumented internal function.\n\ | |
134 @end deftypefn\n\ | |
135 ") | |
136 { | |
137 octave_value retval; | |
138 | |
139 if (args.length () == 2) | |
140 { | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7661
diff
changeset
|
141 if (args(0).is_single_type() || args(1).is_single_type()) |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
142 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
143 if (args(0).is_real_type ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
144 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
145 if (args(1).is_real_type ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
146 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
147 const FloatNDArray a = args (0).float_array_value (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
148 const FloatNDArray b = args (1).float_array_value (); |
7660 | 149 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
150 if (! error_state) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
151 retval = convn (a, b); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
152 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
153 else if (args(1).is_complex_type ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
154 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
155 const FloatNDArray a = args (0).float_array_value (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
156 const FloatComplexNDArray b = args (1).float_complex_array_value (); |
7640 | 157 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
158 if (! error_state) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
159 retval = convn (a, b); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
160 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
161 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
162 error ("__convn__: invalid call"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
163 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
164 else if (args(0).is_complex_type ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
165 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
166 if (args(1).is_complex_type ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
167 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
168 const FloatComplexNDArray a = args (0).float_complex_array_value (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
169 const FloatComplexNDArray b = args (1).float_complex_array_value (); |
7640 | 170 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
171 if (! error_state) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
172 retval = convn (a, b); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
173 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
174 else if (args(1).is_real_type ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
175 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
176 const FloatComplexNDArray a = args (0).float_complex_array_value (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
177 const FloatNDArray b = args (1).float_array_value (); |
7660 | 178 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
179 if (! error_state) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
180 retval = convn (a, b); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
181 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
182 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
183 error ("__convn__: invalid call"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
184 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
185 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
186 error ("__convn__: invalid call"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
187 } |
7661
f3493c40a0bd
__convn__.cc: fix typo in previous change
John W. Eaton <jwe@octave.org>
parents:
7660
diff
changeset
|
188 else |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
189 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
190 if (args(0).is_real_type ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
191 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
192 if (args(1).is_real_type ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
193 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
194 const NDArray a = args (0).array_value (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
195 const NDArray b = args (1).array_value (); |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7661
diff
changeset
|
196 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
197 if (! error_state) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
198 retval = convn (a, b); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
199 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
200 else if (args(1).is_complex_type ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
201 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
202 const NDArray a = args (0).array_value (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
203 const ComplexNDArray b = args (1).complex_array_value (); |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7661
diff
changeset
|
204 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
205 if (! error_state) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
206 retval = convn (a, b); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
207 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
208 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
209 error ("__convn__: invalid call"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
210 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
211 else if (args(0).is_complex_type ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
212 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
213 if (args(1).is_complex_type ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
214 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
215 const ComplexNDArray a = args (0).complex_array_value (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
216 const ComplexNDArray b = args (1).complex_array_value (); |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7661
diff
changeset
|
217 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
218 if (! error_state) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
219 retval = convn (a, b); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
220 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
221 else if (args(1).is_real_type ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
222 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
223 const ComplexNDArray a = args (0).complex_array_value (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
224 const NDArray b = args (1).array_value (); |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7661
diff
changeset
|
225 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
226 if (! error_state) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
227 retval = convn (a, b); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
228 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
229 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
230 error ("__convn__: invalid call"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
231 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
232 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
233 error ("__convn__: invalid call"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
7929
diff
changeset
|
234 } |
7640 | 235 } |
236 else | |
237 print_usage (); | |
238 | |
239 return retval; | |
240 } |