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..907883006d4e --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/diagonal/README.md @@ -0,0 +1,164 @@ + + +# 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 from 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( ndarray2array( x ) ); + +// Extract the main diagonals from the stack: +var y = diagonal( x, [ 1, 2 ], 0, false ); +console.log( ndarray2array( y ) ); + +// Extract super-diagonals from the stack: +y = diagonal( x, [ 1, 2 ], 1, false ); +console.log( ndarray2array( y ) ); + +// 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/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/base/diagonal/benchmark/benchmark.js new file mode 100644 index 000000000000..7eafaa6c0539 --- /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::ndims=2,ctor=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::ndims=2,ctor=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::ndims=3,ctor=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::ndims=3,ctor=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::ndims=4,ctor=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::ndims=4,ctor=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::ndims=5,ctor=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::ndims=5,ctor=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..da74ae936a20 --- /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 from 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..2232ced6dfcf --- /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 from 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..e8983375b6e5 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/diagonal/examples/index.js @@ -0,0 +1,41 @@ +/** +* @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( ndarray2array( x ) ); + +// Extract the main diagonals from the stack: +var y = diagonal( x, [ 1, 2 ], 0, false ); +console.log( ndarray2array( y ) ); + +// Extract super-diagonals from the stack: +y = diagonal( x, [ 1, 2 ], 1, false ); +console.log( ndarray2array( y ) ); + +// 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/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..fdcd57efcbf8 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/diagonal/lib/main.js @@ -0,0 +1,144 @@ +/** +* @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 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 // + +/** +* 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 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 +* 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 ndims; + var shape; + var rows; + var cols; + var sh; + var st; + var sr; + var sc; + var ro; + var co; + var d; + 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. First argument must be an ndarray having at least two dimensions. Number of dimensions: %d.', ndims ) ); + } + st = getStrides( x ); + + // 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 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: + 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 ); + if ( L > 0 ) { + offset += ( co*sc ) + ( ro*sr ); + } + + // Drop the specified dimensions and append the diagonal dimension: + shape = []; + strides = []; + for ( i = 0; i < ndims; 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..bc05c4633aed --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/diagonal/test/test.js @@ -0,0 +1,619 @@ +/** +* @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 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 getStride = require( '@stdlib/ndarray/stride' ); +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 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 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; + 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 value' ); + + expected = [ 2, 6 ]; + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + y = diagonal( x, [ 0, 1 ], 2, false ); + + expected = [ 1 ]; + t.deepEqual( getShape( y ), expected, 'returns expected value' ); + + expected = [ 3 ]; + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + 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 value' ); + + expected = [ 4, 8 ]; + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + y = diagonal( x, [ 0, 1 ], -2, false ); + + expected = [ 1 ]; + t.deepEqual( getShape( y ), expected, 'returns expected value' ); + + expected = [ 7 ]; + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + 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 value' ); + + expected = [ 1, 6 ]; + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + y = diagonal( x, [ 0, 1 ], 2, false ); + + expected = [ 2 ]; + t.deepEqual( getShape( y ), expected, 'returns expected value' ); + + expected = [ 3, 8 ]; + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + y = diagonal( x, [ 0, 1 ], -1, false ); + + expected = [ 1 ]; + t.deepEqual( getShape( y ), expected, 'returns expected value' ); + + expected = [ 5 ]; + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + 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 value' ); + + expected = [ 1, 4 ]; + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + y = diagonal( x, [ 0, 1 ], -2, false ); + + expected = [ 2 ]; + t.deepEqual( getShape( y ), expected, 'returns expected value' ); + + expected = [ 5, 8 ]; + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + y = diagonal( x, [ 0, 1 ], 1, false ); + + expected = [ 1 ]; + t.deepEqual( getShape( y ), expected, 'returns expected value' ); + + expected = [ 2 ]; + t.deepEqual( ndarray2array( y ), 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 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 value' ); + + expected = []; + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + y = diagonal( x, [ 0, 1 ], -3, false ); + + expected = [ 0 ]; + t.deepEqual( getShape( y ), expected, 'returns expected value' ); + + expected = []; + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + y = diagonal( x, [ 0, 1 ], 100, false ); + + expected = [ 0 ]; + t.deepEqual( getShape( y ), expected, 'returns expected value' ); + + expected = []; + t.deepEqual( ndarray2array( y ), 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 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 value' ); + + expected = [ 4, 8 ]; + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + y = diagonal( x, [ 1, 0 ], -1, false ); + + expected = [ 2 ]; + t.deepEqual( getShape( y ), expected, 'returns expected value' ); + + expected = [ 2, 6 ]; + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + 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 value' ); + + expected = [ [ 1, 4 ], [ 5, 8 ] ]; + 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 value' ); + + expected = [ [ 2 ], [ 6 ] ]; + 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 value' ); + + expected = [ [ 3 ], [ 7 ] ]; + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + 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 value' ); + + expected = [ [ 1, 8 ], [ 4, 11 ] ]; + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + 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 value' ); + + expected = [ 1, 5, 9 ]; + t.deepEqual( ndarray2array( y ), 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 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 value' ); + + expected = [ 3 ]; + t.deepEqual( getShape( y ), expected, 'returns expected value' ); + + expected = [ 1, 5, 9 ]; + 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 x; + var y; + + x = array( [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] ); + y = diagonal( x, [ 0, 1 ], 0, true ); + + 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 x; + var y; + + x = array( [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] ); + y = diagonal( x, [ 0, 1 ], 0, false ); + + t.strictEqual( isReadOnly( y ), true, 'returns expected value' ); + + 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 value' ); + + expected = [ 4, 3 ]; + t.deepEqual( getStrides( y ), expected, 'returns expected value' ); + + expected = getOffset( x ); + t.strictEqual( getOffset( y ), expected, 'returns expected value' ); + + 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 ) + getStride( x, 1 ); + t.strictEqual( getOffset( y ), expected, 'returns expected value' ); + + y = diagonal( x, [ 0, 1 ], -1, false ); + + expected = getOffset( x ) + getStride( x, 0 ); + t.strictEqual( getOffset( y ), expected, 'returns expected value' ); + + 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 value' ); + + expected = [ 1, 5, 9 ]; + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + 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 value' ); + + y = diagonal( x, [ 0, 1 ], -1, false ); + + expected = [ 4, 8 ]; + 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; + + 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 value' ); + + expected = [ + [ + [ 0, 8, 16 ], + [ 1, 9, 17 ] + ], + [ + [ 18, 26, 34 ], + [ 19, 27, 35 ] + ] + ]; + + 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 `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 value' ); + + expected = [ 2, 6 ]; + 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 value' ); + + t.end(); +});