4513
|
1 // N-D Array manipulations. |
4511
|
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 |
4513
|
32 #include "dNDArray.h" |
4511
|
33 #include "mx-base.h" |
4513
|
34 #include "lo-error.h" |
4511
|
35 #include "lo-ieee.h" |
|
36 |
4532
|
37 #include "ArrayN-inline.h" |
|
38 |
4543
|
39 NDArray::NDArray (const boolNDArray& a) |
|
40 : MArrayN<double> (a.dims ()) |
|
41 { |
|
42 for (int i = 0; i < a.length (); i++) |
|
43 elem (i) = a.elem (i); |
|
44 } |
|
45 |
|
46 NDArray::NDArray (const charNDArray& a) |
|
47 : MArrayN<double> (a.dims ()) |
|
48 { |
|
49 for (int i = 0; i < a.length (); i++) |
|
50 elem (i) = a.elem (i); |
|
51 } |
|
52 |
|
53 // unary operations |
|
54 |
|
55 boolNDArray |
|
56 NDArray::operator ! (void) const |
|
57 { |
|
58 boolNDArray b (dims ()); |
|
59 |
|
60 for (int i = 0; i < length (); i++) |
|
61 b.elem (i) = ! elem (i); |
|
62 |
|
63 return b; |
|
64 } |
|
65 |
4513
|
66 // XXX FIXME XXX -- this is not quite the right thing. |
|
67 |
|
68 boolMatrix |
|
69 NDArray::all (int dim) const |
|
70 { |
|
71 boolMatrix retval; |
|
72 |
|
73 if (dimensions.length () == 2) |
|
74 { |
|
75 Matrix tmp = matrix_value (); |
|
76 retval = tmp.all (dim); |
|
77 } |
|
78 else |
|
79 (*current_liboctave_error_handler) |
|
80 ("all is not yet implemented for N-d Arrays"); |
|
81 |
|
82 return retval; |
|
83 } |
|
84 |
|
85 boolMatrix |
|
86 NDArray::any (int dim) const |
|
87 { |
|
88 boolMatrix retval; |
|
89 |
|
90 if (dimensions.length () == 2) |
|
91 { |
|
92 Matrix tmp = matrix_value (); |
|
93 retval = tmp.any (dim); |
|
94 } |
|
95 else |
|
96 (*current_liboctave_error_handler) |
|
97 ("any is not yet implemented for N-d Arrays"); |
|
98 |
|
99 return retval; |
|
100 } |
|
101 |
4532
|
102 Matrix |
|
103 NDArray::matrix_value (void) const |
|
104 { |
|
105 Matrix retval; |
|
106 |
|
107 int nd = ndims (); |
|
108 |
|
109 switch (nd) |
|
110 { |
|
111 case 1: |
|
112 retval = Matrix (Array2<double> (*this, dimensions(0), 1)); |
|
113 break; |
|
114 |
|
115 case 2: |
|
116 retval = Matrix (Array2<double> (*this, dimensions(0), dimensions(1))); |
|
117 break; |
|
118 |
|
119 default: |
|
120 (*current_liboctave_error_handler) |
|
121 ("invalid converstion of NDArray to Matrix"); |
|
122 break; |
|
123 } |
|
124 |
|
125 return retval; |
|
126 } |
|
127 |
|
128 void |
|
129 NDArray::increment_index (Array<int>& ra_idx, |
|
130 const dim_vector& dimensions, |
|
131 int start_dimension) |
|
132 { |
|
133 ::increment_index (ra_idx, dimensions, start_dimension); |
|
134 } |
|
135 |
4511
|
136 bool |
|
137 NDArray::any_element_is_negative (bool neg_zero) const |
|
138 { |
|
139 int n = length (); |
|
140 if (neg_zero) |
|
141 { |
|
142 for (int i = 0; i < n; i++) |
|
143 if (lo_ieee_signbit (Array<double>::elem (i))) |
|
144 return true; |
|
145 } |
|
146 else |
|
147 { |
|
148 for (int i = 0; i < n; i++) |
|
149 if (Array<double>::elem (i) < 0) |
|
150 return true; |
|
151 } |
|
152 |
|
153 return false; |
|
154 } |
|
155 |
|
156 bool |
|
157 NDArray::all_integers (double& max_val, double& min_val) const |
|
158 { |
|
159 int n = length (); |
|
160 |
|
161 if (n > 0) |
|
162 { |
|
163 max_val = Array<double>::elem (0); |
|
164 min_val = Array<double>::elem (0); |
|
165 } |
|
166 else |
|
167 return false; |
|
168 |
|
169 for (int i = 0; i < n; i++) |
|
170 { |
|
171 double val = Array<double>::elem (0); |
|
172 |
|
173 if (val > max_val) |
|
174 max_val = val; |
|
175 |
|
176 if (val < min_val) |
|
177 min_val = val; |
|
178 |
|
179 if (D_NINT (val) != val) |
|
180 return false; |
|
181 } |
|
182 |
|
183 return true; |
|
184 } |
4513
|
185 |
4543
|
186 NDS_CMP_OPS(NDArray, , double, ) |
|
187 NDS_BOOL_OPS(NDArray, double, 0.0) |
|
188 |
|
189 SND_CMP_OPS(double, , NDArray, ) |
|
190 SND_BOOL_OPS(double, NDArray, 0.0) |
|
191 |
|
192 NDND_CMP_OPS(NDArray, , NDArray, ) |
|
193 NDND_BOOL_OPS(NDArray, NDArray, 0.0) |
|
194 |
4513
|
195 /* |
|
196 ;;; Local Variables: *** |
|
197 ;;; mode: C++ *** |
|
198 ;;; End: *** |
|
199 */ |