diff --git a/lib/node_modules/@stdlib/ndarray/diagonal/README.md b/lib/node_modules/@stdlib/ndarray/diagonal/README.md new file mode 100644 index 000000000000..1497cb977139 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/diagonal/README.md @@ -0,0 +1,182 @@ + + +# diagonal + +> Return a **read-only** 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/diagonal' ); +``` + +#### diagonal( x\[, options] ) + +Returns a read-only view of the diagonal of a matrix (or stack of matrices). + +```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 ); +// returns [ 1.0, 5.0, 9.0 ] +``` + +The function accepts the following arguments: + +- **x**: input [`ndarray`][@stdlib/ndarray/ctor]. +- **options**: function options. + +The function accepts the following options: + +- **k**: diagonal offset. The diagonal offset 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. Default: `0`. +- **dims**: dimension indices defining the plane from which to extract the diagonal. Must contain exactly two unique dimension indices. The first element specifies the row-like dimension. The second element specifies the column-like dimension. If a dimension index is provided as an integer less than zero, the dimension index is resolved relative to the last dimension, with the last dimension corresponding to the value `-1`. Default: `[-2, -1]`. + +
+ + + + + +
+ +## Notes + +- The order of the dimension indices contained in `options.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]`. + +
+ + + + + +
+ +## Examples + + + +```javascript +var uniform = require( '@stdlib/random/uniform' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var diagonal = require( '@stdlib/ndarray/diagonal' ); + +// Create a stack of matrices: +var x = uniform( [ 2, 3, 3 ], -10.0, 10.0 ); +console.log( ndarray2array( x ) ); + +// Extract the main diagonals from the stack: +var y = diagonal( x ); +console.log( ndarray2array( y ) ); + +// Extract super-diagonals from the stack: +y = diagonal( x, { + 'k': 1 +}); +console.log( ndarray2array( y ) ); + +// Extract sub-diagonals from the stack: +y = diagonal( x, { + 'k': -1 +}); +console.log( ndarray2array( y ) ); +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/ndarray/diagonal/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/diagonal/benchmark/benchmark.js new file mode 100644 index 000000000000..a98591399199 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/diagonal/benchmark/benchmark.js @@ -0,0 +1,196 @@ +/** +* @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 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:ndims=2', 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 ] ); + 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:ndims=3', 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 ] ); + 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:ndims=3,dims=[0,2]', pkg ), function benchmark( b ) { + var values; + var opts; + 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 */ + + opts = { + 'dims': [ 0, 2 ] + }; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = diagonal( values[ i%values.length ], opts ); + 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:ndims=4', 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 ] ); + 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:ndims=5', 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 ] ); + 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/diagonal/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/diagonal/docs/repl.txt new file mode 100644 index 000000000000..cec5eec794ce --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/diagonal/docs/repl.txt @@ -0,0 +1,50 @@ + +{{alias}}( x[, options] ) + Returns a read-only view of the diagonal of a matrix (or stack of + matrices). + + The order of the dimension indices contained in `options.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]. + + Parameters + ---------- + x: ndarray + Input array. + + options: Object (optional) + Function options. + + options.k: integer (optional) + Diagonal offset. The diagonal offset is interpreted as `column - row`. + Accordingly, when equal to zero, the function returns the main diagonal. + When greater than zero, the function returns the diagonal above the main + diagonal. When less than zero, the function returns the diagonal below + the main diagonal. Default: 0. + + options.dims: ArrayLikeObject (optional) + Dimension indices defining the plane from which to extract the + diagonal. The first element specifies the row-like dimension. The + second element specifies the column-like dimension. Must contain + exactly two unique dimension indices. If less than zero, an index is + resolved relative to the last dimension, with the last dimension + corresponding to the value `-1`. Default: [ -2, -1 ]. + + Returns + ------- + out: ndarray + Output array view. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/array}}( [ [ 1, 2 ], [ 3, 4 ] ] ) + [ [ 1, 2 ], [ 3, 4 ] ] + > var y = {{alias}}( x ) + [ 1, 4 ] + > y = {{alias}}( x, { 'k': 1 } ) + [ 2 ] + + See Also + -------- diff --git a/lib/node_modules/@stdlib/ndarray/diagonal/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/diagonal/docs/types/index.d.ts new file mode 100644 index 000000000000..0bfbcf915d56 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/diagonal/docs/types/index.d.ts @@ -0,0 +1,70 @@ +/* +* @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 { ndarray } from '@stdlib/types/ndarray'; +import { Collection } from '@stdlib/types/array'; + +/** +* Interface defining function options. +*/ +interface Options { + /** + * Diagonal offset (default: 0). + */ + k?: number; + + /** + * Dimension indices defining the plane from which to extract the diagonal (default: [-2, -1]). + */ + dims?: Collection; +} + +/** +* Returns a read-only view of the diagonal of a matrix (or stack of matrices). +* +* ## Notes +* +* - The order of the dimension indices contained in `options.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 `options.k` is interpreted as `column - row`. Accordingly, when `options.k = 0`, the function returns the main diagonal; when `options.k > 0`, the function returns the diagonal above the main diagonal; and when `options.k < 0`, the function returns the diagonal below the main diagonal. +* +* @param x - input array +* @param options - function options +* @param options.k - diagonal offset (default: 0) +* @param options.dims - dimension indices defining the plane from which to extract the diagonal (default: [-2, -1]) +* @returns output array +* +* @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 ); +* // returns [ 1.0, 5.0, 9.0 ] +*/ +declare function diagonal( x: T, options?: Options ): T; + + +// EXPORTS // + +export = diagonal; diff --git a/lib/node_modules/@stdlib/ndarray/diagonal/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/diagonal/docs/types/test.ts new file mode 100644 index 000000000000..d7671d4cbc9f --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/diagonal/docs/types/test.ts @@ -0,0 +1,93 @@ +/* +* @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. +*/ + +/* eslint-disable space-in-parens */ + +import zeros = require( '@stdlib/ndarray/zeros' ); +import diagonal = require( './index' ); + + +// TESTS // + +// The function returns an ndarray... +{ + diagonal( zeros( [ 2, 2 ] ) ); // $ExpectType float64ndarray + diagonal( zeros( [ 2, 2 ] ), {} ); // $ExpectType float64ndarray + diagonal( zeros( [ 2, 2 ] ), { 'k': 1 } ); // $ExpectType float64ndarray + diagonal( zeros( [ 2, 2 ] ), { 'dims': [ 0, 1 ] } ); // $ExpectType float64ndarray + diagonal( zeros( [ 2, 2 ] ), { 'k': 1, 'dims': [ 0, 1 ] } ); // $ExpectType float64ndarray +} + +// The compiler throws an error if the function is provided a first argument which is not an ndarray... +{ + diagonal( '10' ); // $ExpectError + diagonal( 10 ); // $ExpectError + diagonal( false ); // $ExpectError + diagonal( true ); // $ExpectError + diagonal( null ); // $ExpectError + diagonal( void 0 ); // $ExpectError + diagonal( [] ); // $ExpectError + diagonal( {} ); // $ExpectError + diagonal( ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not an object... +{ + const x = zeros( [ 2, 2 ] ); + + diagonal( x, '10' ); // $ExpectError + diagonal( x, 10 ); // $ExpectError + diagonal( x, false ); // $ExpectError + diagonal( x, true ); // $ExpectError + diagonal( x, null ); // $ExpectError + diagonal( x, [] ); // $ExpectError + diagonal( x, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided a `k` option which is not a number... +{ + const x = zeros( [ 2, 2 ] ); + + diagonal( x, { 'k': '10' } ); // $ExpectError + diagonal( x, { 'k': false } ); // $ExpectError + diagonal( x, { 'k': true } ); // $ExpectError + diagonal( x, { 'k': null } ); // $ExpectError + diagonal( x, { 'k': [] } ); // $ExpectError + diagonal( x, { 'k': {} } ); // $ExpectError + diagonal( x, { 'k': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the function is provided a `dims` option which is not a collection of numbers... +{ + const x = zeros( [ 2, 2 ] ); + + diagonal( x, { 'dims': '10' } ); // $ExpectError + diagonal( x, { 'dims': 10 } ); // $ExpectError + diagonal( x, { 'dims': false } ); // $ExpectError + diagonal( x, { 'dims': true } ); // $ExpectError + diagonal( x, { 'dims': null } ); // $ExpectError + diagonal( x, { 'dims': {} } ); // $ExpectError + diagonal( x, { 'dims': [ '0', '1' ] } ); // $ExpectError + diagonal( x, { 'dims': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + diagonal(); // $ExpectError + diagonal( zeros( [ 2, 2 ] ), {}, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/ndarray/diagonal/examples/index.js b/lib/node_modules/@stdlib/ndarray/diagonal/examples/index.js new file mode 100644 index 000000000000..9dda62a7d338 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/diagonal/examples/index.js @@ -0,0 +1,43 @@ +/** +* @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 ); +console.log( ndarray2array( x ) ); + +// Extract the main diagonals from the stack: +var y = diagonal( x ); +console.log( ndarray2array( y ) ); + +// Extract super-diagonals from the stack: +y = diagonal( x, { + 'k': 1 +}); +console.log( ndarray2array( y ) ); + +// Extract sub-diagonals from the stack: +y = diagonal( x, { + 'k': -1 +}); +console.log( ndarray2array( y ) ); diff --git a/lib/node_modules/@stdlib/ndarray/diagonal/lib/index.js b/lib/node_modules/@stdlib/ndarray/diagonal/lib/index.js new file mode 100644 index 000000000000..c6453b346908 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/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 read-only view of the diagonal of a matrix (or stack of matrices). +* +* @module @stdlib/ndarray/diagonal +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* var diagonal = require( '@stdlib/ndarray/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 ); +* // 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/diagonal/lib/main.js b/lib/node_modules/@stdlib/ndarray/diagonal/lib/main.js new file mode 100644 index 000000000000..b978f08b56d3 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/diagonal/lib/main.js @@ -0,0 +1,98 @@ +/** +* @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 isIntegerArray = require( '@stdlib/assert/is-integer-array' ).primitives; +var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive; +var isPlainObject = require( '@stdlib/assert/is-plain-object' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var base = require( '@stdlib/ndarray/base/diagonal' ); +var format = require( '@stdlib/string/format' ); + + +// MAIN // + +/** +* Returns a read-only view of the diagonal of a matrix (or stack of matrices). +* +* ## Notes +* +* - The order of the dimension indices contained in `options.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 `options.k` is interpreted as `column - row`. Accordingly, when `options.k = 0`, the function returns the main diagonal; when `options.k > 0`, the function returns the diagonal above the main diagonal; and when `options.k < 0`, the function returns the diagonal below the main diagonal. +* +* @param {ndarray} x - input array +* @param {Options} [options] - function options +* @param {integer} [options.k=0] - diagonal offset +* @param {IntegerArray} [options.dims=[-2,-1]] - dimension indices defining the plane from which to extract the diagonal +* @throws {TypeError} first argument must be an ndarray +* @throws {TypeError} options argument must be an object +* @throws {TypeError} `k` option must be an integer +* @throws {TypeError} `dims` option must be an array of integers +* @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 +* 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 ); +* // returns [ 1.0, 5.0, 9.0 ] +*/ +function diagonal( x, options ) { + var dims; + var k; + + if ( !isndarrayLike( x ) ) { + throw new TypeError( format( 'invalid argument. First argument must be an ndarray. Value: `%s`.', x ) ); + } + dims = [ -2, -1 ]; + k = 0; + if ( arguments.length > 1 ) { + if ( !isPlainObject( options ) ) { + throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + } + if ( hasOwnProp( options, 'k' ) ) { + if ( !isInteger( options.k ) ) { + throw new TypeError( format( 'invalid option. `%s` option must be an integer. Option: `%s`.', 'k', options.k ) ); + } + k = options.k; + } + if ( hasOwnProp( options, 'dims' ) ) { + if ( !isIntegerArray( options.dims ) ) { + throw new TypeError( format( 'invalid option. `%s` option must be an array of integers. Option: `%s`.', 'dims', options.dims ) ); + } + dims = options.dims; + } + } + return base( x, dims, k, false ); +} + + +// EXPORTS // + +module.exports = diagonal; diff --git a/lib/node_modules/@stdlib/ndarray/diagonal/package.json b/lib/node_modules/@stdlib/ndarray/diagonal/package.json new file mode 100644 index 000000000000..4a52d2b2dc40 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/diagonal/package.json @@ -0,0 +1,63 @@ +{ + "name": "@stdlib/ndarray/diagonal", + "version": "0.0.0", + "description": "Return a read-only 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", + "data", + "structure", + "ndarray", + "diagonal", + "matrix", + "stack", + "view" + ] +} diff --git a/lib/node_modules/@stdlib/ndarray/diagonal/test/test.js b/lib/node_modules/@stdlib/ndarray/diagonal/test/test.js new file mode 100644 index 000000000000..59de2800faaa --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/diagonal/test/test.js @@ -0,0 +1,656 @@ +/** +* @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 Float64Array = require( '@stdlib/array/float64' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var isReadOnly = require( '@stdlib/ndarray/base/assert/is-read-only' ); +var isEqualDataType = require( '@stdlib/ndarray/base/assert/is-equal-data-type' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var array = require( '@stdlib/ndarray/array' ); +var zeroTo = require( '@stdlib/blas/ext/zero-to' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var getData = require( '@stdlib/ndarray/data-buffer' ); +var getDType = require( '@stdlib/ndarray/dtype' ); +var getOrder = require( '@stdlib/ndarray/order' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +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 throws an error if provided a first argument which is not an ndarray', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + diagonal( value ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not an object', function test( t ) { + var values; + var x; + var i; + + x = zeroTo( [ 2, 2 ] ); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + diagonal( x, value ); + }; + } +}); + +tape( 'the function throws an error if provided a `k` option which is not an integer', function test( t ) { + var values; + var x; + var i; + + x = zeroTo( [ 2, 2 ] ); + + values = [ + '5', + 3.14, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + diagonal( x, { + 'k': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which is not an array-like object', function test( t ) { + var values; + var x; + var i; + + x = zeroTo( [ 2, 2 ] ); + + values = [ + 5, + 3.14, + NaN, + true, + false, + null, + void 0, + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + diagonal( x, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which is not an array of integers', function test( t ) { + var values; + var x; + var i; + + x = zeroTo( [ 2, 2 ] ); + + values = [ + [ '0', '1' ], + [ 0.5, 1.5 ], + [ NaN, NaN ], + [ null, null ], + [ true, false ], + [ {}, {} ] + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided [' + values[ i ] + ']' ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + diagonal( x, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which does not contain exactly two dimension indices', function test( t ) { + var values; + var x; + var i; + + x = zeroTo( [ 2, 2 ] ); + + values = [ + [ 0 ], + [ 0, 1, 0 ] + ]; + + 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( value ) { + return function badValue() { + diagonal( x, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option having out-of-bounds dimension indices', function test( t ) { + var values; + var x; + var i; + + x = zeroTo( [ 2, 2 ] ); + + values = [ + [ 0, 2 ], + [ 2, 0 ], + [ -3, 0 ], + [ 0, -3 ] + ]; + + 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( value ) { + return function badValue() { + diagonal( x, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option having duplicate dimension indices', function test( t ) { + var values; + var x; + var i; + + x = zeroTo( [ 2, 2 ] ); + + values = [ + [ 0, 0 ], + [ 1, 1 ], + [ 0, -2 ], + [ -1, 1 ] + ]; + + 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( value ) { + return function badValue() { + diagonal( x, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided an input ndarray having fewer than two dimensions', function test( t ) { + var values; + var i; + + values = [ + scalar2ndarray( 5.0 ), + array( [ 1.0, 2.0, 3.0 ] ) + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided an ndarray having ' + values[ i ].ndims + ' dimensions' ); + } + t.end(); + + function badValue( x ) { + return function badValue() { + diagonal( x ); + }; + } +}); + +tape( 'the function returns a read-only view of the main diagonal of a square matrix', function test( t ) { + var expected; + var actual; + var x; + + x = zeroTo( [ 3, 3 ], { + 'dims': [ 0, 1 ] + }); + + actual = diagonal( x ); + expected = [ 0, 4, 8 ]; + + t.notEqual( actual, x, 'returns expected value' ); + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + t.strictEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( actual ), getDType( x ) ), true, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 3 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a read-only view of a super-diagonal (k>0)', function test( t ) { + var expected; + var actual; + var x; + + x = zeroTo( [ 3, 3 ], { + 'dims': [ 0, 1 ] + }); + + actual = diagonal( x, { + 'k': 1 + }); + expected = [ 1, 5 ]; + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + actual = diagonal( x, { + 'k': 2 + }); + expected = [ 2 ]; + t.deepEqual( getShape( actual ), [ 1 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a read-only view of a sub-diagonal (k<0)', function test( t ) { + var expected; + var actual; + var x; + + x = zeroTo( [ 3, 3 ], { + 'dims': [ 0, 1 ] + }); + + actual = diagonal( x, { + 'k': -1 + }); + expected = [ 3, 7 ]; + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + actual = diagonal( x, { + 'k': -2 + }); + expected = [ 6 ]; + t.deepEqual( getShape( actual ), [ 1 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns an empty view when the diagonal offset is out-of-bounds', function test( t ) { + var expected; + var actual; + var x; + + x = zeroTo( [ 3, 3 ], { + 'dims': [ 0, 1 ] + }); + + actual = diagonal( x, { + 'k': 3 + }); + expected = []; + t.deepEqual( getShape( actual ), [ 0 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + actual = diagonal( x, { + 'k': -3 + }); + t.deepEqual( getShape( actual ), [ 0 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a view of a non-square matrix (M < N)', function test( t ) { + var expected; + var actual; + var x; + + x = zeroTo( [ 2, 4 ], { + 'dims': [ 0, 1 ] + }); + + actual = diagonal( x ); + expected = [ 0, 5 ]; + t.deepEqual( getShape( actual ), [ 2 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + actual = diagonal( x, { + 'k': 2 + }); + expected = [ 2, 7 ]; + t.deepEqual( getShape( actual ), [ 2 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a view of a non-square matrix (M > N)', function test( t ) { + var expected; + var actual; + var x; + + x = zeroTo( [ 4, 2 ], { + 'dims': [ 0, 1 ] + }); + + actual = diagonal( x ); + expected = [ 0, 3 ]; + t.deepEqual( getShape( actual ), [ 2 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + actual = diagonal( x, { + 'k': -2 + }); + expected = [ 4, 7 ]; + t.deepEqual( getShape( actual ), [ 2 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports negative dimension indices (default `dims=[-2,-1]`)', function test( t ) { + var expected; + var actual; + var x; + + x = zeroTo( [ 3, 3 ], { + 'dims': [ 0, 1 ] + }); + + actual = diagonal( x, { + 'dims': [ -2, -1 ] + }); + expected = [ 0, 4, 8 ]; + t.deepEqual( getShape( actual ), [ 3 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function swaps the row-like and column-like dimensions when `dims` is reversed', function test( t ) { + var expected; + var actual; + var x; + + x = zeroTo( [ 3, 3 ], { + 'dims': [ 0, 1 ] + }); + + actual = diagonal( x, { + 'dims': [ 1, 0 ], + 'k': 1 + }); + expected = [ 3, 7 ]; + t.deepEqual( getShape( actual ), [ 2 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + actual = diagonal( x, { + 'dims': [ 1, 0 ], + 'k': -1 + }); + expected = [ 1, 5 ]; + t.deepEqual( getShape( actual ), [ 2 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports a stack of matrices (ndims=3)', function test( t ) { + var expected; + var actual; + var x; + + x = zeroTo( [ 2, 2, 2 ], { + 'dims': [ 0, 1, 2 ] + }); + + actual = diagonal( x ); + expected = [ [ 0, 3 ], [ 4, 7 ] ]; + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + actual = diagonal( x, { + 'k': 1 + }); + expected = [ [ 1 ], [ 5 ] ]; + t.deepEqual( getShape( actual ), [ 2, 1 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + actual = diagonal( x, { + 'k': -1 + }); + expected = [ [ 2 ], [ 6 ] ]; + t.deepEqual( getShape( actual ), [ 2, 1 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports diagonals along arbitrary planes (ndims=3)', function test( t ) { + var expected; + var actual; + var x; + + x = zeroTo( [ 2, 2, 3 ], { + 'dims': [ 0, 1, 2 ] + }); + + actual = diagonal( x, { + 'dims': [ 0, 2 ] + }); + expected = [ [ 0, 7 ], [ 3, 10 ] ]; + t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a column-major ndarray when the input ndarray is column-major', function test( t ) { + var expected; + var actual; + var x; + + x = zeroTo( [ 3, 3 ], { + 'dims': [ 0, 1 ], + 'order': 'column-major' + }); + + actual = diagonal( x ); + expected = [ 0, 4, 8 ]; + t.strictEqual( getOrder( actual ), 'column-major', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 3 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports input ndarrays having a non-zero buffer offset', function test( t ) { + var expected; + var actual; + var buf; + var x; + + buf = new Float64Array( [ 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 ] ); + x = new ndarray( 'float64', buf, [ 3, 3 ], [ 3, 1 ], 1, 'row-major' ); + + actual = diagonal( x ); + expected = [ 1.0, 5.0, 9.0 ]; + t.deepEqual( getShape( actual ), [ 3 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + t.strictEqual( getData( actual ), getData( x ), 'returns expected value' ); + + actual = diagonal( x, { + 'k': 1 + }); + expected = [ 2.0, 6.0 ]; + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + actual = diagonal( x, { + 'k': -1 + }); + expected = [ 4.0, 8.0 ]; + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports a stack of non-square matrices (ndims=3)', function test( t ) { + var expected; + var actual; + var x; + + x = zeroTo( [ 2, 2, 4 ], { + 'dims': [ 0, 1, 2 ] + }); + + actual = diagonal( x ); + expected = [ [ 0, 5 ], [ 8, 13 ] ]; + t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + actual = diagonal( x, { + 'k': 2 + }); + expected = [ [ 2, 7 ], [ 10, 15 ] ]; + t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + actual = diagonal( x, { + 'k': -1 + }); + expected = [ [ 4 ], [ 12 ] ]; + t.deepEqual( getShape( actual ), [ 2, 1 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function preserves the input data type (dtype=int32)', function test( t ) { + var expected; + var actual; + var x; + + x = zeroTo( [ 3, 3 ], { + 'dims': [ 0, 1 ], + 'dtype': 'int32' + }); + + actual = diagonal( x ); + expected = [ 0, 4, 8 ]; + + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + t.strictEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( actual ), getDType( x ) ), true, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 3 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +});