4902
|
1 // N-D Array manipulations. |
|
2 /* |
|
3 |
|
4 Copyright (C) 2004 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 |
5307
|
20 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
21 02110-1301, USA. |
4902
|
22 |
|
23 */ |
|
24 |
|
25 #ifdef HAVE_CONFIG_H |
|
26 #include <config.h> |
|
27 #endif |
|
28 |
|
29 #include "Array-util.h" |
|
30 #include "mx-base.h" |
|
31 #include "lo-ieee.h" |
|
32 |
|
33 // unary operations |
|
34 |
|
35 template <class T> |
|
36 boolNDArray |
|
37 intNDArray<T>::operator ! (void) const |
|
38 { |
4932
|
39 boolNDArray b (this->dims ()); |
4902
|
40 |
5275
|
41 for (octave_idx_type i = 0; i < this->length (); i++) |
4932
|
42 b.elem (i) = ! this->elem (i); |
4902
|
43 |
|
44 return b; |
|
45 } |
|
46 |
5943
|
47 template <class T> |
|
48 bool |
|
49 intNDArray<T>::any_element_not_one_or_zero (void) const |
|
50 { |
|
51 octave_idx_type nel = this->nelem (); |
|
52 |
|
53 for (octave_idx_type i = 0; i < nel; i++) |
|
54 { |
|
55 T val = this->elem (i); |
|
56 |
|
57 if (val != 0.0 && val != 1.0) |
|
58 return true; |
|
59 } |
|
60 |
|
61 return false; |
|
62 } |
|
63 |
5775
|
64 // FIXME -- this is not quite the right thing. |
4902
|
65 |
|
66 template <class T> |
|
67 boolNDArray |
|
68 intNDArray<T>::all (int dim) const |
|
69 { |
4932
|
70 MX_ND_ANY_ALL_REDUCTION (MX_ND_ALL_EVAL (this->elem (iter_idx) == T (0)), true); |
4902
|
71 } |
|
72 |
|
73 template <class T> |
|
74 boolNDArray |
|
75 intNDArray<T>::any (int dim) const |
|
76 { |
4932
|
77 MX_ND_ANY_ALL_REDUCTION (MX_ND_ALL_EVAL (this->elem (iter_idx) == T (0)), false); |
4902
|
78 } |
|
79 |
|
80 template <class T> |
|
81 void |
5275
|
82 intNDArray<T>::increment_index (Array<octave_idx_type>& ra_idx, |
4902
|
83 const dim_vector& dimensions, |
|
84 int start_dimension) |
|
85 { |
|
86 ::increment_index (ra_idx, dimensions, start_dimension); |
|
87 } |
|
88 |
|
89 template <class T> |
5275
|
90 octave_idx_type |
|
91 intNDArray<T>::compute_index (Array<octave_idx_type>& ra_idx, |
4902
|
92 const dim_vector& dimensions) |
|
93 { |
|
94 return ::compute_index (ra_idx, dimensions); |
|
95 } |
|
96 |
4915
|
97 template <class T> |
5073
|
98 intNDArray<T> |
5275
|
99 intNDArray<T>::concat (const intNDArray<T>& rb, const Array<octave_idx_type>& ra_idx) |
5073
|
100 { |
6482
|
101 if (rb.numel () > 0) |
5073
|
102 insert (rb, ra_idx); |
|
103 return *this; |
|
104 } |
|
105 |
|
106 template <class T> |
4915
|
107 intNDArray<T>& |
5275
|
108 intNDArray<T>::insert (const intNDArray<T>& a, octave_idx_type r, octave_idx_type c) |
4915
|
109 { |
|
110 Array<T>::insert (a, r, c); |
|
111 return *this; |
|
112 } |
|
113 |
|
114 template <class T> |
|
115 intNDArray<T>& |
5275
|
116 intNDArray<T>::insert (const intNDArray<T>& a, const Array<octave_idx_type>& ra_idx) |
4915
|
117 { |
|
118 Array<T>::insert (a, ra_idx); |
|
119 return *this; |
|
120 } |
|
121 |
4902
|
122 // This contains no information on the array structure !!! |
|
123 |
|
124 template <class T> |
|
125 std::ostream& |
|
126 operator << (std::ostream& os, const intNDArray<T>& a) |
|
127 { |
5275
|
128 octave_idx_type nel = a.nelem (); |
4902
|
129 |
5275
|
130 for (octave_idx_type i = 0; i < nel; i++) |
4902
|
131 os << " " << a.elem (i) << "\n"; |
|
132 |
|
133 return os; |
|
134 } |
|
135 |
|
136 template <class T> |
|
137 std::istream& |
|
138 operator >> (std::istream& is, intNDArray<T>& a) |
|
139 { |
5275
|
140 octave_idx_type nel = a.nelem (); |
4902
|
141 |
|
142 if (nel < 1 ) |
|
143 is.clear (std::ios::badbit); |
|
144 else |
|
145 { |
|
146 T tmp; |
|
147 |
5275
|
148 for (octave_idx_type i = 0; i < nel; i++) |
4902
|
149 { |
|
150 is >> tmp; |
|
151 |
|
152 if (is) |
|
153 a.elem (i) = tmp; |
|
154 else |
|
155 goto done; |
|
156 } |
|
157 } |
|
158 |
|
159 done: |
|
160 |
|
161 return is; |
|
162 } |
|
163 |
|
164 /* |
|
165 ;;; Local Variables: *** |
|
166 ;;; mode: C++ *** |
|
167 ;;; End: *** |
|
168 */ |