4514
|
1 // N-D Array manipulations. |
|
2 /* |
|
3 |
|
4 Copyright (C) 1996, 1997 John W. Eaton |
|
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 |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
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 |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
|
25 #pragma implementation |
|
26 #endif |
|
27 |
|
28 #ifdef HAVE_CONFIG_H |
|
29 #include <config.h> |
|
30 #endif |
|
31 |
|
32 #include "CNDArray.h" |
|
33 #include "mx-base.h" |
|
34 #include "lo-ieee.h" |
|
35 |
4532
|
36 #include "ArrayN-inline.h" |
|
37 |
4543
|
38 // XXX FIXME XXX -- could we use a templated mixed-type copy function |
|
39 // here? |
|
40 |
|
41 ComplexNDArray::ComplexNDArray (const NDArray& a) |
|
42 : MArrayN<Complex> (a.dims ()) |
|
43 { |
|
44 for (int i = 0; i < a.length (); i++) |
|
45 elem (i) = a.elem (i); |
|
46 } |
|
47 |
|
48 ComplexNDArray::ComplexNDArray (const boolNDArray& a) |
|
49 : MArrayN<Complex> (a.dims ()) |
|
50 { |
|
51 for (int i = 0; i < a.length (); i++) |
|
52 elem (i) = a.elem (i); |
|
53 } |
|
54 |
|
55 ComplexNDArray::ComplexNDArray (const charNDArray& a) |
|
56 : MArrayN<Complex> (a.dims ()) |
|
57 { |
|
58 for (int i = 0; i < a.length (); i++) |
|
59 elem (i) = a.elem (i); |
|
60 } |
|
61 |
|
62 // unary operations |
|
63 |
|
64 boolNDArray |
|
65 ComplexNDArray::operator ! (void) const |
|
66 { |
|
67 boolNDArray b (dims ()); |
|
68 |
|
69 for (int i = 0; i < length (); i++) |
|
70 b.elem (i) = elem (i) != 0.0; |
|
71 |
|
72 return b; |
|
73 } |
|
74 |
4514
|
75 // XXX FIXME XXX -- this is not quite the right thing. |
|
76 |
4556
|
77 boolNDArray |
4514
|
78 ComplexNDArray::all (int dim) const |
|
79 { |
4569
|
80 MX_ND_ANY_ALL_REDUCTION |
|
81 (MX_ND_ALL_EVAL (elem (iter_idx) == Complex (0, 0)), true); |
4514
|
82 } |
|
83 |
4556
|
84 boolNDArray |
4514
|
85 ComplexNDArray::any (int dim) const |
|
86 { |
4569
|
87 MX_ND_ANY_ALL_REDUCTION |
|
88 (MX_ND_ANY_EVAL (elem (iter_idx) != Complex (0, 0)), false); |
|
89 } |
|
90 |
|
91 ComplexMatrix |
|
92 ComplexNDArray::cumprod (int dim) const |
|
93 { |
|
94 if (dims () . length () == 2) |
|
95 { |
|
96 MX_CUMULATIVE_OP (ComplexMatrix, Complex, *=); |
|
97 } |
|
98 else |
|
99 { |
|
100 (*current_liboctave_error_handler) |
|
101 ("cumsum is not yet implemented for N-d arrays"); |
|
102 |
|
103 return ComplexMatrix (); |
|
104 } |
|
105 } |
|
106 |
|
107 ComplexMatrix |
|
108 ComplexNDArray::cumsum (int dim) const |
|
109 { |
|
110 if (dims () . length () == 2) |
|
111 { |
|
112 MX_CUMULATIVE_OP (ComplexMatrix, Complex, +=); |
|
113 } |
|
114 else |
|
115 { |
|
116 (*current_liboctave_error_handler) |
|
117 ("cumsum is not yet implemented for N-d arrays"); |
|
118 |
|
119 return ComplexMatrix (); |
|
120 } |
|
121 } |
|
122 |
|
123 ComplexNDArray |
|
124 ComplexNDArray::prod (int dim) const |
|
125 { |
|
126 MX_ND_COMPLEX_OP_REDUCTION (*= elem (iter_idx), Complex (1, 0)); |
|
127 } |
|
128 |
|
129 ComplexNDArray |
|
130 ComplexNDArray::sumsq (int dim) const |
|
131 { |
|
132 MX_ND_COMPLEX_OP_REDUCTION |
|
133 (+= imag (elem (iter_idx)) |
|
134 ? elem (iter_idx) * conj (elem (iter_idx)) |
|
135 : std::pow (elem (iter_idx), 2), Complex (0, 0)); |
|
136 } |
|
137 |
|
138 ComplexNDArray |
|
139 ComplexNDArray::sum (int dim) const |
|
140 { |
|
141 MX_ND_COMPLEX_OP_REDUCTION (+= elem (iter_idx), Complex (0, 0)); |
|
142 } |
|
143 |
|
144 Matrix |
|
145 ComplexNDArray::abs (void) const |
|
146 { |
|
147 Matrix retval; |
|
148 |
|
149 if (dims () . length () == 2) |
|
150 { |
|
151 int nr = rows (); |
|
152 int nc = cols (); |
|
153 |
|
154 retval.resize (nr, nc); |
|
155 |
|
156 for (int j = 0; j < nc; j++) |
|
157 for (int i = 0; i < nr; i++) |
|
158 retval (i, j) = ::abs (elem (i, j)); |
|
159 } |
|
160 else |
|
161 (*current_liboctave_error_handler) |
|
162 ("abs is not yet implemented for N-d arrays"); |
|
163 |
|
164 return retval; |
4514
|
165 } |
|
166 |
|
167 ComplexMatrix |
|
168 ComplexNDArray::matrix_value (void) const |
|
169 { |
|
170 ComplexMatrix retval; |
|
171 |
|
172 int nd = ndims (); |
|
173 |
|
174 switch (nd) |
|
175 { |
|
176 case 1: |
|
177 retval = ComplexMatrix (Array2<Complex> (*this, dimensions(0), 1)); |
|
178 break; |
|
179 |
|
180 case 2: |
|
181 retval = ComplexMatrix (Array2<Complex> (*this, dimensions(0), |
|
182 dimensions(1))); |
|
183 break; |
|
184 |
|
185 default: |
|
186 (*current_liboctave_error_handler) |
|
187 ("invalid converstion of ComplexNDArray to ComplexMatrix"); |
|
188 break; |
|
189 } |
|
190 |
|
191 return retval; |
|
192 } |
|
193 |
4532
|
194 void |
|
195 ComplexNDArray::increment_index (Array<int>& ra_idx, |
|
196 const dim_vector& dimensions, |
|
197 int start_dimension) |
|
198 { |
|
199 ::increment_index (ra_idx, dimensions, start_dimension); |
|
200 } |
|
201 |
4556
|
202 int |
|
203 ComplexNDArray::compute_index (Array<int>& ra_idx, |
|
204 const dim_vector& dimensions) |
|
205 { |
|
206 return ::compute_index (ra_idx, dimensions); |
|
207 } |
|
208 |
4543
|
209 NDS_CMP_OPS(ComplexNDArray, real, Complex, real) |
|
210 NDS_BOOL_OPS(ComplexNDArray, Complex, 0.0) |
|
211 |
|
212 SND_CMP_OPS(Complex, real, ComplexNDArray, real) |
|
213 SND_BOOL_OPS(Complex, ComplexNDArray, 0.0) |
|
214 |
|
215 NDND_CMP_OPS(ComplexNDArray, real, ComplexNDArray, real) |
|
216 NDND_BOOL_OPS(ComplexNDArray, ComplexNDArray, 0.0) |
|
217 |
4514
|
218 /* |
|
219 ;;; Local Variables: *** |
|
220 ;;; mode: C++ *** |
|
221 ;;; End: *** |
|
222 */ |