Repository navigation
feat: add C implementation for blas/ext/base/ndarray/ctril
#16140
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
base: develop
Are you sure you want to change the base?
Changes from all commits
5ddc730
16b375e
8c05949
be9e79b
9cc884e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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 ] ); | ||||||||||
|
|
@@ -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,238 @@ console.log( ndarray2array( out ) ); | |||||||||
|
|
||||||||||
| <!-- /.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"> | ||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
|
||||||||||
| ### 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 "stdlib/complex/float32/real.h" | ||||||||||
| #include "stdlib/complex/float32/imag.h" | ||||||||||
| #include <stdint.h> | ||||||||||
| #include <stdlib.h> | ||||||||||
| #include <stdio.h> | ||||||||||
|
|
||||||||||
| // Create data buffers: | ||||||||||
| 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 }; | ||||||||||
|
|
||||||||||
| // 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_COMPLEX64_BYTES_PER_ELEMENT, | ||||||||||
| STDLIB_NDARRAY_COMPLEX64_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_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. | ||||||||||
| " ); | ||||||||||
| exit( 1 ); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // Define a list of ndarrays: | ||||||||||
| const struct ndarray *arrays[] = { A, B, k }; | ||||||||||
|
|
||||||||||
| // Perform computation: | ||||||||||
| stdlib_blas_ext_ctril( arrays ); | ||||||||||
|
|
||||||||||
| // Print the result: | ||||||||||
| for ( int i = 0; i < 4; i++ ) { | ||||||||||
| printf( "dataB[ %i ] = (%f, %f) | ||||||||||
| ", i, stdlib_complex64_real( dataB[ i ] ), stdlib_complex64_imag( dataB[ i ] ) ); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // 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. | ||||||||||
| - `[inout] struct ndarray*` a two-dimensional output ndarray. | ||||||||||
| - `[in] struct ndarray*` 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. | ||||||||||
|
|
||||||||||
| ```c | ||||||||||
| void stdlib_blas_ext_ctril( const struct ndarray *arrays[] ); | ||||||||||
| ``` | ||||||||||
|
|
||||||||||
| </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"> | ||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
|
||||||||||
| ### Notes | ||||||||||
|
|
||||||||||
| - Elements outside of the copied region are left unchanged. | ||||||||||
|
|
||||||||||
| </section> | ||||||||||
|
|
||||||||||
| <!-- /.notes --> | ||||||||||
|
|
||||||||||
| <!-- C API usage examples. --> | ||||||||||
|
|
||||||||||
| <section class="examples"> | ||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
|
||||||||||
| ### 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 "stdlib/complex/float32/real.h" | ||||||||||
| #include "stdlib/complex/float32/imag.h" | ||||||||||
| #include <stdint.h> | ||||||||||
| #include <stdlib.h> | ||||||||||
| #include <stdio.h> | ||||||||||
|
|
||||||||||
| int main( void ) { | ||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should match |
||||||||||
| // Create data buffers: | ||||||||||
| 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 }; | ||||||||||
|
|
||||||||||
| // 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_COMPLEX64_BYTES_PER_ELEMENT, | ||||||||||
| STDLIB_NDARRAY_COMPLEX64_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_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 ); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // Define a list of ndarrays: | ||||||||||
| const struct ndarray *arrays[] = { A, B, k }; | ||||||||||
|
|
||||||||||
| // Perform computation: | ||||||||||
| stdlib_blas_ext_ctril( arrays ); | ||||||||||
|
|
||||||||||
| // Print the result: | ||||||||||
| for ( int i = 0; i < 4; i++ ) { | ||||||||||
| printf( "dataB[ %i ] = (%f, %f)\n", i, stdlib_complex64_real( dataB[ i ] ), stdlib_complex64_imag( dataB[ i ] ) ); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // Free allocated memory: | ||||||||||
| stdlib_ndarray_free( A ); | ||||||||||
| stdlib_ndarray_free( B ); | ||||||||||
| stdlib_ndarray_free( k ); | ||||||||||
| } | ||||||||||
| ``` | ||||||||||
|
|
||||||||||
| </section> | ||||||||||
|
|
||||||||||
| <!-- /.examples --> | ||||||||||
|
|
||||||||||
| </section> | ||||||||||
|
|
||||||||||
| <!-- /.c --> | ||||||||||
|
|
||||||||||
| <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> | ||||||||||
|
|
||||||||||
| <section class="related"> | ||||||||||
|
|
||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.