Skip to content

Commit 081b2b8

Browse files
committed
Add support for empty arrays
1 parent 26ee457 commit 081b2b8

2 files changed

Lines changed: 44 additions & 1 deletion

File tree

lib/node_modules/@stdlib/ndarray/zeros/lib/main.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,11 @@ function zeros( shape ) {
104104
if ( buf === null ) {
105105
throw new TypeError( 'invalid option. `dtype` option must be a recognized data type. Value: `' + opts.dtype + '`.' );
106106
}
107-
st = shape2strides( sh, opts.order );
107+
if ( sh.length === 0 ) {
108+
st = [ 0 ];
109+
} else {
110+
st = shape2strides( sh, opts.order );
111+
}
108112
return new ndarray( opts.dtype, buf, sh, st, strides2offset( sh, st ), opts.order ); // eslint-disable-line max-len
109113
}
110114

lib/node_modules/@stdlib/ndarray/zeros/test/test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,3 +823,42 @@ tape( 'the function returns a zero-filled array (dtype=generic, order=column-maj
823823

824824
t.end();
825825
});
826+
827+
tape( 'the function supports empty arrays', function test( t ) {
828+
var expected;
829+
var arr;
830+
831+
expected = new Float64Array( 0 );
832+
833+
arr = zeros( [] );
834+
t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' );
835+
t.strictEqual( arr.dtype, 'float64', 'returns expected value' );
836+
t.deepEqual( arr.shape, [], 'returns expected value' );
837+
t.strictEqual( instanceOf( arr.data, Float64Array ), true, 'returns expected value' );
838+
t.deepEqual( arr.data, expected, 'returns expected value' );
839+
t.strictEqual( arr.order, 'row-major' );
840+
841+
t.end();
842+
});
843+
844+
tape( 'the function supports empty arrays (options)', function test( t ) {
845+
var expected;
846+
var opts;
847+
var arr;
848+
849+
expected = [];
850+
851+
opts = {
852+
'dtype': 'generic',
853+
'order': 'row-major'
854+
};
855+
arr = zeros( [], opts );
856+
t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' );
857+
t.strictEqual( arr.dtype, opts.dtype, 'returns expected value' );
858+
t.deepEqual( arr.shape, [], 'returns expected value' );
859+
t.strictEqual( instanceOf( arr.data, Array ), true, 'returns expected value' );
860+
t.deepEqual( arr.data, expected, 'returns expected value' );
861+
t.strictEqual( arr.order, opts.order );
862+
863+
t.end();
864+
});

0 commit comments

Comments
 (0)