Skip to content

Commit

Permalink
refactor: use ndarray/base/assign for assignment
Browse files Browse the repository at this point in the history
This commit should allow for slightly better performance for real-
to-real assignment, as it removes an unnecessary identity function
call. For real-to-complex assignment, we leverage a specialized
array element accessor to convert a real number to an appropriately
cast complex number.
  • Loading branch information
kgryte committed Oct 13, 2023
1 parent c8417b0 commit 5a97436
Showing 1 changed file with 4 additions and 24 deletions.
28 changes: 4 additions & 24 deletions lib/node_modules/@stdlib/ndarray/base/slice-assign/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,8 @@
var isSafeCast = require( '@stdlib/ndarray/base/assert/is-safe-data-type-cast' );
var isSameKindCast = require( '@stdlib/ndarray/base/assert/is-same-kind-data-type-cast' );
var isFloatingPointDataType = require( '@stdlib/ndarray/base/assert/is-floating-point-data-type' );
var isComplexDataType = require( '@stdlib/ndarray/base/assert/is-complex-floating-point-data-type' );
var isRealDataType = require( '@stdlib/ndarray/base/assert/is-real-data-type' );
var broadcast = require( '@stdlib/ndarray/base/broadcast-array' );
var unary = require( '@stdlib/ndarray/base/unary' ); // TODO: replace with `@stdlib/ndarray/base/assign` and add native add-on support
var identity = require( '@stdlib/utils/identity-function' ); // TODO: remove once use `@stdlib/ndarray/base/assign`
var castReturn = require( '@stdlib/complex/base/cast-return' );
var complexCtors = require( '@stdlib/complex/ctors' );
var assign = require( '@stdlib/ndarray/base/assign' );
var slice = require( '@stdlib/ndarray/base/slice' );
var getDType = require( '@stdlib/ndarray/base/dtype' );
var getShape = require( '@stdlib/ndarray/base/shape' );
Expand Down Expand Up @@ -97,29 +92,14 @@ var format = require( '@stdlib/string/format' );
*/
function sliceAssign( x, y, s, strict ) {
var view;
var fcn;
var xdt;
var ydt;

xdt = getDType( x );
ydt = getDType( y );

// Safe casts are always allowed...
if ( isSafeCast( xdt, ydt ) ) {
// Check for real-to-complex conversion...
if ( isRealDataType( xdt ) && isComplexDataType( ydt ) ) {
// Need to cast a real number to a complex number:
fcn = castReturn( identity, 1, complexCtors( ydt ) );
} else {
// Should only be real->real and complex->complex:
fcn = identity;
}
}
// Allow same kind casts (i.e., downcasts) only when the output data type is floating-point...
else if ( isFloatingPointDataType( ydt ) && isSameKindCast( xdt, ydt ) ) {
// At this point, we know that the input data type and output data type are of the same "kind" (e.g., real->real and complex->complex), and, thus, we don't need to perform any special conversions:
fcn = identity;
} else {
// Safe casts are always allowed and allow same kind casts (i.e., downcasts) only when the output data type is floating-point...
if ( !isSafeCast( xdt, ydt ) && !( isFloatingPointDataType( ydt ) && isSameKindCast( xdt, ydt ) ) ) {
throw new TypeError( format( 'invalid argument. Input array values cannot be safely cast to the output array data type. Data types: [%s, %s].', xdt, ydt ) );
}
// Resolve a writable output array view:
Expand All @@ -129,7 +109,7 @@ function sliceAssign( x, y, s, strict ) {
x = broadcast( x, getShape( view, true ) );

// Set elements from `x` in `y`:
unary( [ x, view ], fcn );
assign( [ x, view ] );

// Return the original output array:
return y;
Expand Down

0 comments on commit 5a97436

Please sign in to comment.