Skip to content

Commit 267e16e

Browse files
committed
fix: add support for complex numbers and provided ndarrays having arbitrary rank
1 parent e5e8db8 commit 267e16e

1 file changed

Lines changed: 48 additions & 9 deletions

File tree

lib/node_modules/@stdlib/ndarray/array/lib/copy_view.js

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,18 @@
2020

2121
// MODULES //
2222

23+
var arraylike2object = require( '@stdlib/array/base/arraylike2object' );
24+
var castReturn = require( '@stdlib/complex/base/cast-return' );
25+
var complexCtors = require( '@stdlib/complex/ctors' );
2326
var bufferCtors = require( '@stdlib/ndarray/base/buffer-ctors' );
2427
var allocUnsafe = require( '@stdlib/buffer/alloc-unsafe' );
28+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
29+
var getDType = require( '@stdlib/ndarray/dtype' );
30+
var getShape = require( '@stdlib/ndarray/shape' );
31+
var getStrides = require( '@stdlib/ndarray/strides' );
32+
var getOffset = require( '@stdlib/ndarray/offset' );
33+
var getOrder = require( '@stdlib/ndarray/order' );
34+
var getData = require( '@stdlib/ndarray/data-buffer' );
2535

2636

2737
// FUNCTIONS //
@@ -41,7 +51,7 @@ function generic( arr ) {
4151
len = arr.length;
4252
out = [];
4353
for ( i = 0; i < len; i++ ) {
44-
out.push( arr.get( i ) ); // FIXME: what if `arr` has more than one dimensions?
54+
out.push( arr.iget( i ) ); // as output buffer is generic, should work with both real- and complex-valued ndarrays
4555
}
4656
return out;
4757
}
@@ -61,7 +71,7 @@ function binary( arr ) {
6171
len = arr.length;
6272
out = allocUnsafe( len );
6373
for ( i = 0; i < len; i++ ) {
64-
out[ i ] = arr.get( i ); // FIXME: what if `arr` has more than one dimensions?
74+
out[ i ] = arr.iget( i ); // we're assuming that we're doing something sensible here (e.g., not trying to cast a complex-valued ndarray to a "binary" ndarray or a double-precision floating-point ndarray to binary, etc)
6575
}
6676
return out;
6777
}
@@ -78,15 +88,40 @@ function typed( arr, dtype ) {
7888
var ctor;
7989
var len;
8090
var out;
91+
var set;
92+
var fcn;
93+
var o;
8194
var i;
8295

8396
ctor = bufferCtors( dtype );
8497
len = arr.length;
85-
out = new ctor( len ); // FIXME: need to account for complex number arrays; in which case, we may want to do something similar to `array/convert`
86-
for ( i = 0; i < len; i++ ) {
87-
out[ i ] = arr.get( i ); // FIXME: what if `arr` has more than one dimensions?
98+
out = new ctor( len );
99+
100+
// If the output data buffer is a complex number array, we need to use accessors...
101+
o = arraylike2object( out );
102+
if ( o.accessorProtocol ) {
103+
set = o.accessors[ 1 ];
104+
fcn = castReturn( wrapper, 1, complexCtors( dtype ) );
105+
for ( i = 0; i < len; i++ ) {
106+
set( out, i, fcn( i ) ); // we're assuming that we're doing something sensible here (e.g., not trying to cast arbitrary objects to complex numbers, etc)
107+
}
108+
} else {
109+
for ( i = 0; i < len; i++ ) {
110+
out[ i ] = arr.iget( i ); // we're assuming that we're doing something sensible here (e.g., not trying to cast an ndarray containing generic objects to a double-precision floating-point array or a complex-valued ndarray to a real-valued ndarray, etc)
111+
}
88112
}
89113
return out;
114+
115+
/**
116+
* Returns the ndarray element specified by a provided linear index.
117+
*
118+
* @private
119+
* @param {NonNegativeInteger} i - linear index
120+
* @returns {*} value
121+
*/
122+
function wrapper( i ) {
123+
return arr.iget( i );
124+
}
90125
}
91126

92127

@@ -112,14 +147,18 @@ function typed( arr, dtype ) {
112147
* // returns <Float64Array>[ 3.0, 2.0, 1.0 ]
113148
*/
114149
function copyView( arr, dtype ) {
115-
// TODO: handle complex number dtypes!!
150+
var x;
151+
152+
// Create a new "base" view, thus ensuring we have an `.iget` method and associated meta data...
153+
x = new ndarray( getDType( arr ), getData( arr ), getShape( arr ), getStrides( arr ), getOffset( arr ), getOrder( arr ) ); // eslint-disable-line max-len
154+
116155
if ( dtype === 'generic') {
117-
return generic( arr );
156+
return generic( x );
118157
}
119158
if ( dtype === 'binary' ) {
120-
return binary( arr );
159+
return binary( x );
121160
}
122-
return typed( arr, dtype );
161+
return typed( x, dtype );
123162
}
124163

125164

0 commit comments

Comments
 (0)