Skip to content

Commit

Permalink
feat: add C ndarray implementation for zscal
Browse files Browse the repository at this point in the history
---
type: pre_commit_static_analysis_report
description: Results of running static analysis checks when committing changes.
report:
  - task: lint_filenames
    status: passed
  - task: lint_editorconfig
    status: passed
  - task: lint_markdown
    status: passed
  - task: lint_package_json
    status: na
  - task: lint_repl_help
    status: na
  - task: lint_javascript_src
    status: passed
  - task: lint_javascript_cli
    status: na
  - task: lint_javascript_examples
    status: na
  - task: lint_javascript_tests
    status: passed
  - task: lint_javascript_benchmarks
    status: na
  - task: lint_python
    status: na
  - task: lint_r
    status: na
  - task: lint_c_src
    status: missing_dependencies
  - task: lint_c_examples
    status: missing_dependencies
  - task: lint_c_benchmarks
    status: missing_dependencies
  - task: lint_c_tests_fixtures
    status: na
  - task: lint_shell
    status: na
  - task: lint_typescript_declarations
    status: na
  - task: lint_typescript_tests
    status: na
  - task: lint_license_headers
    status: passed
---

---
type: pre_push_report
description: Results of running various checks prior to pushing changes.
report:
  - task: run_javascript_examples
    status: na
  - task: run_c_examples
    status: na
  - task: run_cpp_examples
    status: na
  - task: run_javascript_readme_examples
    status: na
  - task: run_c_benchmarks
    status: na
  - task: run_cpp_benchmarks
    status: na
  - task: run_fortran_benchmarks
    status: na
  - task: run_javascript_benchmarks
    status: na
  - task: run_julia_benchmarks
    status: na
  - task: run_python_benchmarks
    status: na
  - task: run_r_benchmarks
    status: na
  - task: run_javascript_tests
    status: na
---
  • Loading branch information
aman-095 committed Jan 23, 2025
1 parent 1e28982 commit 73cec23
Show file tree
Hide file tree
Showing 18 changed files with 596 additions and 351 deletions.
39 changes: 36 additions & 3 deletions lib/node_modules/@stdlib/blas/base/zscal/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
@license Apache-2.0
Copyright (c) 2024 The Stdlib Authors.
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.
Expand Down Expand Up @@ -58,7 +58,7 @@ var im = imag( z );
The function has the following parameters:

- **N**: number of indexed elements.
- **za**: scalar [`Complex128`][@stdlib/complex/float64/ctor] constant.
- **za**: scalar [`Complex128`][@stdlib/complex/float64/ctor] constant.
- **zx**: input [`Complex128Array`][@stdlib/array/complex128].
- **strideX**: index increment for `zx`.

Expand Down Expand Up @@ -265,6 +265,31 @@ The function accepts the following arguments:
void c_zscal( const CBLAS_INT N, const stdlib_complex128_t za, void *ZX, const CBLAS_INT strideX );
```

#### c_zscal_ndarray( N, za, \*ZX, strideX, offsetX )

Scales values from `ZX` by `za` using alternative indexing semantics.

```c
#include "stdlib/complex/float64/ctor.h"

double zx[] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 };
const stdlib_complex128_t za = stdlib_complex128( 2.0, 2.0 );

c_zscal_ndarray( 4, za, (void *)zx, 1, 0 );
```
The function accepts the following arguments:
- **N**: `[in] CBLAS_INT` number of indexed elements.
- **za**: `[in] stdlib_complex128_t` scalar constant.
- **ZX**: `[inout] void*` input array.
- **strideX**: `[in] CBLAS_INT` index increment for `ZX`.
- **offsetX**: `[in] CBLAS_INT` starting index for `ZX`.
```c
void c_zscal_ndarray( const CBLAS_INT N, const stdlib_complex128_t za, void *ZX, const CBLAS_INT strideX, const CBLAS_INT offsetX );
```

</section>

<!-- /.usage -->
Expand Down Expand Up @@ -306,7 +331,15 @@ int main( void ) {

// Print the result:
for ( int i = 0; i < N; i++ ) {
printf( "zx[ %i ] = %f + %fj\n", i, zx[ i*2 ], zx[ (i*2)+1 ] );
printf( "zx[ %i ] = %lf + %lfj\n", i, zx[ i*2 ], zx[ (i*2)+1 ] );
}

// Scale the elements of the array using alternative indexing semantics:
c_zscal_ndarray( N, za, (void *)zx, -strideX, N-1 );

// Print the result:
for ( int i = 0; i < N; i++ ) {
printf( "zx[ %i ] = %lf + %lfj\n", i, zx[ i*2 ], zx[ (i*2)+1 ] );
}
}
```
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 The Stdlib Authors.
* 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.
Expand Down 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 za;
double zx[ len*2 ];
double elapsed;
Expand All @@ -122,6 +122,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 ) {
stdlib_complex128_t za;
double zx[ len*2 ];
double elapsed;
double t;
int i;

za = stdlib_complex128( 1.0, 0.0 );
for ( i = 0; i < len*2; i+=2 ) {
zx[ i ] = ( rand_double()*2.0 ) - 1.0;
zx[ i+1 ] = ( rand_double()*2.0 ) - 1.0;
}
t = tic();
for ( i = 0; i < iterations; i++ ) {
c_zscal_ndarray( len, za, (void *)zx, 1, 0 );
if ( zx[ 0 ] != zx[ 0 ] ) {
printf( "should not return NaN\n" );
break;
}
}
elapsed = tic() - t;
if ( zx[ 0 ] != zx[ 0 ] ) {
printf( "should not return NaN\n" );
}
return elapsed;
}

