From 5529af973da62b1108812db69e1482a1f31870c9 Mon Sep 17 00:00:00 2001 From: headlessNode Date: Mon, 4 May 2026 00:53:05 +0500 Subject: [PATCH 1/9] feat: add ndarray/base/fill-diagonal --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed --- --- .../ndarray/base/fill-diagonal/README.md | 152 ++++++ .../base/fill-diagonal/benchmark/benchmark.js | 272 ++++++++++ .../ndarray/base/fill-diagonal/docs/repl.txt | 53 ++ .../base/fill-diagonal/docs/types/index.d.ts | 59 +++ .../base/fill-diagonal/docs/types/test.ts | 82 +++ .../base/fill-diagonal/examples/index.js | 38 ++ .../ndarray/base/fill-diagonal/lib/index.js | 47 ++ .../ndarray/base/fill-diagonal/lib/main.js | 70 +++ .../ndarray/base/fill-diagonal/package.json | 64 +++ .../ndarray/base/fill-diagonal/test/test.js | 496 ++++++++++++++++++ 10 files changed, 1333 insertions(+) create mode 100644 lib/node_modules/@stdlib/ndarray/base/fill-diagonal/README.md create mode 100644 lib/node_modules/@stdlib/ndarray/base/fill-diagonal/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/fill-diagonal/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/ndarray/base/fill-diagonal/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/ndarray/base/fill-diagonal/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/ndarray/base/fill-diagonal/examples/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/fill-diagonal/lib/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/fill-diagonal/lib/main.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/fill-diagonal/package.json create mode 100644 lib/node_modules/@stdlib/ndarray/base/fill-diagonal/test/test.js diff --git a/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/README.md b/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/README.md new file mode 100644 index 000000000000..d2df1a3c4d70 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/README.md @@ -0,0 +1,152 @@ + + +# fillDiagonal + +> Fill a specified diagonal of a matrix (or stack of matrices) with a scalar value. + +
+ +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 fillDiagonal = require( '@stdlib/ndarray/base/fill-diagonal' ); +``` + +#### fillDiagonal( x, value, dims, k ) + +Fills a specified diagonal of a matrix (or stack of matrices) with a scalar value. + +```javascript +var zeros = require( '@stdlib/ndarray/base/zeros' ); + +var x = zeros( 'float64', [ 3, 3 ], 'row-major' ); +// returns [ [ 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0 ] ] + +var out = fillDiagonal( x, 1.0, [ 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 === x ); +// returns true +``` + +The function accepts the following arguments: + +- **x**: input ndarray. +- **value**: scalar value. +- **dims**: dimension indices defining the plane in which to fill 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 fills the main diagonal; when `k > 0`, the function fills the diagonal above the main diagonal; and when `k < 0`, the function fills the diagonal below the main diagonal. +- If `value` is a number and `x` has a complex [data type][@stdlib/ndarray/dtypes], the function fills an input ndarray with a complex number whose real component equals the provided scalar `value` and whose imaginary component is zero. +- A `value` must be able to safely cast to the input ndarray [data type][@stdlib/ndarray/dtypes]. Scalar values having floating-point data types (both real and complex) are allowed to downcast to a lower precision data type of the same kind (e.g., a scalar double-precision floating-point number can be used to fill a `'float32'` input ndarray). +- The function **mutates** the input ndarray in-place. + +
+ + + +
+ +## Examples + + + +```javascript +var zeros = require( '@stdlib/ndarray/base/zeros' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var fillDiagonal = require( '@stdlib/ndarray/base/fill-diagonal' ); + +// Create a stack of zero matrices: +var x = zeros( 'float64', [ 2, 3, 3 ], 'row-major' ); + +// Fill the main diagonals: +fillDiagonal( x, 1.0, [ 1, 2 ], 0 ); +console.log( ndarray2array( x ) ); + +// Fill the super-diagonals: +fillDiagonal( x, 2.0, [ 1, 2 ], 1 ); +console.log( ndarray2array( x ) ); + +// Fill the sub-diagonals: +fillDiagonal( x, 3.0, [ 1, 2 ], -1 ); +console.log( ndarray2array( x ) ); +``` + +
+ + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/benchmark/benchmark.js new file mode 100644 index 000000000000..f4ce5ba7d243 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/fill-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 fillDiagonal = require( './../lib' ); + + +// MAIN // + +bench( format( '%s::ndims=2,ctor=base', pkg ), function benchmark( b ) { + var values; + var out; + 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++ ) { + out = fillDiagonal( values[ i%values.length ], 1.0, [ 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 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++ ) { + out = fillDiagonal( values[ i%values.length ], 1.0, [ 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 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++ ) { + out = fillDiagonal( values[ i%values.length ], 1.0, [ 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 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++ ) { + out = fillDiagonal( values[ i%values.length ], 1.0, [ 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 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++ ) { + out = fillDiagonal( values[ i%values.length ], 1.0, [ 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 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++ ) { + out = fillDiagonal( values[ i%values.length ], 1.0, [ 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 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++ ) { + out = fillDiagonal( values[ i%values.length ], 1.0, [ 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 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++ ) { + out = fillDiagonal( values[ i%values.length ], 1.0, [ 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/fill-diagonal/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/docs/repl.txt new file mode 100644 index 000000000000..d900632755a0 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/docs/repl.txt @@ -0,0 +1,53 @@ + +{{alias}}( x, value, dims, k ) + Fills a specified diagonal of a matrix (or stack of matrices) with a + scalar value. + + 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 fills the main diagonal; when `k > 0`, the + function fills the diagonal above the main diagonal; and when `k < 0`, + the function fills the diagonal below the main diagonal. + + The function mutates the input ndarray in-place. + + Parameters + ---------- + x: ndarray + Input array. + + value: any + Scalar value. Must be able to safely cast to the input ndarray data + type. Scalar values having floating-point data types (both real and + complex) are allowed to downcast to a lower precision data type of the + same kind (e.g., a scalar double-precision floating-point number can be + used to fill a 'float32' input ndarray). + + dims: ArrayLikeObject + Dimension indices defining the plane in which to fill the diagonal. + + k: integer + Diagonal offset. + + Returns + ------- + out: ndarray + Input array. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/base/zeros}}( 'float64', [ 3, 3 ], 'row-major' ) + + > var out = {{alias}}( x, 1.0, [ 0, 1 ], 0 ) + + > var bool = ( out === x ) + true + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/docs/types/index.d.ts new file mode 100644 index 000000000000..1c4e42d059d7 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/docs/types/index.d.ts @@ -0,0 +1,59 @@ +/* +* @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'; + +/** +* Fills a specified diagonal of a matrix (or stack of matrices) with a scalar value. +* +* ## 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 fills the main diagonal; when `k > 0`, the function fills the diagonal above the main diagonal; and when `k < 0`, the function fills the diagonal below the main diagonal. +* - The function mutates the input ndarray in-place. +* +* @param x - input array +* @param value - scalar value +* @param dims - dimension indices defining the plane in which to fill the diagonal +* @param k - diagonal offset +* @returns input ndarray +* +* @example +* var zeros = require( '@stdlib/ndarray/base/zeros' ); +* +* var x = zeros( 'float64', [ 3, 3 ], 'row-major' ); +* // returns [ [ 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0 ] ] +* +* var out = fillDiagonal( x, 1.0, [ 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 === x ); +* // returns true +*/ +declare function fillDiagonal = typedndarray>( x: U, value: T, dims: Collection, k: number ): U; + + +// EXPORTS // + +export = fillDiagonal; diff --git a/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/docs/types/test.ts new file mode 100644 index 000000000000..82c5d4eaf271 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/docs/types/test.ts @@ -0,0 +1,82 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable space-in-parens */ + +import zeros = require( '@stdlib/ndarray/zeros' ); +import fillDiagonal = require( './index' ); + + +// TESTS // + +// The function returns an ndarray... +{ + const x = zeros( [ 2, 2 ] ); + + fillDiagonal( x, 1.0, [ 0, 1 ], 0 ); // $ExpectType float64ndarray +} + +// The compiler throws an error if the function is not provided a first argument which is an ndarray... +{ + fillDiagonal( '5', 1.0, [ 0, 1 ], 0 ); // $ExpectError + fillDiagonal( 5, 1.0, [ 0, 1 ], 0 ); // $ExpectError + fillDiagonal( true, 1.0, [ 0, 1 ], 0 ); // $ExpectError + fillDiagonal( false, 1.0, [ 0, 1 ], 0 ); // $ExpectError + fillDiagonal( null, 1.0, [ 0, 1 ], 0 ); // $ExpectError + fillDiagonal( {}, 1.0, [ 0, 1 ], 0 ); // $ExpectError + fillDiagonal( [ '5' ], 1.0, [ 0, 1 ], 0 ); // $ExpectError + fillDiagonal( ( x: number ): number => x, 1.0, [ 0, 1 ], 0 ); // $ExpectError +} + +// The compiler throws an error if the function is not provided a third argument which is an array-like object containing numbers... +{ + const x = zeros( [ 2, 2 ] ); + + fillDiagonal( x, 1.0, '5', 0 ); // $ExpectError + fillDiagonal( x, 1.0, 5, 0 ); // $ExpectError + fillDiagonal( x, 1.0, true, 0 ); // $ExpectError + fillDiagonal( x, 1.0, false, 0 ); // $ExpectError + fillDiagonal( x, 1.0, null, 0 ); // $ExpectError + fillDiagonal( x, 1.0, {}, 0 ); // $ExpectError + fillDiagonal( x, 1.0, [ '5' ], 0 ); // $ExpectError + fillDiagonal( x, 1.0, ( x: number ): number => x, 0 ); // $ExpectError +} + +// The compiler throws an error if the function is not provided a fourth argument which is a number... +{ + const x = zeros( [ 2, 2 ] ); + + fillDiagonal( x, 1.0, [ 0, 1 ], '5' ); // $ExpectError + fillDiagonal( x, 1.0, [ 0, 1 ], true ); // $ExpectError + fillDiagonal( x, 1.0, [ 0, 1 ], false ); // $ExpectError + fillDiagonal( x, 1.0, [ 0, 1 ], null ); // $ExpectError + fillDiagonal( x, 1.0, [ 0, 1 ], {} ); // $ExpectError + fillDiagonal( x, 1.0, [ 0, 1 ], [] ); // $ExpectError + fillDiagonal( x, 1.0, [ 0, 1 ], ( 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 ] ); + + fillDiagonal(); // $ExpectError + fillDiagonal( x ); // $ExpectError + fillDiagonal( x, 1.0 ); // $ExpectError + fillDiagonal( x, 1.0, [ 0, 1 ] ); // $ExpectError + fillDiagonal( x, 1.0, [ 0, 1 ], 0, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/examples/index.js b/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/examples/index.js new file mode 100644 index 000000000000..277023009e54 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/examples/index.js @@ -0,0 +1,38 @@ +/** +* @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 zeros = require( '@stdlib/ndarray/base/zeros' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var fillDiagonal = require( './../lib' ); + +// Create a stack of zero matrices: +var x = zeros( 'float64', [ 2, 3, 3 ], 'row-major' ); + +// Fill the main diagonals: +fillDiagonal( x, 1.0, [ 1, 2 ], 0 ); +console.log( ndarray2array( x ) ); + +// Fill the super-diagonals: +fillDiagonal( x, 2.0, [ 1, 2 ], 1 ); +console.log( ndarray2array( x ) ); + +// Fill the sub-diagonals: +fillDiagonal( x, 3.0, [ 1, 2 ], -1 ); +console.log( ndarray2array( x ) ); diff --git a/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/lib/index.js b/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/lib/index.js new file mode 100644 index 000000000000..662161495a57 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/lib/index.js @@ -0,0 +1,47 @@ +/** +* @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'; + +/** +* Fill a specified diagonal of a matrix (or stack of matrices) with a scalar value. +* +* @module @stdlib/ndarray/base/fill-diagonal +* +* @example +* var zeros = require( '@stdlib/ndarray/base/zeros' ); +* var fillDiagonal = require( '@stdlib/ndarray/base/fill-diagonal' ); +* +* var x = zeros( 'float64', [ 3, 3 ], 'row-major' ); +* // returns [ [ 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0 ] ] +* +* var out = fillDiagonal( x, 1.0, [ 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 === x ); +* // returns true +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/lib/main.js b/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/lib/main.js new file mode 100644 index 000000000000..6ec55778378b --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/lib/main.js @@ -0,0 +1,70 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var diagonal = require( '@stdlib/ndarray/base/diagonal' ); +var fill = require( '@stdlib/ndarray/base/fill' ); + + +// MAIN // + +/** +* Fills a specified diagonal of a matrix (or stack of matrices) with a scalar value. +* +* ## 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 fills the main diagonal; when `k > 0`, the function fills the diagonal above the main diagonal; and when `k < 0`, the function fills the diagonal below the main diagonal. +* - The function mutates the input ndarray in-place. +* +* @param {ndarray} x - input array +* @param {*} value - scalar value +* @param {IntegerArray} dims - dimension indices defining the plane in which to fill the diagonal +* @param {integer} k - diagonal offset +* @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 +* @throws {TypeError} second argument cannot be safely cast to the input array data type +* @returns {ndarray} input ndarray +* +* @example +* var zeros = require( '@stdlib/ndarray/base/zeros' ); +* +* var x = zeros( 'float64', [ 3, 3 ], 'row-major' ); +* // returns [ [ 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0 ] ] +* +* var out = fillDiagonal( x, 1.0, [ 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 === x ); +* // returns true +*/ +function fillDiagonal( x, value, dims, k ) { + fill( diagonal( x, dims, k, true ), value ); + return x; +} + + +// EXPORTS // + +module.exports = fillDiagonal; diff --git a/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/package.json b/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/package.json new file mode 100644 index 000000000000..59cf523e7875 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/package.json @@ -0,0 +1,64 @@ +{ + "name": "@stdlib/ndarray/base/fill-diagonal", + "version": "0.0.0", + "description": "Fill a specified diagonal of a matrix (or stack of matrices) with a scalar value.", + "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", + "fill", + "matrix", + "stack" + ] +} diff --git a/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/test/test.js b/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/test/test.js new file mode 100644 index 000000000000..1225add25b0e --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/test/test.js @@ -0,0 +1,496 @@ +/** +* @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 getData = require( '@stdlib/ndarray/data-buffer' ); +var fillDiagonal = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof fillDiagonal, '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() { + fillDiagonal( x, 1.0, dims, 0 ); + }; + } +}); + +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() { + fillDiagonal( x, 1.0, [ 0, 1 ], 0 ); + }; + } +}); + +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() { + fillDiagonal( x, 1.0, dims, 0 ); + }; + } +}); + +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() { + fillDiagonal( x, 1.0, dims, 0 ); + }; + } +}); + +tape( 'the function throws an error if provided a value which cannot be safely cast to the input array data type', function test( t ) { + var x; + + x = zeros( 'int32', [ 3, 3 ], 'row-major' ); + + t.throws( badValue, TypeError, 'throws an error' ); + t.end(); + + function badValue() { + fillDiagonal( x, 3.14, [ 0, 1 ], 0 ); + } +}); + +tape( 'the function fills the main diagonal of a square matrix', function test( t ) { + var expected; + var out; + var x; + + x = zeros( 'float64', [ 3, 3 ], 'row-major' ); + + out = fillDiagonal( x, 1.0, [ 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, x, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function fills the super-diagonal of a square matrix', function test( t ) { + var expected; + var out; + var x; + + x = zeros( 'float64', [ 3, 3 ], 'row-major' ); + + out = fillDiagonal( x, 1.0, [ 0, 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' ); + + x = zeros( 'float64', [ 3, 3 ], 'row-major' ); + out = fillDiagonal( x, 1.0, [ 0, 1 ], 2 ); + + expected = [ [ 0.0, 0.0, 1.0 ], [ 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0 ] ]; + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function fills the sub-diagonal of a square matrix', function test( t ) { + var expected; + var out; + var x; + + x = zeros( 'float64', [ 3, 3 ], 'row-major' ); + + out = fillDiagonal( x, 1.0, [ 0, 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' ); + + x = zeros( 'float64', [ 3, 3 ], 'row-major' ); + out = fillDiagonal( x, 1.0, [ 0, 1 ], -2 ); + + expected = [ [ 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0 ], [ 1.0, 0.0, 0.0 ] ]; + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function fills the diagonal of a non-square matrix (M < N)', function test( t ) { + var expected; + var out; + var x; + + x = zeros( 'float64', [ 2, 4 ], 'row-major' ); + + out = fillDiagonal( x, 1.0, [ 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' ); + + x = zeros( 'float64', [ 2, 4 ], 'row-major' ); + out = fillDiagonal( x, 1.0, [ 0, 1 ], 2 ); + + expected = [ [ 0.0, 0.0, 1.0, 0.0 ], [ 0.0, 0.0, 0.0, 1.0 ] ]; + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + + x = zeros( 'float64', [ 2, 4 ], 'row-major' ); + out = fillDiagonal( x, 1.0, [ 0, 1 ], -1 ); + + expected = [ [ 0.0, 0.0, 0.0, 0.0 ], [ 1.0, 0.0, 0.0, 0.0 ] ]; + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function fills the diagonal of a non-square matrix (M > N)', function test( t ) { + var expected; + var out; + var x; + + x = zeros( 'float64', [ 4, 2 ], 'row-major' ); + + out = fillDiagonal( x, 1.0, [ 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' ); + + x = zeros( 'float64', [ 4, 2 ], 'row-major' ); + out = fillDiagonal( x, 1.0, [ 0, 1 ], -2 ); + + expected = [ [ 0.0, 0.0 ], [ 0.0, 0.0 ], [ 1.0, 0.0 ], [ 0.0, 1.0 ] ]; + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + + x = zeros( 'float64', [ 4, 2 ], 'row-major' ); + out = fillDiagonal( x, 1.0, [ 0, 1 ], 1 ); + + expected = [ [ 0.0, 1.0 ], [ 0.0, 0.0 ], [ 0.0, 0.0 ], [ 0.0, 0.0 ] ]; + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns the input ndarray unchanged when the diagonal offset is out-of-bounds', function test( t ) { + var expected; + var out; + var x; + + x = array( [ [ 1.0, 2.0, 3.0 ], [ 4.0, 5.0, 6.0 ], [ 7.0, 8.0, 9.0 ] ] ); + + out = fillDiagonal( x, -1.0, [ 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' ); + + out = fillDiagonal( x, -1.0, [ 0, 1 ], -3 ); + + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + + out = fillDiagonal( x, -1.0, [ 0, 1 ], 100 ); + + t.deepEqual( ndarray2array( out ), 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 out; + var x; + + x = zeros( 'float64', [ 3, 3 ], 'row-major' ); + + out = fillDiagonal( x, 1.0, [ 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' ); + + x = zeros( 'float64', [ 3, 3 ], 'row-major' ); + out = fillDiagonal( x, 1.0, [ 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.end(); +}); + +tape( 'the function fills the diagonals of a stack of matrices', function test( t ) { + var expected; + var out; + var x; + + x = zeros( 'float64', [ 2, 2, 2 ], 'row-major' ); + + out = fillDiagonal( x, 1.0, [ 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' ); + + x = zeros( 'float64', [ 2, 2, 2 ], 'row-major' ); + out = fillDiagonal( x, 1.0, [ 1, 2 ], 1 ); + + expected = [ + [ [ 0.0, 1.0 ], [ 0.0, 0.0 ] ], + [ [ 0.0, 1.0 ], [ 0.0, 0.0 ] ] + ]; + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + + x = zeros( 'float64', [ 2, 2, 2 ], 'row-major' ); + out = fillDiagonal( x, 1.0, [ 1, 2 ], -1 ); + + expected = [ + [ [ 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.end(); +}); + +tape( 'the function fills the diagonal when `dims` selects non-adjacent dimensions', function test( t ) { + var expected; + var out; + var x; + + x = zeros( 'float64', [ 2, 2, 3 ], 'row-major' ); + + out = fillDiagonal( x, 1.0, [ 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.end(); +}); + +tape( 'the function supports input ndarrays having a non-zero buffer offset', function test( t ) { + var expected; + var buf; + var out; + var x; + + buf = [ -1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ]; + x = new ndarray( 'generic', buf, [ 3, 3 ], [ 3, 1 ], 1, 'row-major' ); + + out = fillDiagonal( x, 1.0, [ 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.end(); +}); + +tape( 'the function normalizes negative dimension indices', function test( t ) { + var expected; + var out; + var x; + + x = zeros( 'float64', [ 3, 3 ], 'row-major' ); + + out = fillDiagonal( x, 1.0, [ -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.end(); +}); + +tape( 'the function supports column-major input ndarrays', function test( t ) { + var expected; + var out; + var x; + + x = zeros( 'float64', [ 3, 3 ], 'column-major' ); + + out = fillDiagonal( x, 1.0, [ 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.end(); +}); + +tape( 'the function fills non-trailing dimensions of higher-dimensional ndarrays', function test( t ) { + var expected; + var out; + var x; + + x = zeros( 'float64', [ 2, 3, 2 ], 'row-major' ); + + out = fillDiagonal( x, 1.0, [ 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.end(); +}); + +tape( 'the function supports negative dimension indices combined with a non-zero `k`', function test( t ) { + var expected; + var out; + var x; + + x = zeros( 'float64', [ 3, 3 ], 'row-major' ); + + out = fillDiagonal( x, 1.0, [ -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' ); + + x = zeros( 'float64', [ 3, 3 ], 'row-major' ); + out = fillDiagonal( x, 1.0, [ -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.end(); +}); + +tape( 'the function fills the diagonal of a complex128 ndarray', function test( t ) { + var expected; + var out; + var x; + + x = zeros( 'complex128', [ 3, 3 ], 'row-major' ); + + out = fillDiagonal( x, new Complex128( 1.0, 2.0 ), [ 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, x, 'returns expected value' ); + + t.end(); +}); From 45bb96b76d1b13aaea03f24556165dee172999c1 Mon Sep 17 00:00:00 2001 From: Athan Date: Sun, 3 May 2026 15:26:45 -0700 Subject: [PATCH 2/9] bench: fix descriptions Signed-off-by: Athan --- .../base/fill-diagonal/benchmark/benchmark.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/benchmark/benchmark.js index f4ce5ba7d243..7d78f86f2244 100644 --- a/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/benchmark/benchmark.js @@ -31,7 +31,7 @@ var fillDiagonal = require( './../lib' ); // MAIN // -bench( format( '%s::ndims=2,ctor=base', pkg ), function benchmark( b ) { +bench( format( '%s:ndims=2,ctor=base', pkg ), function benchmark( b ) { var values; var out; var i; @@ -59,7 +59,7 @@ bench( format( '%s::ndims=2,ctor=base', pkg ), function benchmark( b ) { b.end(); }); -bench( format( '%s::ndims=2,ctor=non-base', pkg ), function benchmark( b ) { +bench( format( '%s:ndims=2,ctor=non-base', pkg ), function benchmark( b ) { var values; var out; var i; @@ -91,7 +91,7 @@ bench( format( '%s::ndims=2,ctor=non-base', pkg ), function benchmark( b ) { b.end(); }); -bench( format( '%s::ndims=3,ctor=base', pkg ), function benchmark( b ) { +bench( format( '%s:ndims=3,ctor=base', pkg ), function benchmark( b ) { var values; var out; var i; @@ -119,7 +119,7 @@ bench( format( '%s::ndims=3,ctor=base', pkg ), function benchmark( b ) { b.end(); }); -bench( format( '%s::ndims=3,ctor=non-base', pkg ), function benchmark( b ) { +bench( format( '%s:ndims=3,ctor=non-base', pkg ), function benchmark( b ) { var values; var out; var i; @@ -151,7 +151,7 @@ bench( format( '%s::ndims=3,ctor=non-base', pkg ), function benchmark( b ) { b.end(); }); -bench( format( '%s::ndims=4,ctor=base', pkg ), function benchmark( b ) { +bench( format( '%s:ndims=4,ctor=base', pkg ), function benchmark( b ) { var values; var out; var i; @@ -179,7 +179,7 @@ bench( format( '%s::ndims=4,ctor=base', pkg ), function benchmark( b ) { b.end(); }); -bench( format( '%s::ndims=4,ctor=non-base', pkg ), function benchmark( b ) { +bench( format( '%s:ndims=4,ctor=non-base', pkg ), function benchmark( b ) { var values; var out; var i; @@ -211,7 +211,7 @@ bench( format( '%s::ndims=4,ctor=non-base', pkg ), function benchmark( b ) { b.end(); }); -bench( format( '%s::ndims=5,ctor=base', pkg ), function benchmark( b ) { +bench( format( '%s:ndims=5,ctor=base', pkg ), function benchmark( b ) { var values; var out; var i; @@ -239,7 +239,7 @@ bench( format( '%s::ndims=5,ctor=base', pkg ), function benchmark( b ) { b.end(); }); -bench( format( '%s::ndims=5,ctor=non-base', pkg ), function benchmark( b ) { +bench( format( '%s:ndims=5,ctor=non-base', pkg ), function benchmark( b ) { var values; var out; var i; From f0b922824485be84b92d89b9dfa72c75a847b04f Mon Sep 17 00:00:00 2001 From: Athan Date: Sun, 3 May 2026 15:29:18 -0700 Subject: [PATCH 3/9] Apply suggestions from code review Co-authored-by: Athan Signed-off-by: Athan --- .../@stdlib/ndarray/base/fill-diagonal/docs/types/index.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/docs/types/index.d.ts index 1c4e42d059d7..4a952d53c0f3 100644 --- a/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/docs/types/index.d.ts @@ -40,9 +40,9 @@ import { typedndarray } from '@stdlib/types/ndarray'; * @returns input ndarray * * @example -* var zeros = require( '@stdlib/ndarray/base/zeros' ); +* var zeros = require( '@stdlib/ndarray/zeros' ); * -* var x = zeros( 'float64', [ 3, 3 ], 'row-major' ); +* var x = zeros( [ 3, 3 ] ); * // returns [ [ 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0 ] ] * * var out = fillDiagonal( x, 1.0, [ 0, 1 ], 0 ); From f525580da512a1e9536f87e43f7967542f4fefe5 Mon Sep 17 00:00:00 2001 From: Athan Date: Sun, 3 May 2026 15:32:58 -0700 Subject: [PATCH 4/9] Apply suggestions from code review Co-authored-by: Athan Signed-off-by: Athan --- .../ndarray/base/fill-diagonal/docs/repl.txt | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/docs/repl.txt index d900632755a0..4f50bce4af6d 100644 --- a/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/docs/repl.txt +++ b/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/docs/repl.txt @@ -4,15 +4,15 @@ scalar value. 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. + 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 fills the main diagonal; when `k > 0`, the - function fills the diagonal above the main diagonal; and when `k < 0`, - the function fills the diagonal below the main diagonal. + The diagonal offset `k` is interpreted as `column - row`. Accordingly, when + `k = 0`, the function fills the main diagonal; when `k > 0`, the function + fills a diagonal above the main diagonal; and when `k < 0`, the function + fills a diagonal below the main diagonal. The function mutates the input ndarray in-place. @@ -41,10 +41,10 @@ Examples -------- - > var x = {{alias:@stdlib/ndarray/base/zeros}}( 'float64', [ 3, 3 ], 'row-major' ) - + > var x = {{alias:@stdlib/ndarray/zeros}}( [ 3, 3 ] ) + [ [ 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0 ] ] > var out = {{alias}}( x, 1.0, [ 0, 1 ], 0 ) - + [ [ 1.0, 0.0, 0.0 ], [ 0.0, 1.0, 0.0 ], [ 0.0, 0.0, 1.0 ] ] > var bool = ( out === x ) true From 48119146408d9a1d7026d8ab10335bcc7f5b7c43 Mon Sep 17 00:00:00 2001 From: Athan Date: Sun, 3 May 2026 15:34:21 -0700 Subject: [PATCH 5/9] Apply suggestions from code review Co-authored-by: Athan Signed-off-by: Athan --- .../ndarray/base/fill-diagonal/examples/index.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/examples/index.js b/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/examples/index.js index 277023009e54..341ed79fb4b6 100644 --- a/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/examples/index.js +++ b/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/examples/index.js @@ -18,21 +18,21 @@ 'use strict'; -var zeros = require( '@stdlib/ndarray/base/zeros' ); +var zeros = require( '@stdlib/ndarray/zeros' ); var ndarray2array = require( '@stdlib/ndarray/to-array' ); var fillDiagonal = require( './../lib' ); // Create a stack of zero matrices: -var x = zeros( 'float64', [ 2, 3, 3 ], 'row-major' ); +var x = zeros( [ 2, 3, 3 ] ); -// Fill the main diagonals: +// Fill main diagonals: fillDiagonal( x, 1.0, [ 1, 2 ], 0 ); console.log( ndarray2array( x ) ); -// Fill the super-diagonals: +// Fill super-diagonals: fillDiagonal( x, 2.0, [ 1, 2 ], 1 ); console.log( ndarray2array( x ) ); -// Fill the sub-diagonals: +// Fill sub-diagonals: fillDiagonal( x, 3.0, [ 1, 2 ], -1 ); console.log( ndarray2array( x ) ); From e1f4133aa7aca998563d4918e55e4bf7a9f9c7c6 Mon Sep 17 00:00:00 2001 From: Athan Date: Sun, 3 May 2026 15:34:39 -0700 Subject: [PATCH 6/9] Apply suggestions from code review Co-authored-by: Athan Signed-off-by: Athan --- .../@stdlib/ndarray/base/fill-diagonal/examples/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/examples/index.js b/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/examples/index.js index 341ed79fb4b6..9fc692ce9590 100644 --- a/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/examples/index.js +++ b/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/examples/index.js @@ -22,7 +22,7 @@ var zeros = require( '@stdlib/ndarray/zeros' ); var ndarray2array = require( '@stdlib/ndarray/to-array' ); var fillDiagonal = require( './../lib' ); -// Create a stack of zero matrices: +// Create a stack of matrices: var x = zeros( [ 2, 3, 3 ] ); // Fill main diagonals: From b296cafcbe0da3782b98d643c58a39514a114f9b Mon Sep 17 00:00:00 2001 From: Athan Date: Sun, 3 May 2026 16:44:28 -0700 Subject: [PATCH 7/9] Apply suggestions from code review Co-authored-by: Athan Signed-off-by: Athan --- .../@stdlib/ndarray/base/fill-diagonal/README.md | 16 ++++++++-------- .../ndarray/base/fill-diagonal/lib/index.js | 4 ++-- .../ndarray/base/fill-diagonal/lib/main.js | 4 ++-- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/README.md b/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/README.md index d2df1a3c4d70..cb7e31de2619 100644 --- a/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/README.md +++ b/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/README.md @@ -58,9 +58,9 @@ var fillDiagonal = require( '@stdlib/ndarray/base/fill-diagonal' ); Fills a specified diagonal of a matrix (or stack of matrices) with a scalar value. ```javascript -var zeros = require( '@stdlib/ndarray/base/zeros' ); +var zeros = require( '@stdlib/ndarray/zeros' ); -var x = zeros( 'float64', [ 3, 3 ], 'row-major' ); +var x = zeros( [ 3, 3 ] ); // returns [ [ 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0 ] ] var out = fillDiagonal( x, 1.0, [ 0, 1 ], 0 ); @@ -103,22 +103,22 @@ The function accepts the following arguments: ```javascript -var zeros = require( '@stdlib/ndarray/base/zeros' ); +var zeros = require( '@stdlib/ndarray/zeros' ); var ndarray2array = require( '@stdlib/ndarray/to-array' ); var fillDiagonal = require( '@stdlib/ndarray/base/fill-diagonal' ); -// Create a stack of zero matrices: -var x = zeros( 'float64', [ 2, 3, 3 ], 'row-major' ); +// Create a stack of matrices: +var x = zeros( [ 2, 3, 3 ] ); -// Fill the main diagonals: +// Fill main diagonals: fillDiagonal( x, 1.0, [ 1, 2 ], 0 ); console.log( ndarray2array( x ) ); -// Fill the super-diagonals: +// Fill super-diagonals: fillDiagonal( x, 2.0, [ 1, 2 ], 1 ); console.log( ndarray2array( x ) ); -// Fill the sub-diagonals: +// Fill sub-diagonals: fillDiagonal( x, 3.0, [ 1, 2 ], -1 ); console.log( ndarray2array( x ) ); ``` diff --git a/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/lib/index.js b/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/lib/index.js index 662161495a57..4bdbef3d2f30 100644 --- a/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/lib/index.js +++ b/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/lib/index.js @@ -24,10 +24,10 @@ * @module @stdlib/ndarray/base/fill-diagonal * * @example -* var zeros = require( '@stdlib/ndarray/base/zeros' ); +* var zeros = require( '@stdlib/ndarray/zeros' ); * var fillDiagonal = require( '@stdlib/ndarray/base/fill-diagonal' ); * -* var x = zeros( 'float64', [ 3, 3 ], 'row-major' ); +* var x = zeros( [ 3, 3 ] ); * // returns [ [ 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0 ] ] * * var out = fillDiagonal( x, 1.0, [ 0, 1 ], 0 ); diff --git a/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/lib/main.js b/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/lib/main.js index 6ec55778378b..571a2628bde8 100644 --- a/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/lib/main.js +++ b/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/lib/main.js @@ -48,9 +48,9 @@ var fill = require( '@stdlib/ndarray/base/fill' ); * @returns {ndarray} input ndarray * * @example -* var zeros = require( '@stdlib/ndarray/base/zeros' ); +* var zeros = require( '@stdlib/ndarray/zeros' ); * -* var x = zeros( 'float64', [ 3, 3 ], 'row-major' ); +* var x = zeros( [ 3, 3 ] ); * // returns [ [ 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0 ] ] * * var out = fillDiagonal( x, 1.0, [ 0, 1 ], 0 ); From 25619d65fe2e5b743f1e9fbbc8ca9e7914ca2fdb Mon Sep 17 00:00:00 2001 From: Athan Date: Sun, 3 May 2026 16:49:10 -0700 Subject: [PATCH 8/9] Apply suggestions from code review Co-authored-by: Athan Signed-off-by: Athan --- lib/node_modules/@stdlib/ndarray/base/fill-diagonal/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/README.md b/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/README.md index cb7e31de2619..e03acbcac61d 100644 --- a/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/README.md +++ b/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/README.md @@ -26,13 +26,13 @@ limitations under the License. For an `M`-by-`N` matrix `A`, the `k`-th diagonal is defined as - + ```math D_k = \{\, A_{i,j} : j - i = k \,\} ``` - From c3d5a670737e4f86a896a1347894d984fde7fdb8 Mon Sep 17 00:00:00 2001 From: Athan Date: Sun, 3 May 2026 16:52:18 -0700 Subject: [PATCH 9/9] docs: remove expected values Signed-off-by: Athan --- .../@stdlib/ndarray/base/fill-diagonal/docs/repl.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/docs/repl.txt index 4f50bce4af6d..39a3b54f18a2 100644 --- a/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/docs/repl.txt +++ b/lib/node_modules/@stdlib/ndarray/base/fill-diagonal/docs/repl.txt @@ -42,7 +42,7 @@ Examples -------- > var x = {{alias:@stdlib/ndarray/zeros}}( [ 3, 3 ] ) - [ [ 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0 ] ] + > var out = {{alias}}( x, 1.0, [ 0, 1 ], 0 ) [ [ 1.0, 0.0, 0.0 ], [ 0.0, 1.0, 0.0 ], [ 0.0, 0.0, 1.0 ] ] > var bool = ( out === x )