Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 147 additions & 0 deletions lib/node_modules/@stdlib/blas/ext/base/ndarray/dsort2hp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<!--

@license Apache-2.0

Copyright (c) 2025 The Stdlib Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->

# dsort2hp

> Simultaneously sort two one-dimensional double-precision floating-point ndarrays based on the sort order of the first array using heapsort.

<section class="intro">

</section>

<!-- /.intro -->

<section class="usage">

## Usage

```javascript
var dsort2hp = require( '@stdlib/blas/ext/base/ndarray/dsort2hp' );
```

#### dsort2hp( arrays )

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

```javascript
var Float64Array = require( '@stdlib/array/float64' );
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
var ndarray2array = require( '@stdlib/ndarray/to-array' );
var ndarray = require( '@stdlib/ndarray/base/ctor' );

var xbuf = new Float64Array( [ 1.0, -2.0, 3.0, -4.0 ] );
var x = new ndarray( 'float64', xbuf, [ 4 ], [ 1 ], 0, 'row-major' );

var ybuf = new Float64Array( [ 0.0, 1.0, 2.0, 3.0 ] );
var y = new ndarray( 'float64', ybuf, [ 4 ], [ 1 ], 0, 'row-major' );

var order = scalar2ndarray( 1.0, {
'dtype': 'generic'
});

var out = dsort2hp( [ x, y, order ] );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should probably show that the return array is the first input array.

var bool = ( out === x );
// returns true

// returns <ndarray>

var arrX = ndarray2array( out );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using ndarray2array here is fine, as it is not readily doable to use <ndarray>[ ... ] notation above.

// returns [ -4.0, -2.0, 1.0, 3.0 ]

var arrY = ndarray2array( y );
// returns [ 3.0, 1.0, 0.0, 2.0 ]
```

The function has the following parameters:

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

</section>

<!-- /.usage -->

<section class="notes">

## Notes

- Both input ndarrays are sorted **in-place** (i.e., the input ndarrays are **mutated**).
- Both ndarrays must have the same length.
- 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.
- 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`.
- 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.
- The algorithm has space complexity `O(1)` and time complexity `O(N log2 N)`.
- The algorithm is **unstable**, meaning that the algorithm may change the order of ndarray elements which are equal or equivalent (e.g., `NaN` values).

</section>

<!-- /.notes -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```javascript
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var ndarray = require( '@stdlib/ndarray/base/ctor' );
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
var ndarray2array = require( '@stdlib/ndarray/to-array' );
var ndarraylike2scalar = require( '@stdlib/ndarray/base/ndarraylike2scalar' );
var dsort2hp = require( '@stdlib/blas/ext/base/ndarray/dsort2hp' );

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

var ybuf = discreteUniform( 10, -100, 100, {
'dtype': 'float64'
});
var y = new ndarray( 'float64', ybuf, [ ybuf.length ], [ 1 ], 0, 'row-major' );
console.log( ndarray2array( y ) );

var order = scalar2ndarray( 1.0, {
'dtype': 'generic'
});
console.log( 'Order:', ndarraylike2scalar( order ) );

