|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2026 The Stdlib Authors. |
| 6 | +
|
| 7 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +you may not use this file except in compliance with the License. |
| 9 | +You may obtain a copy of the License at |
| 10 | +
|
| 11 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +
|
| 13 | +Unless required by applicable law or agreed to in writing, software |
| 14 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +See the License for the specific language governing permissions and |
| 17 | +limitations under the License. |
| 18 | +
|
| 19 | +--> |
| 20 | + |
| 21 | +# assignDiagonal |
| 22 | + |
| 23 | +> Assign elements from a broadcasted input [`ndarray`][@stdlib/ndarray/ctor] to a specified diagonal of an output [`ndarray`][@stdlib/ndarray/ctor]. |
| 24 | +
|
| 25 | +<section class="intro"> |
| 26 | + |
| 27 | +For an `M`-by-`N` matrix `A`, the `k`-th diagonal is defined as |
| 28 | + |
| 29 | +<!-- <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."> --> |
| 30 | + |
| 31 | +```math |
| 32 | +D_k = \{\, A_{i,j} : j - i = k \,\} |
| 33 | +``` |
| 34 | + |
| 35 | +<!-- <div class="equation" align="center" data-raw-text="D_k = \{\, A_{i,j} : j - i = k \,\}" data-equation="eq:diagonal_definition"> |
| 36 | + <img src="" alt="Definition of the k-th diagonal of a matrix."> |
| 37 | + <br> |
| 38 | +</div> --> |
| 39 | + |
| 40 | +<!-- </equation> --> |
| 41 | + |
| 42 | +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). |
| 43 | + |
| 44 | +</section> |
| 45 | + |
| 46 | +<!-- /.intro --> |
| 47 | + |
| 48 | +<section class="usage"> |
| 49 | + |
| 50 | +## Usage |
| 51 | + |
| 52 | +```javascript |
| 53 | +var assignDiagonal = require( '@stdlib/ndarray/base/assign-diagonal' ); |
| 54 | +``` |
| 55 | + |
| 56 | +#### assignDiagonal( arrays, dims, k ) |
| 57 | + |
| 58 | +Assigns elements from a broadcasted input [`ndarray`][@stdlib/ndarray/ctor] to a specified diagonal of an output [`ndarray`][@stdlib/ndarray/ctor]. |
| 59 | + |
| 60 | +```javascript |
| 61 | +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); |
| 62 | +var zeros = require( '@stdlib/ndarray/zeros' ); |
| 63 | + |
| 64 | +var x = scalar2ndarray( 1.0 ); |
| 65 | +// returns <ndarray> |
| 66 | + |
| 67 | +var y = zeros( [ 3, 3 ] ); |
| 68 | +// returns <ndarray>[ [ 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0 ] ] |
| 69 | + |
| 70 | +var out = assignDiagonal( [ x, y ], [ 0, 1 ], 0 ); |
| 71 | +// returns <ndarray>[ [ 1.0, 0.0, 0.0 ], [ 0.0, 1.0, 0.0 ], [ 0.0, 0.0, 1.0 ] ] |
| 72 | + |
| 73 | +var bool = ( out === y ); |
| 74 | +// returns true |
| 75 | +``` |
| 76 | + |
| 77 | +The function accepts the following arguments: |
| 78 | + |
| 79 | +- **arrays**: array-like object containing one input ndarray and one output ndarray. |
| 80 | +- **dims**: dimension indices defining the plane in which to assign elements to the diagonal. |
| 81 | +- **k**: diagonal offset. |
| 82 | + |
| 83 | +</section> |
| 84 | + |
| 85 | +<!-- /.usage --> |
| 86 | + |
| 87 | +<section class="notes"> |
| 88 | + |
| 89 | +## Notes |
| 90 | + |
| 91 | +- 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. |
| 92 | +- Each provided dimension index must reside on the interval `[-ndims, ndims-1]`. |
| 93 | +- 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. |
| 94 | +- The input ndarray must be [broadcast compatible][@stdlib/ndarray/base/broadcast-shapes] with the output ndarray view defined by the specified diagonal. |
| 95 | + |
| 96 | +</section> |
| 97 | + |
| 98 | +<!-- /.notes --> |
| 99 | + |
| 100 | +<section class="examples"> |
| 101 | + |
| 102 | +## Examples |
| 103 | + |
| 104 | +<!-- eslint no-undef: "error" --> |
| 105 | + |
| 106 | +```javascript |
| 107 | +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); |
| 108 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 109 | +var zeros = require( '@stdlib/ndarray/zeros' ); |
| 110 | +var assignDiagonal = require( '@stdlib/ndarray/base/assign-diagonal' ); |
| 111 | + |
| 112 | +// Create a stack of matrices: |
| 113 | +var y = zeros( [ 2, 3, 3 ] ); |
| 114 | +console.log( ndarray2array( y ) ); |
| 115 | + |
| 116 | +// Assign a scalar to each main diagonal: |
| 117 | +assignDiagonal( [ scalar2ndarray( 1.0 ), y ], [ 1, 2 ], 0 ); |
| 118 | +console.log( ndarray2array( y ) ); |
| 119 | + |
| 120 | +// Assign a scalar to each super-diagonal: |
| 121 | +assignDiagonal( [ scalar2ndarray( 2.0 ), y ], [ 1, 2 ], 1 ); |
| 122 | +console.log( ndarray2array( y ) ); |
| 123 | + |
| 124 | +// Assign a scalar to each sub-diagonal: |
| 125 | +assignDiagonal( [ scalar2ndarray( 3.0 ), y ], [ 1, 2 ], -1 ); |
| 126 | +console.log( ndarray2array( y ) ); |
| 127 | +``` |
| 128 | + |
| 129 | +</section> |
| 130 | + |
| 131 | +<!-- /.examples --> |
| 132 | + |
| 133 | +<section class="references"> |
| 134 | + |
| 135 | +</section> |
| 136 | + |
| 137 | +<!-- /.references --> |
| 138 | + |
| 139 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 140 | + |
| 141 | +<section class="related"> |
| 142 | + |
| 143 | +</section> |
| 144 | + |
| 145 | +<!-- /.related --> |
| 146 | + |
| 147 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 148 | + |
| 149 | +<section class="links"> |
| 150 | + |
| 151 | +[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/ndarray/tree/main/ctor |
| 152 | + |
| 153 | +[@stdlib/ndarray/base/broadcast-shapes]: https://github.com/stdlib-js/ndarray/tree/main/base/broadcast-shapes |
| 154 | + |
| 155 | +</section> |
| 156 | + |
| 157 | +<!-- /.links --> |
0 commit comments