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

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

-->

# fillDiagonal

> Fill a specified diagonal of a matrix (or stack of matrices) with a scalar value.

<section class="intro">

For an `M`-by-`N` matrix `A`, the `k`-th diagonal is defined as

<!-- <equation class="equation" label="eq:diagonal_definition" align="center" raw="D_k = \{\, A_{i,j} : j - i = k \,\}" alt="Definition of the k-th diagonal of a matrix."> -->

```math
D_k = \{\, A_{i,j} : j - i = k \,\}
```

<!-- <div class="equation" align="center" data-raw-text="D_k = \{\, A_{i,j} : j - i = k \,\}" data-equation="eq:diagonal_definition">
<img src="" alt="Definition of the k-th diagonal of a matrix.">
<br>
</div> -->

<!-- </equation> -->

where `k = 0` corresponds to the main diagonal, `k > 0` corresponds to the super-diagonals (above the main diagonal), and `k < 0` corresponds to the sub-diagonals (below the main diagonal).

</section>

<!-- /.intro -->

<section class="usage">

## Usage

```javascript
var fillDiagonal = require( '@stdlib/ndarray/base/fill-diagonal' );
```

#### fillDiagonal( x, value, dims, k )

Fills a specified diagonal of a matrix (or stack of matrices) with a scalar value.

```javascript
var zeros = require( '@stdlib/ndarray/zeros' );

var x = zeros( [ 3, 3 ] );
// returns <ndarray>[ [ 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0 ] ]

var out = fillDiagonal( x, 1.0, [ 0, 1 ], 0 );
// returns <ndarray>[ [ 1.0, 0.0, 0.0 ], [ 0.0, 1.0, 0.0 ], [ 0.0, 0.0, 1.0 ] ]

var bool = ( out === x );
// returns true
```

The function accepts the following arguments:

- **x**: input ndarray.
- **value**: scalar value.
- **dims**: dimension indices defining the plane in which to fill the diagonal.
- **k**: diagonal offset.

</section>

<!-- /.usage -->

<section class="notes">

## Notes

- The order of the dimension indices contained in `dims` matters. The first element specifies the row-like dimension. The second element specifies the column-like dimension.
- Each provided dimension index must reside on the interval `[-ndims, ndims-1]`.
- The diagonal offset `k` is interpreted as `column - row`. Accordingly, when `k = 0`, the function fills the main diagonal; when `k > 0`, the function fills the diagonal above the main diagonal; and when `k < 0`, the function fills the diagonal below the main diagonal.
- If `value` is a number and `x` has a complex [data type][@stdlib/ndarray/dtypes], the function fills an input ndarray with a complex number whose real component equals the provided scalar `value` and whose imaginary component is zero.
- A `value` must be able to safely cast to the input ndarray [data type][@stdlib/ndarray/dtypes]. Scalar values having floating-point data types (both real and complex) are allowed to downcast to a lower precision data type of the same kind (e.g., a scalar double-precision floating-point number can be used to fill a `'float32'` input ndarray).
- The function **mutates** the input ndarray in-place.

</section>

<!-- /.notes -->

<section class="examples">

## Examples

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

```javascript
var zeros = require( '@stdlib/ndarray/zeros' );
var ndarray2array = require( '@stdlib/ndarray/to-array' );
var fillDiagonal = require( '@stdlib/ndarray/base/fill-diagonal' );

// Create a stack of matrices:
var x = zeros( [ 2, 3, 3 ] );

// Fill main diagonals:
fillDiagonal( x, 1.0, [ 1, 2 ], 0 );
console.log( ndarray2array( x ) );

// Fill super-diagonals:
fillDiagonal( x, 2.0, [ 1, 2 ], 1 );
console.log( ndarray2array( x ) );

// Fill sub-diagonals:
fillDiagonal( x, 3.0, [ 1, 2 ], -1 );
console.log( ndarray2array( x ) );
```

</section>

<!-- /.examples -->

<section class="references">

</section>

<!-- /.references -->

<!-- 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">

[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes

</section>

<!-- /.links -->
Loading