From 12d678c4bd4a77995ebd85883c6a8d045589b130 Mon Sep 17 00:00:00 2001 From: headlessNode Date: Thu, 30 Apr 2026 04:01:28 +0500 Subject: [PATCH 01/12] feat: add ndarray/base/diagonal --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed --- --- .../@stdlib/ndarray/base/diagonal/README.md | 165 ++++++ .../base/diagonal/benchmark/benchmark.js | 272 +++++++++ .../ndarray/base/diagonal/docs/repl.txt | 51 ++ .../base/diagonal/docs/types/index.d.ts | 57 ++ .../ndarray/base/diagonal/docs/types/test.ts | 92 ++++ .../ndarray/base/diagonal/examples/index.js | 42 ++ .../ndarray/base/diagonal/lib/index.js | 44 ++ .../@stdlib/ndarray/base/diagonal/lib/main.js | 121 ++++ .../ndarray/base/diagonal/package.json | 64 +++ .../ndarray/base/diagonal/test/test.js | 521 ++++++++++++++++++ 10 files changed, 1429 insertions(+) create mode 100644 lib/node_modules/@stdlib/ndarray/base/diagonal/README.md create mode 100644 lib/node_modules/@stdlib/ndarray/base/diagonal/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/diagonal/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/ndarray/base/diagonal/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/ndarray/base/diagonal/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/ndarray/base/diagonal/examples/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/diagonal/lib/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/diagonal/lib/main.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/diagonal/package.json create mode 100644 lib/node_modules/@stdlib/ndarray/base/diagonal/test/test.js diff --git a/lib/node_modules/@stdlib/ndarray/base/diagonal/README.md b/lib/node_modules/@stdlib/ndarray/base/diagonal/README.md new file mode 100644 index 000000000000..db1c3ca5d67b --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/diagonal/README.md @@ -0,0 +1,165 @@ + + +# diagonal + +> Return a view of the diagonal of a matrix (or stack of matrices). + +
+ +For an `M`-by-`N` matrix `A`, the `k`-th diagonal is defined as + + + +```math +D_k = \{\, A_{i,j} : j - i = k \,\} +``` + + + + + +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). For example, given the matrix + + + +```math +A = \begin{bmatrix} a_{0,0} & a_{0,1} & a_{0,2} \\ a_{1,0} & a_{1,1} & a_{1,2} \\ a_{2,0} & a_{2,1} & a_{2,2} \end{bmatrix} +``` + + + + + +the main diagonal is `[ a_{0,0}, a_{1,1}, a_{2,2} ]`, the super-diagonal `k = 1` is `[ a_{0,1}, a_{1,2} ]`, and the sub-diagonal `k = -1` is `[ a_{1,0}, a_{2,1} ]`. + +
+ + + +
+ +## Usage + +```javascript +var diagonal = require( '@stdlib/ndarray/base/diagonal' ); +``` + +#### diagonal( x, dims, k, writable ) + +Returns a view of the diagonal of a matrix (or stack of matrices) `x`. + +```javascript +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 [ [ 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 [ 1.0, 5.0, 9.0 ] +``` + +The function accepts the following arguments: + +- **x**: input ndarray. +- **dims**: dimension indices defining the plane in which to extract the diagonal. +- **k**: diagonal offset. +- **writable**: boolean indicating whether the returned ndarray should be writable. + +
+ + + +
+ +## 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. + +
+ + + +
+ +## Examples + + + +```javascript +var uniform = require( '@stdlib/random/uniform' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var diagonal = require( '@stdlib/ndarray/base/diagonal' ); + +// Create a stack of matrices: +var x = uniform( [ 2, 3, 3 ], -10.0, 10.0, { + 'dtype': 'float64' +}); + +console.log( 'X: ', ndarray2array( x ) ); + +// Extract the main diagonals of the stack: +var y = diagonal( x, [ 1, 2 ], 0, false ); +console.log( 'diagonal( X, [ 1, 2 ], 0 ): ', ndarray2array( y ) ); + +// Extract the super-diagonals of the stack: +y = diagonal( x, [ 1, 2 ], 1, false ); +console.log( 'diagonal( X, [ 1, 2 ], 1 ): ', ndarray2array( y ) ); + +// Extract the sub-diagonals of the stack: +y = diagonal( x, [ 1, 2 ], -1, false ); +console.log( 'diagonal( X, [ 1, 2 ], -1 ): ', ndarray2array( y ) ); +``` + +
+ + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/ndarray/base/diagonal/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/base/diagonal/benchmark/benchmark.js new file mode 100644 index 000000000000..a013a1546991 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/diagonal/benchmark/benchmark.js @@ -0,0 +1,272 @@ +/** +* @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 bench = require( '@stdlib/bench' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var baseEmpty = require( '@stdlib/ndarray/base/empty' ); +var empty = require( '@stdlib/ndarray/empty' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var diagonal = require( './../lib' ); + + +// MAIN // + +bench( format( '%s::2d,base', pkg ), function benchmark( b ) { + var values; + var v; + var i; + + values = [ + baseEmpty( 'float64', [ 2, 2 ], 'row-major' ), + baseEmpty( 'float32', [ 2, 2 ], 'row-major' ), + baseEmpty( 'int32', [ 2, 2 ], 'row-major' ), + baseEmpty( 'complex128', [ 2, 2 ], 'row-major' ), + baseEmpty( 'generic', [ 2, 2 ], 'row-major' ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = diagonal( values[ i%values.length ], [ 0, 1 ], 0, false ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( v ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::2d,non-base', pkg ), function benchmark( b ) { + var values; + var v; + var i; + + /* eslint-disable object-curly-newline, stdlib/line-closing-bracket-spacing */ + + values = [ + empty( [ 2, 2 ], { 'dtype': 'float64' } ), + empty( [ 2, 2 ], { 'dtype': 'float32' } ), + empty( [ 2, 2 ], { 'dtype': 'int32' } ), + empty( [ 2, 2 ], { 'dtype': 'complex128' } ), + empty( [ 2, 2 ], { 'dtype': 'generic' } ) + ]; + + /* eslint-enable object-curly-newline, stdlib/line-closing-bracket-spacing */ + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = diagonal( values[ i%values.length ], [ 0, 1 ], 0, false ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( v ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::3d,base', pkg ), function benchmark( b ) { + var values; + var v; + var i; + + values = [ + baseEmpty( 'float64', [ 2, 2, 2 ], 'row-major' ), + baseEmpty( 'float32', [ 2, 2, 2 ], 'row-major' ), + baseEmpty( 'int32', [ 2, 2, 2 ], 'row-major' ), + baseEmpty( 'complex128', [ 2, 2, 2 ], 'row-major' ), + baseEmpty( 'generic', [ 2, 2, 2 ], 'row-major' ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = diagonal( values[ i%values.length ], [ 1, 2 ], 0, false ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( v ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::3d,non-base', pkg ), function benchmark( b ) { + var values; + var v; + var i; + + /* eslint-disable object-curly-newline, stdlib/line-closing-bracket-spacing */ + + values = [ + empty( [ 2, 2, 2 ], { 'dtype': 'float64' } ), + empty( [ 2, 2, 2 ], { 'dtype': 'float32' } ), + empty( [ 2, 2, 2 ], { 'dtype': 'int32' } ), + empty( [ 2, 2, 2 ], { 'dtype': 'complex128' } ), + empty( [ 2, 2, 2 ], { 'dtype': 'generic' } ) + ]; + + /* eslint-enable object-curly-newline, stdlib/line-closing-bracket-spacing */ + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = diagonal( values[ i%values.length ], [ 1, 2 ], 0, false ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( v ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::4d,base', pkg ), function benchmark( b ) { + var values; + var v; + var i; + + values = [ + baseEmpty( 'float64', [ 2, 2, 2, 2 ], 'row-major' ), + baseEmpty( 'float32', [ 2, 2, 2, 2 ], 'row-major' ), + baseEmpty( 'int32', [ 2, 2, 2, 2 ], 'row-major' ), + baseEmpty( 'complex128', [ 2, 2, 2, 2 ], 'row-major' ), + baseEmpty( 'generic', [ 2, 2, 2, 2 ], 'row-major' ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = diagonal( values[ i%values.length ], [ 2, 3 ], 0, false ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( v ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::4d,non-base', pkg ), function benchmark( b ) { + var values; + var v; + var i; + + /* eslint-disable object-curly-newline, stdlib/line-closing-bracket-spacing */ + + values = [ + empty( [ 2, 2, 2, 2 ], { 'dtype': 'float64' } ), + empty( [ 2, 2, 2, 2 ], { 'dtype': 'float32' } ), + empty( [ 2, 2, 2, 2 ], { 'dtype': 'int32' } ), + empty( [ 2, 2, 2, 2 ], { 'dtype': 'complex128' } ), + empty( [ 2, 2, 2, 2 ], { 'dtype': 'generic' } ) + ]; + + /* eslint-enable object-curly-newline, stdlib/line-closing-bracket-spacing */ + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = diagonal( values[ i%values.length ], [ 2, 3 ], 0, false ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( v ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::5d,base', pkg ), function benchmark( b ) { + var values; + var v; + var i; + + values = [ + baseEmpty( 'float64', [ 2, 2, 2, 2, 2 ], 'row-major' ), + baseEmpty( 'float32', [ 2, 2, 2, 2, 2 ], 'row-major' ), + baseEmpty( 'int32', [ 2, 2, 2, 2, 2 ], 'row-major' ), + baseEmpty( 'complex128', [ 2, 2, 2, 2, 2 ], 'row-major' ), + baseEmpty( 'generic', [ 2, 2, 2, 2, 2 ], 'row-major' ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = diagonal( values[ i%values.length ], [ 3, 4 ], 0, false ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( v ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::5d,non-base', pkg ), function benchmark( b ) { + var values; + var v; + var i; + + /* eslint-disable object-curly-newline, stdlib/line-closing-bracket-spacing */ + + values = [ + empty( [ 2, 2, 2, 2, 2 ], { 'dtype': 'float64' } ), + empty( [ 2, 2, 2, 2, 2 ], { 'dtype': 'float32' } ), + empty( [ 2, 2, 2, 2, 2 ], { 'dtype': 'int32' } ), + empty( [ 2, 2, 2, 2, 2 ], { 'dtype': 'complex128' } ), + empty( [ 2, 2, 2, 2, 2 ], { 'dtype': 'generic' } ) + ]; + + /* eslint-enable object-curly-newline, stdlib/line-closing-bracket-spacing */ + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = diagonal( values[ i%values.length ], [ 3, 4 ], 0, false ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( v ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/ndarray/base/diagonal/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/base/diagonal/docs/repl.txt new file mode 100644 index 000000000000..8368394c5401 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/diagonal/docs/repl.txt @@ -0,0 +1,51 @@ + +{{alias}}( x, dims, k, writable ) + Returns a view of the diagonal of a matrix (or stack of matrices). + + 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. + + Parameters + ---------- + x: ndarray + Input array. + + dims: ArrayLikeObject + Dimension indices defining the plane in which to extract the diagonal. + + k: integer + Diagonal offset. + + writable: boolean + Boolean indicating whether the returned ndarray should be writable. + + Returns + ------- + out: ndarray + Output array view. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/array}}( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ) + [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] + > var y = {{alias}}( x, [ 0, 1 ], 0, false ) + [ 1.0, 4.0 ] + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/ndarray/base/diagonal/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/base/diagonal/docs/types/index.d.ts new file mode 100644 index 000000000000..dd97a5b58cc0 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/diagonal/docs/types/index.d.ts @@ -0,0 +1,57 @@ +/* +* @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. +*/ + +// TypeScript Version: 4.1 + +/// + +import { Collection } from '@stdlib/types/array'; +import { typedndarray } from '@stdlib/types/ndarray'; + +/** +* 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 x - input array +* @param dims - dimension indices defining the plane in which to extract the diagonal +* @param k - diagonal offset +* @param writable - boolean indicating whether the returned ndarray should be writable +* @returns 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 [ [ 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 [ 1.0, 5.0, 9.0 ] +*/ +declare function diagonal = typedndarray>( x: U, dims: Collection, k: number, writable: boolean ): U; + + +// EXPORTS // + +export = diagonal; diff --git a/lib/node_modules/@stdlib/ndarray/base/diagonal/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/base/diagonal/docs/types/test.ts new file mode 100644 index 000000000000..017275efe4db --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/diagonal/docs/types/test.ts @@ -0,0 +1,92 @@ +/* +* @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. +*/ + +import zeros = require( '@stdlib/ndarray/zeros' ); +import diagonal = require( './index' ); + + +// TESTS // + +// The function returns an ndarray... +{ + const x = zeros( [ 2, 2 ] ); + + diagonal( x, [ 0, 1 ], 0, false ); // $ExpectType float64ndarray +} + +// The compiler throws an error if the function is not provided a first argument which is an ndarray... +{ + diagonal( '5', [ 0, 1 ], 0, false ); // $ExpectError + diagonal( 5, [ 0, 1 ], 0, false ); // $ExpectError + diagonal( true, [ 0, 1 ], 0, false ); // $ExpectError + diagonal( false, [ 0, 1 ], 0, false ); // $ExpectError + diagonal( null, [ 0, 1 ], 0, false ); // $ExpectError + diagonal( {}, [ 0, 1 ], 0, false ); // $ExpectError + diagonal( [ '5' ], [ 0, 1 ], 0, false ); // $ExpectError + diagonal( ( x: number ): number => x, [ 0, 1 ], 0, false ); // $ExpectError +} + +// The compiler throws an error if the function is not provided a second argument which is an array-like object containing numbers... +{ + const x = zeros( [ 2, 2 ] ); + + diagonal( x, '5', 0, false ); // $ExpectError + diagonal( x, 5, 0, false ); // $ExpectError + diagonal( x, true, 0, false ); // $ExpectError + diagonal( x, false, 0, false ); // $ExpectError + diagonal( x, null, 0, false ); // $ExpectError + diagonal( x, {}, 0, false ); // $ExpectError + diagonal( x, [ '5' ], 0, false ); // $ExpectError + diagonal( x, ( x: number ): number => x, 0, false ); // $ExpectError +} + +// The compiler throws an error if the function is not provided a third argument which is a number... +{ + const x = zeros( [ 2, 2 ] ); + + diagonal( x, [ 0, 1 ], '5', false ); // $ExpectError + diagonal( x, [ 0, 1 ], true, false ); // $ExpectError + diagonal( x, [ 0, 1 ], false, false ); // $ExpectError + diagonal( x, [ 0, 1 ], null, false ); // $ExpectError + diagonal( x, [ 0, 1 ], {}, false ); // $ExpectError + diagonal( x, [ 0, 1 ], [], false ); // $ExpectError + diagonal( x, [ 0, 1 ], ( x: number ): number => x, false ); // $ExpectError +} + +// The compiler throws an error if the function is not provided a fourth argument which is a boolean... +{ + const x = zeros( [ 2, 2 ] ); + + diagonal( x, [ 0, 1 ], 0, '5' ); // $ExpectError + diagonal( x, [ 0, 1 ], 0, 5 ); // $ExpectError + diagonal( x, [ 0, 1 ], 0, null ); // $ExpectError + diagonal( x, [ 0, 1 ], 0, {} ); // $ExpectError + diagonal( x, [ 0, 1 ], 0, [] ); // $ExpectError + diagonal( x, [ 0, 1 ], 0, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = zeros( [ 2, 2 ] ); + + diagonal(); // $ExpectError + diagonal( x ); // $ExpectError + diagonal( x, [ 0, 1 ] ); // $ExpectError + diagonal( x, [ 0, 1 ], 0 ); // $ExpectError + diagonal( x, [ 0, 1 ], 0, false, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/ndarray/base/diagonal/examples/index.js b/lib/node_modules/@stdlib/ndarray/base/diagonal/examples/index.js new file mode 100644 index 000000000000..02b8ce23c308 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/diagonal/examples/index.js @@ -0,0 +1,42 @@ +/** +* @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'; + +var uniform = require( '@stdlib/random/uniform' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var diagonal = require( './../lib' ); + +// Create a stack of matrices: +var x = uniform( [ 2, 3, 3 ], -10.0, 10.0, { + 'dtype': 'float64' +}); + +console.log( 'X: ', ndarray2array( x ) ); + +// Extract the main diagonals of the stack: +var y = diagonal( x, [ 1, 2 ], 0, false ); +console.log( 'diagonal( X, [ 1, 2 ], 0 ): ', ndarray2array( y ) ); + +// Extract the super-diagonals of the stack: +y = diagonal( x, [ 1, 2 ], 1, false ); +console.log( 'diagonal( X, [ 1, 2 ], 1 ): ', ndarray2array( y ) ); + +// Extract the sub-diagonals of the stack: +y = diagonal( x, [ 1, 2 ], -1, false ); +console.log( 'diagonal( X, [ 1, 2 ], -1 ): ', ndarray2array( y ) ); diff --git a/lib/node_modules/@stdlib/ndarray/base/diagonal/lib/index.js b/lib/node_modules/@stdlib/ndarray/base/diagonal/lib/index.js new file mode 100644 index 000000000000..bbd5a1f5735b --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/diagonal/lib/index.js @@ -0,0 +1,44 @@ +/** +* @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'; + +/** +* Return a view of the diagonal of a matrix (or stack of matrices). +* +* @module @stdlib/ndarray/base/diagonal +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* var diagonal = require( '@stdlib/ndarray/base/diagonal' ); +* +* var x = array( [ [ 1.0, 2.0, 3.0 ], [ 4.0, 5.0, 6.0 ], [ 7.0, 8.0, 9.0 ] ] ); +* // returns [ [ 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 [ 1.0, 5.0, 9.0 ] +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/ndarray/base/diagonal/lib/main.js b/lib/node_modules/@stdlib/ndarray/base/diagonal/lib/main.js new file mode 100644 index 000000000000..8b2818fe7ddf --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/diagonal/lib/main.js @@ -0,0 +1,121 @@ +/** +* @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 [ [ 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 [ 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; diff --git a/lib/node_modules/@stdlib/ndarray/base/diagonal/package.json b/lib/node_modules/@stdlib/ndarray/base/diagonal/package.json new file mode 100644 index 000000000000..b8dd826a0f34 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/diagonal/package.json @@ -0,0 +1,64 @@ +{ + "name": "@stdlib/ndarray/base/diagonal", + "version": "0.0.0", + "description": "Return a view of the diagonal of a matrix (or stack of matrices).", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdtypes", + "types", + "base", + "data", + "structure", + "ndarray", + "diagonal", + "matrix", + "stack", + "view" + ] +} diff --git a/lib/node_modules/@stdlib/ndarray/base/diagonal/test/test.js b/lib/node_modules/@stdlib/ndarray/base/diagonal/test/test.js new file mode 100644 index 000000000000..d72e0277221a --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/diagonal/test/test.js @@ -0,0 +1,521 @@ +/** +* @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 tape = require( 'tape' ); +var array = require( '@stdlib/ndarray/array' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var getStrides = require( '@stdlib/ndarray/strides' ); +var getOffset = require( '@stdlib/ndarray/offset' ); +var getData = require( '@stdlib/ndarray/data-buffer' ); +var getOrder = require( '@stdlib/ndarray/order' ); +var isReadOnly = require( '@stdlib/ndarray/base/assert/is-read-only' ); +var diagonal = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof diagonal, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns a view of the main diagonal of a square matrix', function test( t ) { + var expected; + var x; + var y; + + x = array( [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] ); + + y = diagonal( x, [ 0, 1 ], 0, false ); + + expected = [ 3 ]; + t.deepEqual( getShape( y ), expected, 'returns expected shape' ); + + expected = [ 1, 5, 9 ]; + t.deepEqual( ndarray2array( y ), expected, 'returns expected values' ); + + t.strictEqual( getData( y ), getData( x ), 'shares the same data buffer' ); + + t.end(); +}); + +tape( 'the function returns a view of the super-diagonal of a square matrix', function test( t ) { + var expected; + var x; + var y; + + x = array( [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] ); + + y = diagonal( x, [ 0, 1 ], 1, false ); + + expected = [ 2 ]; + t.deepEqual( getShape( y ), expected, 'returns expected shape' ); + + expected = [ 2, 6 ]; + t.deepEqual( ndarray2array( y ), expected, 'returns expected values' ); + + y = diagonal( x, [ 0, 1 ], 2, false ); + + expected = [ 1 ]; + t.deepEqual( getShape( y ), expected, 'returns expected shape' ); + + expected = [ 3 ]; + t.deepEqual( ndarray2array( y ), expected, 'returns expected values' ); + + t.end(); +}); + +tape( 'the function returns a view of the sub-diagonal of a square matrix', function test( t ) { + var expected; + var x; + var y; + + x = array( [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] ); + + y = diagonal( x, [ 0, 1 ], -1, false ); + + expected = [ 2 ]; + t.deepEqual( getShape( y ), expected, 'returns expected shape' ); + + expected = [ 4, 8 ]; + t.deepEqual( ndarray2array( y ), expected, 'returns expected values' ); + + y = diagonal( x, [ 0, 1 ], -2, false ); + + expected = [ 1 ]; + t.deepEqual( getShape( y ), expected, 'returns expected shape' ); + + expected = [ 7 ]; + t.deepEqual( ndarray2array( y ), expected, 'returns expected values' ); + + t.end(); +}); + +tape( 'the function returns a view of the diagonal of a non-square matrix (M < N)', function test( t ) { + var expected; + var x; + var y; + + x = array( [ [ 1, 2, 3, 4 ], [ 5, 6, 7, 8 ] ] ); + + y = diagonal( x, [ 0, 1 ], 0, false ); + + expected = [ 2 ]; + t.deepEqual( getShape( y ), expected, 'returns expected shape' ); + + expected = [ 1, 6 ]; + t.deepEqual( ndarray2array( y ), expected, 'returns expected values' ); + + y = diagonal( x, [ 0, 1 ], 2, false ); + + expected = [ 2 ]; + t.deepEqual( getShape( y ), expected, 'returns expected shape' ); + + expected = [ 3, 8 ]; + t.deepEqual( ndarray2array( y ), expected, 'returns expected values' ); + + y = diagonal( x, [ 0, 1 ], -1, false ); + + expected = [ 1 ]; + t.deepEqual( getShape( y ), expected, 'returns expected shape' ); + + expected = [ 5 ]; + t.deepEqual( ndarray2array( y ), expected, 'returns expected values' ); + + t.end(); +}); + +tape( 'the function returns a view of the diagonal of a non-square matrix (M > N)', function test( t ) { + var expected; + var x; + var y; + + x = array( [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ], [ 7, 8 ] ] ); + + y = diagonal( x, [ 0, 1 ], 0, false ); + + expected = [ 2 ]; + t.deepEqual( getShape( y ), expected, 'returns expected shape' ); + + expected = [ 1, 4 ]; + t.deepEqual( ndarray2array( y ), expected, 'returns expected values' ); + + y = diagonal( x, [ 0, 1 ], -2, false ); + + expected = [ 2 ]; + t.deepEqual( getShape( y ), expected, 'returns expected shape' ); + + expected = [ 5, 8 ]; + t.deepEqual( ndarray2array( y ), expected, 'returns expected values' ); + + y = diagonal( x, [ 0, 1 ], 1, false ); + + expected = [ 1 ]; + t.deepEqual( getShape( y ), expected, 'returns expected shape' ); + + expected = [ 2 ]; + t.deepEqual( ndarray2array( y ), expected, 'returns expected values' ); + + t.end(); +}); + +tape( 'the function returns an empty view when the diagonal offset is out-of-bounds', function test( t ) { + var expected; + var x; + var y; + + x = array( [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] ); + + y = diagonal( x, [ 0, 1 ], 3, false ); + + expected = [ 0 ]; + t.deepEqual( getShape( y ), expected, 'returns expected shape' ); + + expected = []; + t.deepEqual( ndarray2array( y ), expected, 'returns expected values' ); + + y = diagonal( x, [ 0, 1 ], -3, false ); + + expected = [ 0 ]; + t.deepEqual( getShape( y ), expected, 'returns expected shape' ); + + expected = []; + t.deepEqual( ndarray2array( y ), expected, 'returns expected values' ); + + y = diagonal( x, [ 0, 1 ], 100, false ); + + expected = [ 0 ]; + t.deepEqual( getShape( y ), expected, 'returns expected shape' ); + + expected = []; + t.deepEqual( ndarray2array( y ), expected, 'returns expected values' ); + + t.end(); +}); + +tape( 'the function swaps the row-like and column-like dimensions when `dims` is reversed', function test( t ) { + var expected; + var x; + var y; + + x = array( [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] ); + + y = diagonal( x, [ 1, 0 ], 1, false ); + + expected = [ 2 ]; + t.deepEqual( getShape( y ), expected, 'returns expected shape' ); + + expected = [ 4, 8 ]; + t.deepEqual( ndarray2array( y ), expected, 'returns expected values' ); + + y = diagonal( x, [ 1, 0 ], -1, false ); + + expected = [ 2 ]; + t.deepEqual( getShape( y ), expected, 'returns expected shape' ); + + expected = [ 2, 6 ]; + t.deepEqual( ndarray2array( y ), expected, 'returns expected values' ); + + t.end(); +}); + +tape( 'the function returns a view of the diagonals of a stack of matrices', function test( t ) { + var expected; + var x; + var y; + + x = array( [ [ [ 1, 2 ], [ 3, 4 ] ], [ [ 5, 6 ], [ 7, 8 ] ] ] ); + + y = diagonal( x, [ 1, 2 ], 0, false ); + + expected = [ 2, 2 ]; + t.deepEqual( getShape( y ), expected, 'returns expected shape' ); + + expected = [ [ 1, 4 ], [ 5, 8 ] ]; + t.deepEqual( ndarray2array( y ), expected, 'returns expected values' ); + + y = diagonal( x, [ 1, 2 ], 1, false ); + + expected = [ 2, 1 ]; + t.deepEqual( getShape( y ), expected, 'returns expected shape' ); + + expected = [ [ 2 ], [ 6 ] ]; + t.deepEqual( ndarray2array( y ), expected, 'returns expected values' ); + + y = diagonal( x, [ 1, 2 ], -1, false ); + + expected = [ 2, 1 ]; + t.deepEqual( getShape( y ), expected, 'returns expected shape' ); + + expected = [ [ 3 ], [ 7 ] ]; + t.deepEqual( ndarray2array( y ), expected, 'returns expected values' ); + + t.end(); +}); + +tape( 'the function appends the diagonal dimension as the trailing dimension of the output ndarray', function test( t ) { + var expected; + var x; + var y; + + x = array( [ [ [ 1, 2, 3 ], [ 4, 5, 6 ] ], [ [ 7, 8, 9 ], [ 10, 11, 12 ] ] ] ); + + y = diagonal( x, [ 0, 2 ], 0, false ); + + expected = [ 2, 2 ]; + t.deepEqual( getShape( y ), expected, 'returns expected shape' ); + + expected = [ [ 1, 8 ], [ 4, 11 ] ]; + t.deepEqual( ndarray2array( y ), expected, 'returns expected values' ); + + t.end(); +}); + +tape( 'the function normalizes negative dimension indices', function test( t ) { + var expected; + var x; + var y; + + x = array( [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] ); + + y = diagonal( x, [ -2, -1 ], 0, false ); + + expected = [ 3 ]; + t.deepEqual( getShape( y ), expected, 'returns expected shape' ); + + expected = [ 1, 5, 9 ]; + t.deepEqual( ndarray2array( y ), expected, 'returns expected values' ); + + t.end(); +}); + +tape( 'the function returns a column-major ndarray when the input ndarray is column-major', function test( t ) { + var expected; + var x; + var y; + + x = ndarray( 'generic', [ 1, 4, 7, 2, 5, 8, 3, 6, 9 ], [ 3, 3 ], [ 1, 3 ], 0, 'column-major' ); + + y = diagonal( x, [ 0, 1 ], 0, false ); + + expected = 'column-major'; + t.strictEqual( getOrder( y ), expected, 'returns expected order' ); + + expected = [ 3 ]; + t.deepEqual( getShape( y ), expected, 'returns expected shape' ); + + expected = [ 1, 5, 9 ]; + t.deepEqual( ndarray2array( y ), expected, 'returns expected values' ); + + t.end(); +}); + +tape( 'the function returns a writable ndarray when `writable` is `true`', function test( t ) { + var expected; + var x; + var y; + + x = array( [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] ); + + y = diagonal( x, [ 0, 1 ], 0, true ); + + expected = false; + t.strictEqual( isReadOnly( y ), expected, 'returns a writable ndarray' ); + + t.strictEqual( getData( y ), getData( x ), 'shares the same data buffer' ); + + t.end(); +}); + +tape( 'the function returns a read-only ndarray when `writable` is `false`', function test( t ) { + var expected; + var x; + var y; + + x = array( [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] ); + + y = diagonal( x, [ 0, 1 ], 0, false ); + + expected = true; + t.strictEqual( isReadOnly( y ), expected, 'returns a read-only ndarray' ); + + t.end(); +}); + +tape( 'the function constructs the output by retaining all dimensions except the specified dimensions and appending the diagonal dimension', function test( t ) { + var expected; + var x; + var y; + + x = array( [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ], { + 'shape': [ 3, 2, 2 ] + }); + + y = diagonal( x, [ 1, 2 ], 0, false ); + + expected = [ 3, 2 ]; + t.deepEqual( getShape( y ), expected, 'returns expected shape' ); + + expected = [ 4, 3 ]; + t.deepEqual( getStrides( y ), expected, 'returns expected strides' ); + + expected = getOffset( x ); + t.strictEqual( getOffset( y ), expected, 'returns expected offset' ); + + t.end(); +}); + +tape( 'the function adjusts the offset to point to the first element of the requested diagonal', function test( t ) { + var expected; + var x; + var y; + + x = array( [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] ); + + y = diagonal( x, [ 0, 1 ], 1, false ); + + expected = getOffset( x ) + getStrides( x )[1]; + t.strictEqual( getOffset( y ), expected, 'returns expected offset' ); + + y = diagonal( x, [ 0, 1 ], -1, false ); + + expected = getOffset( x ) + getStrides( x )[0]; + t.strictEqual( getOffset( y ), expected, 'returns expected offset' ); + + t.end(); +}); + +tape( 'the function supports input ndarrays having a non-zero buffer offset', function test( t ) { + var expected; + var x; + var y; + + x = ndarray( 'generic', [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ], [ 3, 3 ], [ 3, 1 ], 1, 'row-major' ); + + y = diagonal( x, [ 0, 1 ], 0, false ); + + expected = [ 3 ]; + t.deepEqual( getShape( y ), expected, 'returns expected shape' ); + + expected = [ 1, 5, 9 ]; + t.deepEqual( ndarray2array( y ), expected, 'returns expected values' ); + + t.strictEqual( getData( y ), getData( x ), 'shares the same data buffer' ); + + y = diagonal( x, [ 0, 1 ], 1, false ); + + expected = [ 2, 6 ]; + t.deepEqual( ndarray2array( y ), expected, 'returns expected values' ); + + y = diagonal( x, [ 0, 1 ], -1, false ); + + expected = [ 4, 8 ]; + t.deepEqual( ndarray2array( y ), expected, 'returns expected values' ); + + t.end(); +}); + +tape( 'the function supports 4D inputs with non-trailing `dims`', function test( t ) { + var expected; + var x; + var y; + + x = array([ + 0, + 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 + ], { + 'shape': [ 2, 3, 3, 2 ] + }); + y = diagonal( x, [ 1, 2 ], 0, false ); + expected = [ 2, 2, 3 ]; + + t.deepEqual( getShape( y ), expected, 'returns expected shape' ); + + expected = [ + [ [ 0, 8, 16 ], [ 1, 9, 17 ] ], + [ [ 18, 26, 34 ], [ 19, 27, 35 ] ] + ]; + + t.deepEqual( ndarray2array( y ), expected, 'returns expected values' ); + t.strictEqual( getData( y ), getData( x ), 'shares the same data buffer' ); + + t.end(); +}); + +tape( 'the function supports negative dimension indices combined with a non-zero offset `k`', function test( t ) { + var expected; + var x; + var y; + + x = array( [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] ); + y = diagonal( x, [ -2, -1 ], 1, false ); + + expected = [ 2 ]; + t.deepEqual( getShape( y ), expected, 'returns expected shape' ); + + expected = [ 2, 6 ]; + t.deepEqual( ndarray2array( y ), expected, 'returns expected values' ); + + y = diagonal( x, [ -2, -1 ], -1, false ); + + expected = [ 4, 8 ]; + t.deepEqual( ndarray2array( y ), expected, 'returns expected values' ); + + t.end(); +}); From 765c2b38ed13e0639567fa64e85584b03da89fd0 Mon Sep 17 00:00:00 2001 From: Athan Date: Sat, 2 May 2026 02:30:36 -0700 Subject: [PATCH 02/12] Apply suggestions from code review Co-authored-by: Athan Signed-off-by: Athan --- lib/node_modules/@stdlib/ndarray/base/diagonal/README.md | 7 +++---- .../@stdlib/ndarray/base/diagonal/examples/index.js | 7 +++---- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/base/diagonal/README.md b/lib/node_modules/@stdlib/ndarray/base/diagonal/README.md index db1c3ca5d67b..5be71421a5a2 100644 --- a/lib/node_modules/@stdlib/ndarray/base/diagonal/README.md +++ b/lib/node_modules/@stdlib/ndarray/base/diagonal/README.md @@ -122,20 +122,19 @@ var diagonal = require( '@stdlib/ndarray/base/diagonal' ); var x = uniform( [ 2, 3, 3 ], -10.0, 10.0, { 'dtype': 'float64' }); - console.log( 'X: ', ndarray2array( x ) ); // Extract the main diagonals of the stack: var y = diagonal( x, [ 1, 2 ], 0, false ); -console.log( 'diagonal( X, [ 1, 2 ], 0 ): ', ndarray2array( y ) ); +console.log( ndarray2array( y ) ); // Extract the super-diagonals of the stack: y = diagonal( x, [ 1, 2 ], 1, false ); -console.log( 'diagonal( X, [ 1, 2 ], 1 ): ', ndarray2array( y ) ); +console.log( ndarray2array( y ) ); // Extract the sub-diagonals of the stack: y = diagonal( x, [ 1, 2 ], -1, false ); -console.log( 'diagonal( X, [ 1, 2 ], -1 ): ', ndarray2array( y ) ); +console.log( ndarray2array( y ) ); ``` diff --git a/lib/node_modules/@stdlib/ndarray/base/diagonal/examples/index.js b/lib/node_modules/@stdlib/ndarray/base/diagonal/examples/index.js index 02b8ce23c308..7c1d65590c1c 100644 --- a/lib/node_modules/@stdlib/ndarray/base/diagonal/examples/index.js +++ b/lib/node_modules/@stdlib/ndarray/base/diagonal/examples/index.js @@ -26,17 +26,16 @@ var diagonal = require( './../lib' ); var x = uniform( [ 2, 3, 3 ], -10.0, 10.0, { 'dtype': 'float64' }); - console.log( 'X: ', ndarray2array( x ) ); // Extract the main diagonals of the stack: var y = diagonal( x, [ 1, 2 ], 0, false ); -console.log( 'diagonal( X, [ 1, 2 ], 0 ): ', ndarray2array( y ) ); +console.log( ndarray2array( y ) ); // Extract the super-diagonals of the stack: y = diagonal( x, [ 1, 2 ], 1, false ); -console.log( 'diagonal( X, [ 1, 2 ], 1 ): ', ndarray2array( y ) ); +console.log( ndarray2array( y ) ); // Extract the sub-diagonals of the stack: y = diagonal( x, [ 1, 2 ], -1, false ); -console.log( 'diagonal( X, [ 1, 2 ], -1 ): ', ndarray2array( y ) ); +console.log( ndarray2array( y ) ); From c12971bf0c56358062e4d4d30c7f8ae0a2fa6ce7 Mon Sep 17 00:00:00 2001 From: Athan Date: Sat, 2 May 2026 02:31:09 -0700 Subject: [PATCH 03/12] Apply suggestions from code review Co-authored-by: Athan Signed-off-by: Athan --- lib/node_modules/@stdlib/ndarray/base/diagonal/README.md | 2 +- .../@stdlib/ndarray/base/diagonal/examples/index.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/base/diagonal/README.md b/lib/node_modules/@stdlib/ndarray/base/diagonal/README.md index 5be71421a5a2..213a561a6e00 100644 --- a/lib/node_modules/@stdlib/ndarray/base/diagonal/README.md +++ b/lib/node_modules/@stdlib/ndarray/base/diagonal/README.md @@ -122,7 +122,7 @@ var diagonal = require( '@stdlib/ndarray/base/diagonal' ); var x = uniform( [ 2, 3, 3 ], -10.0, 10.0, { 'dtype': 'float64' }); -console.log( 'X: ', ndarray2array( x ) ); +console.log( ndarray2array( x ) ); // Extract the main diagonals of the stack: var y = diagonal( x, [ 1, 2 ], 0, false ); diff --git a/lib/node_modules/@stdlib/ndarray/base/diagonal/examples/index.js b/lib/node_modules/@stdlib/ndarray/base/diagonal/examples/index.js index 7c1d65590c1c..fc6fe8568930 100644 --- a/lib/node_modules/@stdlib/ndarray/base/diagonal/examples/index.js +++ b/lib/node_modules/@stdlib/ndarray/base/diagonal/examples/index.js @@ -26,7 +26,7 @@ var diagonal = require( './../lib' ); var x = uniform( [ 2, 3, 3 ], -10.0, 10.0, { 'dtype': 'float64' }); -console.log( 'X: ', ndarray2array( x ) ); +console.log( ndarray2array( x ) ); // Extract the main diagonals of the stack: var y = diagonal( x, [ 1, 2 ], 0, false ); From 85df83e8e3e73e3a9ddc88920557428e403a0ea4 Mon Sep 17 00:00:00 2001 From: headlessNode Date: Sat, 2 May 2026 23:46:16 +0500 Subject: [PATCH 04/12] fix: apply suggestions from code review --- .../@stdlib/ndarray/base/diagonal/README.md | 2 +- .../base/diagonal/benchmark/benchmark.js | 16 +-- .../ndarray/base/diagonal/docs/repl.txt | 5 +- .../base/diagonal/docs/types/index.d.ts | 2 +- .../@stdlib/ndarray/base/diagonal/lib/main.js | 67 +++++++----- .../ndarray/base/diagonal/test/test.js | 100 ++++++++++++++++++ 6 files changed, 156 insertions(+), 36 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/base/diagonal/README.md b/lib/node_modules/@stdlib/ndarray/base/diagonal/README.md index 213a561a6e00..040fdf7a99d2 100644 --- a/lib/node_modules/@stdlib/ndarray/base/diagonal/README.md +++ b/lib/node_modules/@stdlib/ndarray/base/diagonal/README.md @@ -85,7 +85,7 @@ var y = diagonal( x, [ 0, 1 ], 0, false ); The function accepts the following arguments: - **x**: input ndarray. -- **dims**: dimension indices defining the plane in which to extract the diagonal. +- **dims**: dimension indices defining the plane from which to extract the diagonal. - **k**: diagonal offset. - **writable**: boolean indicating whether the returned ndarray should be writable. diff --git a/lib/node_modules/@stdlib/ndarray/base/diagonal/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/base/diagonal/benchmark/benchmark.js index a013a1546991..7eafaa6c0539 100644 --- a/lib/node_modules/@stdlib/ndarray/base/diagonal/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/ndarray/base/diagonal/benchmark/benchmark.js @@ -31,7 +31,7 @@ var diagonal = require( './../lib' ); // MAIN // -bench( format( '%s::2d,base', pkg ), function benchmark( b ) { +bench( format( '%s::ndims=2,ctor=base', pkg ), function benchmark( b ) { var values; var v; var i; @@ -59,7 +59,7 @@ bench( format( '%s::2d,base', pkg ), function benchmark( b ) { b.end(); }); -bench( format( '%s::2d,non-base', pkg ), function benchmark( b ) { +bench( format( '%s::ndims=2,ctor=non-base', pkg ), function benchmark( b ) { var values; var v; var i; @@ -91,7 +91,7 @@ bench( format( '%s::2d,non-base', pkg ), function benchmark( b ) { b.end(); }); -bench( format( '%s::3d,base', pkg ), function benchmark( b ) { +bench( format( '%s::ndims=3,ctor=base', pkg ), function benchmark( b ) { var values; var v; var i; @@ -119,7 +119,7 @@ bench( format( '%s::3d,base', pkg ), function benchmark( b ) { b.end(); }); -bench( format( '%s::3d,non-base', pkg ), function benchmark( b ) { +bench( format( '%s::ndims=3,ctor=non-base', pkg ), function benchmark( b ) { var values; var v; var i; @@ -151,7 +151,7 @@ bench( format( '%s::3d,non-base', pkg ), function benchmark( b ) { b.end(); }); -bench( format( '%s::4d,base', pkg ), function benchmark( b ) { +bench( format( '%s::ndims=4,ctor=base', pkg ), function benchmark( b ) { var values; var v; var i; @@ -179,7 +179,7 @@ bench( format( '%s::4d,base', pkg ), function benchmark( b ) { b.end(); }); -bench( format( '%s::4d,non-base', pkg ), function benchmark( b ) { +bench( format( '%s::ndims=4,ctor=non-base', pkg ), function benchmark( b ) { var values; var v; var i; @@ -211,7 +211,7 @@ bench( format( '%s::4d,non-base', pkg ), function benchmark( b ) { b.end(); }); -bench( format( '%s::5d,base', pkg ), function benchmark( b ) { +bench( format( '%s::ndims=5,ctor=base', pkg ), function benchmark( b ) { var values; var v; var i; @@ -239,7 +239,7 @@ bench( format( '%s::5d,base', pkg ), function benchmark( b ) { b.end(); }); -bench( format( '%s::5d,non-base', pkg ), function benchmark( b ) { +bench( format( '%s::ndims=5,ctor=non-base', pkg ), function benchmark( b ) { var values; var v; var i; diff --git a/lib/node_modules/@stdlib/ndarray/base/diagonal/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/base/diagonal/docs/repl.txt index 8368394c5401..3f46279bb541 100644 --- a/lib/node_modules/@stdlib/ndarray/base/diagonal/docs/repl.txt +++ b/lib/node_modules/@stdlib/ndarray/base/diagonal/docs/repl.txt @@ -6,7 +6,8 @@ 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]. + 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 @@ -26,7 +27,7 @@ Input array. dims: ArrayLikeObject - Dimension indices defining the plane in which to extract the diagonal. + Dimension indices defining the plane from which to extract the diagonal. k: integer Diagonal offset. diff --git a/lib/node_modules/@stdlib/ndarray/base/diagonal/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/base/diagonal/docs/types/index.d.ts index dd97a5b58cc0..2232ced6dfcf 100644 --- a/lib/node_modules/@stdlib/ndarray/base/diagonal/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/ndarray/base/diagonal/docs/types/index.d.ts @@ -35,7 +35,7 @@ import { typedndarray } from '@stdlib/types/ndarray'; * - The `writable` parameter **only** applies to ndarray constructors supporting **read-only** instances. * * @param x - input array -* @param dims - dimension indices defining the plane in which to extract the diagonal +* @param dims - dimension indices defining the plane from which to extract the diagonal * @param k - diagonal offset * @param writable - boolean indicating whether the returned ndarray should be writable * @returns ndarray view diff --git a/lib/node_modules/@stdlib/ndarray/base/diagonal/lib/main.js b/lib/node_modules/@stdlib/ndarray/base/diagonal/lib/main.js index 8b2818fe7ddf..1e58a58ea410 100644 --- a/lib/node_modules/@stdlib/ndarray/base/diagonal/lib/main.js +++ b/lib/node_modules/@stdlib/ndarray/base/diagonal/lib/main.js @@ -20,13 +20,16 @@ // 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' ); +var toUniqueNormalizedIndices = require( '@stdlib/ndarray/base/to-unique-normalized-indices' ); +var getDType = require( '@stdlib/ndarray/base/dtype' ); +var getShape = require( '@stdlib/ndarray/base/shape' ); +var getStrides = require( '@stdlib/ndarray/base/strides' ); +var getOffset = require( '@stdlib/ndarray/base/offset' ); +var getOrder = require( '@stdlib/ndarray/base/order' ); +var getData = require( '@stdlib/ndarray/base/data-buffer' ); +var join = require( '@stdlib/array/base/join' ); +var min = require( '@stdlib/math/base/special/min' ); +var format = require( '@stdlib/string/format' ); // MAIN // @@ -43,9 +46,13 @@ var getData = require( '@stdlib/ndarray/data-buffer' ); * - 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 {IntegerArray} dims - dimension indices defining the plane from which to extract the diagonal * @param {integer} k - diagonal offset * @param {boolean} writable - boolean indicating whether the returned ndarray should be writable +* @throws {RangeError} must provide exactly two dimension indices +* @throws {RangeError} input ndarray must have at least two dimensions +* @throws {RangeError} must provide valid dimension indices +* @throws {Error} must provide unique dimension indices * @returns {ndarray} ndarray view * * @example @@ -60,47 +67,59 @@ var getData = require( '@stdlib/ndarray/data-buffer' ); function diagonal( x, dims, k, writable ) { var strides; var offset; + var ndims; var shape; + var rows; + var cols; var sh; var st; var sr; var sc; - var kp; - var kn; + var ro; + var co; var d; - var N; var L; var i; + if ( dims.length !== 2 ) { + throw new RangeError( format( 'invalid argument. Must provide exactly two dimension indices. Value: `[%s]`.', join( dims, ', ' ) ) ); + } sh = getShape( x ); + ndims = sh.length; + if ( ndims < 2 ) { + throw new RangeError( format( 'invalid argument. Input ndarray must have at least two dimensions. Number of dimensions: %d.', ndims ) ); + } st = getStrides( x ); - N = sh.length; - - // Normalize the dimension indices: - d = toNormalizedIndices( dims, N-1 ); + // Resolve dimension indices... + d = toUniqueNormalizedIndices( dims, ndims-1 ); + if ( d === null ) { + throw new RangeError( format( 'invalid argument. Specified dimension index is out-of-bounds. Must be on the interval: [-%u, %u]. Value: `[%s]`.', ndims, ndims-1, join( dims, ', ' ) ) ); + } + if ( d.length !== dims.length ) { + throw new Error( format( 'invalid argument. Must provide unique dimension indices. Value: `[%s]`.', join( dims, ', ' ) ) ); + } sr = st[ d[0] ]; sc = st[ d[1] ]; - // Resolve the positive and negative parts of `k`: - kp = ( k > 0 ) ? k : 0; - kn = kp - k; + // Resolve the row and column offsets corresponding to the diagonal offset `k = column - row`: + co = ( k > 0 ) ? k : 0; + ro = co - 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; - } + rows = sh[ d[0] ] - ro; + cols = sh[ d[1] ] - co; + L = min( rows, cols ); 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 ); + offset = getOffset( x ) + ( co*sc ) + ( ro*sr ); // Drop the specified dimensions and append the diagonal dimension: shape = []; strides = []; - for ( i = 0; i < N; i++ ) { + for ( i = 0; i < ndims; i++ ) { if ( i === d[0] || i === d[1] ) { continue; } diff --git a/lib/node_modules/@stdlib/ndarray/base/diagonal/test/test.js b/lib/node_modules/@stdlib/ndarray/base/diagonal/test/test.js index d72e0277221a..83b477179eab 100644 --- a/lib/node_modules/@stdlib/ndarray/base/diagonal/test/test.js +++ b/lib/node_modules/@stdlib/ndarray/base/diagonal/test/test.js @@ -21,6 +21,7 @@ // MODULES // var tape = require( 'tape' ); +var Float64Array = require( '@stdlib/array/float64' ); var array = require( '@stdlib/ndarray/array' ); var ndarray = require( '@stdlib/ndarray/base/ctor' ); var ndarray2array = require( '@stdlib/ndarray/to-array' ); @@ -61,6 +62,105 @@ tape( 'the function returns a view of the main diagonal of a square matrix', fun t.end(); }); +tape( 'the function throws an error if not provided exactly two dimension indices', function test( t ) { + var values; + var x; + var i; + + x = new ndarray( 'float64', new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ), [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + values = [ + [], + [ 0 ], + [ 0, 1, 0 ], + [ 0, 1, 0, 1 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ].length + ' dimension indices' ); + } + t.end(); + + function badValue( dims ) { + return function badValue() { + diagonal( x, dims, 0, false ); + }; + } +}); + +tape( 'the function throws an error if provided an input ndarray with fewer than two dimensions', function test( t ) { + var values; + var i; + + values = [ + new ndarray( 'float64', new Float64Array( [ 5.0 ] ), [], [ 0 ], 0, 'row-major' ), + new ndarray( 'float64', new Float64Array( [ 1.0, 2.0, 3.0 ] ), [ 3 ], [ 1 ], 0, 'row-major' ) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided an input ndarray with '+values[ i ].ndims+' dimensions' ); + } + t.end(); + + function badValue( x ) { + return function badValue() { + diagonal( x, [ 0, 1 ], 0, false ); + }; + } +}); + +tape( 'the function throws an error if provided out-of-bounds dimension indices', function test( t ) { + var values; + var x; + var i; + + x = new ndarray( 'float64', new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ), [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + values = [ + [ 0, 2 ], + [ 2, 0 ], + [ -3, 0 ], + [ 0, -3 ], + [ 10, 0 ], + [ 0, 10 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided dimension indices ['+values[ i ]+']' ); + } + t.end(); + + function badValue( dims ) { + return function badValue() { + diagonal( x, dims, 0, false ); + }; + } +}); + +tape( 'the function throws an error if provided duplicate dimension indices', function test( t ) { + var values; + var x; + var i; + + x = new ndarray( 'float64', new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ), [ 2, 2, 2 ], [ 4, 2, 1 ], 0, 'row-major' ); + + values = [ + [ 0, 0 ], + [ 1, 1 ], + [ 2, 2 ], + [ 0, -3 ], + [ -2, 1 ], + [ -1, 2 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided dimension indices ['+values[ i ]+']' ); + } + t.end(); + + function badValue( dims ) { + return function badValue() { + diagonal( x, dims, 0, false ); + }; + } +}); + tape( 'the function returns a view of the super-diagonal of a square matrix', function test( t ) { var expected; var x; From 6bcf4f5e59643d348ef9badbf90d6a7728741491 Mon Sep 17 00:00:00 2001 From: headlessNode Date: Sat, 2 May 2026 23:53:20 +0500 Subject: [PATCH 05/12] fix: apply suggestions from code review --- lib/node_modules/@stdlib/ndarray/base/diagonal/lib/main.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/ndarray/base/diagonal/lib/main.js b/lib/node_modules/@stdlib/ndarray/base/diagonal/lib/main.js index 1e58a58ea410..2004b48aa9e0 100644 --- a/lib/node_modules/@stdlib/ndarray/base/diagonal/lib/main.js +++ b/lib/node_modules/@stdlib/ndarray/base/diagonal/lib/main.js @@ -114,7 +114,10 @@ function diagonal( x, dims, k, writable ) { L = 0; } // Adjust the offset so that we point to the first element along the specified diagonal: - offset = getOffset( x ) + ( co*sc ) + ( ro*sr ); + offset = getOffset( x ); + if ( L > 0 ) { + offset += ( co*sc ) + ( ro*sr ); + } // Drop the specified dimensions and append the diagonal dimension: shape = []; From fbdf99c0ff9af2d9c516b4eccf84e35e69d70295 Mon Sep 17 00:00:00 2001 From: headlessNode Date: Sat, 2 May 2026 23:59:54 +0500 Subject: [PATCH 06/12] fix: apply suggestions from code review --- .../@stdlib/ndarray/base/diagonal/lib/main.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/base/diagonal/lib/main.js b/lib/node_modules/@stdlib/ndarray/base/diagonal/lib/main.js index 2004b48aa9e0..cfd0eaeb7999 100644 --- a/lib/node_modules/@stdlib/ndarray/base/diagonal/lib/main.js +++ b/lib/node_modules/@stdlib/ndarray/base/diagonal/lib/main.js @@ -106,16 +106,16 @@ function diagonal( x, dims, k, writable ) { co = ( k > 0 ) ? k : 0; ro = co - k; - // Compute the length of the diagonal (clamped to be non-negative): + // Compute the length of the diagonal: rows = sh[ d[0] ] - ro; cols = sh[ d[1] ] - co; L = min( rows, cols ); + + // Adjust the offset so that we point to the first element along the specified diagonal (otherwise, the diagonal is empty and the offset remains at the buffer base): + offset = getOffset( x ); if ( L < 0 ) { L = 0; - } - // Adjust the offset so that we point to the first element along the specified diagonal: - offset = getOffset( x ); - if ( L > 0 ) { + } else { offset += ( co*sc ) + ( ro*sr ); } From 2def5700fd96796bb00ece35a6bc353097998dea Mon Sep 17 00:00:00 2001 From: Athan Date: Sat, 2 May 2026 21:13:30 -0700 Subject: [PATCH 07/12] Apply suggestions from code review Co-authored-by: Athan Signed-off-by: Athan --- lib/node_modules/@stdlib/ndarray/base/diagonal/lib/main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/ndarray/base/diagonal/lib/main.js b/lib/node_modules/@stdlib/ndarray/base/diagonal/lib/main.js index cfd0eaeb7999..9736248ca6f4 100644 --- a/lib/node_modules/@stdlib/ndarray/base/diagonal/lib/main.js +++ b/lib/node_modules/@stdlib/ndarray/base/diagonal/lib/main.js @@ -87,7 +87,7 @@ function diagonal( x, dims, k, writable ) { sh = getShape( x ); ndims = sh.length; if ( ndims < 2 ) { - throw new RangeError( format( 'invalid argument. Input ndarray must have at least two dimensions. Number of dimensions: %d.', ndims ) ); + throw new RangeError( format( 'invalid argument. First argument must be an ndarray having at least two dimensions. Number of dimensions: %d.', ndims ) ); } st = getStrides( x ); From 6a296b7cc8743714affb17ec3691564c06e95f67 Mon Sep 17 00:00:00 2001 From: Athan Date: Sat, 2 May 2026 21:20:21 -0700 Subject: [PATCH 08/12] Apply suggestions from code review Co-authored-by: Athan Signed-off-by: Athan --- .../@stdlib/ndarray/base/diagonal/lib/main.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/base/diagonal/lib/main.js b/lib/node_modules/@stdlib/ndarray/base/diagonal/lib/main.js index 9736248ca6f4..fdcd57efcbf8 100644 --- a/lib/node_modules/@stdlib/ndarray/base/diagonal/lib/main.js +++ b/lib/node_modules/@stdlib/ndarray/base/diagonal/lib/main.js @@ -110,12 +110,13 @@ function diagonal( x, dims, k, writable ) { rows = sh[ d[0] ] - ro; cols = sh[ d[1] ] - co; L = min( rows, cols ); - - // Adjust the offset so that we point to the first element along the specified diagonal (otherwise, the diagonal is empty and the offset remains at the buffer base): - offset = getOffset( x ); if ( L < 0 ) { L = 0; - } else { + } + + // Adjust the offset so that we point to the first element along the specified diagonal: + offset = getOffset( x ); + if ( L > 0 ) { offset += ( co*sc ) + ( ro*sr ); } From dcbc8c8005cc15f47e0636a85113c735a08a7db6 Mon Sep 17 00:00:00 2001 From: Athan Date: Sat, 2 May 2026 21:24:04 -0700 Subject: [PATCH 09/12] Apply suggestions from code review Co-authored-by: Athan Signed-off-by: Athan --- lib/node_modules/@stdlib/ndarray/base/diagonal/README.md | 6 +++--- .../@stdlib/ndarray/base/diagonal/examples/index.js | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/base/diagonal/README.md b/lib/node_modules/@stdlib/ndarray/base/diagonal/README.md index 040fdf7a99d2..907883006d4e 100644 --- a/lib/node_modules/@stdlib/ndarray/base/diagonal/README.md +++ b/lib/node_modules/@stdlib/ndarray/base/diagonal/README.md @@ -124,15 +124,15 @@ var x = uniform( [ 2, 3, 3 ], -10.0, 10.0, { }); console.log( ndarray2array( x ) ); -// Extract the main diagonals of the stack: +// Extract the main diagonals from the stack: var y = diagonal( x, [ 1, 2 ], 0, false ); console.log( ndarray2array( y ) ); -// Extract the super-diagonals of the stack: +// Extract super-diagonals from the stack: y = diagonal( x, [ 1, 2 ], 1, false ); console.log( ndarray2array( y ) ); -// Extract the sub-diagonals of the stack: +// Extract sub-diagonals from the stack: y = diagonal( x, [ 1, 2 ], -1, false ); console.log( ndarray2array( y ) ); ``` diff --git a/lib/node_modules/@stdlib/ndarray/base/diagonal/examples/index.js b/lib/node_modules/@stdlib/ndarray/base/diagonal/examples/index.js index fc6fe8568930..e8983375b6e5 100644 --- a/lib/node_modules/@stdlib/ndarray/base/diagonal/examples/index.js +++ b/lib/node_modules/@stdlib/ndarray/base/diagonal/examples/index.js @@ -28,14 +28,14 @@ var x = uniform( [ 2, 3, 3 ], -10.0, 10.0, { }); console.log( ndarray2array( x ) ); -// Extract the main diagonals of the stack: +// Extract the main diagonals from the stack: var y = diagonal( x, [ 1, 2 ], 0, false ); console.log( ndarray2array( y ) ); -// Extract the super-diagonals of the stack: +// Extract super-diagonals from the stack: y = diagonal( x, [ 1, 2 ], 1, false ); console.log( ndarray2array( y ) ); -// Extract the sub-diagonals of the stack: +// Extract sub-diagonals from the stack: y = diagonal( x, [ 1, 2 ], -1, false ); console.log( ndarray2array( y ) ); From 5141c4be6337b22ec7f7a8dbcad9e72453bae480 Mon Sep 17 00:00:00 2001 From: Athan Date: Sat, 2 May 2026 23:55:09 -0700 Subject: [PATCH 10/12] Apply suggestions from code review Co-authored-by: Athan Signed-off-by: Athan --- lib/node_modules/@stdlib/ndarray/base/diagonal/docs/repl.txt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/base/diagonal/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/base/diagonal/docs/repl.txt index 3f46279bb541..da74ae936a20 100644 --- a/lib/node_modules/@stdlib/ndarray/base/diagonal/docs/repl.txt +++ b/lib/node_modules/@stdlib/ndarray/base/diagonal/docs/repl.txt @@ -6,8 +6,7 @@ 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]. + 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 From b704fd6825acacc02f1f5b701f0c0cecbe8e323a Mon Sep 17 00:00:00 2001 From: Athan Date: Sun, 3 May 2026 00:18:43 -0700 Subject: [PATCH 11/12] test: clean-up messages and formatting and reorder tests Signed-off-by: Athan --- .../ndarray/base/diagonal/test/test.js | 265 +++++++++--------- 1 file changed, 131 insertions(+), 134 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/base/diagonal/test/test.js b/lib/node_modules/@stdlib/ndarray/base/diagonal/test/test.js index 83b477179eab..97df342ed51c 100644 --- a/lib/node_modules/@stdlib/ndarray/base/diagonal/test/test.js +++ b/lib/node_modules/@stdlib/ndarray/base/diagonal/test/test.js @@ -27,6 +27,7 @@ var ndarray = require( '@stdlib/ndarray/base/ctor' ); var ndarray2array = require( '@stdlib/ndarray/to-array' ); var getShape = require( '@stdlib/ndarray/shape' ); var getStrides = require( '@stdlib/ndarray/strides' ); +var getStride = require( '@stdlib/ndarray/stride' ); var getOffset = require( '@stdlib/ndarray/offset' ); var getData = require( '@stdlib/ndarray/data-buffer' ); var getOrder = require( '@stdlib/ndarray/order' ); @@ -42,26 +43,6 @@ tape( 'main export is a function', function test( t ) { t.end(); }); -tape( 'the function returns a view of the main diagonal of a square matrix', function test( t ) { - var expected; - var x; - var y; - - x = array( [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] ); - - y = diagonal( x, [ 0, 1 ], 0, false ); - - expected = [ 3 ]; - t.deepEqual( getShape( y ), expected, 'returns expected shape' ); - - expected = [ 1, 5, 9 ]; - t.deepEqual( ndarray2array( y ), expected, 'returns expected values' ); - - t.strictEqual( getData( y ), getData( x ), 'shares the same data buffer' ); - - t.end(); -}); - tape( 'the function throws an error if not provided exactly two dimension indices', function test( t ) { var values; var x; @@ -161,6 +142,26 @@ tape( 'the function throws an error if provided duplicate dimension indices', fu } }); +tape( 'the function returns a view of the main diagonal of a square matrix', function test( t ) { + var expected; + var x; + var y; + + x = array( [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] ); + + y = diagonal( x, [ 0, 1 ], 0, false ); + + expected = [ 3 ]; + t.deepEqual( getShape( y ), expected, 'returns expected value' ); + + expected = [ 1, 5, 9 ]; + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + t.strictEqual( getData( y ), getData( x ), 'returns expected value' ); + + t.end(); +}); + tape( 'the function returns a view of the super-diagonal of a square matrix', function test( t ) { var expected; var x; @@ -171,18 +172,18 @@ tape( 'the function returns a view of the super-diagonal of a square matrix', fu y = diagonal( x, [ 0, 1 ], 1, false ); expected = [ 2 ]; - t.deepEqual( getShape( y ), expected, 'returns expected shape' ); + t.deepEqual( getShape( y ), expected, 'returns expected value' ); expected = [ 2, 6 ]; - t.deepEqual( ndarray2array( y ), expected, 'returns expected values' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); y = diagonal( x, [ 0, 1 ], 2, false ); expected = [ 1 ]; - t.deepEqual( getShape( y ), expected, 'returns expected shape' ); + t.deepEqual( getShape( y ), expected, 'returns expected value' ); expected = [ 3 ]; - t.deepEqual( ndarray2array( y ), expected, 'returns expected values' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); t.end(); }); @@ -197,18 +198,18 @@ tape( 'the function returns a view of the sub-diagonal of a square matrix', func y = diagonal( x, [ 0, 1 ], -1, false ); expected = [ 2 ]; - t.deepEqual( getShape( y ), expected, 'returns expected shape' ); + t.deepEqual( getShape( y ), expected, 'returns expected value' ); expected = [ 4, 8 ]; - t.deepEqual( ndarray2array( y ), expected, 'returns expected values' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); y = diagonal( x, [ 0, 1 ], -2, false ); expected = [ 1 ]; - t.deepEqual( getShape( y ), expected, 'returns expected shape' ); + t.deepEqual( getShape( y ), expected, 'returns expected value' ); expected = [ 7 ]; - t.deepEqual( ndarray2array( y ), expected, 'returns expected values' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); t.end(); }); @@ -223,26 +224,26 @@ tape( 'the function returns a view of the diagonal of a non-square matrix (M < N y = diagonal( x, [ 0, 1 ], 0, false ); expected = [ 2 ]; - t.deepEqual( getShape( y ), expected, 'returns expected shape' ); + t.deepEqual( getShape( y ), expected, 'returns expected value' ); expected = [ 1, 6 ]; - t.deepEqual( ndarray2array( y ), expected, 'returns expected values' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); y = diagonal( x, [ 0, 1 ], 2, false ); expected = [ 2 ]; - t.deepEqual( getShape( y ), expected, 'returns expected shape' ); + t.deepEqual( getShape( y ), expected, 'returns expected value' ); expected = [ 3, 8 ]; - t.deepEqual( ndarray2array( y ), expected, 'returns expected values' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); y = diagonal( x, [ 0, 1 ], -1, false ); expected = [ 1 ]; - t.deepEqual( getShape( y ), expected, 'returns expected shape' ); + t.deepEqual( getShape( y ), expected, 'returns expected value' ); expected = [ 5 ]; - t.deepEqual( ndarray2array( y ), expected, 'returns expected values' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); t.end(); }); @@ -257,26 +258,26 @@ tape( 'the function returns a view of the diagonal of a non-square matrix (M > N y = diagonal( x, [ 0, 1 ], 0, false ); expected = [ 2 ]; - t.deepEqual( getShape( y ), expected, 'returns expected shape' ); + t.deepEqual( getShape( y ), expected, 'returns expected value' ); expected = [ 1, 4 ]; - t.deepEqual( ndarray2array( y ), expected, 'returns expected values' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); y = diagonal( x, [ 0, 1 ], -2, false ); expected = [ 2 ]; - t.deepEqual( getShape( y ), expected, 'returns expected shape' ); + t.deepEqual( getShape( y ), expected, 'returns expected value' ); expected = [ 5, 8 ]; - t.deepEqual( ndarray2array( y ), expected, 'returns expected values' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); y = diagonal( x, [ 0, 1 ], 1, false ); expected = [ 1 ]; - t.deepEqual( getShape( y ), expected, 'returns expected shape' ); + t.deepEqual( getShape( y ), expected, 'returns expected value' ); expected = [ 2 ]; - t.deepEqual( ndarray2array( y ), expected, 'returns expected values' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); t.end(); }); @@ -291,26 +292,26 @@ tape( 'the function returns an empty view when the diagonal offset is out-of-bou y = diagonal( x, [ 0, 1 ], 3, false ); expected = [ 0 ]; - t.deepEqual( getShape( y ), expected, 'returns expected shape' ); + t.deepEqual( getShape( y ), expected, 'returns expected value' ); expected = []; - t.deepEqual( ndarray2array( y ), expected, 'returns expected values' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); y = diagonal( x, [ 0, 1 ], -3, false ); expected = [ 0 ]; - t.deepEqual( getShape( y ), expected, 'returns expected shape' ); + t.deepEqual( getShape( y ), expected, 'returns expected value' ); expected = []; - t.deepEqual( ndarray2array( y ), expected, 'returns expected values' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); y = diagonal( x, [ 0, 1 ], 100, false ); expected = [ 0 ]; - t.deepEqual( getShape( y ), expected, 'returns expected shape' ); + t.deepEqual( getShape( y ), expected, 'returns expected value' ); expected = []; - t.deepEqual( ndarray2array( y ), expected, 'returns expected values' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); t.end(); }); @@ -325,18 +326,18 @@ tape( 'the function swaps the row-like and column-like dimensions when `dims` is y = diagonal( x, [ 1, 0 ], 1, false ); expected = [ 2 ]; - t.deepEqual( getShape( y ), expected, 'returns expected shape' ); + t.deepEqual( getShape( y ), expected, 'returns expected value' ); expected = [ 4, 8 ]; - t.deepEqual( ndarray2array( y ), expected, 'returns expected values' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); y = diagonal( x, [ 1, 0 ], -1, false ); expected = [ 2 ]; - t.deepEqual( getShape( y ), expected, 'returns expected shape' ); + t.deepEqual( getShape( y ), expected, 'returns expected value' ); expected = [ 2, 6 ]; - t.deepEqual( ndarray2array( y ), expected, 'returns expected values' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); t.end(); }); @@ -351,26 +352,26 @@ tape( 'the function returns a view of the diagonals of a stack of matrices', fun y = diagonal( x, [ 1, 2 ], 0, false ); expected = [ 2, 2 ]; - t.deepEqual( getShape( y ), expected, 'returns expected shape' ); + t.deepEqual( getShape( y ), expected, 'returns expected value' ); expected = [ [ 1, 4 ], [ 5, 8 ] ]; - t.deepEqual( ndarray2array( y ), expected, 'returns expected values' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); y = diagonal( x, [ 1, 2 ], 1, false ); expected = [ 2, 1 ]; - t.deepEqual( getShape( y ), expected, 'returns expected shape' ); + t.deepEqual( getShape( y ), expected, 'returns expected value' ); expected = [ [ 2 ], [ 6 ] ]; - t.deepEqual( ndarray2array( y ), expected, 'returns expected values' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); y = diagonal( x, [ 1, 2 ], -1, false ); expected = [ 2, 1 ]; - t.deepEqual( getShape( y ), expected, 'returns expected shape' ); + t.deepEqual( getShape( y ), expected, 'returns expected value' ); expected = [ [ 3 ], [ 7 ] ]; - t.deepEqual( ndarray2array( y ), expected, 'returns expected values' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); t.end(); }); @@ -385,10 +386,10 @@ tape( 'the function appends the diagonal dimension as the trailing dimension of y = diagonal( x, [ 0, 2 ], 0, false ); expected = [ 2, 2 ]; - t.deepEqual( getShape( y ), expected, 'returns expected shape' ); + t.deepEqual( getShape( y ), expected, 'returns expected value' ); expected = [ [ 1, 8 ], [ 4, 11 ] ]; - t.deepEqual( ndarray2array( y ), expected, 'returns expected values' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); t.end(); }); @@ -403,10 +404,10 @@ tape( 'the function normalizes negative dimension indices', function test( t ) { y = diagonal( x, [ -2, -1 ], 0, false ); expected = [ 3 ]; - t.deepEqual( getShape( y ), expected, 'returns expected shape' ); + t.deepEqual( getShape( y ), expected, 'returns expected value' ); expected = [ 1, 5, 9 ]; - t.deepEqual( ndarray2array( y ), expected, 'returns expected values' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); t.end(); }); @@ -421,45 +422,38 @@ tape( 'the function returns a column-major ndarray when the input ndarray is col y = diagonal( x, [ 0, 1 ], 0, false ); expected = 'column-major'; - t.strictEqual( getOrder( y ), expected, 'returns expected order' ); + t.strictEqual( getOrder( y ), expected, 'returns expected value' ); expected = [ 3 ]; - t.deepEqual( getShape( y ), expected, 'returns expected shape' ); + t.deepEqual( getShape( y ), expected, 'returns expected value' ); expected = [ 1, 5, 9 ]; - t.deepEqual( ndarray2array( y ), expected, 'returns expected values' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); t.end(); }); tape( 'the function returns a writable ndarray when `writable` is `true`', function test( t ) { - var expected; var x; var y; x = array( [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] ); - y = diagonal( x, [ 0, 1 ], 0, true ); - expected = false; - t.strictEqual( isReadOnly( y ), expected, 'returns a writable ndarray' ); - - t.strictEqual( getData( y ), getData( x ), 'shares the same data buffer' ); + t.strictEqual( isReadOnly( y ), false, 'returns expected value' ); + t.strictEqual( getData( y ), getData( x ), 'returns expected value' ); t.end(); }); tape( 'the function returns a read-only ndarray when `writable` is `false`', function test( t ) { - var expected; var x; var y; x = array( [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] ); - y = diagonal( x, [ 0, 1 ], 0, false ); - expected = true; - t.strictEqual( isReadOnly( y ), expected, 'returns a read-only ndarray' ); + t.strictEqual( isReadOnly( y ), true, 'returns expected value' ); t.end(); }); @@ -476,13 +470,13 @@ tape( 'the function constructs the output by retaining all dimensions except the y = diagonal( x, [ 1, 2 ], 0, false ); expected = [ 3, 2 ]; - t.deepEqual( getShape( y ), expected, 'returns expected shape' ); + t.deepEqual( getShape( y ), expected, 'returns expected value' ); expected = [ 4, 3 ]; - t.deepEqual( getStrides( y ), expected, 'returns expected strides' ); + t.deepEqual( getStrides( y ), expected, 'returns expected value' ); expected = getOffset( x ); - t.strictEqual( getOffset( y ), expected, 'returns expected offset' ); + t.strictEqual( getOffset( y ), expected, 'returns expected value' ); t.end(); }); @@ -496,13 +490,13 @@ tape( 'the function adjusts the offset to point to the first element of the requ y = diagonal( x, [ 0, 1 ], 1, false ); - expected = getOffset( x ) + getStrides( x )[1]; - t.strictEqual( getOffset( y ), expected, 'returns expected offset' ); + expected = getOffset( x ) + getStride( x, 1 ); + t.strictEqual( getOffset( y ), expected, 'returns expected value' ); y = diagonal( x, [ 0, 1 ], -1, false ); - expected = getOffset( x ) + getStrides( x )[0]; - t.strictEqual( getOffset( y ), expected, 'returns expected offset' ); + expected = getOffset( x ) + getStride( x, 0 ); + t.strictEqual( getOffset( y ), expected, 'returns expected value' ); t.end(); }); @@ -517,88 +511,91 @@ tape( 'the function supports input ndarrays having a non-zero buffer offset', fu y = diagonal( x, [ 0, 1 ], 0, false ); expected = [ 3 ]; - t.deepEqual( getShape( y ), expected, 'returns expected shape' ); + t.deepEqual( getShape( y ), expected, 'returns expected value' ); expected = [ 1, 5, 9 ]; - t.deepEqual( ndarray2array( y ), expected, 'returns expected values' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); - t.strictEqual( getData( y ), getData( x ), 'shares the same data buffer' ); + t.strictEqual( getData( y ), getData( x ), 'returns expected value' ); y = diagonal( x, [ 0, 1 ], 1, false ); expected = [ 2, 6 ]; - t.deepEqual( ndarray2array( y ), expected, 'returns expected values' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); y = diagonal( x, [ 0, 1 ], -1, false ); expected = [ 4, 8 ]; - t.deepEqual( ndarray2array( y ), expected, 'returns expected values' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); t.end(); }); tape( 'the function supports 4D inputs with non-trailing `dims`', function test( t ) { var expected; + var arr; var x; var y; - x = array([ - 0, - 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 - ], { - 'shape': [ 2, 3, 3, 2 ] - }); + arr = [ + [ + [ + [ 0, 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 ] + ] + ] + ]; + x = array( arr ); y = diagonal( x, [ 1, 2 ], 0, false ); expected = [ 2, 2, 3 ]; - t.deepEqual( getShape( y ), expected, 'returns expected shape' ); + t.deepEqual( getShape( y ), expected, 'returns expected value' ); expected = [ - [ [ 0, 8, 16 ], [ 1, 9, 17 ] ], - [ [ 18, 26, 34 ], [ 19, 27, 35 ] ] + [ + [ 0, 8, 16 ], + [ 1, 9, 17 ] + ], + [ + [ 18, 26, 34 ], + [ 19, 27, 35 ] + ] ]; - t.deepEqual( ndarray2array( y ), expected, 'returns expected values' ); - t.strictEqual( getData( y ), getData( x ), 'shares the same data buffer' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + t.strictEqual( getData( y ), getData( x ), 'returns expected value' ); t.end(); }); -tape( 'the function supports negative dimension indices combined with a non-zero offset `k`', function test( t ) { +tape( 'the function supports negative dimension indices combined with a non-zero `k`', function test( t ) { var expected; var x; var y; @@ -607,15 +604,15 @@ tape( 'the function supports negative dimension indices combined with a non-zero y = diagonal( x, [ -2, -1 ], 1, false ); expected = [ 2 ]; - t.deepEqual( getShape( y ), expected, 'returns expected shape' ); + t.deepEqual( getShape( y ), expected, 'returns expected value' ); expected = [ 2, 6 ]; - t.deepEqual( ndarray2array( y ), expected, 'returns expected values' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); y = diagonal( x, [ -2, -1 ], -1, false ); expected = [ 4, 8 ]; - t.deepEqual( ndarray2array( y ), expected, 'returns expected values' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); t.end(); }); From 0be71381418fdb888a0ef14cb39eb5b1889ed235 Mon Sep 17 00:00:00 2001 From: Athan Date: Sun, 3 May 2026 00:21:09 -0700 Subject: [PATCH 12/12] Apply suggestions from code review Co-authored-by: Athan Signed-off-by: Athan --- lib/node_modules/@stdlib/ndarray/base/diagonal/test/test.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/node_modules/@stdlib/ndarray/base/diagonal/test/test.js b/lib/node_modules/@stdlib/ndarray/base/diagonal/test/test.js index 97df342ed51c..bc05c4633aed 100644 --- a/lib/node_modules/@stdlib/ndarray/base/diagonal/test/test.js +++ b/lib/node_modules/@stdlib/ndarray/base/diagonal/test/test.js @@ -566,6 +566,7 @@ tape( 'the function supports 4D inputs with non-trailing `dims`', function test( [ 26, 27 ], [ 28, 29 ] ], + [ [ 30, 31 ], [ 32, 33 ], [ 34, 35 ]