diff --git a/lib/node_modules/@stdlib/blas/ext/sorthp/README.md b/lib/node_modules/@stdlib/blas/ext/sorthp/README.md new file mode 100644 index 000000000000..c56ceeb1a214 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/sorthp/README.md @@ -0,0 +1,198 @@ + + +# sorthp + +> Sort an input [ndarray][@stdlib/ndarray/ctor] along one or more [ndarray][@stdlib/ndarray/ctor] dimensions using heapsort. + +
+ +## Usage + +```javascript +var sorthp = require( '@stdlib/blas/ext/sorthp' ); +``` + +#### sorthp( x\[, sortOrder]\[, options] ) + +Sorts an input [ndarray][@stdlib/ndarray/ctor] along one or more [ndarray][@stdlib/ndarray/ctor] dimensions using heapsort. + +```javascript +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var array = require( '@stdlib/ndarray/array' ); + +var x = array( [ -1.0, 2.0, -3.0 ] ); + +var y = sorthp( x ); +// returns + +var arr = ndarray2array( y ); +// returns [ -3.0, -1.0, 2.0 ] + +var bool = ( x === y ); +// returns true +``` + +The function has the following parameters: + +- **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have a real-valued or "generic" [data type][@stdlib/ndarray/dtypes]. +- **sortOrder**: sort order (_optional_). May be either a scalar value, string, or an [ndarray][@stdlib/ndarray/ctor] having a real-valued or "generic" [data type][@stdlib/ndarray/dtypes]. If provided an [ndarray][@stdlib/ndarray/ctor], the value must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the complement of the shape defined by `options.dims`. For example, given the input shape `[2, 3, 4]` and `options.dims=[0]`, an [ndarray][@stdlib/ndarray/ctor] sort order must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the shape `[3, 4]`. Similarly, when performing the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor], an [ndarray][@stdlib/ndarray/ctor] sort order must be a zero-dimensional [ndarray][@stdlib/ndarray/ctor]. By default, the sort order is `1` (i.e., increasing order). +- **options**: function options (_optional_). + +The function accepts the following options: + +- **dims**: list of dimensions over which to perform operation. If not provided, the function performs the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor]. + +By default, the function sorts elements in increasing order. To sort in a different order, provide a `sortOrder` argument. + +```javascript +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var array = require( '@stdlib/ndarray/array' ); + +var x = array( [ -1.0, 2.0, -3.0 ] ); + +var y = sorthp( x, -1.0 ); +// returns + +var arr = ndarray2array( y ); +// returns [ 2.0, -1.0, -3.0 ] +``` + +In addition to numeric values, one can specify the sort order via one of the following string literals: `'ascending'`, `'asc'`, `'descending'`, or `'desc'`. The first two literals indicate to sort in ascending (i.e., increasing) order. The last two literals indicate to sort in descending (i.e., decreasing) order. + +```javascript +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var array = require( '@stdlib/ndarray/array' ); + +var x = array( [ -1.0, 2.0, -3.0 ] ); + +// Sort in ascending order: +var y = sorthp( x, 'asc' ); +// returns + +var arr = ndarray2array( y ); +// returns [ -3.0, -1.0, 2.0 ] + +// Sort in descending order: +y = sorthp( x, 'descending' ); +// returns + +arr = ndarray2array( y ); +// returns [ 2.0, -1.0, -3.0 ] +``` + +By default, the function performs the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor]. To perform the operation over specific dimensions, provide a `dims` option. + +```javascript +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var array = require( '@stdlib/ndarray/array' ); + +var x = array( [ -1.0, 2.0, -3.0, 4.0 ], { + 'shape': [ 2, 2 ], + 'order': 'row-major' +}); + +var v = ndarray2array( x ); +// returns [ [ -1.0, 2.0 ], [ -3.0, 4.0 ] ] + +var y = sorthp( x, { + 'dims': [ 0 ] +}); +// returns + +v = ndarray2array( y ); +// returns [ [ -3.0, 2.0 ], [ -1.0, 4.0 ] ] +``` + +
+ + + +
+ +## Notes + +- The input [ndarray][@stdlib/ndarray/ctor] is sorted **in-place** (i.e., the input [ndarray][@stdlib/ndarray/ctor] is **mutated**). +- If `sortOrder < 0.0` or is either `'desc'` or `'descending'`, the input [ndarray][@stdlib/ndarray/ctor] is sorted in **decreasing** order. If `sortOrder > 0.0` or is either `'asc'` or `'ascending'`, the input [ndarray][@stdlib/ndarray/ctor] is sorted in **increasing** order. If `sortOrder == 0.0`, the input [ndarray][@stdlib/ndarray/ctor] is left unchanged. +- The algorithm distinguishes between `-0` and `+0`. When sorted in increasing order, `-0` is sorted before `+0`. When sorted in decreasing order, `-0` is sorted after `+0`. +- The algorithm sorts `NaN` values to the end. When sorted in increasing order, `NaN` values are sorted last. When sorted in decreasing order, `NaN` values are sorted first. +- The algorithm has space complexity `O(1)` and time complexity `O(N log2 N)`. +- The algorithm is **unstable**, meaning that the algorithm may change the order of [ndarray][@stdlib/ndarray/ctor] elements which are equal or equivalent (e.g., `NaN` values). +- The function iterates over [ndarray][@stdlib/ndarray/ctor] elements according to the memory layout of the input [ndarray][@stdlib/ndarray/ctor]. Accordingly, performance degradation is possible when operating over multiple dimensions of a large non-contiguous multi-dimensional input [ndarray][@stdlib/ndarray/ctor]. In such scenarios, one may want to copy an input [ndarray][@stdlib/ndarray/ctor] to contiguous memory before sorting. + +
+ + + +
+ +## Examples + + + +```javascript +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var sorthp = require( '@stdlib/blas/ext/sorthp' ); + +// Generate an array of random numbers: +var xbuf = discreteUniform( 25, -20, 20, { + 'dtype': 'generic' +}); + +// Wrap in an ndarray: +var x = new ndarray( 'generic', xbuf, [ 5, 5 ], [ 5, 1 ], 0, 'row-major' ); +console.log( ndarray2array( x ) ); + +// Perform operation: +sorthp( x, { + 'dims': [ 0 ] +}); + +// Print the results: +console.log( ndarray2array( x ) ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/blas/ext/sorthp/benchmark/benchmark.js b/lib/node_modules/@stdlib/blas/ext/sorthp/benchmark/benchmark.js new file mode 100644 index 000000000000..701c10f22833 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/sorthp/benchmark/benchmark.js @@ -0,0 +1,105 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 bench = require( '@stdlib/bench' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var uniform = require( '@stdlib/random/array/uniform' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var pkg = require( './../package.json' ).name; +var sorthp = require( './../lib' ); + + +// VARIABLES // + +var options = { + 'dtype': 'float64' +}; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var x = uniform( len, -50.0, 50.0, options ); + x = new ndarray( options.dtype, x, [ len ], [ 1 ], 0, 'row-major' ); + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var o; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + o = sorthp( x, ( i%2 ) ? 1 : -1 ); + if ( typeof o !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( isnan( o.get( i%len ) ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( pkg+':dtype='+options.dtype+',len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/blas/ext/sorthp/docs/repl.txt b/lib/node_modules/@stdlib/blas/ext/sorthp/docs/repl.txt new file mode 100644 index 000000000000..472c27605a4e --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/sorthp/docs/repl.txt @@ -0,0 +1,66 @@ + +{{alias}}( x[, sortOrder][, options] ) + Sorts an input ndarray along one or more ndarray dimensions using heapsort. + + The algorithm distinguishes between `-0` and `+0`. When sorted in increasing + order, `-0` is sorted before `+0`. When sorted in decreasing order, `-0` is + sorted after `+0`. + + The algorithm sorts `NaN` values to the end. When sorted in increasing + order, `NaN` values are sorted last. When sorted in decreasing order, `NaN` + values are sorted first. + + The algorithm has space complexity O(1) and time complexity O(N log2 N). + + The algorithm is *unstable*, meaning that the algorithm may change the order + of ndarray elements which are equal or equivalent (e.g., `NaN` values). + + The function sorts an input ndarray in-place and thus mutates an input + ndarray. + + Parameters + ---------- + x: ndarray + Input array. Must have a real-valued or "generic" data type. + + sortOrder: ndarray|number|string (optional) + Sort order. May be either a scalar value, string, or an ndarray having a + real-valued or "generic" data type. If provided an ndarray, the value + must have a shape which is broadcast compatible with the complement of + the shape defined by `options.dims`. For example, given the input shape + `[2, 3, 4]` and `options.dims=[0]`, an ndarray sort order must have a + shape which is broadcast compatible with the shape `[3, 4]`. Similarly, + when performing the operation over all elements in a provided input + ndarray, an ndarray sort order must be a zero-dimensional ndarray. + + If specified as a string, must be one of the following values: + + - ascending: sort in increasing order. + - asc: sort in increasing order. + - descending: sort in decreasing order. + - desc: sort in decreasing order. + + By default, the sort order is `1` (i.e., increasing order). + + options: Object (optional) + Function options. + + options.dims: Array (optional) + List of dimensions over which to perform operation. If not provided, the + function performs the operation over all elements in a provided input + ndarray. + + Returns + ------- + out: ndarray + Input array. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/array}}( [ -1.0, 2.0, -3.0, -4.0 ] ); + > var y = {{alias}}( x ); + > {{alias:@stdlib/ndarray/to-array}}( y ) + [ -4.0, -3.0, -1.0, 2.0 ] + + See Also + -------- diff --git a/lib/node_modules/@stdlib/blas/ext/sorthp/docs/types/index.d.ts b/lib/node_modules/@stdlib/blas/ext/sorthp/docs/types/index.d.ts new file mode 100644 index 000000000000..0106cb13ac7f --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/sorthp/docs/types/index.d.ts @@ -0,0 +1,159 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2025 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. +*/ + +// TypeScript Version: 4.1 + +/// + +import { ArrayLike } from '@stdlib/types/array'; +import { typedndarray, realndarray, genericndarray } from '@stdlib/types/ndarray'; + +/** +* Input array. +*/ +type InputArray = realndarray | genericndarray; + +/** +* Sort order. +*/ +type SortOrder = typedndarray | genericndarray | number | 'descending' | 'desc' | 'ascending' | 'asc'; + + +/** +* Interface defining options. +*/ +interface Options { + /** + * List of dimensions over which to perform operation. + */ + dims?: ArrayLike; +} + +/** +* Interface for performing an operation on an ndarray. +*/ +interface Sorthp { + /** + * Sorts an input ndarray along one or more ndarray dimensions using heapsort. + * + * ## Notes + * + * - The input ndarray is sorted **in-place** (i.e., the input ndarray is **mutated**). + * - If `sortOrder < 0.0` or is either `'desc'` or `'descending'`, the input ndarray is sorted in **decreasing** order. If `sortOrder > 0.0` or is either `'asc'` or `'ascending'`, the input ndarray is sorted in **increasing** order. If `sortOrder == 0.0`, the input ndarray is left unchanged. + * - The algorithm distinguishes between `-0` and `+0`. When sorted in increasing order, `-0` is sorted before `+0`. When sorted in decreasing order, `-0` is sorted after `+0`. + * - The algorithm sorts `NaN` values to the end. When sorted in increasing order, `NaN` values are sorted last. When sorted in decreasing order, `NaN` values are sorted first. + * - The algorithm has space complexity `O(1)` and time complexity `O(N log2 N)`. + * - The algorithm is **unstable**, meaning that the algorithm may change the order of ndarray elements which are equal or equivalent (e.g., `NaN` values). + * + * @param x - input ndarray + * @param options - function options + * @returns output ndarray + * + * @example + * var ndarray2array = require( '@stdlib/ndarray/to-array' ); + * var array = require( '@stdlib/ndarray/array' ); + * + * var x = array( [ -1.0, 2.0, -3.0 ] ); + * + * var y = sorthp( x ); + * // returns + * + * var arr = ndarray2array( y ); + * // returns [ -3.0, -1.0, 2.0 ] + */ + ( x: T, options?: Options ): T; + + /** + * Sorts an input ndarray along one or more ndarray dimensions using heapsort. + * + * ## Notes + * + * - The input ndarray is sorted **in-place** (i.e., the input ndarray is **mutated**). + * - If `sortOrder < 0.0` or is either `'desc'` or `'descending'`, the input ndarray is sorted in **decreasing** order. If `sortOrder > 0.0` or is either `'asc'` or `'ascending'`, the input ndarray is sorted in **increasing** order. If `sortOrder == 0.0`, the input ndarray is left unchanged. + * - The algorithm distinguishes between `-0` and `+0`. When sorted in increasing order, `-0` is sorted before `+0`. When sorted in decreasing order, `-0` is sorted after `+0`. + * - The algorithm sorts `NaN` values to the end. When sorted in increasing order, `NaN` values are sorted last. When sorted in decreasing order, `NaN` values are sorted first. + * - The algorithm has space complexity `O(1)` and time complexity `O(N log2 N)`. + * - The algorithm is **unstable**, meaning that the algorithm may change the order of ndarray elements which are equal or equivalent (e.g., `NaN` values). + * + * @param x - input ndarray + * @param sortOrder - sort order + * @param options - function options + * @returns output ndarray + * + * @example + * var ndarray2array = require( '@stdlib/ndarray/to-array' ); + * var array = require( '@stdlib/ndarray/array' ); + * + * var x = array( [ -1.0, 2.0, -3.0 ] ); + * + * var y = sorthp( x, 1.0 ); + * // returns + * + * var arr = ndarray2array( y ); + * // returns [ -3.0, -1.0, 2.0 ] + */ + ( x: T, sortOrder: SortOrder, options?: Options ): T; +} + +/** +* Sorts an input ndarray along one or more ndarray dimensions using heapsort. +* +* ## Notes +* +* - The input ndarray is sorted **in-place** (i.e., the input ndarray is **mutated**). +* - If `sortOrder < 0.0` or is either `'desc'` or `'descending'`, the input ndarray is sorted in **decreasing** order. If `sortOrder > 0.0` or is either `'asc'` or `'ascending'`, the input ndarray is sorted in **increasing** order. If `sortOrder == 0.0`, the input ndarray is left unchanged. +* - The algorithm distinguishes between `-0` and `+0`. When sorted in increasing order, `-0` is sorted before `+0`. When sorted in decreasing order, `-0` is sorted after `+0`. +* - The algorithm sorts `NaN` values to the end. When sorted in increasing order, `NaN` values are sorted last. When sorted in decreasing order, `NaN` values are sorted first. +* - The algorithm has space complexity `O(1)` and time complexity `O(N log2 N)`. +* - The algorithm is **unstable**, meaning that the algorithm may change the order of ndarray elements which are equal or equivalent (e.g., `NaN` values). +* +* @param x - input ndarray +* @param sortOrder - sort order +* @param options - function options +* @returns output ndarray +* +* @example +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* var array = require( '@stdlib/ndarray/array' ); +* +* var x = array( [ -1.0, 2.0, -3.0 ] ); +* +* var y = sorthp( x, 1.0 ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ -3.0, -1.0, 2.0 ] +* +* @example +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* var array = require( '@stdlib/ndarray/array' ); +* +* var x = array( [ -1.0, 2.0, -3.0 ] ); +* +* var y = sorthp( x, 1.0 ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ -3.0, -1.0, 2.0 ] +*/ +declare const sorthp: Sorthp; + + +// EXPORTS // + +export = sorthp; diff --git a/lib/node_modules/@stdlib/blas/ext/sorthp/docs/types/test.ts b/lib/node_modules/@stdlib/blas/ext/sorthp/docs/types/test.ts new file mode 100644 index 000000000000..62ded8a7235d --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/sorthp/docs/types/test.ts @@ -0,0 +1,151 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2025 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. +*/ + +/* eslint-disable space-in-parens */ + +/// + +import zeros = require( '@stdlib/ndarray/zeros' ); +import sorthp = require( './index' ); + + +// TESTS // + +// The function returns an ndarray... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + sorthp( x ); // $ExpectType float64ndarray + sorthp( x, 1.0 ); // $ExpectType float64ndarray + sorthp( x, {} ); // $ExpectType float64ndarray + sorthp( x, 1.0, {} ); // $ExpectType float64ndarray +} + +// The compiler throws an error if the function is provided a first argument which is not an ndarray... +{ + sorthp( '5' ); // $ExpectError + sorthp( 5 ); // $ExpectError + sorthp( true ); // $ExpectError + sorthp( false ); // $ExpectError + sorthp( null ); // $ExpectError + sorthp( void 0 ); // $ExpectError + sorthp( {} ); // $ExpectError + sorthp( ( x: number ): number => x ); // $ExpectError + + sorthp( '5', 1.0 ); // $ExpectError + sorthp( 5, 1.0 ); // $ExpectError + sorthp( true, 1.0 ); // $ExpectError + sorthp( false, 1.0 ); // $ExpectError + sorthp( null, 1.0 ); // $ExpectError + sorthp( void 0, 1.0 ); // $ExpectError + sorthp( {}, 1.0 ); // $ExpectError + sorthp( ( x: number ): number => x, 1.0 ); // $ExpectError + + sorthp( '5', {} ); // $ExpectError + sorthp( 5, {} ); // $ExpectError + sorthp( true, {} ); // $ExpectError + sorthp( false, {} ); // $ExpectError + sorthp( null, {} ); // $ExpectError + sorthp( void 0, {} ); // $ExpectError + sorthp( {}, {} ); // $ExpectError + sorthp( ( x: number ): number => x, {} ); // $ExpectError + + sorthp( '5', 1.0, {} ); // $ExpectError + sorthp( 5, 1.0, {} ); // $ExpectError + sorthp( true, 1.0, {} ); // $ExpectError + sorthp( false, 1.0, {} ); // $ExpectError + sorthp( null, 1.0, {} ); // $ExpectError + sorthp( void 0, 1.0, {} ); // $ExpectError + sorthp( {}, 1.0, {} ); // $ExpectError + sorthp( ( x: number ): number => x, 1.0, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided a sort order argument which is not an ndarray, supported string literal, or scalar value... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + sorthp( x, true ); // $ExpectError + sorthp( x, false ); // $ExpectError + sorthp( x, [] ); // $ExpectError + sorthp( x, ( x: number ): number => x ); // $ExpectError + + sorthp( x, 'foo', {} ); // $ExpectError + sorthp( x, true, {} ); // $ExpectError + sorthp( x, false, {} ); // $ExpectError + sorthp( x, null, {} ); // $ExpectError + sorthp( x, void 0, {} ); // $ExpectError + sorthp( x, [], {} ); // $ExpectError + sorthp( x, {}, {} ); // $ExpectError + sorthp( x, ( x: number ): number => x, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided a options argument which is not an object... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + sorthp( x, true ); // $ExpectError + sorthp( x, false ); // $ExpectError + sorthp( x, [] ); // $ExpectError + sorthp( x, ( x: number ): number => x ); // $ExpectError + + sorthp( x, 1.0, '5' ); // $ExpectError + sorthp( x, 1.0, true ); // $ExpectError + sorthp( x, 1.0, false ); // $ExpectError + sorthp( x, 1.0, null ); // $ExpectError + sorthp( x, 1.0, [] ); // $ExpectError + sorthp( x, 1.0, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid `dims` option... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + sorthp( x, { 'dims': '5' } ); // $ExpectError + sorthp( x, { 'dims': 5 } ); // $ExpectError + sorthp( x, { 'dims': true } ); // $ExpectError + sorthp( x, { 'dims': false } ); // $ExpectError + sorthp( x, { 'dims': null } ); // $ExpectError + sorthp( x, { 'dims': {} } ); // $ExpectError + sorthp( x, { 'dims': ( x: number ): number => x } ); // $ExpectError + + sorthp( x, 1.0, { 'dims': '5' } ); // $ExpectError + sorthp( x, 1.0, { 'dims': 5 } ); // $ExpectError + sorthp( x, 1.0, { 'dims': true } ); // $ExpectError + sorthp( x, 1.0, { 'dims': false } ); // $ExpectError + sorthp( x, 1.0, { 'dims': null } ); // $ExpectError + sorthp( x, 1.0, { 'dims': {} } ); // $ExpectError + sorthp( x, 1.0, { 'dims': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + sorthp(); // $ExpectError + sorthp( x, 10.0, {}, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/blas/ext/sorthp/examples/index.js b/lib/node_modules/@stdlib/blas/ext/sorthp/examples/index.js new file mode 100644 index 000000000000..cf738a9bfe31 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/sorthp/examples/index.js @@ -0,0 +1,41 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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'; + +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var sorthp = require( './../lib' ); + +// Generate an array of random numbers: +var xbuf = discreteUniform( 25, -20, 20, { + 'dtype': 'generic' +}); + +// Wrap in an ndarray: +var x = new ndarray( 'generic', xbuf, [ 5, 5 ], [ 5, 1 ], 0, 'row-major' ); +console.log( ndarray2array( x ) ); + +// Perform operation: +sorthp( x, { + 'dims': [ 0 ] +}); + +// Print the results: +console.log( ndarray2array( x ) ); diff --git a/lib/node_modules/@stdlib/blas/ext/sorthp/lib/base.js b/lib/node_modules/@stdlib/blas/ext/sorthp/lib/base.js new file mode 100644 index 000000000000..47e1a0d3ca35 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/sorthp/lib/base.js @@ -0,0 +1,109 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 dtypes = require( '@stdlib/ndarray/dtypes' ); +var gsorthp = require( '@stdlib/blas/ext/base/ndarray/gsorthp' ); +var dsorthp = require( '@stdlib/blas/ext/base/ndarray/dsorthp' ); +var ssorthp = require( '@stdlib/blas/ext/base/ndarray/ssorthp' ); +var factory = require( '@stdlib/ndarray/base/nullary-strided1d-dispatch-factory' ); + + +// VARIABLES // + +var idtypes0 = dtypes( 'real_and_generic' ); // input ndarray +var idtypes1 = dtypes( 'real_and_generic' ); // sortOrder ndarray +var odtypes = dtypes( 'real_and_generic' ); +var table = { + 'types': [ + 'float64', // input/output + 'float32' // input/output + ], + 'fcns': [ + dsorthp, + ssorthp + ], + 'default': gsorthp +}; +var options = { + 'strictTraversalOrder': true +}; + + +// MAIN // + +/** +* Sorts an input ndarray along one or more ndarray dimensions using heapsort. +* +* @private +* @name sorthp +* @type {Function} +* @param {ndarray} x - input ndarray +* @param {ndarray} sortOrder - ndarray containing the sort order +* @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} second 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 {Error} must provide valid options +* @returns {ndarray} output ndarray +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* +* // Create a data buffer: +* var xbuf = new Float64Array( [ 1.0, 2.0, -3.0, 4.0, -5.0, 6.0 ] ); +* +* // Define the shape of the input array: +* var sh = [ 3, 1, 2 ]; +* +* // Define the array strides: +* var sx = [ 2, 2, 1 ]; +* +* // Define the index offset: +* var ox = 0; +* +* // Create an input ndarray: +* var x = new ndarray( 'float64', xbuf, sh, sx, ox, 'row-major' ); +* +* // Create an ndarray containing the sort order: +* var sortOrder = scalar2ndarray( 1.0, { +* 'dtype': 'float64' +* }); +* +* // Perform operation: +* var out = sorthp( x, sortOrder ); +* // returns +* +* var arr = ndarray2array( out ); +* // returns [ [ [ -5.0, -3.0 ] ], [ [ 1.0, 2.0 ] ], [ [ 4.0, 6.0 ] ] ] +*/ +var sorthp = factory( table, [ idtypes0, idtypes1 ], odtypes, options ); + + +// EXPORTS // + +module.exports = sorthp; diff --git a/lib/node_modules/@stdlib/blas/ext/sorthp/lib/index.js b/lib/node_modules/@stdlib/blas/ext/sorthp/lib/index.js new file mode 100644 index 000000000000..8123cc15d6a5 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/sorthp/lib/index.js @@ -0,0 +1,64 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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'; + +/** +* Sort an input ndarray along one or more ndarray dimensions using heapsort. +* +* @module @stdlib/blas/ext/sorthp +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* var sorthp = require( '@stdlib/blas/ext/sorthp' ); +* +* // Create a data buffer: +* var xbuf = new Float64Array( [ 1.0, 2.0, -3.0, 4.0, -5.0, 6.0 ] ); +* +* // Define the shape of the input array: +* var sh = [ 3, 1, 2 ]; +* +* // Define the array strides: +* var sx = [ 2, 2, 1 ]; +* +* // Define the index offset: +* var ox = 0; +* +* // Create an input ndarray: +* var x = new ndarray( 'float64', xbuf, sh, sx, ox, 'row-major' ); +* +* // Perform operation: +* var out = sorthp( x ); +* // returns +* +* var arr = ndarray2array( out ); +* // returns [ [ [ -5.0, -3.0 ] ], [ [ 1.0, 2.0 ] ], [ [ 4.0, 6.0 ] ] ] +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; + +// exports: { "assign": "main.assign" } diff --git a/lib/node_modules/@stdlib/blas/ext/sorthp/lib/main.js b/lib/node_modules/@stdlib/blas/ext/sorthp/lib/main.js new file mode 100644 index 000000000000..f244d4e74c35 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/sorthp/lib/main.js @@ -0,0 +1,221 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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. +*/ + +/* eslint-disable max-len */ + +'use strict'; + +// MODULES // + +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +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 isRealFloatingDataType = require( '@stdlib/ndarray/base/assert/is-real-floating-point-data-type' ); +var isSignedIntegerDataType = require( '@stdlib/ndarray/base/assert/is-signed-integer-data-type' ); +var broadcastScalar = require( '@stdlib/ndarray/base/broadcast-scalar' ); +var maybeBroadcastArray = require( '@stdlib/ndarray/base/maybe-broadcast-array' ); +var getDType = require( '@stdlib/ndarray/dtype' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var getOrder = require( '@stdlib/ndarray/order' ); +var format = require( '@stdlib/string/format' ); +var nonCoreShape = require( './non_core_shape.js' ); +var base = require( './base.js' ); + + +// FUNCTIONS // + +/** +* Returns a boolean indicating if a value is a string literal specifying ascending sort order. +* +* @private +* @param {*} value - input value +* @returns {boolean} boolean result +*/ +function isAscending( value ) { + return ( value === 'asc' || value === 'ascending' ); +} + +/** +* Returns a boolean indicating if a value is a string literal specifying descending sort order. +* +* @private +* @param {*} value - input value +* @returns {boolean} boolean result +*/ +function isDescending( value ) { + return ( value === 'desc' || value === 'descending' ); +} + +/** +* Converts a string literal to a numeric sort order value. +* +* @private +* @param {string} value - input value +* @throws {TypeError} must provide a supported string +* @returns {number} sort order +*/ +function string2order( value ) { + if ( isAscending( value ) ) { + return 1; + } + if ( isDescending( value ) ) { + return -1; + } + throw new TypeError( format( 'invalid argument. Second argument must be a valid sort order. Value: `%s`.', value ) ); +} + +/** +* Normalize a numeric sort order value. +* +* ## Notes +* +* - Normalizing numeric sort order values to canonical values `-1`, `+1`, and `0` ensures that we can avoid truncation rounding errors when casting a provided sort order to the data type of the input ndarray. +* +* @private +* @param {number} value - input value +* @returns {number} normalized value +*/ +function normalizeOrder( value ) { + if ( value < 0 ) { + return -1; + } + if ( value > 0 ) { + return 1; + } + return value; +} + + +// MAIN // + +/** +* Sorts an input ndarray along one or more ndarray dimensions using heapsort. +* +* @param {ndarrayLike} x - input ndarray +* @param {(ndarrayLike|number|string)} [sortOrder=1.0] - sort order +* @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} 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 {Error} must provide valid options +* @returns {ndarray} output ndarray +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* +* // Create a data buffer: +* var xbuf = new Float64Array( [ 1.0, 2.0, -3.0, 4.0, -5.0, 6.0 ] ); +* +* // Define the shape of the input array: +* var sh = [ 3, 1, 2 ]; +* +* // Define the array strides: +* var sx = [ 2, 2, 1 ]; +* +* // Define the index offset: +* var ox = 0; +* +* // Create an input ndarray: +* var x = new ndarray( 'float64', xbuf, sh, sx, ox, 'row-major' ); +* +* // Perform operation: +* var out = sorthp( x ); +* // returns +* +* var arr = ndarray2array( out ); +* // returns [ [ [ -5.0, -3.0 ] ], [ [ 1.0, 2.0 ] ], [ [ 4.0, 6.0 ] ] ] +*/ +function sorthp( x ) { + var nargs; + var isStr; + var opts; + var ord; + var dt; + var sh; + var o; + + nargs = arguments.length; + + // Resolve input ndarray meta data: + dt = getDType( x ); + if ( !isRealFloatingDataType( dt ) && !isSignedIntegerDataType( dt ) ) { + // Fallback to "generic" only if we cannot safely cast `-1` to the data type of the input ndarray: + dt = 'generic'; + } + ord = getOrder( x ); + + // Case: sorthp( x ) + if ( nargs < 2 ) { + return base( x, broadcastScalar( 1, dt, [], ord ) ); + } + o = arguments[ 1 ]; + + // Case: sorthp( x, ??? ) + if ( nargs === 2 ) { + // Case: sorthp( x, sortOrder_scalar || sortOrder_string ) + isStr = isString( o ); + if ( isStr || isNumber( o ) ) { + return base( x, broadcastScalar( ( isStr ) ? string2order( o ) : normalizeOrder( o ), dt, [], ord ) ); + } + // Case: sorthp( x, sortOrder_ndarray ) + if ( isndarrayLike( o ) ) { + // As the operation is performed across all dimensions, `o` is assumed to be a zero-dimensional ndarray... + return base( x, o ); + } + // Case: sorthp( x, opts ) + opts = o; + o = 1; + + // Intentionally fall through... + } + // Case: sorthp( x, sortOrder, opts ) + else { // nargs > 2 + opts = arguments[ 2 ]; + } + // Case: sorthp( x, sortOrder_scalar || sortOrder_string, opts ) + isStr = isString( o ); + if ( isStr || isNumber( o ) ) { + if ( hasOwnProp( opts, 'dims' ) ) { + sh = nonCoreShape( getShape( x ), opts.dims ); + } else { + sh = []; + } + o = broadcastScalar( ( isStr ) ? string2order( o ) : normalizeOrder( o ), dt, sh, getOrder( x ) ); + } + // Case: sorthp( x, sortOrder_ndarray, opts ) + else if ( isndarrayLike( o ) ) { + // When not provided `dims`, the operation is performed across all dimensions and `o` is assumed to be a zero-dimensional ndarray; when `dims` is provided, we need to broadcast `o` to match the shape of the non-core dimensions... + if ( hasOwnProp( opts, 'dims' ) ) { + o = maybeBroadcastArray( o, nonCoreShape( getShape( x ), opts.dims ) ); + } + } else { + throw new TypeError( format( 'invalid argument. Second argument must be either an ndarray, a numeric scalar value, or a supported string. Value: `%s`.', o ) ); + } + return base( x, o, opts ); +} + + +// EXPORTS // + +module.exports = sorthp; diff --git a/lib/node_modules/@stdlib/blas/ext/sorthp/lib/non_core_shape.js b/lib/node_modules/@stdlib/blas/ext/sorthp/lib/non_core_shape.js new file mode 100644 index 000000000000..07e99731e89f --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/sorthp/lib/non_core_shape.js @@ -0,0 +1,50 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 normalizeIndices = require( '@stdlib/ndarray/base/to-unique-normalized-indices' ); +var indicesComplement = require( '@stdlib/array/base/indices-complement' ); +var takeIndexed = require( '@stdlib/array/base/take-indexed' ); + + +// MAIN // + +/** +* Returns the shape defined by the dimensions which are **not** included in a list of dimensions. +* +* @private +* @param {NonNegativeIntegerArray} shape - input ndarray +* @param {IntegerArray} dims - list of dimensions +* @returns {NonNegativeIntegerArray} shape +*/ +function nonCoreShape( shape, dims ) { // TODO: consider moving to a `@stdlib/ndarray/base` utility + var ind = normalizeIndices( dims, shape.length-1 ); + if ( ind === null ) { + // Note: this is an error condition, as `null` is returned when provided out-of-bounds indices... + return []; + } + return takeIndexed( shape, indicesComplement( shape.length, ind ) ); +} + + +// EXPORTS // + +module.exports = nonCoreShape; diff --git a/lib/node_modules/@stdlib/blas/ext/sorthp/package.json b/lib/node_modules/@stdlib/blas/ext/sorthp/package.json new file mode 100644 index 000000000000..8bb791530abb --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/sorthp/package.json @@ -0,0 +1,63 @@ +{ + "name": "@stdlib/blas/ext/sorthp", + "version": "0.0.0", + "description": "Sorth an input ndarray along one or more ndarray dimensions using heapsort.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdmath", + "statistics", + "stats", + "mathematics", + "math", + "arrange", + "sort", + "ndarray" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/blas/ext/sorthp/test/test.js b/lib/node_modules/@stdlib/blas/ext/sorthp/test/test.js new file mode 100644 index 000000000000..e336c3c9e1bc --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/sorthp/test/test.js @@ -0,0 +1,1479 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 tape = require( 'tape' ); +var isSameArray = require( '@stdlib/assert/is-same-array' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var empty = require( '@stdlib/ndarray/empty' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var getDType = require( '@stdlib/ndarray/dtype' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var getOrder = require( '@stdlib/ndarray/order' ); +var getData = require( '@stdlib/ndarray/data-buffer' ); +var sorthp = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof sorthp, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + sorthp( value ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object (scalar sort order)', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + sorthp( value, 1.0 ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object (ndarray sort order)', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + sorthp( value, scalar2ndarray( 1.0 ) ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object (options)', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + sorthp( value, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object (scalar sort order, options)', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + sorthp( value, 1.0, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object (ndarray sort order, options)', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + sorthp( value, scalar2ndarray( 1.0 ), {} ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object having a supported data type', function test( t ) { + var values; + var i; + + values = [ + empty( [ 2, 2 ], { + 'dtype': 'bool' + }) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + sorthp( value ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object having a supported data type (scalar sort order)', function test( t ) { + var values; + var i; + + values = [ + empty( [ 2, 2 ], { + 'dtype': 'bool' + }) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + sorthp( value, 1.0 ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object having a supported data type (ndarray sort order)', function test( t ) { + var values; + var i; + + values = [ + empty( [ 2, 2 ], { + 'dtype': 'bool' + }) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + sorthp( value, scalar2ndarray( 1.0 ) ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object having a supported data type (options)', function test( t ) { + var values; + var i; + + values = [ + empty( [ 2, 2 ], { + 'dtype': 'bool' + }) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + sorthp( value, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object having a supported data type (scalar sort order, options)', function test( t ) { + var values; + var i; + + values = [ + empty( [ 2, 2 ], { + 'dtype': 'bool' + }) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + sorthp( value, 1.0, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object having a supported data type (ndarray sort order, options)', function test( t ) { + var values; + var i; + + values = [ + empty( [ 2, 2 ], { + 'dtype': 'bool' + }) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + sorthp( value, scalar2ndarray( 1.0 ), {} ); + }; + } +}); + +tape( 'the function throws an error if provided a `sortOrder` argument which is not an ndarray-like object, a numeric scalar, or a support string', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + 'invalid', + true, + false, + null, + void 0, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + sorthp( x, value ); + }; + } +}); + +tape( 'the function throws an error if provided a `sortOrder` argument which is not an ndarray-like object, a numeric scalar, or a support string (options)', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + 'invalid', + true, + false, + null, + void 0, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + sorthp( x, value, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a `sortOrder` argument which is not broadcast-compatible', function test( t ) { + var values; + var opts; + var x; + var i; + + opts = { + 'dtype': 'generic' + }; + x = zeros( [ 2, 2 ], opts ); + + values = [ + zeros( [ 4 ], opts ), + zeros( [ 2, 2, 2 ], opts ), + zeros( [ 0 ], opts ) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + sorthp( x, value ); + }; + } +}); + +tape( 'the function throws an error if provided a `sortOrder` argument which is not broadcast-compatible (options)', function test( t ) { + var values; + var opts; + var x; + var i; + + opts = { + 'dtype': 'generic' + }; + x = zeros( [ 2, 2 ], opts ); + + values = [ + zeros( [ 4 ], opts ), + zeros( [ 2, 2, 2 ], opts ), + zeros( [ 0 ], opts ) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + sorthp( x, value, {} ); + }; + } +}); + +tape( 'the function throws an error if provided an options argument which is not an object', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + true, + false, + null, + void 0, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + sorthp( x, value ); + }; + } +}); + +tape( 'the function throws an error if provided an options argument which is not an object (scalar sort order)', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + sorthp( x, 1.0, value ); + }; + } +}); + +tape( 'the function throws an error if provided an options argument which is not an object (ndarray sort order)', function test( t ) { + var values; + var opts; + var x; + var i; + + opts = { + 'dtype': 'generic' + }; + x = zeros( [ 2, 2 ], opts ); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + sorthp( x, scalar2ndarray( 1.0, opts ), value ); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which is not an array-like object of integers', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [ 'a' ], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + sorthp( x, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which is not an array-like object of integers (scalar sort order)', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [ 'a' ], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + sorthp( x, 1.0, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which is not an array-like object of integers (ndarray sort order)', function test( t ) { + var values; + var opts; + var x; + var i; + + opts = { + 'dtype': 'generic' + }; + x = zeros( [ 2, 2 ], opts ); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [ 'a' ], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + sorthp( x, scalar2ndarray( 1.0, opts ), { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which contains out-of-bounds indices', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + [ -10 ], + [ 0, 20 ], + [ 20 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + sorthp( x, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which contains out-of-bounds indices (scalar sort order)', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + [ -10 ], + [ 0, 20 ], + [ 20 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + sorthp( x, 1.0, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which contains out-of-bounds indices (ndarray sort order)', function test( t ) { + var values; + var opts; + var x; + var i; + + opts = { + 'dtype': 'generic' + }; + x = zeros( [ 2, 2 ], opts ); + + values = [ + [ -10 ], + [ 0, 20 ], + [ 20 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + sorthp( x, scalar2ndarray( 1.0, opts ), { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which contains too many indices', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + [ 0, 1, 2 ], + [ 0, 1, 2, 3 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + sorthp( x, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which contains too many indices (scalar sort order)', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + [ 0, 1, 2 ], + [ 0, 1, 2, 3 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + sorthp( x, 1.0, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which contains too many indices (ndarray sort order)', function test( t ) { + var values; + var opts; + var x; + var i; + + opts = { + 'dtype': 'generic' + }; + x = zeros( [ 2, 2 ], opts ); + + values = [ + [ 0, 1, 2 ], + [ 0, 1, 2, 3 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + sorthp( x, scalar2ndarray( 1.0, opts ), { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which contains duplicate indices', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + [ 0, 0 ], + [ 1, 1 ], + [ 0, 1, 0 ], + [ 1, 0, 1 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + sorthp( x, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which contains duplicate indices (scalar sort order)', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + [ 0, 0 ], + [ 1, 1 ], + [ 0, 1, 0 ], + [ 1, 0, 1 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + sorthp( x, 1.0, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which contains duplicate indices (ndarray sort order)', function test( t ) { + var values; + var opts; + var x; + var i; + + opts = { + 'dtype': 'generic' + }; + x = zeros( [ 2, 2 ], opts ); + + values = [ + [ 0, 0 ], + [ 1, 1 ], + [ 0, 1, 0 ], + [ 1, 0, 1 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + sorthp( x, scalar2ndarray( 1.0, opts ), { + 'dims': value + }); + }; + } +}); + +tape( 'the function sorts elements in an input ndarray (default, row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = sorthp( x ); + expected = [ -3.0, -1.0, 2.0, 4.0 ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( getDType( actual ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), getShape( x ), 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( isSameArray( getData( actual ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function sorts elements in an input ndarray (default, column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = sorthp( x ); + expected = [ -3.0, -1.0, 2.0, 4.0 ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( getDType( actual ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), getShape( x ), 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( isSameArray( getData( actual ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function sorts elements in an input ndarray (all dimensions, row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = sorthp( x, { + 'dims': [ 0, 1 ] + }); + expected = [ -3.0, -1.0, 2.0, 4.0 ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( getDType( actual ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), getShape( x ), 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( isSameArray( getData( actual ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function sorts elements in an input ndarray (all dimensions, column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = sorthp( x, { + 'dims': [ 0, 1 ] + }); + expected = [ -3.0, -1.0, 2.0, 4.0 ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( getDType( actual ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), getShape( x ), 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( isSameArray( getData( actual ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function sorts elements in an input ndarray (no dimensions, row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = sorthp( x, { + 'dims': [] + }); + expected = [ [ -1.0, 2.0 ], [ -3.0, 4.0 ] ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( getDType( actual ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), getShape( x ), 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function sorts elements in an input ndarray (no dimensions, column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = sorthp( x, { + 'dims': [] + }); + expected = [ [ -1.0, -3.0 ], [ 2.0, 4.0 ] ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( getDType( actual ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), getShape( x ), 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying operation dimensions (row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = sorthp( x, { + 'dims': [ 0 ] + }); + expected = [ [ -3.0, 2.0 ], [ -1.0, 4.0 ] ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( getDType( actual ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), getShape( x ), 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = sorthp( x, { + 'dims': [ 1 ] + }); + expected = [ [ -1.0, 2.0 ], [ -3.0, 4.0 ] ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( getDType( actual ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), getShape( x ), 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying operation dimensions (column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = sorthp( x, { + 'dims': [ 0 ] + }); + expected = [ [ -1.0, -3.0 ], [ 2.0, 4.0 ] ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( getDType( actual ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), getShape( x ), 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = sorthp( x, { + 'dims': [ 1 ] + }); + expected = [ [ -3.0, -1.0 ], [ 2.0, 4.0 ] ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( getDType( actual ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), getShape( x ), 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports providing a `sortOrder` argument (scalar)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = sorthp( x, 1.0 ); + expected = [ -3.0, -1.0, 2.0, 4.0 ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( getDType( actual ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), getShape( x ), 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( isSameArray( getData( actual ), expected ), true, 'returns expected value' ); + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'column-major' ); + + actual = sorthp( x, -1.0 ); + expected = [ 4.0, 2.0, -1.0, -3.0 ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( getDType( actual ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), getShape( x ), 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( isSameArray( getData( actual ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports providing a `sortOrder` argument (scalar, options)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = sorthp( x, 1.0, {} ); + expected = [ -3.0, -1.0, 2.0, 4.0 ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( getDType( actual ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), getShape( x ), 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( isSameArray( getData( actual ), expected ), true, 'returns expected value' ); + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'column-major' ); + + actual = sorthp( x, -1.0, {} ); + expected = [ 4.0, 2.0, -1.0, -3.0 ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( getDType( actual ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), getShape( x ), 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( isSameArray( getData( actual ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports providing a `sortOrder` argument (0d ndarray)', function test( t ) { + var expected; + var actual; + var xbuf; + var opts; + var x; + + opts = { + 'dtype': 'generic' + }; + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( opts.dtype, xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = sorthp( x, scalar2ndarray( 1.0, opts ) ); + expected = [-3.0, -1.0, 2.0, 4.0 ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( getDType( actual ), opts.dtype, 'returns expected value' ); + t.deepEqual( getShape( actual ), getShape( x ), 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( isSameArray( getData( actual ), expected ), true, 'returns expected value' ); + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( opts.dtype, xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'column-major' ); + + actual = sorthp( x, scalar2ndarray( -1.0, opts ) ); + expected = [ 4.0, 2.0, -1.0, -3.0 ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( getDType( actual ), opts.dtype, 'returns expected value' ); + t.deepEqual( getShape( actual ), getShape( x ), 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( isSameArray( getData( actual ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports providing a `sortOrder` argument (0d ndarray, options)', function test( t ) { + var expected; + var actual; + var xbuf; + var opts; + var x; + + opts = { + 'dtype': 'generic' + }; + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( opts.dtype, xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = sorthp( x, scalar2ndarray( 1.0, opts ), {} ); + expected = [ -3.0, -1.0, 2.0, 4.0 ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( getDType( actual ), opts.dtype, 'returns expected value' ); + t.deepEqual( getShape( actual ), getShape( x ), 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( isSameArray( getData( actual ), expected ), true, 'returns expected value' ); + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( opts.dtype, xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'column-major' ); + + actual = sorthp( x, scalar2ndarray( -1.0, opts ), {} ); + expected = [ 4.0, 2.0, -1.0, -3.0 ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( getDType( actual ), opts.dtype, 'returns expected value' ); + t.deepEqual( getShape( actual ), getShape( x ), 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( isSameArray( getData( actual ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports providing a `sortOrder` argument (scalar, broadcasted)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = sorthp( x, 1.0, { + 'dims': [ 0 ] + }); + expected = [ [ -3.0, 2.0 ], [ -1.0, 4.0 ] ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( getDType( actual ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), getShape( x ), 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = sorthp( x, -1.0, { + 'dims': [ 0 ] + }); + expected = [ [ 2.0, 4.0 ], [ -1.0, -3.0 ] ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( getDType( actual ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), getShape( x ), 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports providing a `sortOrder` argument (0d ndarray, broadcasted)', function test( t ) { + var expected; + var actual; + var xbuf; + var opts; + var x; + + opts = { + 'dtype': 'generic' + }; + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( opts.dtype, xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = sorthp( x, scalar2ndarray( 1.0, opts ), { + 'dims': [ 0 ] + }); + expected = [ [ -3.0, 2.0 ], [ -1.0, 4.0 ] ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( getDType( actual ), opts.dtype, 'returns expected value' ); + t.deepEqual( getShape( actual ), getShape( x ), 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( opts.dtype, xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = sorthp( x, scalar2ndarray( -1.0, opts ), { + 'dims': [ 0 ] + }); + expected = [ [ 2.0, 4.0 ], [ -1.0, -3.0 ] ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( getDType( actual ), opts.dtype, 'returns expected value' ); + t.deepEqual( getShape( actual ), getShape( x ), 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports providing a `sortOrder` argument (string literals)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = sorthp( x, 'asc' ); + expected = [ -3.0, -1.0, 2.0, 4.0 ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( getDType( actual ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), getShape( x ), 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( isSameArray( getData( actual ), expected ), true, 'returns expected value' ); + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = sorthp( x, 'ascending' ); + expected = [ -3.0, -1.0, 2.0, 4.0 ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( getDType( actual ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), getShape( x ), 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( isSameArray( getData( actual ), expected ), true, 'returns expected value' ); + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = sorthp( x, 'desc' ); + expected = [ 4.0, 2.0, -1.0, -3.0 ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( getDType( actual ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), getShape( x ), 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( isSameArray( getData( actual ), expected ), true, 'returns expected value' ); + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = sorthp( x, 'descending' ); + expected = [ 4.0, 2.0, -1.0, -3.0 ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( getDType( actual ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), getShape( x ), 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( isSameArray( getData( actual ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports providing a `sortOrder` argument (string literals, options)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = sorthp( x, 'asc', {} ); + expected = [ -3.0, -1.0, 2.0, 4.0 ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( getDType( actual ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), getShape( x ), 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( isSameArray( getData( actual ), expected ), true, 'returns expected value' ); + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = sorthp( x, 'descending', { + 'dims': [ 0 ] + }); + expected = [ [ -1.0, 4.0 ], [ -3.0, 2.0 ] ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( getDType( actual ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), getShape( x ), 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports providing a `sortOrder` argument (ndarray)', function test( t ) { + var sortOrder; + var expected; + var actual; + var xbuf; + var obuf; + var opts; + var x; + + opts = { + 'dtype': 'generic' + }; + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( opts.dtype, xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + obuf = [ 1.0, -1.0 ]; + sortOrder = new ndarray( opts.dtype, obuf, [ 2 ], [ 1 ], 0, 'row-major' ); + actual = sorthp( x, sortOrder, { + 'dims': [ 0 ] + }); + expected = [ [ -3.0, 4.0 ], [ -1.0, 2.0 ] ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( getDType( actual ), opts.dtype, 'returns expected value' ); + t.deepEqual( getShape( actual ), getShape( x ), 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ -1.0, 2.0, -3.0, 4.0 ]; + x = new ndarray( opts.dtype, xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + obuf = [ 1.0, -1.0 ]; + sortOrder = new ndarray( opts.dtype, obuf, [ 2 ], [ 1 ], 0, 'row-major' ); + actual = sorthp( x, sortOrder, { + 'dims': [ 0 ] + }); + expected = [ [ -1.0, 4.0 ], [ 2.0, -3.0 ] ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( getDType( actual ), opts.dtype, 'returns expected value' ); + t.deepEqual( getShape( actual ), getShape( x ), 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +});