Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
182 changes: 182 additions & 0 deletions lib/node_modules/@stdlib/blas/ext/base/ndarray/dtril/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,188 @@ 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">

### Usage

```c
#include "stdlib/blas/ext/base/ndarray/dtril.h"
```

#### stdlib_blas_ext_dtril( arrays )

Copies the lower triangular part of a double-precision 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 <stdint.h>

// Create data buffers:
double dataA[] = { 1.0, 2.0, 3.0, 4.0 };
double dataB[] = { 0.0, 0.0, 0.0, 0.0 };

// Specify the array shape:
int64_t shape[] = { 2, 2 };

// Specify the array strides:
int64_t strides[] = { 2 * STDLIB_NDARRAY_FLOAT64_BYTES_PER_ELEMENT, STDLIB_NDARRAY_FLOAT64_BYTES_PER_ELEMENT };

// Specify the subscript index modes:
int8_t submodes[] = { STDLIB_NDARRAY_INDEX_ERROR, STDLIB_NDARRAY_INDEX_ERROR };

// Create ndarrays for A and B:
struct ndarray *A = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT64, (uint8_t *)dataA, 2, shape, strides, 0, STDLIB_NDARRAY_ROW_MAJOR, STDLIB_NDARRAY_INDEX_ERROR, 2, submodes );
struct ndarray *B = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT64, (uint8_t *)dataB, 2, shape, strides, 0, STDLIB_NDARRAY_ROW_MAJOR, STDLIB_NDARRAY_INDEX_ERROR, 2, submodes );

// Create a scalar ndarray for the diagonal offset `k`:
int64_t k = 0;
struct ndarray *k_arr = stdlib_ndarray_allocate( STDLIB_NDARRAY_INT64, (uint8_t *)&k, 0, NULL, NULL, 0, STDLIB_NDARRAY_ROW_MAJOR, STDLIB_NDARRAY_INDEX_ERROR, 0, NULL );

// Extract lower triangular part:
const struct ndarray *arrays[] = { A, B, k_arr };
stdlib_blas_ext_dtril( arrays );
// dataB => { 1.0, 0.0, 3.0, 4.0 }

// Free allocated memory:
stdlib_ndarray_free( A );
stdlib_ndarray_free( B );
stdlib_ndarray_free( k_arr );
```

The function accepts the following arguments:

- **arrays**: `[in] struct ndarray**` list containing a two-dimensional input ndarray `A`, a two-dimensional output ndarray `B`, and a zero-dimensional scalar ndarray `k`.

```c
void stdlib_blas_ext_dtril( 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">

</section>

<!-- /.notes -->

<!-- C API usage examples. -->

<section class="examples">

### Examples

```c
#include "stdlib/blas/ext/base/ndarray/dtril.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 <stdint.h>
#include <stdlib.h>
#include <stdio.h>

int main( void ) {
// Create data buffers:
double dataA[] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 };
double dataB[] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };

// Specify the number of array dimensions:
const int64_t ndims = 2;

// Specify the array shape:
int64_t shape[] = { 3, 3 };

// Specify the array strides:
int64_t strides[] = { 3*STDLIB_NDARRAY_FLOAT64_BYTES_PER_ELEMENT, STDLIB_NDARRAY_FLOAT64_BYTES_PER_ELEMENT };

// 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, STDLIB_NDARRAY_INDEX_ERROR };
const int64_t nsubmodes = 2;

// Create ndarrays for A and B:
struct ndarray *A = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT64, (uint8_t *)dataA, ndims, shape, strides, offset, order, imode, nsubmodes, submodes );
struct ndarray *B = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT64, (uint8_t *)dataB, ndims, shape, strides, offset, order, imode, nsubmodes, submodes );

if ( A == NULL || B == NULL ) {
fprintf( stderr, "Error allocating memory.\n" );
exit( 1 );
}

// Create a scalar ndarray for the diagonal offset `k`:
int64_t k = 0;
struct ndarray *k_arr = stdlib_ndarray_allocate( STDLIB_NDARRAY_INT64, (uint8_t *)&k, 0, NULL, NULL, 0, order, imode, 0, NULL );

if ( k_arr == NULL ) {
fprintf( stderr, "Error allocating memory.\n" );
exit( 1 );
}

// Define a list of ndarrays:
const struct ndarray *arrays[] = { A, B, k_arr };

// Extract lower triangular part of A to B:
stdlib_blas_ext_dtril( arrays );

// Print the result:
int64_t M = shape[ 0 ];
int64_t N = shape[ 1 ];
for ( int i = 0; i < M; i++ ) {
for ( int j = 0; j < N; j++ ) {
printf( "B[ %i,%i ] = %lf\n", i, j, dataB[ (i*N)+j ] );
}
}

// Free allocated memory:
stdlib_ndarray_free( A );
stdlib_ndarray_free( B );
stdlib_ndarray_free( k_arr );
}
```

</section>

<!-- /.examples -->

</section>

<!-- /.c -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var pow = require( '@stdlib/math/base/special/pow' );
var format = require( '@stdlib/string/format' );
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
var pkg = require( './../package.json' ).name;
var dtril = require( './../lib' );
var dtril = require( './../lib/main.js' );


// VARIABLES //
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2026 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var resolve = require( 'path' ).resolve;
var bench = require( '@stdlib/bench' );
var uniform = require( '@stdlib/random/uniform' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var pow = require( '@stdlib/math/base/special/pow' );
var format = require( '@stdlib/string/format' );
var tryRequire = require( '@stdlib/utils/try-require' );
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
var pkg = require( './../package.json' ).name;


// VARIABLES //

var dtril = tryRequire( resolve( __dirname, './../lib/native.js' ) );
var opts = {
'skip': ( dtril instanceof Error )
};
var options = {
'dtype': 'float64'
};


// FUNCTIONS //

/**
* Creates a benchmark function.
*
* @private
* @param {PositiveInteger} N - number of elements along each dimension
* @returns {Function} benchmark function
*/
function createBenchmark( N ) {
var A = uniform( [ N, N ], -100.0, 100.0, options );
var B = uniform( [ N, N ], -100.0, 100.0, options );

var k = scalar2ndarray( 0, {
'dtype': 'generic'
});

return benchmark;

/**
* Benchmark function.
*
* @private
* @param {Benchmark} b - benchmark instance
*/
function benchmark( b ) {
var out;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = dtril( [ A, B, k ] );
if ( typeof out !== 'object' ) {
b.fail( 'should return an ndarray' );
}
}
b.toc();
if ( isnan( out.get( i%N, i%N ) ) ) {
b.fail( 'should not return NaN' );
}
b.pass( 'benchmark finished' );
b.end();
}
}


// MAIN //

/**
* Main execution sequence.
*
* @private
*/
function main() {
var min;
var max;
var N;
var f;
var i;

min = 1; // 10^min
max = 3; // 10^max

for ( i = min; i <= max; i++ ) {
N = pow( 10, i );
f = createBenchmark( N );
bench( format( '%s::native:size=%d', pkg, N*N ), opts, f );
}
}

main();
Loading