From 30465e42497d98dfeaf9c5450595e9a973fb63dc Mon Sep 17 00:00:00 2001 From: AtharvaPatange <164712914+AtharvaPatange@users.noreply.github.com> Date: Sun, 18 Oct 2026 20:36:34 +0530 Subject: [PATCH 1/2] feat: add C implementation for blas/ext/base/ndarray/ztriu --- .../blas/ext/base/ndarray/ztriu/README.md | 198 +++++++++++++- .../base/ndarray/ztriu/benchmark/benchmark.js | 4 +- .../ztriu/benchmark/benchmark.native.js | 124 +++++++++ .../base/ndarray/ztriu/benchmark/c/Makefile | 146 +++++++++++ .../ztriu/benchmark/c/benchmark.length.c | 222 ++++++++++++++++ .../blas/ext/base/ndarray/ztriu/binding.gyp | 170 ++++++++++++ .../base/ndarray/ztriu/examples/c/Makefile | 146 +++++++++++ .../base/ndarray/ztriu/examples/c/example.c | 98 +++++++ .../blas/ext/base/ndarray/ztriu/include.gypi | 53 ++++ .../stdlib/blas/ext/base/ndarray/ztriu.h | 40 +++ .../blas/ext/base/ndarray/ztriu/lib/index.js | 16 +- .../blas/ext/base/ndarray/ztriu/lib/native.js | 74 ++++++ .../blas/ext/base/ndarray/ztriu/manifest.json | 116 +++++++++ .../blas/ext/base/ndarray/ztriu/package.json | 4 + .../blas/ext/base/ndarray/ztriu/src/Makefile | 70 +++++ .../blas/ext/base/ndarray/ztriu/src/addon.c | 67 +++++ .../blas/ext/base/ndarray/ztriu/src/main.c | 58 +++++ .../blas/ext/base/ndarray/ztriu/test/test.js | 213 ++------------- .../ext/base/ndarray/ztriu/test/test.main.js | 237 +++++++++++++++++ .../base/ndarray/ztriu/test/test.native.js | 246 ++++++++++++++++++ 20 files changed, 2110 insertions(+), 192 deletions(-) create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/ztriu/benchmark/benchmark.native.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/ztriu/benchmark/c/Makefile create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/ztriu/benchmark/c/benchmark.length.c create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/ztriu/binding.gyp create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/ztriu/examples/c/Makefile create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/ztriu/examples/c/example.c create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/ztriu/include.gypi create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/ztriu/include/stdlib/blas/ext/base/ndarray/ztriu.h create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/ztriu/lib/native.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/ztriu/manifest.json create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/ztriu/src/Makefile create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/ztriu/src/addon.c create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/ztriu/src/main.c create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/ztriu/test/test.main.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/ztriu/test/test.native.js diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/ztriu/README.md b/lib/node_modules/@stdlib/blas/ext/base/ndarray/ztriu/README.md index c09f5298e564..7d597d07935e 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/ztriu/README.md +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/ztriu/README.md @@ -50,7 +50,7 @@ var A = new Complex128Matrix( [ [ 1.0, 2.0, 3.0, 4.0 ], [ 5.0, 6.0, 7.0, 8.0 ] ] var B = new Complex128Matrix( [ [ 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 = ztriu( [ A, B, k ] ); @@ -108,7 +108,7 @@ var B = new Complex128Matrix( discreteUniform( 5*5*2, -50, 50, opts ).buffer, 0, console.log( ndarray2array( B ) ); var k = scalar2ndarray( 0, { - 'dtype': 'generic' + 'dtype': 'int32' }); var out = ztriu( [ A, B, k ] ); @@ -119,6 +119,200 @@ console.log( ndarray2array( out ) ); + + +* * * + +
+ +## C APIs + + + +
+ +
+ + + + + +
+ +### Usage + +```c +#include "stdlib/blas/ext/base/ndarray/ztriu.h" +``` + +#### stdlib_blas_ext_ztriu( arrays ) + +Copies the upper triangular part of a double-precision complex floating-point matrix `A` to another matrix `B`. + +```c +#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/float64/ctor.h" +#include + +// Create ndarrays: +const stdlib_complex128_t dataA[] = { + stdlib_complex128( 1.0, 1.0 ), + stdlib_complex128( 2.0, 2.0 ), + stdlib_complex128( 3.0, 3.0 ), + stdlib_complex128( 4.0, 4.0 ) +}; +stdlib_complex128_t dataB[] = { + stdlib_complex128( 0.0, 0.0 ), + stdlib_complex128( 0.0, 0.0 ), + stdlib_complex128( 0.0, 0.0 ), + stdlib_complex128( 0.0, 0.0 ) +}; +int32_t dataK[] = { 0 }; +int64_t shape[] = { 2, 2 }; +int64_t strides[] = { 2 * STDLIB_NDARRAY_COMPLEX128_BYTES_PER_ELEMENT, STDLIB_NDARRAY_COMPLEX128_BYTES_PER_ELEMENT }; +int64_t kstrides[] = { 0 }; +int8_t submodes[] = { STDLIB_NDARRAY_INDEX_ERROR }; + +struct ndarray *A = stdlib_ndarray_allocate( STDLIB_NDARRAY_COMPLEX128, (uint8_t *)dataA, 2, shape, strides, 0, STDLIB_NDARRAY_ROW_MAJOR, STDLIB_NDARRAY_INDEX_ERROR, 1, submodes ); +struct ndarray *B = stdlib_ndarray_allocate( STDLIB_NDARRAY_COMPLEX128, (uint8_t *)dataB, 2, shape, strides, 0, STDLIB_NDARRAY_ROW_MAJOR, STDLIB_NDARRAY_INDEX_ERROR, 1, submodes ); +struct ndarray *k = stdlib_ndarray_allocate( STDLIB_NDARRAY_INT32, (uint8_t *)dataK, 0, NULL, kstrides, 0, STDLIB_NDARRAY_ROW_MAJOR, STDLIB_NDARRAY_INDEX_ERROR, 1, submodes ); + +// Perform computation: +const struct ndarray *arrays[] = { A, B, k }; +stdlib_blas_ext_ztriu( arrays ); + +// Free allocated memory: +stdlib_ndarray_free( A ); +stdlib_ndarray_free( B ); +stdlib_ndarray_free( k ); +``` + +The function accepts the following arguments: + +- **arrays**: `[in] struct ndarray**` list containing the following ndarrays: + + - `[in] struct ndarray*` a two-dimensional input ndarray corresponding to `A`. + - `[inout] struct ndarray*` a two-dimensional output ndarray corresponding to `B`. + - `[in] struct ndarray*` a zero-dimensional ndarray specifying the diagonal below which to ignore. + +```c +void stdlib_blas_ext_ztriu( const struct ndarray *arrays[] ); +``` + +
+ + + + + +
+ +### Notes + +- Elements outside of the copied region are left unchanged. + +
+ + + + + +
+ +### Examples + +```c +#include "stdlib/blas/ext/base/ndarray/ztriu.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/float64/ctor.h" +#include "stdlib/complex/float64/real.h" +#include "stdlib/complex/float64/imag.h" +#include +#include +#include + +int main( void ) { + // Create data buffers: + const stdlib_complex128_t dataA[] = { + stdlib_complex128( 1.0, 1.0 ), + stdlib_complex128( 2.0, 2.0 ), + stdlib_complex128( 3.0, 3.0 ), + stdlib_complex128( 4.0, 4.0 ) + }; + stdlib_complex128_t dataB[] = { + stdlib_complex128( 0.0, 0.0 ), + stdlib_complex128( 0.0, 0.0 ), + stdlib_complex128( 0.0, 0.0 ), + stdlib_complex128( 0.0, 0.0 ) + }; + int32_t dataK[] = { 0 }; + + // Specify the number of matrix dimensions: + const int64_t ndims = 2; + + // Specify the matrix shape: + int64_t shape[] = { 2, 2 }; + + // Specify the array strides (in bytes): + int64_t strides[] = { 2 * STDLIB_NDARRAY_COMPLEX128_BYTES_PER_ELEMENT, STDLIB_NDARRAY_COMPLEX128_BYTES_PER_ELEMENT }; + int64_t kstrides[] = { 0 }; + + // Specify the byte offset: + const int64_t offset = 0; + + // Specify the array order: + const enum STDLIB_NDARRAY_ORDER order = STDLIB_NDARRAY_ROW_MAJOR; + + // Specify the index mode: + const enum STDLIB_NDARRAY_INDEX_MODE imode = STDLIB_NDARRAY_INDEX_ERROR; + + // Specify the subscript index modes: + int8_t submodes[] = { STDLIB_NDARRAY_INDEX_ERROR }; + const int64_t nsubmodes = 1; + + // Create ndarrays: + struct ndarray *A = stdlib_ndarray_allocate( STDLIB_NDARRAY_COMPLEX128, (uint8_t *)dataA, ndims, shape, strides, offset, order, imode, nsubmodes, submodes ); + struct ndarray *B = stdlib_ndarray_allocate( STDLIB_NDARRAY_COMPLEX128, (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 ); + } + + // Define a list of ndarrays: + const struct ndarray *arrays[] = { A, B, k }; + + // Copy the upper triangular part of A into B: + stdlib_blas_ext_ztriu( arrays ); + + // Print the output buffer: + for ( int i = 0; i < 4; i++ ) { + printf( "dataB[ %i ] = (%lf, %lf)\n", i, stdlib_complex128_real( dataB[ i ] ), stdlib_complex128_imag( dataB[ i ] ) ); + } + + // Free allocated memory: + stdlib_ndarray_free( A ); + stdlib_ndarray_free( B ); + stdlib_ndarray_free( k ); +} +``` + +
+ + + +
+ + +