4514
|
1 // N-D Array manipulations. |
|
2 /* |
|
3 |
|
4 Copyright (C) 2003 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 "chNDArray.h" |
|
34 #include "mx-base.h" |
|
35 #include "lo-ieee.h" |
4915
|
36 #include "lo-mappers.h" |
4514
|
37 |
|
38 // XXX FIXME XXX -- this is not quite the right thing. |
|
39 |
4556
|
40 boolNDArray |
4514
|
41 charNDArray::all (int dim) const |
|
42 { |
4569
|
43 MX_ND_ANY_ALL_REDUCTION (MX_ND_ALL_EVAL (elem (iter_idx) == ' '), true); |
4514
|
44 } |
|
45 |
4556
|
46 boolNDArray |
4514
|
47 charNDArray::any (int dim) const |
|
48 { |
4569
|
49 MX_ND_ANY_ALL_REDUCTION (MX_ND_ANY_EVAL (elem (iter_idx) != ' '), false); |
4514
|
50 } |
|
51 |
4915
|
52 charNDArray |
|
53 concat (const charNDArray& ra, const charNDArray& rb, const Array<int>& ra_idx) |
|
54 { |
|
55 charNDArray retval (ra); |
|
56 if (ra.numel () > 0) |
|
57 retval.insert (rb, ra_idx); |
|
58 return retval; |
|
59 } |
|
60 |
|
61 charNDArray |
|
62 concat (const charNDArray& ra, const NDArray& rb, const Array<int>& ra_idx) |
4758
|
63 { |
4915
|
64 charNDArray retval (ra); |
|
65 charNDArray tmp (rb.dims ()); |
|
66 int nel = rb.numel (); |
|
67 |
|
68 if (ra.numel () == 0) |
|
69 return retval; |
|
70 |
|
71 for (int i = 0; i < nel; i++) |
|
72 { |
|
73 double d = rb.elem (i); |
|
74 |
|
75 if (xisnan (d)) |
|
76 { |
|
77 (*current_liboctave_error_handler) |
|
78 ("invalid conversion from NaN to character"); |
|
79 return retval; |
|
80 } |
|
81 else |
|
82 { |
|
83 int ival = NINT (d); |
|
84 |
|
85 if (ival < 0 || ival > UCHAR_MAX) |
|
86 // XXX FIXME XXX -- is there something |
|
87 // better we could do? Should we warn the user? |
|
88 ival = 0; |
|
89 |
|
90 tmp.elem (i) = static_cast<char>(ival); |
|
91 } |
|
92 } |
|
93 |
|
94 retval.insert (tmp, ra_idx); |
|
95 return retval; |
|
96 } |
|
97 |
|
98 charNDArray |
|
99 concat (const NDArray& ra, const charNDArray& rb, const Array<int>& ra_idx) |
|
100 { |
|
101 charNDArray retval (ra.dims ()); |
|
102 int nel = ra.numel (); |
|
103 |
|
104 for (int i = 0; i < nel; i++) |
|
105 { |
|
106 double d = ra.elem (i); |
|
107 |
|
108 if (xisnan (d)) |
|
109 { |
|
110 (*current_liboctave_error_handler) |
|
111 ("invalid conversion from NaN to character"); |
|
112 return retval; |
|
113 } |
|
114 else |
|
115 { |
|
116 int ival = NINT (d); |
|
117 |
|
118 if (ival < 0 || ival > UCHAR_MAX) |
|
119 // XXX FIXME XXX -- is there something |
|
120 // better we could do? Should we warn the user? |
|
121 ival = 0; |
|
122 |
|
123 retval.elem (i) = static_cast<char>(ival); |
|
124 } |
|
125 } |
|
126 |
|
127 retval.insert (rb, ra_idx); |
|
128 return retval; |
|
129 } |
|
130 |
|
131 charNDArray& |
|
132 charNDArray::insert (const charNDArray& a, int r, int c) |
|
133 { |
|
134 Array<char>::insert (a, r, c); |
|
135 return *this; |
|
136 } |
|
137 |
|
138 charNDArray& |
|
139 charNDArray::insert (const charNDArray& a, const Array<int>& ra_idx) |
|
140 { |
|
141 Array<char>::insert (a, ra_idx); |
|
142 return *this; |
4758
|
143 } |
|
144 |
4514
|
145 charMatrix |
|
146 charNDArray::matrix_value (void) const |
|
147 { |
|
148 charMatrix retval; |
|
149 |
|
150 int nd = ndims (); |
|
151 |
|
152 switch (nd) |
|
153 { |
|
154 case 1: |
|
155 retval = charMatrix (Array2<char> (*this, dimensions(0), 1)); |
|
156 break; |
|
157 |
|
158 case 2: |
|
159 retval = charMatrix (Array2<char> (*this, dimensions(0), |
|
160 dimensions(1))); |
|
161 break; |
|
162 |
|
163 default: |
|
164 (*current_liboctave_error_handler) |
4770
|
165 ("invalid conversion of charNDArray to charMatrix"); |
4514
|
166 break; |
|
167 } |
|
168 |
|
169 return retval; |
|
170 } |
|
171 |
4532
|
172 void |
|
173 charNDArray::increment_index (Array<int>& ra_idx, |
|
174 const dim_vector& dimensions, |
|
175 int start_dimension) |
|
176 { |
|
177 ::increment_index (ra_idx, dimensions, start_dimension); |
|
178 } |
|
179 |
4556
|
180 int |
|
181 charNDArray::compute_index (Array<int>& ra_idx, |
|
182 const dim_vector& dimensions) |
|
183 { |
|
184 return ::compute_index (ra_idx, dimensions); |
|
185 } |
|
186 |
4514
|
187 /* |
|
188 ;;; Local Variables: *** |
|
189 ;;; mode: C++ *** |
|
190 ;;; End: *** |
|
191 */ |