Skip to content
Open
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
170 changes: 170 additions & 0 deletions lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
<!--

@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.

-->

# gfill-diagonal

> Fill the diagonal of a matrix with a specified scalar constant.

<section class="usage">

## Usage

```javascript
var gfillDiagonal = require( '@stdlib/blas/ext/base/gfill-diagonal' );
```

#### gfillDiagonal( order, M, N, k, alpha, A, LDA )

Fills the diagonal of a matrix with a specified scalar constant.

```javascript
var x = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ];

gfillDiagonal( 'row-major', 2, 3, 0, 5.0, x, 3 );
// x => [ 5.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]
```

The function has the following parameters:

- **order**: storage layout.
- **M**: number of rows in `A`.
- **N**: number of columns in `A`.
- **k**: diagonal offset.
- **alpha**: scalar constant.
- **A**: input matrix as a linear array.
- **LDA**: stride of the first dimension of `A` (a.k.a., leading dimension of the matrix `A`).

The `order` and `LDA` parameters determine the matrix layout. For example, to fill an upper diagonal:

```javascript
var A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ];

gfillDiagonal( 'row-major', 2, 3, 1, 7.0, A, 3 );
// A => [ 1.0, 7.0, 3.0, 4.0, 5.0, 7.0 ]
```

Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.

```javascript
var Float64Array = require( '@stdlib/array/float64' );

// Initial matrix...
var A0 = new Float64Array( [ 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );

// Create an offset view...
var A1 = new Float64Array( A0.buffer, A0.BYTES_PER_ELEMENT*1 ); // start at 2nd element

// Fill main diagonal...
gfillDiagonal( 'row-major', 2, 3, 0, 8.0, A1, 3 );
// A0 => <Float64Array>[ 0.0, 8.0, 2.0, 3.0, 4.0, 8.0, 6.0 ]
```

#### gfillDiagonal.ndarray( M, N, k, alpha, A, strideA1, strideA2, offsetA )

Fills the diagonal of a matrix with a specified scalar constant using alternative indexing semantics.

```javascript
var A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ];

gfillDiagonal.ndarray( 2, 3, 0, 5.0, A, 3, 1, 0 );
// A => [ 5.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]
```

The function has the following parameters:

- **M**: number of rows in `A`.
- **N**: number of columns in `A`.
- **k**: diagonal offset.
- **alpha**: scalar constant.
- **A**: input matrix as a linear array.
- **strideA1**: stride of the first dimension of `A`.
- **strideA2**: stride of the second dimension of `A`.
- **offsetA**: starting index for `A`.

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, with an offset:

```javascript
var A = [ 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ];

gfillDiagonal.ndarray( 2, 3, 0, 8.0, A, 3, 1, 1 );
// A => [ 0.0, 8.0, 2.0, 3.0, 4.0, 8.0, 6.0 ]
```

</section>

<!-- /.usage -->

<section class="notes">

## Notes

- If `M <= 0` or `N <= 0`, both functions return `A` unchanged.
- Both functions support array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor]).

</section>

<!-- /.notes -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```javascript
var uniform = require( '@stdlib/random/array/uniform' );
var gfillDiagonal = require( '@stdlib/blas/ext/base/gfill-diagonal' );

var A = uniform( 6, -100, 100, {
'dtype': 'float64'
});
console.log( A );

gfillDiagonal( 'row-major', 2, 3, 0, 5.0, A, 3 );
console.log( A );
```

</section>

<!-- /.examples -->

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

<section class="related">

</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray

[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor

<!-- <related-links> -->

<!-- </related-links> -->

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/**
* @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 bench = require( '@stdlib/bench' );
var uniform = require( '@stdlib/random/array/uniform' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var pow = require( '@stdlib/math/base/special/pow' );
var floor = require( '@stdlib/math/base/special/floor' );
var sqrt = require( '@stdlib/math/base/special/sqrt' );
var format = require( '@stdlib/string/format' );
var pkg = require( './../package.json' ).name;
var gfillDiagonal = require( './../lib/main.js' );


// VARIABLES //

var options = {
'dtype': 'generic'
};

var LAYOUTS = [
'row-major',
'column-major'
];


// FUNCTIONS //

/**
* Create a benchmark function.
*
* @private
* @param {string} order - storage layout order
* @param {PositiveInteger} size - matrix size (N where matrix is NxN)
* @returns {Function} benchmark function
*/
function createBenchmark( order, size ) {
var A = uniform( size * size, -100, 100, options );
return benchmark;

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

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
y = gfillDiagonal( order, size, size, 0, i, A, size );
if ( isnan( y[ 0 ] ) ) {
b.fail( 'should not return NaN' );
}
}
b.toc();
if ( isnan( y[ 0 ] ) ) {
b.fail( 'should not return NaN' );
}
b.pass( 'benchmark finished' );
b.end();
}
}


// MAIN //

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

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

for ( k = 0; k < LAYOUTS.length; k++ ) {
ord = LAYOUTS[ k ];
for ( i = min; i <= max; i++ ) {
size = floor( sqrt( pow( 10, i ) ) );
f = createBenchmark( ord, size );
bench( format( '%s::square_matrix:order=%s,size=%d', pkg, ord, size*size ), f );
}
}
}

main();
Loading