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 |
4556
|
68 boolNDArray |
4513
|
69 NDArray::all (int dim) const |
|
70 { |
4556
|
71 MX_ND_ALL_ANY (MX_ND_ALL_EVAL (MX_ND_ALL_EXPR)); |
4513
|
72 } |
|
73 |
4556
|
74 boolNDArray |
4513
|
75 NDArray::any (int dim) const |
|
76 { |
4556
|
77 MX_ND_ALL_ANY (MX_ND_ANY_EVAL (MX_ND_ANY_EXPR)); |
4513
|
78 } |
|
79 |
4532
|
80 Matrix |
|
81 NDArray::matrix_value (void) const |
|
82 { |
|
83 Matrix retval; |
|
84 |
|
85 int nd = ndims (); |
|
86 |
|
87 switch (nd) |
|
88 { |
|
89 case 1: |
|
90 retval = Matrix (Array2<double> (*this, dimensions(0), 1)); |
|
91 break; |
|
92 |
|
93 case 2: |
|
94 retval = Matrix (Array2<double> (*this, dimensions(0), dimensions(1))); |
|
95 break; |
|
96 |
|
97 default: |
|
98 (*current_liboctave_error_handler) |
|
99 ("invalid converstion of NDArray to Matrix"); |
|
100 break; |
|
101 } |
|
102 |
|
103 return retval; |
|
104 } |
|
105 |
|
106 void |
|
107 NDArray::increment_index (Array<int>& ra_idx, |
|
108 const dim_vector& dimensions, |
|
109 int start_dimension) |
|
110 { |
|
111 ::increment_index (ra_idx, dimensions, start_dimension); |
|
112 } |
|
113 |
4556
|
114 int |
|
115 NDArray::compute_index (Array<int>& ra_idx, |
|
116 const dim_vector& dimensions) |
|
117 { |
|
118 return ::compute_index (ra_idx, dimensions); |
|
119 } |
|
120 |
4511
|
121 bool |
|
122 NDArray::any_element_is_negative (bool neg_zero) const |
|
123 { |
|
124 int n = length (); |
|
125 if (neg_zero) |
|
126 { |
|
127 for (int i = 0; i < n; i++) |
|
128 if (lo_ieee_signbit (Array<double>::elem (i))) |
|
129 return true; |
|
130 } |
|
131 else |
|
132 { |
|
133 for (int i = 0; i < n; i++) |
|
134 if (Array<double>::elem (i) < 0) |
|
135 return true; |
|
136 } |
|
137 |
|
138 return false; |
|
139 } |
|
140 |
|
141 bool |
|
142 NDArray::all_integers (double& max_val, double& min_val) const |
|
143 { |
|
144 int n = length (); |
|
145 |
|
146 if (n > 0) |
|
147 { |
|
148 max_val = Array<double>::elem (0); |
|
149 min_val = Array<double>::elem (0); |
|
150 } |
|
151 else |
|
152 return false; |
|
153 |
|
154 for (int i = 0; i < n; i++) |
|
155 { |
|
156 double val = Array<double>::elem (0); |
|
157 |
|
158 if (val > max_val) |
|
159 max_val = val; |
|
160 |
|
161 if (val < min_val) |
|
162 min_val = val; |
|
163 |
|
164 if (D_NINT (val) != val) |
|
165 return false; |
|
166 } |
|
167 |
|
168 return true; |
|
169 } |
4513
|
170 |
4543
|
171 NDS_CMP_OPS(NDArray, , double, ) |
|
172 NDS_BOOL_OPS(NDArray, double, 0.0) |
|
173 |
|
174 SND_CMP_OPS(double, , NDArray, ) |
|
175 SND_BOOL_OPS(double, NDArray, 0.0) |
|
176 |
|
177 NDND_CMP_OPS(NDArray, , NDArray, ) |
|
178 NDND_BOOL_OPS(NDArray, NDArray, 0.0) |
|
179 |
4513
|
180 /* |
|
181 ;;; Local Variables: *** |
|
182 ;;; mode: C++ *** |
|
183 ;;; End: *** |
|
184 */ |