Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 275x 275x 275x 275x 275x 275x 275x 275x 275x 275x 275x 60x 60x 215x 215x 215x 215x 215x 215x 215x 275x 27x 27x 10x 10x 27x 188x 188x 81x 81x 23x 23x 23x 23x 9x 9x 14x 14x 8x 8x 6x 6x 6x 6x 23x 58x 58x 58x 58x 20x 20x 38x 38x 38x 81x 107x 107x 107x 107x 49x 49x 58x 58x 52x 52x 6x 6x 6x 6x 101x 107x 20x 20x 81x 81x 81x 275x 27x 27x 126x 126x 275x 19x 19x 107x 275x 86x 86x 15x 275x 3x 3x 3x 3x 3x | /**
* @license Apache-2.0
*
* Copyright (c) 2026 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
// MODULES //
var isPlainObject = require( '@stdlib/assert/is-plain-object' );
var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive;
var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
var isMostlySafeCast = require( '@stdlib/ndarray/base/assert/is-mostly-safe-data-type-cast' );
var resolveStr = require( '@stdlib/ndarray/base/dtype-resolve-str' );
var copy = require( '@stdlib/ndarray/base/assign' );
var getDType = require( '@stdlib/ndarray/dtype' );
var sort = require( '@stdlib/blas/ext/sort' );
var format = require( '@stdlib/string/format' );
// MAIN //
/**
* Sorts the elements of an input ndarray along one or more ndarray dimensions and assigns the results to an output ndarray.
*
* @param {ndarrayLike} x - input ndarray
* @param {(ndarrayLike|number|string)} [sortOrder=1.0] - sort order
* @param {ndarrayLike} out - output ndarray
* @param {Options} [options] - function options
* @param {IntegerArray} [options.dims] - list of dimensions over which to perform operation
* @throws {TypeError} first argument must be an ndarray-like object
* @throws {TypeError} sort order argument must be either an ndarray-like object, a numeric value, or a supported string
* @throws {TypeError} output argument must be an ndarray-like object
* @throws {TypeError} options argument must be an object
* @throws {RangeError} dimension indices must not exceed input ndarray bounds
* @throws {RangeError} number of dimension indices must not exceed the number of input ndarray dimensions
* @throws {TypeError} first argument cannot be safely cast to the data type of the output argument
* @throws {Error} must provide valid options
* @returns {ndarray} output ndarray
*
* @example
* var zeros = require( '@stdlib/ndarray/zeros' );
* var array = require( '@stdlib/ndarray/array' );
*
* var x = array( [ [ [ 1.0, 2.0 ] ], [ [ -3.0, 4.0 ] ], [ [ -5.0, 6.0 ] ] ] );
* // returns <ndarray>[ [ [ 1.0, 2.0 ] ], [ [ -3.0, 4.0 ] ], [ [ -5.0, 6.0 ] ] ]
*
* var y = zeros( [ 3, 1, 2 ] );
* // returns <ndarray>[ [ [ 0.0, 0.0 ] ], [ [ 0.0, 0.0 ] ], [ [ 0.0, 0.0 ] ] ]
*
* var out = assign( x, y );
* // returns <ndarray>[ [ [ -5.0, -3.0 ] ], [ [ 1.0, 2.0 ] ], [ [ 4.0, 6.0 ] ] ]
*
* var bool = ( y === out );
* // returns true
*/
function assign( x, sortOrder, out ) {
var hasOptions;
var options;
var nargs;
var xdt;
var odt;
var so;
var o;
nargs = arguments.length;
if ( !isndarrayLike( x ) ) {
throw new TypeError( format( 'invalid argument. First argument must be an ndarray-like object. Value: `%s`.', x ) );
}
// Initialize the sort order to ascending:
so = 1;
// Initialize a flag indicating whether an `options` argument was provided:
hasOptions = false;
// Case: assign( x, out )
if ( nargs <= 2 ) {
o = sortOrder;
if ( !isndarrayLike( o ) ) {
throw new TypeError( format( 'invalid argument. Second argument must be an ndarray-like object. Value: `%s`.', o ) );
}
}
// Case: assign( x, ???, ??? )
else if ( nargs === 3 ) {
// Case: assign( x, sort_order, out )
if ( isndarrayLike( out ) ) {
o = out;
// Case: assign( x, sort_order_scalar, out )
if ( isNumber( sortOrder ) || isString( sortOrder ) ) {
so = sortOrder;
}
// Case: assign( x, sort_order_ndarray, out )
else if ( isndarrayLike( sortOrder ) ) {
so = sortOrder;
}
// Case: assign( x, ???, out )
else {
throw new TypeError( format( 'invalid argument. Second argument must be either an ndarray-like object, a numeric value, or a supported string. Value: `%s`.', sortOrder ) );
}
}
// Case: assign( x, out, options )
else {
o = sortOrder;
if ( !isndarrayLike( o ) ) {
throw new TypeError( format( 'invalid argument. Second argument must be an ndarray-like object. Value: `%s`.', o ) );
}
options = out;
hasOptions = true;
}
}
// Case: assign( x, sort_order, out, options )
else { // nargs > 3
// Case: assign( x, sort_order_scalar, out, options )
if ( isNumber( sortOrder ) || isString( sortOrder ) ) {
so = sortOrder;
}
// Case: assign( x, sort_order_ndarray, out, options )
else if ( isndarrayLike( sortOrder ) ) {
so = sortOrder;
}
// Case: assign( x, ???, out, options )
else {
throw new TypeError( format( 'invalid argument. Second argument must be either an ndarray-like object, a numeric value, or a supported string. Value: `%s`.', sortOrder ) );
}
o = out;
if ( !isndarrayLike( o ) ) {
throw new TypeError( format( 'invalid argument. Third argument must be an ndarray-like object. Value: `%s`.', o ) );
}
options = arguments[ 3 ];
hasOptions = true;
}
if ( hasOptions && !isPlainObject( options ) ) {
throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );
}
xdt = getDType( x );
odt = getDType( o );
if ( !isMostlySafeCast( xdt, odt ) ) {
throw new TypeError( format( 'invalid argument. First argument cannot be safely cast to the output data type. Data types: [%s, %s].', resolveStr( xdt ), resolveStr( odt ) ) );
}
copy( [ x, o ] );
if ( hasOptions ) {
return sort( o, so, options );
}
return sort( o, so );
}
// EXPORTS //
module.exports = assign;
|