diff --git a/CHANGELOG.md b/CHANGELOG.md index d11953f34..5ca706bd8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -608,6 +608,50 @@ +
+ +#### [@stdlib/blas/tools](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/tools) + +
+ +
+ +##### Features + +- [`339ba1b`](https://github.com/stdlib-js/stdlib/commit/339ba1bf405e085779be64f98aa197115483b857) - add `blas/tools` namespace + +
+ + + +
+ +
+ + + +
+ +#### [@stdlib/blas/tools/swap-factory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/tools/swap-factory) + +
+ +
+ +##### Features + +- [`c070a88`](https://github.com/stdlib-js/stdlib/commit/c070a889f13926400f2422a6334d5252df1bacaf) - add `blas/tools/swap-factory` + +
+ + + +
+ +
+ + + @@ -647,6 +691,10 @@ A total of 6 people contributed to this release. Thank you to the following cont
+- [`339ba1b`](https://github.com/stdlib-js/stdlib/commit/339ba1bf405e085779be64f98aa197115483b857) - **feat:** add `blas/tools` namespace _(by Athan Reines)_ +- [`dc22190`](https://github.com/stdlib-js/stdlib/commit/dc22190e473acb1a413af6d4482305c65f2420fd) - **refactor:** use `blas/tools/swap-factory` _(by Athan Reines)_ +- [`2e2f268`](https://github.com/stdlib-js/stdlib/commit/2e2f2681aa86405fd2a5d7cad8c48fe32a2b8cb5) - **refactor:** use `blas/tools/swap-factory` _(by Athan Reines)_ +- [`c070a88`](https://github.com/stdlib-js/stdlib/commit/c070a889f13926400f2422a6334d5252df1bacaf) - **feat:** add `blas/tools/swap-factory` _(by Athan Reines)_ - [`e454c91`](https://github.com/stdlib-js/stdlib/commit/e454c91ae2af928b61effcddadb31548758f8675) - **chore:** improve code style and conditionals _(by Philipp Burckhardt)_ - [`898b50d`](https://github.com/stdlib-js/stdlib/commit/898b50d8d705bdf6a55db8cf1858ea1e1d257c35) - **fix:** fix includes and types _(by Philipp Burckhardt)_ - [`7e366ae`](https://github.com/stdlib-js/stdlib/commit/7e366ae8bd41439be0e99e958d1c3fbb1b7dd0c2) - **chore:** update package.json descriptions _(by Philipp Burckhardt)_ diff --git a/dswap/lib/main.js b/dswap/lib/main.js index b75dde7dd..b689e122f 100644 --- a/dswap/lib/main.js +++ b/dswap/lib/main.js @@ -20,18 +20,8 @@ // MODULES // -var isFloat64ndarrayLike = require( '@stdlib/assert/is-float64ndarray-like' ); -var isNegativeInteger = require( '@stdlib/assert/is-negative-integer' ).isPrimitive; -var isReadOnly = require( '@stdlib/ndarray/base/assert/is-read-only' ); -var hasEqualValues = require( '@stdlib/array/base/assert/has-equal-values-indexed' ); -var min = require( '@stdlib/math/base/special/fast/min' ); -var without = require( '@stdlib/array/base/without' ); -var ndarraylike2ndarray = require( '@stdlib/ndarray/base/ndarraylike2ndarray' ); -var normalizeIndex = require( '@stdlib/ndarray/base/normalize-index' ); -var nditerStacks = require( '@stdlib/ndarray/iter/stacks' ); -var numel = require( '@stdlib/ndarray/base/numel' ); var base = require( './../../base/dswap' ).ndarray; -var format = require( '@stdlib/string/format' ); +var factory = require( './../../tools/swap-factory' ); // MAIN // @@ -39,6 +29,8 @@ var format = require( '@stdlib/string/format' ); /** * Interchanges two double-precision floating-point vectors. * +* @name dswap +* @type {Function} * @param {ndarrayLike} x - first input array * @param {ndarrayLike} y - second input array * @param {NegativeInteger} [dim] - dimension along which to interchange elements @@ -66,86 +58,7 @@ var format = require( '@stdlib/string/format' ); * var ybuf = y.data; * // returns [ 4.0, 2.0, -3.0, 5.0, -1.0 ] */ -function dswap( x, y ) { - var dim; - var xsh; - var ysh; - var xit; - var yit; - var xc; - var yc; - var vx; - var vy; - var dm; - var S; - var N; - var i; - if ( !isFloat64ndarrayLike( x ) ) { - throw new TypeError( format( 'invalid argument. First argument must be an ndarray containing double-precision floating-point numbers. Value: `%s`.', x ) ); - } - if ( !isFloat64ndarrayLike( y ) ) { - throw new TypeError( format( 'invalid argument. Second argument must be an ndarray containing double-precision floating-point numbers. Value: `%s`.', y ) ); - } - if ( isReadOnly( x ) || isReadOnly( y ) ) { - throw new Error( 'invalid argument. Cannot write to read-only array.' ); - } - // Convert the input arrays to "base" ndarrays: - xc = ndarraylike2ndarray( x ); - yc = ndarraylike2ndarray( y ); - - // Resolve the input array shapes: - xsh = xc.shape; - ysh = yc.shape; - - // Validate that we've been provided non-zero-dimensional arrays... - if ( xsh.length < 1 ) { - throw new TypeError( format( 'invalid argument. First argument must have at least one dimension.' ) ); - } - if ( ysh.length < 1 ) { - throw new TypeError( format( 'invalid argument. Second argument must have at least one dimension.' ) ); - } - // Validate that the arrays have the same shape... - if ( !hasEqualValues( xsh, ysh ) ) { - throw new Error( 'invalid arguments. The first and second arguments must have the same shape.' ); - } - // Validate that the dimension argument is a negative integer... - if ( arguments.length > 2 ) { - dim = arguments[ 2 ]; - if ( !isNegativeInteger( dim ) ) { - throw new TypeError( format( 'invalid argument. Third argument must be a negative integer. Value: `%s`.', dim ) ); - } - } else { - dim = -1; - } - // Validate that a provided dimension index is within bounds... - dm = min( xsh.length, ysh.length ) - 1; - dim = normalizeIndex( dim, dm ); - if ( dim === -1 ) { - throw new RangeError( format( 'invalid argument. Third argument must be a value on the interval: [%d,%d]. Value: `%d`.', -dm, -1, arguments[ 2 ] ) ); - } - // Resolve the size of the interchange dimension: - S = xsh[ dim ]; - - // If we are only provided one-dimensional input arrays, we can skip iterating over stacks... - if ( xsh.length === 1 ) { - base( S, xc.data, xc.strides[0], xc.offset, yc.data, yc.strides[0], yc.offset ); // eslint-disable-line max-len - return y; - } - // Resolve the number of stacks: - N = numel( without( xsh, dim ) ); - - // Create iterators for iterating over stacks of vectors: - xit = nditerStacks( xc, [ dim ] ); - yit = nditerStacks( yc, [ dim ] ); - - // Interchange each pair of vectors... - for ( i = 0; i < N; i++ ) { - vx = xit.next().value; - vy = yit.next().value; - base( S, vx.data, vx.strides[0], vx.offset, vy.data, vy.strides[0], vy.offset ); // eslint-disable-line max-len - } - return y; -} +var dswap = factory( base, 'float64' ); // EXPORTS // diff --git a/sswap/lib/main.js b/sswap/lib/main.js index 540a35bfa..bd33770fb 100644 --- a/sswap/lib/main.js +++ b/sswap/lib/main.js @@ -20,18 +20,8 @@ // MODULES // -var isFloat32ndarrayLike = require( '@stdlib/assert/is-float32ndarray-like' ); -var isNegativeInteger = require( '@stdlib/assert/is-negative-integer' ).isPrimitive; -var isReadOnly = require( '@stdlib/ndarray/base/assert/is-read-only' ); -var hasEqualValues = require( '@stdlib/array/base/assert/has-equal-values-indexed' ); -var min = require( '@stdlib/math/base/special/fast/min' ); -var without = require( '@stdlib/array/base/without' ); -var ndarraylike2ndarray = require( '@stdlib/ndarray/base/ndarraylike2ndarray' ); -var normalizeIndex = require( '@stdlib/ndarray/base/normalize-index' ); -var nditerStacks = require( '@stdlib/ndarray/iter/stacks' ); -var numel = require( '@stdlib/ndarray/base/numel' ); var base = require( './../../base/sswap' ).ndarray; -var format = require( '@stdlib/string/format' ); +var factory = require( './../../tools/swap-factory' ); // MAIN // @@ -39,6 +29,8 @@ var format = require( '@stdlib/string/format' ); /** * Interchanges two single-precision floating-point vectors. * +* @name sswap +* @type {Function} * @param {ndarrayLike} x - first input array * @param {ndarrayLike} y - second input array * @param {NegativeInteger} [dim=-1] - dimension along which to interchange elements @@ -66,86 +58,7 @@ var format = require( '@stdlib/string/format' ); * var ybuf = y.data; * // returns [ 4.0, 2.0, -3.0, 5.0, -1.0 ] */ -function sswap( x, y ) { - var dim; - var xsh; - var ysh; - var xit; - var yit; - var xc; - var yc; - var vx; - var vy; - var dm; - var S; - var N; - var i; - if ( !isFloat32ndarrayLike( x ) ) { - throw new TypeError( format( 'invalid argument. First argument must be an ndarray containing single-precision floating-point numbers. Value: `%s`.', x ) ); - } - if ( !isFloat32ndarrayLike( y ) ) { - throw new TypeError( format( 'invalid argument. Second argument must be an ndarray containing single-precision floating-point numbers. Value: `%s`.', y ) ); - } - if ( isReadOnly( x ) || isReadOnly( y ) ) { - throw new Error( 'invalid argument. Cannot write to read-only array.' ); - } - // Convert the input arrays to "base" ndarrays: - xc = ndarraylike2ndarray( x ); - yc = ndarraylike2ndarray( y ); - - // Resolve the input array shapes: - xsh = xc.shape; - ysh = yc.shape; - - // Validate that we've been provided non-zero-dimensional arrays... - if ( xsh.length < 1 ) { - throw new TypeError( format( 'invalid argument. First argument must have at least one dimension.' ) ); - } - if ( ysh.length < 1 ) { - throw new TypeError( format( 'invalid argument. Second argument must have at least one dimension.' ) ); - } - // Validate that the arrays have the same shape... - if ( !hasEqualValues( xsh, ysh ) ) { - throw new Error( 'invalid arguments. The first and second arguments must have the same shape.' ); - } - // Validate that the dimension argument is a negative integer... - if ( arguments.length > 2 ) { - dim = arguments[ 2 ]; - if ( !isNegativeInteger( dim ) ) { - throw new TypeError( format( 'invalid argument. Third argument must be a negative integer. Value: `%s`.', dim ) ); - } - } else { - dim = -1; - } - // Validate that a provided dimension index is within bounds... - dm = min( xsh.length, ysh.length ) - 1; - dim = normalizeIndex( dim, dm ); - if ( dim === -1 ) { - throw new RangeError( format( 'invalid argument. Third argument must be a value on the interval: [%d,%d]. Value: `%d`.', -dm, -1, arguments[ 2 ] ) ); - } - // Resolve the size of the interchange dimension: - S = xsh[ dim ]; - - // If we are only provided one-dimensional input arrays, we can skip iterating over stacks... - if ( xsh.length === 1 ) { - base( S, xc.data, xc.strides[0], xc.offset, yc.data, yc.strides[0], yc.offset ); // eslint-disable-line max-len - return y; - } - // Resolve the number of stacks: - N = numel( without( xsh, dim ) ); - - // Create iterators for iterating over stacks of vectors: - xit = nditerStacks( xc, [ dim ] ); - yit = nditerStacks( yc, [ dim ] ); - - // Interchange each pair of vectors... - for ( i = 0; i < N; i++ ) { - vx = xit.next().value; - vy = yit.next().value; - base( S, vx.data, vx.strides[0], vx.offset, vy.data, vy.strides[0], vy.offset ); // eslint-disable-line max-len - } - return y; -} +var sswap = factory( base, 'float32' ); // EXPORTS // diff --git a/tools/README.md b/tools/README.md new file mode 100644 index 000000000..211c7552b --- /dev/null +++ b/tools/README.md @@ -0,0 +1,85 @@ + + +# Tools + +> Tools for basic linear algebra subprograms (BLAS). + +
+ +## Usage + +```javascript +var ns = require( '@stdlib/blas/tools' ); +``` + +#### ns + +Namespace for basic linear algebra subprograms (BLAS) tools. + +```javascript +var o = ns; +// returns {...} +``` + +The namespace contains the following: + + + + + +
+ + + +
+ +## Examples + + + + + +```javascript +var objectKeys = require( '@stdlib/utils/keys' ); +var ns = require( '@stdlib/blas/tools' ); + +console.log( objectKeys( ns ) ); +``` + +
+ + + + + + + + + + + + + + diff --git a/tools/docs/types/index.d.ts b/tools/docs/types/index.d.ts new file mode 100644 index 000000000..5256191d0 --- /dev/null +++ b/tools/docs/types/index.d.ts @@ -0,0 +1,43 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2024 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 + +/* eslint-disable max-lines */ + +import swapFactory = require( './../../../tools/swap-factory' ); + +/** +* Interface describing the namespace. +*/ +interface Namespace { + /** + * TODO + */ + swapFactory: typeof swapFactory; +} + +/** +* BLAS tools. +*/ +declare var ns: Namespace; + + +// EXPORTS // + +export = ns; diff --git a/tools/docs/types/test.ts b/tools/docs/types/test.ts new file mode 100644 index 000000000..2f81653a2 --- /dev/null +++ b/tools/docs/types/test.ts @@ -0,0 +1,29 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2024 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 @typescript-eslint/no-unused-expressions */ + +import ns = require( './index' ); + + +// TESTS // + +// The exported value is the expected interface... +{ + ns; // $ExpectType Namespace +} diff --git a/tools/examples/index.js b/tools/examples/index.js new file mode 100644 index 000000000..6115ca083 --- /dev/null +++ b/tools/examples/index.js @@ -0,0 +1,24 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 objectKeys = require( '@stdlib/utils/keys' ); +var ns = require( './../lib' ); + +console.log( objectKeys( ns ) ); diff --git a/tools/lib/index.js b/tools/lib/index.js new file mode 100644 index 000000000..943f1e8e9 --- /dev/null +++ b/tools/lib/index.js @@ -0,0 +1,51 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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'; + +/* +* When adding modules to the namespace, ensure that they are added in alphabetical order according to module name. +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-read-only-property' ); + + +// MAIN // + +/** +* Top-level namespace. +* +* @namespace ns +*/ +var ns = {}; + +/** +* @name swapFactory +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/blas/tools/swap-factory} +*/ +setReadOnly( ns, 'swapFactory', require( './../../tools/swap-factory' ) ); + + +// EXPORTS // + +module.exports = ns; diff --git a/tools/package.json b/tools/package.json new file mode 100644 index 000000000..b9efe54e4 --- /dev/null +++ b/tools/package.json @@ -0,0 +1,68 @@ +{ + "name": "@stdlib/blas/tools", + "version": "0.0.0", + "description": "BLAS tools.", + "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": { + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdmath", + "stdblas", + "standard", + "library", + "std", + "lib", + "mathematics", + "math", + "blas", + "linear", + "algebra", + "linalg", + "tools", + "utils", + "utilities" + ] +} diff --git a/tools/swap-factory/README.md b/tools/swap-factory/README.md new file mode 100644 index 000000000..1f4573da9 --- /dev/null +++ b/tools/swap-factory/README.md @@ -0,0 +1,187 @@ + + +# factory + +> Return a function which interchange two vectors. + +
+ +
+ + + +
+ +## Usage + +```javascript +var factory = require( '@stdlib/blas/tools/swap-factory' ); +``` + +#### factory( base, dtype ) + +Returns a function which interchanges two vectors. + +```javascript +var dswap = require( '@stdlib/blas/base/dswap' ).ndarray; + +var swap = factory( dswap, 'float64' ); +``` + +The function has the following parameters: + +- **base**: "base" function which interchanges two vectors. Must have an `ndarray` function signature (i.e., must support index offsets). +- **dtype**: array data type. The function assumes that the data type of all provided arrays is the same. + +#### swap( x, y\[, dim] ) + +Interchanges two vectors. + +```javascript +var Float64Array = require( '@stdlib/array/float64' ); +var array = require( '@stdlib/ndarray/array' ); +var dswap = require( '@stdlib/blas/base/dswap' ).ndarray; + +var swap = factory( dswap, 'float64' ); + +var x = array( new Float64Array( [ 4.0, 2.0, -3.0, 5.0, -1.0 ] ) ); +var y = array( new Float64Array( [ 2.0, 6.0, -1.0, -4.0, 8.0 ] ) ); + +swap( x, y ); + +var xbuf = x.data; +// returns [ 2.0, 6.0, -1.0, -4.0, 8.0 ] + +var ybuf = y.data; +// returns [ 4.0, 2.0, -3.0, 5.0, -1.0 ] +``` + +The returned function has the following parameters: + +- **x**: a non-zero-dimensional [`ndarray`][@stdlib/ndarray/ctor]. Must have the same shape as `y`. +- **y**: a non-zero-dimensional [`ndarray`][@stdlib/ndarray/ctor]. Must have the same shape as `x`. +- **dim**: dimension along which to interchange vectors. Must be a negative integer. Negative indices are resolved relative to the last array dimension, with the last dimension corresponding to `-1`. Default: `-1`. + +For multi-dimensional input [`ndarrays`][@stdlib/ndarray/ctor], the function performs batched computation, such that the function interchanges each pair of vectors in `x` and `y` according to the specified dimension index. + +```javascript +var Float64Array = require( '@stdlib/array/float64' ); +var array = require( '@stdlib/ndarray/array' ); +var dswap = require( '@stdlib/blas/base/dswap' ).ndarray; + +var swap = factory( dswap, 'float64' ); + +var opts = { + 'shape': [ 2, 3 ] +}; +var x = array( new Float64Array( [ 4.0, 2.0, -3.0, 5.0, -1.0, 3.0 ] ), opts ); +var y = array( new Float64Array( [ 2.0, 6.0, -1.0, -4.0, 8.0, 2.0 ] ), opts ); + +var v1 = x.get( 0, 0 ); +// returns 4.0 + +var v2 = y.get( 0, 0 ); +// returns 2.0 + +swap( x, y ); + +v1 = x.get( 0, 0 ); +// returns 2.0 + +v2 = y.get( 0, 0 ); +// returns 4.0 +``` + +
+ + + +
+ +## Notes + +For the returned function, + +- Both input [`ndarrays`][@stdlib/ndarray/ctor] must have the same shape. +- Negative indices are resolved relative to the last [`ndarray`][@stdlib/ndarray/ctor] dimension, with the last dimension corresponding to `-1`. +- For multi-dimensional [`ndarrays`][@stdlib/ndarray/ctor], batched computation effectively means swapping all of `x` with all of `y`; however, the choice of `dim` will significantly affect performance. For best performance, specify a `dim` which best aligns with the [memory layout][@stdlib/ndarray/orders] of provided [`ndarrays`][@stdlib/ndarray/ctor]. + +
+ + + +
+ +## Examples + + + +```javascript +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var array = require( '@stdlib/ndarray/array' ); +var dswap = require( '@stdlib/blas/base/dswap' ).ndarray; +var factory = require( '@stdlib/blas/tools/swap-factory' ); + +var swap = factory( dswap, 'float64' ); + +var opts = { + 'dtype': 'float64' +}; + +var x = array( discreteUniform( 10, 0, 100, opts ), { + 'shape': [ 5, 2 ] +}); +console.log( ndarray2array( x ) ); + +var y = array( discreteUniform( 10, 0, 10, opts ), { + 'shape': x.shape +}); +console.log( ndarray2array( y ) ); + +swap( x, y ); +console.log( ndarray2array( x ) ); +console.log( ndarray2array( y ) ); +``` + +
+ + + + + + + + + + + + + + diff --git a/tools/swap-factory/benchmark/benchmark.js b/tools/swap-factory/benchmark/benchmark.js new file mode 100644 index 000000000..88c15cda0 --- /dev/null +++ b/tools/swap-factory/benchmark/benchmark.js @@ -0,0 +1,100 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 uniform = require( '@stdlib/random/array/uniform' ); +var array = require( '@stdlib/ndarray/array' ); +var dswap = require( './../../../base/dswap' ).ndarray; +var pkg = require( './../package.json' ).name; +var factory = require( './../lib' ); + + +// VARIABLES // + +var opts = { + 'dtype': 'float64' +}; +var swap = factory( dswap, opts.dtype ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var x = array( uniform( len, -100.0, 100.0, opts ) ); + var y = array( uniform( len, -100.0, 100.0, opts ) ); + return benchmark; + + function benchmark( b ) { + var d; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + d = swap( x, y ); + if ( isnan( d.data[ i%len ] ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( d.data[ i%len ] ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( pkg+':len='+len, f ); + } +} + +main(); diff --git a/tools/swap-factory/benchmark/benchmark.stacks.js b/tools/swap-factory/benchmark/benchmark.stacks.js new file mode 100644 index 000000000..e978e6a51 --- /dev/null +++ b/tools/swap-factory/benchmark/benchmark.stacks.js @@ -0,0 +1,124 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 uniform = require( '@stdlib/random/array/uniform' ); +var numel = require( '@stdlib/ndarray/base/numel' ); +var array = require( '@stdlib/ndarray/array' ); +var dswap = require( './../../../base/dswap' ).ndarray; +var pkg = require( './../package.json' ).name; +var factory = require( './../lib' ); + + +// VARIABLES // + +var opts = { + 'dtype': 'float64' +}; +var swap = factory( dswap, opts.dtype ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveIntegerArray} shape - array shape +* @returns {Function} benchmark function +*/ +function createBenchmark( shape ) { + var x; + var y; + var N; + var o; + + N = numel( shape ); + o = { + 'shape': shape + }; + x = array( uniform( N, -100.0, 100.0, opts ), o ); + y = array( uniform( N, -100.0, 100.0, opts ), o ); + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var d; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + d = swap( x, y ); + if ( isnan( d.data[ i%N ] ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( d.data[ i%N ] ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var shape; + var min; + var max; + var N; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + N = pow( 10, i ); + + shape = [ 2, N/2 ]; + f = createBenchmark( shape ); + bench( pkg+'::stacks:size='+N+',ndims='+shape.length+',shape=('+shape.join( ',' )+')', f ); + + shape = [ N/2, 2 ]; + f = createBenchmark( shape ); + bench( pkg+'::stacks:size='+N+',ndims='+shape.length+',shape=('+shape.join( ',' )+')', f ); + } +} + +main(); diff --git a/tools/swap-factory/docs/repl.txt b/tools/swap-factory/docs/repl.txt new file mode 100644 index 000000000..e42fa11bd --- /dev/null +++ b/tools/swap-factory/docs/repl.txt @@ -0,0 +1,68 @@ + +{{alias}}( base, dtype ) + Returns a function which interchanges two vectors. + + Parameters + ---------- + base: Function + Base function which interchanges two vectors. Must have an ndarray + signature (i.e., must support index offsets). + + dtype: any + Array data type. The function assumes that the data type of all provided + arrays is the same. + + Returns + ------- + fcn: Function + Function which interchanges two vectors. + + Examples + -------- + > var fcn = {{alias}}( {{alias:@stdlib/blas/base/dswap}}, 'float64' ) + + + +fcn( x, y[, dim] ) + Interchanges two vectors. + + For multi-dimensional input arrays, the function performs batched + computation, such that the function interchanges each pair of vectors in `x` + and `y` according to the specified dimension index. + + Both input arrays must have the same shape. + + Parameters + ---------- + x: ndarray + First input array. Must have at least one dimension and must have the + same shape as the second input array. + + y: ndarray + Second input array. Must have at least one dimension and must have the + same shape as the first input array. + + dim: integer (optional) + Dimension index along which to interchange vectors. Must be a negative + integer. Negative indices are resolved relative to the last array + dimension, with the last dimension corresponding to `-1`. Default: -1. + + Returns + ------- + y: ndarray + The second input array `y`. + + Examples + -------- + > var fcn = {{alias}}( {{alias:@stdlib/blas/base/dswap}}.ndarray, 'float64' ); + > var x = {{alias:@stdlib/ndarray/array}}( new {{alias:@stdlib/array/float64}}( [ 4.0, 2.0, -3.0, 5.0, -1.0 ] ) ); + > var y = {{alias:@stdlib/ndarray/array}}( new {{alias:@stdlib/array/float64}}( [ 2.0, 6.0, -1.0, -4.0, 8.0 ] ) ); + > fcn( x, y ); + > x.data + [ 2.0, 6.0, -1.0, -4.0, 8.0 ] + > y.data + [ 4.0, 2.0, -3.0, 5.0, -1.0 ] + + See Also + -------- + diff --git a/tools/swap-factory/docs/types/index.d.ts b/tools/swap-factory/docs/types/index.d.ts new file mode 100644 index 000000000..f044afc79 --- /dev/null +++ b/tools/swap-factory/docs/types/index.d.ts @@ -0,0 +1,88 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2024 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 { ndarray } from '@stdlib/types/ndarray'; +import { Collection, AccessorArrayLike } from '@stdlib/types/array'; + +/** +* "Base" function which interchanges two vectors. +* +* @param N - number of indexed elements +* @param x - first input array +* @param strideX - `x` stride length +* @param offsetX - starting index for `x` +* @param y - second input array +* @param strideY - `y` stride length +* @param offsetY - starting index for `y` +* @returns `y` +*/ +type BaseFunction = ( N: number, x: T, strideX: number, offsetX: number, y: T, strideY: number, offsetY: number ) => T; + +/** +* Interchanges two vectors. +* +* ## Notes +* +* - For multi-dimensional input arrays, the function performs batched computation, such that the function interchanges each pair of vectors in `x` and `y` according to the specified dimension index. +* - Both input arrays must have the same shape. +* - Negative indices are resolved relative to the last array dimension, with the last dimension corresponding to `-1`. +* +* @param x - first input array +* @param y - second input array +* @param dim - dimension for which to compute the dot product (default: -1) +* @throws first argument must be a non-zero-dimensional ndarray +* @throws second argument must be a non-zero-dimensional ndarray +* @throws input arrays must have the same shape +* @returns `y` +*/ +type SwapFunction = ( x: ndarray, y: ndarray, dim?: number ) => ndarray; + +/** +* Returns a function which interchanges two vectors. +* +* @param base - "base" function which interchanges two vectors +* @param dtype - array data type +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var array = require( '@stdlib/ndarray/array' ); +* var dswap = require( '@stdlib/blas/base/dswap' ).ndarray; +* +* var swap = factory( dswap, 'float64' ); +* +* var x = array( new Float64Array( [ 4.0, 2.0, -3.0, 5.0, -1.0 ] ) ); +* var y = array( new Float64Array( [ 2.0, 6.0, -1.0, -4.0, 8.0 ] ) ); +* +* swap( x, y ); +* +* var xbuf = x.data; +* // returns [ 2.0, 6.0, -1.0, -4.0, 8.0 ] +* +* var ybuf = y.data; +* // returns [ 4.0, 2.0, -3.0, 5.0, -1.0 ] +*/ +declare function factory>( base: BaseFunction, dtype: any ): SwapFunction; + + +// EXPORTS // + +export = factory; diff --git a/tools/swap-factory/docs/types/test.ts b/tools/swap-factory/docs/types/test.ts new file mode 100644 index 000000000..00e467a90 --- /dev/null +++ b/tools/swap-factory/docs/types/test.ts @@ -0,0 +1,133 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2024 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 @typescript-eslint/unbound-method */ + +import dswap = require( './../../../../base/dswap' ); +import zeros = require( '@stdlib/ndarray/zeros' ); +import factory = require( './index' ); + + +// TESTS // + +// The function returns a function... +{ + factory( dswap.ndarray, 'float64' ); // $ExpectType SwapFunction +} + +// The compiler throws an error if the function is provided a first argument which is not a valid function... +{ + factory( 10, 'float64' ); // $ExpectError + factory( '10', 'float64' ); // $ExpectError + factory( true, 'float64' ); // $ExpectError + factory( false, 'float64' ); // $ExpectError + factory( null, 'float64' ); // $ExpectError + factory( undefined, 'float64' ); // $ExpectError + factory( {}, 'float64' ); // $ExpectError + factory( [], 'float64' ); // $ExpectError + factory( ( x: number ): number => x, 'float64' ); // $ExpectError +} + +// The function returns a function which returns an ndarray... +{ + const swap = factory( dswap.ndarray, 'float64' ); + + swap( zeros( [ 10 ], { 'dtype': 'float64' } ), zeros( [ 10 ], { 'dtype': 'float64' } ) ); // $ExpectType ndarray +} + +// The compiler throws an error if the returned function is provided a first argument which is not an ndarray... +{ + const swap = factory( dswap.ndarray, 'float64' ); + + const y = zeros( [ 10 ], { 'dtype': 'float64' } ); + + swap( 10, y ); // $ExpectError + swap( '10', y ); // $ExpectError + swap( true, y ); // $ExpectError + swap( false, y ); // $ExpectError + swap( null, y ); // $ExpectError + swap( undefined, y ); // $ExpectError + swap( {}, y ); // $ExpectError + swap( [], y ); // $ExpectError + swap( ( x: number ): number => x, y ); // $ExpectError + + swap( 10, y, -1 ); // $ExpectError + swap( '10', y, -1 ); // $ExpectError + swap( true, y, -1 ); // $ExpectError + swap( false, y, -1 ); // $ExpectError + swap( null, y, -1 ); // $ExpectError + swap( undefined, y, -1 ); // $ExpectError + swap( {}, y, -1 ); // $ExpectError + swap( [], y, -1 ); // $ExpectError + swap( ( x: number ): number => x, y, -1 ); // $ExpectError +} + +// The compiler throws an error if the returned function is provided a second argument which is not an ndarray... +{ + const swap = factory( dswap.ndarray, 'float64' ); + + const x = zeros( [ 10 ], { 'dtype': 'float64' } ); + + swap( x, 10 ); // $ExpectError + swap( x, '10' ); // $ExpectError + swap( x, true ); // $ExpectError + swap( x, false ); // $ExpectError + swap( x, null ); // $ExpectError + swap( x, undefined ); // $ExpectError + swap( x, {} ); // $ExpectError + swap( x, [] ); // $ExpectError + swap( x, ( x: number ): number => x ); // $ExpectError + + swap( x, 10, -1 ); // $ExpectError + swap( x, '10', -1 ); // $ExpectError + swap( x, true, -1 ); // $ExpectError + swap( x, false, -1 ); // $ExpectError + swap( x, null, -1 ); // $ExpectError + swap( x, undefined, -1 ); // $ExpectError + swap( x, {}, -1 ); // $ExpectError + swap( x, [], -1 ); // $ExpectError + swap( x, ( x: number ): number => x, -1 ); // $ExpectError +} + +// The compiler throws an error if the returned function is provided a third argument which is not a number... +{ + const swap = factory( dswap.ndarray, 'float64' ); + + const x = zeros( [ 10 ], { 'dtype': 'float64' } ); + const y = zeros( [ 10 ], { 'dtype': 'float64' } ); + + swap( x, y, '10' ); // $ExpectError + swap( x, y, true ); // $ExpectError + swap( x, y, false ); // $ExpectError + swap( x, y, null ); // $ExpectError + swap( x, y, {} ); // $ExpectError + swap( x, y, [] ); // $ExpectError + swap( x, y, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the returned function is provided an unsupported number of arguments... +{ + const swap = factory( dswap.ndarray, 'float64' ); + + const x = zeros( [ 10 ], { 'dtype': 'float64' } ); + const y = zeros( [ 10 ], { 'dtype': 'float64' } ); + + swap(); // $ExpectError + swap( x ); // $ExpectError + swap( x, y, -1, {} ); // $ExpectError +} diff --git a/tools/swap-factory/examples/index.js b/tools/swap-factory/examples/index.js new file mode 100644 index 000000000..a895e0bdc --- /dev/null +++ b/tools/swap-factory/examples/index.js @@ -0,0 +1,45 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var array = require( '@stdlib/ndarray/array' ); +var dswap = require( './../../../base/dswap' ).ndarray; +var factory = require( './../lib' ); + +var swap = factory( dswap, 'float64' ); + +var opts = { + 'dtype': 'float64' +}; + +var x = array( discreteUniform( 10, 0, 100, opts ), { + 'shape': [ 5, 2 ] +}); +console.log( ndarray2array( x ) ); + +var y = array( discreteUniform( 10, 0, 10, opts ), { + 'shape': x.shape +}); +console.log( ndarray2array( y ) ); + +swap( x, y ); +console.log( ndarray2array( x ) ); +console.log( ndarray2array( y ) ); diff --git a/tools/swap-factory/lib/index.js b/tools/swap-factory/lib/index.js new file mode 100644 index 000000000..e25903c50 --- /dev/null +++ b/tools/swap-factory/lib/index.js @@ -0,0 +1,53 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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'; + +/** +* Return a function which interchanges vectors. +* +* @module @stdlib/blas/tools/swap-factory +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var array = require( '@stdlib/ndarray/array' ); +* var dswap = require( '@stdlib/blas/base/dswap' ).ndarray; +* var factory = require( '@stdlib/blas/tools/swap-factory' ); +* +* var swap = factory( dswap, 'float64' ); +* +* var x = array( new Float64Array( [ 4.0, 2.0, -3.0, 5.0, -1.0 ] ) ); +* var y = array( new Float64Array( [ 2.0, 6.0, -1.0, -4.0, 8.0 ] ) ); +* +* swap( x, y ); +* +* var xbuf = x.data; +* // returns [ 2.0, 6.0, -1.0, -4.0, 8.0 ] +* +* var ybuf = y.data; +* // returns [ 4.0, 2.0, -3.0, 5.0, -1.0 ] +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/tools/swap-factory/lib/main.js b/tools/swap-factory/lib/main.js new file mode 100644 index 000000000..587ccfc2f --- /dev/null +++ b/tools/swap-factory/lib/main.js @@ -0,0 +1,191 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 isFunction = require( '@stdlib/assert/is-function' ); +var isndarrayLikeWithDataType = require( '@stdlib/assert/is-ndarray-like-with-data-type' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var isNegativeInteger = require( '@stdlib/assert/is-negative-integer' ).isPrimitive; +var isDataType = require( '@stdlib/ndarray/base/assert/is-data-type' ); +var isReadOnly = require( '@stdlib/ndarray/base/assert/is-read-only' ); +var hasEqualValues = require( '@stdlib/array/base/assert/has-equal-values-indexed' ); +var min = require( '@stdlib/math/base/special/fast/min' ); +var without = require( '@stdlib/array/base/without' ); +var ndarraylike2ndarray = require( '@stdlib/ndarray/base/ndarraylike2ndarray' ); +var normalizeIndex = require( '@stdlib/ndarray/base/normalize-index' ); +var nditerStacks = require( '@stdlib/ndarray/iter/stacks' ); +var numel = require( '@stdlib/ndarray/base/numel' ); +var format = require( '@stdlib/string/format' ); + + +// MAIN // + +/** +* Returns a function which interchanges two vectors. +* +* @param {Function} base - "base" function which interchanges two vectors +* @param {(String|null)} dtype - array data type +* @throws {TypeError} first argument must be a function +* @throws {TypeError} second argument must be a data type +* @returns {Function} function wrapper +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var array = require( '@stdlib/ndarray/array' ); +* var dswap = require( '@stdlib/blas/base/dswap' ).ndarray; +* +* var swap = factory( dswap, 'float64' ); +* +* var x = array( new Float64Array( [ 4.0, 2.0, -3.0, 5.0, -1.0 ] ) ); +* var y = array( new Float64Array( [ 2.0, 6.0, -1.0, -4.0, 8.0 ] ) ); +* +* swap( x, y ); +* +* var xbuf = x.data; +* // returns [ 2.0, 6.0, -1.0, -4.0, 8.0 ] +* +* var ybuf = y.data; +* // returns [ 4.0, 2.0, -3.0, 5.0, -1.0 ] +*/ +function factory( base, dtype ) { + var isValid; + if ( !isFunction( base ) ) { + throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', base ) ); + } + if ( !isDataType( dtype ) && dtype !== null ) { + throw new TypeError( format( 'invalid argument. Second argument must a data type. Value: `%s`.', dtype ) ); + } + isValid = ( dtype ) ? isValidWrapper : isndarrayLike; + return swap; + + /** + * Tests if an input value is an ndarray-like object having a specified data type. + * + * @private + * @param {*} value - value to test + * @returns {boolean} boolean indicating if an input value is an ndarray-like object having a specified data type + */ + function isValidWrapper( value ) { + return isndarrayLikeWithDataType( value, dtype ); + } + + /** + * Interchanges two vectors. + * + * @private + * @param {ndarrayLike} x - first input array + * @param {ndarrayLike} y - second input array + * @param {NegativeInteger} [dim] - dimension along which to interchange elements + * @throws {TypeError} first argument must be an ndarray + * @throws {TypeError} first argument must have at least one dimension + * @throws {TypeError} second argument must be an ndarray + * @throws {TypeError} second argument must have at least one dimension + * @throws {Error} both input arrays must have the same shape + * @throws {RangeError} third argument is out-of-bounds + * @throws {Error} cannot write to read-only array + * @returns {ndarrayLike} `y` + */ + function swap( x, y ) { + var dim; + var xsh; + var ysh; + var xit; + var yit; + var xc; + var yc; + var vx; + var vy; + var dm; + var S; + var N; + var i; + if ( !isValid( x ) ) { + throw new TypeError( format( 'invalid argument. First argument must be an ndarray-like object having a supported data type. Value: `%s`.', x ) ); + } + if ( !isValid( y ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be an ndarray-like object having a supported data type. Value: `%s`.', y ) ); + } + if ( isReadOnly( x ) || isReadOnly( y ) ) { + throw new Error( 'invalid argument. Cannot write to read-only array.' ); + } + // Convert the input arrays to "base" ndarrays: + xc = ndarraylike2ndarray( x ); + yc = ndarraylike2ndarray( y ); + + // Resolve the input array shapes: + xsh = xc.shape; + ysh = yc.shape; + + // Validate that we've been provided non-zero-dimensional arrays... + if ( xsh.length < 1 ) { + throw new TypeError( format( 'invalid argument. First argument must have at least one dimension.' ) ); + } + if ( ysh.length < 1 ) { + throw new TypeError( format( 'invalid argument. Second argument must have at least one dimension.' ) ); + } + // Validate that the arrays have the same shape... + if ( !hasEqualValues( xsh, ysh ) ) { + throw new Error( 'invalid arguments. The first and second arguments must have the same shape.' ); + } + // Validate that the dimension argument is a negative integer... + if ( arguments.length > 2 ) { + dim = arguments[ 2 ]; + if ( !isNegativeInteger( dim ) ) { + throw new TypeError( format( 'invalid argument. Third argument must be a negative integer. Value: `%s`.', dim ) ); + } + } else { + dim = -1; + } + // Validate that a provided dimension index is within bounds... + dm = min( xsh.length, ysh.length ) - 1; + dim = normalizeIndex( dim, dm ); + if ( dim === -1 ) { + throw new RangeError( format( 'invalid argument. Third argument must be a value on the interval: [%d,%d]. Value: `%d`.', -dm, -1, arguments[ 2 ] ) ); + } + // Resolve the size of the interchange dimension: + S = xsh[ dim ]; + + // If we are only provided one-dimensional input arrays, we can skip iterating over stacks... + if ( xsh.length === 1 ) { + base( S, xc.data, xc.strides[0], xc.offset, yc.data, yc.strides[0], yc.offset ); // eslint-disable-line max-len + return y; + } + // Resolve the number of stacks: + N = numel( without( xsh, dim ) ); + + // Create iterators for iterating over stacks of vectors: + xit = nditerStacks( xc, [ dim ] ); + yit = nditerStacks( yc, [ dim ] ); + + // Interchange each pair of vectors... + for ( i = 0; i < N; i++ ) { + vx = xit.next().value; + vy = yit.next().value; + base( S, vx.data, vx.strides[0], vx.offset, vy.data, vy.strides[0], vy.offset ); // eslint-disable-line max-len + } + return y; + } +} + + +// EXPORTS // + +module.exports = factory; diff --git a/tools/swap-factory/package.json b/tools/swap-factory/package.json new file mode 100644 index 000000000..a6773fbc1 --- /dev/null +++ b/tools/swap-factory/package.json @@ -0,0 +1,73 @@ +{ + "name": "@stdlib/blas/tools/swap-factory", + "version": "0.0.0", + "description": "Return a function which interchanges two vectors.", + "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", + "browser": "./lib/main.js", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdmath", + "mathematics", + "math", + "blas", + "level 1", + "linear", + "algebra", + "subroutines", + "swap", + "interchange", + "vector", + "array", + "ndarray", + "factory", + "tool", + "tools", + "utilities", + "utils" + ] +} diff --git a/tools/swap-factory/test/test.js b/tools/swap-factory/test/test.js new file mode 100644 index 000000000..1d810d0f9 --- /dev/null +++ b/tools/swap-factory/test/test.js @@ -0,0 +1,884 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Float64Array = require( '@stdlib/array/float64' ); +var Int16Array = require( '@stdlib/array/int16' ); +var array = require( '@stdlib/ndarray/array' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var dcopy = require( './../../../base/dcopy' ).ndarray; +var dswap = require( './../../../base/dswap' ).ndarray; +var factory = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof factory, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a first argument which is not a function', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {} + ]; + 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() { + factory( value, 'float64' ); + }; + } +}); + +tape( 'the function throws an error if provided an invalid second argument', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + 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() { + factory( dswap, value ); + }; + } +}); + +tape( 'the function returns a function', function test( t ) { + t.strictEqual( typeof factory( dswap, 'float64' ), 'function', 'returns expected value' ); + t.end(); +}); + +tape( 'the returned function has an arity of 2', function test( t ) { + var swap = factory( dswap, 'float64' ); + t.strictEqual( swap.length, 2, 'has expected arity' ); + t.end(); +}); + +tape( 'the returned function throws an error if provided a first argument which is not a non-zero-dimensional ndarray having a supported data type', function test( t ) { + var values; + var swap; + var i; + + swap = factory( dswap, 'float64' ); + + values = [ + 5, + '5', + true, + false, + null, + void 0, + {}, + function noop() {}, + zeros( [], { + 'dtype': 'float64' + }), + array( new Int16Array( 10 ) ) + ]; + + 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 ) { + var y = array( new Float64Array( 10 ) ); + + return function badValue() { + swap( value, y ); + }; + } +}); + +tape( 'the returned function throws an error if provided a first argument which is not a non-zero-dimensional ndarray having a supported data type (dimension)', function test( t ) { + var values; + var swap; + var i; + + swap = factory( dswap, 'float64' ); + + values = [ + 5, + '5', + true, + false, + null, + void 0, + {}, + function noop() {}, + zeros( [], { + 'dtype': 'float64' + }), + array( new Int16Array( 10 ) ) + ]; + + 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 ) { + var y = array( new Float64Array( 10 ) ); + + return function badValue() { + swap( value, y, -1 ); + }; + } +}); + +tape( 'the returned function throws an error if provided a first argument which is a read-only ndarray', function test( t ) { + var values; + var opts; + var swap; + var i; + + swap = factory( dswap, 'float64' ); + + opts = { + 'readonly': true + }; + + values = [ + array( new Float64Array( 10 ), opts ), + array( new Float64Array( 5 ), opts ) + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + var y = array( new Float64Array( value.length ) ); + + return function badValue() { + swap( value, y ); + }; + } +}); + +tape( 'the returned function throws an error if provided a second argument which is not a non-zero-dimensional ndarray having a supported data type', function test( t ) { + var values; + var swap; + var i; + + swap = factory( dswap, 'float64' ); + + values = [ + 5, + '5', + true, + false, + null, + void 0, + {}, + function noop() {}, + zeros( [], { + 'dtype': 'float64' + }), + array( new Int16Array( 10 ) ) + ]; + + 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 ) { + var x = array( new Float64Array( 10 ) ); + + return function badValue() { + swap( x, value ); + }; + } +}); + +tape( 'the returned function throws an error if provided a second argument which is not a non-zero-dimensional ndarray having a supported data type (dimension)', function test( t ) { + var values; + var swap; + var i; + + swap = factory( dswap, 'float64' ); + + values = [ + 5, + '5', + true, + false, + null, + void 0, + {}, + function noop() {}, + zeros( [], { + 'dtype': 'float64' + }), + array( new Int16Array( 10 ) ) + ]; + + 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 ) { + var x = array( new Float64Array( 10 ) ); + + return function badValue() { + swap( x, value, -1 ); + }; + } +}); + +tape( 'the returned function throws an error if provided a second argument which is a read-only ndarray', function test( t ) { + var values; + var opts; + var swap; + var i; + + swap = factory( dswap, 'float64' ); + + opts = { + 'readonly': true + }; + + values = [ + array( new Float64Array( 10 ), opts ), + array( new Float64Array( 5 ), opts ) + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + var x = array( new Float64Array( value.length ) ); + + return function badValue() { + swap( x, value ); + }; + } +}); + +tape( 'the returned function throws an error if provided a third argument which is not a negative integer (vectors)', function test( t ) { + var values; + var swap; + var i; + + swap = factory( dswap, 'float64' ); + + values = [ + '5', + 0, + 5, + NaN, + -3.14, + 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 ) { + var x = array( new Float64Array( 10 ) ); + var y = array( new Float64Array( 10 ) ); + + return function badValue() { + swap( x, y, value ); + }; + } +}); + +tape( 'the returned function throws an error if provided a third argument which is not a negative integer (multi-dimensional arrays)', function test( t ) { + var values; + var swap; + var i; + + swap = factory( dswap, 'float64' ); + + values = [ + '5', + 0, + 5, + NaN, + -3.14, + 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 ) { + var opts = { + 'shape': [ 2, 5 ] + }; + var x = array( new Float64Array( 10 ), opts ); + var y = array( new Float64Array( 10 ), opts ); + + return function badValue() { + swap( x, y, value ); + }; + } +}); + +tape( 'the returned function throws an error if provided a third argument which is out-of-bounds (vectors)', function test( t ) { + var values; + var swap; + var i; + + swap = factory( dswap, 'float64' ); + + values = [ + -2, + -3, + -4, + -5 + ]; + + 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 ) { + var x = array( new Float64Array( 10 ) ); + var y = array( new Float64Array( 10 ) ); + + return function badValue() { + swap( x, y, value ); + }; + } +}); + +tape( 'the returned function throws an error if provided a third argument which is out-of-bounds (multi-dimensional arrays)', function test( t ) { + var values; + var swap; + var i; + + swap = factory( dswap, 'float64' ); + + values = [ + -3, + -4, + -5, + -10 + ]; + + 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 ) { + var opts = { + 'shape': [ 2, 5 ] + }; + var x = array( new Float64Array( 10 ), opts ); + var y = array( new Float64Array( 10 ), opts ); + + return function badValue() { + swap( x, y, value ); + }; + } +}); + +tape( 'the returned function throws an error if provided arrays having different shapes (vectors)', function test( t ) { + var swap = factory( dswap, 'float64' ); + + t.throws( badValue, Error, 'throws an error' ); + t.end(); + + function badValue() { + var x = array( new Float64Array( 10 ) ); + var y = array( new Float64Array( 100 ) ); + swap( x, y ); + } +}); + +tape( 'the returned function throws an error if provided arrays having different shapes (multi-dimensional)', function test( t ) { + var swap = factory( dswap, 'float64' ); + + t.throws( badValue, Error, 'throws an error' ); + t.end(); + + function badValue() { + var x = array( new Float64Array( 100 ), { + 'shape': [ 25, 4 ] + }); + var y = array( new Float64Array( 100 ), { + 'shape': [ 50, 2 ] + }); + swap( x, y, -1 ); + } +}); + +tape( 'the returned function interchanges vectors `x` and `y`', function test( t ) { + var swap; + var xe; + var ye; + var x; + var y; + + swap = factory( dswap, 'float64' ); + + x = new Float64Array( [ 4.0, 2.0, -3.0, 5.0, -1.0, 2.0, -5.0, 6.0 ] ); + y = new Float64Array( [ 2.0, 6.0, -1.0, -4.0, 8.0, 8.0, 2.0, -3.0 ] ); + + xe = new Float64Array( y.length ); + dcopy( y.length, y, 1, 0, xe, 1, 0 ); + + ye = new Float64Array( x.length ); + dcopy( x.length, x, 1, 0, ye, 1, 0 ); + + x = array( x ); + y = array( y ); + + swap( x, y ); + + t.deepEqual( x.data, xe, 'returns expected value' ); + t.notEqual( x.data, xe, 'returns expected value' ); + + t.deepEqual( y.data, ye, 'returns expected value' ); + t.notEqual( y.data, ye, 'returns expected value' ); + + t.end(); +}); + +tape( 'the returned function supports operating on stacks of vectors (default)', function test( t ) { + var swap; + var opts; + var xe; + var ye; + var x; + var y; + + swap = factory( dswap, 'float64' ); + + x = new Float64Array( [ 4.0, 2.0, -3.0, 5.0, -1.0, 2.0, -5.0, 6.0 ] ); + y = new Float64Array( [ 2.0, 6.0, -1.0, -4.0, 8.0, 8.0, 2.0, -3.0 ] ); + + xe = new Float64Array( y.length ); + dcopy( y.length, y, 1, 0, xe, 1, 0 ); + + ye = new Float64Array( x.length ); + dcopy( x.length, x, 1, 0, ye, 1, 0 ); + + opts = { + 'shape': [ 4, 2 ] + }; + x = array( x, opts ); + y = array( y, opts ); + + swap( x, y ); + + t.deepEqual( x.data, xe, 'returns expected value' ); + t.notEqual( x.data, xe, 'returns expected value' ); + + t.deepEqual( y.data, ye, 'returns expected value' ); + t.notEqual( y.data, ye, 'returns expected value' ); + + t.end(); +}); + +tape( 'the returned function supports operating on stacks of vectors (dim=-1)', function test( t ) { + var swap; + var opts; + var xe; + var ye; + var x; + var y; + + swap = factory( dswap, 'float64' ); + + x = new Float64Array( [ 4.0, 2.0, -3.0, 5.0, -1.0, 2.0, -5.0, 6.0 ] ); + y = new Float64Array( [ 2.0, 6.0, -1.0, -4.0, 8.0, 8.0, 2.0, -3.0 ] ); + + xe = new Float64Array( y.length ); + dcopy( y.length, y, 1, 0, xe, 1, 0 ); + + ye = new Float64Array( x.length ); + dcopy( x.length, x, 1, 0, ye, 1, 0 ); + + opts = { + 'shape': [ 4, 2 ] + }; + x = array( x, opts ); + y = array( y, opts ); + + swap( x, y, -1 ); + + t.deepEqual( x.data, xe, 'returns expected value' ); + t.notEqual( x.data, xe, 'returns expected value' ); + + t.deepEqual( y.data, ye, 'returns expected value' ); + t.notEqual( y.data, ye, 'returns expected value' ); + + t.end(); +}); + +tape( 'the returned function supports operating on stacks of vectors (dim=-2)', function test( t ) { + var swap; + var opts; + var xe; + var ye; + var x; + var y; + + swap = factory( dswap, 'float64' ); + + x = new Float64Array( [ 4.0, 2.0, -3.0, 5.0, -1.0, 2.0, -5.0, 6.0 ] ); + y = new Float64Array( [ 2.0, 6.0, -1.0, -4.0, 8.0, 8.0, 2.0, -3.0 ] ); + + xe = new Float64Array( y.length ); + dcopy( y.length, y, 1, 0, xe, 1, 0 ); + + ye = new Float64Array( x.length ); + dcopy( x.length, x, 1, 0, ye, 1, 0 ); + + opts = { + 'shape': [ 4, 2 ] + }; + x = array( x, opts ); + y = array( y, opts ); + + swap( x, y, -2 ); + + t.deepEqual( x.data, xe, 'returns expected value' ); + t.notEqual( x.data, xe, 'returns expected value' ); + + t.deepEqual( y.data, ye, 'returns expected value' ); + t.notEqual( y.data, ye, 'returns expected value' ); + + t.end(); +}); + +tape( 'the returned function supports a strided vector for the first argument', function test( t ) { + var swap; + var xe; + var ye; + var x; + var y; + + swap = factory( dswap, 'float64' ); + + x = new Float64Array([ + 1.0, // 0 + 2.0, + 3.0, // 1 + 4.0, + 5.0 // 2 + ]); + y = new Float64Array([ + 6.0, // 0 + 7.0, // 1 + 8.0 // 2 + ]); + + xe = new Float64Array( [ 6.0, 2.0, 7.0, 4.0, 8.0 ] ); + ye = new Float64Array( [ 1.0, 3.0, 5.0 ] ); + + x = ndarray( 'float64', x, [ 3 ], [ 2 ], 0, 'row-major' ); + y = ndarray( 'float64', y, [ 3 ], [ 1 ], 0, 'row-major' ); + + swap( x, y ); + + t.deepEqual( x.data, xe, 'returns expected value' ); + t.notEqual( x.data, xe, 'returns expected value' ); + + t.deepEqual( y.data, ye, 'returns expected value' ); + t.notEqual( y.data, ye, 'returns expected value' ); + + t.end(); +}); + +tape( 'the returned function supports a strided vector for the second argument', function test( t ) { + var swap; + var xe; + var ye; + var x; + var y; + + swap = factory( dswap, 'float64' ); + + x = new Float64Array([ + 1.0, // 0 + 2.0, // 1 + 3.0 // 2 + ]); + y = new Float64Array([ + 6.0, // 0 + 7.0, + 8.0, // 1 + 9.0, + 10.0 // 2 + ]); + + xe = new Float64Array( [ 6.0, 8.0, 10.0 ] ); + ye = new Float64Array( [ 1.0, 7.0, 2.0, 9.0, 3.0 ] ); + + x = ndarray( 'float64', x, [ 3 ], [ 1 ], 0, 'row-major' ); + y = ndarray( 'float64', y, [ 3 ], [ 2 ], 0, 'row-major' ); + + swap( x, y ); + + t.deepEqual( x.data, xe, 'returns expected value' ); + t.notEqual( x.data, xe, 'returns expected value' ); + + t.deepEqual( y.data, ye, 'returns expected value' ); + t.notEqual( y.data, ye, 'returns expected value' ); + + t.end(); +}); + +tape( 'the returned function supports negative strides', function test( t ) { + var swap; + var xe; + var ye; + var x; + var y; + + swap = factory( dswap, 'float64' ); + + x = new Float64Array([ + 1.0, // 2 + 2.0, + 3.0, // 1 + 4.0, + 5.0 // 0 + ]); + y = new Float64Array([ + 6.0, // 2 + 7.0, // 1 + 8.0 // 0 + ]); + + xe = new Float64Array( [ 6.0, 2.0, 7.0, 4.0, 8.0 ] ); + ye = new Float64Array( [ 1.0, 3.0, 5.0 ] ); + + x = ndarray( 'float64', x, [ 3 ], [ -2 ], x.length-1, 'row-major' ); + y = ndarray( 'float64', y, [ 3 ], [ -1 ], y.length-1, 'row-major' ); + + swap( x, y ); + + t.deepEqual( x.data, xe, 'returns expected value' ); + t.notEqual( x.data, xe, 'returns expected value' ); + + t.deepEqual( y.data, ye, 'returns expected value' ); + t.notEqual( y.data, ye, 'returns expected value' ); + + t.end(); +}); + +tape( 'the returned function supports complex access patterns', function test( t ) { + var swap; + var xe; + var ye; + var x; + var y; + + swap = factory( dswap, 'float64' ); + + x = new Float64Array([ + 1.0, // 0 + 2.0, + 3.0, // 1 + 4.0, + 5.0, // 2 + 6.0 + ]); + y = new Float64Array([ + 7.0, // 2 + 8.0, // 1 + 9.0 // 0 + ]); + + xe = new Float64Array( [ 9.0, 2.0, 8.0, 4.0, 7.0, 6.0 ] ); + ye = new Float64Array( [ 5.0, 3.0, 1.0 ] ); + + x = ndarray( 'float64', x, [ 3 ], [ 2 ], 0, 'row-major' ); + y = ndarray( 'float64', y, [ 3 ], [ -1 ], y.length-1, 'row-major' ); + + swap( x, y ); + + t.deepEqual( x.data, xe, 'returns expected value' ); + t.notEqual( x.data, xe, 'returns expected value' ); + + t.deepEqual( y.data, ye, 'returns expected value' ); + t.notEqual( y.data, ye, 'returns expected value' ); + + t.end(); +}); + +tape( 'the returned function supports strided vectors having offsets', function test( t ) { + var swap; + var xe; + var ye; + var x; + var y; + + swap = factory( dswap, 'float64' ); + + x = new Float64Array([ + 1.0, + 2.0, // 2 + 3.0, + 4.0, // 1 + 5.0, + 6.0 // 0 + ]); + y = new Float64Array([ + 7.0, + 8.0, + 9.0, + 10.0, // 0 + 11.0, // 1 + 12.0 // 2 + ]); + + xe = new Float64Array( [ 1.0, 12.0, 3.0, 11.0, 5.0, 10.0 ] ); + ye = new Float64Array( [ 7.0, 8.0, 9.0, 6.0, 4.0, 2.0 ] ); + + x = ndarray( 'float64', x, [ 3 ], [ -2 ], x.length-1, 'row-major' ); + y = ndarray( 'float64', y, [ 3 ], [ 1 ], 3, 'row-major' ); + + swap( x, y ); + + t.deepEqual( x.data, xe, 'returns expected value' ); + t.notEqual( x.data, xe, 'returns expected value' ); + + t.deepEqual( y.data, ye, 'returns expected value' ); + t.notEqual( y.data, ye, 'returns expected value' ); + + t.end(); +}); + +tape( 'the returned function supports underlying data buffers with view offsets', function test( t ) { + var swap; + var x0; + var y0; + var x1; + var y1; + var xe; + var ye; + + swap = factory( dswap, 'float64' ); + + x0 = new Float64Array([ + 1.0, + 2.0, // 0 + 3.0, + 4.0, // 1 + 5.0, + 6.0 // 2 + ]); + y0 = new Float64Array([ + 7.0, + 8.0, + 9.0, + 10.0, // 0 + 11.0, // 1 + 12.0 // 2 + ]); + + x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); + y1 = new Float64Array( y0.buffer, y0.BYTES_PER_ELEMENT*3 ); + + xe = new Float64Array( [ 1.0, 10.0, 3.0, 11.0, 5.0, 12.0 ] ); + ye = new Float64Array( [ 7.0, 8.0, 9.0, 2.0, 4.0, 6.0 ] ); + + x1 = ndarray( 'float64', x1, [ 3 ], [ 2 ], 0, 'row-major' ); + y1 = ndarray( 'float64', y1, [ 3 ], [ 1 ], 0, 'row-major' ); + + swap( x1, y1 ); + + t.deepEqual( x0, xe, 'returns expected value' ); + t.notEqual( x0, xe, 'returns expected value' ); + + t.deepEqual( y0, ye, 'returns expected value' ); + t.notEqual( y0, ye, 'returns expected value' ); + + t.end(); +}); + +tape( 'the returned function returns a reference to the second input array', function test( t ) { + var swap; + var out; + var x; + var y; + + swap = factory( dswap, 'float64' ); + + x = array( new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ) ); + y = array( new Float64Array( [ 6.0, 7.0, 8.0, 9.0, 10.0 ] ) ); + + out = swap( x, y ); + + t.strictEqual( out, y, 'returns expected value' ); + t.end(); +}); diff --git a/tools/test/test.js b/tools/test/test.js new file mode 100644 index 000000000..a7d71c830 --- /dev/null +++ b/tools/test/test.js @@ -0,0 +1,40 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 objectKeys = require( '@stdlib/utils/keys' ); +var ns = require( './../lib' ); + + +// TESTS // + +tape( 'main export is an object', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof ns, 'object', 'main export is an object' ); + t.end(); +}); + +tape( 'the exported object contains key-value pairs', function test( t ) { + var keys = objectKeys( ns ); + t.equal( keys.length > 0, true, 'has keys' ); + t.end(); +});