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

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

-->

# assignDiagonal

> Assign elements from a broadcasted input [`ndarray`][@stdlib/ndarray/ctor] to a specified diagonal of an output [`ndarray`][@stdlib/ndarray/ctor].

<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 assignDiagonal = require( '@stdlib/ndarray/base/assign-diagonal' );
```

#### assignDiagonal( arrays, dims, k )

Assigns elements from a broadcasted input [`ndarray`][@stdlib/ndarray/ctor] to a specified diagonal of an output [`ndarray`][@stdlib/ndarray/ctor].

```javascript
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
var zeros = require( '@stdlib/ndarray/zeros' );

var x = scalar2ndarray( 1.0 );
// returns <ndarray>

var y = 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 = assignDiagonal( [ x, y ], [ 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 === y );
// returns true
```

The function accepts the following arguments:

- **arrays**: array-like object containing one input ndarray and one output ndarray.
- **dims**: dimension indices defining the plane in which to assign elements to 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 assigns to the main diagonal; when `k > 0`, the function assigns to a diagonal above the main diagonal; and when `k < 0`, the function assigns to a diagonal below the main diagonal.
- The input ndarray must be [broadcast compatible][@stdlib/ndarray/base/broadcast-shapes] with the output ndarray view defined by the specified diagonal.
- The function **mutates** the output ndarray in-place.

</section>

<!-- /.notes -->

<section class="examples">

## Examples

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

```javascript
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
var ndarray2array = require( '@stdlib/ndarray/to-array' );
var zeros = require( '@stdlib/ndarray/zeros' );
var assignDiagonal = require( '@stdlib/ndarray/base/assign-diagonal' );

// Create a stack of matrices:
var y = zeros( [ 2, 3, 3 ] );
console.log( ndarray2array( y ) );

// Assign a scalar to each main diagonal:
assignDiagonal( [ scalar2ndarray( 1.0 ), y ], [ 1, 2 ], 0 );
console.log( ndarray2array( y ) );

// Assign a scalar to each super-diagonal:
assignDiagonal( [ scalar2ndarray( 2.0 ), y ], [ 1, 2 ], 1 );
console.log( ndarray2array( y ) );

// Assign a scalar to each sub-diagonal:
assignDiagonal( [ scalar2ndarray( 3.0 ), y ], [ 1, 2 ], -1 );
console.log( ndarray2array( y ) );
```

</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/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor

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

</section>

<!-- /.links -->
Loading