Skip to content

Commit 2698b20

Browse files
committed
feat: add blas/ext/base/ndarray/gsort2hp
--- 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 1ec9586 commit 2698b20

10 files changed

Lines changed: 765 additions & 0 deletions

File tree

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 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+
# gsort2ins
22+
23+
> Simultaneously sort two one-dimensional ndarrays based on the sort order of the first ndarray using insertion sort.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var gsort2ins = require( '@stdlib/blas/ext/base/ndarray/gsort2ins' );
37+
```
38+
39+
#### gsort2ins( arrays )
40+
41+
Simultaneously sorts two one-dimensional ndarrays based on the sort order of the first ndarray using insertion sort.
42+
43+
```javascript
44+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
45+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
46+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
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 ybuf = [ 0.0, 1.0, 2.0, 3.0 ];
52+
var y = new ndarray( 'generic', ybuf, [ 4 ], [ 1 ], 0, 'row-major' );
53+
54+
var ord = scalar2ndarray( 1.0, {
55+
'dtype': 'generic'
56+
});
57+
58+
gsort2ins( [ x, y, ord ] );
59+
// x => <ndarray>[ -4.0, -2.0, 1.0, 3.0 ]
60+
// y => <ndarray>[ 3.0, 1.0, 0.0, 2.0 ]
61+
```
62+
63+
The function has the following parameters:
64+
65+
- **arrays**: array-like object containing two one-dimensional input ndarrays 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 ndarrays are sorted **in-place** (i.e., the input ndarrays are **mutated**).
76+
- When the sort order is less than zero, the first input ndarray is sorted in **decreasing** order. When the sort order is greater than zero, the first input ndarray is sorted in **increasing** order. When the sort order is equal to zero, the input ndarrays are 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 uniform = require( '@stdlib/random/uniform' );
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 gsort2ins = require( '@stdlib/blas/ext/base/ndarray/gsort2ins' );
94+
95+
var x = uniform( [ 10 ], -100.0, 100.0 );
96+
var y = uniform( [ 10 ], -100.0, 100.0 );
97+
98+
console.log( ndarray2array( x ) );
99+
console.log( ndarray2array( y ) );
100+
101+
var order = scalar2ndarray( 1.0, {
102+
'dtype': 'generic'
103+
});
104+
console.log( 'Order:', ndarraylike2scalar( order ) );
105+
106+
gsort2ins( [ x, y, order ] );
107+
108+
console.log( ndarray2array( x ) );
109+
console.log( ndarray2array( y ) );
110+
111+
order = scalar2ndarray( -1.0, {
112+
'dtype': 'generic'
113+
});
114+
console.log( 'Order:', ndarraylike2scalar( order ) );
115+
116+
gsort2ins( [ x, y, order ] );
117+
118+
console.log( ndarray2array( x ) );
119+
console.log( ndarray2array( y ) );
120+
```
121+
122+
</section>
123+
124+
<!-- /.examples -->
125+
126+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
127+
128+
<section class="related">
129+
130+
</section>
131+
132+
<!-- /.related -->
133+
134+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
135+
136+
<section class="links">
137+
138+
</section>
139+
140+
<!-- /.links -->
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 gsort2ins = 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 ybuf;
52+
var x;
53+
var y;
54+
55+
xbuf = uniform( len, 0.0, 100.0, options );
56+
x = new ndarray( options.dtype, xbuf, [ len ], [ 1 ], 0, 'row-major' );
57+
58+
ybuf = uniform( len, 0.0, 100.0, options );
59+
y = new ndarray( options.dtype, ybuf, [ len ], [ 1 ], 0, 'row-major' );
60+
61+
order = scalar2ndarray( -1.0, options );
62+
63+
return benchmark;
64+
65+
/**
66+
* Benchmark function.
67+
*
68+
* @private
69+
* @param {Benchmark} b - benchmark instance
70+
*/
71+
function benchmark( b ) {
72+
var out;
73+
var i;
74+
75+
b.tic();
76+
for ( i = 0; i < b.iterations; i++ ) {
77+
// Note: We are sorting in-place, so repeated calls sort already sorted arrays...
78+
out = gsort2ins( [ x, y, order ] );
79+
if ( typeof out !== 'object' ) {
80+
b.fail( 'should return an ndarray' );
81+
}
82+
}
83+
b.toc();
84+
if ( xbuf[ i % len ] !== xbuf[ i % len ] ) {
85+
b.fail( 'should not return NaN' );
86+
}
87+
b.pass( 'benchmark finished' );
88+
b.end();
89+
}
90+
}
91+
92+
93+
// MAIN //
94+
95+
/**
96+
* Main execution sequence.
97+
*
98+
* @private
99+
*/
100+
function main() {
101+
var len;
102+
var min;
103+
var max;
104+
var f;
105+
var i;
106+
107+
min = 1; // 10^1
108+
max = 6; // 10^6
109+
110+
for ( i = min; i <= max; i++ ) {
111+
len = pow( 10, i );
112+
f = createBenchmark( len );
113+
bench( pkg + ':len=' + len, f );
114+
}
115+
}
116+
117+
main();
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
{{alias}}( arrays )
3+
Simultaneously sorts two one-dimensional ndarrays based on the sort order
4+
of the first ndarray using insertion sort.
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 ndarrays are sorted *in-place* (i.e., the input ndarrays are
12+
*mutated*).
13+
14+
Parameters
15+
----------
16+
arrays: ArrayLikeObject<ndarray>
17+
Array-like object containing two one-dimensional input ndarrays and a
18+
zero-dimensional ndarray specifying the sort order.
19+
20+
Returns
21+
-------
22+
out: ndarray
23+
First input ndarray.
24+
25+
Examples
26+
--------
27+
> var xbuf = [ 1.0, -2.0, 3.0, -4.0 ];
28+
> var dt = 'generic';
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 ybuf = [ 0.0, 1.0, 2.0, 3.0 ];
35+
> var y = new {{alias:@stdlib/ndarray/ctor}}( dt, ybuf, sh, sx, ox, ord );
36+
> var o = {{alias:@stdlib/ndarray/from-scalar}}( 1.0 );
37+
> {{alias}}( [ x, y, o ] )
38+
<ndarray>
39+
> var data = x.data
40+
[ -4.0, -2.0, 1.0, 3.0 ]
41+
> data = y.data
42+
[ 3.0, 1.0, 0.0, 2.0 ]
43+
44+
See Also
45+
--------
46+
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) 2026 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+
* Simultaneously sorts two one-dimensional ndarrays based on the sort order of the first ndarray using insertion sort.
27+
*
28+
* ## Notes
29+
*
30+
* - The algorithm sorts the first input ndarray in specified order and applies the same permutation to the second input ndarray.
31+
* - 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.
32+
*
33+
* @param arrays - array-like object containing two one-dimensional input ndarrays and a zero-dimensional ndarray specifying the sort order
34+
* @returns first input ndarray
35+
*
36+
* @example
37+
* var ndarray2array = require( '@stdlib/ndarray/to-array' );
38+
* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
39+
* var ndarray = require( '@stdlib/ndarray/base/ctor' );
40+
*
41+
* var xbuf = [ 1.0, -2.0, 3.0, -4.0 ];
42+
* var x = new ndarray( 'generic', xbuf, [ 4 ], [ 1 ], 0, 'row-major' );
43+
*
44+
* var ybuf = [ 0.0, 1.0, 2.0, 3.0 ];
45+
* var y = new ndarray( 'generic', ybuf, [ 4 ], [ 1 ], 0, 'row-major' );
46+
*
47+
* var ord = scalar2ndarray( 1.0, {
48+
* 'dtype': 'generic'
49+
* });
50+
*
51+
* gsort2ins( [ x, y, ord ] );
52+
* // x => <ndarray>[ -4.0, -2.0, 1.0, 3.0 ]
53+
* // y => <ndarray>[ 3.0, 1.0, 0.0, 2.0 ]
54+
*/
55+
declare function gsort2ins<T = unknown, U = unknown>( arrays: [typedndarray<T>, typedndarray<U>, typedndarray<number>] ): typedndarray<T>;
56+
57+
// EXPORTS //
58+
59+
export = gsort2ins;

0 commit comments

Comments
 (0)