From 2ea4452fa0f63499be526f392fa7fdd647d1a9b5 Mon Sep 17 00:00:00 2001 From: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Date: Sun, 19 Jan 2025 13:59:39 +0500 Subject: [PATCH] feat!: add C `ndarray` API and refactor `blas/ext/base/sapx` This PR renames `c_sapx` to `stdlib_strided_sapx`. The original naming convention stemmed from early construction of the `blas/ext/base` namespace in which it was not known whether to follow BLAS conventions (e.g., `c_*`) or stdlib's strided naming conventions (e.g., `stdlib_strided_`). Ultimately, we've opted to only use BLAS and LAPACK conventions for the known symbols in those respective libraries in order to avoid future naming collisions and explicitly distinguish stdlib's extensions from that of symbols whose origins are found elsewhere. BREAKING CHANGE: rename `c_sapx` to `stdlib_strided_sapx` To migrate, users should replace all instances of `c_sapx` with `stdlib_strided_sapx`. PR-URL: https://github.com/stdlib-js/stdlib/pull/4696 Co-authored-by: Athan Reines Reviewed-by: Athan Reines --- .../@stdlib/blas/ext/base/sapx/README.md | 144 ++++++++++++++++-- .../blas/ext/base/sapx/benchmark/benchmark.js | 9 +- .../base/sapx/benchmark/benchmark.native.js | 9 +- .../base/sapx/benchmark/benchmark.ndarray.js | 9 +- .../benchmark/benchmark.ndarray.native.js | 9 +- .../base/sapx/benchmark/c/benchmark.length.c | 50 +++++- .../@stdlib/blas/ext/base/sapx/docs/repl.txt | 32 ++-- .../blas/ext/base/sapx/docs/types/index.d.ts | 24 +-- .../blas/ext/base/sapx/examples/c/example.c | 10 +- .../blas/ext/base/sapx/examples/index.js | 8 +- .../sapx/include/stdlib/blas/ext/base/sapx.h | 11 +- .../@stdlib/blas/ext/base/sapx/lib/index.js | 2 +- .../@stdlib/blas/ext/base/sapx/lib/ndarray.js | 21 ++- .../blas/ext/base/sapx/lib/ndarray.native.js | 22 +-- .../@stdlib/blas/ext/base/sapx/lib/sapx.js | 53 +------ .../blas/ext/base/sapx/lib/sapx.native.js | 10 +- .../@stdlib/blas/ext/base/sapx/manifest.json | 32 ++-- .../@stdlib/blas/ext/base/sapx/package.json | 2 +- .../@stdlib/blas/ext/base/sapx/src/addon.c | 27 +++- .../@stdlib/blas/ext/base/sapx/src/main.c | 84 ++++++++++ .../@stdlib/blas/ext/base/sapx/src/sapx.c | 70 --------- 21 files changed, 401 insertions(+), 237 deletions(-) create mode 100644 lib/node_modules/@stdlib/blas/ext/base/sapx/src/main.c delete mode 100644 lib/node_modules/@stdlib/blas/ext/base/sapx/src/sapx.c diff --git a/lib/node_modules/@stdlib/blas/ext/base/sapx/README.md b/lib/node_modules/@stdlib/blas/ext/base/sapx/README.md index d5021571d5ea..438f5be61036 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/sapx/README.md +++ b/lib/node_modules/@stdlib/blas/ext/base/sapx/README.md @@ -20,7 +20,7 @@ limitations under the License. # sapx -> Add a constant to each element in a single-precision floating-point strided array. +> Add a scalar constant to each element in a single-precision floating-point strided array.
@@ -30,9 +30,9 @@ limitations under the License. var sapx = require( '@stdlib/blas/ext/base/sapx' ); ``` -#### sapx( N, alpha, x, stride ) +#### sapx( N, alpha, x, strideX ) -Adds a constant `alpha` to each element in a single-precision floating-point strided array `x`. +Adds a scalar constant to each element in a single-precision floating-point strided array. ```javascript var Float32Array = require( '@stdlib/array/float32' ); @@ -48,9 +48,9 @@ The function has the following parameters: - **N**: number of indexed elements. - **alpha**: scalar constant. - **x**: input [`Float32Array`][@stdlib/array/float32]. -- **stride**: index increment. +- **strideX**: stride length. -The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to add a constant to every other element +The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to add a constant to every other element: ```javascript var Float32Array = require( '@stdlib/array/float32' ); @@ -77,9 +77,9 @@ sapx( 3, 5.0, x1, 2 ); // x0 => [ 1.0, 3.0, 3.0, 1.0, 5.0, -1.0 ] ``` -#### sapx.ndarray( N, alpha, x, stride, offset ) +#### sapx.ndarray( N, alpha, x, strideX, offsetX ) -Adds a constant `alpha` to each element in a single-precision floating-point strided array `x` using alternative indexing semantics. +Adds a scalar constant to each element in a single-precision floating-point strided array using alternative indexing semantics. ```javascript var Float32Array = require( '@stdlib/array/float32' ); @@ -92,9 +92,9 @@ sapx.ndarray( x.length, 5.0, x, 1, 0 ); 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 three 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 three elements of the strided array: ```javascript var Float32Array = require( '@stdlib/array/float32' ); @@ -126,11 +126,12 @@ sapx.ndarray( 3, 5.0, x, 1, x.length-3 ); ```javascript -var uniform = require( '@stdlib/random/base/uniform' ).factory; -var filledarrayBy = require( '@stdlib/array/filled-by' ); +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); var sapx = require( '@stdlib/blas/ext/base/sapx' ); -var x = filledarrayBy( 10, 'float32', uniform( -100.0, 100.0 ) ); +var x = discreteUniform( 10, -100, 100, { + 'dtype': 'float32' +}); console.log( x ); sapx( x.length, 5.0, x, 1 ); @@ -141,6 +142,125 @@ console.log( x ); + + +* * * + +
+ +## C APIs + + + +
+ +
+ + + + + +
+ +### Usage + +```c +#include "stdlib/blas/ext/base/sapx.h" +``` + +#### stdlib_strided_sapx( N, alpha, \*X, strideX ) + +Adds a scalar constant to each element in a single-precision floating-point strided array. + +```c +float x[] = { 1.0f, 2.0f, 3.0f, 4.0f }; + +stdlib_strided_sapx( 4, 5.0f, x, 1 ); +``` + +The function accepts the following arguments: + +- **N**: `[in] CBLAS_INT` number of indexed elements. +- **alpha**: `[in] float` scalar constant. +- **X**: `[inout] float*` input array. +- **strideX**: `[in] CBLAS_INT` stride length. + +```c +void stdlib_strided_sapx( const CBLAS_INT N, const float alpha, float *X, const CBLAS_INT strideX ); +``` + +#### stdlib_strided_sapx_ndarray( N, alpha, \*X, strideX, offsetX ) + +Adds a scalar constant to each element in a single-precision floating-point strided array using alternative indexing semantics. + +```c +float x[] = { 1.0f, 2.0f, 3.0f, 4.0f }; + +stdlib_strided_sapx_ndarray( 4, 5.0f, x, 1, 0 ); +``` + +The function accepts the following arguments: + +- **N**: `[in] CBLAS_INT` number of indexed elements. +- **alpha**: `[in] float` scalar constant. +- **X**: `[inout] float*` input array. +- **strideX**: `[in] CBLAS_INT` stride length. +- **offsetX**: `[in] CBLAS_INT` starting index. + +```c +void stdlib_strided_sapx_ndarray( const CBLAS_INT N, const float alpha, float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX ); +``` + +
+ + + + + +
+ +
+ + + + + +
+ +### Examples + +```c +#include "stdlib/blas/ext/base/sapx.h" +#include + +int main( void ) { + // Create a strided array: + float x[] = { 1.0f, -2.0f, 3.0f, -4.0f, 5.0f, -6.0f, 7.0f, -8.0f }; + + // Specify the number of indexed elements: + const int N = 8; + + // Specify a stride: + const int strideX = 1; + + // Add a constant to each element: + stdlib_strided_sapx( N, 5.0f, x, strideX ); + + // Print the result: + for ( int i = 0; i < 8; i++ ) { + printf( "x[ %i ] = %f\n", i, x[ i ] ); + } +} +``` + +
+ + + +
+ + +