Skip to content

Commit f1e7758

Browse files
committed
feat: add blas/ext/base/ndarray/dsort2hp
1 parent e7efc6b commit f1e7758

File tree

10 files changed

+132
-66
lines changed

10 files changed

+132
-66
lines changed

lib/node_modules/@stdlib/blas/ext/base/ndarray/dsort2hp/README.md

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ limitations under the License.
2020

2121
# dsort2hp
2222

23-
> Simultaneously sort two double-precision floating-point ndarray based on the sort order of the first array using heapsort.
23+
> Simultaneously sort two one-dimensional double-precision floating-point ndarrays based on the sort order of the first array using heapsort.
2424
2525
<section class="intro">
2626

@@ -38,7 +38,7 @@ var dsort2hp = require( '@stdlib/blas/ext/base/ndarray/dsort2hp' );
3838

3939
#### dsort2hp( arrays )
4040

41-
Simultaneously sort two double-precision floating-point ndarray based on the sort order of the first array using heapsort.
41+
Simultaneously sorts two one-dimensional double-precision floating-point ndarrays based on the sort order of the first array using heapsort.
4242

4343
```javascript
4444
var Float64Array = require( '@stdlib/array/float64' );
@@ -49,20 +49,26 @@ var ndarray = require( '@stdlib/ndarray/base/ctor' );
4949
var xbuf = new Float64Array( [ 1.0, -2.0, 3.0, -4.0 ] );
5050
var x = new ndarray( 'float64', xbuf, [ 4 ], [ 1 ], 0, 'row-major' );
5151

52+
var ybuf = new Float64Array( [ 0.0, 1.0, 2.0, 3.0 ] );
53+
var y = new ndarray( 'float64', ybuf, [ 4 ], [ 1 ], 0, 'row-major' );
54+
5255
var order = scalar2ndarray( 1.0, {
5356
'dtype': 'generic'
5457
});
5558

56-
var out = dsort2hp( [ x, order ] );
59+
var out = dsort2hp( [ x, y, order ] );
5760
// returns <ndarray>
5861

59-
var arr = ndarray2array( out );
62+
var arrX = ndarray2array( out );
6063
// returns [ -4.0, -2.0, 1.0, 3.0 ]
64+
65+
var arrY = ndarray2array( y );
66+
// returns [ 3.0, 1.0, 0.0, 2.0 ]
6167
```
6268

6369
The function has the following parameters:
6470

65-
- **arrays**: array-like object containing a one-dimensional input ndarray and a zero-dimensional ndarray specifying the sort order.
71+
- **arrays**: array-like object containing two one-dimensional input ndarrays and a zero-dimensional ndarray specifying the sort order.
6672

6773
</section>
6874

@@ -72,8 +78,13 @@ The function has the following parameters:
7278

7379
## Notes
7480

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.
81+
- Both input ndarrays are sorted **in-place** (i.e., the input ndarrays are **mutated**).
82+
- Both ndarrays must have the same length.
83+
- When the sort order is less than zero, the input ndarrays are sorted in **decreasing** order. When the sort order is greater than zero, the input ndarrays are sorted in **increasing** order. When the sort order is equal to zero, the input ndarrays are left unchanged.
84+
- The algorithm distinguishes between `-0` and `+0`. When sorted in increasing order, `-0` is sorted before `+0`. When sorted in decreasing order, `-0` is sorted after `+0`.
85+
- The algorithm sorts `NaN` values to the end. When sorted in increasing order, `NaN` values are sorted last. When sorted in decreasing order, `NaN` values are sorted first.
86+
- The algorithm has space complexity `O(1)` and time complexity `O(N log2 N)`.
87+
- The algorithm is **unstable**, meaning that the algorithm may change the order of ndarray elements which are equal or equivalent (e.g., `NaN` values).
7788

7889
</section>
7990

