Skip to content

feat: add C ndarray interface and refactor implementation for stats/base/dmaxabssorted #4181

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
144 changes: 127 additions & 17 deletions lib/node_modules/@stdlib/stats/base/dmaxabssorted/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ limitations under the License.
var dmaxabssorted = require( '@stdlib/stats/base/dmaxabssorted' );
```

#### dmaxabssorted( N, x, stride )
#### dmaxabssorted( N, x, strideX )

Computes the maximum absolute value of a sorted double-precision floating-point strided array `x`.

Expand All @@ -56,18 +56,16 @@ The function has the following parameters:

- **N**: number of indexed elements.
- **x**: sorted input [`Float64Array`][@stdlib/array/float64].
- **stride**: index increment for `x`.
- **strideX**: stride length for `x`.

The `N` and `stride` parameters determine which elements in `x` are accessed at runtime. For example, to compute the maximum absolute value of every other element in `x`,
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to compute the maximum absolute value of every other element in `x`,

```javascript
var Float64Array = require( '@stdlib/array/float64' );
var floor = require( '@stdlib/math/base/special/floor' );

var x = new Float64Array( [ 1.0, 2.0, 2.0, -7.0, 3.0, 3.0, 4.0, 2.0 ] );
var N = floor( x.length / 2 );

var v = dmaxabssorted( N, x, 2 );
var v = dmaxabssorted( 4, x, 2 );
// returns 4.0
```

Expand All @@ -77,18 +75,15 @@ Note that indexing is relative to the first index. To introduce an offset, use [

```javascript
var Float64Array = require( '@stdlib/array/float64' );
var floor = require( '@stdlib/math/base/special/floor' );

var x0 = new Float64Array( [ 2.0, 1.0, 2.0, 2.0, -2.0, 2.0, 3.0, 4.0 ] );
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element

var N = floor( x0.length / 2 );

var v = dmaxabssorted( N, x1, 2 );
var v = dmaxabssorted( 4, x1, 2 );
// returns 4.0
```

#### dmaxabssorted.ndarray( N, x, stride, offset )
#### dmaxabssorted.ndarray( N, x, strideX, offsetX )

Computes the maximum absolute value of a sorted double-precision floating-point strided array using alternative indexing semantics.

Expand All @@ -102,18 +97,16 @@ var v = dmaxabssorted.ndarray( x.length, x, 1, 0 );

The function has the following additional parameters:

- **offset**: starting index for `x`.
- **offsetX**: starting index for `x`.

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 calculate the maximum absolute value for every other value in `x` starting from the second value
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 calculate the maximum absolute value for every other element in `x` starting from the second element

```javascript
var Float64Array = require( '@stdlib/array/float64' );
var floor = require( '@stdlib/math/base/special/floor' );

var x = new Float64Array( [ 2.0, 1.0, 2.0, 2.0, -2.0, 2.0, 3.0, 4.0 ] );
var N = floor( x.length / 2 );

var v = dmaxabssorted.ndarray( N, x, 2, 1 );
var v = dmaxabssorted.ndarray( 4, x, 2, 1 );
// returns 4.0
```

Expand Down Expand Up @@ -147,7 +140,7 @@ var i;

x = new Float64Array( 10 );
for ( i = 0; i < x.length; i++ ) {
x[ i ] = i - 5.0;
x[ i ] = i - x.length;
}
console.log( x );

Expand All @@ -159,6 +152,123 @@ console.log( v );

<!-- /.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/stats/base/dmaxabssorted.h"
```

#### stdlib_strided_dmaxabssorted( N, \*X, strideX )

Computes the maximum absolute value of a sorted double-precision floating-point strided array.

```c
const double x[] = { -1.0, -2.0, -3.0, -4.0, -5.0, -6.0, -7.0, -8.0 };

double v = stdlib_strided_dmaxabssorted( 4, x, 2 );
// returns 7.0
```

The function accepts the following arguments:

- **N**: `[in] CBLAS_INT` number of indexed elements.
- **X**: `[in] double*` input array.
- **strideX**: `[in] CBLAS_INT` stride length for `X`.

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

#### stdlib_strided_dmaxabssorted_ndarray( N, \*X, strideX, offsetX )

Computes the maximum absolute value of a sorted double-precision floating-point strided array using alternative indexing semantics.

```c
const double x[] = { -1.0, -2.0, -3.0, -4.0, -5.0, -6.0, -7.0, -8.0 };

double v = stdlib_strided_dmaxabssorted_ndarray( 4, x, 2, 0 );
// returns 7.0
```

The function accepts the following arguments:

- **N**: `[in] CBLAS_INT` number of indexed elements.
- **X**: `[in] double*` input array.
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.

```c
double stdlib_strided_dmaxabssorted_ndarray( const CBLAS_INT N, const 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/stats/base/dmaxabssorted.h"
#include <stdio.h>

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

// Specify the number of elements:
const int N = 4;

// Specify the stride length:
const int strideX = 2;

// Compute the maximum absolute value:
double v = stdlib_strided_dmaxabssorted( N, x, strideX );

// Print the result:
printf( "maxabs: %lf\n", v );
}
```

