Mercurial > octave-nkf
annotate src/DLD-FUNCTIONS/__convn__.cc @ 7648:e7b999840056
Added tests to scripts/polynomial/convn.m and allow '__convn__' to actually get N-dimensional complex data.
author | sh@sh-laptop |
---|---|
date | Wed, 26 Mar 2008 15:55:20 -0400 |
parents | 3398ce778b4b |
children | 5f6e11567f70 |
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 | |
34 // FIXME -- this function should maybe be available in liboctave? | |
35 template <class MT, class ST> | |
36 octave_value | |
37 convn (const MT& a, const MT& b) | |
38 { | |
39 octave_value retval; | |
40 | |
41 // Get sizes | |
42 const octave_idx_type ndims = a.ndims (); | |
43 const octave_idx_type b_numel = b.numel (); | |
44 | |
45 const dim_vector a_size = a.dims (); | |
46 const dim_vector b_size = b.dims (); | |
47 | |
48 if (ndims != b.ndims ()) | |
49 { | |
50 error ("__convn__: first and second argument must have same dimensionality"); | |
51 return retval; | |
52 } | |
53 | |
54 // Allocate output | |
55 dim_vector out_size (a_size); | |
56 for (octave_idx_type n = 0; n < ndims; n++) | |
57 out_size(n) = std::max (a_size(n) - b_size(n) + 1, 0); | |
58 | |
59 MT out = MT (out_size); | |
60 | |
61 const octave_idx_type out_numel = out.numel (); | |
62 | |
63 // Iterate over every element of 'out'. | |
64 dim_vector idx_dim (ndims); | |
65 | |
66 Array<octave_idx_type> a_idx (idx_dim); | |
67 Array<octave_idx_type> b_idx (idx_dim); | |
68 Array<octave_idx_type> out_idx (idx_dim, 0); | |
69 | |
70 for (octave_idx_type i = 0; i < out_numel; i++) | |
71 { | |
72 OCTAVE_QUIT; | |
73 | |
74 // For each neighbour | |
75 ST sum = 0; | |
76 | |
77 for (octave_idx_type n = 0; n < ndims; n++) | |
78 b_idx(n) = 0; | |
79 | |
80 for (octave_idx_type j = 0; j < b_numel; j++) | |
81 { | |
82 for (octave_idx_type n = 0; n < ndims; n++) | |
83 a_idx(n) = out_idx(n) + (b_size(n) - 1 - b_idx(n)); | |
84 | |
85 sum += a(a_idx) * b(b_idx); | |
86 | |
87 b.increment_index (b_idx, b_size); | |
88 } | |
89 | |
90 // Compute filter result | |
91 out(out_idx) = sum; | |
92 | |
93 // Prepare for next iteration | |
94 out.increment_index (out_idx, out_size); | |
95 } | |
96 | |
97 return out; | |
98 } | |
99 | |
100 DEFUN_DLD (__convn__, args, , | |
101 "-*- texinfo -*-\n\ | |
102 @deftypefn {Loadable Function} {} __convn__(@var{a}, @var{b})\n\ | |
103 Undocumented internal function.\n\ | |
104 @end deftypefn\n\ | |
105 ") | |
106 { | |
107 octave_value retval; | |
108 | |
109 if (args.length () == 2) | |
110 { | |
111 if (args(0).is_real_type () && args(1).is_real_type ()) | |
112 { | |
113 const NDArray a = args (0).array_value (); | |
114 const NDArray b = args (1).array_value (); | |
115 | |
116 if (! error_state) | |
117 retval = convn<NDArray, double> (a, b); | |
118 } | |
119 else if (args(0).is_complex_type () && args(1).is_complex_type ()) | |
120 { | |
7648
e7b999840056
Added tests to scripts/polynomial/convn.m and allow '__convn__' to actually get N-dimensional complex data.
sh@sh-laptop
parents:
7640
diff
changeset
|
121 const ComplexNDArray a = args (0).complex_array_value (); |
e7b999840056
Added tests to scripts/polynomial/convn.m and allow '__convn__' to actually get N-dimensional complex data.
sh@sh-laptop
parents:
7640
diff
changeset
|
122 const ComplexNDArray b = args (1).complex_array_value (); |
7640 | 123 |
124 if (! error_state) | |
125 retval = convn<ComplexNDArray, Complex> (a, b); | |
126 } | |
127 else | |
128 error ("__convn__: first and second input should be real, or complex arrays"); | |
129 } | |
130 else | |
131 print_usage (); | |
132 | |
133 return retval; | |
134 } |