Skip to content

Commit a7b8f1e

Browse files
headlessNodekgryte
andauthored
feat: add blas/ext/base/ndarray/ssorthp
PR-URL: #7839 Ref: #2656 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]>
1 parent 566c748 commit a7b8f1e

File tree

10 files changed

+717
-0
lines changed

10 files changed

+717
-0
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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+
# ssorthp
22+
23+
> Sort a one-dimensional single-precision floating-point ndarray using heapsort.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var ssorthp = require( '@stdlib/blas/ext/base/ndarray/ssorthp' );
37+
```
38+
39+
#### ssorthp( arrays )
40+
41+
Sorts a one-dimensional single-precision floating-point ndarray using heapsort.
42+
43+
```javascript
44+
var Float32Array = require( '@stdlib/array/float32' );
45+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
46+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
47+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
48+
49+
var xbuf = new Float32Array( [ 1.0, -2.0, 3.0, -4.0 ] );
50+
var x = new ndarray( 'float32', xbuf, [ 4 ], [ 1 ], 0, 'row-major' );
51+
52+
var order = scalar2ndarray( 1.0, {
53+
'dtype': 'generic'
54+
});
55+
56+
var out = ssorthp( [ x, order ] );
57+
// returns <ndarray>
58+
59+
var arr = ndarray2array( out );
60+
// returns [ -4.0, -2.0, 1.0, 3.0 ]
61+
```
62+
63+
The function has the following parameters:
64+
65+
- **arrays**: array-like object containing a one-dimensional input ndarray and a zero-dimensional ndarray specifying the sort order.
66+
67+
</section>
68+
69+
<!-- /.usage -->
70+
71+
<section class="notes">
72+
73+
## Notes
74+
75+
- The input ndarray is sorted **in-place** (i.e., the input ndarray is **mutated**).
76+
- When the sort order is less than zero, the input ndarray is sorted in **decreasing** order. When the sort order is greater than zero, the input ndarray is sorted in **increasing** order. When the sort order is equal to zero, the input ndarray is left unchanged.
77+
78+
</section>
79+
80+
<!-- /.notes -->
81+
82+
<section class="examples">
83+
84+
## Examples
85+
86+
<!-- eslint no-undef: "error" -->
87+
88+
```javascript
89+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
90+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
91+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
92+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
93+
var ndarraylike2scalar = require( '@stdlib/ndarray/base/ndarraylike2scalar' );
94+
var ssorthp = require( '@stdlib/blas/ext/base/ndarray/ssorthp' );
95+
96+
var xbuf = discreteUniform( 10, -100, 100, {
97+
'dtype': 'float32'
98+
});
99+
var x = new ndarray( 'float32', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' );
100+
console.log( ndarray2array( x ) );
101+
102+
var order = scalar2ndarray( 1.0, {
103+
'dtype': 'generic'
104+
});
105+
console.log( 'Order:', ndarraylike2scalar( order ) );
106+
107+
ssorthp( [ x, order ] );
108+
console.log( ndarray2array( x ) );
109+
```
110+
111+
</section>
112+
113+
<!-- /.examples -->
114+
115+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
116+
117+
<section class="related">
118+
119+
</section>
120+
121+
<!-- /.related -->
122+
123+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
124+
125+
<section class="links">
126+
127+
</section>
128+
129+
<!-- /.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 uniform = require( '@stdlib/random/array/uniform' );
25+
var pow = require( '@stdlib/math/base/special/pow' );
26+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
27+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
28+
var pkg = require( './../package.json' ).name;
29+
var ssorthp = require( './../lib' );
30+
31+
32+
// VARIABLES //
33+
34+
var options = {
35+
'dtype': 'float32'
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 order;
50+
var xbuf;
51+
var x;
52+
53+
xbuf = uniform( len, 0.0, 100.0, options );
54+
x = new ndarray( options.dtype, xbuf, [ len ], [ 1 ], 0, 'row-major' );
55+
56+
order = scalar2ndarray( -1.0, options );
57+
58+
return benchmark;
59+
60+
function benchmark( b ) {
61+
var out;
62+
var i;
63+
64+
b.tic();
65+
for ( i = 0; i < b.iterations; i++ ) {
66+
out = ssorthp( [ x, order ] );
67+
if ( out !== out ) {
68+
b.fail( 'should not return NaN' );
69+
}
70+
}
71+
b.toc();
72+
if ( out !== out ) {
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+':len='+len, f );
102+
}
103+
}
104+
105+
main();
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
{{alias}}( arrays )
3+
Sorts a one-dimensional single-precision floating-point ndarray using
4+
heapsort.
5+
6+
When the sort order is less than zero, the input ndarray is sorted in
7+
decreasing order. When the sort order is greater than zero, the input
8+
ndarray is sorted in increasing order. When the sort order is equal to zero,
9+
the input ndarray is left unchanged.
10+
11+
The input ndarray is sorted *in-place* (i.e., the input strided array is
12+
*mutated*).
13+
14+
Parameters
15+
----------
16+
arrays: ArrayLikeObject<ndarray>
17+
Array-like object containing a one-dimensional input ndarray and a
18+
zero-dimensional ndarray specifying the sort order.
19+
20+
Returns
21+
-------
22+
out: ndarray
23+
Input ndarray.
24+
25+
Examples
26+
--------
27+
> var xbuf = new {{alias:@stdlib/array/float32}}( [ 1.0, -2.0, 3.0, -4.0 ] );
28+
> var dt = 'float32';
29+
> var sh = [ xbuf.length ];
30+
> var sx = [ 1 ];
31+
> var ox = 0;
32+
> var ord = 'row-major';
33+
> var x = new {{alias:@stdlib/ndarray/ctor}}( dt, xbuf, sh, sx, ox, ord );
34+
> var o = {{alias:@stdlib/ndarray/from-scalar}}( 1.0, { 'dtype': dt } );
35+
> {{alias}}( [ x, o ] )
36+
<ndarray>
37+
> var data = x.data
38+
<Float32Array>[ -4.0, -2.0, 1.0, 3.0 ]
39+
40+
See Also
41+
--------
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
// TypeScript Version: 4.1
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
import { typedndarray, float32ndarray } from '@stdlib/types/ndarray';
24+
25+
/**
26+
* Sorts a one-dimensional single-precision floating-point ndarray using heapsort.
27+
*
28+
* ## Notes
29+
*
30+
* - When the sort order is less than zero, the input ndarray is sorted in **decreasing** order. When the sort order is greater than zero, the input ndarray is sorted in **increasing** order. When the sort order is equal to zero, the input ndarray is left unchanged.
31+
*
32+
* @param arrays - array-like object containing a one-dimensional input ndarray and a zero-dimensional ndarray specifying the sort order
33+
* @returns input ndarray
34+
*
35+
* @example
36+
* var Float32Array = require( '@stdlib/array/float32' );
37+
* var ndarray2array = require( '@stdlib/ndarray/base/to-array' );
38+
* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
39+
* var ndarray = require( '@stdlib/ndarray/base/ctor' );
40+
*
41+
* var xbuf = new Float32Array( [ 1.0, -2.0, 3.0, -4.0 ] );
42+
* var x = new ndarray( 'float32', xbuf, [ 4 ], [ 1 ], 0, 'row-major' );
43+
*
44+
* var ord = scalar2ndarray( 1.0, {
45+
* 'dtype': 'generic'
46+
* });
47+
*
48+
* var out = ssorthp( [ x, ord ] );
49+
* // returns <ndarray>
50+
*
51+
* var arr = ndarray2array( out );
52+
* // returns [ -4.0, -2.0, 1.0, 3.0 ]
53+
*/
54+
declare function ssorthp( arrays: [ float32ndarray, typedndarray<number> ] ): float32ndarray;
55+
56+
57+
// EXPORTS //
58+
59+
export = ssorthp;

0 commit comments

Comments
 (0)