|
| 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 | +# fillDiagonal |
| 22 | + |
| 23 | +> Fill a specified diagonal of a matrix (or stack of matrices) with a scalar value. |
| 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 fillDiagonal = require( '@stdlib/ndarray/base/fill-diagonal' ); |
| 54 | +``` |
| 55 | + |
| 56 | +#### fillDiagonal( x, value, dims, k ) |
| 57 | + |
| 58 | +Fills a specified diagonal of a matrix (or stack of matrices) with a scalar value. |
| 59 | + |
| 60 | +```javascript |
| 61 | +var zeros = require( '@stdlib/ndarray/zeros' ); |
| 62 | + |
| 63 | +var x = zeros( [ 3, 3 ] ); |
| 64 | +// returns <ndarray>[ [ 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0 ] ] |
| 65 | + |
| 66 | +var out = fillDiagonal( x, 1.0, [ 0, 1 ], 0 ); |
| 67 | +// returns <ndarray>[ [ 1.0, 0.0, 0.0 ], [ 0.0, 1.0, 0.0 ], [ 0.0, 0.0, 1.0 ] ] |
| 68 | + |
| 69 | +var bool = ( out === x ); |
| 70 | +// returns true |
| 71 | +``` |
| 72 | + |
| 73 | +The function accepts the following arguments: |
| 74 | + |
| 75 | +- **x**: input ndarray. |
| 76 | +- **value**: scalar value. |
| 77 | +- **dims**: dimension indices defining the plane in which to fill the diagonal. |
| 78 | +- **k**: diagonal offset. |
| 79 | + |
| 80 | +</section> |
| 81 | + |
| 82 | +<!-- /.usage --> |
| 83 | + |
| 84 | +<section class="notes"> |
| 85 | + |
| 86 | +## Notes |
| 87 | + |
| 88 | +- 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. |
| 89 | +- Each provided dimension index must reside on the interval `[-ndims, ndims-1]`. |
| 90 | +- 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. |
| 91 | +- 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. |
| 92 | +- 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). |
| 93 | +- The function **mutates** the input ndarray in-place. |
| 94 | + |
| 95 | +</section> |
| 96 | + |
| 97 | +<!-- /.notes --> |
| 98 | + |
| 99 | +<section class="examples"> |
| 100 | + |
| 101 | +## Examples |
| 102 | + |
| 103 | +<!-- eslint no-undef: "error" --> |
| 104 | + |
| 105 | +```javascript |
| 106 | +var zeros = require( '@stdlib/ndarray/zeros' ); |
| 107 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 108 | +var fillDiagonal = require( '@stdlib/ndarray/base/fill-diagonal' ); |
| 109 | + |
| 110 | +// Create a stack of matrices: |
| 111 | +var x = zeros( [ 2, 3, 3 ] ); |
| 112 | + |
| 113 | +// Fill main diagonals: |
| 114 | +fillDiagonal( x, 1.0, [ 1, 2 ], 0 ); |
| 115 | +console.log( ndarray2array( x ) ); |
| 116 | + |
| 117 | +// Fill super-diagonals: |
| 118 | +fillDiagonal( x, 2.0, [ 1, 2 ], 1 ); |
| 119 | +console.log( ndarray2array( x ) ); |
| 120 | + |
| 121 | +// Fill sub-diagonals: |
| 122 | +fillDiagonal( x, 3.0, [ 1, 2 ], -1 ); |
| 123 | +console.log( ndarray2array( x ) ); |
| 124 | +``` |
| 125 | + |
| 126 | +</section> |
| 127 | + |
| 128 | +<!-- /.examples --> |
| 129 | + |
| 130 | +<section class="references"> |
| 131 | + |
| 132 | +</section> |
| 133 | + |
| 134 | +<!-- /.references --> |
| 135 | + |
| 136 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 137 | + |
| 138 | +<section class="related"> |
| 139 | + |
| 140 | +</section> |
| 141 | + |
| 142 | +<!-- /.related --> |
| 143 | + |
| 144 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 145 | + |
| 146 | +<section class="links"> |
| 147 | + |
| 148 | +[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/ndarray/tree/main/dtypes |
| 149 | + |
| 150 | +</section> |
| 151 | + |
| 152 | +<!-- /.links --> |
0 commit comments