From e2df0c7c1980af5bf5b87e940d59c9917ac97a15 Mon Sep 17 00:00:00 2001 From: headlessNode Date: Fri, 8 May 2026 04:25:26 +0500 Subject: [PATCH] feat: add ndarray/base/assign-diagonal --- .../ndarray/base/assign-diagonal/README.md | 158 +++++ .../assign-diagonal/benchmark/benchmark.js | 297 +++++++++ .../base/assign-diagonal/docs/repl.txt | 52 ++ .../assign-diagonal/docs/types/index.d.ts | 63 ++ .../base/assign-diagonal/docs/types/test.ts | 85 +++ .../base/assign-diagonal/examples/index.js | 40 ++ .../ndarray/base/assign-diagonal/lib/index.js | 51 ++ .../ndarray/base/assign-diagonal/lib/main.js | 88 +++ .../ndarray/base/assign-diagonal/package.json | 65 ++ .../ndarray/base/assign-diagonal/test/test.js | 587 ++++++++++++++++++ 10 files changed, 1486 insertions(+) create mode 100644 lib/node_modules/@stdlib/ndarray/base/assign-diagonal/README.md create mode 100644 lib/node_modules/@stdlib/ndarray/base/assign-diagonal/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/assign-diagonal/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/ndarray/base/assign-diagonal/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/ndarray/base/assign-diagonal/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/ndarray/base/assign-diagonal/examples/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/assign-diagonal/lib/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/assign-diagonal/lib/main.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/assign-diagonal/package.json create mode 100644 lib/node_modules/@stdlib/ndarray/base/assign-diagonal/test/test.js diff --git a/lib/node_modules/@stdlib/ndarray/base/assign-diagonal/README.md b/lib/node_modules/@stdlib/ndarray/base/assign-diagonal/README.md new file mode 100644 index 000000000000..0aa4c32050e7 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/assign-diagonal/README.md @@ -0,0 +1,158 @@ + + +# assignDiagonal + +> Assign elements from a broadcasted input [`ndarray`][@stdlib/ndarray/ctor] to a specified diagonal of an output [`ndarray`][@stdlib/ndarray/ctor]. + +
+ +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). + +
+ + + +
+ +## Usage + +```javascript +var assignDiagonal = require( '@stdlib/ndarray/base/assign-diagonal' ); +``` + +#### assignDiagonal( arrays, dims, k ) + +Assigns elements from a broadcasted input [`ndarray`][@stdlib/ndarray/ctor] to a specified diagonal of an output [`ndarray`][@stdlib/ndarray/ctor]. + +```javascript +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var zeros = require( '@stdlib/ndarray/zeros' ); + +var x = scalar2ndarray( 1.0 ); +// returns + +var y = zeros( [ 3, 3 ] ); +// returns [ [ 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0 ] ] + +var out = assignDiagonal( [ x, y ], [ 0, 1 ], 0 ); +// returns [ [ 1.0, 0.0, 0.0 ], [ 0.0, 1.0, 0.0 ], [ 0.0, 0.0, 1.0 ] ] + +var bool = ( out === y ); +// returns true +``` + +The function accepts the following arguments: + +- **arrays**: array-like object containing one input ndarray and one output ndarray. +- **dims**: dimension indices defining the plane in which to assign elements to the diagonal. +- **k**: diagonal offset. + +
+ + + +
+ +## 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 assigns to the main diagonal; when `k > 0`, the function assigns to a diagonal above the main diagonal; and when `k < 0`, the function assigns to a diagonal below the main diagonal. +- The input ndarray must be [broadcast compatible][@stdlib/ndarray/base/broadcast-shapes] with the output ndarray view defined by the specified diagonal. +- The function **mutates** the output ndarray in-place. + +
+ + + +
+ +## Examples + + + +```javascript +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var assignDiagonal = require( '@stdlib/ndarray/base/assign-diagonal' ); + +// Create a stack of matrices: +var y = zeros( [ 2, 3, 3 ] ); +console.log( ndarray2array( y ) ); + +// Assign a scalar to each main diagonal: +assignDiagonal( [ scalar2ndarray( 1.0 ), y ], [ 1, 2 ], 0 ); +console.log( ndarray2array( y ) ); + +// Assign a scalar to each super-diagonal: +assignDiagonal( [ scalar2ndarray( 2.0 ), y ], [ 1, 2 ], 1 ); +console.log( ndarray2array( y ) ); + +// Assign a scalar to each sub-diagonal: +assignDiagonal( [ scalar2ndarray( 3.0 ), y ], [ 1, 2 ], -1 ); +console.log( ndarray2array( y ) ); +``` + +
+ + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/ndarray/base/assign-diagonal/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/base/assign-diagonal/benchmark/benchmark.js new file mode 100644 index 000000000000..b62466f5d49e --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/assign-diagonal/benchmark/benchmark.js @@ -0,0 +1,297 @@ +/** +* @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 scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var assignDiagonal = require( './../lib' ); + + +// MAIN // + +bench( format( '%s:ndims=2,ctor=base', pkg ), function benchmark( b ) { + var values; + var out; + var x; + var i; + + x = scalar2ndarray( 1.0 ); + + 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++ ) { + out = assignDiagonal( [ x, values[ i%values.length ] ], [ 0, 1 ], 0 ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( out ) ) { + 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 out; + var x; + var i; + + x = scalar2ndarray( 1.0 ); + + /* 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++ ) { + out = assignDiagonal( [ x, values[ i%values.length ] ], [ 0, 1 ], 0 ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( out ) ) { + 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 out; + var x; + var i; + + x = scalar2ndarray( 1.0 ); + + 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++ ) { + out = assignDiagonal( [ x, values[ i%values.length ] ], [ 1, 2 ], 0 ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( out ) ) { + 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 out; + var x; + var i; + + x = scalar2ndarray( 1.0 ); + + /* 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++ ) { + out = assignDiagonal( [ x, values[ i%values.length ] ], [ 1, 2 ], 0 ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( out ) ) { + 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 out; + var x; + var i; + + x = scalar2ndarray( 1.0 ); + + 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++ ) { + out = assignDiagonal( [ x, values[ i%values.length ] ], [ 2, 3 ], 0 ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( out ) ) { + 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 out; + var x; + var i; + + x = scalar2ndarray( 1.0 ); + + /* 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++ ) { + out = assignDiagonal( [ x, values[ i%values.length ] ], [ 2, 3 ], 0 ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( out ) ) { + 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 out; + var x; + var i; + + x = scalar2ndarray( 1.0 ); + + 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++ ) { + out = assignDiagonal( [ x, values[ i%values.length ] ], [ 3, 4 ], 0 ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( out ) ) { + 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 out; + var x; + var i; + + x = scalar2ndarray( 1.0 ); + + /* 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++ ) { + out = assignDiagonal( [ x, values[ i%values.length ] ], [ 3, 4 ], 0 ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( out ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/ndarray/base/assign-diagonal/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/base/assign-diagonal/docs/repl.txt new file mode 100644 index 000000000000..0f3e23b5d2a3 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/assign-diagonal/docs/repl.txt @@ -0,0 +1,52 @@ + +{{alias}}( arrays, dims, k ) + Assigns elements from a broadcasted input ndarray to a specified diagonal + of an output ndarray. + + 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 assigns to the main diagonal; when `k > 0`, the + function assigns to a diagonal above the main diagonal; and when `k < 0`, + the function assigns to a diagonal below the main diagonal. + + The input ndarray must be broadcast compatible with the output ndarray view + defined by the specified diagonal. + + The function mutates the output ndarray in-place. + + Parameters + ---------- + arrays: ArrayLikeObject + Array-like object containing one input array and one output array. + + dims: ArrayLikeObject + Dimension indices defining the plane in which to assign elements to + the diagonal. + + k: integer + Diagonal offset. + + Returns + ------- + out: ndarray + Output ndarray. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/from-scalar}}( 1.0 ) + + > var y = {{alias:@stdlib/ndarray/zeros}}( [ 3, 3 ] ) + + > var out = {{alias}}( [ x, y ], [ 0, 1 ], 0 ) + [ [ 1.0, 0.0, 0.0 ], [ 0.0, 1.0, 0.0 ], [ 0.0, 0.0, 1.0 ] ] + > var bool = ( out === y ) + true + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/ndarray/base/assign-diagonal/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/base/assign-diagonal/docs/types/index.d.ts new file mode 100644 index 000000000000..497c8a1ef1d3 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/assign-diagonal/docs/types/index.d.ts @@ -0,0 +1,63 @@ +/* +* @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 { ndarray, typedndarray } from '@stdlib/types/ndarray'; + +/** +* Assigns elements from a broadcasted input ndarray to a specified diagonal of an output ndarray. +* +* ## 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 assigns to the main diagonal; when `k > 0`, the function assigns to a diagonal above the main diagonal; and when `k < 0`, the function assigns to a diagonal below the main diagonal. +* - The input ndarray must be broadcast compatible with the output ndarray view defined by the specified diagonal. +* - The function mutates the output ndarray in-place. +* +* @param arrays - array-like object containing one input array and one output array +* @param dims - dimension indices defining the plane in which to assign elements to the diagonal +* @param k - diagonal offset +* @returns output ndarray +* +* @example +* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +* var zeros = require( '@stdlib/ndarray/zeros' ); +* +* var x = scalar2ndarray( 1.0 ); +* // returns +* +* var y = zeros( [ 3, 3 ] ); +* // returns [ [ 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0 ] ] +* +* var out = assignDiagonal( [ x, y ], [ 0, 1 ], 0 ); +* // returns [ [ 1.0, 0.0, 0.0 ], [ 0.0, 1.0, 0.0 ], [ 0.0, 0.0, 1.0 ] ] +* +* var bool = ( out === y ); +* // returns true +*/ +declare function assignDiagonal = typedndarray>( arrays: [ ndarray, U ], dims: Collection, k: number ): U; + + +// EXPORTS // + +export = assignDiagonal; diff --git a/lib/node_modules/@stdlib/ndarray/base/assign-diagonal/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/base/assign-diagonal/docs/types/test.ts new file mode 100644 index 000000000000..7337458115b2 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/assign-diagonal/docs/types/test.ts @@ -0,0 +1,85 @@ +/* +* @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 scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +import zeros = require( '@stdlib/ndarray/zeros' ); +import assignDiagonal = require( './index' ); + + +// TESTS // + +// The function returns an ndarray... +{ + const x = scalar2ndarray( 1.0 ); + const y = zeros( [ 2, 2 ] ); + + assignDiagonal( [ x, y ], [ 0, 1 ], 0 ); // $ExpectType float64ndarray +} + +// The compiler throws an error if the function is not provided a first argument which is an array-like object containing ndarrays... +{ + assignDiagonal( '5', [ 0, 1 ], 0 ); // $ExpectError + assignDiagonal( 5, [ 0, 1 ], 0 ); // $ExpectError + assignDiagonal( true, [ 0, 1 ], 0 ); // $ExpectError + assignDiagonal( false, [ 0, 1 ], 0 ); // $ExpectError + assignDiagonal( null, [ 0, 1 ], 0 ); // $ExpectError + assignDiagonal( {}, [ 0, 1 ], 0 ); // $ExpectError + assignDiagonal( ( x: number ): number => x, [ 0, 1 ], 0 ); // $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 = scalar2ndarray( 1.0 ); + const y = zeros( [ 2, 2 ] ); + + assignDiagonal( [ x, y ], '5', 0 ); // $ExpectError + assignDiagonal( [ x, y ], 5, 0 ); // $ExpectError + assignDiagonal( [ x, y ], true, 0 ); // $ExpectError + assignDiagonal( [ x, y ], false, 0 ); // $ExpectError + assignDiagonal( [ x, y ], null, 0 ); // $ExpectError + assignDiagonal( [ x, y ], {}, 0 ); // $ExpectError + assignDiagonal( [ x, y ], [ '5' ], 0 ); // $ExpectError + assignDiagonal( [ x, y ], ( x: number ): number => x, 0 ); // $ExpectError +} + +// The compiler throws an error if the function is not provided a third argument which is a number... +{ + const x = scalar2ndarray( 1.0 ); + const y = zeros( [ 2, 2 ] ); + + assignDiagonal( [ x, y ], [ 0, 1 ], '5' ); // $ExpectError + assignDiagonal( [ x, y ], [ 0, 1 ], true ); // $ExpectError + assignDiagonal( [ x, y ], [ 0, 1 ], false ); // $ExpectError + assignDiagonal( [ x, y ], [ 0, 1 ], null ); // $ExpectError + assignDiagonal( [ x, y ], [ 0, 1 ], {} ); // $ExpectError + assignDiagonal( [ x, y ], [ 0, 1 ], [] ); // $ExpectError + assignDiagonal( [ x, y ], [ 0, 1 ], ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = scalar2ndarray( 1.0 ); + const y = zeros( [ 2, 2 ] ); + + assignDiagonal(); // $ExpectError + assignDiagonal( [ x, y ] ); // $ExpectError + assignDiagonal( [ x, y ], [ 0, 1 ] ); // $ExpectError + assignDiagonal( [ x, y ], [ 0, 1 ], 0, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/ndarray/base/assign-diagonal/examples/index.js b/lib/node_modules/@stdlib/ndarray/base/assign-diagonal/examples/index.js new file mode 100644 index 000000000000..5e889a112cc8 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/assign-diagonal/examples/index.js @@ -0,0 +1,40 @@ +/** +* @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 scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var assignDiagonal = require( './../lib' ); + +// Create a stack of matrices: +var y = zeros( [ 2, 3, 3 ] ); +console.log( ndarray2array( y ) ); + +// Assign a scalar to each main diagonal: +assignDiagonal( [ scalar2ndarray( 1.0 ), y ], [ 1, 2 ], 0 ); +console.log( ndarray2array( y ) ); + +// Assign a scalar to each super-diagonal: +assignDiagonal( [ scalar2ndarray( 2.0 ), y ], [ 1, 2 ], 1 ); +console.log( ndarray2array( y ) ); + +// Assign a scalar to each sub-diagonal: +assignDiagonal( [ scalar2ndarray( 3.0 ), y ], [ 1, 2 ], -1 ); +console.log( ndarray2array( y ) ); diff --git a/lib/node_modules/@stdlib/ndarray/base/assign-diagonal/lib/index.js b/lib/node_modules/@stdlib/ndarray/base/assign-diagonal/lib/index.js new file mode 100644 index 000000000000..976c185770f1 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/assign-diagonal/lib/index.js @@ -0,0 +1,51 @@ +/** +* @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'; + +/** +* Assign elements from a broadcasted input ndarray to a specified diagonal of an output ndarray. +* +* @module @stdlib/ndarray/base/assign-diagonal +* +* @example +* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +* var zeros = require( '@stdlib/ndarray/zeros' ); +* var assignDiagonal = require( '@stdlib/ndarray/base/assign-diagonal' ); +* +* var x = scalar2ndarray( 1.0 ); +* // returns +* +* var y = zeros( [ 3, 3 ] ); +* // returns [ [ 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0 ] ] +* +* var out = assignDiagonal( [ x, y ], [ 0, 1 ], 0 ); +* // returns [ [ 1.0, 0.0, 0.0 ], [ 0.0, 1.0, 0.0 ], [ 0.0, 0.0, 1.0 ] ] +* +* var bool = ( out === y ); +* // returns true +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/ndarray/base/assign-diagonal/lib/main.js b/lib/node_modules/@stdlib/ndarray/base/assign-diagonal/lib/main.js new file mode 100644 index 000000000000..7830ea38f4ed --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/assign-diagonal/lib/main.js @@ -0,0 +1,88 @@ +/** +* @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 broadcast = require( '@stdlib/ndarray/base/broadcast-array' ); +var diagonal = require( '@stdlib/ndarray/base/diagonal' ); +var getShape = require( '@stdlib/ndarray/base/shape' ); +var assign = require( '@stdlib/ndarray/base/assign' ); + + +// MAIN // + +/** +* Assigns elements from a broadcasted input ndarray to a specified diagonal of an output ndarray. +* +* ## 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 assigns to the main diagonal; when `k > 0`, the function assigns to a diagonal above the main diagonal; and when `k < 0`, the function assigns to a diagonal below the main diagonal. +* - The input ndarray must be broadcast compatible with the output ndarray view defined by the specified diagonal. +* - The function mutates the output ndarray in-place. +* +* @param {ArrayLikeObject} arrays - array-like object containing one input array and one output array +* @param {IntegerArray} dims - dimension indices defining the plane in which to assign elements to the diagonal +* @param {integer} k - diagonal offset +* @throws {RangeError} must provide exactly two dimension indices +* @throws {RangeError} output ndarray must have at least two dimensions +* @throws {RangeError} must provide valid dimension indices +* @throws {Error} must provide unique dimension indices +* @throws {Error} input ndarray must be broadcast compatible with the output ndarray diagonal view +* @returns {ndarray} output ndarray +* +* @example +* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +* var zeros = require( '@stdlib/ndarray/zeros' ); +* +* var x = scalar2ndarray( 1.0 ); +* // returns +* +* var y = zeros( [ 3, 3 ] ); +* // returns [ [ 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0 ] ] +* +* var out = assignDiagonal( [ x, y ], [ 0, 1 ], 0 ); +* // returns [ [ 1.0, 0.0, 0.0 ], [ 0.0, 1.0, 0.0 ], [ 0.0, 0.0, 1.0 ] ] +* +* var bool = ( out === y ); +* // returns true +*/ +function assignDiagonal( arrays, dims, k ) { + var view; + var x; + + // Resolve a writable output array view of the specified diagonal: + view = diagonal( arrays[ 1 ], dims, k, true ); + + // Broadcast the input array to the diagonal view shape: + x = broadcast( arrays[ 0 ], getShape( view, true ) ); + + // Assign elements from the broadcasted input array to the output array view: + assign( [ x, view ] ); + + // Return the original output ndarray: + return arrays[ 1 ]; +} + + +// EXPORTS // + +module.exports = assignDiagonal; diff --git a/lib/node_modules/@stdlib/ndarray/base/assign-diagonal/package.json b/lib/node_modules/@stdlib/ndarray/base/assign-diagonal/package.json new file mode 100644 index 000000000000..14f2f63f6693 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/assign-diagonal/package.json @@ -0,0 +1,65 @@ +{ + "name": "@stdlib/ndarray/base/assign-diagonal", + "version": "0.0.0", + "description": "Assign elements from a broadcasted input ndarray to a specified diagonal of an output ndarray.", + "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", + "assign", + "broadcast", + "matrix", + "stack" + ] +} diff --git a/lib/node_modules/@stdlib/ndarray/base/assign-diagonal/test/test.js b/lib/node_modules/@stdlib/ndarray/base/assign-diagonal/test/test.js new file mode 100644 index 000000000000..e15120cf8f9c --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/assign-diagonal/test/test.js @@ -0,0 +1,587 @@ +/** +* @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 Complex128Array = require( '@stdlib/array/complex128' ); +var Complex128 = require( '@stdlib/complex/float64/ctor' ); +var isSameComplex128Array = require( '@stdlib/assert/is-same-complex128array' ); +var array = require( '@stdlib/ndarray/array' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var zeros = require( '@stdlib/ndarray/base/zeros' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var getData = require( '@stdlib/ndarray/data-buffer' ); +var assignDiagonal = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof assignDiagonal, '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 y; + var i; + + x = scalar2ndarray( 1.0 ); + y = new ndarray( 'float64', new Float64Array( [ 0.0, 0.0, 0.0, 0.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() { + assignDiagonal( [ x, y ], dims, 0 ); + }; + } +}); + +tape( 'the function throws an error if provided an output ndarray with fewer than two dimensions', function test( t ) { + var values; + var x; + var i; + + x = scalar2ndarray( 1.0 ); + + 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 output ndarray with ' + values[ i ].ndims + ' dimensions' ); + } + t.end(); + + function badValue( y ) { + return function badValue() { + assignDiagonal( [ x, y ], [ 0, 1 ], 0 ); + }; + } +}); + +tape( 'the function throws an error if provided out-of-bounds dimension indices', function test( t ) { + var values; + var x; + var y; + var i; + + x = scalar2ndarray( 1.0 ); + y = new ndarray( 'float64', new Float64Array( [ 0.0, 0.0, 0.0, 0.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() { + assignDiagonal( [ x, y ], dims, 0 ); + }; + } +}); + +tape( 'the function throws an error if provided duplicate dimension indices', function test( t ) { + var values; + var x; + var y; + var i; + + x = scalar2ndarray( 1.0 ); + y = new ndarray( 'float64', new Float64Array( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.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() { + assignDiagonal( [ x, y ], dims, 0 ); + }; + } +}); + +tape( 'the function throws an error if provided an input ndarray which is not broadcast compatible with the output ndarray diagonal view', function test( t ) { + var values; + var y; + var i; + + y = zeros( 'float64', [ 3, 3 ], 'row-major' ); + + values = [ + array( [ 1.0, 2.0, 3.0, 4.0 ] ), + array( [ [ 1.0, 2.0, 3.0 ] ] ), + array( [ [ 1.0, 2.0, 3.0 ], [ 4.0, 5.0, 6.0 ] ] ) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided an input ndarray having shape [' + values[ i ].shape.join( ',' ) + ']' ); + } + t.end(); + + function badValue( x ) { + return function badValue() { + assignDiagonal( [ x, y ], [ 0, 1 ], 0 ); + }; + } +}); + +tape( 'the function assigns a broadcasted scalar to the main diagonal of a square matrix', function test( t ) { + var expected; + var out; + var x; + var y; + + x = scalar2ndarray( 1.0 ); + y = zeros( 'float64', [ 3, 3 ], 'row-major' ); + + out = assignDiagonal( [ x, y ], [ 0, 1 ], 0 ); + + expected = [ [ 1.0, 0.0, 0.0 ], [ 0.0, 1.0, 0.0 ], [ 0.0, 0.0, 1.0 ] ]; + + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + t.strictEqual( out, y, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function assigns a vector to the main diagonal of a square matrix', function test( t ) { + var expected; + var out; + var x; + var y; + + x = array( [ 1.0, 2.0, 3.0 ] ); + y = zeros( 'float64', [ 3, 3 ], 'row-major' ); + + out = assignDiagonal( [ x, y ], [ 0, 1 ], 0 ); + + expected = [ [ 1.0, 0.0, 0.0 ], [ 0.0, 2.0, 0.0 ], [ 0.0, 0.0, 3.0 ] ]; + + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + t.strictEqual( out, y, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function assigns to the super-diagonal of a square matrix', function test( t ) { + var expected; + var out; + var x; + var y; + + x = array( [ 1.0, 2.0 ] ); + y = zeros( 'float64', [ 3, 3 ], 'row-major' ); + + out = assignDiagonal( [ x, y ], [ 0, 1 ], 1 ); + + expected = [ [ 0.0, 1.0, 0.0 ], [ 0.0, 0.0, 2.0 ], [ 0.0, 0.0, 0.0 ] ]; + + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + + x = array( [ 7.0 ] ); + y = zeros( 'float64', [ 3, 3 ], 'row-major' ); + + out = assignDiagonal( [ x, y ], [ 0, 1 ], 2 ); + + expected = [ [ 0.0, 0.0, 7.0 ], [ 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0 ] ]; + + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + t.strictEqual( out, y, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function assigns to the sub-diagonal of a square matrix', function test( t ) { + var expected; + var out; + var x; + var y; + + x = array( [ 1.0, 2.0 ] ); + y = zeros( 'float64', [ 3, 3 ], 'row-major' ); + + out = assignDiagonal( [ x, y ], [ 0, 1 ], -1 ); + + expected = [ [ 0.0, 0.0, 0.0 ], [ 1.0, 0.0, 0.0 ], [ 0.0, 2.0, 0.0 ] ]; + + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + + x = scalar2ndarray( 7.0 ); + y = zeros( 'float64', [ 3, 3 ], 'row-major' ); + + out = assignDiagonal( [ x, y ], [ 0, 1 ], -2 ); + + expected = [ [ 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0 ], [ 7.0, 0.0, 0.0 ] ]; + + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + t.strictEqual( out, y, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function assigns to the diagonal of a non-square matrix (M < N)', function test( t ) { + var expected; + var out; + var x; + var y; + + x = scalar2ndarray( 1.0 ); + y = zeros( 'float64', [ 2, 4 ], 'row-major' ); + + out = assignDiagonal( [ x, y ], [ 0, 1 ], 0 ); + + expected = [ [ 1.0, 0.0, 0.0, 0.0 ], [ 0.0, 1.0, 0.0, 0.0 ] ]; + + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + t.strictEqual( out, y, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function assigns to the diagonal of a non-square matrix (M > N)', function test( t ) { + var expected; + var out; + var x; + var y; + + x = scalar2ndarray( 1.0 ); + y = zeros( 'float64', [ 4, 2 ], 'row-major' ); + + out = assignDiagonal( [ x, y ], [ 0, 1 ], 0 ); + + expected = [ [ 1.0, 0.0 ], [ 0.0, 1.0 ], [ 0.0, 0.0 ], [ 0.0, 0.0 ] ]; + + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + t.strictEqual( out, y, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns the output ndarray unchanged when the diagonal offset is out-of-bounds', function test( t ) { + var expected; + var out; + var x; + var y; + + x = scalar2ndarray( -1.0 ); + y = array( [ [ 1.0, 2.0, 3.0 ], [ 4.0, 5.0, 6.0 ], [ 7.0, 8.0, 9.0 ] ] ); + + out = assignDiagonal( [ x, y ], [ 0, 1 ], 3 ); + + expected = [ [ 1.0, 2.0, 3.0 ], [ 4.0, 5.0, 6.0 ], [ 7.0, 8.0, 9.0 ] ]; + + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + t.strictEqual( out, y, 'returns expected value' ); + + out = assignDiagonal( [ x, y ], [ 0, 1 ], -3 ); + + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + t.strictEqual( out, y, 'returns expected value' ); + + out = assignDiagonal( [ x, y ], [ 0, 1 ], 100 ); + + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + t.strictEqual( out, y, '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 out; + var x; + var y; + + x = scalar2ndarray( 1.0 ); + y = zeros( 'float64', [ 3, 3 ], 'row-major' ); + + out = assignDiagonal( [ x, y ], [ 1, 0 ], 1 ); + + expected = [ [ 0.0, 0.0, 0.0 ], [ 1.0, 0.0, 0.0 ], [ 0.0, 1.0, 0.0 ] ]; + + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + + y = zeros( 'float64', [ 3, 3 ], 'row-major' ); + + out = assignDiagonal( [ x, y ], [ 1, 0 ], -1 ); + + expected = [ [ 0.0, 1.0, 0.0 ], [ 0.0, 0.0, 1.0 ], [ 0.0, 0.0, 0.0 ] ]; + + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + t.strictEqual( out, y, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function assigns to the diagonals of a stack of matrices', function test( t ) { + var expected; + var out; + var x; + var y; + + x = scalar2ndarray( 1.0 ); + y = zeros( 'float64', [ 2, 2, 2 ], 'row-major' ); + + out = assignDiagonal( [ x, y ], [ 1, 2 ], 0 ); + + expected = [ + [ [ 1.0, 0.0 ], [ 0.0, 1.0 ] ], + [ [ 1.0, 0.0 ], [ 0.0, 1.0 ] ] + ]; + + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + t.strictEqual( out, y, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function broadcasts a per-stack diagonal vector across a stack of matrices', function test( t ) { + var expected; + var out; + var x; + var y; + + x = array( [ 1.0, 2.0 ] ); + y = zeros( 'float64', [ 3, 2, 2 ], 'row-major' ); + + out = assignDiagonal( [ x, y ], [ 1, 2 ], 0 ); + + expected = [ + [ [ 1.0, 0.0 ], [ 0.0, 2.0 ] ], + [ [ 1.0, 0.0 ], [ 0.0, 2.0 ] ], + [ [ 1.0, 0.0 ], [ 0.0, 2.0 ] ] + ]; + + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + t.strictEqual( out, y, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function assigns to the diagonal when `dims` selects non-adjacent dimensions', function test( t ) { + var expected; + var out; + var x; + var y; + + x = scalar2ndarray( 1.0 ); + y = zeros( 'float64', [ 2, 2, 3 ], 'row-major' ); + + out = assignDiagonal( [ x, y ], [ 0, 2 ], 0 ); + + expected = [ + [ [ 1.0, 0.0, 0.0 ], [ 1.0, 0.0, 0.0 ] ], + [ [ 0.0, 1.0, 0.0 ], [ 0.0, 1.0, 0.0 ] ] + ]; + + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + t.strictEqual( out, y, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports output ndarrays having a non-zero buffer offset', function test( t ) { + var expected; + var buf; + var out; + var x; + var y; + + buf = [ -1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ]; + y = new ndarray( 'generic', buf, [ 3, 3 ], [ 3, 1 ], 1, 'row-major' ); + x = scalar2ndarray( 1.0 ); + + out = assignDiagonal( [ x, y ], [ 0, 1 ], 0 ); + + expected = [ [ 1.0, 0.0, 0.0 ], [ 0.0, 1.0, 0.0 ], [ 0.0, 0.0, 1.0 ] ]; + + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + t.strictEqual( buf[ 0 ], -1.0, 'returns expected value' ); + t.strictEqual( out, y, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function normalizes negative dimension indices', function test( t ) { + var expected; + var out; + var x; + var y; + + x = scalar2ndarray( 1.0 ); + y = zeros( 'float64', [ 3, 3 ], 'row-major' ); + + out = assignDiagonal( [ x, y ], [ -2, -1 ], 0 ); + + expected = [ [ 1.0, 0.0, 0.0 ], [ 0.0, 1.0, 0.0 ], [ 0.0, 0.0, 1.0 ] ]; + + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + t.strictEqual( out, y, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports column-major output ndarrays', function test( t ) { + var expected; + var out; + var x; + var y; + + x = scalar2ndarray( 1.0 ); + y = zeros( 'float64', [ 3, 3 ], 'column-major' ); + + out = assignDiagonal( [ x, y ], [ 0, 1 ], 0 ); + + expected = [ [ 1.0, 0.0, 0.0 ], [ 0.0, 1.0, 0.0 ], [ 0.0, 0.0, 1.0 ] ]; + + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + t.strictEqual( out, y, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function assigns to non-trailing dimensions of higher-dimensional ndarrays', function test( t ) { + var expected; + var out; + var x; + var y; + + x = scalar2ndarray( 1.0 ); + y = zeros( 'float64', [ 2, 3, 2 ], 'row-major' ); + + out = assignDiagonal( [ x, y ], [ 0, 1 ], 0 ); + + expected = [ + [ + [ 1.0, 1.0 ], + [ 0.0, 0.0 ], + [ 0.0, 0.0 ] + ], + [ + [ 0.0, 0.0 ], + [ 1.0, 1.0 ], + [ 0.0, 0.0 ] + ] + ]; + + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + t.strictEqual( out, y, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports negative dimension indices combined with a non-zero `k`', function test( t ) { + var expected; + var out; + var x; + var y; + + x = scalar2ndarray( 1.0 ); + y = zeros( 'float64', [ 3, 3 ], 'row-major' ); + + out = assignDiagonal( [ x, y ], [ -2, -1 ], 1 ); + + expected = [ [ 0.0, 1.0, 0.0 ], [ 0.0, 0.0, 1.0 ], [ 0.0, 0.0, 0.0 ] ]; + + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + + y = zeros( 'float64', [ 3, 3 ], 'row-major' ); + + out = assignDiagonal( [ x, y ], [ -2, -1 ], -1 ); + + expected = [ [ 0.0, 0.0, 0.0 ], [ 1.0, 0.0, 0.0 ], [ 0.0, 1.0, 0.0 ] ]; + + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + t.strictEqual( out, y, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function assigns to the diagonal of a complex128 ndarray', function test( t ) { + var expected; + var out; + var x; + var y; + + x = scalar2ndarray( new Complex128( 1.0, 2.0 ), { + 'dtype': 'complex128' + }); + y = zeros( 'complex128', [ 3, 3 ], 'row-major' ); + + out = assignDiagonal( [ x, y ], [ 0, 1 ], 0 ); + + expected = new Complex128Array([ + 1.0, + 2.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 1.0, + 2.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 1.0, + 2.0 + ]); + + t.strictEqual( isSameComplex128Array( getData( out ), expected ), true, 'returns expected value' ); + t.strictEqual( out, y, 'returns expected value' ); + + t.end(); +});