/**
* Main execution sequence.
*/
Expand All @@ -144,7 +178,14 @@ 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 ( 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: 9 additions & 1 deletion lib/node_modules/@stdlib/blas/base/zscal/examples/c/example.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 The Stdlib Authors.
* 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.
Expand Down Expand Up @@ -40,4 +40,12 @@ int main( void ) {
for ( int i = 0; i < N; i++ ) {
printf( "zx[ %i ] = %lf + %lfj\n", i, zx[ i*2 ], zx[ (i*2)+1 ] );
}

// Scale the elements of the array using alternative indexing semantics:
c_zscal_ndarray( N, za, (void *)zx, -strideX, N-1 );

// Print the result:
for ( int i = 0; i < N; i++ ) {
printf( "zx[ %i ] = %lf + %lfj\n", i, zx[ i*2 ], zx[ (i*2)+1 ] );
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 The Stdlib Authors.
* 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.
Expand Down Expand Up @@ -37,6 +37,11 @@ extern "C" {
*/
void API_SUFFIX(c_zscal)( const CBLAS_INT N, const stdlib_complex128_t za, void *ZX, const CBLAS_INT strideX );

/**
* Scales a double-precision complex floating-point vector by a double-precision complex floating-point constant using alternative indexing semantics.
*/
void API_SUFFIX(c_zscal_ndarray)( const CBLAS_INT N, const stdlib_complex128_t za, void *ZX, const CBLAS_INT strideX, const CBLAS_INT offsetX );

#ifdef __cplusplus
}
#endif
Expand Down
37 changes: 8 additions & 29 deletions lib/node_modules/@stdlib/blas/base/zscal/lib/ndarray.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 The Stdlib Authors.
* 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.
Expand All @@ -20,10 +20,7 @@

// MODULES //

var reinterpret = require( '@stdlib/strided/base/reinterpret-complex128' );
var cmul = require( '@stdlib/complex/float64/base/mul' ).assign;
var real = require( '@stdlib/complex/float64/real' );
var imag = require( '@stdlib/complex/float64/imag' );
var cmul = require( '@stdlib/complex/float64/base/mul' );


// MAIN //
Expand All @@ -34,8 +31,8 @@ var imag = require( '@stdlib/complex/float64/imag' );
* @param {PositiveInteger} N - number of indexed elements
* @param {Complex128} za - constant
* @param {Complex128Array} zx - input array
* @param {integer} strideZX - `zx` stride length
* @param {NonNegativeInteger} offsetZX - starting `zx` index
* @param {integer} strideX - `zx` stride length
* @param {NonNegativeInteger} offsetX - starting index for `zx`
* @returns {Complex128Array} input array
*
* @example
Expand All @@ -48,35 +45,17 @@ var imag = require( '@stdlib/complex/float64/imag' );
* zscal( 3, za, zx, 1, 0 );
* // zx => <Complex128Array>[ -2.0, 6.0, -2.0, 14.0, -2.0, 22.0 ]
*/
function zscal( N, za, zx, strideZX, offsetZX ) {
var view;
var re1;
var im1;
var re2;
var im2;
var sx;
function zscal( N, za, zx, strideX, offsetX ) {
var ix;
var i;

if ( N <= 0 ) {
return zx;
}
// Reinterpret the input array as a real-valued array of interleaved real and imaginary components:
view = reinterpret( zx, 0 );

// Adjust the stride and offset:
sx = strideZX * 2;
ix = offsetZX * 2;

// Decompose the input scalar to real and imaginary components:
re1 = real( za );
im1 = imag( za );

ix = offsetX;
for ( i = 0; i < N; i++ ) {
re2 = view[ ix ];
im2 = view[ ix+1 ];
cmul( re1, im1, re2, im2, view, 1, ix );
ix += sx;
zx.set( cmul( za, zx.get( ix ) ), ix );
ix += strideX;
}
return zx;
}
Expand Down
15 changes: 6 additions & 9 deletions lib/node_modules/@stdlib/blas/base/zscal/lib/ndarray.native.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 The Stdlib Authors.
* 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.
Expand All @@ -21,7 +21,6 @@
// MODULES //

var reinterpret = require( '@stdlib/strided/base/reinterpret-complex128' );
var minViewBufferIndex = require( '@stdlib/strided/base/min-view-buffer-index' );
var addon = require( './../src/addon.node' );


Expand All @@ -33,8 +32,8 @@ var addon = require( './../src/addon.node' );
* @param {PositiveInteger} N - number of indexed elements
* @param {Complex128} za - scalar constant
* @param {Complex128Array} zx - input array
* @param {integer} strideZX - `zx` stride length
* @param {NonNegativeInteger} offsetZX - starting `zx` index
* @param {integer} strideX - `zx` stride length
* @param {NonNegativeInteger} offsetX - starting index for `zx`
* @returns {Complex128Array} input array
*
* @example
Expand All @@ -47,11 +46,9 @@ var addon = require( './../src/addon.node' );
* zscal( 3, za, zx, 1, 0 );
* // zx => <Complex128Array>[ -2.0, 6.0, -2.0, 14.0, -2.0, 22.0 ]
*/
function zscal( N, za, zx, strideZX, offsetZX ) {
var viewZX;
offsetZX = minViewBufferIndex( N, strideZX, offsetZX );
viewZX = reinterpret( zx, offsetZX );
addon( N, za, viewZX, strideZX );
function zscal( N, za, zx, strideX, offsetX ) {
var viewZX = reinterpret( zx, 0 );
addon.ndarray( N, za, viewZX, strideX, offsetX );
return zx;
}

Expand Down
9 changes: 5 additions & 4 deletions lib/node_modules/@stdlib/blas/base/zscal/lib/zscal.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 The Stdlib Authors.
* 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.
Expand Down Expand Up @@ -32,7 +32,7 @@ var ndarray = require( './ndarray.js' );
* @param {PositiveInteger} N - number of indexed elements
* @param {Complex128} za - constant
* @param {Complex128Array} zx - input array
* @param {integer} strideZX - `zx` stride length
* @param {integer} strideX - `zx` stride length
* @returns {Complex128Array} input array
*
* @example
Expand All @@ -45,8 +45,9 @@ var ndarray = require( './ndarray.js' );
* zscal( 3, za, zx, 1 );
* // zx => <Complex128Array>[ -2.0, 6.0, -2.0, 14.0, -2.0, 22.0 ]
*/
function zscal( N, za, zx, strideZX ) {
return ndarray( N, za, zx, strideZX, stride2offset( N, strideZX ) );
function zscal( N, za, zx, strideX ) {
var ox = stride2offset( N, strideX );
return ndarray( N, za, zx, strideX, ox );
}


Expand Down
8 changes: 4 additions & 4 deletions lib/node_modules/@stdlib/blas/base/zscal/lib/zscal.native.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 The Stdlib Authors.
* 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.
Expand Down Expand Up @@ -32,7 +32,7 @@ var addon = require( './../src/addon.node' );
* @param {PositiveInteger} N - number of indexed elements
* @param {Complex128} za - scalar constant
* @param {Complex128Array} zx - input array
* @param {integer} strideZX - `zx` stride length
* @param {integer} strideX - `zx` stride length
* @returns {Complex128Array} input array
*
* @example
Expand All @@ -45,9 +45,9 @@ var addon = require( './../src/addon.node' );
* zscal( 3, za, zx, 1 );
* // zx => <Complex128Array>[ -2.0, 6.0, -2.0, 14.0, -2.0, 22.0 ]
*/
function zscal( N, za, zx, strideZX ) {
function zscal( N, za, zx, strideX ) {
var viewZX = reinterpret( zx, 0 );
addon( N, za, viewZX, strideZX );
addon( N, za, viewZX, strideX );
return zx;
}

Expand Down
Loading

0 comments on commit 73cec23

Please sign in to comment.