dsort2hp( [ x, y, order ] );
console.log( ndarray2array( x ) );
console.log( ndarray2array( y ) );
```

</section>

<!-- /.examples -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var uniform = require( '@stdlib/random/array/uniform' );
var pow = require( '@stdlib/math/base/special/pow' );
var ndarray = require( '@stdlib/ndarray/base/ctor' );
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
var pkg = require( './../package.json' ).name;
var dsort2hp = require( './../lib' );


// VARIABLES //

var options = {
'dtype': 'float64'
};


// FUNCTIONS //

/**
* Creates a benchmark function.
*
* @private
* @param {PositiveInteger} len - array length
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var order;
var xbuf;
var ybuf;
var x;
var y;

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

ybuf = uniform( len, 0.0, 100.0, options );
y = new ndarray( options.dtype, ybuf, [ len ], [ 1 ], 0, 'row-major' );

order = scalar2ndarray( -1.0, options );

return benchmark;

function benchmark( b ) {
var out;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = dsort2hp( [ x, y, order ] );
if ( typeof out !== 'object' ) {
b.fail( 'should return an ndarray' );
}
}
b.toc();
if ( xbuf[ i%len ] !== xbuf[ i%len ] ) {
b.fail( 'should not return NaN' );
}
b.pass( 'benchmark finished' );
b.end();
}
}


// MAIN //

/**
* Main execution sequence.
*
* @private
*/
function main() {
var len;
var min;
var max;
var f;
var i;

min = 1; // 10^min
max = 6; // 10^max

for ( i = min; i <= max; i++ ) {
len = pow( 10, i );
f = createBenchmark( len );
bench( pkg+':len='+len, f );

Check warning on line 106 in lib/node_modules/@stdlib/blas/ext/base/ndarray/dsort2hp/benchmark/benchmark.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Use `@stdlib/string/format` instead of string concatenation for benchmark descriptions
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MeKaustubh07 Mind for all future PRs going ahead and heeding the lint warning here? Namely, using @string/string/format for benchmark descriptions. This will save ourselves future work.

}
}

main();
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@

{{alias}}( arrays )
Simultaneously sorts two one-dimensional double-precision floating-point
ndarrays based on the sort order of the first array using heapsort.

When the sort order is less than zero, the input ndarrays are sorted in
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This note is not accurate. We only sort the first input ndarray in decreasing order. The second input ndarray is sorted based on the first and that likely means it is not sorted in either ascending or descending order.

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compared to blas/ext/base/dsort2hp, you've left out a number of the notes regarding sort properties. Why?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should likely include those notes, with some modification, as well as include the omitted notes in the sorting packages you've already landed.

Both input ndarrays are sorted *in-place* (i.e., the input ndarrays are
*mutated*).

Parameters
----------
arrays: ArrayLikeObject<ndarray>
Array-like object containing two one-dimensional input ndarrays and a
zero-dimensional ndarray specifying the sort order.

Returns
-------
out: ndarray
Input ndarray.

Examples
--------
> var xbuf = new {{alias:@stdlib/array/float64}}( [ 1.0, -2.0, 3.0, -4.0 ] );
> var ybuf = new {{alias:@stdlib/array/float64}}( [ 0.0, 1.0, 2.0, 3.0 ] );
> var dt = 'float64';
> var sh = [ xbuf.length ];
> var sx = [ 1 ];
> var ox = 0;
> var ord = 'row-major';
> var x = new {{alias:@stdlib/ndarray/ctor}}( dt, xbuf, sh, sx, ox, ord );
> var y = new {{alias:@stdlib/ndarray/ctor}}( dt, ybuf, sh, sx, ox, ord );
> var o = {{alias:@stdlib/ndarray/from-scalar}}( 1.0, { 'dtype': dt } );
> {{alias}}( [ x, y, o ] )
<ndarray>
> var xdata = x.data
<Float64Array>[ -4.0, -2.0, 1.0, 3.0 ]
> var ydata = y.data
<Float64Array>[ 3.0, 1.0, 0.0, 2.0 ]

See Also
--------
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// TypeScript Version: 4.1

/// <reference types="@stdlib/types"/>

import { typedndarray, float64ndarray } from '@stdlib/types/ndarray';

/**
* Simultaneously sorts two one-dimensional double-precision floating-point ndarrays based on the sort order of the first array using heapsort.
*
* ## Notes
*
* - 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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This note isn't accurate.

*
* @param arrays - array-like object containing two one-dimensional input ndarrays and a zero-dimensional ndarray specifying the sort order
* @returns first input ndarray
*
* @example
* var Float64Array = require( '@stdlib/array/float64' );
* var ndarray2array = require( '@stdlib/ndarray/to-array' );
* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
* var ndarray = require( '@stdlib/ndarray/base/ctor' );
*
* var xbuf = new Float64Array( [ 1.0, -2.0, 3.0, -4.0 ] );
* var x = new ndarray( 'float64', xbuf, [ 4 ], [ 1 ], 0, 'row-major' );
*
* var ybuf = new Float64Array( [ 0.0, 1.0, 2.0, 3.0 ] );
* var y = new ndarray( 'float64', ybuf, [ 4 ], [ 1 ], 0, 'row-major' );
*
* var ord = scalar2ndarray( 1.0, {
* 'dtype': 'generic'
* });
*
* var out = dsort2hp( [ x, y, ord ] );
* // returns <ndarray>
*
* var arrX = ndarray2array( out );
* // returns [ -4.0, -2.0, 1.0, 3.0 ]
*
* var arrY = ndarray2array( y );
* // returns [ 3.0, 1.0, 0.0, 2.0 ]
*/
declare function dsort2hp( arrays: [ float64ndarray, float64ndarray, typedndarray<number> ] ): float64ndarray;


// EXPORTS //

export = dsort2hp;
Loading