Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 11x 11x 33x 1x 1x 33x 33x 33x 33x 33x 33x 33x 73x 66x 66x 7x 7x 7x 33x 33x 33x 33x 33x 33x 33x 1x 1x 1x 1x 1x | /**
* @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 toNormalizedIndices = require( '@stdlib/ndarray/base/to-normalized-indices' );
var getDType = require( '@stdlib/ndarray/dtype' );
var getShape = require( '@stdlib/ndarray/shape' );
var getStrides = require( '@stdlib/ndarray/strides' );
var getOffset = require( '@stdlib/ndarray/offset' );
var getOrder = require( '@stdlib/ndarray/order' );
var getData = require( '@stdlib/ndarray/data-buffer' );
// MAIN //
/**
* Returns a view of the diagonal of a matrix (or stack of matrices).
*
* ## 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 returns the main diagonal; when `k > 0`, the function returns the diagonal above the main diagonal; and when `k < 0`, the function returns the diagonal below the main diagonal.
* - The returned ndarray is a **view** of the input ndarray. Accordingly, writing to the original ndarray will **mutate** the returned ndarray and vice versa.
* - The `writable` parameter **only** applies to ndarray constructors supporting **read-only** instances.
*
* @param {ndarray} x - input array
* @param {IntegerArray} dims - dimension indices defining the plane in which to extract the diagonal
* @param {integer} k - diagonal offset
* @param {boolean} writable - boolean indicating whether the returned ndarray should be writable
* @returns {ndarray} ndarray view
*
* @example
* var array = require( '@stdlib/ndarray/array' );
*
* var x = array( [ [ 1.0, 2.0, 3.0 ], [ 4.0, 5.0, 6.0 ], [ 7.0, 8.0, 9.0 ] ] );
* // returns <ndarray>[ [ 1.0, 2.0, 3.0 ], [ 4.0, 5.0, 6.0 ], [ 7.0, 8.0, 9.0 ] ]
*
* var y = diagonal( x, [ 0, 1 ], 0, false );
* // returns <ndarray>[ 1.0, 5.0, 9.0 ]
*/
function diagonal( x, dims, k, writable ) {
var strides;
var offset;
var shape;
var sh;
var st;
var sr;
var sc;
var kp;
var kn;
var d;
var N;
var L;
var i;
sh = getShape( x );
st = getStrides( x );
N = sh.length;
// Normalize the dimension indices:
d = toNormalizedIndices( dims, N-1 );
sr = st[ d[0] ];
sc = st[ d[1] ];
// Resolve the positive and negative parts of `k`:
kp = ( k > 0 ) ? k : 0;
kn = kp - k;
// Compute the length of the diagonal (clamped to be non-negative):
L = sh[ d[0] ] - kn;
if ( sh[ d[1] ] - kp < L ) {
L = sh[ d[1] ] - kp;
}
if ( L < 0 ) {
L = 0;
}
// Adjust the offset so that we point to the first element along the specified diagonal:
offset = getOffset( x ) + ( kp*sc ) + ( kn*sr );
// Drop the specified dimensions and append the diagonal dimension:
shape = [];
strides = [];
for ( i = 0; i < N; i++ ) {
if ( i === d[0] || i === d[1] ) {
continue;
}
shape.push( sh[ i ] );
strides.push( st[ i ] );
}
shape.push( L );
strides.push( sr + sc );
return new x.constructor( getDType( x ), getData( x ), shape, strides, offset, getOrder( x ), { // eslint-disable-line max-len
'readonly': !writable
});
}
// EXPORTS //
module.exports = diagonal;
|