</section>

<!-- /.examples -->

</section>

<!-- /.c -->

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

<section class="related">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ static double rand_double( void ) {
*
* @param iterations number of iterations
* @param len array length
* @return elapsed time in seconds
* @return elapsed time in seconds
*/
static double benchmark( int iterations, int len ) {
static double benchmark1( int iterations, int len ) {
double elapsed;
double x[ len ];
double v;
Expand All @@ -107,6 +107,7 @@ static double benchmark( int iterations, int len ) {
v = 0.0;
t = tic();
for ( i = 0; i < iterations; i++ ) {
// cppcheck-suppress uninitvar
v = stdlib_strided_dmaxabssorted( len, x, 1 );
if ( v != v ) {
printf( "should not return NaN\n" );
Expand All @@ -120,6 +121,40 @@ static double benchmark( int iterations, int len ) {
return elapsed;
}

/**
* Runs a benchmark.
*
* @param iterations number of iterations
* @param len array length
* @return elapsed time in seconds
*/
static double benchmark2( int iterations, int len ) {
double elapsed;
double x[ len ];
double v;
double t;
int i;

for ( i = 0; i < len; i++ ) {
x[ i ] = i - (len/2);
}
v = 0.0;
t = tic();
for ( i = 0; i < iterations; i++ ) {
// cppcheck-suppress uninitvar
v = stdlib_strided_dmaxabssorted_ndarray( len, x, 1, 0 );
if ( v != v ) {
printf( "should not return NaN\n" );
break;
}
}
elapsed = tic() - t;
if ( v != v ) {
printf( "should not return NaN\n" );
}
return elapsed;
}

/**
* Main execution sequence.
*/
Expand All @@ -142,7 +177,18 @@ int main( void ) {
for ( j = 0; j < REPEATS; j++ ) {
count += 1;
printf( "# c::%s:len=%d\n", NAME, len );
elapsed = benchmark( iter, len );
elapsed = benchmark1( iter, len );
print_results( iter, elapsed );
printf( "ok %d benchmark finished\n", count );
}
}
for ( i = MIN; i <= MAX; i++ ) {
len = pow( 10, i );
iter = ITERATIONS / pow( 10, i-1 );
for ( j = 0; j < REPEATS; j++ ) {
count += 1;
printf( "# c::%s:ndarray:len=%d\n", NAME, len );
elapsed = benchmark2( iter, len );
print_results( iter, elapsed );
printf( "ok %d benchmark finished\n", count );
}
Expand Down
32 changes: 14 additions & 18 deletions lib/node_modules/@stdlib/stats/base/dmaxabssorted/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@

{{alias}}( N, x, stride )
{{alias}}( N, x, strideX )
Computes the maximum absolute value of a sorted double-precision floating-
point strided array.

The input strided array must be sorted in either strictly ascending or
descending order.

The `N` and `stride` parameters determine which elements in `x` are accessed
at runtime.
The `N` and stride parameters determine which elements in the strided array
are accessed at runtime.

Indexing is relative to the first index. To introduce an offset, use a typed
array view.
Expand All @@ -22,8 +22,8 @@
x: Float64Array
Sorted input array.

stride: integer
Index increment.
strideX: integer
Stride Length.

Returns
-------
Expand All @@ -37,22 +37,19 @@
> {{alias}}( x.length, x, 1 )
3.0

// Using `N` and `stride` parameters:
// Using `N` and stride parameters:
> x = new {{alias:@stdlib/array/float64}}( [ -2.0, 1.0, 1.0, -5.0, 2.0, -1.0 ] );
> var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 );
> var stride = 2;
> {{alias}}( N, x, stride )
> {{alias}}( 3, x, 2 )
2.0

// Using view offsets:
> var x0 = new {{alias:@stdlib/array/float64}}( [ 1.0, -2.0, 3.0, 2.0, 5.0, 3.0 ] );
> var x1 = new {{alias:@stdlib/array/float64}}( x0.buffer, x0.BYTES_PER_ELEMENT*1 );
> N = {{alias:@stdlib/math/base/special/floor}}( x0.length / 2 );
> stride = 2;
> {{alias}}( N, x1, stride )
> {{alias}}( 3, x1, 2 )
3.0

{{alias}}.ndarray( N, x, stride, offset )

{{alias}}.ndarray( N, x, strideX, offsetX )
Computes the maximum absolute value of a sorted double-precision floating-
point strided array using alternative indexing semantics.

Expand All @@ -68,10 +65,10 @@
x: Float64Array
Sorted input array.

stride: integer
Index increment.
strideX: integer
Stride Length.

offset: integer
offsetX: integer
Starting index.

Returns
Expand All @@ -88,8 +85,7 @@

// Using offset parameter:
> var x = new {{alias:@stdlib/array/float64}}( [ 1.0, -2.0, 3.0, 2.0, 5.0, 3.0 ] );
> var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 );
> {{alias}}.ndarray( N, x, 2, 1 )
> {{alias}}.ndarray( 3, x, 2, 1 )
3.0

See Also
Expand Down
Loading
Loading