Skip to content

Commit

Permalink
Auto-generated commit
Browse files Browse the repository at this point in the history
  • Loading branch information
stdlib-bot committed Oct 10, 2023
1 parent 4e784e7 commit 91637f1
Show file tree
Hide file tree
Showing 10 changed files with 212 additions and 36 deletions.
32 changes: 19 additions & 13 deletions array/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,13 @@ var isOrder = require( './../../base/assert/is-order' );
var isCastingMode = require( './../../base/assert/is-casting-mode' );
var isAllowedCast = require( './../../base/assert/is-allowed-data-type-cast' );
var createBuffer = require( './../../base/buffer' );
var getType = require( './../../base/buffer-dtype' );
var getBufferDType = require( './../../base/buffer-dtype' );
var getDType = require( './../../dtype' );
var getShape = require( './../../shape' );
var getStrides = require( './../../strides' );
var getOffset = require( './../../offset' );
var getOrder = require( './../../order' );
var getData = require( './../../data-buffer' );
var arrayShape = require( '@stdlib/array/shape' );
var flatten = require( '@stdlib/array/base/flatten' );
var format = require( '@stdlib/string/format' );
Expand Down Expand Up @@ -156,10 +162,10 @@ function array() {
}
if ( buffer ) {
if ( isndarrayLike( buffer ) ) {
btype = buffer.dtype;
btype = getDType( buffer );
FLG = true;
} else {
btype = getType( buffer );
btype = getBufferDType( buffer );
FLG = false;
}
}
Expand Down Expand Up @@ -221,18 +227,18 @@ function array() {
// If the user indicated that "any" order suffices (meaning the user does not care about ndarray order), then we use the default order, unless the input ndarray is either unequivocally "row-major" or "column-major" or configured as such....
if ( order === 'any' ) {
// Compute the layout order in order to ascertain whether an ndarray can be considered both "row-major" and "column-major":
ord = strides2order( buffer.strides );
ord = strides2order( getStrides( buffer ) );

// If the ndarray can be considered both "row-major" and "column-major", then use the default order; otherwise, use the ndarray's stated layout order...
if ( ord === 3 ) {
order = defaults.order;
} else {
order = buffer.order;
order = getOrder( buffer );
}
}
// Otherwise, use the same order as the provided ndarray...
else if ( order === 'same' ) {
order = buffer.order;
order = getOrder( buffer );
}
} else {
order = defaults.order;
Expand Down Expand Up @@ -276,9 +282,9 @@ function array() {
len = numel( shape );
} else if ( buffer ) {
if ( FLG ) {
shape = buffer.shape;
ndims = buffer.ndims;
len = buffer.length;
shape = getShape( buffer );
ndims = shape.length;
len = numel( shape );
} else if ( opts.flatten && isArray( buffer ) ) {
shape = arrayShape( buffer );
osh = shape; // cache a reference to the inferred shape
Expand All @@ -299,15 +305,15 @@ function array() {
}
// If not provided a data buffer, create it; otherwise, see if we need to cast a provided data buffer to another data type or perform a copy...
if ( FLG ) {
if ( buffer.length !== len ) {
if ( numel( buffer.shape ) !== len ) {
throw new RangeError( 'invalid arguments. Array shape is incompatible with provided data source. Number of data source elements does not match array shape.' );
}
if ( btype !== dtype || opts.copy ) {
buffer = copyView( buffer, dtype );
} else {
strides = buffer.strides;
offset = buffer.offset;
buffer = buffer.data;
strides = getStrides( buffer );
offset = getOffset( buffer );
buffer = getData( buffer );
if ( strides.length < ndims ) {
// Account for augmented dimensions (note: expanding the strides array to account for prepended singleton dimensions does **not** affect the index offset):
strides = expandStrides( ndims, shape, strides, order );
Expand Down
23 changes: 22 additions & 1 deletion base/strides/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,15 @@

// MODULES //

var shape2strides = require( './../../../base/shape2strides' );
var copyIndexed = require( '@stdlib/array/base/copy-indexed' );


// VARIABLES //

var ROW_MAJOR = 'row-major';


// MAIN //

/**
Expand All @@ -39,7 +45,22 @@ var copyIndexed = require( '@stdlib/array/base/copy-indexed' );
* // returns [ 9, 3, 1 ]
*/
function strides( x, copy ) {
var st = x.strides;
var ord;
var sh;
var st;

st= x.strides;
if ( typeof st !== 'object' || st === null ) {
sh = x.shape;
if ( sh.length === 0 ) {
return [ 0 ];
}
ord = x.order;
if ( typeof ord !== 'string' ) {
ord = ROW_MAJOR;
}
return shape2strides( sh, ord );
}
if ( copy ) {
return copyIndexed( st );
}
Expand Down
135 changes: 135 additions & 0 deletions base/strides/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ tape( 'if provided a zero-dimensional ndarray, the function returns a single-ele
t.end();
});

tape( 'if provided a zero-dimensional minimal ndarray-like object, the function returns a single-element strides array', function test( t ) {
var x = {
'shape': []
};
t.deepEqual( strides( x, false ), [ 0 ], 'returns expected value' );
t.end();
});

tape( 'the function returns the strides of an ndarray', function test( t ) {
var expected;
var values;
Expand Down Expand Up @@ -106,6 +114,133 @@ tape( 'the function accepts minimal ndarray-like objects (strides)', function te
t.end();
});

tape( 'the function accepts minimal ndarray-like objects (shape)', function test( t ) {
var expected;
var values;
var out;
var i;

values = [
{
'shape': [ 3, 3, 3 ]
},
{
'shape': [ 1, 1 ]
},
{
'shape': [ 3, 3, 0, 3 ]
},
{
'shape': [ 1, 2, 3, 4 ]
},
{
'shape': [ 5 ]
}
];

expected = [
[ 9, 3, 1 ],
[ 1, 1 ],
[ 0, 0, 3, 1 ],
[ 24, 12, 4, 1 ],
[ 1 ]
];

for ( i = 0; i < values.length; i++ ) {
out = strides( values[ i ], false );
t.deepEqual( out, expected[ i ], 'returns expected value for shape '+values[ i ].shape.join( 'x' ) );
}
t.end();
});

tape( 'the function accepts minimal ndarray-like objects (shape, order=row-major)', function test( t ) {
var expected;
var values;
var out;
var i;

values = [
{
'shape': [ 3, 3, 3 ],
'order': 'row-major'
},
{
'shape': [ 1, 1 ],
'order': 'row-major'
},
{
'shape': [ 3, 3, 0, 3 ],
'order': 'row-major'
},
{
'shape': [ 1, 2, 3, 4 ],
'order': 'row-major'
},
{
'shape': [ 5 ],
'order': 'row-major'
}
];

expected = [
[ 9, 3, 1 ],
[ 1, 1 ],
[ 0, 0, 3, 1 ],
[ 24, 12, 4, 1 ],
[ 1 ]
];

for ( i = 0; i < values.length; i++ ) {
out = strides( values[ i ], false );
t.deepEqual( out, expected[ i ], 'returns expected value for shape '+values[ i ].shape.join( 'x' ) );
}
t.end();
});

tape( 'the function accepts minimal ndarray-like objects (shape, order=column-major)', function test( t ) {
var expected;
var values;
var out;
var i;

values = [
{
'shape': [ 3, 3, 3 ],
'order': 'column-major'
},
{
'shape': [ 1, 1 ],
'order': 'column-major'
},
{
'shape': [ 3, 3, 0, 3 ],
'order': 'column-major'
},
{
'shape': [ 1, 2, 3, 4 ],
'order': 'column-major'
},
{
'shape': [ 5 ],
'order': 'column-major'
}
];

expected = [
[ 1, 3, 9 ],
[ 1, 1 ],
[ 1, 3, 9, 0 ],
[ 1, 1, 2, 6 ],
[ 1 ]
];

for ( i = 0; i < values.length; i++ ) {
out = strides( values[ i ], false );
t.deepEqual( out, expected[ i ], 'returns expected value for shape '+values[ i ].shape.join( 'x' ) );
}
t.end();
});

tape( 'the function supports returning a guaranteed copy of an input ndarray strides', function test( t ) {
var expected;
var values;
Expand Down
12 changes: 9 additions & 3 deletions broadcast-array/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
var isCollection = require( '@stdlib/assert/is-collection' );
var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive;
var copy = require( '@stdlib/array/base/copy-indexed' );
var getDType = require( './../../dtype' );
var getShape = require( './../../shape' );
var getStrides = require( './../../strides' );
var getOffset = require( './../../offset' );
var getOrder = require( './../../order' );
var getData = require( './../../data-buffer' );
var format = require( '@stdlib/string/format' );


Expand Down Expand Up @@ -105,7 +111,7 @@ function broadcastArray( x, shape ) {
throw new TypeError( format( 'invalid argument. Second argument must be an array of nonnegative integers. Value: `%s`.', shape ) );
}
N = shape.length;
sh = x.shape;
sh = getShape( x );
M = sh.length;
if ( N < M ) {
throw new Error( 'invalid argument. Cannot broadcast an array to a shape having fewer dimensions. Arrays can only be broadcasted to shapes having the same or more dimensions.' );
Expand All @@ -116,7 +122,7 @@ function broadcastArray( x, shape ) {
strides.push( 0 );
}
// Determine the output array strides...
st = x.strides;
st = getStrides( x );
for ( i = N-1; i >= 0; i-- ) {
j = M - N + i;
if ( j < 0 ) {
Expand All @@ -141,7 +147,7 @@ function broadcastArray( x, shape ) {
throw new Error( format( 'invalid argument. Input array and the specified shape are broadcast incompatible. Array shape: (%s). Desired shape: (%s). Dimension: %u.', copy( sh ).join( ', ' ), copy( shape ).join( ', ' ), i ) );
}
}
return new x.constructor( x.dtype, x.data, copy( shape ), strides, x.offset, x.order, { // eslint-disable-line max-len
return new x.constructor( getDType( x ), getData( x ), copy( shape ), strides, getOffset( x ), getOrder( x ), { // eslint-disable-line max-len
'readonly': true
});
}
Expand Down
3 changes: 2 additions & 1 deletion dispatch-by/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var isFunction = require( '@stdlib/assert/is-function' );
var isCollection = require( '@stdlib/assert/is-collection' );
var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
var format = require( '@stdlib/string/format' );
var getDType = require( './../../dtype' );
var resolveEnum = require( './../../base/dtype-resolve-enum' );
var indexOfTypes = require( './index_of_types.js' );

Expand Down Expand Up @@ -209,7 +210,7 @@ function dispatchBy( fcns, types, data, nargs, nin, nout ) {
}
}
arrays.push( v );
dtypes.push( resolveEnum( v.dtype ) );
dtypes.push( resolveEnum( getDType( v ) ) );
}
// Resolve the ndarray function satisfying the input array types:
idx = indexOfTypes( nfcns, narrays, types, narrays, 1, 0, dtypes, 1, 0 ); // eslint-disable-line max-len
Expand Down
3 changes: 2 additions & 1 deletion dispatch/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var isFunction = require( '@stdlib/assert/is-function' );
var isCollection = require( '@stdlib/assert/is-collection' );
var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
var format = require( '@stdlib/string/format' );
var getDType = require( './../../dtype' );
var resolveEnum = require( './../../base/dtype-resolve-enum' );
var indexOfTypes = require( './index_of_types.js' );

Expand Down Expand Up @@ -193,7 +194,7 @@ function dispatch( fcns, types, data, nargs, nin, nout ) {
}
}
arrays.push( v );
dtypes.push( resolveEnum( v.dtype ) );
dtypes.push( resolveEnum( getDType( v ) ) );
}
// Resolve the ndarray function satisfying the input array types:
idx = indexOfTypes( nfcns, narrays, types, narrays, 1, 0, dtypes, 1, 0 ); // eslint-disable-line max-len
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions dist/index.js.map

Large diffs are not rendered by default.

15 changes: 9 additions & 6 deletions empty-like/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ var hasOwnProp = require( '@stdlib/assert/has-own-property' );
var shape2strides = require( './../../base/shape2strides' );
var strides2offset = require( './../../base/strides2offset' );
var numel = require( './../../base/numel' );
var getDType = require( './../../dtype' );
var getShape = require( './../../shape' );
var getOrder = require( './../../order' );
var ndarray = require( './../../ctor' );
var emptyArray = require( '@stdlib/array/empty' );
var allocUnsafe = require( '@stdlib/buffer/alloc-unsafe' );
Expand Down Expand Up @@ -91,7 +94,7 @@ function emptyLike( x ) {
if ( hasOwnProp( options, 'dtype' ) ) {
dtype = options.dtype;
} else {
dtype = x.dtype;
dtype = getDType( x );
}
if ( hasOwnProp( options, 'shape' ) ) {
sh = options.shape;
Expand All @@ -102,12 +105,12 @@ function emptyLike( x ) {
throw new TypeError( format( 'invalid option. `%s` option must be a nonnegative integer or an array of nonnegative integers. Option: `%s`.', 'shape', sh ) );
}
} else {
sh = x.shape;
sh = getShape( x );
}
if ( hasOwnProp( options, 'order' ) ) {
order = options.order;
} else {
order = x.order;
order = getOrder( x );
}
if ( hasOwnProp( options, 'mode' ) ) {
opts.mode = options.mode;
Expand All @@ -116,9 +119,9 @@ function emptyLike( x ) {
opts.submode = options.submode;
}
} else {
dtype = x.dtype;
sh = x.shape;
order = x.order;
dtype = getDType( x );
sh = getShape( x );
order = getOrder( x );
}
ndims = sh.length;
if ( ndims > 0 ) {
Expand Down
Loading

0 comments on commit 91637f1

Please sign in to comment.