From 5ddc7301b0d83c67f48f1fab3bc38d0a29d80469 Mon Sep 17 00:00:00 2001 From: yb175 Date: Wed, 14 Oct 2026 09:22:30 +0000 Subject: [PATCH 1/7] feat: add C implementation for blas/ext/base/ndarray/ctril --- .../blas/ext/base/ndarray/ctril/README.md | 174 ++++++++++++- .../base/ndarray/ctril/benchmark/benchmark.js | 2 +- .../ctril/benchmark/benchmark.native.js | 126 +++++++++ .../base/ndarray/ctril/benchmark/c/Makefile | 146 +++++++++++ .../ctril/benchmark/c/benchmark.length.c | 236 +++++++++++++++++ .../blas/ext/base/ndarray/ctril/binding.gyp | 170 ++++++++++++ .../blas/ext/base/ndarray/ctril/docs/repl.txt | 6 +- .../base/ndarray/ctril/docs/types/index.d.ts | 4 +- .../ext/base/ndarray/ctril/docs/types/test.ts | 4 +- .../base/ndarray/ctril/examples/c/Makefile | 146 +++++++++++ .../base/ndarray/ctril/examples/c/example.c | 99 +++++++ .../ext/base/ndarray/ctril/examples/index.js | 2 +- .../blas/ext/base/ndarray/ctril/include.gypi | 53 ++++ .../stdlib/blas/ext/base/ndarray/ctril.h | 48 ++++ .../blas/ext/base/ndarray/ctril/lib/index.js | 18 +- .../blas/ext/base/ndarray/ctril/lib/main.js | 4 +- .../blas/ext/base/ndarray/ctril/lib/native.js | 74 ++++++ .../blas/ext/base/ndarray/ctril/manifest.json | 114 ++++++++ .../blas/ext/base/ndarray/ctril/package.json | 4 + .../blas/ext/base/ndarray/ctril/src/Makefile | 70 +++++ .../blas/ext/base/ndarray/ctril/src/addon.c | 67 +++++ .../blas/ext/base/ndarray/ctril/src/main.c | 61 +++++ .../blas/ext/base/ndarray/ctril/test/test.js | 212 ++------------- .../ext/base/ndarray/ctril/test/test.main.js | 237 +++++++++++++++++ .../base/ndarray/ctril/test/test.native.js | 246 ++++++++++++++++++ 25 files changed, 2121 insertions(+), 202 deletions(-) create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/ctril/benchmark/benchmark.native.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/ctril/benchmark/c/Makefile create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/ctril/benchmark/c/benchmark.length.c create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/ctril/binding.gyp create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/ctril/examples/c/Makefile create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/ctril/examples/c/example.c create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/ctril/include.gypi create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/ctril/include/stdlib/blas/ext/base/ndarray/ctril.h create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/ctril/lib/native.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/ctril/manifest.json create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/ctril/src/Makefile create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/ctril/src/addon.c create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/ctril/src/main.c create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/ctril/test/test.main.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/ctril/test/test.native.js diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/ctril/README.md b/lib/node_modules/@stdlib/blas/ext/base/ndarray/ctril/README.md index e4c7c965bb03..3ef115912fef 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/ctril/README.md +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/ctril/README.md @@ -50,7 +50,7 @@ var A = new Complex64Matrix( [ [ 1.0, 2.0, 3.0, 4.0 ], [ 5.0, 6.0, 7.0, 8.0 ] ] var B = new Complex64Matrix( [ [ 0.0, 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0, 0.0 ] ] ); var k = scalar2ndarray( 0, { - 'dtype': 'generic' + 'dtype': 'int32' }); var out = ctril( [ A, B, k ] ); @@ -66,7 +66,7 @@ The function has the following parameters: - a two-dimensional input ndarray corresponding to `A`. - a two-dimensional output ndarray corresponding to `B`. - - a zero-dimensional ndarray specifying the diagonal above which to ignore. A value equal to zero refers to the main diagonal, greater than zero refers to a diagonal above the main diagonal, and less than zero refers to a diagonal below the main diagonal. + - a zero-dimensional int32 ndarray specifying the diagonal above which to ignore. A value equal to zero refers to the main diagonal, greater than zero refers to a diagonal above the main diagonal, and less than zero refers to a diagonal below the main diagonal. @@ -108,7 +108,7 @@ var B = new Complex64Matrix( discreteUniform( 5*5*2, -50, 50, opts ).buffer, 0, console.log( ndarray2array( B ) ); var k = scalar2ndarray( 0, { - 'dtype': 'generic' + 'dtype': 'int32' }); var out = ctril( [ A, B, k ] ); @@ -119,6 +119,174 @@ console.log( ndarray2array( out ) ); + + +* * * + +
+ +## C APIs + +
+ +
+ + + +
+ +### Usage + +```c +#include "stdlib/blas/ext/base/ndarray/ctril.h" +``` + +#### stdlib_blas_ext_ctril( arrays ) + +Copies the lower triangular part of a single-precision complex floating-point matrix `A` to another matrix `B`. + +```c +#include "stdlib/blas/ext/base/ndarray/ctril.h" +#include "stdlib/ndarray/ctor.h" +#include "stdlib/ndarray/dtypes.h" +#include "stdlib/ndarray/index_modes.h" +#include "stdlib/ndarray/orders.h" +#include "stdlib/ndarray/base/bytes_per_element.h" +#include "stdlib/complex/float32/ctor.h" +#include +#include +#include + +int main( void ) { + const stdlib_complex64_t dataA[] = { + stdlib_complex64( 1.0f, 1.0f ), stdlib_complex64( 2.0f, 2.0f ), + stdlib_complex64( 3.0f, 3.0f ), stdlib_complex64( 4.0f, 4.0f ) + }; + stdlib_complex64_t dataB[] = { + stdlib_complex64( 0.0f, 0.0f ), stdlib_complex64( 0.0f, 0.0f ), + stdlib_complex64( 0.0f, 0.0f ), stdlib_complex64( 0.0f, 0.0f ) + }; + int32_t dataK[] = { 0 }; + const int64_t ndims = 2; + int64_t shape[] = { 2, 2 }; + int64_t strides[] = { + 2 * STDLIB_NDARRAY_COMPLEX64_BYTES_PER_ELEMENT, + STDLIB_NDARRAY_COMPLEX64_BYTES_PER_ELEMENT + }; + int64_t kstrides[] = { STDLIB_NDARRAY_INT32_BYTES_PER_ELEMENT }; + const int64_t offset = 0; + const enum STDLIB_NDARRAY_ORDER order = STDLIB_NDARRAY_ROW_MAJOR; + const enum STDLIB_NDARRAY_INDEX_MODE imode = STDLIB_NDARRAY_INDEX_ERROR; + int8_t submodes[] = { STDLIB_NDARRAY_INDEX_ERROR }; + const int64_t nsubmodes = 1; + struct ndarray *A = stdlib_ndarray_allocate( STDLIB_NDARRAY_COMPLEX64, (uint8_t *)dataA, ndims, shape, strides, offset, order, imode, nsubmodes, submodes ); + struct ndarray *B = stdlib_ndarray_allocate( STDLIB_NDARRAY_COMPLEX64, (uint8_t *)dataB, ndims, shape, strides, offset, order, imode, nsubmodes, submodes ); + struct ndarray *k = stdlib_ndarray_allocate( STDLIB_NDARRAY_INT32, (uint8_t *)dataK, 0, NULL, kstrides, offset, order, imode, nsubmodes, submodes ); + if ( A == NULL || B == NULL || k == NULL ) { + fprintf( stderr, "Error allocating memory.\n" ); + exit( 1 ); + } + const struct ndarray *arrays[] = { A, B, k }; + stdlib_blas_ext_ctril( arrays ); + stdlib_ndarray_free( A ); + stdlib_ndarray_free( B ); + stdlib_ndarray_free( k ); +} +``` + +The function accepts an ordered list of ndarrays: + +- **arrays**: `[in] struct ndarray**` containing `A`, `B`, and `k` in that order. + - `[in]` two-dimensional complex64 input ndarray `A`. + - `[out]` two-dimensional complex64 output ndarray `B`. + - `[in]` zero-dimensional int32 ndarray `k`, the diagonal offset. + +The `k` value must be representable as an int32. A value of zero selects the main diagonal; positive values include diagonals above it, and negative values include diagonals below it. + +```c +void stdlib_blas_ext_ctril( const struct ndarray *arrays[] ); +``` + +
+ + + +
+ +### Notes + +- Elements outside of the copied region are left unchanged. + +
+ + + +
+ +### Examples + +```c +#include "stdlib/blas/ext/base/ndarray/ctril.h" +#include "stdlib/ndarray/ctor.h" +#include "stdlib/ndarray/dtypes.h" +#include "stdlib/ndarray/index_modes.h" +#include "stdlib/ndarray/orders.h" +#include "stdlib/ndarray/base/bytes_per_element.h" +#include "stdlib/complex/float32/ctor.h" +#include +#include +#include + +int main( void ) { + const stdlib_complex64_t dataA[] = { + stdlib_complex64( 1.0f, 1.0f ), stdlib_complex64( 2.0f, 2.0f ), + stdlib_complex64( 3.0f, 3.0f ), stdlib_complex64( 4.0f, 4.0f ) + }; + stdlib_complex64_t dataB[] = { + stdlib_complex64( 0.0f, 0.0f ), stdlib_complex64( 0.0f, 0.0f ), + stdlib_complex64( 0.0f, 0.0f ), stdlib_complex64( 0.0f, 0.0f ) + }; + int32_t dataK[] = { 0 }; + const int64_t ndims = 2; + int64_t shape[] = { 2, 2 }; + int64_t strides[] = { + 2 * STDLIB_NDARRAY_COMPLEX64_BYTES_PER_ELEMENT, + STDLIB_NDARRAY_COMPLEX64_BYTES_PER_ELEMENT + }; + int64_t kstrides[] = { STDLIB_NDARRAY_INT32_BYTES_PER_ELEMENT }; + const int64_t offset = 0; + const enum STDLIB_NDARRAY_ORDER order = STDLIB_NDARRAY_ROW_MAJOR; + const enum STDLIB_NDARRAY_INDEX_MODE imode = STDLIB_NDARRAY_INDEX_ERROR; + int8_t submodes[] = { STDLIB_NDARRAY_INDEX_ERROR }; + const int64_t nsubmodes = 1; + struct ndarray *A = stdlib_ndarray_allocate( STDLIB_NDARRAY_COMPLEX64, (uint8_t *)dataA, ndims, shape, strides, offset, order, imode, nsubmodes, submodes ); + struct ndarray *B = stdlib_ndarray_allocate( STDLIB_NDARRAY_COMPLEX64, (uint8_t *)dataB, ndims, shape, strides, offset, order, imode, nsubmodes, submodes ); + struct ndarray *k = stdlib_ndarray_allocate( STDLIB_NDARRAY_INT32, (uint8_t *)dataK, 0, NULL, kstrides, offset, order, imode, nsubmodes, submodes ); + if ( A == NULL || B == NULL || k == NULL ) { + fprintf( stderr, "Error allocating memory.\n" ); + exit( 1 ); + } + const struct ndarray *arrays[] = { A, B, k }; + stdlib_blas_ext_ctril( arrays ); + for ( int i = 0; i < 4; i++ ) { + stdlib_complex64_parts_t parts; + parts.value = dataB[ i ]; + printf( "dataB[ %i ] = (%f, %f)\n", i, parts.parts[ 0 ], parts.parts[ 1 ] ); + } + stdlib_ndarray_free( A ); + stdlib_ndarray_free( B ); + stdlib_ndarray_free( k ); +} +``` + +
+ + + +
+ + +