Skip to content

Commit e0aa4bb

Browse files
sagar7162kgrytestdlib-bot
authored
feat: add blas/ext/base/ndarray/dsort
PR-URL: #9710 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Co-authored-by: stdlib-bot <noreply@stdlib.io>
1 parent be217cb commit e0aa4bb

File tree

10 files changed

+714
-0
lines changed

10 files changed

+714
-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) 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+
# dsort
22+
23+
> Sort a double-precision floating-point ndarray.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var dsort = require( '@stdlib/blas/ext/base/ndarray/dsort' );
37+
```
38+
39+
#### dsort( arrays )
40+
41+
Sorts a double-precision floating-point ndarray.
42+
43+
```javascript
44+
var Float64Array = require( '@stdlib/array/float64' );
45+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
46+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
47+
48+
var xbuf = new Float64Array( [ 1.0, -2.0, 3.0, -4.0 ] );
49+
var x = new ndarray( 'float64', xbuf, [ 4 ], [ 1 ], 0, 'row-major' );
50+
51+
var order = scalar2ndarray( 1.0, {
52+
'dtype': 'generic'
53+
});
54+
55+
var out = dsort( [ x, order ] );
56+
// returns <ndarray>[ -4.0, -2.0, 1.0, 3.0 ]
57+
58+
var bool = ( out === x );
59+
// returns true
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 uniform = require( '@stdlib/random/uniform' );
89+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
90+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
91+
var dsort = require( '@stdlib/blas/ext/base/ndarray/dsort' );
92+
93+
var x = uniform( [ 10 ], -100, 100 );
94+
console.log( ndarray2array( x ) );
95+
96+
var order = scalar2ndarray( 1.0, {
97+
'dtype': 'generic'
98+
});
99+
100+
dsort( [ x, order ] );
101+
console.log( ndarray2array( x ) );
102+
103+
order = scalar2ndarray( -1.0, {
104+
'dtype': 'generic'
105+
});
106+
107+
dsort( [ 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: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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 format = require( '@stdlib/string/format' );
28+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
29+
var pkg = require( './../package.json' ).name;
30+
var dsort = require( './../lib' );
31+
32+
33+
// VARIABLES //
34+
35+
var options = {
36+
'dtype': 'float64'
37+
};
38+
39+
40+
// FUNCTIONS //
41+
42+
/**
43+
* Creates a benchmark function.
44+
*
45+
* @private
46+
* @param {PositiveInteger} len - array length
47+
* @returns {Function} benchmark function
48+
*/
49+
function createBenchmark( len ) {
50+
var order;
51+
var xbuf;
52+
var x;
53+
54+
xbuf = uniform( len, 0.0, 100.0, options );
55+
x = new ndarray( options.dtype, xbuf, [ len ], [ 1 ], 0, 'row-major' );
56+
order = scalar2ndarray( -1.0, options );
57+
58+
return benchmark;
59+
60+
/**
61+
* Benchmark function.
62+
*
63+
* @private
64+
* @param {Benchmark} b - benchmark instance
65+
*/
66+
function benchmark( b ) {
67+
var out;
68+
var i;
69+
70+
b.tic();
71+
for ( i = 0; i < b.iterations; i++ ) {
72+
out = dsort( [ x, order ] );
73+
if ( typeof out !== 'object' ) {
74+
b.fail( 'should return an ndarray' );
75+
}
76+
}
77+
b.toc();
78+
if ( xbuf[ i%len ] !== xbuf[ i%len ] ) {
79+
b.fail( 'should not return NaN' );
80+
}
81+
b.pass( 'benchmark finished' );
82+
b.end();
83+
}
84+
}
85+
86+
87+
// MAIN //
88+
89+
/**
90+
* Main execution sequence.
91+
*
92+
* @private
93+
*/
94+
function main() {
95+
var len;
96+
var min;
97+
var max;
98+
var f;
99+
var i;
100+
101+
min = 1; // 10^min
102+
max = 6; // 10^max
103+
104+
for ( i = min; i <= max; i++ ) {
105+
len = pow( 10, i );
106+
f = createBenchmark( len );
107+
bench( format( '%s:len=%d', pkg, len ), f );
108+
}
109+
}
110+
111+
main();
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
{{alias}}( arrays )
3+
Sorts a double-precision floating-point ndarray.
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 strided array is
11+
*mutated*).
12+
13+
Parameters
14+
----------
15+
arrays: ArrayLikeObject<ndarray>
16+
Array-like object containing a one-dimensional input ndarray and a
17+
zero-dimensional ndarray specifying the sort order.
18+
19+
Returns
20+
-------
21+
out: ndarray
22+
Input ndarray.
23+
24+
Examples
25+
--------
26+
> var xbuf = new {{alias:@stdlib/array/float64}}( [ 1.0, -2.0, 3.0, -4.0 ] );
27+
> var dt = 'float64';
28+
> var sh = [ xbuf.length ];
29+
> var sx = [ 1 ];
30+
> var ox = 0;
31+
> var ord = 'row-major';
32+
> var x = new {{alias:@stdlib/ndarray/ctor}}( dt, xbuf, sh, sx, ox, ord );
33+
> var o = {{alias:@stdlib/ndarray/from-scalar}}( 1.0, { 'dtype': dt } );
34+
> {{alias}}( [ x, o ] )
35+
<ndarray>[ -4.0, -2.0, 1.0, 3.0 ]
36+
37+
See Also
38+
--------
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) 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, float64ndarray } from '@stdlib/types/ndarray';
24+
25+
/**
26+
* Sorts a double-precision floating-point ndarray.
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 Float64Array = require( '@stdlib/array/float64' );
37+
* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
38+
* var ndarray = require( '@stdlib/ndarray/base/ctor' );
39+
*
40+
* var xbuf = new Float64Array( [ 1.0, -2.0, 3.0, -4.0 ] );
41+
* var x = new ndarray( 'float64', xbuf, [ 4 ], [ 1 ], 0, 'row-major' );
42+
*
43+
* var ord = scalar2ndarray( 1.0, {
44+
* 'dtype': 'generic'
45+
* });
46+
*
47+
* var out = dsort( [ x, ord ] );
48+
* // returns <ndarray>[ -4.0, -2.0, 1.0, 3.0 ]
49+
*
50+
* var bool = ( out === x );
51+
* // returns true
52+
*/
53+
declare function dsort( arrays: [ float64ndarray, typedndarray<number> ] ): float64ndarray;
54+
55+
56+
// EXPORTS //
57+
58+
export = dsort;

0 commit comments

Comments
 (0)