Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
298 changes: 298 additions & 0 deletions lib/node_modules/@stdlib/blas/ext/base/dsortnans/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,298 @@
<!--

@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.

-->

# dsortnans

> Partition a double-precision floating-point strided array by moving all NaNs either to the beginning or the end of the array.

<section class="usage">

## Usage

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

#### dsortnans( N, order, x, strideX )

Partitions a double-precision floating-point strided array by moving all NaNs either to the beginning or the end of the array.

```javascript
var Float64Array = require( '@stdlib/array/float64' );

var x = new Float64Array( [ 1.0, -2.0, NaN, 3.0, -4.0, NaN ] );

dsortnans( x.length, 1.0, x, 1 );
// x => <Float64Array>[ 1.0, -2.0, -4.0, 3.0, NaN, NaN ]
```

The function has the following parameters:

- **N**: number of indexed elements.
- **order**: NaN placement order. If `order < 0`, the function places NaNs at the **beginning** of `x`. If `order > 0`, the function places NaNs at the **end** of `x`. If `order == 0.0`, the input strided array is left **unchanged**.
- **x**: input [`Float64Array`][@stdlib/array/float64].
- **strideX**: stride length.

The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to sort every other element:

```javascript
var Float64Array = require( '@stdlib/array/float64' );

var x = new Float64Array( [ 1.0, -2.0, NaN, -3.0, 5.0 ] );

dsortnans( 2, -1.0, x, 2 );
// x => <Float64Array>[ NaN, -2.0, 1.0, -3.0, 5.0 ]
```

Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.

```javascript
var Float64Array = require( '@stdlib/array/float64' );

// Initial array...
var x0 = new Float64Array( [ 1.0, NaN, -2.0, 5.0 ] );

// Create an offset view...
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element

// Sort every other element...
dsortnans( 2, 1.0, x1, 2 );
// x0 => <Float64Array>[ 1.0, 5.0, -2.0, NaN ]
```

#### dsortnans.ndarray( N, order, x, strideX, offsetX )

Partitions a double-precision floating-point strided array by moving all NaNs either to the beginning or the end of the array using alternative indexing semantics.

```javascript
var Float64Array = require( '@stdlib/array/float64' );

var x = new Float64Array( [ NaN, -2.0, NaN, 3.0, -4.0 ] );

dsortnans.ndarray( x.length, 1.0, x, 1, 0 );
// x => <Float64Array>[ -4.0, -2.0, 3.0, NaN, NaN ]
```

The function has the following additional parameters:

- **offsetX**: starting index.

While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameter supports indexing semantics based on a starting index. For example, to access only the last three elements:

```javascript
var Float64Array = require( '@stdlib/array/float64' );

var x = new Float64Array( [ 1.0, -2.0, 3.0, -4.0, NaN, -6.0 ] );

dsortnans.ndarray( 3, 1.0, x, 1, x.length-3 );
// x => <Float64Array>[ 1.0, -2.0, 3.0, -4.0, -6.0, NaN ]
```

</section>

<!-- /.usage -->

<section class="notes">

## Notes

- If `N <= 0` or `order == 0.0`, both functions return `x` unchanged.
- A positive order places `NaN` values at the end of the array, while a negative order places `NaN` values at the beginning.
Copy link
Contributor Author

@anandkaranubc anandkaranubc Dec 30, 2025

Choose a reason for hiding this comment

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

Any explicit need to mention that (the point regarding positive/negative order)?

- The algorithm has space complexity `O(1)` and worst case time complexity `O(N)`.
- The algorithm is **not stable**, meaning that the relative order of non-`NaN` elements is not guaranteed to be preserved.

</section>

<!-- /.notes -->

<section class="examples">

## Examples

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

```javascript
var randu = require( '@stdlib/random/base/randu' );
var Float64Array = require( '@stdlib/array/float64' );
var dsortnans = require( '@stdlib/blas/ext/base/dsortnans' );

var x;
var i;

x = new Float64Array( 10 );
for ( i = 0; i < x.length; i++ ) {
if ( randu() < 0.3 ) {
x[ i ] = NaN;
} else {
x[ i ] = (randu()*20.0) - 10.0;
}
}
console.log( x );

dsortnans( x.length, -1.0, x, -1 );
console.log( x );
```

</section>

<!-- /.examples -->

<!-- C interface documentation. -->

* * *

<section class="c">

## C APIs

<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->

<section class="intro">

</section>

<!-- /.intro -->

<!-- C usage documentation. -->

<section class="usage">

### Usage

```c
#include "stdlib/blas/ext/base/dsortnans.h"
```

#### stdlib_strided_dsortnans( N, order, \*X, strideX )

Partitions a double-precision floating-point strided array by moving all NaNs either to the beginning or the end of the array.

```c
double x[] = { 0.0/0.0, -2.0, 0.0/0.0, -4.0 };

stdlib_strided_dsortnans( 2, -1.0, x, 1 );
```

The function accepts the following arguments:

- **N**: `[in] CBLAS_INT` number of indexed elements.
- **order**: `[in] double` NaN placement order. If `order < 0`, the function places NaNs at the **beginning** of `x`. If `order > 0`, the function places NaNs at the **end** of `x`. If `order == 0.0`, the input strided array is left **unchanged**.
- **X**: `[inout] double*` input array.
- **strideX**: `[in] CBLAS_INT` stride length for `X`.

```c
stdlib_strided_dsortnans( const CBLAS_INT N, const double order, double *X, const CBLAS_INT strideX );
```

<!--lint disable maximum-heading-length-->

#### stdlib_strided_dsortnans_ndarray( N, order, \*X, strideX, offsetX )

<!--lint enable maximum-heading-length-->

Partitions a double-precision floating-point strided array by moving all NaNs either to the beginning or the end of the array using alternative indexing semantics.

```c
double x[] = { 0.0/0.0, -2.0, 0.0/0.0, -4.0 };

stdlib_strided_dsortnans_ndarray( 4, 1.0, x, 1, 0 );
```

The function accepts the following arguments:

- **N**: `[in] CBLAS_INT` number of indexed elements.
- **order**: `[in] double` NaN placement order. If `order < 0`, the function places NaNs at the **beginning** of `x`. If `order > 0`, the function places NaNs at the **end** of `x`. If `order == 0.0`, the input strided array is left **unchanged**.
- **X**: `[inout] double*` input array.
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.

```c
stdlib_strided_dsortnans_ndarray( const CBLAS_INT N, const double order, double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX );
```

</section>

<!-- /.usage -->

<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="notes">

</section>

<!-- /.notes -->

<!-- C API usage examples. -->

<section class="examples">

### Examples

```c
#include "stdlib/blas/ext/base/dsortnans.h"
#include <stdio.h>

int main( void ) {
// Create a strided array:
double x[] = { 1.0, -2.0, 0.0/0.0, -4.0, 0.0/0.0, -6.0, 7.0, 0.0/0.0 };

// Specify the number of elements:
int N = 8;

// Specify a stride:
int strideX = 1;

// Sort the array:
stdlib_strided_dsortnans( N, 1.0, x, strideX );

// Print the result:
for ( int i = 0; i < 8; i++ ) {
printf( "x[ %i ] = %lf\n", i, x[ i ] );
}
}
```

</section>

<!-- /.examples -->

</section>

<!-- /.c -->

<!-- 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">

[@stdlib/array/float64]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/float64

[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray

</section>

<!-- /.links -->
Loading