Skip to content
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

feat: add C ndarray API and refactor blas/ext/base/zfill #2962

Merged
merged 11 commits into from
Oct 1, 2024
8 changes: 4 additions & 4 deletions lib/node_modules/@stdlib/blas/ext/base/cfill/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ console.log( x.get( 0 ).toString() );

#### stdlib_strided_cfill( N, alpha, \*X, strideX )

Fills a single-precision floating-point strided array `X` with a specified scalar constant `alpha`.
Fills a single-precision complex floating-point strided array `X` with a specified scalar constant `alpha`.

```c
float x[] = { 1.0f, 2.0f, 3.0f, 4.0f };
Expand Down Expand Up @@ -321,13 +321,13 @@ stdlib_strided_cfill_ndarray( 4, alpha, (stdlib_complex64_t *x), 1, 0 );
The function accepts the following arguments:

- **N**: `[in] CBLAS_INT` number of indexed elements.
- **alpha**: `[in] stlib_complex64_t` scalar constant.
- **alpha**: `[in] stdlib_complex64_t` scalar constant.
- **X**: `[out] stdlib_complex64_t*` input array.
- **strideX**: `[in] CBLAS_INT` index increment for `X`.
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.

```c
void stdlib_strided_cfill_ndarray( const CBLAS_INT N, const stdlib_complex64_t alpha, stdlib_complex_64_t *X, const CBLAS_INT strideX, const CBLAS_INT offsetX );
void stdlib_strided_cfill_ndarray( const CBLAS_INT N, const stdlib_complex64_t alpha, stdlib_complex64_t *X, const CBLAS_INT strideX, const CBLAS_INT offsetX );
```

</section>
Expand Down Expand Up @@ -367,7 +367,7 @@ int main( void ) {
const int strideX = 1;

// Fill the array:
stdlib_strided_cfill( N, alpha, (stdlib_complex_64_t *)x, strideX );
stdlib_strided_cfill( N, alpha, (stdlib_complex64_t *)x, strideX );

// Print the result:
for ( int i = 0; i < N; i++ ) {
Expand Down
152 changes: 140 additions & 12 deletions lib/node_modules/@stdlib/blas/ext/base/zfill/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ limitations under the License.
var zfill = require( '@stdlib/blas/ext/base/zfill' );
```

#### zfill( N, alpha, x, stride )
#### zfill( N, alpha, x, strideX )

Fills a double-precision complex floating-point strided array `x` with a specified scalar constant `alpha`.

Expand Down Expand Up @@ -63,7 +63,7 @@ The function has the following parameters:
- **N**: number of indexed elements.
- **alpha**: scalar constant.
- **x**: input [`Complex128Array`][@stdlib/array/complex128].
- **stride**: index increment.
- **strideX**: index increment.

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

Expand Down Expand Up @@ -143,7 +143,7 @@ im = imag( y );
// returns 10.0
```

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

Fills a double-precision complex floating-point strided array `x` with a specified scalar constant `alpha` using alternative indexing semantics.

Expand Down Expand Up @@ -173,9 +173,11 @@ var im = imag( y );

The function has the following additional parameters:

- **offset**: starting index.
- **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 two elements of the strided array
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 two elements of the strided array

<!-- eslint-disable stdlib/doctest -->
headlessNode marked this conversation as resolved.
Show resolved Hide resolved

```javascript
var Float64Array = require( '@stdlib/array/float64' );
Expand Down Expand Up @@ -219,6 +221,8 @@ im = imag( y );
// returns 10.0
```

<!-- eslint-enable stdlib/doctest -->

</section>

<!-- /.usage -->
Expand All @@ -240,16 +244,15 @@ im = imag( y );
<!-- eslint no-undef: "error" -->

```javascript
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
var filledarrayBy = require( '@stdlib/array/filled-by' );
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var Complex128 = require( '@stdlib/complex/float64/ctor' );
var Complex128Array = require( '@stdlib/array/complex128' );
var zfill = require( '@stdlib/blas/ext/base/zfill' );

function rand() {
return new Complex128( discreteUniform( 0, 10 ), discreteUniform( -5, 5 ) );
}

var x = filledarrayBy( 10, 'complex128', rand );
var xbuf = discreteUniform( 20, -100, 100, {
'dtype': 'float64'
});
var x = new Complex128Array( xbuf.buffer );
var alpha = new Complex128( 10.0, 10.0 );

zfill( x.length, alpha, x, 1 );
Expand All @@ -260,6 +263,131 @@ console.log( x.get( 0 ).toString() );

<!-- /.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/zfill.h"
```

#### stdlib_strided_zfill( N, alpha, \*X, strideX )

Fills a double-precision complex floating-point strided array `X` with a specified scalar constant `alpha`.

```c
double x[] = { 1.0, 2.0, 3.0, 4.0 };
const stdlib_complex128_t alpha = stdlib_complex128( 2.0, 2.0 );

stdlib_strided_zfill( 2, alpha, (stdlib_complex128_t *)x, 1 );
```

The function accepts the following arguments:

- **N**: `[in] CBLAS_INT` number of indexed elements.
- **alpha**: `[in] stdlib_complex128_t` scalar constant.
- **X**: `[out] stdlib_complex128_t*` input array.
- **strideX**: `[in] CBLAS_INT` index increment for `X`.

```c
void stdlib_strided_zfill( const CBLAS_INT N, const stdlib_complex128_t alpha, stdlib_complex128_t *X, const CBLAS_INT strideX );
```

#### stdlib_strided_zfill_ndarray( N, alpha, \*X, strideX, offsetX )

Fills a double-precision complex floating-point strided array `X` with a specified scalar constant `alpha` using alternative indexing semantics.

```c
double x[] = { 1.0, 2.0, 3.0, 4.0 };
const stdlib_complex128_t alpha = stdlib_complex128( 2.0, 2.0 );

