Skip to content

Commit de15f8b

Browse files
committed
feat: add blas/ext/sorthp
1 parent ab2e77b commit de15f8b

File tree

12 files changed

+2451
-0
lines changed

12 files changed

+2451
-0
lines changed
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# sorthp
22+
23+
> Sort an input [ndarray][@stdlib/ndarray/ctor] along one or more [ndarray][@stdlib/ndarray/ctor] dimensions using heapsort.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var sorthp = require( '@stdlib/blas/ext/sorthp' );
31+
```
32+
33+
#### sorthp( x\[, sortOrder]\[, options] )
34+
35+
Sorts an input [ndarray][@stdlib/ndarray/ctor] along one or more [ndarray][@stdlib/ndarray/ctor] dimensions using heapsort.
36+
37+
```javascript
38+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
39+
var array = require( '@stdlib/ndarray/array' );
40+
41+
var x = array( [ -1.0, 2.0, -3.0 ] );
42+
43+
var y = sorthp( x );
44+
// returns <ndarray>
45+
46+
var arr = ndarray2array( y );
47+
// returns [ -3.0, -1.0, 2.0 ]
48+
49+
var bool = ( x === y );
50+
// returns true
51+
```
52+
53+
The function has the following parameters:
54+
55+
- **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have a numeric or "generic" [data type][@stdlib/ndarray/dtypes].
56+
- **sortOrder**: sort order (_optional_). May be either a scalar value or an [ndarray][@stdlib/ndarray/ctor] having a real 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).
57+
- **options**: function options (_optional_).
58+
59+
The function accepts the following options:
60+
61+
- **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].
62+
63+
By default, the function uses an increasing sort order. To sort in a different order, provide a `sortOrder` argument.
64+
65+
```javascript
66+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
67+
var array = require( '@stdlib/ndarray/array' );
68+
69+
var x = array( [ -1.0, 2.0, -3.0 ] );
70+
71+
var y = sorthp( x, -1.0 );
72+
// returns <ndarray>
73+
74+
var arr = ndarray2array( y );
75+
// returns [ 2.0, -1.0, -3.0 ]
76+
```
77+
78+
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.
79+
80+
```javascript
81+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
82+
var array = require( '@stdlib/ndarray/array' );
83+
84+
var x = array( [ -1.0, 2.0, -3.0, 4.0 ], {
85+
'shape': [ 2, 2 ],
86+
'order': 'row-major'
87+
});
88+
89+
var v = ndarray2array( x );
90+
// returns [ [ -1.0, 2.0 ], [ -3.0, 4.0 ] ]
91+
92+
var y = sorthp( x, {
93+
'dims': [ 0 ]
94+
});
95+
// returns <ndarray>
96+
97+
v = ndarray2array( y );
98+
// returns [ [ -3.0, 2.0 ], [ -1.0, 4.0 ] ]
99+
```
100+
101+
</section>
102+
103+
<!-- /.usage -->
104+
105+
<section class="notes">
106+
107+
## Notes
108+
109+
- The input [ndarray][@stdlib/ndarray/ctor] is sorted **in-place** (i.e., the input [ndarray][@stdlib/ndarray/ctor] is **mutated**).
110+
- If `sortOrder < 0.0`, the input [ndarray][@stdlib/ndarray/ctor] is sorted in **decreasing** order. If `sortOrder > 0.0`, the input [ndarray][@stdlib/ndarray/ctor] is sorted in **increasing** order. If `sortOrder == 0.0`, the input [ndarray][@stdlib/ndarray/ctor] is left unchanged.
111+
- 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.
112+
113+
</section>
114+
115+
<!-- /.notes -->
116+
117+
<section class="examples">
118+
119+
## Examples
120+
121+
<!-- eslint no-undef: "error" -->
122+
123+
```javascript
124+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
125+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
126+
var ndarray = require( '@stdlib/ndarray/ctor' );
127+
var sorthp = require( '@stdlib/blas/ext/sorthp' );
128+
129+
// Generate an array of random numbers:
130+
var xbuf = discreteUniform( 25, -20, 20, {
131+
'dtype': 'generic'
132+
});
133+
134+
// Wrap in an ndarray:
135+
var x = new ndarray( 'generic', xbuf, [ 5, 5 ], [ 5, 1 ], 0, 'row-major' );
136+
console.log( ndarray2array( x ) );
137+
138+
// Perform operation:
139+
sorthp( x, {
140+
'dims': [ 0 ]
141+
});
142+
143+
// Print the results:
144+
console.log( ndarray2array( x ) );
145+
```
146+
147+
</section>
148+
149+
<!-- /.examples -->
150+
151+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
152+
153+
<section class="related">
154+
155+
</section>
156+
157+
<!-- /.related -->
158+
159+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
160+
161+
<section class="links">
162+
163+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
164+
165+
[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes
166+
167+
[@stdlib/ndarray/base/broadcast-shapes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/broadcast-shapes
168+
169+
</section>
170+
171+
<!-- /.links -->
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var pow = require( '@stdlib/math/base/special/pow' );
26+
var uniform = require( '@stdlib/random/array/uniform' );
27+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
28+
var pkg = require( './../package.json' ).name;
29+
var sorthp = require( './../lib' );
30+
31+
32+
// VARIABLES //
33+
34+
var options = {
35+
'dtype': 'float64'
36+
};
37+
38+
39+
// FUNCTIONS //
40+
41+
/**
42+
* Creates a benchmark function.
43+
*
44+
* @private
45+
* @param {PositiveInteger} len - array length
46+
* @returns {Function} benchmark function
47+
*/
48+
function createBenchmark( len ) {
49+
var x = uniform( len, -50.0, 50.0, options );
50+
x = new ndarray( options.dtype, x, [ len ], [ 1 ], 0, 'row-major' );
51+
52+
return benchmark;
53+
54+
/**
55+
* Benchmark function.
56+
*
57+
* @private
58+
* @param {Benchmark} b - benchmark instance
59+
*/
60+
function benchmark( b ) {
61+
var o;
62+
var i;
63+
64+
b.tic();
65+
for ( i = 0; i < b.iterations; i++ ) {
66+
o = sorthp( x );
67+
if ( typeof o !== 'object' ) {
68+
b.fail( 'should return an ndarray' );
69+
}
70+
}
71+
b.toc();
72+
if ( isnan( o.get( i%len ) ) ) {
73+
b.fail( 'should not return NaN' );
74+
}
75+
b.pass( 'benchmark finished' );
76+
b.end();
77+
}
78+
}
79+
80+
81+
// MAIN //
82+
83+
/**
84+
* Main execution sequence.
85+
*
86+
* @private
87+
*/
88+
function main() {
89+
var len;
90+
var min;
91+
var max;
92+
var f;
93+
var i;
94+
95+
min = 1; // 10^min
96+
max = 6; // 10^max
97+
98+
for ( i = min; i <= max; i++ ) {
99+
len = pow( 10, i );
100+
f = createBenchmark( len );
101+
bench( pkg+':dtype='+options.dtype+',len='+len, f );
102+
}
103+
}
104+
105+
main();
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
{{alias}}( x[, sortOrder][, options] )
3+
Sorts an input ndarray along one or more ndarray dimensions using heapsort.
4+
5+
Parameters
6+
----------
7+
x: ndarray
8+
Input array. Must have a numeric or "generic" data type.
9+
10+
sortOrder: ndarray|number (optional)
11+
Sort order. May be either a scalar value or an ndarray having a real or
12+
"generic" data type. If provided an ndarray, the value must have a shape
13+
which is broadcast compatible with the complement of the shape defined
14+
by `options.dims`. For example, given the input shape `[2, 3, 4]` and
15+
`options.dims=[0]`, an ndarray sort order must have a shape which is
16+
broadcast compatible with the shape `[3, 4]`. Similarly, when performing
17+
the operation over all elements in a provided input ndarray, an ndarray
18+
sort order must be a zero-dimensional ndarray. By default, the sort
19+
order is `1` (i.e., increasing order).
20+
21+
options: Object (optional)
22+
Function options.
23+
24+
options.dims: Array<integer> (optional)
25+
List of dimensions over which to perform operation. If not provided, the
26+
function performs the operation over all elements in a provided input
27+
ndarray.
28+
29+
Returns
30+
-------
31+
out: ndarray
32+
Output array.
33+
34+
Examples
35+
--------
36+
> var x = {{alias:@stdlib/ndarray/array}}( [ -1.0, 2.0, -3.0, -4.0 ] );
37+
> var y = {{alias}}( x );
38+
> {{alias:@stdlib/ndarray/to-array}}( y )
39+
[ -4.0, -3.0, -1.0, 2.0 ]
40+
41+
See Also
42+
--------

0 commit comments

Comments
 (0)