diff --git a/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/README.md b/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/README.md new file mode 100644 index 000000000000..a7d6294865b4 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/README.md @@ -0,0 +1,182 @@ + + +# gswapRows + +> Interchange two rows of a matrix. + +
+ +## Usage + +```javascript +var gswapRows = require( '@stdlib/blas/ext/base/gswap-rows' ); +``` + +#### gswapRows( order, M, N, i, j, A, LDA ) + +Interchanges two rows of a matrix. + +```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 ]; + +gswapRows( 'row-major', 3, 2, 0, 2, A, 2 ); +// A => [ 5.0, 6.0, 3.0, 4.0, 1.0, 2.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 row to interchange. +- **j**: index of the second row 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( [ 9999.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 + +gswapRows( 'row-major', 3, 2, 0, 2, A1, 2 ); +// A0 => [ 9999.0, 5.0, 6.0, 3.0, 4.0, 1.0, 2.0 ] +``` + +#### gswapRows.ndarray( M, N, i, j, A, strideA1, strideA2, offsetA ) + +Interchanges two rows of a matrix 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 ]; + +gswapRows.ndarray( 3, 2, 0, 2, A, 2, 1, 0 ); +// A => [ 5.0, 6.0, 3.0, 4.0, 1.0, 2.0 ] +``` + +The function has the following parameters: + +- **M**: number of rows in `A`. +- **N**: number of columns in `A`. +- **i**: index of the first row to interchange. +- **j**: index of the second row to interchange. +- **A**: input matrix as a linear array. +- **strideA1**: stride length for the first dimension of `A`. +- **strideA2**: stride length for 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, offset parameters support indexing semantics based on starting indices. For example, + +```javascript +/* + A = [ + [ 1.0, 2.0 ], + [ 3.0, 4.0 ], + [ 5.0, 6.0 ] + ] +*/ +var A = [ 9999.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + +gswapRows.ndarray( 3, 2, 0, 2, A, 2, 1, 1 ); +// A => [ 9999.0, 5.0, 6.0, 3.0, 4.0, 1.0, 2.0 ] +``` + +
+ + + +
+ +## Notes + +- If `i` or `j` is negative, the index is resolved relative to the last row, with the last row corresponding to the value `-1`. +- 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 shape2strides = require( '@stdlib/ndarray/base/shape2strides' ); +var gswapRows = require( '@stdlib/blas/ext/base/gswap-rows' ); + +var shape = [ 3, 3 ]; +var order = 'row-major'; +var strides = shape2strides( shape, order ); + +var A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 ]; +console.log( ndarray2array( A, shape, strides, 0, order ) ); + +gswapRows( order, shape[ 0 ], shape[ 1 ], 0, 2, A, strides[ 0 ] ); +console.log( ndarray2array( A, shape, strides, 0, order ) ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/benchmark/benchmark.js b/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/benchmark/benchmark.js new file mode 100644 index 000000000000..f2f5145d6f67 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/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 gswapRows = 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 = gswapRows( order, N, N, 0, N-1, A, N ); + if ( isnan( z[ 0 ] ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( z[ 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 = 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-rows/benchmark/benchmark.ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/benchmark/benchmark.ndarray.js new file mode 100644 index 000000000000..1da1b6ba9515 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/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 gswapRows = 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 z; + 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++ ) { + z = gswapRows( N, N, 0, N-1, A, sa1, sa2, 0 ); + if ( isnan( z[ 0 ] ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( z[ 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 = 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-rows/docs/repl.txt b/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/docs/repl.txt new file mode 100644 index 000000000000..7930d2b7c746 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/docs/repl.txt @@ -0,0 +1,94 @@ + +{{alias}}( order, M, N, i, j, A, LDA ) + Interchanges two rows of a matrix. + + Indexing is relative to the first index. To introduce an offset, use typed + array views. + + If `i` or `j` is negative, the index is resolved relative to the last row. + + 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 row to interchange. + + j: integer + Index of the second row to interchange. + + A: Array|TypedArray + Input matrix. + + 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, 2, A, 2 ) + [ 5.0, 6.0, 3.0, 4.0, 1.0, 2.0 ] + + +{{alias}}.ndarray( M, N, i, j, A, strideA1, strideA2, offsetA ) + Interchanges two rows of a matrix using alternative indexing semantics. + + While typed array views mandate a view offset based on the underlying + buffer, offset parameters support indexing semantics based on starting + indices. + + If `i` or `j` is negative, the index is resolved relative to the last row. + + Parameters + ---------- + M: integer + Number of rows in `A`. + + N: integer + Number of columns in `A`. + + i: integer + Index of the first row to interchange. + + j: integer + Index of the second row to interchange. + + A: Array|TypedArray + Input matrix. + + strideA1: integer + Stride length for the first dimension of `A`. + + strideA2: integer + Stride length for the second dimension of `A`. + + offsetA: 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, 2, A, 2, 1, 0 ) + [ 5.0, 6.0, 3.0, 4.0, 1.0, 2.0 ] + + See Also + -------- diff --git a/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/docs/types/index.d.ts b/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/docs/types/index.d.ts new file mode 100644 index 000000000000..84f11d699e42 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/docs/types/index.d.ts @@ -0,0 +1,106 @@ +/* +* @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 `gswapRows`. +*/ +interface Routine { + /** + * Interchanges two rows of a matrix. + * + * @param order - storage layout + * @param M - number of rows in `A` + * @param N - number of columns in `A` + * @param i - index of the first row to interchange + * @param j - index of the second row 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 input matrix `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 ] ] + * + * gswapRows( 'row-major', 3, 2, 0, 2, A, 2 ); + * // A => [ 5.0, 6.0, 3.0, 4.0, 1.0, 2.0 ] + */ + ( order: Layout, M: number, N: number, i: number, j: number, A: T, LDA: number ): T; + + /** + * Interchanges two rows of a matrix using alternative indexing semantics. + * + * @param M - number of rows in `A` + * @param N - number of columns in `A` + * @param i - index of the first row to interchange + * @param j - index of the second row 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 input matrix `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 ] ] + * + * gswapRows.ndarray( 3, 2, 0, 2, A, 2, 1, 0 ); + * // A => [ 5.0, 6.0, 3.0, 4.0, 1.0, 2.0 ] + */ + ndarray( M: number, N: number, i: number, j: number, A: T, strideA1: number, strideA2: number, offsetA: number ): T; +} + +/** +* Interchanges two rows of a matrix. +* +* @param order - storage layout +* @param M - number of rows in `A` +* @param N - number of columns in `A` +* @param i - index of the first row to interchange +* @param j - index of the second row 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 input matrix `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 ] ] +* +* gswapRows( 'row-major', 3, 2, 0, 2, A, 2 ); +* // A => [ 5.0, 6.0, 3.0, 4.0, 1.0, 2.0 ] +* +* @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 ] ] +* +* gswapRows.ndarray( 3, 2, 0, 2, A, 2, 1, 0 ); +* // A => [ 5.0, 6.0, 3.0, 4.0, 1.0, 2.0 ] +*/ +declare var gswapRows: Routine; + + +// EXPORTS // + +export = gswapRows; diff --git a/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/docs/types/test.ts b/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/docs/types/test.ts new file mode 100644 index 000000000000..0f62bbb11f2e --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/docs/types/test.ts @@ -0,0 +1,269 @@ +/* +* @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 gswapRows = require( './index' ); + + +// TESTS // + +// The function returns a collection... +{ + const A = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + + gswapRows( 'row-major', 3, 2, 0, 2, A, 2 ); // $ExpectType Float64Array +} + +// The compiler throws an error if the function is provided a first argument which is not a string... +{ + const A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + + gswapRows( 5, 3, 2, 0, 2, A, 2 ); // $ExpectError + gswapRows( true, 3, 2, 0, 2, A, 2 ); // $ExpectError + gswapRows( false, 3, 2, 0, 2, A, 2 ); // $ExpectError + gswapRows( null, 3, 2, 0, 2, A, 2 ); // $ExpectError + gswapRows( void 0, 3, 2, 0, 2, A, 2 ); // $ExpectError + gswapRows( [], 3, 2, 0, 2, A, 2 ); // $ExpectError + gswapRows( {}, 3, 2, 0, 2, A, 2 ); // $ExpectError + gswapRows( ( x: number ): number => x, 3, 2, 0, 2, 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 ]; + + gswapRows( 'row-major', '5', 2, 0, 2, A, 2 ); // $ExpectError + gswapRows( 'row-major', true, 2, 0, 2, A, 2 ); // $ExpectError + gswapRows( 'row-major', false, 2, 0, 2, A, 2 ); // $ExpectError + gswapRows( 'row-major', null, 2, 0, 2, A, 2 ); // $ExpectError + gswapRows( 'row-major', void 0, 2, 0, 2, A, 2 ); // $ExpectError + gswapRows( 'row-major', [], 2, 0, 2, A, 2 ); // $ExpectError + gswapRows( 'row-major', {}, 2, 0, 2, A, 2 ); // $ExpectError + gswapRows( 'row-major', ( x: number ): number => x, 2, 0, 2, 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 ]; + + gswapRows( 'row-major', 3, '5', 0, 2, A, 2 ); // $ExpectError + gswapRows( 'row-major', 3, true, 0, 2, A, 2 ); // $ExpectError + gswapRows( 'row-major', 3, false, 0, 2, A, 2 ); // $ExpectError + gswapRows( 'row-major', 3, null, 0, 2, A, 2 ); // $ExpectError + gswapRows( 'row-major', 3, void 0, 0, 2, A, 2 ); // $ExpectError + gswapRows( 'row-major', 3, [], 0, 2, A, 2 ); // $ExpectError + gswapRows( 'row-major', 3, {}, 0, 2, A, 2 ); // $ExpectError + gswapRows( 'row-major', 3, ( x: number ): number => x, 0, 2, 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 ]; + + gswapRows( 'row-major', 3, 2, '5', 2, A, 2 ); // $ExpectError + gswapRows( 'row-major', 3, 2, true, 2, A, 2 ); // $ExpectError + gswapRows( 'row-major', 3, 2, false, 2, A, 2 ); // $ExpectError + gswapRows( 'row-major', 3, 2, null, 2, A, 2 ); // $ExpectError + gswapRows( 'row-major', 3, 2, void 0, 2, A, 2 ); // $ExpectError + gswapRows( 'row-major', 3, 2, [], 2, A, 2 ); // $ExpectError + gswapRows( 'row-major', 3, 2, {}, 2, A, 2 ); // $ExpectError + gswapRows( 'row-major', 3, 2, ( x: number ): number => x, 2, 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 ]; + + gswapRows( 'row-major', 3, 2, 0, '5', A, 2 ); // $ExpectError + gswapRows( 'row-major', 3, 2, 0, true, A, 2 ); // $ExpectError + gswapRows( 'row-major', 3, 2, 0, false, A, 2 ); // $ExpectError + gswapRows( 'row-major', 3, 2, 0, null, A, 2 ); // $ExpectError + gswapRows( 'row-major', 3, 2, 0, void 0, A, 2 ); // $ExpectError + gswapRows( 'row-major', 3, 2, 0, [], A, 2 ); // $ExpectError + gswapRows( 'row-major', 3, 2, 0, {}, A, 2 ); // $ExpectError + gswapRows( '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... +{ + gswapRows( 'row-major', 3, 2, 0, 2, 5, 2 ); // $ExpectError + gswapRows( 'row-major', 3, 2, 0, 2, true, 2 ); // $ExpectError + gswapRows( 'row-major', 3, 2, 0, 2, false, 2 ); // $ExpectError + gswapRows( 'row-major', 3, 2, 0, 2, null, 2 ); // $ExpectError + gswapRows( 'row-major', 3, 2, 0, 2, void 0, 2 ); // $ExpectError + gswapRows( 'row-major', 3, 2, 0, 2, {}, 2 ); // $ExpectError + gswapRows( 'row-major', 3, 2, 0, 2, ( 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 ]; + + gswapRows( 'row-major', 3, 2, 0, 2, A, '5' ); // $ExpectError + gswapRows( 'row-major', 3, 2, 0, 2, A, true ); // $ExpectError + gswapRows( 'row-major', 3, 2, 0, 2, A, false ); // $ExpectError + gswapRows( 'row-major', 3, 2, 0, 2, A, null ); // $ExpectError + gswapRows( 'row-major', 3, 2, 0, 2, A, void 0 ); // $ExpectError + gswapRows( 'row-major', 3, 2, 0, 2, A, [] ); // $ExpectError + gswapRows( 'row-major', 3, 2, 0, 2, A, {} ); // $ExpectError + gswapRows( 'row-major', 3, 2, 0, 2, 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 ]; + + gswapRows(); // $ExpectError + gswapRows( 'row-major' ); // $ExpectError + gswapRows( 'row-major', 3 ); // $ExpectError + gswapRows( 'row-major', 3, 2 ); // $ExpectError + gswapRows( 'row-major', 3, 2, 0 ); // $ExpectError + gswapRows( 'row-major', 3, 2, 0, 2 ); // $ExpectError + gswapRows( 'row-major', 3, 2, 0, 2, A ); // $ExpectError + gswapRows( 'row-major', 3, 2, 0, 2, A, 2, {} ); // $ExpectError +} + +// Attached to main export is an `ndarray` method which returns a collection... +{ + const A = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + + gswapRows.ndarray( 3, 2, 0, 2, A, 2, 1, 0 ); // $ExpectType Float64Array +} + +// 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 ]; + + gswapRows.ndarray( '5', 2, 0, 2, A, 2, 1, 0 ); // $ExpectError + gswapRows.ndarray( true, 2, 0, 2, A, 2, 1, 0 ); // $ExpectError + gswapRows.ndarray( false, 2, 0, 2, A, 2, 1, 0 ); // $ExpectError + gswapRows.ndarray( null, 2, 0, 2, A, 2, 1, 0 ); // $ExpectError + gswapRows.ndarray( void 0, 2, 0, 2, A, 2, 1, 0 ); // $ExpectError + gswapRows.ndarray( [], 2, 0, 2, A, 2, 1, 0 ); // $ExpectError + gswapRows.ndarray( {}, 2, 0, 2, A, 2, 1, 0 ); // $ExpectError + gswapRows.ndarray( ( x: number ): number => x, 2, 0, 2, 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 ]; + + gswapRows.ndarray( 3, '5', 0, 2, A, 2, 1, 0 ); // $ExpectError + gswapRows.ndarray( 3, true, 0, 2, A, 2, 1, 0 ); // $ExpectError + gswapRows.ndarray( 3, false, 0, 2, A, 2, 1, 0 ); // $ExpectError + gswapRows.ndarray( 3, null, 0, 2, A, 2, 1, 0 ); // $ExpectError + gswapRows.ndarray( 3, void 0, 0, 2, A, 2, 1, 0 ); // $ExpectError + gswapRows.ndarray( 3, [], 0, 2, A, 2, 1, 0 ); // $ExpectError + gswapRows.ndarray( 3, {}, 0, 2, A, 2, 1, 0 ); // $ExpectError + gswapRows.ndarray( 3, ( x: number ): number => x, 0, 2, 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 ]; + + gswapRows.ndarray( 3, 2, '5', 2, A, 2, 1, 0 ); // $ExpectError + gswapRows.ndarray( 3, 2, true, 2, A, 2, 1, 0 ); // $ExpectError + gswapRows.ndarray( 3, 2, false, 2, A, 2, 1, 0 ); // $ExpectError + gswapRows.ndarray( 3, 2, null, 2, A, 2, 1, 0 ); // $ExpectError + gswapRows.ndarray( 3, 2, void 0, 2, A, 2, 1, 0 ); // $ExpectError + gswapRows.ndarray( 3, 2, [], 2, A, 2, 1, 0 ); // $ExpectError + gswapRows.ndarray( 3, 2, {}, 2, A, 2, 1, 0 ); // $ExpectError + gswapRows.ndarray( 3, 2, ( x: number ): number => x, 2, 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 ]; + + gswapRows.ndarray( 3, 2, 0, '5', A, 2, 1, 0 ); // $ExpectError + gswapRows.ndarray( 3, 2, 0, true, A, 2, 1, 0 ); // $ExpectError + gswapRows.ndarray( 3, 2, 0, false, A, 2, 1, 0 ); // $ExpectError + gswapRows.ndarray( 3, 2, 0, null, A, 2, 1, 0 ); // $ExpectError + gswapRows.ndarray( 3, 2, 0, void 0, A, 2, 1, 0 ); // $ExpectError + gswapRows.ndarray( 3, 2, 0, [], A, 2, 1, 0 ); // $ExpectError + gswapRows.ndarray( 3, 2, 0, {}, A, 2, 1, 0 ); // $ExpectError + gswapRows.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... +{ + gswapRows.ndarray( 3, 2, 0, 2, 5, 2, 1, 0 ); // $ExpectError + gswapRows.ndarray( 3, 2, 0, 2, true, 2, 1, 0 ); // $ExpectError + gswapRows.ndarray( 3, 2, 0, 2, false, 2, 1, 0 ); // $ExpectError + gswapRows.ndarray( 3, 2, 0, 2, null, 2, 1, 0 ); // $ExpectError + gswapRows.ndarray( 3, 2, 0, 2, void 0, 2, 1, 0 ); // $ExpectError + gswapRows.ndarray( 3, 2, 0, 2, {}, 2, 1, 0 ); // $ExpectError + gswapRows.ndarray( 3, 2, 0, 2, ( 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 ]; + + gswapRows.ndarray( 3, 2, 0, 2, A, '5', 1, 0 ); // $ExpectError + gswapRows.ndarray( 3, 2, 0, 2, A, true, 1, 0 ); // $ExpectError + gswapRows.ndarray( 3, 2, 0, 2, A, false, 1, 0 ); // $ExpectError + gswapRows.ndarray( 3, 2, 0, 2, A, null, 1, 0 ); // $ExpectError + gswapRows.ndarray( 3, 2, 0, 2, A, void 0, 1, 0 ); // $ExpectError + gswapRows.ndarray( 3, 2, 0, 2, A, [], 1, 0 ); // $ExpectError + gswapRows.ndarray( 3, 2, 0, 2, A, {}, 1, 0 ); // $ExpectError + gswapRows.ndarray( 3, 2, 0, 2, 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 ]; + + gswapRows.ndarray( 3, 2, 0, 2, A, 2, '5', 0 ); // $ExpectError + gswapRows.ndarray( 3, 2, 0, 2, A, 2, true, 0 ); // $ExpectError + gswapRows.ndarray( 3, 2, 0, 2, A, 2, false, 0 ); // $ExpectError + gswapRows.ndarray( 3, 2, 0, 2, A, 2, null, 0 ); // $ExpectError + gswapRows.ndarray( 3, 2, 0, 2, A, 2, void 0, 0 ); // $ExpectError + gswapRows.ndarray( 3, 2, 0, 2, A, 2, [], 0 ); // $ExpectError + gswapRows.ndarray( 3, 2, 0, 2, A, 2, {}, 0 ); // $ExpectError + gswapRows.ndarray( 3, 2, 0, 2, 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 ]; + + gswapRows.ndarray( 3, 2, 0, 2, A, 2, 1, '5' ); // $ExpectError + gswapRows.ndarray( 3, 2, 0, 2, A, 2, 1, true ); // $ExpectError + gswapRows.ndarray( 3, 2, 0, 2, A, 2, 1, false ); // $ExpectError + gswapRows.ndarray( 3, 2, 0, 2, A, 2, 1, null ); // $ExpectError + gswapRows.ndarray( 3, 2, 0, 2, A, 2, 1, void 0 ); // $ExpectError + gswapRows.ndarray( 3, 2, 0, 2, A, 2, 1, [] ); // $ExpectError + gswapRows.ndarray( 3, 2, 0, 2, A, 2, 1, {} ); // $ExpectError + gswapRows.ndarray( 3, 2, 0, 2, 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 ]; + + gswapRows.ndarray(); // $ExpectError + gswapRows.ndarray( 3 ); // $ExpectError + gswapRows.ndarray( 3, 2 ); // $ExpectError + gswapRows.ndarray( 3, 2, 0 ); // $ExpectError + gswapRows.ndarray( 3, 2, 0, 2 ); // $ExpectError + gswapRows.ndarray( 3, 2, 0, 2, A ); // $ExpectError + gswapRows.ndarray( 3, 2, 0, 2, A, 2 ); // $ExpectError + gswapRows.ndarray( 3, 2, 0, 2, A, 2, 1 ); // $ExpectError + gswapRows.ndarray( 3, 2, 0, 2, A, 2, 1, 0, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/examples/index.js b/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/examples/index.js new file mode 100644 index 000000000000..9802ce15bf9d --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/examples/index.js @@ -0,0 +1,33 @@ +/** +* @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 shape2strides = require( '@stdlib/ndarray/base/shape2strides' ); +var gswapRows = require( './../lib' ); + +var shape = [ 3, 3 ]; +var order = 'row-major'; +var strides = shape2strides( shape, order ); + +var A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 ]; +console.log( ndarray2array( A, shape, strides, 0, order ) ); + +gswapRows( order, shape[ 0 ], shape[ 1 ], 0, 2, A, strides[ 0 ] ); +console.log( ndarray2array( A, shape, strides, 0, order ) ); diff --git a/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/lib/accessors.js b/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/lib/accessors.js new file mode 100644 index 000000000000..9c4aa2fc7ea0 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/lib/accessors.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'; + +// MAIN // + +/** +* Interchanges two rows of a matrix. +* +* @private +* @param {PositiveInteger} M - number of rows in `A` +* @param {PositiveInteger} N - number of columns in `A` +* @param {NonNegativeInteger} i - index of the first row to interchange +* @param {NonNegativeInteger} j - index of the second row 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 {void} +* +* @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 ]; // => [ [ 1.0, 4.0 ], [ 2.0, 5.0 ], [ 3.0, 6.0 ] ] +* +* gswapRows( 3, 2, 0, 2, arraylike2object( toAccessorArray( A ) ), 1, 3, 0 ); +* // A => [ 3.0, 2.0, 1.0, 6.0, 5.0, 4.0 ] +*/ +function gswapRows( M, N, i, j, A, strideA1, strideA2, offsetA ) { + var abuf; + var aget; + var aset; + var da0; + var da1; + var S0; + var S1; + var vi; + var vj; + var pi; + var pj; + var ia; + var i0; + var i1; + + // Cache references to array data: + abuf = A.data; + + // Cache references to the element accessors: + aget = A.accessors[ 0 ]; + aset = A.accessors[ 1 ]; + + // Column-major... + S0 = M; + S1 = N; + + // Resolve loop offsets: + da0 = strideA1; + da1 = strideA2 - ( S0*strideA1 ); + + ia = offsetA; + for ( i1 = 0; i1 < S1; i1++ ) { + // Scan down the rows in a column to find elements at rows `i` and `j`... + for ( i0 = 0; i0 < S0; i0++ ) { + if ( i0 === i ) { + vi = aget( abuf, ia ); + pi = ia; + } + if ( i0 === j ) { + vj = aget( abuf, ia ); + pj = ia; + } + ia += da0; + } + aset( abuf, pi, vj ); + aset( abuf, pj, vi ); + ia += da1; + } +} + + +// EXPORTS // + +module.exports = gswapRows; diff --git a/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/lib/index.js b/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/lib/index.js new file mode 100644 index 000000000000..1cb07301f2eb --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/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'; + +/** +* Interchange two rows of a matrix. +* +* @module @stdlib/blas/ext/base/gswap-rows +* +* @example +* var gswapRows = require( '@stdlib/blas/ext/base/gswap-rows' ); +* +* 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 ] ] +* +* gswapRows( 'row-major', 3, 2, 0, 2, A, 2 ); +* // A => [ 5.0, 6.0, 3.0, 4.0, 1.0, 2.0 ] +* +* @example +* var gswapRows = require( '@stdlib/blas/ext/base/gswap-rows' ); +* +* 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 ] ] +* +* gswapRows.ndarray( 3, 2, 0, 2, A, 2, 1, 0 ); +* // A => [ 5.0, 6.0, 3.0, 4.0, 1.0, 2.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/gswap-rows/lib/main.js b/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/lib/main.js new file mode 100644 index 000000000000..74f0aa86a091 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/lib/main.js @@ -0,0 +1,81 @@ +/** +* @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 isColumnMajor = require( '@stdlib/ndarray/base/assert/is-column-major-string' ); +var max = require( '@stdlib/math/base/special/fast/max' ); +var format = require( '@stdlib/string/format' ); +var ndarray = require( './ndarray.js' ); + + +// MAIN // + +/** +* Interchanges two rows of a matrix. +* +* @param {string} order - storage layout +* @param {PositiveInteger} M - number of rows in `A` +* @param {PositiveInteger} N - number of columns in `A` +* @param {integer} i - index of the first row to interchange +* @param {integer} j - index of the second row to interchange +* @param {Collection} A - input matrix +* @param {integer} 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} seventh argument must be a valid leading dimension +* @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 ] ] +* +* gswapRows( 'row-major', 3, 2, 0, 2, A, 2 ); +* // A => [ 5.0, 6.0, 3.0, 4.0, 1.0, 2.0 ] +*/ +function gswapRows( order, M, N, i, j, A, LDA ) { + var sa1; + var sa2; + var s; + if ( !isLayout( order ) ) { + throw new TypeError( format( 'invalid argument. First argument must be a valid order. Value: `%s`.', order ) ); + } + if ( isRowMajor( order ) ) { + s = N; + } else { + s = M; + } + 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 ) ) { + sa1 = 1; + sa2 = LDA; + } else { // order === 'row-major' + sa1 = LDA; + sa2 = 1; + } + return ndarray( M, N, i, j, A, sa1, sa2, 0 ); +} + + +// EXPORTS // + +module.exports = gswapRows; diff --git a/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/lib/ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/lib/ndarray.js new file mode 100644 index 000000000000..3890f6aa7828 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/lib/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 isRowMajor = require( '@stdlib/ndarray/base/assert/is-row-major' ); +var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +var gswap = require( '@stdlib/blas/base/gswap' ).ndarray; +var accessors = require( './accessors.js' ); + + +// MAIN // + +/** +* Interchanges two rows of a matrix using alternative indexing semantics. +* +* @param {PositiveInteger} M - number of rows in `A` +* @param {PositiveInteger} N - number of columns in `A` +* @param {integer} i - index of the first row to interchange +* @param {integer} j - index of the second row 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` +* @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 ] ] +* +* gswapRows( 3, 2, 0, 2, A, 2, 1, 0 ); +* // A => [ 5.0, 6.0, 3.0, 4.0, 1.0, 2.0 ] +* +* @example +* var A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; // => [ [ 1.0, 4.0 ], [ 2.0, 5.0 ], [ 3.0, 6.0 ] ] +* +* gswapRows( 3, 2, 0, 2, A, 1, 3, 0 ); +* // A => [ 3.0, 2.0, 1.0, 6.0, 5.0, 4.0 ] +*/ +function gswapRows( M, N, i, j, A, strideA1, strideA2, offsetA ) { + var da0; + var da1; + var S0; + var S1; + var vi; + var vj; + var pi; + var pj; + var ia; + var oa; + var i0; + var i1; + + if ( M <= 0 || N <= 0 ) { + return A; + } + if ( i < 0 ) { + i += M; + } + if ( j < 0 ) { + j += M; + } + if ( i === j ) { + return A; + } + // Row-major... + if ( isRowMajor( [ strideA1, strideA2 ] ) ) { + gswap( N, A, strideA2, offsetA+(i*strideA1), A, strideA2, offsetA+(j*strideA1) ); // eslint-disable-line max-len + return A; + } + // Column-major... + oa = arraylike2object( A ); + if ( oa.accessorProtocol ) { + accessors( M, N, i, j, oa, strideA1, strideA2, offsetA ); + return A; + } + S0 = M; + S1 = N; + + // Resolve loop offsets: + da0 = strideA1; + da1 = strideA2 - ( S0*strideA1 ); + + ia = offsetA; + for ( i1 = 0; i1 < S1; i1++ ) { + // Scan down the rows in a column to find elements at rows `i` and `j`... + for ( i0 = 0; i0 < S0; i0++ ) { + if ( i0 === i ) { + vi = A[ ia ]; + pi = ia; + } + if ( i0 === j ) { + vj = A[ ia ]; + pj = ia; + } + ia += da0; + } + A[ pi ] = vj; + A[ pj ] = vi; + ia += da1; + } + return A; +} + + +// EXPORTS // + +module.exports = gswapRows; diff --git a/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/package.json b/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/package.json new file mode 100644 index 000000000000..04edb653cab8 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/package.json @@ -0,0 +1,65 @@ +{ + "name": "@stdlib/blas/ext/base/gswap-rows", + "version": "0.0.0", + "description": "Interchange two rows of a matrix.", + "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", + "matrix", + "strided", + "array", + "ndarray", + "vector", + "row", + "swap", + "interchange", + "exchange", + "permute" + ] +} diff --git a/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/test/fixtures/column_major.json b/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/test/fixtures/column_major.json new file mode 100644 index 000000000000..778032c538bc --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/test/fixtures/column_major.json @@ -0,0 +1,25 @@ +{ + "order": "column-major", + "A": [ 1.0, 2.0, 2.0, 0.0, 3.0, 4.0, 4.0, 0.0 ], + "M": 4, + "N": 2, + "i": 0, + "j": 2, + "strideA1": 1, + "strideA2": 4, + "offsetA": 0, + "LDA": 4, + "A_mat": [ + [ 1.0, 3.0 ], + [ 2.0, 4.0 ], + [ 2.0, 4.0 ], + [ 0.0, 0.0 ] + ], + "expected": [ 2.0, 2.0, 1.0, 0.0, 4.0, 4.0, 3.0, 0.0 ], + "expected_mat": [ + [ 2.0, 4.0 ], + [ 2.0, 4.0 ], + [ 1.0, 3.0 ], + [ 0.0, 0.0 ] + ] +} diff --git a/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/test/fixtures/large-strides/column_major.json b/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/test/fixtures/large-strides/column_major.json new file mode 100644 index 000000000000..2390dba3b07c --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/test/fixtures/large-strides/column_major.json @@ -0,0 +1,49 @@ +{ + "order": "column-major", + "A": [ + 1.0, + 9999.0, + 2.0, + 9999.0, + 0.0, + 9999.0, + 3.0, + 9999.0, + 4.0, + 9999.0, + 0.0, + 9999.0 + ], + "M": 3, + "N": 2, + "i": 0, + "j": 2, + "strideA1": 2, + "strideA2": 6, + "offsetA": 0, + "LDA": 3, + "A_mat": [ + [ 1.0, 3.0 ], + [ 2.0, 4.0 ], + [ 0.0, 0.0 ] + ], + "expected": [ + 0.0, + 9999.0, + 2.0, + 9999.0, + 1.0, + 9999.0, + 0.0, + 9999.0, + 4.0, + 9999.0, + 3.0, + 9999.0 + ], + "expected_mat": [ + [ 0.0, 0.0 ], + [ 2.0, 4.0 ], + [ 1.0, 3.0 ] + ] +} diff --git a/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/test/fixtures/large-strides/row_major.json b/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/test/fixtures/large-strides/row_major.json new file mode 100644 index 000000000000..0f0095b7eded --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/test/fixtures/large-strides/row_major.json @@ -0,0 +1,49 @@ +{ + "order": "row-major", + "A": [ + 1.0, + 9999.0, + 2.0, + 9999.0, + 3.0, + 9999.0, + 4.0, + 9999.0, + 0.0, + 9999.0, + 0.0, + 9999.0 + ], + "M": 3, + "N": 2, + "i": 0, + "j": 2, + "strideA1": 4, + "strideA2": 2, + "offsetA": 0, + "LDA": 2, + "A_mat": [ + [ 1.0, 2.0 ], + [ 3.0, 4.0 ], + [ 0.0, 0.0 ] + ], + "expected": [ + 0.0, + 9999.0, + 0.0, + 9999.0, + 3.0, + 9999.0, + 4.0, + 9999.0, + 1.0, + 9999.0, + 2.0, + 9999.0 + ], + "expected_mat": [ + [ 0.0, 0.0 ], + [ 3.0, 4.0 ], + [ 1.0, 2.0 ] + ] +} diff --git a/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/test/fixtures/mixed-strides/column_major.json b/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/test/fixtures/mixed-strides/column_major.json new file mode 100644 index 000000000000..1210db79049a --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/test/fixtures/mixed-strides/column_major.json @@ -0,0 +1,37 @@ +{ + "order": "column-major", + "A": [ + 3.0, + 4.0, + 0.0, + 1.0, + 2.0, + 0.0 + ], + "M": 3, + "N": 2, + "i": 0, + "j": 2, + "strideA1": 1, + "strideA2": -3, + "offsetA": 3, + "LDA": 3, + "A_mat": [ + [ 1.0, 3.0 ], + [ 2.0, 4.0 ], + [ 0.0, 0.0 ] + ], + "expected": [ + 0.0, + 4.0, + 3.0, + 0.0, + 2.0, + 1.0 + ], + "expected_mat": [ + [ 0.0, 0.0 ], + [ 2.0, 4.0 ], + [ 1.0, 3.0 ] + ] +} diff --git a/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/test/fixtures/mixed-strides/row_major.json b/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/test/fixtures/mixed-strides/row_major.json new file mode 100644 index 000000000000..60cab89165eb --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/test/fixtures/mixed-strides/row_major.json @@ -0,0 +1,37 @@ +{ + "order": "row-major", + "A": [ + 0.0, + 0.0, + 3.0, + 4.0, + 1.0, + 2.0 + ], + "M": 3, + "N": 2, + "i": 0, + "j": 2, + "strideA1": -2, + "strideA2": 1, + "offsetA": 4, + "LDA": 2, + "A_mat": [ + [ 1.0, 2.0 ], + [ 3.0, 4.0 ], + [ 0.0, 0.0 ] + ], + "expected": [ + 1.0, + 2.0, + 3.0, + 4.0, + 0.0, + 0.0 + ], + "expected_mat": [ + [ 0.0, 0.0 ], + [ 3.0, 4.0 ], + [ 1.0, 2.0 ] + ] +} diff --git a/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/test/fixtures/negative-strides/column_major.json b/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/test/fixtures/negative-strides/column_major.json new file mode 100644 index 000000000000..4d505d8527c2 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/test/fixtures/negative-strides/column_major.json @@ -0,0 +1,37 @@ +{ + "order": "column-major", + "A": [ + 0.0, + 4.0, + 3.0, + 0.0, + 2.0, + 1.0 + ], + "M": 3, + "N": 2, + "i": 0, + "j": 2, + "strideA1": -1, + "strideA2": -3, + "offsetA": 5, + "LDA": 3, + "A_mat": [ + [ 1.0, 3.0 ], + [ 2.0, 4.0 ], + [ 0.0, 0.0 ] + ], + "expected": [ + 3.0, + 4.0, + 0.0, + 1.0, + 2.0, + 0.0 + ], + "expected_mat": [ + [ 0.0, 0.0 ], + [ 2.0, 4.0 ], + [ 1.0, 3.0 ] + ] +} diff --git a/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/test/fixtures/negative-strides/row_major.json b/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/test/fixtures/negative-strides/row_major.json new file mode 100644 index 000000000000..52bca20e7b6f --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/test/fixtures/negative-strides/row_major.json @@ -0,0 +1,37 @@ +{ + "order": "row-major", + "A": [ + 0.0, + 0.0, + 4.0, + 3.0, + 2.0, + 1.0 + ], + "M": 3, + "N": 2, + "i": 0, + "j": 2, + "strideA1": -2, + "strideA2": -1, + "offsetA": 5, + "LDA": 2, + "A_mat": [ + [ 1.0, 2.0 ], + [ 3.0, 4.0 ], + [ 0.0, 0.0 ] + ], + "expected": [ + 2.0, + 1.0, + 4.0, + 3.0, + 0.0, + 0.0 + ], + "expected_mat": [ + [ 0.0, 0.0 ], + [ 3.0, 4.0 ], + [ 1.0, 2.0 ] + ] +} diff --git a/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/test/fixtures/offsets/column_major.json b/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/test/fixtures/offsets/column_major.json new file mode 100644 index 000000000000..b33789ce3a28 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/test/fixtures/offsets/column_major.json @@ -0,0 +1,39 @@ +{ + "order": "column-major", + "A": [ + 9999.0, + 1.0, + 2.0, + 0.0, + 3.0, + 4.0, + 0.0 + ], + "M": 3, + "N": 2, + "i": 0, + "j": 2, + "strideA1": 1, + "strideA2": 3, + "offsetA": 1, + "LDA": 3, + "A_mat": [ + [ 1.0, 3.0 ], + [ 2.0, 4.0 ], + [ 0.0, 0.0 ] + ], + "expected": [ + 9999.0, + 0.0, + 2.0, + 1.0, + 0.0, + 4.0, + 3.0 + ], + "expected_mat": [ + [ 0.0, 0.0 ], + [ 2.0, 4.0 ], + [ 1.0, 3.0 ] + ] +} diff --git a/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/test/fixtures/offsets/row_major.json b/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/test/fixtures/offsets/row_major.json new file mode 100644 index 000000000000..f6efb72d4655 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/test/fixtures/offsets/row_major.json @@ -0,0 +1,39 @@ +{ + "order": "row-major", + "A": [ + 9999.0, + 1.0, + 2.0, + 3.0, + 4.0, + 0.0, + 0.0 + ], + "M": 3, + "N": 2, + "i": 0, + "j": 2, + "strideA1": 2, + "strideA2": 1, + "offsetA": 1, + "LDA": 2, + "A_mat": [ + [ 1.0, 2.0 ], + [ 3.0, 4.0 ], + [ 0.0, 0.0 ] + ], + "expected": [ + 9999.0, + 0.0, + 0.0, + 3.0, + 4.0, + 1.0, + 2.0 + ], + "expected_mat": [ + [ 0.0, 0.0 ], + [ 3.0, 4.0 ], + [ 1.0, 2.0 ] + ] +} diff --git a/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/test/fixtures/row_major.json b/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/test/fixtures/row_major.json new file mode 100644 index 000000000000..116fe770e1ca --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/test/fixtures/row_major.json @@ -0,0 +1,25 @@ +{ + "order": "row-major", + "A": [ 1.0, 2.0, 3.0, 4.0, 3.0, 4.0, 0.0, 0.0 ], + "M": 4, + "N": 2, + "i": 0, + "j": 2, + "strideA1": 2, + "strideA2": 1, + "offsetA": 0, + "LDA": 2, + "A_mat": [ + [ 1.0, 2.0 ], + [ 3.0, 4.0 ], + [ 3.0, 4.0 ], + [ 0.0, 0.0 ] + ], + "expected": [ 3.0, 4.0, 3.0, 4.0, 1.0, 2.0, 0.0, 0.0 ], + "expected_mat": [ + [ 3.0, 4.0 ], + [ 3.0, 4.0 ], + [ 1.0, 2.0 ], + [ 0.0, 0.0 ] + ] +} diff --git a/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/test/test.js b/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/test/test.js new file mode 100644 index 000000000000..3016f673ecab --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/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 gswapRows = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof gswapRows, '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 gswapRows.ndarray, 'function', 'method is a function' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/test/test.main.js b/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/test/test.main.js new file mode 100644 index 000000000000..009e6c6695a0 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/test/test.main.js @@ -0,0 +1,249 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable max-len */ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var gswapRows = require( './../lib' ); + + +// FIXTURES // + +var ROW_MAJOR_DATA = require( './fixtures/row_major.json' ); +var COLUMN_MAJOR_DATA = require( './fixtures/column_major.json' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof gswapRows, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 7', function test( t ) { + t.strictEqual( gswapRows.length, 7, 'returns expected value' ); + t.end(); +}); + +tape( 'the function throws an error if provided a first argument which is not a valid order', function test( t ) { + var values; + var data; + var i; + + data = ROW_MAJOR_DATA; + + 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() { + gswapRows( value, data.M, data.N, data.i, data.j, data.A, data.LDA ); + }; + } +}); + +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 data; + var i; + + data = ROW_MAJOR_DATA; + + 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() { + gswapRows( data.order, data.M, data.N, data.i, data.j, data.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 data; + var i; + + data = COLUMN_MAJOR_DATA; + + 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() { + gswapRows( data.order, data.M, data.N, data.i, data.j, data.A, value ); + }; + } +}); + +tape( 'the function returns `A` unchanged when M is less than or equal to zero', function test( t ) { + var expected; + var data; + var out; + var A; + + data = ROW_MAJOR_DATA; + A = data.A.slice(); + expected = data.A.slice(); + out = gswapRows( data.order, 0, data.N, data.i, data.j, A, data.LDA ); + + t.strictEqual( out, A, 'returns expected value' ); + t.deepEqual( A, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns `A` unchanged when N is less than or equal to zero', function test( t ) { + var expected; + var data; + var out; + var A; + + data = ROW_MAJOR_DATA; + A = data.A.slice(); + expected = data.A.slice(); + out = gswapRows( data.order, data.M, 0, data.i, data.j, A, data.LDA ); + + t.strictEqual( out, A, 'returns expected value' ); + t.deepEqual( A, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function interchanges two rows of a matrix (row-major)', function test( t ) { + var data; + var out; + var A; + + data = ROW_MAJOR_DATA; + A = data.A.slice(); + out = gswapRows( data.order, data.M, data.N, data.i, data.j, A, data.LDA ); + + t.strictEqual( out, A, 'returns expected value' ); + t.deepEqual( A, data.expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function interchanges two rows of a matrix (column-major)', function test( t ) { + var data; + var out; + var A; + + data = COLUMN_MAJOR_DATA; + A = data.A.slice(); + out = gswapRows( data.order, data.M, data.N, data.i, data.j, A, data.LDA ); + + t.strictEqual( out, A, 'returns expected value' ); + t.deepEqual( A, data.expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports negative row indices (row-major)', function test( t ) { + var data; + var out; + var A; + + data = ROW_MAJOR_DATA; + A = data.A.slice(); + out = gswapRows( data.order, data.M, data.N, data.i - data.M, data.j - data.M, A, data.LDA ); + + t.strictEqual( out, A, 'returns expected value' ); + t.deepEqual( A, data.expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports negative row indices (column-major)', function test( t ) { + var data; + var out; + var A; + + data = COLUMN_MAJOR_DATA; + A = data.A.slice(); + out = gswapRows( data.order, data.M, data.N, data.i - data.M, data.j - data.M, A, data.LDA ); + + t.strictEqual( out, A, 'returns expected value' ); + t.deepEqual( A, data.expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns `A` unchanged when row indices are the same (row-major)', function test( t ) { + var expected; + var data; + var out; + var A; + + data = ROW_MAJOR_DATA; + A = data.A.slice(); + expected = data.A.slice(); + out = gswapRows( data.order, data.M, data.N, 1, 1, A, data.LDA ); + + t.strictEqual( out, A, 'returns expected value' ); + t.deepEqual( A, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns `A` unchanged when row indices are the same (column-major)', function test( t ) { + var expected; + var data; + var out; + var A; + + data = COLUMN_MAJOR_DATA; + A = data.A.slice(); + expected = data.A.slice(); + out = gswapRows( data.order, data.M, data.N, 1, 1, A, data.LDA ); + + t.strictEqual( out, A, 'returns expected value' ); + t.deepEqual( A, expected, 'returns expected value' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/test/test.ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/test/test.ndarray.js new file mode 100644 index 000000000000..a7839cab5ac7 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gswap-rows/test/test.ndarray.js @@ -0,0 +1,343 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable max-len, id-length */ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +var gswapRows = require( './../lib/ndarray.js' ); + + +// FIXTURES // + +var ROW_MAJOR_DATA = require( './fixtures/row_major.json' ); +var COLUMN_MAJOR_DATA = require( './fixtures/column_major.json' ); +var OFFSET_ROW_MAJOR_DATA = require( './fixtures/offsets/row_major.json' ); +var OFFSET_COLUMN_MAJOR_DATA = require( './fixtures/offsets/column_major.json' ); +var NEGATIVE_STRIDES_ROW_MAJOR_DATA = require( './fixtures/negative-strides/row_major.json' ); +var NEGATIVE_STRIDES_COLUMN_MAJOR_DATA = require( './fixtures/negative-strides/column_major.json' ); +var MIXED_STRIDES_ROW_MAJOR_DATA = require( './fixtures/mixed-strides/row_major.json' ); +var MIXED_STRIDES_COLUMN_MAJOR_DATA = require( './fixtures/mixed-strides/column_major.json' ); +var LARGE_STRIDES_ROW_MAJOR_DATA = require( './fixtures/large-strides/row_major.json' ); +var LARGE_STRIDES_COLUMN_MAJOR_DATA = require( './fixtures/large-strides/column_major.json' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof gswapRows, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 8', function test( t ) { + t.strictEqual( gswapRows.length, 8, 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns `A` unchanged when M is less than or equal to zero (row-major)', function test( t ) { + var expected; + var data; + var out; + var A; + + data = ROW_MAJOR_DATA; + A = data.A.slice(); + expected = data.A.slice(); + out = gswapRows( 0, data.N, data.i, data.j, A, data.strideA1, data.strideA2, data.offsetA ); + + t.strictEqual( out, A, 'returns expected value' ); + t.deepEqual( A, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns `A` unchanged when N is less than or equal to zero (row-major)', function test( t ) { + var expected; + var data; + var out; + var A; + + data = ROW_MAJOR_DATA; + A = data.A.slice(); + expected = data.A.slice(); + out = gswapRows( data.M, 0, data.i, data.j, A, data.strideA1, data.strideA2, data.offsetA ); + + t.strictEqual( out, A, 'returns expected value' ); + t.deepEqual( A, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns `A` unchanged when M is less than or equal to zero (column-major)', function test( t ) { + var expected; + var data; + var out; + var A; + + data = COLUMN_MAJOR_DATA; + A = data.A.slice(); + expected = data.A.slice(); + out = gswapRows( 0, data.N, data.i, data.j, A, data.strideA1, data.strideA2, data.offsetA ); + + t.strictEqual( out, A, 'returns expected value' ); + t.deepEqual( A, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns `A` unchanged when N is less than or equal to zero (column-major)', function test( t ) { + var expected; + var data; + var out; + var A; + + data = COLUMN_MAJOR_DATA; + A = data.A.slice(); + expected = data.A.slice(); + out = gswapRows( data.M, 0, data.i, data.j, A, data.strideA1, data.strideA2, data.offsetA ); + + t.strictEqual( out, A, 'returns expected value' ); + t.deepEqual( A, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function interchanges two rows of a matrix (row-major)', function test( t ) { + var data; + var out; + var A; + + data = ROW_MAJOR_DATA; + A = data.A.slice(); + out = gswapRows( data.M, data.N, data.i, data.j, A, data.strideA1, data.strideA2, data.offsetA ); + + t.strictEqual( out, A, 'returns expected value' ); + t.deepEqual( A, data.expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function interchanges two rows of a matrix (column-major)', function test( t ) { + var data; + var out; + var A; + + data = COLUMN_MAJOR_DATA; + A = data.A.slice(); + out = gswapRows( data.M, data.N, data.i, data.j, A, data.strideA1, data.strideA2, data.offsetA ); + + t.strictEqual( out, A, 'returns expected value' ); + t.deepEqual( A, data.expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function interchanges two rows of a matrix (row-major, offsets)', function test( t ) { + var data; + var out; + var A; + + data = OFFSET_ROW_MAJOR_DATA; + A = data.A.slice(); + out = gswapRows( data.M, data.N, data.i, data.j, A, data.strideA1, data.strideA2, data.offsetA ); + + t.strictEqual( out, A, 'returns expected value' ); + t.deepEqual( A, data.expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function interchanges two rows of a matrix (column-major, offsets)', function test( t ) { + var data; + var out; + var A; + + data = OFFSET_COLUMN_MAJOR_DATA; + A = data.A.slice(); + out = gswapRows( data.M, data.N, data.i, data.j, A, data.strideA1, data.strideA2, data.offsetA ); + + t.strictEqual( out, A, 'returns expected value' ); + t.deepEqual( A, data.expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function interchanges two rows of a matrix (row-major, mixed strides)', function test( t ) { + var data; + var out; + var A; + + data = MIXED_STRIDES_ROW_MAJOR_DATA; + A = data.A.slice(); + out = gswapRows( data.M, data.N, data.i, data.j, A, data.strideA1, data.strideA2, data.offsetA ); + + t.strictEqual( out, A, 'returns expected value' ); + t.deepEqual( A, data.expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function interchanges two rows of a matrix (column-major, mixed strides)', function test( t ) { + var data; + var out; + var A; + + data = MIXED_STRIDES_COLUMN_MAJOR_DATA; + A = data.A.slice(); + out = gswapRows( data.M, data.N, data.i, data.j, A, data.strideA1, data.strideA2, data.offsetA ); + + t.strictEqual( out, A, 'returns expected value' ); + t.deepEqual( A, data.expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function interchanges two rows of a matrix (row-major, negative strides)', function test( t ) { + var data; + var out; + var A; + + data = NEGATIVE_STRIDES_ROW_MAJOR_DATA; + A = data.A.slice(); + out = gswapRows( data.M, data.N, data.i, data.j, A, data.strideA1, data.strideA2, data.offsetA ); + + t.strictEqual( out, A, 'returns expected value' ); + t.deepEqual( A, data.expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function interchanges two rows of a matrix (column-major, negative strides)', function test( t ) { + var data; + var out; + var A; + + data = NEGATIVE_STRIDES_COLUMN_MAJOR_DATA; + A = data.A.slice(); + out = gswapRows( data.M, data.N, data.i, data.j, A, data.strideA1, data.strideA2, data.offsetA ); + + t.strictEqual( out, A, 'returns expected value' ); + t.deepEqual( A, data.expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function interchanges two rows of a matrix (row-major, large strides)', function test( t ) { + var data; + var out; + var A; + + data = LARGE_STRIDES_ROW_MAJOR_DATA; + A = data.A.slice(); + out = gswapRows( data.M, data.N, data.i, data.j, A, data.strideA1, data.strideA2, data.offsetA ); + + t.strictEqual( out, A, 'returns expected value' ); + t.deepEqual( A, data.expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function interchanges two rows of a matrix (column-major, large strides)', function test( t ) { + var data; + var out; + var A; + + data = LARGE_STRIDES_COLUMN_MAJOR_DATA; + A = data.A.slice(); + out = gswapRows( data.M, data.N, data.i, data.j, A, data.strideA1, data.strideA2, data.offsetA ); + + t.strictEqual( out, A, 'returns expected value' ); + t.deepEqual( A, data.expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports negative row indices (row-major)', function test( t ) { + var data; + var out; + var A; + + data = ROW_MAJOR_DATA; + A = data.A.slice(); + out = gswapRows( data.M, data.N, data.i - data.M, data.j - data.M, A, data.strideA1, data.strideA2, data.offsetA ); + + t.strictEqual( out, A, 'returns expected value' ); + t.deepEqual( A, data.expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports negative row indices (column-major)', function test( t ) { + var data; + var out; + var A; + + data = COLUMN_MAJOR_DATA; + A = data.A.slice(); + out = gswapRows( data.M, data.N, data.i - data.M, data.j - data.M, A, data.strideA1, data.strideA2, data.offsetA ); + + t.strictEqual( out, A, 'returns expected value' ); + t.deepEqual( A, data.expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns `A` unchanged when row indices are the same (row-major)', function test( t ) { + var expected; + var data; + var out; + var A; + + data = ROW_MAJOR_DATA; + A = data.A.slice(); + expected = data.A.slice(); + out = gswapRows( data.M, data.N, 1, 1, A, data.strideA1, data.strideA2, data.offsetA ); + + t.strictEqual( out, A, 'returns expected value' ); + t.deepEqual( A, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns `A` unchanged when row indices are the same (column-major)', function test( t ) { + var expected; + var data; + var out; + var A; + + data = COLUMN_MAJOR_DATA; + A = data.A.slice(); + expected = data.A.slice(); + out = gswapRows( data.M, data.N, 1, 1, A, data.strideA1, data.strideA2, data.offsetA ); + + t.strictEqual( out, A, 'returns expected value' ); + t.deepEqual( A, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function interchanges two rows of a matrix (row-major, accessors)', function test( t ) { + var data; + var A; + + data = ROW_MAJOR_DATA; + A = data.A.slice(); + gswapRows( data.M, data.N, data.i, data.j, toAccessorArray( A ), data.strideA1, data.strideA2, data.offsetA ); + + t.deepEqual( A, data.expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function interchanges two rows of a matrix (column-major, accessors)', function test( t ) { + var data; + var A; + + data = COLUMN_MAJOR_DATA; + A = data.A.slice(); + gswapRows( data.M, data.N, data.i, data.j, toAccessorArray( A ), data.strideA1, data.strideA2, data.offsetA ); + + t.deepEqual( A, data.expected, 'returns expected value' ); + t.end(); +});