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 14, 2023
1 parent 4f2df91 commit d5a31ee
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 38 deletions.
57 changes: 48 additions & 9 deletions array/lib/copy_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,18 @@

// MODULES //

var arraylike2object = require( '@stdlib/array/base/arraylike2object' );
var castReturn = require( '@stdlib/complex/base/cast-return' );
var complexCtors = require( '@stdlib/complex/ctors' );
var bufferCtors = require( './../../base/buffer-ctors' );
var allocUnsafe = require( '@stdlib/buffer/alloc-unsafe' );
var ndarray = require( './../../base/ctor' );
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' );


// FUNCTIONS //
Expand All @@ -41,7 +51,7 @@ function generic( arr ) {
len = arr.length;
out = [];
for ( i = 0; i < len; i++ ) {
out.push( arr.get( i ) ); // FIXME: what if `arr` has more than one dimensions?
out.push( arr.iget( i ) ); // as output buffer is generic, should work with both real- and complex-valued ndarrays
}
return out;
}
Expand All @@ -61,7 +71,7 @@ function binary( arr ) {
len = arr.length;
out = allocUnsafe( len );
for ( i = 0; i < len; i++ ) {
out[ i ] = arr.get( i ); // FIXME: what if `arr` has more than one dimensions?
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)
}
return out;
}
Expand All @@ -78,15 +88,40 @@ function typed( arr, dtype ) {
var ctor;
var len;
var out;
var set;
var fcn;
var o;
var i;

ctor = bufferCtors( dtype );
len = arr.length;
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`
for ( i = 0; i < len; i++ ) {
out[ i ] = arr.get( i ); // FIXME: what if `arr` has more than one dimensions?
out = new ctor( len );

// If the output data buffer is a complex number array, we need to use accessors...
o = arraylike2object( out );
if ( o.accessorProtocol ) {
set = o.accessors[ 1 ];
fcn = castReturn( wrapper, 1, complexCtors( dtype ) );
for ( i = 0; i < len; i++ ) {
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)
}
} else {
for ( i = 0; i < len; i++ ) {
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)
}
}
return out;

/**
* Returns the ndarray element specified by a provided linear index.
*
* @private
* @param {NonNegativeInteger} i - linear index
* @returns {*} value
*/
function wrapper( i ) {
return arr.iget( i );
}
}


Expand All @@ -112,14 +147,18 @@ function typed( arr, dtype ) {
* // returns <Float64Array>[ 3.0, 2.0, 1.0 ]
*/
function copyView( arr, dtype ) {
// TODO: handle complex number dtypes!!
var x;

// Create a new "base" view, thus ensuring we have an `.iget` method and associated meta data...
x = new ndarray( getDType( arr ), getData( arr ), getShape( arr ), getStrides( arr ), getOffset( arr ), getOrder( arr ) ); // eslint-disable-line max-len

if ( dtype === 'generic') {
return generic( arr );
return generic( x );
}
if ( dtype === 'binary' ) {
return binary( arr );
return binary( x );
}
return typed( arr, dtype );
return typed( x, dtype );
}


Expand Down
9 changes: 9 additions & 0 deletions base/assign/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@

// MODULES //

var isComplexDataType = require( './../../../base/assert/is-complex-floating-point-data-type' );
var isRealDataType = require( './../../../base/assert/is-real-data-type' );
var iterationOrder = require( './../../../base/iteration-order' );
var castReturn = require( '@stdlib/complex/base/cast-return' );
var complexCtors = require( '@stdlib/complex/ctors' );
var minmaxViewBufferIndex = require( './../../../base/minmax-view-buffer-index' );
var ndarray2object = require( './../../../base/ndarraylike2object' );
var blockedaccessorassign2d = require( './2d_blocked_accessors.js' );
Expand Down Expand Up @@ -206,6 +210,11 @@ function assign( arrays ) {
x = ndarray2object( arrays[ 0 ] );
y = ndarray2object( arrays[ 1 ] );

// Determine whether we are casting a real data type to a complex data type and we need to use a specialized accessor (note: we don't support the other way, complex-to-real, as this is not an allowed (mostly) safe cast)...
if ( isRealDataType( x.dtype ) && isComplexDataType( y.dtype ) ) {
x.accessorProtocol = true;
x.accessors[ 0 ] = castReturn( x.accessors[ 0 ], 2, complexCtors( y.dtype ) ); // eslint-disable-line max-len
}
// Verify that the input and output arrays have the same number of dimensions...
shx = x.shape;
shy = y.shape;
Expand Down
2 changes: 2 additions & 0 deletions base/assign/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ tape( 'main export is a function', function test( t ) {
t.strictEqual( typeof assign, 'function', 'main export is a function' );
t.end();
});

// FIXME: add tests
9 changes: 9 additions & 0 deletions base/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ var ns = {};
*/
setReadOnly( ns, 'assert', require( './../../base/assert' ) );

/**
* @name assign
* @memberof ns
* @readonly
* @type {Function}
* @see {@link module:@stdlib/ndarray/base/assign}
*/
setReadOnly( ns, 'assign', require( './../../base/assign' ) );

/**
* @name binaryLoopOrder
* @memberof ns
Expand Down
2 changes: 2 additions & 0 deletions base/nullary/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ tape( 'main export is a function', function test( t ) {
t.strictEqual( typeof nullary, 'function', 'main export is a function' );
t.end();
});

// FIXME: add tests
28 changes: 4 additions & 24 deletions 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( './../../../base/assert/is-safe-data-type-cast' );
var isSameKindCast = require( './../../../base/assert/is-same-kind-data-type-cast' );
var isFloatingPointDataType = require( './../../../base/assert/is-floating-point-data-type' );
var isComplexDataType = require( './../../../base/assert/is-complex-floating-point-data-type' );
var isRealDataType = require( './../../../base/assert/is-real-data-type' );
var broadcast = require( './../../../base/broadcast-array' );
var unary = require( './../../../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( './../../../base/assign' );
var slice = require( './../../../base/slice' );
var getDType = require( './../../../base/dtype' );
var getShape = require( './../../../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 ) ) ) { // eslint-disable-line max-len
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
2 changes: 2 additions & 0 deletions base/unary/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ tape( 'main export is a function', function test( t ) {
t.strictEqual( typeof unary, 'function', 'main export is a function' );
t.end();
});

// FIXME: add tests
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.

0 comments on commit d5a31ee

Please sign in to comment.