You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: lib/node_modules/@stdlib/array/convert/README.md
+4-17
Original file line number
Diff line number
Diff line change
@@ -20,7 +20,7 @@ limitations under the License.
20
20
21
21
# convert
22
22
23
-
> Convert an array to an array of a different data type.
23
+
> Convert an array to an array of a different [data type][@stdlib/array/dtypes].
24
24
25
25
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26
26
@@ -42,29 +42,14 @@ var convert = require( '@stdlib/array/convert' );
42
42
43
43
#### convert( arr, dtype )
44
44
45
-
Converts an array to an array of a different data type.
45
+
Converts an array to an array of a different [data type][@stdlib/array/dtypes].
@@ -70,6 +72,25 @@ function isComplex128( dtype ) {
70
72
return(dtype==='complex128');
71
73
}
72
74
75
+
/**
76
+
* Tests whether a data type is a boolean data type.
77
+
*
78
+
* @private
79
+
* @param {string} dtype - data type
80
+
* @returns {boolean} boolean indicating whether a provided data type is a boolean data type
81
+
*
82
+
* @example
83
+
* var bool = isBool( 'bool' );
84
+
* // returns true
85
+
*
86
+
* @example
87
+
* var bool = isBool( 'complex128' );
88
+
* // returns false
89
+
*/
90
+
functionisBool(dtype){
91
+
return(dtype==='bool');
92
+
}
93
+
73
94
74
95
// MAIN //
75
96
@@ -93,9 +114,11 @@ function convert( x, dtype ) {
93
114
varctor;
94
115
varxbuf;
95
116
varobuf;
117
+
varget;
96
118
varout;
97
119
varlen;
98
120
vart;
121
+
vari;
99
122
100
123
if(!isCollection(x)){
101
124
thrownewTypeError(format('invalid argument. First argument must be an array-like object. Value: `%s`.',x));
@@ -136,10 +159,47 @@ function convert( x, dtype ) {
136
159
gcopy(len*2,xbuf,1,obuf,1);
137
160
returnout;
138
161
}
162
+
// Check whether the output data type is a boolean data type...
163
+
if(isBool(dtype)){// cmplx => bool
164
+
obuf=reinterpretBoolean(out,0);
165
+
for(i=0;i<len;i++){
166
+
// A complex number is only falsy when both the real and imaginary components are zero...
167
+
if(xbuf[2*i]||xbuf[(2*i)+1]){
168
+
obuf[i]=1;
169
+
}else{
170
+
obuf[i]=0;
171
+
}
172
+
}
173
+
returnout;
174
+
}
139
175
// We assume that the output data type is a real number data type, given that we're looking to convert a provided complex number array; in which case, we'll only extract the real components from the complex number input array...
140
176
gcopy(len,xbuf,2,out,1);// cmplx => real
141
177
returnout;
142
178
}
179
+
// Check whether the input array is a boolean array...
180
+
if(isBool(t)){
181
+
xbuf=reinterpretBoolean(x,0);
182
+
183
+
// Check whether the output data type is a boolean data type...
184
+
if(isBool(dtype)){// bool => bool
185
+
obuf=reinterpretBoolean(out,0);
186
+
gcopy(len,xbuf,1,obuf,1);
187
+
returnout;
188
+
}
189
+
// Check whether the output data type is a complex number data type...
190
+
if(isComplex64(dtype)){// bool => cmplx
191
+
obuf=reinterpret64(out,0);
192
+
gcopy(len,xbuf,1,obuf,2);
193
+
returnout;
194
+
}
195
+
if(isComplex128(dtype)){// bool => cmplx
196
+
obuf=reinterpret128(out,0);
197
+
gcopy(len,xbuf,1,obuf,2);
198
+
returnout;
199
+
}
200
+
gcopy(len,xbuf,1,out,1);// bool => real
201
+
returnout;
202
+
}
143
203
// Check whether we need to explicitly handle complex number output arrays...
144
204
isc64=isComplex64(dtype);
145
205
if(isc64||isComplex128(dtype)){
@@ -152,7 +212,20 @@ function convert( x, dtype ) {
152
212
gcopy(len,x,1,obuf,2);// real => cmplx
153
213
returnout;
154
214
}
155
-
// At this point, we're no longer handling complex number arrays, so we'll just assume that we can perform a straightforward copy...
215
+
// Check whether the output data type is a boolean data type...
216
+
if(isBool(dtype)){
217
+
obuf=reinterpretBoolean(out,0);
218
+
get=resolveGetter(x);
219
+
for(i=0;i<len;i++){
220
+
if(get(x,i)){
221
+
obuf[i]=1;
222
+
}else{
223
+
obuf[i]=0;
224
+
}
225
+
}
226
+
returnout;
227
+
}
228
+
// At this point, we're no longer handling complex number or boolean arrays, so we'll just assume that we can perform a straightforward copy...
156
229
gcopy(len,x,1,out,1);// note: `gcopy` is assumed to support arrays using accessors
0 commit comments