From 79d02c82f5e4118920a54ad138463b593659df0d Mon Sep 17 00:00:00 2001 From: headlessNode Date: Thu, 25 Jun 2026 17:29:59 +0500 Subject: [PATCH 1/2] feat: add blas/ext/base/gfill-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_pkg_readmes status: passed - task: lint_markdown_docs status: na - task: lint_markdown status: na - 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 --- --- .../blas/ext/base/gfill-diagonal/README.md | 170 +++++ .../gfill-diagonal/benchmark/benchmark.js | 116 ++++ .../benchmark/benchmark.ndarray.js | 122 ++++ .../ext/base/gfill-diagonal/docs/repl.txt | 111 +++ .../base/gfill-diagonal/docs/types/index.d.ts | 146 ++++ .../base/gfill-diagonal/docs/types/test.ts | 218 ++++++ .../ext/base/gfill-diagonal/examples/index.js | 30 + .../ext/base/gfill-diagonal/lib/accessors.js | 131 ++++ .../blas/ext/base/gfill-diagonal/lib/index.js | 57 ++ .../blas/ext/base/gfill-diagonal/lib/main.js | 84 +++ .../ext/base/gfill-diagonal/lib/ndarray.js | 121 ++++ .../blas/ext/base/gfill-diagonal/package.json | 69 ++ .../blas/ext/base/gfill-diagonal/test/test.js | 38 ++ .../ext/base/gfill-diagonal/test/test.main.js | 351 ++++++++++ .../base/gfill-diagonal/test/test.ndarray.js | 630 ++++++++++++++++++ 15 files changed, 2394 insertions(+) create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/README.md create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/benchmark/benchmark.ndarray.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/examples/index.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/lib/accessors.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/lib/index.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/lib/main.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/lib/ndarray.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/package.json create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/test/test.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/test/test.main.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/test/test.ndarray.js diff --git a/lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/README.md b/lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/README.md new file mode 100644 index 000000000000..74821fbfb864 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/README.md @@ -0,0 +1,170 @@ + + +# gfill-diagonal + +> Fill the diagonal of a matrix with a specified scalar constant. + +
+ +## Usage + +```javascript +var gfillDiagonal = require( '@stdlib/blas/ext/base/gfill-diagonal' ); +``` + +#### gfillDiagonal( order, M, N, k, alpha, A, LDA ) + +Fills the diagonal of a matrix with a specified scalar constant. + +```javascript +var x = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + +gfillDiagonal( 'row-major', 2, 3, 0, 5.0, x, 3 ); +// x => [ 5.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] +``` + +The function has the following parameters: + +- **order**: storage layout. +- **M**: number of rows in `A`. +- **N**: number of columns in `A`. +- **k**: diagonal offset. +- **alpha**: scalar constant. +- **A**: input matrix as a linear array. +- **LDA**: stride of the first dimension of `A` (a.k.a., leading dimension of the matrix `A`). + +The `order` and `LDA` parameters determine the matrix layout. For example, to fill an upper diagonal: + +```javascript +var A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + +gfillDiagonal( 'row-major', 2, 3, 1, 7.0, A, 3 ); +// A => [ 1.0, 7.0, 3.0, 4.0, 5.0, 7.0 ] +``` + +Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views. + +```javascript +var Float64Array = require( '@stdlib/array/float64' ); + +// Initial matrix... +var A0 = new Float64Array( [ 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + +// Create an offset view... +var A1 = new Float64Array( A0.buffer, A0.BYTES_PER_ELEMENT*1 ); // start at 2nd element + +// Fill main diagonal... +gfillDiagonal( 'row-major', 2, 3, 0, 8.0, A1, 3 ); +// A0 => [ 0.0, 8.0, 2.0, 3.0, 4.0, 8.0, 6.0 ] +``` + +#### gfillDiagonal.ndarray( M, N, k, alpha, A, strideA1, strideA2, offsetA ) + +Fills the diagonal of a matrix with a specified scalar constant using alternative indexing semantics. + +```javascript +var A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + +gfillDiagonal.ndarray( 2, 3, 0, 5.0, A, 3, 1, 0 ); +// A => [ 5.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] +``` + +The function has the following parameters: + +- **M**: number of rows in `A`. +- **N**: number of columns in `A`. +- **k**: diagonal offset. +- **alpha**: scalar constant. +- **A**: input matrix as a linear array. +- **strideA1**: stride of the first dimension of `A`. +- **strideA2**: stride of the second dimension of `A`. +- **offsetA**: starting index for `A`. + +While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameter supports indexing semantics based on a starting index. For example, with an offset: + +```javascript +var A = [ 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + +gfillDiagonal.ndarray( 2, 3, 0, 8.0, A, 3, 1, 1 ); +// A => [ 0.0, 8.0, 2.0, 3.0, 4.0, 8.0, 6.0 ] +``` + +
+ + + +
+ +## Notes + +- If `M <= 0` or `N <= 0`, both functions return `A` unchanged. +- Both functions support array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor]). + +
+ + + +
+ +## Examples + + + +```javascript +var uniform = require( '@stdlib/random/array/uniform' ); +var gfillDiagonal = require( '@stdlib/blas/ext/base/gfill-diagonal' ); + +var A = uniform( 6, -100, 100, { + 'dtype': 'float64' +}); +console.log( A ); + +gfillDiagonal( 'row-major', 2, 3, 0, 5.0, A, 3 ); +console.log( A ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/benchmark/benchmark.js b/lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/benchmark/benchmark.js new file mode 100644 index 000000000000..fdc302038008 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/benchmark/benchmark.js @@ -0,0 +1,116 @@ +/** +* @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 uniform = require( '@stdlib/random/array/uniform' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var floor = require( '@stdlib/math/base/special/floor' ); +var sqrt = require( '@stdlib/math/base/special/sqrt' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var gfillDiagonal = require( './../lib/main.js' ); + + +// VARIABLES // + +var options = { + 'dtype': 'generic' +}; + +var LAYOUTS = [ + 'row-major', + 'column-major' +]; + + +// FUNCTIONS // + +/** +* Create a benchmark function. +* +* @private +* @param {string} order - storage layout order +* @param {PositiveInteger} size - matrix size (N where matrix is NxN) +* @returns {Function} benchmark function +*/ +function createBenchmark( order, size ) { + var A = uniform( size * size, -100, 100, options ); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var y; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + y = gfillDiagonal( order, size, size, 0, i, A, size ); + if ( isnan( y[ 0 ] ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( y[ 0 ] ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var size; + var min; + var max; + var ord; + var f; + var i; + var k; + + min = 1; // 10^min + max = 3; // 10^max + + for ( k = 0; k < LAYOUTS.length; k++ ) { + ord = LAYOUTS[ k ]; + for ( i = min; i <= max; i++ ) { + size = floor( sqrt( pow( 10, i ) ) ); + f = createBenchmark( ord, size ); + bench( format( '%s::square_matrix:order=%s,size=%d', pkg, ord, size*size ), f ); + } + } +} + +main(); diff --git a/lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/benchmark/benchmark.ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/benchmark/benchmark.ndarray.js new file mode 100644 index 000000000000..2d37e42a4dba --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/benchmark/benchmark.ndarray.js @@ -0,0 +1,122 @@ +/** +* @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 isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var floor = require( '@stdlib/math/base/special/floor' ); +var zeros = require( '@stdlib/array/zeros' ); +var isColumnMajor = require( '@stdlib/ndarray/base/assert/is-column-major-string' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var gfillDiagonal = require( './../lib/ndarray.js' ); + + +// VARIABLES // + +var LAYOUTS = [ + 'row-major', + 'column-major' +]; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {string} order - storage layout +* @param {PositiveInteger} N - number of elements along each dimension +* @returns {Function} benchmark function +*/ +function createBenchmark( order, N ) { + var A = zeros( N*N, 'generic' ); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var sa1; + var sa2; + var y; + var i; + + if ( isColumnMajor( order ) ) { + sa1 = 1; + sa2 = N; + } else { // order === 'row-major' + sa1 = N; + sa2 = 1; + } + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + y = gfillDiagonal( N, N, 0, i, A, sa1, sa2, 0 ); + if ( isnan( y[ 0 ] ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( y[ 0 ] ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var min; + var max; + var ord; + var N; + var f; + var i; + var k; + + min = 1; // 10^min + max = 3; // 10^max + + for ( k = 0; k < LAYOUTS.length; k++ ) { + ord = LAYOUTS[ k ]; + for ( i = min; i <= max; i++ ) { + N = floor( pow( pow( 10, i ), 1.0/2.0 ) ); + f = createBenchmark( ord, N ); + bench( format( '%s::square_matrix:ndarray:order=%s,size=%d', pkg, ord, N*N ), f ); + } + } +} + +main(); diff --git a/lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/docs/repl.txt b/lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/docs/repl.txt new file mode 100644 index 000000000000..a23ed9c5dd19 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/docs/repl.txt @@ -0,0 +1,111 @@ + +{{alias}}( order, M, N, k, alpha, A, LDA ) + Fills the diagonal of a matrix with a specified scalar constant. + + The `order` parameter specifies the storage layout of the matrix. + + The `k` parameter determines which diagonal to fill: 0 for the main + diagonal, positive values for upper diagonals, and negative values for + lower diagonals. + + If `M <= 0` or `N <= 0`, the function returns `A` unchanged. + + Parameters + ---------- + order: string + Storage layout. + + M: integer + Number of rows in `A`. + + N: integer + Number of columns in `A`. + + k: integer + Diagonal offset (0 = main diagonal, >0 = upper, <0 = lower). + + alpha: any + Scalar constant. + + A: ArrayLikeObject + Input matrix as a linear array. + + LDA: integer + Stride of the first dimension of `A` (a.k.a., leading dimension of + the matrix `A`). + + Returns + ------- + A: ArrayLikeObject + Input matrix. + + Examples + -------- + // Standard Usage: + > var A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + > {{alias}}( 'row-major', 2, 3, 0, 7.0, A, 3 ) + [ 7.0, 2.0, 3.0, 4.0, 7.0, 6.0 ] + + // Filling upper diagonal: + > A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + > {{alias}}( 'row-major', 2, 3, 1, 8.0, A, 3 ) + [ 1.0, 8.0, 3.0, 4.0, 5.0, 8.0 ] + + // Filling lower diagonal: + > A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + > {{alias}}( 'row-major', 2, 3, -1, 9.0, A, 3 ) + [ 1.0, 2.0, 3.0, 9.0, 5.0, 6.0 ] + + +{{alias}}.ndarray( M, N, k, alpha, A, strideA1, strideA2, offsetA ) + Fills the diagonal of a matrix with a specified scalar constant using + alternative indexing semantics. + + While the main interface uses LDA (leading dimension), this method accepts + explicit strides and offsets. + + Parameters + ---------- + M: integer + Number of rows in `A`. + + N: integer + Number of columns in `A`. + + k: integer + Diagonal offset. + + alpha: any + Scalar constant. + + A: ArrayLikeObject + Input matrix as a linear array. + + strideA1: integer + Stride of the first dimension of `A`. + + strideA2: integer + Stride of the second dimension of `A`. + + offsetA: integer + Starting index for `A`. + + Returns + ------- + A: ArrayLikeObject + Input matrix. + + Examples + -------- + // Standard Usage: + > var A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + > {{alias}}.ndarray( 2, 3, 0, 7.0, A, 3, 1, 0 ) + [ 7.0, 2.0, 3.0, 4.0, 7.0, 6.0 ] + + // Using an offset: + > A = [ 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + > {{alias}}.ndarray( 2, 3, 0, 8.0, A, 3, 1, 1 ) + [ 0.0, 8.0, 2.0, 3.0, 4.0, 8.0, 6.0 ] + + See Also + -------- diff --git a/lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/docs/types/index.d.ts b/lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/docs/types/index.d.ts new file mode 100644 index 000000000000..e861f9b8213d --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/docs/types/index.d.ts @@ -0,0 +1,146 @@ +/* +* @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, AccessorArrayLike } from '@stdlib/types/array'; +import { Layout } from '@stdlib/types/blas'; + +/** +* Interface describing `gfillDiagonal`. +*/ +interface Routine { + /** + * Fills the diagonal of a matrix with a specified scalar constant. + * + * @param order - storage layout + * @param M - number of rows in `A` + * @param N - number of columns in `A` + * @param k - diagonal offset + * @param alpha - scalar constant + * @param A - input matrix + * @param LDA - stride of the first dimension of `A` + * @returns `A` + * + * @example + * var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); + * + * var A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + * + * gfillDiagonal( 'row-major', 2, 3, 0, 5.0, toAccessorArray( A ), 3 ); + * // A => [ 5.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] + */ + ( order: Layout, M: number, N: number, k: number, alpha: T, A: AccessorArrayLike, LDA: number ): AccessorArrayLike; + + /** + * Fills the diagonal of a matrix with a specified scalar constant. + * + * @param order - storage layout + * @param M - number of rows in `A` + * @param N - number of columns in `A` + * @param k - diagonal offset + * @param alpha - scalar constant + * @param A - input matrix + * @param LDA - stride of the first dimension of `A` + * @returns `A` + * + * @example + * var A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + * + * gfillDiagonal( 'row-major', 2, 3, 0, 5.0, A, 3 ); + * // A => [ 5.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] + */ + ( order: Layout, M: number, N: number, k: number, alpha: T, A: Collection, LDA: number ): Collection; + + /** + * Fills the diagonal of a matrix with a specified scalar constant using alternative indexing semantics. + * + * @param M - number of rows in `A` + * @param N - number of columns in `A` + * @param k - diagonal offset + * @param alpha - scalar constant + * @param A - input matrix as a linear array + * @param strideA1 - stride of the first dimension of `A` + * @param strideA2 - stride of the second dimension of `A` + * @param offsetA - starting index for `A` + * @returns `A` + * + * @example + * var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); + * + * var A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + * + * gfillDiagonal.ndarray( 2, 3, 0, 5.0, toAccessorArray( A ), 3, 1, 0 ); + * // A => [ 5.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] + */ + ndarray( M: number, N: number, k: number, alpha: T, A: AccessorArrayLike, strideA1: number, strideA2: number, offsetA: number ): AccessorArrayLike; + + /** + * Fills the diagonal of a matrix with a specified scalar constant using alternative indexing semantics. + * + * @param M - number of rows in `A` + * @param N - number of columns in `A` + * @param k - diagonal offset + * @param alpha - scalar constant + * @param A - input matrix as a linear array + * @param strideA1 - stride of the first dimension of `A` + * @param strideA2 - stride of the second dimension of `A` + * @param offsetA - starting index for `A` + * @returns `A` + * + * @example + * var A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + * + * gfillDiagonal.ndarray( 2, 3, 0, 5.0, A, 3, 1, 0 ); + * // A => [ 5.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] + */ + ndarray( M: number, N: number, k: number, alpha: T, A: Collection, strideA1: number, strideA2: number, offsetA: number ): Collection; +} + +/** +* Fills the diagonal of a matrix with a specified scalar constant. +* +* @param order - storage layout +* @param M - number of rows in `A` +* @param N - number of columns in `A` +* @param k - diagonal offset +* @param alpha - scalar constant +* @param A - input matrix +* @param LDA - stride of the first dimension of `A` +* @returns `A` +* +* @example +* var A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; +* +* gfillDiagonal( 'row-major', 2, 3, 0, 5.0, A, 3 ); +* // A => [ 5.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] +* +* @example +* var A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; +* +* gfillDiagonal.ndarray( 2, 3, 0, 5.0, A, 3, 1, 0 ); +* // A => [ 5.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] +*/ +declare var gfillDiagonal: Routine; + + +// EXPORTS // + +export = gfillDiagonal; diff --git a/lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/docs/types/test.ts b/lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/docs/types/test.ts new file mode 100644 index 000000000000..0ff88958a4d3 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/docs/types/test.ts @@ -0,0 +1,218 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import AccessorArray = require( '@stdlib/array/base/accessor' ); +import gfillDiagonal = require( './index' ); + + +// TESTS // + +// The function returns a collection... +{ + const A = new Float64Array( 6 ); + + gfillDiagonal( 'row-major', 2, 3, 0, 5.0, A, 3 ); // $ExpectType Collection + gfillDiagonal( 'column-major', 2, 3, 0, 5.0, new AccessorArray( A ), 2 ); // $ExpectType AccessorArrayLike +} + +// The compiler throws an error if the function is provided a first argument which is not a string... +{ + const A = new Float64Array( 6 ); + + gfillDiagonal( 10, 2, 3, 0, 5.0, A, 3 ); // $ExpectError + gfillDiagonal( true, 2, 3, 0, 5.0, A, 3 ); // $ExpectError + gfillDiagonal( null, 2, 3, 0, 5.0, A, 3 ); // $ExpectError + gfillDiagonal( undefined, 2, 3, 0, 5.0, A, 3 ); // $ExpectError + gfillDiagonal( [], 2, 3, 0, 5.0, A, 3 ); // $ExpectError + gfillDiagonal( {}, 2, 3, 0, 5.0, A, 3 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not a number... +{ + const A = new Float64Array( 6 ); + + gfillDiagonal( 'row-major', '2', 3, 0, 5.0, A, 3 ); // $ExpectError + gfillDiagonal( 'row-major', true, 3, 0, 5.0, A, 3 ); // $ExpectError + gfillDiagonal( 'row-major', null, 3, 0, 5.0, A, 3 ); // $ExpectError + gfillDiagonal( 'row-major', undefined, 3, 0, 5.0, A, 3 ); // $ExpectError + gfillDiagonal( 'row-major', [], 3, 0, 5.0, A, 3 ); // $ExpectError + gfillDiagonal( 'row-major', {}, 3, 0, 5.0, A, 3 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a third argument which is not a number... +{ + const A = new Float64Array( 6 ); + + gfillDiagonal( 'row-major', 2, '3', 0, 5.0, A, 3 ); // $ExpectError + gfillDiagonal( 'row-major', 2, true, 0, 5.0, A, 3 ); // $ExpectError + gfillDiagonal( 'row-major', 2, null, 0, 5.0, A, 3 ); // $ExpectError + gfillDiagonal( 'row-major', 2, undefined, 0, 5.0, A, 3 ); // $ExpectError + gfillDiagonal( 'row-major', 2, [], 0, 5.0, A, 3 ); // $ExpectError + gfillDiagonal( 'row-major', 2, {}, 0, 5.0, A, 3 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a fourth argument which is not a number... +{ + const A = new Float64Array( 6 ); + + gfillDiagonal( 'row-major', 2, 3, '0', 5.0, A, 3 ); // $ExpectError + gfillDiagonal( 'row-major', 2, 3, true, 5.0, A, 3 ); // $ExpectError + gfillDiagonal( 'row-major', 2, 3, null, 5.0, A, 3 ); // $ExpectError + gfillDiagonal( 'row-major', 2, 3, undefined, 5.0, A, 3 ); // $ExpectError + gfillDiagonal( 'row-major', 2, 3, [], 5.0, A, 3 ); // $ExpectError + gfillDiagonal( 'row-major', 2, 3, {}, 5.0, A, 3 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a sixth argument which is not a collection... +{ + gfillDiagonal( 'row-major', 2, 3, 0, 5.0, 10, 3 ); // $ExpectError + gfillDiagonal( 'row-major', 2, 3, 0, 5.0, true, 3 ); // $ExpectError + gfillDiagonal( 'row-major', 2, 3, 0, 5.0, null, 3 ); // $ExpectError + gfillDiagonal( 'row-major', 2, 3, 0, 5.0, undefined, 3 ); // $ExpectError + gfillDiagonal( 'row-major', 2, 3, 0, 5.0, {}, 3 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a seventh argument which is not a number... +{ + const A = new Float64Array( 6 ); + + gfillDiagonal( 'row-major', 2, 3, 0, 5.0, A, '3' ); // $ExpectError + gfillDiagonal( 'row-major', 2, 3, 0, 5.0, A, true ); // $ExpectError + gfillDiagonal( 'row-major', 2, 3, 0, 5.0, A, null ); // $ExpectError + gfillDiagonal( 'row-major', 2, 3, 0, 5.0, A, undefined ); // $ExpectError + gfillDiagonal( 'row-major', 2, 3, 0, 5.0, A, [] ); // $ExpectError + gfillDiagonal( 'row-major', 2, 3, 0, 5.0, A, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const A = new Float64Array( 6 ); + + gfillDiagonal(); // $ExpectError + gfillDiagonal( 'row-major' ); // $ExpectError + gfillDiagonal( 'row-major', 2 ); // $ExpectError + gfillDiagonal( 'row-major', 2, 3 ); // $ExpectError + gfillDiagonal( 'row-major', 2, 3, 0 ); // $ExpectError + gfillDiagonal( 'row-major', 2, 3, 0, 5.0 ); // $ExpectError + gfillDiagonal( 'row-major', 2, 3, 0, 5.0, A ); // $ExpectError + gfillDiagonal( 'row-major', 2, 3, 0, 5.0, A, 3, 10 ); // $ExpectError +} + +// Attached to main export is an `ndarray` method which returns a collection... +{ + const A = new Float64Array( 6 ); + + gfillDiagonal.ndarray( 2, 3, 0, 5.0, A, 3, 1, 0 ); // $ExpectType Collection + gfillDiagonal.ndarray( 2, 3, 0, 5.0, new AccessorArray( A ), 3, 1, 0 ); // $ExpectType AccessorArrayLike +} + +// The compiler throws an error if the `ndarray` method is provided a first argument which is not a number... +{ + const A = new Float64Array( 6 ); + + gfillDiagonal.ndarray( '2', 3, 0, 5.0, A, 3, 1, 0 ); // $ExpectError + gfillDiagonal.ndarray( true, 3, 0, 5.0, A, 3, 1, 0 ); // $ExpectError + gfillDiagonal.ndarray( null, 3, 0, 5.0, A, 3, 1, 0 ); // $ExpectError + gfillDiagonal.ndarray( undefined, 3, 0, 5.0, A, 3, 1, 0 ); // $ExpectError + gfillDiagonal.ndarray( [], 3, 0, 5.0, A, 3, 1, 0 ); // $ExpectError + gfillDiagonal.ndarray( {}, 3, 0, 5.0, A, 3, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a second argument which is not a number... +{ + const A = new Float64Array( 6 ); + + gfillDiagonal.ndarray( 2, '3', 0, 5.0, A, 3, 1, 0 ); // $ExpectError + gfillDiagonal.ndarray( 2, true, 0, 5.0, A, 3, 1, 0 ); // $ExpectError + gfillDiagonal.ndarray( 2, null, 0, 5.0, A, 3, 1, 0 ); // $ExpectError + gfillDiagonal.ndarray( 2, undefined, 0, 5.0, A, 3, 1, 0 ); // $ExpectError + gfillDiagonal.ndarray( 2, [], 0, 5.0, A, 3, 1, 0 ); // $ExpectError + gfillDiagonal.ndarray( 2, {}, 0, 5.0, A, 3, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a third argument which is not a number... +{ + const A = new Float64Array( 6 ); + + gfillDiagonal.ndarray( 2, 3, '0', 5.0, A, 3, 1, 0 ); // $ExpectError + gfillDiagonal.ndarray( 2, 3, true, 5.0, A, 3, 1, 0 ); // $ExpectError + gfillDiagonal.ndarray( 2, 3, null, 5.0, A, 3, 1, 0 ); // $ExpectError + gfillDiagonal.ndarray( 2, 3, undefined, 5.0, A, 3, 1, 0 ); // $ExpectError + gfillDiagonal.ndarray( 2, 3, [], 5.0, A, 3, 1, 0 ); // $ExpectError + gfillDiagonal.ndarray( 2, 3, {}, 5.0, A, 3, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a fifth argument which is not a collection... +{ + gfillDiagonal.ndarray( 2, 3, 0, 5.0, 10, 3, 1, 0 ); // $ExpectError + gfillDiagonal.ndarray( 2, 3, 0, 5.0, true, 3, 1, 0 ); // $ExpectError + gfillDiagonal.ndarray( 2, 3, 0, 5.0, null, 3, 1, 0 ); // $ExpectError + gfillDiagonal.ndarray( 2, 3, 0, 5.0, undefined, 3, 1, 0 ); // $ExpectError + gfillDiagonal.ndarray( 2, 3, 0, 5.0, {}, 3, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a sixth argument which is not a number... +{ + const A = new Float64Array( 6 ); + + gfillDiagonal.ndarray( 2, 3, 0, 5.0, A, '3', 1, 0 ); // $ExpectError + gfillDiagonal.ndarray( 2, 3, 0, 5.0, A, true, 1, 0 ); // $ExpectError + gfillDiagonal.ndarray( 2, 3, 0, 5.0, A, null, 1, 0 ); // $ExpectError + gfillDiagonal.ndarray( 2, 3, 0, 5.0, A, undefined, 1, 0 ); // $ExpectError + gfillDiagonal.ndarray( 2, 3, 0, 5.0, A, [], 1, 0 ); // $ExpectError + gfillDiagonal.ndarray( 2, 3, 0, 5.0, A, {}, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a seventh argument which is not a number... +{ + const A = new Float64Array( 6 ); + + gfillDiagonal.ndarray( 2, 3, 0, 5.0, A, 3, '1', 0 ); // $ExpectError + gfillDiagonal.ndarray( 2, 3, 0, 5.0, A, 3, true, 0 ); // $ExpectError + gfillDiagonal.ndarray( 2, 3, 0, 5.0, A, 3, null, 0 ); // $ExpectError + gfillDiagonal.ndarray( 2, 3, 0, 5.0, A, 3, undefined, 0 ); // $ExpectError + gfillDiagonal.ndarray( 2, 3, 0, 5.0, A, 3, [], 0 ); // $ExpectError + gfillDiagonal.ndarray( 2, 3, 0, 5.0, A, 3, {}, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided an eighth argument which is not a number... +{ + const A = new Float64Array( 6 ); + + gfillDiagonal.ndarray( 2, 3, 0, 5.0, A, 3, 1, '0' ); // $ExpectError + gfillDiagonal.ndarray( 2, 3, 0, 5.0, A, 3, 1, true ); // $ExpectError + gfillDiagonal.ndarray( 2, 3, 0, 5.0, A, 3, 1, null ); // $ExpectError + gfillDiagonal.ndarray( 2, 3, 0, 5.0, A, 3, 1, undefined ); // $ExpectError + gfillDiagonal.ndarray( 2, 3, 0, 5.0, A, 3, 1, [] ); // $ExpectError + gfillDiagonal.ndarray( 2, 3, 0, 5.0, A, 3, 1, {} ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided an unsupported number of arguments... +{ + const A = new Float64Array( 6 ); + + gfillDiagonal.ndarray(); // $ExpectError + gfillDiagonal.ndarray( 2 ); // $ExpectError + gfillDiagonal.ndarray( 2, 3 ); // $ExpectError + gfillDiagonal.ndarray( 2, 3, 0 ); // $ExpectError + gfillDiagonal.ndarray( 2, 3, 0, 5.0 ); // $ExpectError + gfillDiagonal.ndarray( 2, 3, 0, 5.0, A ); // $ExpectError + gfillDiagonal.ndarray( 2, 3, 0, 5.0, A, 3 ); // $ExpectError + gfillDiagonal.ndarray( 2, 3, 0, 5.0, A, 3, 1 ); // $ExpectError + gfillDiagonal.ndarray( 2, 3, 0, 5.0, A, 3, 1, 0, 10 ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/examples/index.js b/lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/examples/index.js new file mode 100644 index 000000000000..cb8a254d6a0a --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/examples/index.js @@ -0,0 +1,30 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var uniform = require( '@stdlib/random/array/uniform' ); +var gfillDiagonal = require( './../lib' ); + +var A = uniform( 6, -100, 100, { + 'dtype': 'float64' +}); +console.log( A ); + +gfillDiagonal( 'row-major', 2, 3, 0, 5.0, A, 3 ); +console.log( A ); diff --git a/lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/lib/accessors.js b/lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/lib/accessors.js new file mode 100644 index 000000000000..77388a138bbc --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/lib/accessors.js @@ -0,0 +1,131 @@ +/** +* @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 min = require( '@stdlib/math/base/special/fast/min' ); + + +// MAIN // + +/** +* Fills the diagonal of a matrix with a specified scalar constant using accessors. +* +* @private +* @param {integer} M - number of rows in `A` +* @param {integer} N - number of columns in `A` +* @param {integer} k - diagonal offset +* @param {*} alpha - scalar constant +* @param {Object} A - input matrix object +* @param {Collection} A.data - input matrix data +* @param {Array} A.accessors - array element accessors +* @param {integer} strideA1 - stride of the first dimension of `A` +* @param {integer} strideA2 - stride of the second dimension of `A` +* @param {NonNegativeInteger} offsetA - starting index for `A` +* @returns {Object} input matrix +* +* @example +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +* var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +* +* var A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; +* var out = gfillDiagonal( 2, 3, 0, 7.0, arraylike2object( toAccessorArray( A ) ), 3, 1, 0 ); +* // A => [ 7.0, 2.0, 3.0, 4.0, 7.0, 6.0 ] +* +* @example +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +* var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +* +* var A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; +* var out = gfillDiagonal( 2, 3, 0, 7.0, arraylike2object( toAccessorArray( A ) ), 1, 2, 0 ); +* // A => [ 7.0, 2.0, 3.0, 7.0, 5.0, 6.0 ] +* +* @example +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +* var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +* +* var A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; +* var out = gfillDiagonal( 2, 3, 1, 8.0, arraylike2object( toAccessorArray( A ) ), 3, 1, 0 ); +* // A => [ 1.0, 8.0, 3.0, 4.0, 5.0, 8.0 ] +* +* @example +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +* var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +* +* var A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; +* var out = gfillDiagonal( 2, 3, -1, 9.0, arraylike2object( toAccessorArray( A ) ), 3, 1, 0 ); +* // A => [ 1.0, 2.0, 3.0, 9.0, 5.0, 6.0 ] +* +* @example +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +* var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +* +* var A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ]; +* var out = gfillDiagonal( 2, 3, 0, 10.0, arraylike2object( toAccessorArray( A ) ), 4, 1, 5 ); +* // A => [ 1.0, 2.0, 3.0, 4.0, 5.0, 10.0, 7.0, 8.0, 9.0, 10.0, 10.0, 12.0 ] +*/ +function gfillDiagonal( M, N, k, alpha, A, strideA1, strideA2, offsetA ) { + var dStride; + var offset; + var nDiag; + var set; + var buf; + var ia; + var i; + + // Early return if dimensions are invalid + if ( M <= 0 || N <= 0 ) { + return A; + } + + // Cache reference to matrix data and setter accessor + buf = A.data; + set = A.accessors[ 1 ]; + + // Determine the number of elements on the diagonal + if ( k >= 0 ) { + nDiag = min( M, N - k ); + offset = offsetA + (k * strideA2); + } else { + nDiag = min( M + k, N ); + offset = offsetA - (k * strideA1); + } + + if ( nDiag <= 0 ) { + return A; + } + + // The diagonal stride is the sum of strides for moving to the next diagonal element + dStride = strideA1 + strideA2; + + // Fill the diagonal elements using the setter accessor + ia = offset; + for ( i = 0; i < nDiag; i++ ) { + set( buf, ia, alpha ); + ia += dStride; + } + + return A; +} + + +// EXPORTS // + +module.exports = gfillDiagonal; diff --git a/lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/lib/index.js b/lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/lib/index.js new file mode 100644 index 000000000000..4844e011c6af --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/lib/index.js @@ -0,0 +1,57 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Fill the diagonal of a matrix with a specified scalar constant. +* +* @module @stdlib/blas/ext/base/gfill-diagonal +* +* @example +* var gfillDiagonal = require( '@stdlib/blas/ext/base/gfill-diagonal' ); +* +* var x = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; +* +* gfillDiagonal( 'row-major', 2, 3, 0, 5.0, x, 3 ); +* // x => [ 5.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] +* +* @example +* var gfillDiagonal = require( '@stdlib/blas/ext/base/gfill-diagonal' ); +* +* var x = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; +* +* gfillDiagonal.ndarray( 2, 3, 0, 5.0, x, 3, 1, 0 ); +* // x => [ 5.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var main = require( './main.js' ); +var ndarray = require( './ndarray.js' ); + + +// MAIN // + +setReadOnly( main, 'ndarray', ndarray ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/lib/main.js b/lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/lib/main.js new file mode 100644 index 000000000000..6d4f09788d2f --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/lib/main.js @@ -0,0 +1,84 @@ +/** +* @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 isLayout = require( '@stdlib/blas/base/assert/is-layout' ); +var isColumnMajor = require( '@stdlib/ndarray/base/assert/is-column-major-string' ); +var format = require( '@stdlib/string/format' ); +var max = require( '@stdlib/math/base/special/fast/max' ); +var ndarray = require( './ndarray.js' ); + + +// MAIN // + +/** +* Fills the diagonal of a matrix with a specified scalar constant. +* +* @param {string} order - storage layout +* @param {integer} M - number of rows in `A` +* @param {integer} N - number of columns in `A` +* @param {integer} k - diagonal offset +* @param {*} alpha - scalar constant +* @param {Collection} A - input matrix +* @param {integer} LDA - stride of the first dimension of `A` (a.k.a., leading dimension of the matrix `A`) +* @throws {TypeError} first argument must be a valid order +* @throws {RangeError} seventh argument must be greater than or equal to max(1,M) for column-major or max(1,N) for row-major +* @returns {Collection} input matrix +* +* @example +* var x = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; +* +* gfillDiagonal( 'row-major', 2, 3, 0, 5.0, x, 3 ); +* // x => [ 5.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] +*/ +function gfillDiagonal( order, M, N, k, alpha, A, LDA ) { + var strideA1; + var strideA2; + var s; + + if ( !isLayout( order ) ) { + throw new TypeError( format( 'invalid argument. First argument must be a valid order. Value: `%s`.', order ) ); + } + + if ( isColumnMajor( order ) ) { + s = M; + } else { + s = N; + } + if ( LDA < max( 1, s ) ) { + throw new RangeError( format( 'invalid argument. Seventh argument must be greater than or equal to max(1,%d). Value: `%d`.', s, LDA ) ); + } + + if ( isColumnMajor( order ) ) { + strideA1 = 1; + strideA2 = LDA; + } else { // order === 'row-major' + strideA1 = LDA; + strideA2 = 1; + } + + return ndarray( M, N, k, alpha, A, strideA1, strideA2, 0 ); +} + + +// EXPORTS // + +module.exports = gfillDiagonal; diff --git a/lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/lib/ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/lib/ndarray.js new file mode 100644 index 000000000000..8c2a4369b81c --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/lib/ndarray.js @@ -0,0 +1,121 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +var min = require( '@stdlib/math/base/special/fast/min' ); +var accessors = require( './accessors.js' ); + + +// MAIN // + +/** +* Fills the diagonal of a matrix with a specified scalar constant using alternative indexing semantics. +* +* @param {integer} M - number of rows in `A` +* @param {integer} N - number of columns in `A` +* @param {integer} k - diagonal offset +* @param {*} alpha - scalar constant +* @param {Collection} A - input matrix +* @param {integer} strideA1 - stride of the first dimension of `A` +* @param {integer} strideA2 - stride of the second dimension of `A` +* @param {NonNegativeInteger} offsetA - starting index for `A` +* @returns {Collection} input matrix +* +* @example +* var A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; +* +* gfillDiagonal( 2, 3, 0, 5.0, A, 3, 1, 0 ); +* // A => [ 5.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] +* +* @example +* var A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; +* +* gfillDiagonal( 2, 3, 0, 7.0, A, 1, 2, 0 ); +* // A => [ 7.0, 2.0, 3.0, 7.0, 5.0, 6.0 ] +* +* @example +* var A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; +* +* gfillDiagonal( 2, 3, 1, 8.0, A, 3, 1, 0 ); +* // A => [ 1.0, 8.0, 3.0, 4.0, 5.0, 8.0 ] +* +* @example +* var A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; +* +* gfillDiagonal( 2, 3, -1, 9.0, A, 3, 1, 0 ); +* // A => [ 1.0, 2.0, 3.0, 9.0, 5.0, 6.0 ] +* +* @example +* var A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ]; +* +* gfillDiagonal( 2, 3, 0, 10.0, A, 4, 1, 5 ); +* // A => [ 1.0, 2.0, 3.0, 4.0, 5.0, 10.0, 7.0, 8.0, 9.0, 10.0, 10.0, 12.0 ] +*/ +function gfillDiagonal( M, N, k, alpha, A, strideA1, strideA2, offsetA ) { + var dStride; + var offset; + var nDiag; + var ia; + var oa; + var i; + + if ( M <= 0 || N <= 0 ) { + return A; + } + + // Handle accessor arrays + oa = arraylike2object( A ); + if ( oa.accessorProtocol ) { + accessors( M, N, k, alpha, oa, strideA1, strideA2, offsetA ); + return A; + } + + // Determine the number of elements on the diagonal + if ( k >= 0 ) { + nDiag = min( M, N - k ); + offset = offsetA + (k * strideA2); + } else { + nDiag = min( M + k, N ); + offset = offsetA - (k * strideA1); + } + + if ( nDiag <= 0 ) { + return A; + } + + // The diagonal stride is the sum of strides for moving to the next diagonal element + dStride = strideA1 + strideA2; + + // Fill the diagonal elements + ia = offset; + for ( i = 0; i < nDiag; i++ ) { + A[ ia ] = alpha; + ia += dStride; + } + + return A; +} + + +// EXPORTS // + +module.exports = gfillDiagonal; diff --git a/lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/package.json b/lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/package.json new file mode 100644 index 000000000000..44f8c1a34820 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/package.json @@ -0,0 +1,69 @@ +{ + "name": "@stdlib/blas/ext/base/gfill-diagonal", + "version": "0.0.0", + "description": "Fill the diagonal of a matrix with a specified scalar constant.", + "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", + "stdmath", + "mathematics", + "math", + "blas", + "extended", + "fill", + "diagonal", + "assign", + "set", + "matrix", + "constant", + "strided", + "array", + "ndarray" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/test/test.js b/lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/test/test.js new file mode 100644 index 000000000000..7adfaa36d7e9 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/test/test.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'; + +// MODULES // + +var tape = require( 'tape' ); +var gfillDiagonal = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof gfillDiagonal, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method providing an ndarray interface', function test( t ) { + t.strictEqual( typeof gfillDiagonal.ndarray, 'function', 'method is a function' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/test/test.main.js b/lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/test/test.main.js new file mode 100644 index 000000000000..75f4dc793b7c --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/test/test.main.js @@ -0,0 +1,351 @@ +/** +* @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 toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +var gfillDiagonal = require( './../lib/main.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof gfillDiagonal, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided an invalid order', function test( t ) { + var values; + var A; + var i; + + A = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + + values = [ + 'foo', + 'bar', + -5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + gfillDiagonal( value, 2, 3, 0, 5.0, A, 3 ); + }; + } +}); + +tape( 'the function throws an error if LDA is less than max(1, M) for column-major', function test( t ) { + var A = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + + t.throws( badValue, RangeError, 'throws an error when LDA < M' ); + t.end(); + + function badValue() { + gfillDiagonal( 'column-major', 3, 2, 0, 5.0, A, 2 ); + } +}); + +tape( 'the function throws an error if LDA is less than max(1, N) for row-major', function test( t ) { + var A = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + + t.throws( badValue, RangeError, 'throws an error when LDA < N' ); + t.end(); + + function badValue() { + gfillDiagonal( 'row-major', 2, 5, 0, 5.0, A, 3 ); + } +}); + +tape( 'the function has an arity of 7', function test( t ) { + t.strictEqual( gfillDiagonal.length, 7, 'has expected arity' ); + t.end(); +}); + +tape( 'the function returns the input matrix', function test( t ) { + var out; + var A; + + A = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + out = gfillDiagonal( 'row-major', 2, 3, 0, 5.0, A, 3 ); + + t.strictEqual( out, A, 'returns input matrix' ); + t.end(); +}); + +tape( 'the function handles empty matrices (M <= 0)', function test( t ) { + var expected; + var A; + + A = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + expected = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + + gfillDiagonal( 'row-major', 0, 3, 0, 5.0, A, 3 ); + + t.deepEqual( A, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function handles empty matrices (N <= 0)', function test( t ) { + var expected; + var A; + + A = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + expected = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + + gfillDiagonal( 'row-major', 2, 0, 0, 5.0, A, 3 ); + + t.deepEqual( A, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function handles out-of-range diagonal offsets', function test( t ) { + var expected; + var A; + + A = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + expected = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + + gfillDiagonal( 'row-major', 2, 3, 10, 5.0, A, 3 ); + + t.deepEqual( A, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function fills the main diagonal of a row-major matrix', function test( t ) { + var expected; + var A; + + A = new Float64Array([ + 1.0, + 2.0, + 3.0, + 4.0, + 5.0, + 6.0 + ]); + expected = new Float64Array([ + 5.0, + 2.0, + 3.0, + 4.0, + 5.0, + 6.0 + ]); + + gfillDiagonal( 'row-major', 2, 3, 0, 5.0, A, 3 ); + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function fills the main diagonal of a row-major matrix (accessors)', function test( t ) { + var expected; + var A; + + A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + expected = [ 5.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + + gfillDiagonal( 'row-major', 2, 3, 0, 5.0, toAccessorArray( A ), 3 ); + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function fills an upper diagonal of a row-major matrix', function test( t ) { + var expected; + var A; + + A = new Float64Array([ + 1.0, + 2.0, + 3.0, + 4.0, + 5.0, + 6.0 + ]); + expected = new Float64Array([ + 1.0, + 7.0, + 3.0, + 4.0, + 5.0, + 7.0 + ]); + + gfillDiagonal( 'row-major', 2, 3, 1, 7.0, A, 3 ); + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function fills an upper diagonal of a row-major matrix (accessors)', function test( t ) { + var expected; + var A; + + A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + expected = [ 1.0, 7.0, 3.0, 4.0, 5.0, 7.0 ]; + + gfillDiagonal( 'row-major', 2, 3, 1, 7.0, toAccessorArray( A ), 3 ); + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function fills a lower diagonal of a row-major matrix', function test( t ) { + var expected; + var A; + + A = new Float64Array([ + 1.0, + 2.0, + 3.0, + 4.0, + 5.0, + 6.0 + ]); + expected = new Float64Array([ + 1.0, + 2.0, + 3.0, + 8.0, + 5.0, + 6.0 + ]); + + gfillDiagonal( 'row-major', 2, 3, -1, 8.0, A, 3 ); + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function fills a lower diagonal of a row-major matrix (accessors)', function test( t ) { + var expected; + var A; + + A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + expected = [ 1.0, 2.0, 3.0, 8.0, 5.0, 6.0 ]; + + gfillDiagonal( 'row-major', 2, 3, -1, 8.0, toAccessorArray( A ), 3 ); + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function fills the main diagonal of a column-major matrix', function test( t ) { + var expected; + var A; + + A = new Float64Array([ + 1.0, + 4.0, + 2.0, + 5.0, + 3.0, + 6.0 + ]); + expected = new Float64Array([ + 5.0, + 4.0, + 2.0, + 5.0, + 3.0, + 6.0 + ]); + + gfillDiagonal( 'column-major', 2, 3, 0, 5.0, A, 2 ); + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function fills the main diagonal of a column-major matrix (accessors)', function test( t ) { + var expected; + var A; + + A = [ 1.0, 4.0, 2.0, 5.0, 3.0, 6.0 ]; + expected = [ 5.0, 4.0, 2.0, 5.0, 3.0, 6.0 ]; + + gfillDiagonal( 'column-major', 2, 3, 0, 5.0, toAccessorArray( A ), 2 ); + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function fills a 3x3 matrix main diagonal', function test( t ) { + var expected; + var A; + + A = new Float64Array([ + 1.0, + 2.0, + 3.0, + 4.0, + 5.0, + 6.0, + 7.0, + 8.0, + 9.0 + ]); + expected = new Float64Array([ + 7.0, + 2.0, + 3.0, + 4.0, + 7.0, + 6.0, + 7.0, + 8.0, + 7.0 + ]); + + gfillDiagonal( 'row-major', 3, 3, 0, 7.0, A, 3 ); + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function fills a 3x3 matrix main diagonal (accessors)', function test( t ) { + var expected; + var A; + + A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 ]; + expected = [ 7.0, 2.0, 3.0, 4.0, 7.0, 6.0, 7.0, 8.0, 7.0 ]; + + gfillDiagonal( 'row-major', 3, 3, 0, 7.0, toAccessorArray( A ), 3 ); + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); diff --git a/lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/test/test.ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/test/test.ndarray.js new file mode 100644 index 000000000000..15404d6ccb39 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/test/test.ndarray.js @@ -0,0 +1,630 @@ +/** +* @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 toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +var gfillDiagonal = require( './../lib/ndarray.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof gfillDiagonal, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 8', function test( t ) { + t.strictEqual( gfillDiagonal.length, 8, 'has expected arity' ); + t.end(); +}); + +tape( 'the function returns the input matrix', function test( t ) { + var out; + var A; + + A = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + out = gfillDiagonal( 2, 3, 0, 5.0, A, 3, 1, 0 ); + + t.strictEqual( out, A, 'returns input matrix' ); + t.end(); +}); + +tape( 'the function handles empty matrices (M <= 0)', function test( t ) { + var expected; + var A; + + A = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + expected = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + + gfillDiagonal( 0, 3, 0, 5.0, A, 3, 1, 0 ); + + t.deepEqual( A, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function handles empty matrices (N <= 0)', function test( t ) { + var expected; + var A; + + A = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + expected = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + + gfillDiagonal( 2, 0, 0, 5.0, A, 3, 1, 0 ); + + t.deepEqual( A, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function handles out-of-range diagonal offsets', function test( t ) { + var expected; + var A; + + A = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + expected = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + + gfillDiagonal( 2, 3, 10, 5.0, A, 3, 1, 0 ); + + t.deepEqual( A, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function fills the main diagonal of a matrix', function test( t ) { + var expected; + var A; + + A = new Float64Array([ + 1.0, + 2.0, + 3.0, + 4.0, + 5.0, + 6.0 + ]); + expected = new Float64Array([ + 5.0, + 2.0, + 3.0, + 4.0, + 5.0, + 6.0 + ]); + + gfillDiagonal( 2, 3, 0, 5.0, A, 3, 1, 0 ); + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function fills the main diagonal of a matrix (accessors)', function test( t ) { + var expected; + var A; + + A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + expected = [ 5.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + + gfillDiagonal( 2, 3, 0, 5.0, toAccessorArray( A ), 3, 1, 0 ); + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function fills an upper diagonal of a matrix', function test( t ) { + var expected; + var A; + + A = new Float64Array([ + 1.0, + 2.0, + 3.0, + 4.0, + 5.0, + 6.0 + ]); + expected = new Float64Array([ + 1.0, + 7.0, + 3.0, + 4.0, + 5.0, + 7.0 + ]); + + gfillDiagonal( 2, 3, 1, 7.0, A, 3, 1, 0 ); + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function fills an upper diagonal of a matrix (accessors)', function test( t ) { + var expected; + var A; + + A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + expected = [ 1.0, 8.0, 3.0, 4.0, 5.0, 8.0 ]; + + gfillDiagonal( 2, 3, 1, 8.0, toAccessorArray( A ), 3, 1, 0 ); + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function fills a lower diagonal of a matrix', function test( t ) { + var expected; + var A; + + A = new Float64Array([ + 1.0, + 2.0, + 3.0, + 4.0, + 5.0, + 6.0 + ]); + expected = new Float64Array([ + 1.0, + 2.0, + 3.0, + 8.0, + 5.0, + 6.0 + ]); + + gfillDiagonal( 2, 3, -1, 8.0, A, 3, 1, 0 ); + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function fills a lower diagonal of a matrix (accessors)', function test( t ) { + var expected; + var A; + + A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + expected = [ 1.0, 2.0, 3.0, 8.0, 5.0, 6.0 ]; + + gfillDiagonal( 2, 3, -1, 8.0, toAccessorArray( A ), 3, 1, 0 ); + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function handles offset parameter', function test( t ) { + var expected; + var A; + + A = new Float64Array([ + 1.0, + 2.0, + 3.0, + 4.0, + 5.0, + 6.0, + 7.0, + 8.0, + 9.0, + 10.0, + 11.0, + 12.0 + ]); + expected = new Float64Array([ + 1.0, + 2.0, + 3.0, + 4.0, + 5.0, + 9.0, + 7.0, + 8.0, + 9.0, + 10.0, + 9.0, + 12.0 + ]); + + gfillDiagonal( 2, 3, 0, 9.0, A, 4, 1, 5 ); + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function handles offset parameter (accessors)', function test( t ) { + var expected; + var A; + + A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ]; + expected = [ 1.0, 2.0, 3.0, 4.0, 5.0, 9.0, 7.0, 8.0, 9.0, 10.0, 9.0, 12.0 ]; + + gfillDiagonal( 2, 3, 0, 9.0, toAccessorArray( A ), 4, 1, 5 ); + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function fills a 3x3 matrix main diagonal', function test( t ) { + var expected; + var A; + + A = new Float64Array([ + 1.0, + 2.0, + 3.0, + 4.0, + 5.0, + 6.0, + 7.0, + 8.0, + 9.0 + ]); + expected = new Float64Array([ + 7.0, + 2.0, + 3.0, + 4.0, + 7.0, + 6.0, + 7.0, + 8.0, + 7.0 + ]); + + gfillDiagonal( 3, 3, 0, 7.0, A, 3, 1, 0 ); + t.deepEqual( A, expected, 'returns expected value for 3x3 matrix' ); + + t.end(); +}); + +tape( 'the function fills a 3x3 matrix main diagonal (accessors)', function test( t ) { + var expected; + var A; + + A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 ]; + expected = [ 7.0, 2.0, 3.0, 4.0, 7.0, 6.0, 7.0, 8.0, 7.0 ]; + + gfillDiagonal( 3, 3, 0, 7.0, toAccessorArray( A ), 3, 1, 0 ); + t.deepEqual( A, expected, 'returns expected value for 3x3 matrix' ); + + t.end(); +}); + +tape( 'the function fills the main diagonal of a column-major matrix', function test( t ) { + var expected; + var A; + + A = new Float64Array([ + 1.0, + 4.0, + 2.0, + 5.0, + 3.0, + 6.0 + ]); + expected = new Float64Array([ + 5.0, + 4.0, + 2.0, + 5.0, + 3.0, + 6.0 + ]); + + gfillDiagonal( 2, 3, 0, 5.0, A, 1, 2, 0 ); + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function fills the main diagonal of a column-major matrix (accessors)', function test( t ) { + var expected; + var A; + + A = [ 1.0, 4.0, 2.0, 5.0, 3.0, 6.0 ]; + expected = [ 5.0, 4.0, 2.0, 5.0, 3.0, 6.0 ]; + + gfillDiagonal( 2, 3, 0, 5.0, toAccessorArray( A ), 1, 2, 0 ); + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function fills an upper diagonal of a column-major matrix', function test( t ) { + var expected; + var A; + + A = new Float64Array([ + 1.0, + 4.0, + 2.0, + 5.0, + 3.0, + 6.0 + ]); + expected = new Float64Array([ + 1.0, + 4.0, + 7.0, + 5.0, + 3.0, + 7.0 + ]); + + gfillDiagonal( 2, 3, 1, 7.0, A, 1, 2, 0 ); + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function fills an upper diagonal of a column-major matrix (accessors)', function test( t ) { + var expected; + var A; + + A = [ 1.0, 4.0, 2.0, 5.0, 3.0, 6.0 ]; + expected = [ 1.0, 4.0, 7.0, 5.0, 3.0, 7.0 ]; + + gfillDiagonal( 2, 3, 1, 7.0, toAccessorArray( A ), 1, 2, 0 ); + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function fills a lower diagonal of a column-major matrix', function test( t ) { + var expected; + var A; + + A = new Float64Array([ + 1.0, + 4.0, + 2.0, + 5.0, + 3.0, + 6.0 + ]); + expected = new Float64Array([ + 1.0, + 8.0, + 2.0, + 5.0, + 3.0, + 6.0 + ]); + + gfillDiagonal( 2, 3, -1, 8.0, A, 1, 2, 0 ); + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function fills a lower diagonal of a column-major matrix (accessors)', function test( t ) { + var expected; + var A; + + A = [ 1.0, 4.0, 2.0, 5.0, 3.0, 6.0 ]; + expected = [ 1.0, 8.0, 2.0, 5.0, 3.0, 6.0 ]; + + gfillDiagonal( 2, 3, -1, 8.0, toAccessorArray( A ), 1, 2, 0 ); + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function handles mixed strides (row-major)', function test( t ) { + var expected; + var A; + + A = new Float64Array([ + 1.0, + 2.0, + 3.0, + 4.0, + 5.0, + 6.0, + 7.0, + 8.0, + 9.0, + 10.0, + 11.0, + 12.0 + ]); + expected = new Float64Array([ + 6.0, + 2.0, + 3.0, + 4.0, + 5.0, + 6.0, + 6.0, + 8.0, + 9.0, + 10.0, + 11.0, + 12.0 + ]); + + gfillDiagonal( 2, 3, 0, 6.0, A, 5, 1, 0 ); + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function handles mixed strides (row-major, accessors)', function test( t ) { + var expected; + var A; + + A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ]; + expected = [ 6.0, 2.0, 3.0, 4.0, 5.0, 6.0, 6.0, 8.0, 9.0, 10.0, 11.0, 12.0 ]; + + gfillDiagonal( 2, 3, 0, 6.0, toAccessorArray( A ), 5, 1, 0 ); + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function handles mixed strides (column-major)', function test( t ) { + var expected; + var A; + + A = new Float64Array([ + 1.0, + 2.0, + 3.0, + 4.0, + 5.0, + 6.0, + 7.0, + 8.0, + 9.0, + 10.0, + 11.0, + 12.0 + ]); + expected = new Float64Array([ + 6.0, + 2.0, + 3.0, + 4.0, + 5.0, + 6.0, + 7.0, + 8.0, + 9.0, + 10.0, + 11.0, + 12.0 + ]); + + gfillDiagonal( 2, 3, 0, 6.0, A, 1, 4, 0 ); + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function handles mixed strides (column-major, accessors)', function test( t ) { + var expected; + var A; + + A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ]; + expected = [ 6.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ]; + + gfillDiagonal( 2, 3, 0, 6.0, toAccessorArray( A ), 1, 4, 0 ); + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function handles negative strides', function test( t ) { + var expected; + var A; + + A = new Float64Array([ + 1.0, + 2.0, + 3.0, + 4.0, + 5.0, + 6.0, + 7.0, + 8.0, + 9.0 + ]); + expected = new Float64Array([ + 6.0, + 2.0, + 3.0, + 4.0, + 6.0, + 6.0, + 7.0, + 8.0, + 6.0 + ]); + + gfillDiagonal( 3, 3, 0, 6.0, A, -3, -1, 8 ); + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function handles negative strides (accessors)', function test( t ) { + var expected; + var A; + + A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 ]; + expected = [ 6.0, 2.0, 3.0, 4.0, 6.0, 6.0, 7.0, 8.0, 6.0 ]; + + gfillDiagonal( 3, 3, 0, 6.0, toAccessorArray( A ), -3, -1, 8 ); + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function handles offset with column-major layout', function test( t ) { + var expected; + var A; + + A = new Float64Array([ + 1.0, + 2.0, + 3.0, + 4.0, + 5.0, + 6.0, + 7.0, + 8.0, + 9.0, + 10.0, + 11.0, + 12.0 + ]); + expected = new Float64Array([ + 1.0, + 2.0, + 3.0, + 4.0, + 5.0, + 9.0, + 7.0, + 8.0, + 9.0, + 10.0, + 9.0, + 12.0 + ]); + + gfillDiagonal( 2, 3, 0, 9.0, A, 1, 4, 5 ); + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function handles offset with column-major layout (accessors)', function test( t ) { + var expected; + var A; + + A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ]; + expected = [ 1.0, 2.0, 3.0, 4.0, 5.0, 9.0, 7.0, 8.0, 9.0, 10.0, 9.0, 12.0 ]; + + gfillDiagonal( 2, 3, 0, 9.0, toAccessorArray( A ), 1, 4, 5 ); + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); From ab1bb22c614bfba9a7fbaa7b94a2f998d5c5a229 Mon Sep 17 00:00:00 2001 From: headlessNode Date: Thu, 25 Jun 2026 17:44:40 +0500 Subject: [PATCH 2/2] refactor: use gfill --- 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_pkg_readmes status: na - task: lint_markdown_docs status: na - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - 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: na - task: lint_license_headers status: passed --- --- .../ext/base/gfill-diagonal/lib/accessors.js | 131 ------------------ .../ext/base/gfill-diagonal/lib/ndarray.js | 27 +--- 2 files changed, 5 insertions(+), 153 deletions(-) delete mode 100644 lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/lib/accessors.js diff --git a/lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/lib/accessors.js b/lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/lib/accessors.js deleted file mode 100644 index 77388a138bbc..000000000000 --- a/lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/lib/accessors.js +++ /dev/null @@ -1,131 +0,0 @@ -/** -* @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 min = require( '@stdlib/math/base/special/fast/min' ); - - -// MAIN // - -/** -* Fills the diagonal of a matrix with a specified scalar constant using accessors. -* -* @private -* @param {integer} M - number of rows in `A` -* @param {integer} N - number of columns in `A` -* @param {integer} k - diagonal offset -* @param {*} alpha - scalar constant -* @param {Object} A - input matrix object -* @param {Collection} A.data - input matrix data -* @param {Array} A.accessors - array element accessors -* @param {integer} strideA1 - stride of the first dimension of `A` -* @param {integer} strideA2 - stride of the second dimension of `A` -* @param {NonNegativeInteger} offsetA - starting index for `A` -* @returns {Object} input matrix -* -* @example -* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); -* var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); -* -* var A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; -* var out = gfillDiagonal( 2, 3, 0, 7.0, arraylike2object( toAccessorArray( A ) ), 3, 1, 0 ); -* // A => [ 7.0, 2.0, 3.0, 4.0, 7.0, 6.0 ] -* -* @example -* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); -* var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); -* -* var A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; -* var out = gfillDiagonal( 2, 3, 0, 7.0, arraylike2object( toAccessorArray( A ) ), 1, 2, 0 ); -* // A => [ 7.0, 2.0, 3.0, 7.0, 5.0, 6.0 ] -* -* @example -* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); -* var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); -* -* var A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; -* var out = gfillDiagonal( 2, 3, 1, 8.0, arraylike2object( toAccessorArray( A ) ), 3, 1, 0 ); -* // A => [ 1.0, 8.0, 3.0, 4.0, 5.0, 8.0 ] -* -* @example -* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); -* var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); -* -* var A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; -* var out = gfillDiagonal( 2, 3, -1, 9.0, arraylike2object( toAccessorArray( A ) ), 3, 1, 0 ); -* // A => [ 1.0, 2.0, 3.0, 9.0, 5.0, 6.0 ] -* -* @example -* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); -* var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); -* -* var A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ]; -* var out = gfillDiagonal( 2, 3, 0, 10.0, arraylike2object( toAccessorArray( A ) ), 4, 1, 5 ); -* // A => [ 1.0, 2.0, 3.0, 4.0, 5.0, 10.0, 7.0, 8.0, 9.0, 10.0, 10.0, 12.0 ] -*/ -function gfillDiagonal( M, N, k, alpha, A, strideA1, strideA2, offsetA ) { - var dStride; - var offset; - var nDiag; - var set; - var buf; - var ia; - var i; - - // Early return if dimensions are invalid - if ( M <= 0 || N <= 0 ) { - return A; - } - - // Cache reference to matrix data and setter accessor - buf = A.data; - set = A.accessors[ 1 ]; - - // Determine the number of elements on the diagonal - if ( k >= 0 ) { - nDiag = min( M, N - k ); - offset = offsetA + (k * strideA2); - } else { - nDiag = min( M + k, N ); - offset = offsetA - (k * strideA1); - } - - if ( nDiag <= 0 ) { - return A; - } - - // The diagonal stride is the sum of strides for moving to the next diagonal element - dStride = strideA1 + strideA2; - - // Fill the diagonal elements using the setter accessor - ia = offset; - for ( i = 0; i < nDiag; i++ ) { - set( buf, ia, alpha ); - ia += dStride; - } - - return A; -} - - -// EXPORTS // - -module.exports = gfillDiagonal; diff --git a/lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/lib/ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/lib/ndarray.js index 8c2a4369b81c..2a49674f39e2 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/lib/ndarray.js +++ b/lib/node_modules/@stdlib/blas/ext/base/gfill-diagonal/lib/ndarray.js @@ -20,9 +20,8 @@ // MODULES // -var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +var gfill = require( '@stdlib/blas/ext/base/gfill' ).ndarray; var min = require( '@stdlib/math/base/special/fast/min' ); -var accessors = require( './accessors.js' ); // MAIN // @@ -74,22 +73,12 @@ function gfillDiagonal( M, N, k, alpha, A, strideA1, strideA2, offsetA ) { var dStride; var offset; var nDiag; - var ia; - var oa; - var i; if ( M <= 0 || N <= 0 ) { return A; } - // Handle accessor arrays - oa = arraylike2object( A ); - if ( oa.accessorProtocol ) { - accessors( M, N, k, alpha, oa, strideA1, strideA2, offsetA ); - return A; - } - - // Determine the number of elements on the diagonal + // Determine the number of elements on the diagonal... if ( k >= 0 ) { nDiag = min( M, N - k ); offset = offsetA + (k * strideA2); @@ -102,17 +91,11 @@ function gfillDiagonal( M, N, k, alpha, A, strideA1, strideA2, offsetA ) { return A; } - // The diagonal stride is the sum of strides for moving to the next diagonal element + // The diagonal stride is the sum of strides for moving to the next diagonal element: dStride = strideA1 + strideA2; - // Fill the diagonal elements - ia = offset; - for ( i = 0; i < nDiag; i++ ) { - A[ ia ] = alpha; - ia += dStride; - } - - return A; + // Fill the diagonal: + return gfill( nDiag, alpha, A, dStride, offset ); }