Skip to content

Commit 7c488c6

Browse files
headlessNodekgryte
andauthored
feat: add blas/ext/base/ndarray/gsorthp
PR-URL: #7756 Ref: #2656 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]>
1 parent f845fcc commit 7c488c6

File tree

10 files changed

+699
-0
lines changed

10 files changed

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

0 commit comments

Comments
 (0)