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 |
4588
|
32 #include "Array-util.h" |
4514
|
33 #include "CNDArray.h" |
|
34 #include "mx-base.h" |
|
35 #include "lo-ieee.h" |
|
36 |
4543
|
37 // XXX FIXME XXX -- could we use a templated mixed-type copy function |
|
38 // here? |
|
39 |
|
40 ComplexNDArray::ComplexNDArray (const NDArray& a) |
|
41 : MArrayN<Complex> (a.dims ()) |
|
42 { |
|
43 for (int i = 0; i < a.length (); i++) |
|
44 elem (i) = a.elem (i); |
|
45 } |
|
46 |
|
47 ComplexNDArray::ComplexNDArray (const boolNDArray& a) |
|
48 : MArrayN<Complex> (a.dims ()) |
|
49 { |
|
50 for (int i = 0; i < a.length (); i++) |
|
51 elem (i) = a.elem (i); |
|
52 } |
|
53 |
|
54 ComplexNDArray::ComplexNDArray (const charNDArray& a) |
|
55 : MArrayN<Complex> (a.dims ()) |
|
56 { |
|
57 for (int i = 0; i < a.length (); i++) |
|
58 elem (i) = a.elem (i); |
|
59 } |
|
60 |
|
61 // unary operations |
|
62 |
|
63 boolNDArray |
|
64 ComplexNDArray::operator ! (void) const |
|
65 { |
|
66 boolNDArray b (dims ()); |
|
67 |
|
68 for (int i = 0; i < length (); i++) |
|
69 b.elem (i) = elem (i) != 0.0; |
|
70 |
|
71 return b; |
|
72 } |
|
73 |
4514
|
74 // XXX FIXME XXX -- this is not quite the right thing. |
|
75 |
4556
|
76 boolNDArray |
4514
|
77 ComplexNDArray::all (int dim) const |
|
78 { |
4569
|
79 MX_ND_ANY_ALL_REDUCTION |
|
80 (MX_ND_ALL_EVAL (elem (iter_idx) == Complex (0, 0)), true); |
4514
|
81 } |
|
82 |
4556
|
83 boolNDArray |
4514
|
84 ComplexNDArray::any (int dim) const |
|
85 { |
4569
|
86 MX_ND_ANY_ALL_REDUCTION |
|
87 (MX_ND_ANY_EVAL (elem (iter_idx) != Complex (0, 0)), false); |
|
88 } |
|
89 |
4584
|
90 ComplexNDArray |
4569
|
91 ComplexNDArray::cumprod (int dim) const |
|
92 { |
4584
|
93 MX_ND_CUMULATIVE_OP (ComplexNDArray, Complex, Complex (1, 0), *); |
4569
|
94 } |
|
95 |
4584
|
96 ComplexNDArray |
4569
|
97 ComplexNDArray::cumsum (int dim) const |
|
98 { |
4584
|
99 MX_ND_CUMULATIVE_OP (ComplexNDArray, Complex, Complex (0, 0), +); |
4569
|
100 } |
|
101 |
|
102 ComplexNDArray |
|
103 ComplexNDArray::prod (int dim) const |
|
104 { |
|
105 MX_ND_COMPLEX_OP_REDUCTION (*= elem (iter_idx), Complex (1, 0)); |
|
106 } |
|
107 |
|
108 ComplexNDArray |
|
109 ComplexNDArray::sumsq (int dim) const |
|
110 { |
|
111 MX_ND_COMPLEX_OP_REDUCTION |
|
112 (+= imag (elem (iter_idx)) |
|
113 ? elem (iter_idx) * conj (elem (iter_idx)) |
|
114 : std::pow (elem (iter_idx), 2), Complex (0, 0)); |
|
115 } |
|
116 |
|
117 ComplexNDArray |
|
118 ComplexNDArray::sum (int dim) const |
|
119 { |
|
120 MX_ND_COMPLEX_OP_REDUCTION (+= elem (iter_idx), Complex (0, 0)); |
|
121 } |
|
122 |
|
123 Matrix |
|
124 ComplexNDArray::abs (void) const |
|
125 { |
|
126 Matrix retval; |
|
127 |
|
128 if (dims () . length () == 2) |
|
129 { |
|
130 int nr = rows (); |
|
131 int nc = cols (); |
|
132 |
|
133 retval.resize (nr, nc); |
|
134 |
|
135 for (int j = 0; j < nc; j++) |
|
136 for (int i = 0; i < nr; i++) |
|
137 retval (i, j) = ::abs (elem (i, j)); |
|
138 } |
|
139 else |
|
140 (*current_liboctave_error_handler) |
|
141 ("abs is not yet implemented for N-d arrays"); |
|
142 |
|
143 return retval; |
4514
|
144 } |
|
145 |
|
146 ComplexMatrix |
|
147 ComplexNDArray::matrix_value (void) const |
|
148 { |
|
149 ComplexMatrix retval; |
|
150 |
|
151 int nd = ndims (); |
|
152 |
|
153 switch (nd) |
|
154 { |
|
155 case 1: |
|
156 retval = ComplexMatrix (Array2<Complex> (*this, dimensions(0), 1)); |
|
157 break; |
|
158 |
|
159 case 2: |
|
160 retval = ComplexMatrix (Array2<Complex> (*this, dimensions(0), |
|
161 dimensions(1))); |
|
162 break; |
|
163 |
|
164 default: |
|
165 (*current_liboctave_error_handler) |
|
166 ("invalid converstion of ComplexNDArray to ComplexMatrix"); |
|
167 break; |
|
168 } |
|
169 |
|
170 return retval; |
|
171 } |
|
172 |
4532
|
173 void |
|
174 ComplexNDArray::increment_index (Array<int>& ra_idx, |
|
175 const dim_vector& dimensions, |
|
176 int start_dimension) |
|
177 { |
|
178 ::increment_index (ra_idx, dimensions, start_dimension); |
|
179 } |
|
180 |
4556
|
181 int |
|
182 ComplexNDArray::compute_index (Array<int>& ra_idx, |
|
183 const dim_vector& dimensions) |
|
184 { |
|
185 return ::compute_index (ra_idx, dimensions); |
|
186 } |
|
187 |
4543
|
188 NDS_CMP_OPS(ComplexNDArray, real, Complex, real) |
|
189 NDS_BOOL_OPS(ComplexNDArray, Complex, 0.0) |
|
190 |
|
191 SND_CMP_OPS(Complex, real, ComplexNDArray, real) |
|
192 SND_BOOL_OPS(Complex, ComplexNDArray, 0.0) |
|
193 |
|
194 NDND_CMP_OPS(ComplexNDArray, real, ComplexNDArray, real) |
|
195 NDND_BOOL_OPS(ComplexNDArray, ComplexNDArray, 0.0) |
|
196 |
4514
|
197 /* |
|
198 ;;; Local Variables: *** |
|
199 ;;; mode: C++ *** |
|
200 ;;; End: *** |
|
201 */ |