stdlib_strided_zfill_ndarray( 4, alpha, (stdlib_complex128_t *x), 1, 0 );
```

The function accepts the following arguments:

- **N**: `[in] CBLAS_INT` number of indexed elements.
- **alpha**: `[in] stdlib_complex128_t` scalar constant.
- **X**: `[out] stdlib_complex128_t*` input array.
- **strideX**: `[in] CBLAS_INT` index increment for `X`.
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.

```c
void stdlib_strided_zfill_ndarray( const CBLAS_INT N, const stdlib_complex128_t alpha, stdlib_complex128_t *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/complex/float64/ctor.h"
#include "stdlib/blas/ext/base/zfill.h"
#include <stdio.h>

int main() {
// Create a strided array of interleaved real and imaginary components:
double x[] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 };

// Create a scalar constant:
const stdlib_complex128_t alpha = stdlib_complex128( 2.0, 2.0 );

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

// Specify a stride:
const int strideX = 1;

// Fill the array:
stdlib_strided_zfill( N, alpha, (stdlib_complex128_t *)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">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ static double rand_double( void ) {
* @param len array length
* @return elapsed time in seconds
*/
static double benchmark( int iterations, int len ) {
static double benchmark1( int iterations, int len ) {
stdlib_complex128_t alpha;
double elapsed;
double *x;
Expand All @@ -110,7 +110,43 @@ static double benchmark( int iterations, int len ) {
}
t = tic();
for ( i = 0; i < iterations; i++ ) {
c_zfill( len, alpha, (stdlib_complex128_t *)x, 1 );
stdlib_strided_zfill( len, alpha, (stdlib_complex128_t *)x, 1 );
if ( x[ 0 ] != x[ 0 ] ) {
printf( "should not return NaN\n" );
break;
}
}
elapsed = tic() - t;
if ( x[ 0 ] != x[ 0 ] ) {
printf( "should not return NaN\n" );
}
free( x );
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 ) {
stdlib_complex128_t alpha;
double elapsed;
double *x;
double t;
int i;

x = (double *)malloc( len * 2 * sizeof( double ) );
alpha = stdlib_complex128( 1.0, 0.0 );
for ( i = 0; i < len*2; i+=2 ) {
x[ i ] = ( rand_double()*2.0 ) - 1.0;
x[ i+1 ] = ( rand_double()*2.0 ) - 1.0;
}
t = tic();
for ( i = 0; i < iterations; i++ ) {
stdlib_strided_zfill_ndarray( len, alpha, (stdlib_complex128_t *)x, 1, 0 );
if ( x[ 0 ] != x[ 0 ] ) {
printf( "should not return NaN\n" );
break;
Expand Down Expand Up @@ -146,7 +182,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
10 changes: 5 additions & 5 deletions lib/node_modules/@stdlib/blas/ext/base/zfill/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

{{alias}}( N, alpha, x, stride )
{{alias}}( N, alpha, x, strideX )
Fills a double-precision complex floating-point strided array with a
specified scalar constant.

Expand All @@ -22,7 +22,7 @@
x: Complex128Array
Input array.

stride: integer
strideX: integer
Index increment.

Returns
Expand Down Expand Up @@ -74,7 +74,7 @@
-5.0


{{alias}}.ndarray( N, alpha, x, stride, offset )
{{alias}}.ndarray( N, alpha, x, strideX, offsetX )
Fills a double-precision complex floating-point strided array with a
specified scalar constant using alternative indexing semantics.

Expand All @@ -93,10 +93,10 @@
x: Complex128Array
Input array.

stride: integer
strideX: integer
Index increment.

offset: integer
offsetX: integer
Starting index.

Returns
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ interface Routine {
* @param N - number of indexed elements
* @param alpha - scalar constant
* @param x - input array
* @param stride - stride length
* @param strideX - stride length
* @returns input array
*
* @example
Expand All @@ -59,16 +59,16 @@ interface Routine {
* var im = imag( y );
* // returns 10.0
*/
( N: number, alpha: Complex128, x: Complex128Array, stride: number ): Complex128Array;
( N: number, alpha: Complex128, x: Complex128Array, strideX: number ): Complex128Array;

/**
* Fills a double-precision complex floating-point strided array with a specified scalar constant.
*
* @param N - number of indexed elements
* @param alpha - scalar constant
* @param x - input array
* @param stride - stride length
* @param offset - starting index
* @param strideX - stride length
* @param offsetX - starting index
* @returns input array
*
* @example
Expand All @@ -94,7 +94,7 @@ interface Routine {
* var im = imag( y );
* // returns 10.0
*/
ndarray( N: number, alpha: Complex128, x: Complex128Array, stride: number, offset: number ): Complex128Array;
ndarray( N: number, alpha: Complex128, x: Complex128Array, strideX: number, offsetX: number ): Complex128Array;
}

/**
Expand All @@ -103,7 +103,7 @@ interface Routine {
* @param N - number of indexed elements
* @param alpha - scalar constant
* @param x - input array
* @param stride - index increment
* @param strideX - index increment
* @returns input array
*
* @example
Expand Down
Loading
Loading