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 |
4584
|
91 ComplexNDArray |
4569
|
92 ComplexNDArray::cumprod (int dim) const |
|
93 { |
4584
|
94 MX_ND_CUMULATIVE_OP (ComplexNDArray, Complex, Complex (1, 0), *); |
4569
|
95 } |
|
96 |
4584
|
97 ComplexNDArray |
4569
|
98 ComplexNDArray::cumsum (int dim) const |
|
99 { |
4584
|
100 MX_ND_CUMULATIVE_OP (ComplexNDArray, Complex, Complex (0, 0), +); |
4569
|
101 } |
|
102 |
|
103 ComplexNDArray |
|
104 ComplexNDArray::prod (int dim) const |
|
105 { |
|
106 MX_ND_COMPLEX_OP_REDUCTION (*= elem (iter_idx), Complex (1, 0)); |
|
107 } |
|
108 |
|
109 ComplexNDArray |
|
110 ComplexNDArray::sumsq (int dim) const |
|
111 { |
|
112 MX_ND_COMPLEX_OP_REDUCTION |
|
113 (+= imag (elem (iter_idx)) |
|
114 ? elem (iter_idx) * conj (elem (iter_idx)) |
|
115 : std::pow (elem (iter_idx), 2), Complex (0, 0)); |
|
116 } |
|
117 |
|
118 ComplexNDArray |
|
119 ComplexNDArray::sum (int dim) const |
|
120 { |
|
121 MX_ND_COMPLEX_OP_REDUCTION (+= elem (iter_idx), Complex (0, 0)); |
|
122 } |
|
123 |
|
124 Matrix |
|
125 ComplexNDArray::abs (void) const |
|
126 { |
|
127 Matrix retval; |
|
128 |
|
129 if (dims () . length () == 2) |
|
130 { |
|
131 int nr = rows (); |
|
132 int nc = cols (); |
|
133 |
|
134 retval.resize (nr, nc); |
|
135 |
|
136 for (int j = 0; j < nc; j++) |
|
137 for (int i = 0; i < nr; i++) |
|
138 retval (i, j) = ::abs (elem (i, j)); |
|
139 } |
|
140 else |
|
141 (*current_liboctave_error_handler) |
|
142 ("abs is not yet implemented for N-d arrays"); |
|
143 |
|
144 return retval; |
4514
|
145 } |
|
146 |
|
147 ComplexMatrix |
|
148 ComplexNDArray::matrix_value (void) const |
|
149 { |
|
150 ComplexMatrix retval; |
|
151 |
|
152 int nd = ndims (); |
|
153 |
|
154 switch (nd) |
|
155 { |
|
156 case 1: |
|
157 retval = ComplexMatrix (Array2<Complex> (*this, dimensions(0), 1)); |
|
158 break; |
|
159 |
|
160 case 2: |
|
161 retval = ComplexMatrix (Array2<Complex> (*this, dimensions(0), |
|
162 dimensions(1))); |
|
163 break; |
|
164 |
|
165 default: |
|
166 (*current_liboctave_error_handler) |
|
167 ("invalid converstion of ComplexNDArray to ComplexMatrix"); |
|
168 break; |
|
169 } |
|
170 |
|
171 return retval; |
|
172 } |
|
173 |
4532
|
174 void |
|
175 ComplexNDArray::increment_index (Array<int>& ra_idx, |
|
176 const dim_vector& dimensions, |
|
177 int start_dimension) |
|
178 { |
|
179 ::increment_index (ra_idx, dimensions, start_dimension); |
|
180 } |
|
181 |
4556
|
182 int |
|
183 ComplexNDArray::compute_index (Array<int>& ra_idx, |
|
184 const dim_vector& dimensions) |
|
185 { |
|
186 return ::compute_index (ra_idx, dimensions); |
|
187 } |
|
188 |
4543
|
189 NDS_CMP_OPS(ComplexNDArray, real, Complex, real) |
|
190 NDS_BOOL_OPS(ComplexNDArray, Complex, 0.0) |
|
191 |
|
192 SND_CMP_OPS(Complex, real, ComplexNDArray, real) |
|
193 SND_BOOL_OPS(Complex, ComplexNDArray, 0.0) |
|
194 |
|
195 NDND_CMP_OPS(ComplexNDArray, real, ComplexNDArray, real) |
|
196 NDND_BOOL_OPS(ComplexNDArray, ComplexNDArray, 0.0) |
|
197 |
4514
|
198 /* |
|
199 ;;; Local Variables: *** |
|
200 ;;; mode: C++ *** |
|
201 ;;; End: *** |
|
202 */ |