Skip to content

Commit 1943a05

Browse files
committed
Auto-generated commit
1 parent 850e17c commit 1943a05

File tree

7 files changed

+88
-10
lines changed

7 files changed

+88
-10
lines changed

NOTICE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Copyright (c) 2016-2021 The Stdlib Authors.
1+
Copyright (c) 2016-2022 The Stdlib Authors.

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ var arr = ndarray2array( buffer, shape, strides, offset, order );
116116
```javascript
117117
var shape2strides = require( '@stdlib/ndarray-base-shape2strides' );
118118
var strides2offset = require( '@stdlib/ndarray-base-strides2offset' );
119-
var dicreteUniform = require( '@stdlib/random-base-discrete-uniform' );
119+
var discreteUniform = require( '@stdlib/random-base-discrete-uniform' );
120120
var ndarray2array = require( '@stdlib/ndarray-base-to-array' );
121121

122122
// Create a data buffer:
@@ -143,7 +143,7 @@ console.log( 'Dims: %s', shape.join( 'x' ) );
143143
var arr;
144144
var j;
145145
for ( i = 0; i < 20; i++ ) {
146-
j = dicreteUniform( 0, ndims-1 );
146+
j = discreteUniform( 0, ndims-1 );
147147
strides[ j ] *= -1;
148148
offset = strides2offset( shape, strides );
149149

examples/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
var shape2strides = require( '@stdlib/ndarray-base-shape2strides' );
2222
var strides2offset = require( '@stdlib/ndarray-base-strides2offset' );
23-
var dicreteUniform = require( '@stdlib/random-base-discrete-uniform' );
23+
var discreteUniform = require( '@stdlib/random-base-discrete-uniform' );
2424
var ndarray2array = require( './../lib' );
2525

2626
// Create a data buffer:
@@ -47,7 +47,7 @@ console.log( 'Dims: %s', shape.join( 'x' ) );
4747
var arr;
4848
var j;
4949
for ( i = 0; i < 20; i++ ) {
50-
j = dicreteUniform( 0, ndims-1 );
50+
j = discreteUniform( 0, ndims-1 );
5151
strides[ j ] *= -1;
5252
offset = strides2offset( shape, strides );
5353

lib/main.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
// MODULES //
2222

23+
var arraylike2object = require( '@stdlib/array-base-arraylike2object' );
2324
var recurse = require( './recurse.js' );
2425

2526

@@ -55,7 +56,7 @@ function ndarray2array( buffer, shape, strides, offset, order ) {
5556
return [];
5657
}
5758
}
58-
return recurse( buffer, shape, strides, offset, order, 0 );
59+
return recurse( arraylike2object( buffer ), shape, strides, offset, order, 0 ); // eslint-disable-line max-len
5960
}
6061

6162

lib/recurse.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,31 +24,33 @@
2424
* Recursively converts an ndarray to a generic array.
2525
*
2626
* @private
27-
* @param {(ArrayLikeObject|TypedArray|Buffer)} buffer - data buffer
27+
* @param {Object} obj - array object
28+
* @param {(ArrayLikeObject|TypedArray|Buffer)} obj.data - data buffer
29+
* @param {Function} obj.getter - element accessor
2830
* @param {NonNegativeIntegerArray} shape - array shape
2931
* @param {IntegerArray} strides - array strides
3032
* @param {NonNegativeInteger} offset - index offset
3133
* @param {string} order - specifies whether an array is row-major (C-style) or column-major (Fortran-style)
3234
* @param {NonNegativeInteger} dim - dimension
3335
* @returns {(Array|Array<Array>)} output array
3436
*/
35-
function recurse( buffer, shape, strides, offset, order, dim ) {
37+
function recurse( obj, shape, strides, offset, order, dim ) {
3638
var stride;
3739
var item;
3840
var out;
3941
var n;
4042
var i;
4143

4244
if ( dim >= shape.length ) {
43-
return buffer[ offset ];
45+
return obj.getter( obj.data, offset );
4446
}
4547
out = [];
4648

4749
n = shape[ dim ];
4850
stride = strides[ dim ];
4951

5052
for ( i = 0; i < n; i++ ) {
51-
item = recurse( buffer, shape, strides, offset, order, dim+1 );
53+
item = recurse( obj, shape, strides, offset, order, dim+1 );
5254
out.push( item );
5355
offset += stride;
5456
}

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,16 @@
3737
"url": "https://github.com/stdlib-js/stdlib/issues"
3838
},
3939
"dependencies": {
40+
"@stdlib/array-base-arraylike2object": "^0.0.x",
4041
"@stdlib/types": "^0.0.x"
4142
},
4243
"devDependencies": {
44+
"@stdlib/array-complex64": "^0.0.x",
4345
"@stdlib/assert-is-array": "^0.0.x",
4446
"@stdlib/assert-is-array-array": "^0.0.x",
4547
"@stdlib/bench": "^0.0.x",
48+
"@stdlib/complex-imagf": "^0.0.x",
49+
"@stdlib/complex-realf": "^0.0.x",
4650
"@stdlib/ndarray-base-numel": "^0.0.x",
4751
"@stdlib/ndarray-base-shape2strides": "^0.0.x",
4852
"@stdlib/ndarray-base-strides2offset": "^0.0.x",

test/test.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222

2323
var tape = require( 'tape' );
2424
var isArray = require( '@stdlib/assert-is-array' );
25+
var Complex64Array = require( '@stdlib/array-complex64' );
26+
var realf = require( '@stdlib/complex-realf' );
27+
var imagf = require( '@stdlib/complex-imagf' );
2528
var ndarray2array = require( './../lib' );
2629

2730

@@ -71,6 +74,40 @@ tape( 'the function converts an ndarray buffer to a generic array (1d; order=row
7174
t.end();
7275
});
7376

77+
tape( 'the function converts an ndarray buffer to a generic array (1d; order=row-major; complex data type)', function test( t ) {
78+
var strides;
79+
var offset;
80+
var buffer;
81+
var order;
82+
var shape;
83+
var out;
84+
var v;
85+
86+
buffer = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
87+
88+
shape = [ 3 ];
89+
order = 'row-major';
90+
strides = [ -1 ];
91+
offset = 2;
92+
93+
out = ndarray2array( buffer, shape, strides, offset, order );
94+
t.strictEqual( isArray( out ), true, 'returns an array' );
95+
96+
v = out[ 0 ];
97+
t.strictEqual( realf( v ), 5.0, 'returns expected value' );
98+
t.strictEqual( imagf( v ), 6.0, 'returns expected value' );
99+
100+
v = out[ 1 ];
101+
t.strictEqual( realf( v ), 3.0, 'returns expected value' );
102+
t.strictEqual( imagf( v ), 4.0, 'returns expected value' );
103+
104+
v = out[ 2 ];
105+
t.strictEqual( realf( v ), 1.0, 'returns expected value' );
106+
t.strictEqual( imagf( v ), 2.0, 'returns expected value' );
107+
108+
t.end();
109+
});
110+
74111
tape( 'the function converts an ndarray buffer to a generic array (1d; order=column-major)', function test( t ) {
75112
var expected;
76113
var strides;
@@ -95,6 +132,40 @@ tape( 'the function converts an ndarray buffer to a generic array (1d; order=col
95132
t.end();
96133
});
97134

135+
tape( 'the function converts an ndarray buffer to a generic array (1d; order=column-major; complex data type)', function test( t ) {
136+
var strides;
137+
var offset;
138+
var buffer;
139+
var order;
140+
var shape;
141+
var out;
142+
var v;
143+
144+
buffer = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
145+
146+
shape = [ 3 ];
147+
order = 'column-major';
148+
strides = [ 1 ];
149+
offset = 0;
150+
151+
out = ndarray2array( buffer, shape, strides, offset, order );
152+
t.strictEqual( isArray( out ), true, 'returns an array' );
153+
154+
v = out[ 0 ];
155+
t.strictEqual( realf( v ), 1.0, 'returns expected value' );
156+
t.strictEqual( imagf( v ), 2.0, 'returns expected value' );
157+
158+
v = out[ 1 ];
159+
t.strictEqual( realf( v ), 3.0, 'returns expected value' );
160+
t.strictEqual( imagf( v ), 4.0, 'returns expected value' );
161+
162+
v = out[ 2 ];
163+
t.strictEqual( realf( v ), 5.0, 'returns expected value' );
164+
t.strictEqual( imagf( v ), 6.0, 'returns expected value' );
165+
166+
t.end();
167+
});
168+
98169
tape( 'the function converts an ndarray buffer to a generic array (2d; order=row-major)', function test( t ) {
99170
var expected;
100171
var strides;

0 commit comments

Comments
 (0)