From e01f87ac1d035542ee9f0e4e4293969c7dd45c1c Mon Sep 17 00:00:00 2001 From: headlessNode Date: Sat, 8 Aug 2026 21:23:57 +0500 Subject: [PATCH] feat: add blas/ext/base/gswap-columns --- 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/gswap-columns/README.md | 186 ++++++++ .../base/gswap-columns/benchmark/benchmark.js | 111 +++++ .../benchmark/benchmark.ndarray.js | 124 +++++ .../blas/ext/base/gswap-columns/docs/repl.txt | 97 ++++ .../base/gswap-columns/docs/types/index.d.ts | 114 +++++ .../ext/base/gswap-columns/docs/types/test.ts | 272 +++++++++++ .../ext/base/gswap-columns/examples/index.js | 39 ++ .../ext/base/gswap-columns/lib/accessors.js | 82 ++++ .../blas/ext/base/gswap-columns/lib/index.js | 59 +++ .../blas/ext/base/gswap-columns/lib/main.js | 94 ++++ .../ext/base/gswap-columns/lib/ndarray.js | 102 +++++ .../blas/ext/base/gswap-columns/package.json | 67 +++ .../blas/ext/base/gswap-columns/test/test.js | 38 ++ .../ext/base/gswap-columns/test/test.main.js | 433 ++++++++++++++++++ .../base/gswap-columns/test/test.ndarray.js | 421 +++++++++++++++++ 15 files changed, 2239 insertions(+) create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gswap-columns/README.md create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gswap-columns/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gswap-columns/benchmark/benchmark.ndarray.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gswap-columns/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gswap-columns/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gswap-columns/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gswap-columns/examples/index.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gswap-columns/lib/accessors.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gswap-columns/lib/index.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gswap-columns/lib/main.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gswap-columns/lib/ndarray.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gswap-columns/package.json create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gswap-columns/test/test.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gswap-columns/test/test.main.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/gswap-columns/test/test.ndarray.js diff --git a/lib/node_modules/@stdlib/blas/ext/base/gswap-columns/README.md b/lib/node_modules/@stdlib/blas/ext/base/gswap-columns/README.md new file mode 100644 index 000000000000..498e819d3b2a --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gswap-columns/README.md @@ -0,0 +1,186 @@ + + +# gswapColumns + +> Interchange two columns in a matrix `A`. + +
+ +## Usage + +```javascript +var gswapColumns = require( '@stdlib/blas/ext/base/gswap-columns' ); +``` + +#### gswapColumns( order, M, N, i, j, A, LDA ) + +Interchanges two columns in a matrix `A`. + +```javascript +/* + A = [ + [ 1.0, 2.0 ], + [ 3.0, 4.0 ], + [ 5.0, 6.0 ] + ] +*/ +var A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + +gswapColumns( 'row-major', 3, 2, 0, 1, A, 2 ); +// A => [ 2.0, 1.0, 4.0, 3.0, 6.0, 5.0 ] +``` + +The function has the following parameters: + +- **order**: storage layout. +- **M**: number of rows in `A`. +- **N**: number of columns in `A`. +- **i**: index of the first column to interchange. +- **j**: index of the second column to interchange. +- **A**: input matrix as a linear array. +- **LDA**: stride length for the first dimension of `A` (a.k.a., leading dimension of the matrix `A`). + +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 array... +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 + +// Interchange the first and second columns... +gswapColumns( 'row-major', 3, 2, 0, 1, A1, 2 ); +// A0 => [ 0.0, 2.0, 1.0, 4.0, 3.0, 6.0, 5.0 ] +``` + +#### gswapColumns.ndarray( M, N, i, j, A, sa1, sa2, oa ) + +Interchanges two columns in a matrix `A` using alternative indexing semantics. + +```javascript +/* + A = [ + [ 1.0, 2.0 ], + [ 3.0, 4.0 ], + [ 5.0, 6.0 ] + ] +*/ +var A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + +gswapColumns.ndarray( 3, 2, 0, 1, A, 2, 1, 0 ); +// A => [ 2.0, 1.0, 4.0, 3.0, 6.0, 5.0 ] +``` + +The function has the following parameters: + +- **M**: number of rows in `A`. +- **N**: number of columns in `A`. +- **i**: index of the first column to interchange. +- **j**: index of the second column to interchange. +- **A**: input matrix as a linear array. +- **sa1**: stride length for the first dimension of `A`. +- **sa2**: stride length for the second dimension of `A`. +- **oa**: 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, + +```javascript +/* + A = [ + [ 1.0, 2.0 ], + [ 3.0, 4.0 ], + [ 5.0, 6.0 ] + ] +*/ +var A = [ 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + +gswapColumns.ndarray( 3, 2, 0, 1, A, 2, 1, 1 ); +// A => [ 0.0, 2.0, 1.0, 4.0, 3.0, 6.0, 5.0 ] +``` + +
+ + + +
+ +## Notes + +- If provided negative column indices, both functions resolve the indices relative to the last column, with the last column corresponding to `-1`. +- Both functions mutate the input matrix `A`. +- 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 ndarray2array = require( '@stdlib/ndarray/base/to-array' ); +var uniform = require( '@stdlib/random/array/discrete-uniform' ); +var numel = require( '@stdlib/ndarray/base/numel' ); +var shape2strides = require( '@stdlib/ndarray/base/shape2strides' ); +var gswapColumns = require( '@stdlib/blas/ext/base/gswap-columns' ); + +var shape = [ 5, 8 ]; +var order = 'row-major'; +var strides = shape2strides( shape, order ); + +var N = numel( shape ); + +var A = uniform( N, -10, 10, { + 'dtype': 'generic' +}); +console.log( ndarray2array( A, shape, strides, 0, order ) ); + +gswapColumns( order, shape[ 0 ], shape[ 1 ], 0, -1, A, strides[ 0 ] ); +console.log( ndarray2array( A, shape, strides, 0, order ) ); +``` + +
+ + + + + + + + + + diff --git a/lib/node_modules/@stdlib/blas/ext/base/gswap-columns/benchmark/benchmark.js b/lib/node_modules/@stdlib/blas/ext/base/gswap-columns/benchmark/benchmark.js new file mode 100644 index 000000000000..76f774676163 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gswap-columns/benchmark/benchmark.js @@ -0,0 +1,111 @@ +/** +* @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 zeros = require( '@stdlib/array/zeros' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var floor = require( '@stdlib/math/base/special/floor' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var gswapColumns = require( './../lib' ); + + +// 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 z; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + z = gswapColumns( order, N, N, 0, N-1, A, N ); + if ( isnan( z[ i%z.length ] ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( z[ i%z.length ] ) ) { + 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 = 6; // 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:order=%s,size=%d', pkg, ord, N*N ), f ); + } + } +} + +main(); diff --git a/lib/node_modules/@stdlib/blas/ext/base/gswap-columns/benchmark/benchmark.ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/gswap-columns/benchmark/benchmark.ndarray.js new file mode 100644 index 000000000000..43740712d8e9 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gswap-columns/benchmark/benchmark.ndarray.js @@ -0,0 +1,124 @@ +/** +* @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 zeros = require( '@stdlib/array/zeros' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var floor = require( '@stdlib/math/base/special/floor' ); +var isColumnMajor = require( '@stdlib/ndarray/base/assert/is-column-major-string' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var gswapColumns = require( './../lib' ).ndarray; + + +// 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 sa1; + var sa2; + var A; + + A = zeros( N*N, 'generic' ); + + if ( isColumnMajor( order ) ) { + sa1 = 1; + sa2 = N; + } else { // order === 'row-major' + sa1 = N; + sa2 = 1; + } + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var z; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + z = gswapColumns( N, N, 0, N-1, A, sa1, sa2, 0 ); + if ( isnan( z[ i%z.length ] ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( z[ i%z.length ] ) ) { + 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 = 6; // 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/gswap-columns/docs/repl.txt b/lib/node_modules/@stdlib/blas/ext/base/gswap-columns/docs/repl.txt new file mode 100644 index 000000000000..36377350a544 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gswap-columns/docs/repl.txt @@ -0,0 +1,97 @@ + +{{alias}}( order, M, N, i, j, A, LDA ) + Interchanges two columns in a matrix `A`. + + Indexing is relative to the first index. To introduce an offset, use typed + array views. + + If provided negative column indices, the function resolves the indices + relative to the last column, with the last column corresponding to `-1`. + + Parameters + ---------- + order: string + Row-major (C-style) or column-major (Fortran-style) order. Must be + either 'row-major' or 'column-major'. + + M: integer + Number of rows in `A`. + + N: integer + Number of columns in `A`. + + i: integer + Index of the first column to interchange. + + j: integer + Index of the second column to interchange. + + A: Array|TypedArray + Input matrix `A`. + + LDA: integer + Stride length for the first dimension of `A` (a.k.a., leading dimension + of the matrix `A`). + + Returns + ------- + A: Array|TypedArray + Input matrix. + + Examples + -------- + > var A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + > {{alias}}( 'row-major', 3, 2, 0, 1, A, 2 ) + [ 2.0, 1.0, 4.0, 3.0, 6.0, 5.0 ] + + +{{alias}}.ndarray( M, N, i, j, A, sa1, sa2, oa ) + Interchanges two columns in a matrix `A` using alternative indexing + semantics. + + Negative column indices are resolved relative to the last column, with the + last column corresponding to `-1`. + + While typed array views mandate a view offset based on the underlying + buffer, the offset parameter supports indexing semantics based on a + starting index. + + Parameters + ---------- + M: integer + Number of rows in `A`. + + N: integer + Number of columns in `A`. + + i: integer + Index of the first column to interchange. + + j: integer + Index of the second column to interchange. + + A: Array|TypedArray + Input matrix `A`. + + sa1: integer + Stride length for the first dimension of `A`. + + sa2: integer + Stride length for the second dimension of `A`. + + oa: integer + Starting index for `A`. + + Returns + ------- + A: Array|TypedArray + Input matrix. + + Examples + -------- + > var A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + > {{alias}}.ndarray( 3, 2, 0, 1, A, 2, 1, 0 ) + [ 2.0, 1.0, 4.0, 3.0, 6.0, 5.0 ] + + See Also + -------- diff --git a/lib/node_modules/@stdlib/blas/ext/base/gswap-columns/docs/types/index.d.ts b/lib/node_modules/@stdlib/blas/ext/base/gswap-columns/docs/types/index.d.ts new file mode 100644 index 000000000000..50c1f6d93c1a --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gswap-columns/docs/types/index.d.ts @@ -0,0 +1,114 @@ +/* +* @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'; + +/** +* Input array. +*/ +type InputArray = Collection | AccessorArrayLike; + +/** +* Interface describing `gswapColumns`. +*/ +interface Routine { + /** + * Interchanges two columns in a matrix `A`. + * + * ## Notes + * + * - If provided negative column indices, the function resolves the indices relative to the last column, with the last column corresponding to `-1`. + * + * @param order - storage layout of `A` + * @param M - number of rows in matrix `A` + * @param N - number of columns in matrix `A` + * @param i - index of the first column to interchange + * @param j - index of the second column to interchange + * @param A - input matrix + * @param LDA - stride length for the first dimension of `A` (a.k.a., leading dimension of the matrix `A`) + * @returns `A` + * + * @example + * var A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + * + * gswapColumns( 'row-major', 3, 2, 0, 1, A, 2 ); + * // A => [ 2.0, 1.0, 4.0, 3.0, 6.0, 5.0 ] + */ + = InputArray>( order: Layout, M: number, N: number, i: number, j: number, A: U, LDA: number ): U; + + /** + * Interchanges two columns in a matrix `A` using alternative indexing semantics. + * + * ## Notes + * + * - If provided negative column indices, the function resolves the indices relative to the last column, with the last column corresponding to `-1`. + * + * @param M - number of rows in matrix `A` + * @param N - number of columns in matrix `A` + * @param i - index of the first column to interchange + * @param j - index of the second column to interchange + * @param A - input matrix + * @param strideA1 - stride length for the first dimension of `A` + * @param strideA2 - stride length for 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 ]; + * + * gswapColumns.ndarray( 3, 2, 0, 1, A, 2, 1, 0 ); + * // A => [ 2.0, 1.0, 4.0, 3.0, 6.0, 5.0 ] + */ + ndarray = InputArray>( M: number, N: number, i: number, j: number, A: U, strideA1: number, strideA2: number, offsetA: number ): U; +} + +/** +* Interchanges two columns in a matrix `A`. +* +* @param order - storage layout of `A` +* @param M - number of rows in matrix `A` +* @param N - number of columns in matrix `A` +* @param i - index of the first column to interchange +* @param j - index of the second column to interchange +* @param A - input matrix +* @param LDA - stride length for the first dimension of `A` (a.k.a., leading dimension of the matrix `A`) +* @returns `A` +* +* @example +* var A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; +* +* gswapColumns( 'row-major', 3, 2, 0, 1, A, 2 ); +* // A => [ 2.0, 1.0, 4.0, 3.0, 6.0, 5.0 ] +* +* @example +* var A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; +* +* gswapColumns.ndarray( 3, 2, 0, 1, A, 2, 1, 0 ); +* // A => [ 2.0, 1.0, 4.0, 3.0, 6.0, 5.0 ] +*/ +declare var gswapColumns: Routine; + + +// EXPORTS // + +export = gswapColumns; diff --git a/lib/node_modules/@stdlib/blas/ext/base/gswap-columns/docs/types/test.ts b/lib/node_modules/@stdlib/blas/ext/base/gswap-columns/docs/types/test.ts new file mode 100644 index 000000000000..1a8147b48a47 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gswap-columns/docs/types/test.ts @@ -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. +*/ + +import AccessorArray = require( '@stdlib/array/base/accessor' ); +import gswapColumns = require( './index' ); + + +// TESTS // + +// The function returns a collection... +{ + const A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + + gswapColumns( 'row-major', 3, 2, 0, 1, A, 2 ); // $ExpectType number[] + gswapColumns( 'row-major', 3, 2, 0, 1, new AccessorArray( A ), 2 ); // $ExpectType AccessorArray +} + +// The compiler throws an error if the function is provided a first argument which is not a valid order... +{ + const A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + + gswapColumns( 5, 3, 2, 0, 1, A, 2 ); // $ExpectError + gswapColumns( true, 3, 2, 0, 1, A, 2 ); // $ExpectError + gswapColumns( false, 3, 2, 0, 1, A, 2 ); // $ExpectError + gswapColumns( null, 3, 2, 0, 1, A, 2 ); // $ExpectError + gswapColumns( void 0, 3, 2, 0, 1, A, 2 ); // $ExpectError + gswapColumns( [], 3, 2, 0, 1, A, 2 ); // $ExpectError + gswapColumns( {}, 3, 2, 0, 1, A, 2 ); // $ExpectError + gswapColumns( ( x: number ): number => x, 3, 2, 0, 1, A, 2 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not a number... +{ + const A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + + gswapColumns( 'row-major', '5', 2, 0, 1, A, 2 ); // $ExpectError + gswapColumns( 'row-major', true, 2, 0, 1, A, 2 ); // $ExpectError + gswapColumns( 'row-major', false, 2, 0, 1, A, 2 ); // $ExpectError + gswapColumns( 'row-major', null, 2, 0, 1, A, 2 ); // $ExpectError + gswapColumns( 'row-major', void 0, 2, 0, 1, A, 2 ); // $ExpectError + gswapColumns( 'row-major', [], 2, 0, 1, A, 2 ); // $ExpectError + gswapColumns( 'row-major', {}, 2, 0, 1, A, 2 ); // $ExpectError + gswapColumns( 'row-major', ( x: number ): number => x, 2, 0, 1, A, 2 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a third argument which is not a number... +{ + const A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + + gswapColumns( 'row-major', 3, '5', 0, 1, A, 2 ); // $ExpectError + gswapColumns( 'row-major', 3, true, 0, 1, A, 2 ); // $ExpectError + gswapColumns( 'row-major', 3, false, 0, 1, A, 2 ); // $ExpectError + gswapColumns( 'row-major', 3, null, 0, 1, A, 2 ); // $ExpectError + gswapColumns( 'row-major', 3, void 0, 0, 1, A, 2 ); // $ExpectError + gswapColumns( 'row-major', 3, [], 0, 1, A, 2 ); // $ExpectError + gswapColumns( 'row-major', 3, {}, 0, 1, A, 2 ); // $ExpectError + gswapColumns( 'row-major', 3, ( x: number ): number => x, 0, 1, A, 2 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a fourth argument which is not a number... +{ + const A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + + gswapColumns( 'row-major', 3, 2, '5', 1, A, 2 ); // $ExpectError + gswapColumns( 'row-major', 3, 2, true, 1, A, 2 ); // $ExpectError + gswapColumns( 'row-major', 3, 2, false, 1, A, 2 ); // $ExpectError + gswapColumns( 'row-major', 3, 2, null, 1, A, 2 ); // $ExpectError + gswapColumns( 'row-major', 3, 2, void 0, 1, A, 2 ); // $ExpectError + gswapColumns( 'row-major', 3, 2, [], 1, A, 2 ); // $ExpectError + gswapColumns( 'row-major', 3, 2, {}, 1, A, 2 ); // $ExpectError + gswapColumns( 'row-major', 3, 2, ( x: number ): number => x, 1, A, 2 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a fifth argument which is not a number... +{ + const A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + + gswapColumns( 'row-major', 3, 2, 0, '5', A, 2 ); // $ExpectError + gswapColumns( 'row-major', 3, 2, 0, true, A, 2 ); // $ExpectError + gswapColumns( 'row-major', 3, 2, 0, false, A, 2 ); // $ExpectError + gswapColumns( 'row-major', 3, 2, 0, null, A, 2 ); // $ExpectError + gswapColumns( 'row-major', 3, 2, 0, void 0, A, 2 ); // $ExpectError + gswapColumns( 'row-major', 3, 2, 0, [], A, 2 ); // $ExpectError + gswapColumns( 'row-major', 3, 2, 0, {}, A, 2 ); // $ExpectError + gswapColumns( 'row-major', 3, 2, 0, ( x: number ): number => x, A, 2 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a sixth argument which is not a collection... +{ + gswapColumns( 'row-major', 3, 2, 0, 1, 5, 2 ); // $ExpectError + gswapColumns( 'row-major', 3, 2, 0, 1, true, 2 ); // $ExpectError + gswapColumns( 'row-major', 3, 2, 0, 1, false, 2 ); // $ExpectError + gswapColumns( 'row-major', 3, 2, 0, 1, null, 2 ); // $ExpectError + gswapColumns( 'row-major', 3, 2, 0, 1, void 0, 2 ); // $ExpectError + gswapColumns( 'row-major', 3, 2, 0, 1, {}, 2 ); // $ExpectError + gswapColumns( 'row-major', 3, 2, 0, 1, ( x: number ): number => x, 2 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a seventh argument which is not a number... +{ + const A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + + gswapColumns( 'row-major', 3, 2, 0, 1, A, '5' ); // $ExpectError + gswapColumns( 'row-major', 3, 2, 0, 1, A, true ); // $ExpectError + gswapColumns( 'row-major', 3, 2, 0, 1, A, false ); // $ExpectError + gswapColumns( 'row-major', 3, 2, 0, 1, A, null ); // $ExpectError + gswapColumns( 'row-major', 3, 2, 0, 1, A, void 0 ); // $ExpectError + gswapColumns( 'row-major', 3, 2, 0, 1, A, [] ); // $ExpectError + gswapColumns( 'row-major', 3, 2, 0, 1, A, {} ); // $ExpectError + gswapColumns( 'row-major', 3, 2, 0, 1, A, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + + gswapColumns(); // $ExpectError + gswapColumns( 'row-major' ); // $ExpectError + gswapColumns( 'row-major', 3 ); // $ExpectError + gswapColumns( 'row-major', 3, 2 ); // $ExpectError + gswapColumns( 'row-major', 3, 2, 0 ); // $ExpectError + gswapColumns( 'row-major', 3, 2, 0, 1 ); // $ExpectError + gswapColumns( 'row-major', 3, 2, 0, 1, A ); // $ExpectError + gswapColumns( 'row-major', 3, 2, 0, 1, A, 2, 10 ); // $ExpectError +} + +// Attached to main export is an `ndarray` method which returns a collection... +{ + const A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + + gswapColumns.ndarray( 3, 2, 0, 1, A, 2, 1, 0 ); // $ExpectType number[] + gswapColumns.ndarray( 3, 2, 0, 1, new AccessorArray( A ), 2, 1, 0 ); // $ExpectType AccessorArray +} + +// The compiler throws an error if the `ndarray` method is provided a first argument which is not a number... +{ + const A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + + gswapColumns.ndarray( '5', 2, 0, 1, A, 2, 1, 0 ); // $ExpectError + gswapColumns.ndarray( true, 2, 0, 1, A, 2, 1, 0 ); // $ExpectError + gswapColumns.ndarray( false, 2, 0, 1, A, 2, 1, 0 ); // $ExpectError + gswapColumns.ndarray( null, 2, 0, 1, A, 2, 1, 0 ); // $ExpectError + gswapColumns.ndarray( void 0, 2, 0, 1, A, 2, 1, 0 ); // $ExpectError + gswapColumns.ndarray( [], 2, 0, 1, A, 2, 1, 0 ); // $ExpectError + gswapColumns.ndarray( {}, 2, 0, 1, A, 2, 1, 0 ); // $ExpectError + gswapColumns.ndarray( ( x: number ): number => x, 2, 0, 1, A, 2, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a second argument which is not a number... +{ + const A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + + gswapColumns.ndarray( 3, '5', 0, 1, A, 2, 1, 0 ); // $ExpectError + gswapColumns.ndarray( 3, true, 0, 1, A, 2, 1, 0 ); // $ExpectError + gswapColumns.ndarray( 3, false, 0, 1, A, 2, 1, 0 ); // $ExpectError + gswapColumns.ndarray( 3, null, 0, 1, A, 2, 1, 0 ); // $ExpectError + gswapColumns.ndarray( 3, void 0, 0, 1, A, 2, 1, 0 ); // $ExpectError + gswapColumns.ndarray( 3, [], 0, 1, A, 2, 1, 0 ); // $ExpectError + gswapColumns.ndarray( 3, {}, 0, 1, A, 2, 1, 0 ); // $ExpectError + gswapColumns.ndarray( 3, ( x: number ): number => x, 0, 1, A, 2, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a third argument which is not a number... +{ + const A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + + gswapColumns.ndarray( 3, 2, '5', 1, A, 2, 1, 0 ); // $ExpectError + gswapColumns.ndarray( 3, 2, true, 1, A, 2, 1, 0 ); // $ExpectError + gswapColumns.ndarray( 3, 2, false, 1, A, 2, 1, 0 ); // $ExpectError + gswapColumns.ndarray( 3, 2, null, 1, A, 2, 1, 0 ); // $ExpectError + gswapColumns.ndarray( 3, 2, void 0, 1, A, 2, 1, 0 ); // $ExpectError + gswapColumns.ndarray( 3, 2, [], 1, A, 2, 1, 0 ); // $ExpectError + gswapColumns.ndarray( 3, 2, {}, 1, A, 2, 1, 0 ); // $ExpectError + gswapColumns.ndarray( 3, 2, ( x: number ): number => x, 1, A, 2, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a fourth argument which is not a number... +{ + const A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + + gswapColumns.ndarray( 3, 2, 0, '5', A, 2, 1, 0 ); // $ExpectError + gswapColumns.ndarray( 3, 2, 0, true, A, 2, 1, 0 ); // $ExpectError + gswapColumns.ndarray( 3, 2, 0, false, A, 2, 1, 0 ); // $ExpectError + gswapColumns.ndarray( 3, 2, 0, null, A, 2, 1, 0 ); // $ExpectError + gswapColumns.ndarray( 3, 2, 0, void 0, A, 2, 1, 0 ); // $ExpectError + gswapColumns.ndarray( 3, 2, 0, [], A, 2, 1, 0 ); // $ExpectError + gswapColumns.ndarray( 3, 2, 0, {}, A, 2, 1, 0 ); // $ExpectError + gswapColumns.ndarray( 3, 2, 0, ( x: number ): number => x, A, 2, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a fifth argument which is not a collection... +{ + gswapColumns.ndarray( 3, 2, 0, 1, 5, 2, 1, 0 ); // $ExpectError + gswapColumns.ndarray( 3, 2, 0, 1, true, 2, 1, 0 ); // $ExpectError + gswapColumns.ndarray( 3, 2, 0, 1, false, 2, 1, 0 ); // $ExpectError + gswapColumns.ndarray( 3, 2, 0, 1, null, 2, 1, 0 ); // $ExpectError + gswapColumns.ndarray( 3, 2, 0, 1, void 0, 2, 1, 0 ); // $ExpectError + gswapColumns.ndarray( 3, 2, 0, 1, {}, 2, 1, 0 ); // $ExpectError + gswapColumns.ndarray( 3, 2, 0, 1, ( x: number ): number => x, 2, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a sixth argument which is not a number... +{ + const A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + + gswapColumns.ndarray( 3, 2, 0, 1, A, '5', 1, 0 ); // $ExpectError + gswapColumns.ndarray( 3, 2, 0, 1, A, true, 1, 0 ); // $ExpectError + gswapColumns.ndarray( 3, 2, 0, 1, A, false, 1, 0 ); // $ExpectError + gswapColumns.ndarray( 3, 2, 0, 1, A, null, 1, 0 ); // $ExpectError + gswapColumns.ndarray( 3, 2, 0, 1, A, void 0, 1, 0 ); // $ExpectError + gswapColumns.ndarray( 3, 2, 0, 1, A, [], 1, 0 ); // $ExpectError + gswapColumns.ndarray( 3, 2, 0, 1, A, {}, 1, 0 ); // $ExpectError + gswapColumns.ndarray( 3, 2, 0, 1, A, ( x: number ): number => x, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a seventh argument which is not a number... +{ + const A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + + gswapColumns.ndarray( 3, 2, 0, 1, A, 2, '5', 0 ); // $ExpectError + gswapColumns.ndarray( 3, 2, 0, 1, A, 2, true, 0 ); // $ExpectError + gswapColumns.ndarray( 3, 2, 0, 1, A, 2, false, 0 ); // $ExpectError + gswapColumns.ndarray( 3, 2, 0, 1, A, 2, null, 0 ); // $ExpectError + gswapColumns.ndarray( 3, 2, 0, 1, A, 2, void 0, 0 ); // $ExpectError + gswapColumns.ndarray( 3, 2, 0, 1, A, 2, [], 0 ); // $ExpectError + gswapColumns.ndarray( 3, 2, 0, 1, A, 2, {}, 0 ); // $ExpectError + gswapColumns.ndarray( 3, 2, 0, 1, A, 2, ( x: number ): number => x, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided an eighth argument which is not a number... +{ + const A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + + gswapColumns.ndarray( 3, 2, 0, 1, A, 2, 1, '5' ); // $ExpectError + gswapColumns.ndarray( 3, 2, 0, 1, A, 2, 1, true ); // $ExpectError + gswapColumns.ndarray( 3, 2, 0, 1, A, 2, 1, false ); // $ExpectError + gswapColumns.ndarray( 3, 2, 0, 1, A, 2, 1, null ); // $ExpectError + gswapColumns.ndarray( 3, 2, 0, 1, A, 2, 1, void 0 ); // $ExpectError + gswapColumns.ndarray( 3, 2, 0, 1, A, 2, 1, [] ); // $ExpectError + gswapColumns.ndarray( 3, 2, 0, 1, A, 2, 1, {} ); // $ExpectError + gswapColumns.ndarray( 3, 2, 0, 1, A, 2, 1, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided an unsupported number of arguments... +{ + const A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + + gswapColumns.ndarray(); // $ExpectError + gswapColumns.ndarray( 3 ); // $ExpectError + gswapColumns.ndarray( 3, 2 ); // $ExpectError + gswapColumns.ndarray( 3, 2, 0 ); // $ExpectError + gswapColumns.ndarray( 3, 2, 0, 1 ); // $ExpectError + gswapColumns.ndarray( 3, 2, 0, 1, A ); // $ExpectError + gswapColumns.ndarray( 3, 2, 0, 1, A, 2 ); // $ExpectError + gswapColumns.ndarray( 3, 2, 0, 1, A, 2, 1 ); // $ExpectError + gswapColumns.ndarray( 3, 2, 0, 1, A, 2, 1, 0, 0 ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/blas/ext/base/gswap-columns/examples/index.js b/lib/node_modules/@stdlib/blas/ext/base/gswap-columns/examples/index.js new file mode 100644 index 000000000000..b29642ee79aa --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gswap-columns/examples/index.js @@ -0,0 +1,39 @@ +/** +* @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 ndarray2array = require( '@stdlib/ndarray/base/to-array' ); +var uniform = require( '@stdlib/random/array/discrete-uniform' ); +var numel = require( '@stdlib/ndarray/base/numel' ); +var shape2strides = require( '@stdlib/ndarray/base/shape2strides' ); +var gswapColumns = require( './../lib' ); + +var shape = [ 5, 8 ]; +var order = 'row-major'; +var strides = shape2strides( shape, order ); + +var N = numel( shape ); + +var A = uniform( N, -10, 10, { + 'dtype': 'generic' +}); +console.log( ndarray2array( A, shape, strides, 0, order ) ); + +gswapColumns( order, shape[ 0 ], shape[ 1 ], 0, -1, A, strides[ 0 ] ); +console.log( ndarray2array( A, shape, strides, 0, order ) ); diff --git a/lib/node_modules/@stdlib/blas/ext/base/gswap-columns/lib/accessors.js b/lib/node_modules/@stdlib/blas/ext/base/gswap-columns/lib/accessors.js new file mode 100644 index 000000000000..00cc892fb3ac --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gswap-columns/lib/accessors.js @@ -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. +*/ + +'use strict'; + +// MAIN // + +/** +* Interchanges two columns in a matrix `A`. +* +* ## Notes +* +* - If provided negative column indices, the function resolves the indices relative to the last column, with the last column corresponding to `-1`. +* +* @private +* @param {NonNegativeInteger} M - number of rows in matrix `A` +* @param {NonNegativeInteger} i - index of the first column to interchange +* @param {NonNegativeInteger} j - index of the second column to interchange +* @param {Object} A - input matrix object +* @param {Collection} A.data - input matrix data +* @param {Array} A.accessors - matrix element accessors +* @param {integer} strideA1 - stride length for the first dimension of `A` +* @param {integer} strideA2 - stride length for the second dimension of `A` +* @param {NonNegativeInteger} offsetA - starting index for `A` +* @returns {Object} `A` +* +* @example +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +* var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +* +* var A = arraylike2object( toAccessorArray( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ) ); +* +* gswapColumns( 3, 0, 1, A, 2, 1, 0 ); +* // A.data => [ 2.0, 1.0, 4.0, 3.0, 6.0, 5.0 ] +*/ +function gswapColumns( M, i, j, A, strideA1, strideA2, offsetA ) { + var abuf; + var aget; + var aset; + var tmp; + var ia; + var ja; + var i1; + + // Cache a reference to the array data: + abuf = A.data; + + // Cache references to the element accessors: + aget = A.accessors[ 0 ]; + aset = A.accessors[ 1 ]; + + ia = offsetA + ( i*strideA2 ); + ja = offsetA + ( j*strideA2 ); + for ( i1 = 0; i1 < M; i1++ ) { + tmp = aget( abuf, ia ); + aset( abuf, ia, aget( abuf, ja ) ); + aset( abuf, ja, tmp ); + ia += strideA1; + ja += strideA1; + } + return A; +} + + +// EXPORTS // + +module.exports = gswapColumns; diff --git a/lib/node_modules/@stdlib/blas/ext/base/gswap-columns/lib/index.js b/lib/node_modules/@stdlib/blas/ext/base/gswap-columns/lib/index.js new file mode 100644 index 000000000000..9f3cb25501dc --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gswap-columns/lib/index.js @@ -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. +*/ + +'use strict'; + +/** +* Interchange two columns in a matrix `A`. +* +* @module @stdlib/blas/ext/base/gswap-columns +* +* @example +* var gswapColumns = require( '@stdlib/blas/ext/base/gswap-columns' ); +* +* var A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; // => [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +* +* gswapColumns( 'row-major', 3, 2, 0, 1, A, 2 ); +* // A => [ 2.0, 1.0, 4.0, 3.0, 6.0, 5.0 ] +* +* @example +* var gswapColumns = require( '@stdlib/blas/ext/base/gswap-columns' ); +* +* var A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; // => [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +* +* gswapColumns.ndarray( 3, 2, 0, 1, A, 2, 1, 0 ); +* // A => [ 2.0, 1.0, 4.0, 3.0, 6.0, 5.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; + +// exports: { "ndarray": "main.ndarray" } diff --git a/lib/node_modules/@stdlib/blas/ext/base/gswap-columns/lib/main.js b/lib/node_modules/@stdlib/blas/ext/base/gswap-columns/lib/main.js new file mode 100644 index 000000000000..83cd570db76c --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gswap-columns/lib/main.js @@ -0,0 +1,94 @@ +/** +* @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 isRowMajor = require( '@stdlib/ndarray/base/assert/is-row-major-string' ); +var normalizeIndex = require( '@stdlib/ndarray/base/normalize-index' ); +var max = require( '@stdlib/math/base/special/fast/max' ); +var format = require( '@stdlib/string/format' ); +var ndarray = require( './ndarray.js' ); + + +// MAIN // + +/** +* Interchanges two columns in a matrix `A`. +* +* ## Notes +* +* - If provided negative column indices, the function resolves the indices relative to the last column, with the last column corresponding to `-1`. +* +* @param {string} order - storage layout of `A` +* @param {NonNegativeInteger} M - number of rows in matrix `A` +* @param {NonNegativeInteger} N - number of columns in matrix `A` +* @param {integer} i - index of the first column to interchange +* @param {integer} j - index of the second column to interchange +* @param {Collection} A - input matrix +* @param {PositiveInteger} LDA - stride length for 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} fourth argument must resolve to a valid column index +* @throws {RangeError} fifth argument must resolve to a valid column index +* @throws {RangeError} seventh argument must be greater than or equal to max(1,M) when `A` is stored in column-major order and max(1,N) when `A` is stored in row-major order +* @returns {Collection} `A` +* +* @example +* var A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; // => [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +* +* gswapColumns( 'row-major', 3, 2, 0, 1, A, 2 ); +* // A => [ 2.0, 1.0, 4.0, 3.0, 6.0, 5.0 ] +*/ +function gswapColumns( order, M, N, i, j, A, LDA ) { + var sa1; + var sa2; + var io; + var jo; + var s; + if ( !isLayout( order ) ) { + throw new TypeError( format( 'invalid argument. First argument must be a valid order. Value: `%s`.', order ) ); + } + io = normalizeIndex( i, N-1 ); + if ( io < 0 ) { + throw new RangeError( format( 'invalid argument. Fourth argument must resolve to a valid column index. Value: `%d`.', i ) ); + } + jo = normalizeIndex( j, N-1 ); + if ( jo < 0 ) { + throw new RangeError( format( 'invalid argument. Fifth argument must resolve to a valid column index. Value: `%d`.', j ) ); + } + if ( isRowMajor( order ) ) { + s = N; + sa1 = LDA; + sa2 = 1; + } else { + s = M; + sa1 = 1; + sa2 = LDA; + } + 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 ) ); + } + return ndarray( M, N, io, jo, A, sa1, sa2, 0 ); +} + + +// EXPORTS // + +module.exports = gswapColumns; diff --git a/lib/node_modules/@stdlib/blas/ext/base/gswap-columns/lib/ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/gswap-columns/lib/ndarray.js new file mode 100644 index 000000000000..fb12dcd7148b --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gswap-columns/lib/ndarray.js @@ -0,0 +1,102 @@ +/** +* @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 isColumnMajor = require( '@stdlib/ndarray/base/assert/is-column-major' ); +var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +var normalizeIndex = require( '@stdlib/ndarray/base/normalize-index' ); +var gswap = require( '@stdlib/blas/base/gswap' ).ndarray; +var format = require( '@stdlib/string/format' ); +var accessors = require( './accessors.js' ); + + +// MAIN // + +/** +* Interchanges two columns in a matrix `A` using alternative indexing semantics. +* +* ## Notes +* +* - If provided negative column indices, the function resolves the indices relative to the last column, with the last column corresponding to `-1`. +* +* @param {NonNegativeInteger} M - number of rows in matrix `A` +* @param {NonNegativeInteger} N - number of columns in matrix `A` +* @param {integer} i - index of the first column to interchange +* @param {integer} j - index of the second column to interchange +* @param {Collection} A - input matrix +* @param {integer} strideA1 - stride length for the first dimension of `A` +* @param {integer} strideA2 - stride length for the second dimension of `A` +* @param {NonNegativeInteger} offsetA - starting index for `A` +* @throws {RangeError} third argument must resolve to a valid column index +* @throws {RangeError} fourth argument must resolve to a valid column index +* @returns {Collection} `A` +* +* @example +* var A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; // => [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +* +* gswapColumns( 3, 2, 0, 1, A, 2, 1, 0 ); +* // A => [ 2.0, 1.0, 4.0, 3.0, 6.0, 5.0 ] +*/ +function gswapColumns( M, N, i, j, A, strideA1, strideA2, offsetA ) { + var tmp; + var io; + var jo; + var oa; + var ia; + var ja; + var i1; + + io = normalizeIndex( i, N-1 ); + if ( io < 0 ) { + throw new RangeError( format( 'invalid argument. Third argument must resolve to a valid column index. Value: `%d`.', i ) ); + } + jo = normalizeIndex( j, N-1 ); + if ( jo < 0 ) { + throw new RangeError( format( 'invalid argument. Fourth argument must resolve to a valid column index. Value: `%d`.', j ) ); + } + if ( io === jo ) { + return A; + } + if ( isColumnMajor( [ strideA1, strideA2 ] ) ) { + gswap( M, A, strideA1, offsetA+(io*strideA2), A, strideA1, offsetA+(jo*strideA2) ); // eslint-disable-line max-len + return A; + } + oa = arraylike2object( A ); + if ( oa.accessorProtocol ) { + accessors( M, io, jo, oa, strideA1, strideA2, offsetA ); + return A; + } + ia = offsetA + ( io*strideA2 ); + ja = offsetA + ( jo*strideA2 ); + for ( i1 = 0; i1 < M; i1++ ) { + tmp = A[ ia ]; + A[ ia ] = A[ ja ]; + A[ ja ] = tmp; + ia += strideA1; + ja += strideA1; + } + return A; +} + + +// EXPORTS // + +module.exports = gswapColumns; diff --git a/lib/node_modules/@stdlib/blas/ext/base/gswap-columns/package.json b/lib/node_modules/@stdlib/blas/ext/base/gswap-columns/package.json new file mode 100644 index 000000000000..1e7c876cdad2 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gswap-columns/package.json @@ -0,0 +1,67 @@ +{ + "name": "@stdlib/blas/ext/base/gswap-columns", + "version": "0.0.0", + "description": "Interchange two columns in a matrix A.", + "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", + "blas", + "extended", + "ext", + "linear", + "algebra", + "subroutines", + "matrix", + "swap", + "interchange", + "columns", + "column", + "array", + "ndarray" + ] +} diff --git a/lib/node_modules/@stdlib/blas/ext/base/gswap-columns/test/test.js b/lib/node_modules/@stdlib/blas/ext/base/gswap-columns/test/test.js new file mode 100644 index 000000000000..bdb74e37eef6 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gswap-columns/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 gswapColumns = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof gswapColumns, '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 gswapColumns.ndarray, 'function', 'method is a function' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/blas/ext/base/gswap-columns/test/test.main.js b/lib/node_modules/@stdlib/blas/ext/base/gswap-columns/test/test.main.js new file mode 100644 index 000000000000..f93176afd682 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gswap-columns/test/test.main.js @@ -0,0 +1,433 @@ +/** +* @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 toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +var gswapColumns = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof gswapColumns, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 7', function test( t ) { + t.strictEqual( gswapColumns.length, 7, 'returns expected value' ); + t.end(); +}); + +tape( 'the function throws an error if provided an invalid first argument', function test( t ) { + var values; + var i; + + values = [ + 'foo', + 'bar', + 'beep', + 'boop', + -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() { + var A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + gswapColumns( value, 3, 2, 0, 1, A, 2 ); + }; + } +}); + +tape( 'the function throws an error if provided a fourth argument which does not resolve to a valid column index', function test( t ) { + var values; + var i; + + values = [ 2, 3, -3, -4 ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + var A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + gswapColumns( 'row-major', 3, 2, value, 1, A, 2 ); + }; + } +}); + +tape( 'the function throws an error if provided a fifth argument which does not resolve to a valid column index', function test( t ) { + var values; + var i; + + values = [ 2, 3, -3, -4 ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + var A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + gswapColumns( 'row-major', 3, 2, 0, value, A, 2 ); + }; + } +}); + +tape( 'the function throws an error if provided a seventh argument which is not a valid LDA value (row-major)', function test( t ) { + var values; + var i; + + values = [ 0, 1 ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + var A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + gswapColumns( 'row-major', 3, 2, 0, 1, A, value ); + }; + } +}); + +tape( 'the function throws an error if provided a seventh argument which is not a valid LDA value (column-major)', function test( t ) { + var values; + var i; + + values = [ 0, 2 ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + var A = [ 1.0, 4.0, 2.0, 5.0, 3.0, 6.0 ]; + gswapColumns( 'column-major', 3, 2, 0, 1, A, value ); + }; + } +}); + +tape( 'the function interchanges two columns in a matrix (row-major)', 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 ]; + + gswapColumns( 'row-major', 3, 3, 0, 2, A, 3 ); + + expected = [ 3.0, 2.0, 1.0, 6.0, 5.0, 4.0, 9.0, 8.0, 7.0 ]; + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function interchanges two columns in a matrix (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 ]; + + gswapColumns( 'row-major', 3, 3, 0, 2, toAccessorArray( A ), 3 ); + + expected = [ 3.0, 2.0, 1.0, 6.0, 5.0, 4.0, 9.0, 8.0, 7.0 ]; + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function interchanges two columns in a matrix (column-major)', function test( t ) { + var expected; + var A; + + A = [ 1.0, 4.0, 7.0, 2.0, 5.0, 8.0, 3.0, 6.0, 9.0 ]; + + gswapColumns( 'column-major', 3, 3, 0, 2, A, 3 ); + + expected = [ 3.0, 6.0, 9.0, 2.0, 5.0, 8.0, 1.0, 4.0, 7.0 ]; + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function interchanges two columns in a matrix (column-major, accessors)', function test( t ) { + var expected; + var A; + + A = [ 1.0, 4.0, 7.0, 2.0, 5.0, 8.0, 3.0, 6.0, 9.0 ]; + + gswapColumns( 'column-major', 3, 3, 0, 2, toAccessorArray( A ), 3 ); + + expected = [ 3.0, 6.0, 9.0, 2.0, 5.0, 8.0, 1.0, 4.0, 7.0 ]; + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports negative column indices', 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 ]; + + gswapColumns( 'row-major', 3, 3, -3, -1, A, 3 ); + + expected = [ 3.0, 2.0, 1.0, 6.0, 5.0, 4.0, 9.0, 8.0, 7.0 ]; + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports negative column indices (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 ]; + + gswapColumns( 'row-major', 3, 3, -3, -1, toAccessorArray( A ), 3 ); + + expected = [ 3.0, 2.0, 1.0, 6.0, 5.0, 4.0, 9.0, 8.0, 7.0 ]; + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports non-square matrices (row-major)', function test( t ) { + var expected; + var A; + + A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + + gswapColumns( 'row-major', 2, 3, 0, 1, A, 3 ); + + expected = [ 2.0, 1.0, 3.0, 5.0, 4.0, 6.0 ]; + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports non-square matrices (row-major, accessors)', function test( t ) { + var expected; + var A; + + A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + + gswapColumns( 'row-major', 2, 3, 0, 1, toAccessorArray( A ), 3 ); + + expected = [ 2.0, 1.0, 3.0, 5.0, 4.0, 6.0 ]; + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports non-square matrices (column-major)', function test( t ) { + var expected; + var A; + + A = [ 1.0, 4.0, 2.0, 5.0, 3.0, 6.0 ]; + + gswapColumns( 'column-major', 2, 3, 0, 1, A, 2 ); + + expected = [ 2.0, 5.0, 1.0, 4.0, 3.0, 6.0 ]; + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports non-square matrices (column-major, accessors)', function test( t ) { + var expected; + var A; + + A = [ 1.0, 4.0, 2.0, 5.0, 3.0, 6.0 ]; + + gswapColumns( 'column-major', 2, 3, 0, 1, toAccessorArray( A ), 2 ); + + expected = [ 2.0, 5.0, 1.0, 4.0, 3.0, 6.0 ]; + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports an `LDA` parameter greater than the number of columns (row-major)', function test( t ) { + var expected; + var A; + + A = [ 1.0, 2.0, 999.0, 999.0, 3.0, 4.0, 999.0, 999.0 ]; + + gswapColumns( 'row-major', 2, 2, 0, 1, A, 4 ); + + expected = [ 2.0, 1.0, 999.0, 999.0, 4.0, 3.0, 999.0, 999.0 ]; + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports an `LDA` parameter greater than the number of columns (row-major, accessors)', function test( t ) { + var expected; + var A; + + A = [ 1.0, 2.0, 999.0, 999.0, 3.0, 4.0, 999.0, 999.0 ]; + + gswapColumns( 'row-major', 2, 2, 0, 1, toAccessorArray( A ), 4 ); + + expected = [ 2.0, 1.0, 999.0, 999.0, 4.0, 3.0, 999.0, 999.0 ]; + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports an `LDA` parameter greater than the number of rows (column-major)', function test( t ) { + var expected; + var A; + + A = [ 1.0, 3.0, 999.0, 999.0, 2.0, 4.0, 999.0, 999.0 ]; + + gswapColumns( 'column-major', 2, 2, 0, 1, A, 4 ); + + expected = [ 2.0, 4.0, 999.0, 999.0, 1.0, 3.0, 999.0, 999.0 ]; + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports an `LDA` parameter greater than the number of rows (column-major, accessors)', function test( t ) { + var expected; + var A; + + A = [ 1.0, 3.0, 999.0, 999.0, 2.0, 4.0, 999.0, 999.0 ]; + + gswapColumns( 'column-major', 2, 2, 0, 1, toAccessorArray( A ), 4 ); + + expected = [ 2.0, 4.0, 999.0, 999.0, 1.0, 3.0, 999.0, 999.0 ]; + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided equal column indices, the function returns the input matrix unchanged', function test( t ) { + var expected; + var A; + + A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + + gswapColumns( 'row-major', 3, 2, 1, 1, A, 2 ); + + expected = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided equal column indices, the function returns the input matrix unchanged (accessors)', function test( t ) { + var expected; + var A; + + A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + + gswapColumns( 'row-major', 3, 2, 1, 1, toAccessorArray( A ), 2 ); + + expected = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided column indices resolving to the same column, the function returns the input matrix unchanged', function test( t ) { + var expected; + var A; + + A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + + gswapColumns( 'row-major', 3, 2, 1, -1, A, 2 ); + + expected = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided column indices resolving to the same column, the function returns the input matrix unchanged (accessors)', function test( t ) { + var expected; + var A; + + A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + + gswapColumns( 'row-major', 3, 2, 1, -1, toAccessorArray( A ), 2 ); + + expected = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided a second matrix dimension equal to zero, the function throws an error', function test( t ) { + t.throws( badValue, RangeError, 'throws an error' ); + t.end(); + + function badValue() { + var A = [ 1.0, 2.0, 3.0, 4.0 ]; + gswapColumns( 'row-major', 2, 0, 0, 1, A, 2 ); + } +}); + +tape( 'if provided a first matrix dimension equal to zero, the function returns the input matrix unchanged', function test( t ) { + var expected; + var A; + + A = [ 1.0, 2.0, 3.0, 4.0 ]; + + gswapColumns( 'row-major', 0, 2, 0, 1, A, 2 ); + + expected = [ 1.0, 2.0, 3.0, 4.0 ]; + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided a first matrix dimension equal to zero, the function returns the input matrix unchanged (accessors)', function test( t ) { + var expected; + var A; + + A = [ 1.0, 2.0, 3.0, 4.0 ]; + + gswapColumns( 'row-major', 0, 2, 0, 1, toAccessorArray( A ), 2 ); + + expected = [ 1.0, 2.0, 3.0, 4.0 ]; + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); diff --git a/lib/node_modules/@stdlib/blas/ext/base/gswap-columns/test/test.ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/gswap-columns/test/test.ndarray.js new file mode 100644 index 000000000000..40219fa742ec --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gswap-columns/test/test.ndarray.js @@ -0,0 +1,421 @@ +/** +* @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 toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +var gswapColumns = require( './../lib' ).ndarray; + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof gswapColumns, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 8', function test( t ) { + t.strictEqual( gswapColumns.length, 8, 'returns expected value' ); + t.end(); +}); + +tape( 'the function throws an error if provided a third argument which does not resolve to a valid column index', function test( t ) { + var values; + var i; + + values = [ 2, 3, -3, -4 ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + var A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + gswapColumns( 3, 2, value, 1, A, 2, 1, 0 ); + }; + } +}); + +tape( 'the function throws an error if provided a fourth argument which does not resolve to a valid column index', function test( t ) { + var values; + var i; + + values = [ 2, 3, -3, -4 ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + var A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + gswapColumns( 3, 2, 0, value, A, 2, 1, 0 ); + }; + } +}); + +tape( 'if provided a second matrix dimension equal to zero, the function throws an error', function test( t ) { + t.throws( badValue, RangeError, 'throws an error' ); + t.end(); + + function badValue() { + var A = [ 1.0, 2.0, 3.0, 4.0 ]; + gswapColumns( 2, 0, 0, 1, A, 2, 1, 0 ); + } +}); + +tape( 'the function interchanges two columns in a matrix (row-major)', 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 ]; + + gswapColumns( 3, 3, 0, 2, A, 3, 1, 0 ); + + expected = [ 3.0, 2.0, 1.0, 6.0, 5.0, 4.0, 9.0, 8.0, 7.0 ]; + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function interchanges two columns in a matrix (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 ]; + + gswapColumns( 3, 3, 0, 2, toAccessorArray( A ), 3, 1, 0 ); + + expected = [ 3.0, 2.0, 1.0, 6.0, 5.0, 4.0, 9.0, 8.0, 7.0 ]; + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function interchanges two columns in a matrix (column-major)', function test( t ) { + var expected; + var A; + + A = [ 1.0, 4.0, 7.0, 2.0, 5.0, 8.0, 3.0, 6.0, 9.0 ]; + + gswapColumns( 3, 3, 0, 2, A, 1, 3, 0 ); + + expected = [ 3.0, 6.0, 9.0, 2.0, 5.0, 8.0, 1.0, 4.0, 7.0 ]; + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function interchanges two columns in a matrix (column-major, accessors)', function test( t ) { + var expected; + var A; + + A = [ 1.0, 4.0, 7.0, 2.0, 5.0, 8.0, 3.0, 6.0, 9.0 ]; + + gswapColumns( 3, 3, 0, 2, toAccessorArray( A ), 1, 3, 0 ); + + expected = [ 3.0, 6.0, 9.0, 2.0, 5.0, 8.0, 1.0, 4.0, 7.0 ]; + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports negative column indices', 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 ]; + + gswapColumns( 3, 3, -3, -1, A, 3, 1, 0 ); + + expected = [ 3.0, 2.0, 1.0, 6.0, 5.0, 4.0, 9.0, 8.0, 7.0 ]; + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports negative column indices (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 ]; + + gswapColumns( 3, 3, -3, -1, toAccessorArray( A ), 3, 1, 0 ); + + expected = [ 3.0, 2.0, 1.0, 6.0, 5.0, 4.0, 9.0, 8.0, 7.0 ]; + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports non-square matrices (row-major)', function test( t ) { + var expected; + var A; + + A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + + gswapColumns( 2, 3, 0, 1, A, 3, 1, 0 ); + + expected = [ 2.0, 1.0, 3.0, 5.0, 4.0, 6.0 ]; + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports non-square matrices (row-major, accessors)', function test( t ) { + var expected; + var A; + + A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + + gswapColumns( 2, 3, 0, 1, toAccessorArray( A ), 3, 1, 0 ); + + expected = [ 2.0, 1.0, 3.0, 5.0, 4.0, 6.0 ]; + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports non-square matrices (column-major)', function test( t ) { + var expected; + var A; + + A = [ 1.0, 4.0, 2.0, 5.0, 3.0, 6.0 ]; + + gswapColumns( 2, 3, 0, 1, A, 1, 2, 0 ); + + expected = [ 2.0, 5.0, 1.0, 4.0, 3.0, 6.0 ]; + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports non-square matrices (column-major, accessors)', function test( t ) { + var expected; + var A; + + A = [ 1.0, 4.0, 2.0, 5.0, 3.0, 6.0 ]; + + gswapColumns( 2, 3, 0, 1, toAccessorArray( A ), 1, 2, 0 ); + + expected = [ 2.0, 5.0, 1.0, 4.0, 3.0, 6.0 ]; + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports an `A` offset', function test( t ) { + var expected; + var A; + + A = [ 999.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + + gswapColumns( 3, 2, 0, 1, A, 2, 1, 1 ); + + expected = [ 999.0, 2.0, 1.0, 4.0, 3.0, 6.0, 5.0 ]; + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports an `A` offset (accessors)', function test( t ) { + var expected; + var A; + + A = [ 999.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + + gswapColumns( 3, 2, 0, 1, toAccessorArray( A ), 2, 1, 1 ); + + expected = [ 999.0, 2.0, 1.0, 4.0, 3.0, 6.0, 5.0 ]; + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports negative strides (row-major)', function test( t ) { + var expected; + var A; + + A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + + gswapColumns( 3, 2, 0, 1, A, -2, 1, 4 ); + + expected = [ 2.0, 1.0, 4.0, 3.0, 6.0, 5.0 ]; + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports negative strides (row-major, accessors)', function test( t ) { + var expected; + var A; + + A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + + gswapColumns( 3, 2, 0, 1, toAccessorArray( A ), -2, 1, 4 ); + + expected = [ 2.0, 1.0, 4.0, 3.0, 6.0, 5.0 ]; + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports negative strides (column-major)', function test( t ) { + var expected; + var A; + + A = [ 1.0, 4.0, 2.0, 5.0, 3.0, 6.0 ]; + + gswapColumns( 2, 3, 0, 2, A, 1, -2, 4 ); + + expected = [ 3.0, 6.0, 2.0, 5.0, 1.0, 4.0 ]; + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports negative strides (column-major, accessors)', function test( t ) { + var expected; + var A; + + A = [ 1.0, 4.0, 2.0, 5.0, 3.0, 6.0 ]; + + gswapColumns( 2, 3, 0, 2, toAccessorArray( A ), 1, -2, 4 ); + + expected = [ 3.0, 6.0, 2.0, 5.0, 1.0, 4.0 ]; + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports complex access patterns', function test( t ) { + var expected; + var A; + + A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ]; + + gswapColumns( 2, 2, 0, 1, A, 4, 2, 0 ); + + expected = [ 3.0, 2.0, 1.0, 4.0, 7.0, 6.0, 5.0, 8.0 ]; + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports complex access patterns (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 ]; + + gswapColumns( 2, 2, 0, 1, toAccessorArray( A ), 4, 2, 0 ); + + expected = [ 3.0, 2.0, 1.0, 4.0, 7.0, 6.0, 5.0, 8.0 ]; + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided equal column indices, the function returns the input matrix unchanged', function test( t ) { + var expected; + var A; + + A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + + gswapColumns( 3, 2, 1, 1, A, 2, 1, 0 ); + + expected = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided equal column indices, the function returns the input matrix unchanged (accessors)', function test( t ) { + var expected; + var A; + + A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + + gswapColumns( 3, 2, 1, 1, toAccessorArray( A ), 2, 1, 0 ); + + expected = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided column indices resolving to the same column, the function returns the input matrix unchanged', function test( t ) { + var expected; + var A; + + A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + + gswapColumns( 3, 2, 1, -1, A, 2, 1, 0 ); + + expected = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided column indices resolving to the same column, the function returns the input matrix unchanged (accessors)', function test( t ) { + var expected; + var A; + + A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + + gswapColumns( 3, 2, 1, -1, toAccessorArray( A ), 2, 1, 0 ); + + expected = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided a first matrix dimension equal to zero, the function returns the input matrix unchanged', function test( t ) { + var expected; + var A; + + A = [ 1.0, 2.0, 3.0, 4.0 ]; + + gswapColumns( 0, 2, 0, 1, A, 2, 1, 0 ); + + expected = [ 1.0, 2.0, 3.0, 4.0 ]; + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided a first matrix dimension equal to zero, the function returns the input matrix unchanged (accessors)', function test( t ) { + var expected; + var A; + + A = [ 1.0, 2.0, 3.0, 4.0 ]; + + gswapColumns( 0, 2, 0, 1, toAccessorArray( A ), 2, 1, 0 ); + + expected = [ 1.0, 2.0, 3.0, 4.0 ]; + t.deepEqual( A, expected, 'returns expected value' ); + + t.end(); +});