Skip to content

Commit 04cea8e

Browse files
committed
feat: add blas/ext/to-sortedhp
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent 76a39f5 commit 04cea8e

File tree

14 files changed

+5445
-0
lines changed

14 files changed

+5445
-0
lines changed
Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
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+
# toSortedhp
22+
23+
> Return a new [ndarray][@stdlib/ndarray/ctor] containing the elements of an input [ndarray][@stdlib/ndarray/ctor] sorted along one or more [ndarray][@stdlib/ndarray/ctor] dimensions using heapsort.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var toSortedhp = require( '@stdlib/blas/ext/to-sortedhp' );
31+
```
32+
33+
#### toSortedhp( x\[, sortOrder]\[, options] )
34+
35+
Returns a new [ndarray][@stdlib/ndarray/ctor] containing the elements of an input [ndarray][@stdlib/ndarray/ctor] sorted 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 = toSortedhp( x );
44+
// returns <ndarray>
45+
46+
var arr = ndarray2array( y );
47+
// returns [ -3.0, -1.0, 2.0 ]
48+
```
49+
50+
The function has the following parameters:
51+
52+
- **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have a real-valued or "generic" [data type][@stdlib/ndarray/dtypes].
53+
- **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).
54+
- **options**: function options (_optional_).
55+
56+
The function accepts the following options:
57+
58+
- **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].
59+
60+
By default, the function sorts elements in increasing order. To sort in a different order, provide a `sortOrder` argument.
61+
62+
```javascript
63+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
64+
var array = require( '@stdlib/ndarray/array' );
65+
66+
var x = array( [ -1.0, 2.0, -3.0 ] );
67+
68+
var y = toSortedhp( x, -1.0 );
69+
// returns <ndarray>
70+
71+
var arr = ndarray2array( y );
72+
// returns [ 2.0, -1.0, -3.0 ]
73+
```
74+
75+
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.
76+
77+
```javascript
78+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
79+
var array = require( '@stdlib/ndarray/array' );
80+
81+
var x = array( [ -1.0, 2.0, -3.0 ] );
82+
83+
// Sort in ascending order:
84+
var y = toSortedhp( x, 'asc' );
85+
// returns <ndarray>
86+
87+
var arr = ndarray2array( y );
88+
// returns [ -3.0, -1.0, 2.0 ]
89+
90+
// Sort in descending order:
91+
y = toSortedhp( x, 'descending' );
92+
// returns <ndarray>
93+
94+
arr = ndarray2array( y );
95+
// returns [ 2.0, -1.0, -3.0 ]
96+
```
97+
98+
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.
99+
100+
```javascript
101+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
102+
var array = require( '@stdlib/ndarray/array' );
103+
104+
var x = array( [ -1.0, 2.0, -3.0, 4.0 ], {
105+
'shape': [ 2, 2 ],
106+
'order': 'row-major'
107+
});
108+
109+
var v = ndarray2array( x );
110+
// returns [ [ -1.0, 2.0 ], [ -3.0, 4.0 ] ]
111+
112+
var y = toSortedhp( x, {
113+
'dims': [ 0 ]
114+
});
115+
// returns <ndarray>
116+
117+
v = ndarray2array( y );
118+
// returns [ [ -3.0, 2.0 ], [ -1.0, 4.0 ] ]
119+
```
120+
121+
#### toSortedhp.assign( x, y\[, sortOrder]\[, options] )
122+
123+
Sorts the elements of an input [ndarray][@stdlib/ndarray/ctor] along one or more [ndarray][@stdlib/ndarray/ctor] dimensions using heapsort and assigns the results to an output [ndarray][@stdlib/ndarray/ctor].
124+
125+
```javascript
126+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
127+
var zeros = require( '@stdlib/ndarray/zeros' );
128+
var array = require( '@stdlib/ndarray/array' );
129+
130+
var x = array( [ -1.0, 2.0, -3.0 ] );
131+
var y = zeros( [ 3 ] );
132+
133+
var out = toSortedhp.assign( x, y );
134+
// returns <ndarray>
135+
136+
var arr = ndarray2array( out );
137+
// returns [ -3.0, -1.0, 2.0 ]
138+
139+
var bool = ( y === out );
140+
// returns true
141+
```
142+
143+
The function has the following parameters:
144+
145+
- **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have a real-valued or "generic" [data type][@stdlib/ndarray/dtypes].
146+
- **y**: output [ndarray][@stdlib/ndarray/ctor]. Must have a real-valued or "generic" [data type][@stdlib/ndarray/dtypes].
147+
- **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).
148+
- **options**: function options (_optional_).
149+
150+
The function accepts the following options:
151+
152+
- **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].
153+
154+
</section>
155+
156+
<!-- /.usage -->
157+
158+
<section class="notes">
159+
160+
## Notes
161+
162+
- 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.
163+
- 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`.
164+
- 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.
165+
- The algorithm has space complexity `O(1)` and time complexity `O(N log2 N)`.
166+
- 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).
167+
- 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.
168+
169+
</section>
170+
171+
<!-- /.notes -->
172+
173+
<section class="examples">
174+
175+
## Examples
176+
177+
<!-- eslint no-undef: "error" -->
178+
179+
```javascript
180+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
181+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
182+
var ndarray = require( '@stdlib/ndarray/ctor' );
183+
var toSortedhp = require( '@stdlib/blas/ext/to-sortedhp' );
184+
185+
// Generate an array of random numbers:
186+
var xbuf = discreteUniform( 25, -20, 20, {
187+
'dtype': 'generic'
188+
});
189+
190+
// Wrap in an ndarray:
191+
var x = new ndarray( 'generic', xbuf, [ 5, 5 ], [ 5, 1 ], 0, 'row-major' );
192+
console.log( ndarray2array( x ) );
193+
194+
// Perform operation:
195+
var out = toSortedhp( x, {
196+
'dims': [ 0 ]
197+
});
198+
199+
// Print the results:
200+
console.log( ndarray2array( out ) );
201+
```
202+
203+
</section>
204+
205+
<!-- /.examples -->
206+
207+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
208+
209+
<section class="related">
210+
211+
</section>
212+
213+
<!-- /.related -->
214+
215+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
216+
217+
<section class="links">
218+
219+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
220+
221+
[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes
222+
223+
[@stdlib/ndarray/base/broadcast-shapes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/broadcast-shapes
224+
225+
</section>
226+
227+
<!-- /.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 toSortedhp = 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 = toSortedhp( x, ( i%2 ) ? 1 : -1 );
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();

0 commit comments

Comments
 (0)