@@ -99,13 +110,20 @@ var xbuf = discreteUniform( 10, -100, 100, {
99110
var x = new ndarray( 'float64', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' );
100111
console.log( ndarray2array( x ) );
101112

113+
var ybuf = discreteUniform( 10, -100, 100, {
114+
'dtype': 'float64'
115+
});
116+
var y = new ndarray( 'float64', ybuf, [ ybuf.length ], [ 1 ], 0, 'row-major' );
117+
console.log( ndarray2array( y ) );
118+
102119
var order = scalar2ndarray( 1.0, {
103120
'dtype': 'generic'
104121
});
105122
console.log( 'Order:', ndarraylike2scalar( order ) );
106123

107-
dsort2hp( [ x, order ] );
124+
dsort2hp( [ x, y, order ] );
108125
console.log( ndarray2array( x ) );
126+
console.log( ndarray2array( y ) );
109127
```
110128

111129
</section>

lib/node_modules/@stdlib/blas/ext/base/ndarray/dsort2hp/benchmark/benchmark.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ var pow = require( '@stdlib/math/base/special/pow' );
2626
var ndarray = require( '@stdlib/ndarray/base/ctor' );
2727
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
2828
var pkg = require( './../package.json' ).name;
29-
var dsorthp = require( './../lib' );
29+
var dsort2hp = require( './../lib' );
3030

3131

3232
// VARIABLES //
@@ -48,11 +48,16 @@ var options = {
4848
function createBenchmark( len ) {
4949
var order;
5050
var xbuf;
51+
var ybuf;
5152
var x;
53+
var y;
5254

5355
xbuf = uniform( len, 0.0, 100.0, options );
5456
x = new ndarray( options.dtype, xbuf, [ len ], [ 1 ], 0, 'row-major' );
5557

58+
ybuf = uniform( len, 0.0, 100.0, options );
59+
y = new ndarray( options.dtype, ybuf, [ len ], [ 1 ], 0, 'row-major' );
60+
5661
order = scalar2ndarray( -1.0, options );
5762

5863
return benchmark;
@@ -63,7 +68,7 @@ function createBenchmark( len ) {
6368

6469
b.tic();
6570
for ( i = 0; i < b.iterations; i++ ) {
66-
out = dsorthp( [ x, order ] );
71+
out = dsort2hp( [ x, y, order ] );
6772
if ( typeof out !== 'object' ) {
6873
b.fail( 'should return an ndarray' );
6974
}
Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11

22
{{alias}}( arrays )
3-
Sorts a one-dimensional double-precision floating-point ndarray using
4-
heapsort.
3+
Simultaneously sorts two one-dimensional double-precision floating-point
4+
ndarrays based on the sort order of the first array using heapsort.
55

6-
When the sort order is less than zero, the input ndarray is sorted in
6+
When the sort order is less than zero, the input ndarrays are sorted in
77
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.
8+
ndarrays are sorted in increasing order. When the sort order is equal to
9+
zero, the input ndarrays are left unchanged.
1010

11-
The input ndarray is sorted *in-place* (i.e., the input strided array is
11+
Both input ndarrays are sorted *in-place* (i.e., the input ndarrays are
1212
*mutated*).
1313

1414
Parameters
1515
----------
1616
arrays: ArrayLikeObject<ndarray>
17-
Array-like object containing a one-dimensional input ndarray and a
17+
Array-like object containing two one-dimensional input ndarrays and a
1818
zero-dimensional ndarray specifying the sort order.
1919

2020
Returns
@@ -25,17 +25,21 @@
2525
Examples
2626
--------
2727
> var xbuf = new {{alias:@stdlib/array/float64}}( [ 1.0, -2.0, 3.0, -4.0 ] );
28+
> var ybuf = new {{alias:@stdlib/array/float64}}( [ 0.0, 1.0, 2.0, 3.0 ] );
2829
> var dt = 'float64';
2930
> var sh = [ xbuf.length ];
3031
> var sx = [ 1 ];
3132
> var ox = 0;
3233
> var ord = 'row-major';
3334
> var x = new {{alias:@stdlib/ndarray/ctor}}( dt, xbuf, sh, sx, ox, ord );
35+
> var y = new {{alias:@stdlib/ndarray/ctor}}( dt, ybuf, sh, sx, ox, ord );
3436
> var o = {{alias:@stdlib/ndarray/from-scalar}}( 1.0, { 'dtype': dt } );
35-
> {{alias}}( [ x, o ] )
37+
> {{alias}}( [ x, y, o ] )
3638
<ndarray>
37-
> var data = x.data
39+
> var xdata = x.data
3840
<Float64Array>[ -4.0, -2.0, 1.0, 3.0 ]
41+
> var ydata = y.data
42+
<Float64Array>[ 3.0, 1.0, 0.0, 2.0 ]
3943

4044
See Also
4145
--------

lib/node_modules/@stdlib/blas/ext/base/ndarray/dsort2hp/docs/types/index.d.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@
2323
import { typedndarray, float64ndarray } from '@stdlib/types/ndarray';
2424

2525
/**
26-
* Sorts a one-dimensional double-precision floating-point ndarray using heapsort.
26+
* Simultaneously sorts two one-dimensional double-precision floating-point ndarrays based on the sort order of the first array using heapsort.
2727
*
2828
* ## Notes
2929
*
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.
30+
* - When the sort order is less than zero, the input ndarrays are sorted in **decreasing** order. When the sort order is greater than zero, the input ndarrays are sorted in **increasing** order. When the sort order is equal to zero, the input ndarrays are left unchanged.
3131
*
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
32+
* @param arrays - array-like object containing two one-dimensional input ndarrays and a zero-dimensional ndarray specifying the sort order
33+
* @returns first input ndarray
3434
*
3535
* @example
3636
* var Float64Array = require( '@stdlib/array/float64' );
@@ -41,19 +41,25 @@ import { typedndarray, float64ndarray } from '@stdlib/types/ndarray';
4141
* var xbuf = new Float64Array( [ 1.0, -2.0, 3.0, -4.0 ] );
4242
* var x = new ndarray( 'float64', xbuf, [ 4 ], [ 1 ], 0, 'row-major' );
4343
*
44+
* var ybuf = new Float64Array( [ 0.0, 1.0, 2.0, 3.0 ] );
45+
* var y = new ndarray( 'float64', ybuf, [ 4 ], [ 1 ], 0, 'row-major' );
46+
*
4447
* var ord = scalar2ndarray( 1.0, {
4548
* 'dtype': 'generic'
4649
* });
4750
*
48-
* var out = dsorthp( [ x, ord ] );
51+
* var out = dsort2hp( [ x, y, ord ] );
4952
* // returns <ndarray>
5053
*
51-
* var arr = ndarray2array( out );
54+
* var arrX = ndarray2array( out );
5255
* // returns [ -4.0, -2.0, 1.0, 3.0 ]
56+
*
57+
* var arrY = ndarray2array( y );
58+
* // returns [ 3.0, 1.0, 0.0, 2.0 ]
5359
*/
54-
declare function dsorthp( arrays: [ float64ndarray, typedndarray<number> ] ): float64ndarray;
60+
declare function dsort2hp( arrays: [ float64ndarray, float64ndarray, typedndarray<number> ] ): float64ndarray;
5561

5662

5763
// EXPORTS //
5864

59-
export = dsorthp;
65+
export = dsort2hp;

lib/node_modules/@stdlib/blas/ext/base/ndarray/dsort2hp/docs/types/test.ts

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
import zeros = require( '@stdlib/ndarray/zeros' );
2222
import scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
23-
import dsorthp = require( './index' );
23+
import dsort2hp = require( './index' );
2424

2525

2626
// TESTS //
@@ -30,35 +30,41 @@ import dsorthp = require( './index' );
3030
const x = zeros( [ 10 ], {
3131
'dtype': 'float64'
3232
});
33+
const y = zeros( [ 10 ], {
34+
'dtype': 'float64'
35+
});
3336
const order = scalar2ndarray( 1.0, {
3437
'dtype': 'generic'
3538
});
3639

37-
dsorthp( [ x, order ] ); // $ExpectType float64ndarray
40+
dsort2hp( [ x, y, order ] ); // $ExpectType float64ndarray
3841
}
3942

4043
// The compiler throws an error if the function is provided a first argument which is not an array of ndarrays...
4144
{
42-
dsorthp( '10' ); // $ExpectError
43-
dsorthp( 10 ); // $ExpectError
44-
dsorthp( true ); // $ExpectError
45-
dsorthp( false ); // $ExpectError
46-
dsorthp( null ); // $ExpectError
47-
dsorthp( undefined ); // $ExpectError
48-
dsorthp( [] ); // $ExpectError
49-
dsorthp( {} ); // $ExpectError
50-
dsorthp( ( x: number ): number => x ); // $ExpectError
45+
dsort2hp( '10' ); // $ExpectError
46+
dsort2hp( 10 ); // $ExpectError
47+
dsort2hp( true ); // $ExpectError
48+
dsort2hp( false ); // $ExpectError
49+
dsort2hp( null ); // $ExpectError
50+
dsort2hp( undefined ); // $ExpectError
51+
dsort2hp( [] ); // $ExpectError
52+
dsort2hp( {} ); // $ExpectError
53+
dsort2hp( ( x: number ): number => x ); // $ExpectError
5154
}
5255

5356
// The compiler throws an error if the function is provided an unsupported number of arguments...
5457
{
5558
const x = zeros( [ 10 ], {
5659
'dtype': 'float64'
5760
});
61+
const y = zeros( [ 10 ], {
62+
'dtype': 'float64'
63+
});
5864
const order = scalar2ndarray( 1.0, {
5965
'dtype': 'generic'
6066
});
6167

62-
dsorthp(); // $ExpectError
63-
dsorthp( [ x, order ], {} ); // $ExpectError
68+
dsort2hp(); // $ExpectError
69+
dsort2hp( [ x, y, order ], {} ); // $ExpectError
6470
}

lib/node_modules/@stdlib/blas/ext/base/ndarray/dsort2hp/examples/index.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,25 @@ var ndarray = require( '@stdlib/ndarray/base/ctor' );
2323
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
2424
var ndarray2array = require( '@stdlib/ndarray/to-array' );
2525
var ndarraylike2scalar = require( '@stdlib/ndarray/base/ndarraylike2scalar' );
26-
var dsorthp = require( './../lib' );
26+
var dsort2hp = require( './../lib' );
2727

2828
var xbuf = discreteUniform( 10, -100, 100, {
2929
'dtype': 'float64'
3030
});
3131
var x = new ndarray( 'float64', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' );
3232
console.log( ndarray2array( x ) );
3333

34+
var ybuf = discreteUniform( 10, -100, 100, {
35+
'dtype': 'float64'
36+
});
37+
var y = new ndarray( 'float64', ybuf, [ ybuf.length ], [ 1 ], 0, 'row-major' );
38+
console.log( ndarray2array( y ) );
39+
3440
var order = scalar2ndarray( 1.0, {
3541
'dtype': 'generic'
3642
});
3743
console.log( 'Order:', ndarraylike2scalar( order ) );
3844

39-
dsorthp( [ x, order ] );
45+
dsort2hp( [ x, y, order ] );
4046
console.log( ndarray2array( x ) );
47+
console.log( ndarray2array( y ) );

lib/node_modules/@stdlib/blas/ext/base/ndarray/dsort2hp/lib/index.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,35 @@
1919
'use strict';
2020

2121
/**
22-
* Sort a one-dimensional double-precision floating-point ndarray using heapsort.
22+
* Simultaneously sort two one-dimensional double-precision floating-point ndarrays based on the sort order of the first array using heapsort.
2323
*
24-
* @module @stdlib/blas/ext/base/ndarray/dsorthp
24+
* @module @stdlib/blas/ext/base/ndarray/dsort2hp
2525
*
2626
* @example
2727
* var Float64Array = require( '@stdlib/array/float64' );
2828
* var ndarray = require( '@stdlib/ndarray/base/ctor' );
2929
* var ndarray2array = require( '@stdlib/ndarray/to-array' );
3030
* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
31-
* var dsorthp = require( '@stdlib/blas/ext/base/ndarray/dsorthp' );
31+
* var dsort2hp = require( '@stdlib/blas/ext/base/ndarray/dsort2hp' );
3232
*
3333
* var xbuf = new Float64Array( [ 1.0, -2.0, 3.0, -4.0 ] );
3434
* var x = new ndarray( 'float64', xbuf, [ 4 ], [ 1 ], 0, 'row-major' );
3535
*
36+
* var ybuf = new Float64Array( [ 0.0, 1.0, 2.0, 3.0 ] );
37+
* var y = new ndarray( 'float64', ybuf, [ 4 ], [ 1 ], 0, 'row-major' );
38+
*
3639
* var ord = scalar2ndarray( 1.0, {
3740
* 'dtype': 'generic'
3841
* });
3942
*
40-
* var out = dsorthp( [ x, ord ] );
43+
* var out = dsort2hp( [ x, y, ord ] );
4144
* // returns <ndarray>
4245
*
43-
* var arr = ndarray2array( out );
46+
* var arrX = ndarray2array( out );
4447
* // returns [ -4.0, -2.0, 1.0, 3.0 ]
48+
*
49+
* var arrY = ndarray2array( y );
50+
* // returns [ 3.0, 1.0, 0.0, 2.0 ]
4551
*/
4652

4753
// MODULES //

0 commit comments

Comments
 (0)