Skip to content

Commit e36c29e

Browse files
committed
feat: add blas/ext/base/ndarray/gsorthp
--- 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 53948cc commit e36c29e

File tree

10 files changed

+692
-0
lines changed

10 files changed

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