From a38509a637ceea8ebca5e8cc231aa5a7fcd25eba Mon Sep 17 00:00:00 2001 From: Karan Anand Date: Wed, 30 Sep 2026 11:05:47 -0700 Subject: [PATCH] feat: add `blas/ext/base/ndarray/swapx` --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown_pkg_readmes status: passed - task: lint_markdown_docs status: na - task: lint_markdown status: na - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: passed - task: lint_c_examples status: passed - task: lint_c_benchmarks status: passed - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed --- --- .../blas/ext/base/ndarray/swapx/README.md | 299 ++++++++++++ .../base/ndarray/swapx/benchmark/benchmark.js | 110 +++++ .../swapx/benchmark/benchmark.native.js | 115 +++++ .../base/ndarray/swapx/benchmark/c/Makefile | 146 ++++++ .../swapx/benchmark/c/benchmark.length.c | 210 ++++++++ .../blas/ext/base/ndarray/swapx/binding.gyp | 265 +++++++++++ .../blas/ext/base/ndarray/swapx/docs/repl.txt | 33 ++ .../base/ndarray/swapx/docs/types/index.d.ts | 58 +++ .../ext/base/ndarray/swapx/docs/types/test.ts | 70 +++ .../base/ndarray/swapx/examples/c/Makefile | 146 ++++++ .../base/ndarray/swapx/examples/c/example.c | 92 ++++ .../ext/base/ndarray/swapx/examples/index.js | 41 ++ .../blas/ext/base/ndarray/swapx/include.gypi | 53 +++ .../stdlib/blas/ext/base/ndarray/swapx.h | 40 ++ .../blas/ext/base/ndarray/swapx/lib/index.js | 63 +++ .../blas/ext/base/ndarray/swapx/lib/main.js | 79 +++ .../blas/ext/base/ndarray/swapx/lib/native.js | 73 +++ .../blas/ext/base/ndarray/swapx/manifest.json | 109 +++++ .../blas/ext/base/ndarray/swapx/package.json | 76 +++ .../blas/ext/base/ndarray/swapx/src/Makefile | 70 +++ .../blas/ext/base/ndarray/swapx/src/addon.c | 67 +++ .../blas/ext/base/ndarray/swapx/src/main.c | 53 +++ .../blas/ext/base/ndarray/swapx/test/test.js | 77 +++ .../ext/base/ndarray/swapx/test/test.main.js | 439 +++++++++++++++++ .../base/ndarray/swapx/test/test.native.js | 448 ++++++++++++++++++ 25 files changed, 3232 insertions(+) create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/README.md create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/benchmark/benchmark.native.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/benchmark/c/Makefile create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/benchmark/c/benchmark.length.c create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/binding.gyp create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/examples/c/Makefile create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/examples/c/example.c create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/examples/index.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/include.gypi create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/include/stdlib/blas/ext/base/ndarray/swapx.h create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/lib/index.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/lib/main.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/lib/native.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/manifest.json create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/package.json create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/src/Makefile create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/src/addon.c create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/src/main.c create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/test/test.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/test/test.main.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/test/test.native.js diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/README.md b/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/README.md new file mode 100644 index 000000000000..e98c1b34f10a --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/README.md @@ -0,0 +1,299 @@ + + +# swapx + +> Add a scalar constant to each element in an input one-dimensional single-precision floating-point ndarray and assign the results to elements in a one-dimensional single-precision floating-point output ndarray. + +
+ +
+ + + +
+ +## Usage + +```javascript +var swapx = require( '@stdlib/blas/ext/base/ndarray/swapx' ); +``` + +#### swapx( arrays ) + +Adds a scalar constant to each element in an input one-dimensional single-precision floating-point ndarray and assigns the results to elements in a one-dimensional single-precision floating-point output ndarray. + +```javascript +var Float32Vector = require( '@stdlib/ndarray/vector/float32' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); + +var x = new Float32Vector( [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ] ); +var w = new Float32Vector( 8 ); + +var alpha = scalar2ndarray( 5.0, { + 'dtype': 'float32' +}); + +swapx( [ x, w, alpha ] ); +// w => [ 3.0, 6.0, 8.0, 0.0, 9.0, 5.0, 4.0, 2.0 ] +``` + +The function has the following parameters: + +- **arrays**: array-like object containing the following ndarrays: + + - a one-dimensional input ndarray. + - a one-dimensional output ndarray. + - a zero-dimensional ndarray containing the scalar constant to add. + +
+ + + +
+ +
+ + + +
+ +## Examples + + + +```javascript +var discreteUniform = require( '@stdlib/random/discrete-uniform' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var ndarraylike2scalar = require( '@stdlib/ndarray/ndarraylike2scalar' ); +var swapx = require( '@stdlib/blas/ext/base/ndarray/swapx' ); + +var opts = { + 'dtype': 'float32' +}; + +var x = discreteUniform( [ 10 ], -100, 100, opts ); +console.log( ndarray2array( x ) ); + +var w = discreteUniform( [ 10 ], -100, 100, opts ); +console.log( ndarray2array( w ) ); + +var alpha = scalar2ndarray( 5.0, opts ); +console.log( 'Alpha: %d', ndarraylike2scalar( alpha ) ); + +swapx( [ x, w, alpha ] ); +console.log( ndarray2array( w ) ); +``` + +
+ + + + + +* * * + +
+ +## C APIs + + + +
+ +
+ + + + + +
+ +### Usage + +```c +#include "stdlib/blas/ext/base/ndarray/swapx.h" +``` + +#### stdlib_blas_ext_swapx( arrays ) + +Adds a scalar constant to each element in an input one-dimensional single-precision floating-point ndarray and assigns the results to elements in a one-dimensional single-precision floating-point output ndarray. + +```c +#include "stdlib/ndarray/ctor.h" +#include "stdlib/ndarray/dtypes.h" +#include "stdlib/ndarray/index_modes.h" +#include "stdlib/ndarray/orders.h" +#include "stdlib/ndarray/base/bytes_per_element.h" +#include + +// Create ndarrays: +const float dataX[] = { -2.0f, 1.0f, 3.0f, -5.0f }; +float dataW[] = { 0.0f, 0.0f, 0.0f, 0.0f }; +int64_t shape[] = { 4 }; +int64_t strides[] = { STDLIB_NDARRAY_FLOAT32_BYTES_PER_ELEMENT }; +int8_t submodes[] = { STDLIB_NDARRAY_INDEX_ERROR }; + +struct ndarray *x = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT32, (uint8_t *)dataX, 1, shape, strides, 0, STDLIB_NDARRAY_ROW_MAJOR, STDLIB_NDARRAY_INDEX_ERROR, 1, submodes ); +struct ndarray *w = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT32, (uint8_t *)dataW, 1, shape, strides, 0, STDLIB_NDARRAY_ROW_MAJOR, STDLIB_NDARRAY_INDEX_ERROR, 1, submodes ); + +// Create an ndarray containing the scalar constant to add: +const float adata[] = { 5.0f }; +int64_t astrides[] = { 0 }; + +struct ndarray *alpha = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT32, (uint8_t *)adata, 0, NULL, astrides, 0, STDLIB_NDARRAY_ROW_MAJOR, STDLIB_NDARRAY_INDEX_ERROR, 1, submodes ); + +// Perform computation: +const struct ndarray *arrays[] = { x, w, alpha }; +stdlib_blas_ext_swapx( arrays ); + +// Free allocated memory: +stdlib_ndarray_free( x ); +stdlib_ndarray_free( w ); +stdlib_ndarray_free( alpha ); +``` + +The function accepts the following arguments: + +- **arrays**: `[in] struct ndarray**` list containing the following ndarrays: + + - `[in] struct ndarray*` a one-dimensional input ndarray. + - `[out] struct ndarray*` a one-dimensional output ndarray. + - `[in] struct ndarray*` a zero-dimensional ndarray containing the scalar constant to add. + +```c +void stdlib_blas_ext_swapx( const struct ndarray *arrays[] ); +``` + +
+ + + + + +
+ +
+ + + + + +
+ +### Examples + +```c +#include "stdlib/blas/ext/base/ndarray/swapx.h" +#include "stdlib/ndarray/ctor.h" +#include "stdlib/ndarray/dtypes.h" +#include "stdlib/ndarray/index_modes.h" +#include "stdlib/ndarray/orders.h" +#include "stdlib/ndarray/base/bytes_per_element.h" +#include +#include +#include + +int main( void ) { + // Create data buffers: + const float dataX[] = { -2.0f, 1.0f, 3.0f, -5.0f }; + float dataW[] = { 0.0f, 0.0f, 0.0f, 0.0f }; + + // Specify the number of array dimensions: + const int64_t ndims = 1; + + // Specify the array shape: + int64_t shape[] = { 4 }; + + // Specify the array strides: + int64_t strides[] = { STDLIB_NDARRAY_FLOAT32_BYTES_PER_ELEMENT }; + + // Specify the byte offset: + const int64_t offset = 0; + + // Specify the array order: + const enum STDLIB_NDARRAY_ORDER order = STDLIB_NDARRAY_ROW_MAJOR; + + // Specify the index mode: + const enum STDLIB_NDARRAY_INDEX_MODE imode = STDLIB_NDARRAY_INDEX_ERROR; + + // Specify the subscript index modes: + int8_t submodes[] = { STDLIB_NDARRAY_INDEX_ERROR }; + const int64_t nsubmodes = 1; + + // Create ndarrays: + struct ndarray *x = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT32, (uint8_t *)dataX, ndims, shape, strides, offset, order, imode, nsubmodes, submodes ); + struct ndarray *w = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT32, (uint8_t *)dataW, ndims, shape, strides, offset, order, imode, nsubmodes, submodes ); + + // Create a data buffer for an ndarray containing the scalar constant to add: + const float adata[] = { 5.0f }; + + // Specify the array strides for a zero-dimensional ndarray: + int64_t astrides[] = { 0 }; + + // Create an ndarray containing the scalar constant to add: + struct ndarray *alpha = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT32, (uint8_t *)adata, 0, NULL, astrides, 0, order, imode, nsubmodes, submodes ); + if ( x == NULL || w == NULL || alpha == NULL ) { + fprintf( stderr, "Error allocating memory.\n" ); + exit( 1 ); + } + + // Define a list of ndarrays: + const struct ndarray *arrays[] = { x, w, alpha }; + + // Perform computation: + stdlib_blas_ext_swapx( arrays ); + + // Print the result: + for ( int i = 0; i < 4; i++ ) { + printf( "w[ %i ] = %f\n", i, dataW[ i ] ); + } + + // Free allocated memory: + stdlib_ndarray_free( x ); + stdlib_ndarray_free( w ); + stdlib_ndarray_free( alpha ); +} +``` + +
+ + + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/benchmark/benchmark.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/benchmark/benchmark.js new file mode 100644 index 000000000000..92b7c1d094e6 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/benchmark/benchmark.js @@ -0,0 +1,110 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var uniform = require( '@stdlib/random/uniform' ); +var Float32Vector = require( '@stdlib/ndarray/vector/float32' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var swapx = require( './../lib/main.js' ); + + +// VARIABLES // + +var options = { + 'dtype': 'float32' +}; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - ndarray length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var alpha; + var x; + var w; + + x = uniform( [ len ], -100.0, 100.0, options ); + w = new Float32Vector( len ); + alpha = scalar2ndarray( 5.0, options ); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var out; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = swapx( [ x, w, alpha ] ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( typeof out !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + 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( format( '%s:len=%d', pkg, len ), f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/benchmark/benchmark.native.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/benchmark/benchmark.native.js new file mode 100644 index 000000000000..f36002eb87f6 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/benchmark/benchmark.native.js @@ -0,0 +1,115 @@ +/** +* @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 resolve = require( 'path' ).resolve; +var bench = require( '@stdlib/bench' ); +var uniform = require( '@stdlib/random/uniform' ); +var Float32Vector = require( '@stdlib/ndarray/vector/float32' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var format = require( '@stdlib/string/format' ); +var tryRequire = require( '@stdlib/utils/try-require' ); +var pkg = require( './../package.json' ).name; + + +// VARIABLES // + +var swapx = tryRequire( resolve( __dirname, './../lib/native.js' ) ); +var opts = { + 'skip': ( swapx instanceof Error ) +}; +var options = { + 'dtype': 'float32' +}; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - ndarray length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var alpha; + var x; + var w; + + x = uniform( [ len ], -100.0, 100.0, options ); + w = new Float32Vector( len ); + alpha = scalar2ndarray( 5.0, options ); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var out; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = swapx( [ x, w, alpha ] ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( typeof out !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + 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( format( '%s::native:len=%d', pkg, len ), opts, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/benchmark/c/Makefile b/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/benchmark/c/Makefile new file mode 100644 index 000000000000..0756dc7da20a --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/benchmark/c/Makefile @@ -0,0 +1,146 @@ +#/ +# @license Apache-2.0 +# +# Copyright (c) 2026 The Stdlib Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +#/ + +# VARIABLES # + +ifndef VERBOSE + QUIET := @ +else + QUIET := +endif + +# Determine the OS ([1][1], [2][2]). +# +# [1]: https://en.wikipedia.org/wiki/Uname#Examples +# [2]: http://stackoverflow.com/a/27776822/2225624 +OS ?= $(shell uname) +ifneq (, $(findstring MINGW,$(OS))) + OS := WINNT +else +ifneq (, $(findstring MSYS,$(OS))) + OS := WINNT +else +ifneq (, $(findstring CYGWIN,$(OS))) + OS := WINNT +else +ifneq (, $(findstring Windows_NT,$(OS))) + OS := WINNT +endif +endif +endif +endif + +# Define the program used for compiling C source files: +ifdef C_COMPILER + CC := $(C_COMPILER) +else + CC := gcc +endif + +# Define the command-line options when compiling C files: +CFLAGS ?= \ + -std=c99 \ + -O3 \ + -Wall \ + -pedantic + +# Determine whether to generate position independent code ([1][1], [2][2]). +# +# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options +# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option +ifeq ($(OS), WINNT) + fPIC ?= +else + fPIC ?= -fPIC +endif + +# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): +INCLUDE ?= + +# List of source files: +SOURCE_FILES ?= + +# List of libraries (e.g., `-lopenblas -lpthread`): +LIBRARIES ?= + +# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): +LIBPATH ?= + +# List of C targets: +c_targets := benchmark.length.out + + +# RULES # + +#/ +# Compiles source files. +# +# @param {string} [C_COMPILER] - C compiler (e.g., `gcc`) +# @param {string} [CFLAGS] - C compiler options +# @param {(string|void)} [fPIC] - compiler flag determining whether to generate position independent code (e.g., `-fPIC`) +# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop/include`) +# @param {string} [SOURCE_FILES] - list of source files +# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) +# @param {string} [LIBRARIES] - list of libraries (e.g., `-lopenblas -lpthread`) +# +# @example +# make +# +# @example +# make all +#/ +all: $(c_targets) + +.PHONY: all + +#/ +# Compiles C source files. +# +# @private +# @param {string} CC - C compiler (e.g., `gcc`) +# @param {string} CFLAGS - C compiler options +# @param {(string|void)} fPIC - compiler flag determining whether to generate position independent code (e.g., `-fPIC`) +# @param {string} INCLUDE - list of includes (e.g., `-I /foo/bar`) +# @param {string} SOURCE_FILES - list of source files +# @param {string} LIBPATH - list of library paths (e.g., `-L /foo/bar`) +# @param {string} LIBRARIES - list of libraries (e.g., `-lopenblas`) +#/ +$(c_targets): %.out: %.c + $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) + +#/ +# Runs compiled benchmarks. +# +# @example +# make run +#/ +run: $(c_targets) + $(QUIET) ./$< + +.PHONY: run + +#/ +# Removes generated files. +# +# @example +# make clean +#/ +clean: + $(QUIET) -rm -f *.o *.out + +.PHONY: clean diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/benchmark/c/benchmark.length.c b/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/benchmark/c/benchmark.length.c new file mode 100644 index 000000000000..eb2b9586884f --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/benchmark/c/benchmark.length.c @@ -0,0 +1,210 @@ +/** +* @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. +*/ + +#include "stdlib/blas/ext/base/ndarray/swapx.h" +#include "stdlib/ndarray/ctor.h" +#include "stdlib/ndarray/dtypes.h" +#include "stdlib/ndarray/index_modes.h" +#include "stdlib/ndarray/orders.h" +#include "stdlib/ndarray/base/bytes_per_element.h" +#include +#include +#include +#include +#include +#include + +#define NAME "swapx" +#define ITERATIONS 1000000 +#define REPEATS 3 +#define MIN 1 +#define MAX 6 + +/** +* Prints the TAP version. +*/ +static void print_version( void ) { + printf( "TAP version 13\n" ); +} + +/** +* Prints the TAP summary. +* +* @param total total number of tests +* @param passing total number of passing tests +*/ +static void print_summary( int total, int passing ) { + printf( "#\n" ); + printf( "1..%d\n", total ); // TAP plan + printf( "# total %d\n", total ); + printf( "# pass %d\n", passing ); + printf( "#\n" ); + printf( "# ok\n" ); +} + +/** +* Prints benchmarks results. +* +* @param iterations number of iterations +* @param elapsed elapsed time in seconds +*/ +static void print_results( int iterations, double elapsed ) { + double rate = (double)iterations / elapsed; + printf( " ---\n" ); + printf( " iterations: %d\n", iterations ); + printf( " elapsed: %0.9f\n", elapsed ); + printf( " rate: %0.9f\n", rate ); + printf( " ...\n" ); +} + +/** +* Returns a clock time. +* +* @return clock time +*/ +static double tic( void ) { + struct timeval now; + gettimeofday( &now, NULL ); + return (double)now.tv_sec + (double)now.tv_usec/1.0e6; +} + +/** +* Generates a random number on the interval [min,max). +* +* @param min minimum value (inclusive) +* @param max maximum value (exclusive) +* @return random number +*/ +static float random_uniform( const float min, const float max ) { + float v = (float)rand() / ( (float)RAND_MAX + 1.0f ); + return min + ( v*(max-min) ); +} + +/** +* Runs a benchmark. +* +* @param iterations number of iterations +* @param len array length +* @return elapsed time in seconds +*/ +static double benchmark( int iterations, int len ) { + enum STDLIB_NDARRAY_INDEX_MODE imode; + const struct ndarray *arrays[ 3 ]; + enum STDLIB_NDARRAY_ORDER order; + int64_t astrides[ 1 ]; + struct ndarray *alpha; + int8_t submodes[ 1 ]; + int64_t strides[ 1 ]; + int64_t shape[ 1 ]; + int64_t nsubmodes; + struct ndarray *x; + struct ndarray *w; + float adata[ 1 ]; + int64_t offset; + double elapsed; + int64_t ndims; + float *dataX; + float *dataW; + double t; + int i; + + ndims = 1; + shape[ 0 ] = len; + strides[ 0 ] = STDLIB_NDARRAY_FLOAT32_BYTES_PER_ELEMENT; + offset = 0; + order = STDLIB_NDARRAY_ROW_MAJOR; + imode = STDLIB_NDARRAY_INDEX_ERROR; + submodes[ 0 ] = imode; + nsubmodes = 1; + + dataX = (float *)malloc( len * sizeof( float ) ); + dataW = (float *)malloc( len * sizeof( float ) ); + for ( i = 0; i < len; i++ ) { + dataX[ i ] = random_uniform( -100.0f, 100.0f ); + dataW[ i ] = 0.0f; + } + + // cppcheck-suppress invalidPointerCast + x = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT32, (uint8_t *)dataX, ndims, shape, strides, offset, order, imode, nsubmodes, submodes ); + + // cppcheck-suppress invalidPointerCast + w = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT32, (uint8_t *)dataW, ndims, shape, strides, offset, order, imode, nsubmodes, submodes ); + + adata[ 0 ] = 5.0f; + astrides[ 0 ] = 0; + + // cppcheck-suppress invalidPointerCast + alpha = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT32, (uint8_t *)adata, 0, NULL, astrides, 0, order, imode, nsubmodes, submodes ); + + arrays[ 0 ] = x; + arrays[ 1 ] = w; + arrays[ 2 ] = alpha; + + t = tic(); + for ( i = 0; i < iterations; i++ ) { + stdlib_blas_ext_swapx( arrays ); + if ( dataW[ 0 ] != dataW[ 0 ] ) { + printf( "should not return NaN\n" ); + break; + } + } + elapsed = tic() - t; + if ( dataW[ 0 ] != dataW[ 0 ] ) { + printf( "should not return NaN\n" ); + } + stdlib_ndarray_free( x ); + stdlib_ndarray_free( w ); + stdlib_ndarray_free( alpha ); + free( dataX ); + free( dataW ); + arrays[ 0 ] = NULL; + arrays[ 1 ] = NULL; + arrays[ 2 ] = NULL; + + return elapsed; +} + +/** +* Main execution sequence. +*/ +int main( void ) { + double elapsed; + int count; + int iter; + int len; + int i; + int j; + + // Use the current time to seed the random number generator: + srand( time( NULL ) ); + + print_version(); + count = 0; + for ( i = MIN; i <= MAX; i++ ) { + len = pow( 10, i ); + iter = ITERATIONS / pow( 10, i-1 ); + for ( j = 0; j < REPEATS; j++ ) { + count += 1; + printf( "# c::%s:len=%d\n", NAME, len ); + elapsed = benchmark( iter, len ); + print_results( iter, elapsed ); + printf( "ok %d benchmark finished\n", count ); + } + } + print_summary( count, count ); +} diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/binding.gyp b/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/binding.gyp new file mode 100644 index 000000000000..60dce9d0b31a --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/binding.gyp @@ -0,0 +1,265 @@ +# @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. + +# A `.gyp` file for building a Node.js native add-on. +# +# [1]: https://gyp.gsrc.io/docs/InputFormatReference.md +# [2]: https://gyp.gsrc.io/docs/UserDocumentation.md +{ + # List of files to include in this file: + 'includes': [ + './include.gypi', + ], + + # Define variables to be used throughout the configuration for all targets: + 'variables': { + # Target name should match the add-on export name: + 'addon_target_name%': 'addon', + + # Fortran compiler (to override -Dfortran_compiler=): + 'fortran_compiler%': 'gfortran', + + # Fortran compiler flags: + 'fflags': [ + # Specify the Fortran standard to which a program is expected to conform: + '-std=f95', + + # Indicate that the layout is free-form source code: + '-ffree-form', + + # Aggressive optimization: + '-O3', + + # Enable commonly used warning options: + '-Wall', + + # Warn if source code contains problematic language features: + '-Wextra', + + # Warn if a procedure is called without an explicit interface: + '-Wimplicit-interface', + + # Do not transform names of entities specified in Fortran source files by appending underscores (i.e., don't mangle names, thus allowing easier usage in C wrappers): + '-fno-underscoring', + + # Warn if source code contains Fortran 95 extensions and C-language constructs: + '-pedantic', + + # Compile but do not link (output is an object file): + '-c', + ], + + # Set variables based on the host OS: + 'conditions': [ + [ + 'OS=="win"', + { + # Define the object file suffix: + 'obj': 'obj', + }, + { + # Define the object file suffix: + 'obj': 'o', + } + ], # end condition (OS=="win") + ], # end conditions + }, # end variables + + # Define compile targets: + 'targets': [ + + # Target to generate an add-on: + { + # The target name should match the add-on export name: + 'target_name': '<(addon_target_name)', + + # Define dependencies: + 'dependencies': [], + + # Define directories which contain relevant include headers: + 'include_dirs': [ + # Local include directory: + '<@(include_dirs)', + ], + + # List of source files: + 'sources': [ + '<@(src_files)', + ], + + # Settings which should be applied when a target's object files are used as linker input: + 'link_settings': { + # Define libraries: + 'libraries': [ + '<@(libraries)', + ], + + # Define library directories: + 'library_dirs': [ + '<@(library_dirs)', + ], + }, + + # C/C++ compiler flags: + 'cflags': [ + # Enable commonly used warning options: + '-Wall', + + # Aggressive optimization: + '-O3', + ], + + # C specific compiler flags: + 'cflags_c': [ + # Specify the C standard to which a program is expected to conform: + '-std=c99', + ], + + # C++ specific compiler flags: + 'cflags_cpp': [ + # Specify the C++ standard to which a program is expected to conform: + '-std=c++11', + ], + + # Linker flags: + 'ldflags': [], + + # Apply conditions based on the host OS: + 'conditions': [ + [ + 'OS=="mac"', + { + # Linker flags: + 'ldflags': [ + '-undefined dynamic_lookup', + '-Wl,-no-pie', + '-Wl,-search_paths_first', + ], + }, + ], # end condition (OS=="mac") + [ + 'OS!="win"', + { + # C/C++ flags: + 'cflags': [ + # Generate platform-independent code: + '-fPIC', + ], + }, + ], # end condition (OS!="win") + ], # end conditions + + # Define custom build actions for particular inputs: + 'rules': [ + { + # Define a rule for processing Fortran files: + 'extension': 'f', + + # Define the pathnames to be used as inputs when performing processing: + 'inputs': [ + # Full path of the current input: + '<(RULE_INPUT_PATH)' + ], + + # Define the outputs produced during processing: + 'outputs': [ + # Store an output object file in a directory for placing intermediate results (only accessible within a single target): + '<(INTERMEDIATE_DIR)/<(RULE_INPUT_ROOT).<(obj)' + ], + + # Define the rule for compiling Fortran based on the host OS: + 'conditions': [ + [ + 'OS=="win"', + + # Rule to compile Fortran on Windows: + { + 'rule_name': 'compile_fortran_windows', + 'message': 'Compiling Fortran file <(RULE_INPUT_PATH) on Windows...', + + 'process_outputs_as_sources': 0, + + # Define the command-line invocation: + 'action': [ + '<(fortran_compiler)', + '<@(fflags)', + '<@(_inputs)', + '-o', + '<@(_outputs)', + ], + }, + + # Rule to compile Fortran on non-Windows: + { + 'rule_name': 'compile_fortran_linux', + 'message': 'Compiling Fortran file <(RULE_INPUT_PATH) on Linux...', + + 'process_outputs_as_sources': 1, + + # Define the command-line invocation: + 'action': [ + '<(fortran_compiler)', + '<@(fflags)', + '-fPIC', # generate platform-independent code + '<@(_inputs)', + '-o', + '<@(_outputs)', + ], + } + ], # end condition (OS=="win") + ], # end conditions + }, # end rule (extension=="f") + ], # end rules + }, # end target <(addon_target_name) + + # Target to copy a generated add-on to a standard location: + { + 'target_name': 'copy_addon', + + # Declare that the output of this target is not linked: + 'type': 'none', + + # Define dependencies: + 'dependencies': [ + # Require that the add-on be generated before building this target: + '<(addon_target_name)', + ], + + # Define a list of actions: + 'actions': [ + { + 'action_name': 'copy_addon', + 'message': 'Copying addon...', + + # Explicitly list the inputs in the command-line invocation below: + 'inputs': [], + + # Declare the expected outputs: + 'outputs': [ + '<(addon_output_dir)/<(addon_target_name).node', + ], + + # Define the command-line invocation: + 'action': [ + 'cp', + '<(PRODUCT_DIR)/<(addon_target_name).node', + '<(addon_output_dir)/<(addon_target_name).node', + ], + }, + ], # end actions + }, # end target copy_addon + ], # end targets +} diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/docs/repl.txt b/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/docs/repl.txt new file mode 100644 index 000000000000..fc8fbe89d3f0 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/docs/repl.txt @@ -0,0 +1,33 @@ + +{{alias}}( arrays ) + Adds a scalar constant to each element in an input one-dimensional single- + precision floating-point ndarray and assigns the results to elements in a + one-dimensional single-precision floating-point output ndarray. + + Parameters + ---------- + arrays: ArrayLikeObject + Array-like object containing the following ndarrays: + + - a one-dimensional input ndarray. + - a one-dimensional output ndarray. + - a zero-dimensional ndarray containing the scalar constant to add. + + Returns + ------- + out: ndarray + Output ndarray. + + Examples + -------- + > var xbuf = [ -2.0, 1.0, 3.0, -5.0 ]; + > var x = new {{alias:@stdlib/ndarray/vector/float32}}( xbuf ); + > var w = new {{alias:@stdlib/ndarray/vector/float32}}( 4 ); + > var opts = { 'dtype': 'float32' }; + > var alpha = {{alias:@stdlib/ndarray/from-scalar}}( 5.0, opts ); + > {{alias}}( [ x, w, alpha ] ) + [ 3.0, 6.0, 8.0, 0.0 ] + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/docs/types/index.d.ts b/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/docs/types/index.d.ts new file mode 100644 index 000000000000..808697f07338 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/docs/types/index.d.ts @@ -0,0 +1,58 @@ +/* +* @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 { float32ndarray, typedndarray } from '@stdlib/types/ndarray'; + +/** +* Adds a scalar constant to each element in an input one-dimensional single-precision floating-point ndarray and assigns the results to elements in a one-dimensional single-precision floating-point output ndarray. +* +* ## Notes +* +* - The function expects the following ndarrays: +* +* - a one-dimensional input ndarray. +* - a one-dimensional output ndarray. +* - a zero-dimensional ndarray containing the scalar constant to add. +* +* @param arrays - array-like object containing ndarrays +* @returns output ndarray +* +* @example +* var Float32Vector = require( '@stdlib/ndarray/vector/float32' ); +* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +* +* var x = new Float32Vector( [ -2.0, 1.0, 3.0, -5.0 ] ); +* var w = new Float32Vector( [ 0.0, 0.0, 0.0, 0.0 ] ); +* +* var alpha = scalar2ndarray( 5.0, { +* 'dtype': 'float32' +* }); +* +* var out = swapx( [ x, w, alpha ] ); +* // returns [ 3.0, 6.0, 8.0, 0.0 ] +*/ +declare function swapx( arrays: [ float32ndarray, float32ndarray, typedndarray ] ): float32ndarray; + + +// EXPORTS // + +export = swapx; diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/docs/types/test.ts b/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/docs/types/test.ts new file mode 100644 index 000000000000..ae311ca7a201 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/docs/types/test.ts @@ -0,0 +1,70 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable space-in-parens */ + +import zeros = require( '@stdlib/ndarray/zeros' ); +import scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +import swapx = require( './index' ); + + +// TESTS // + +// The function returns an ndarray... +{ + const x = zeros( [ 10 ], { + 'dtype': 'float32' + }); + const w = zeros( [ 10 ], { + 'dtype': 'float32' + }); + const alpha = scalar2ndarray( 5.0, { + 'dtype': 'float32' + }); + + swapx( [ x, w, alpha ] ); // $ExpectType float32ndarray +} + +// The compiler throws an error if the function is provided a first argument which is not an array of ndarrays... +{ + swapx( '10' ); // $ExpectError + swapx( 5 ); // $ExpectError + swapx( true ); // $ExpectError + swapx( false ); // $ExpectError + swapx( null ); // $ExpectError + swapx( undefined ); // $ExpectError + swapx( [] ); // $ExpectError + swapx( {} ); // $ExpectError + swapx( ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = zeros( [ 10 ], { + 'dtype': 'float32' + }); + const w = zeros( [ 10 ], { + 'dtype': 'float32' + }); + const alpha = scalar2ndarray( 5.0, { + 'dtype': 'float32' + }); + + swapx(); // $ExpectError + swapx( [ x, w, alpha ], {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/examples/c/Makefile b/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/examples/c/Makefile new file mode 100644 index 000000000000..c8f8e9a1517b --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/examples/c/Makefile @@ -0,0 +1,146 @@ +#/ +# @license Apache-2.0 +# +# Copyright (c) 2026 The Stdlib Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +#/ + +# VARIABLES # + +ifndef VERBOSE + QUIET := @ +else + QUIET := +endif + +# Determine the OS ([1][1], [2][2]). +# +# [1]: https://en.wikipedia.org/wiki/Uname#Examples +# [2]: http://stackoverflow.com/a/27776822/2225624 +OS ?= $(shell uname) +ifneq (, $(findstring MINGW,$(OS))) + OS := WINNT +else +ifneq (, $(findstring MSYS,$(OS))) + OS := WINNT +else +ifneq (, $(findstring CYGWIN,$(OS))) + OS := WINNT +else +ifneq (, $(findstring Windows_NT,$(OS))) + OS := WINNT +endif +endif +endif +endif + +# Define the program used for compiling C source files: +ifdef C_COMPILER + CC := $(C_COMPILER) +else + CC := gcc +endif + +# Define the command-line options when compiling C files: +CFLAGS ?= \ + -std=c99 \ + -O3 \ + -Wall \ + -pedantic + +# Determine whether to generate position independent code ([1][1], [2][2]). +# +# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options +# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option +ifeq ($(OS), WINNT) + fPIC ?= +else + fPIC ?= -fPIC +endif + +# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): +INCLUDE ?= + +# List of source files: +SOURCE_FILES ?= + +# List of libraries (e.g., `-lopenblas -lpthread`): +LIBRARIES ?= + +# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): +LIBPATH ?= + +# List of C targets: +c_targets := example.out + + +# RULES # + +#/ +# Compiles source files. +# +# @param {string} [C_COMPILER] - C compiler (e.g., `gcc`) +# @param {string} [CFLAGS] - C compiler options +# @param {(string|void)} [fPIC] - compiler flag determining whether to generate position independent code (e.g., `-fPIC`) +# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop/include`) +# @param {string} [SOURCE_FILES] - list of source files +# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) +# @param {string} [LIBRARIES] - list of libraries (e.g., `-lopenblas -lpthread`) +# +# @example +# make +# +# @example +# make all +#/ +all: $(c_targets) + +.PHONY: all + +#/ +# Compiles C source files. +# +# @private +# @param {string} CC - C compiler (e.g., `gcc`) +# @param {string} CFLAGS - C compiler options +# @param {(string|void)} fPIC - compiler flag determining whether to generate position independent code (e.g., `-fPIC`) +# @param {string} INCLUDE - list of includes (e.g., `-I /foo/bar`) +# @param {string} SOURCE_FILES - list of source files +# @param {string} LIBPATH - list of library paths (e.g., `-L /foo/bar`) +# @param {string} LIBRARIES - list of libraries (e.g., `-lopenblas`) +#/ +$(c_targets): %.out: %.c + $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) + +#/ +# Runs compiled examples. +# +# @example +# make run +#/ +run: $(c_targets) + $(QUIET) ./$< + +.PHONY: run + +#/ +# Removes generated files. +# +# @example +# make clean +#/ +clean: + $(QUIET) -rm -f *.o *.out + +.PHONY: clean diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/examples/c/example.c b/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/examples/c/example.c new file mode 100644 index 000000000000..8bd55a02693b --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/examples/c/example.c @@ -0,0 +1,92 @@ +/** +* @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. +*/ + +#include "stdlib/blas/ext/base/ndarray/swapx.h" +#include "stdlib/ndarray/ctor.h" +#include "stdlib/ndarray/dtypes.h" +#include "stdlib/ndarray/index_modes.h" +#include "stdlib/ndarray/orders.h" +#include "stdlib/ndarray/base/bytes_per_element.h" +#include +#include +#include + +int main( void ) { + // Create data buffers: + const float dataX[] = { -2.0f, 1.0f, 3.0f, -5.0f }; + float dataW[] = { 0.0f, 0.0f, 0.0f, 0.0f }; + + // Specify the number of array dimensions: + const int64_t ndims = 1; + + // Specify the array shape: + int64_t shape[] = { 4 }; + + // Specify the array strides: + int64_t strides[] = { STDLIB_NDARRAY_FLOAT32_BYTES_PER_ELEMENT }; + + // Specify the byte offset: + const int64_t offset = 0; + + // Specify the array order: + const enum STDLIB_NDARRAY_ORDER order = STDLIB_NDARRAY_ROW_MAJOR; + + // Specify the index mode: + const enum STDLIB_NDARRAY_INDEX_MODE imode = STDLIB_NDARRAY_INDEX_ERROR; + + // Specify the subscript index modes: + int8_t submodes[] = { STDLIB_NDARRAY_INDEX_ERROR }; + const int64_t nsubmodes = 1; + + // Create ndarrays: + // cppcheck-suppress invalidPointerCast + struct ndarray *x = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT32, (uint8_t *)dataX, ndims, shape, strides, offset, order, imode, nsubmodes, submodes ); + + // cppcheck-suppress invalidPointerCast + struct ndarray *w = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT32, (uint8_t *)dataW, ndims, shape, strides, offset, order, imode, nsubmodes, submodes ); + + // Create a data buffer for an ndarray containing the scalar constant to add: + const float adata[] = { 5.0f }; + + // Specify the array strides for a zero-dimensional ndarray: + int64_t astrides[] = { 0 }; + + // Create an ndarray containing the scalar constant to add: + // cppcheck-suppress invalidPointerCast + struct ndarray *alpha = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT32, (uint8_t *)adata, 0, NULL, astrides, 0, order, imode, nsubmodes, submodes ); + if ( x == NULL || w == NULL || alpha == NULL ) { + fprintf( stderr, "Error allocating memory.\n" ); + exit( 1 ); + } + + // Define a list of ndarrays: + const struct ndarray *arrays[] = { x, w, alpha }; + + // Perform computation: + stdlib_blas_ext_swapx( arrays ); + + // Print the result: + for ( int i = 0; i < 4; i++ ) { + printf( "w[ %i ] = %f\n", i, dataW[ i ] ); + } + + // Free allocated memory: + stdlib_ndarray_free( x ); + stdlib_ndarray_free( w ); + stdlib_ndarray_free( alpha ); +} diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/examples/index.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/examples/index.js new file mode 100644 index 000000000000..97b8f254b7be --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/examples/index.js @@ -0,0 +1,41 @@ +/** +* @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 discreteUniform = require( '@stdlib/random/discrete-uniform' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var ndarraylike2scalar = require( '@stdlib/ndarray/ndarraylike2scalar' ); +var swapx = require( './../lib' ); + +var opts = { + 'dtype': 'float32' +}; + +var x = discreteUniform( [ 10 ], -100, 100, opts ); +console.log( ndarray2array( x ) ); + +var w = discreteUniform( [ 10 ], -100, 100, opts ); +console.log( ndarray2array( w ) ); + +var alpha = scalar2ndarray( 5.0, opts ); +console.log( 'Alpha: %d', ndarraylike2scalar( alpha ) ); + +swapx( [ x, w, alpha ] ); +console.log( ndarray2array( w ) ); diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/include.gypi b/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/include.gypi new file mode 100644 index 000000000000..bee8d41a2caf --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/include.gypi @@ -0,0 +1,53 @@ +# @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. + +# A GYP include file for building a Node.js native add-on. +# +# Main documentation: +# +# [1]: https://gyp.gsrc.io/docs/InputFormatReference.md +# [2]: https://gyp.gsrc.io/docs/UserDocumentation.md +{ + # Define variables to be used throughout the configuration for all targets: + 'variables': { + # Source directory: + 'src_dir': './src', + + # Include directories: + 'include_dirs': [ + '[ 3.0, 6.0, 8.0, 0.0 ] +*/ + +// MODULES // + +var join = require( 'path' ).join; +var tryRequire = require( '@stdlib/utils/try-require' ); +var isError = require( '@stdlib/assert/is-error' ); +var main = require( './main.js' ); + + +// MAIN // + +var swapx; +var tmp = tryRequire( join( __dirname, './native.js' ) ); +if ( isError( tmp ) ) { + swapx = main; +} else { + swapx = tmp; +} + + +// EXPORTS // + +module.exports = swapx; diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/lib/main.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/lib/main.js new file mode 100644 index 000000000000..8ffe54c04451 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/lib/main.js @@ -0,0 +1,79 @@ +/** +* @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 numelDimension = require( '@stdlib/ndarray/base/numel-dimension' ); +var getStride = require( '@stdlib/ndarray/base/stride' ); +var getOffset = require( '@stdlib/ndarray/base/offset' ); +var getData = require( '@stdlib/ndarray/base/data-buffer' ); +var strided = require( '@stdlib/blas/ext/base/swapx' ).ndarray; +var ndarraylike2scalar = require( '@stdlib/ndarray/base/ndarraylike2scalar' ); + + +// MAIN // + +/** +* Adds a scalar constant to each element in an input one-dimensional single-precision floating-point ndarray and assigns the results to elements in a one-dimensional single-precision floating-point output ndarray. +* +* ## Notes +* +* - The function expects the following ndarrays: +* +* - a one-dimensional input ndarray. +* - a one-dimensional output ndarray. +* - a zero-dimensional ndarray containing the scalar constant to add. +* +* @param {ArrayLikeObject} arrays - array-like object containing ndarrays +* @returns {ndarray} output ndarray +* +* @example +* var Float32Vector = require( '@stdlib/ndarray/vector/float32' ); +* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +* +* var x = new Float32Vector( [ -2.0, 1.0, 3.0, -5.0 ] ); +* var w = new Float32Vector( [ 0.0, 0.0, 0.0, 0.0 ] ); +* +* var alpha = scalar2ndarray( 5.0, { +* 'dtype': 'float32' +* }); +* +* var out = swapx( [ x, w, alpha ] ); +* // returns [ 3.0, 6.0, 8.0, 0.0 ] +* +* var bool = ( out === w ); +* // returns true +*/ +function swapx( arrays ) { + var alpha; + var x; + var w; + + x = arrays[ 0 ]; + w = arrays[ 1 ]; + alpha = ndarraylike2scalar( arrays[ 2 ] ); + strided( numelDimension( x, 0 ), alpha, getData( x ), getStride( x, 0 ), getOffset( x ), getData( w ), getStride( w, 0 ), getOffset( w ) ); // eslint-disable-line max-len + return w; +} + + +// EXPORTS // + +module.exports = swapx; diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/lib/native.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/lib/native.js new file mode 100644 index 000000000000..31fc3d1079fa --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/lib/native.js @@ -0,0 +1,73 @@ +/** +* @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 serialize = require( '@stdlib/ndarray/base/serialize-meta-data' ); +var getData = require( '@stdlib/ndarray/base/data-buffer' ); +var addon = require( './../src/addon.node' ); + + +// MAIN // + +/** +* Adds a scalar constant to each element in an input one-dimensional single-precision floating-point ndarray and assigns the results to elements in a one-dimensional single-precision floating-point output ndarray. +* +* ## Notes +* +* - The function expects the following ndarrays: +* +* - a one-dimensional input ndarray. +* - a one-dimensional output ndarray. +* - a zero-dimensional ndarray containing the scalar constant to add. +* +* @private +* @param {ArrayLikeObject} arrays - array-like object containing ndarrays +* @returns {ndarray} output ndarray +* +* @example +* var Float32Vector = require( '@stdlib/ndarray/vector/float32' ); +* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +* +* var x = new Float32Vector( [ -2.0, 1.0, 3.0, -5.0 ] ); +* var w = new Float32Vector( [ 0.0, 0.0, 0.0, 0.0 ] ); +* +* var alpha = scalar2ndarray( 5.0, { +* 'dtype': 'float32' +* }); +* +* var out = swapx( [ x, w, alpha ] ); +* // returns [ 3.0, 6.0, 8.0, 0.0 ] +* +* var bool = ( out === w ); +* // returns true +*/ +function swapx( arrays ) { + var alpha = arrays[ 2 ]; + var x = arrays[ 0 ]; + var w = arrays[ 1 ]; + addon( getData( x ), serialize( x ), getData( w ), serialize( w ), getData( alpha ), serialize( alpha ) ); // eslint-disable-line max-len + return w; +} + + +// EXPORTS // + +module.exports = swapx; diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/manifest.json b/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/manifest.json new file mode 100644 index 000000000000..3e0fde06d44d --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/manifest.json @@ -0,0 +1,109 @@ +{ + "options": { + "task": "build", + "wasm": false + }, + "fields": [ + { + "field": "src", + "resolve": true, + "relative": true + }, + { + "field": "include", + "resolve": true, + "relative": true + }, + { + "field": "libraries", + "resolve": false, + "relative": false + }, + { + "field": "libpath", + "resolve": true, + "relative": false + } + ], + "confs": [ + { + "task": "build", + "wasm": false, + "src": [ + "./src/main.c" + ], + "include": [ + "./include" + ], + "libraries": [], + "libpath": [], + "dependencies": [ + "@stdlib/blas/base/shared", + "@stdlib/blas/ext/base/swapx", + "@stdlib/ndarray/ctor", + "@stdlib/ndarray/base/napi/addon-arguments", + "@stdlib/napi/export", + "@stdlib/napi/argv" + ] + }, + { + "task": "benchmark", + "wasm": false, + "src": [ + "./src/main.c" + ], + "include": [ + "./include" + ], + "libraries": [], + "libpath": [], + "dependencies": [ + "@stdlib/blas/base/shared", + "@stdlib/blas/ext/base/swapx", + "@stdlib/ndarray/ctor", + "@stdlib/ndarray/dtypes", + "@stdlib/ndarray/index-modes", + "@stdlib/ndarray/orders", + "@stdlib/ndarray/base/bytes-per-element" + ] + }, + { + "task": "examples", + "wasm": false, + "src": [ + "./src/main.c" + ], + "include": [ + "./include" + ], + "libraries": [], + "libpath": [], + "dependencies": [ + "@stdlib/blas/base/shared", + "@stdlib/blas/ext/base/swapx", + "@stdlib/ndarray/ctor", + "@stdlib/ndarray/dtypes", + "@stdlib/ndarray/index-modes", + "@stdlib/ndarray/orders", + "@stdlib/ndarray/base/bytes-per-element" + ] + }, + { + "task": "", + "wasm": true, + "src": [ + "./src/main.c" + ], + "include": [ + "./include" + ], + "libraries": [], + "libpath": [], + "dependencies": [ + "@stdlib/blas/base/shared", + "@stdlib/blas/ext/base/swapx", + "@stdlib/ndarray/ctor" + ] + } + ] +} diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/package.json b/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/package.json new file mode 100644 index 000000000000..41d2e8fc3e39 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/package.json @@ -0,0 +1,76 @@ +{ + "name": "@stdlib/blas/ext/base/ndarray/swapx", + "version": "0.0.0", + "description": "Add a scalar constant to each element in an input one-dimensional single-precision floating-point ndarray and assign the results to elements in a one-dimensional single-precision floating-point output ndarray.", + "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", + "gypfile": true, + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "include": "./include", + "lib": "./lib", + "src": "./src", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdmath", + "mathematics", + "math", + "blas", + "extended", + "ext", + "swapx", + "wapx", + "add", + "scalar", + "constant", + "strided", + "array", + "ndarray", + "float32", + "single", + "single-precision" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/src/Makefile b/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/src/Makefile new file mode 100644 index 000000000000..2caf905cedbe --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/src/Makefile @@ -0,0 +1,70 @@ +#/ +# @license Apache-2.0 +# +# Copyright (c) 2026 The Stdlib Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +#/ + +# VARIABLES # + +ifndef VERBOSE + QUIET := @ +else + QUIET := +endif + +# Determine the OS ([1][1], [2][2]). +# +# [1]: https://en.wikipedia.org/wiki/Uname#Examples +# [2]: http://stackoverflow.com/a/27776822/2225624 +OS ?= $(shell uname) +ifneq (, $(findstring MINGW,$(OS))) + OS := WINNT +else +ifneq (, $(findstring MSYS,$(OS))) + OS := WINNT +else +ifneq (, $(findstring CYGWIN,$(OS))) + OS := WINNT +else +ifneq (, $(findstring Windows_NT,$(OS))) + OS := WINNT +endif +endif +endif +endif + + +# RULES # + +#/ +# Removes generated files for building an add-on. +# +# @example +# make clean-addon +#/ +clean-addon: + $(QUIET) -rm -f *.o *.node + +.PHONY: clean-addon + +#/ +# Removes generated files. +# +# @example +# make clean +#/ +clean: clean-addon + +.PHONY: clean diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/src/addon.c b/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/src/addon.c new file mode 100644 index 000000000000..9825da39081c --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/src/addon.c @@ -0,0 +1,67 @@ +/** +* @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. +*/ + +#include "stdlib/blas/ext/base/ndarray/swapx.h" +#include "stdlib/ndarray/ctor.h" +#include "stdlib/ndarray/base/napi/addon_arguments.h" +#include "stdlib/napi/export.h" +#include "stdlib/napi/argv.h" +#include +#include + +/** +* Receives JavaScript callback invocation data. +* +* @param env environment under which the function is invoked +* @param info callback data +* @return Node-API value +*/ +static napi_value addon( napi_env env, napi_callback_info info ) { + STDLIB_NAPI_ARGV( env, info, argv, argc, 6 ); + + // Process provided arguments: + struct ndarray *arrays[ 3 ]; + napi_value err; + napi_status status = stdlib_ndarray_napi_addon_arguments( env, argv, 6, 3, arrays, &err ); + assert( status == napi_ok ); + if ( err != NULL ) { + status = napi_throw( env, err ); + assert( status == napi_ok ); + return NULL; + } + // Create a const-qualified view of the argument pointer list: + const struct ndarray *arr[ 3 ] = { + arrays[ 0 ], + arrays[ 1 ], + arrays[ 2 ] + }; + // Perform computation: + stdlib_blas_ext_swapx( arr ); + + // Free allocated memory: + stdlib_ndarray_free( arrays[ 0 ] ); + stdlib_ndarray_free( arrays[ 1 ] ); + stdlib_ndarray_free( arrays[ 2 ] ); + arrays[ 0 ] = NULL; + arrays[ 1 ] = NULL; + arrays[ 2 ] = NULL; + + return NULL; +} + +STDLIB_NAPI_MODULE_EXPORT_FCN( addon ) diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/src/main.c b/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/src/main.c new file mode 100644 index 000000000000..19b4cf4497e8 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/src/main.c @@ -0,0 +1,53 @@ +/** +* @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. +*/ + +#include "stdlib/blas/ext/base/ndarray/swapx.h" +#include "stdlib/blas/ext/base/swapx.h" +#include "stdlib/ndarray/ctor.h" +#include "stdlib/blas/base/shared.h" + +/** +* Adds a scalar constant to each element in an input one-dimensional single-precision floating-point ndarray and assigns the results to elements in a one-dimensional single-precision floating-point output ndarray. +* +* ## Notes +* +* - The function expects the following ndarrays: +* +* - a one-dimensional input ndarray. +* - a one-dimensional output ndarray. +* - a zero-dimensional ndarray containing the scalar constant to add. +* +* @param arrays list containing ndarrays +*/ +void stdlib_blas_ext_swapx( const struct ndarray *arrays[] ) { + const struct ndarray *x = arrays[ 0 ]; + const struct ndarray *w = arrays[ 1 ]; + + float alpha; + stdlib_ndarray_get_float32( arrays[ 2 ], NULL, &alpha ); + + const CBLAS_INT N = stdlib_ndarray_dimension( x, 0 ); + const CBLAS_INT strideX = stdlib_ndarray_stride_elements( x, 0 ); + const CBLAS_INT offsetX = stdlib_ndarray_offset_elements( x ); + const CBLAS_INT strideW = stdlib_ndarray_stride_elements( w, 0 ); + const CBLAS_INT offsetW = stdlib_ndarray_offset_elements( w ); + + const float *dataX = (const float *)stdlib_ndarray_data( x ); + float *dataW = (float *)stdlib_ndarray_data( w ); + API_SUFFIX(stdlib_strided_swapx_ndarray)( N, alpha, dataX, strideX, offsetX, dataW, strideW, offsetW ); +} diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/test/test.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/test/test.js new file mode 100644 index 000000000000..7822726a79bd --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/test/test.js @@ -0,0 +1,77 @@ +/** +* @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 proxyquire = require( 'proxyquire' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var swapx = require( './../lib' ); + + +// VARIABLES // + +var opts = { + 'skip': IS_BROWSER +}; + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof swapx, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'if a native implementation is available, the main export is the native implementation', opts, function test( t ) { + var swapx = proxyquire( './../lib', { + '@stdlib/utils/try-require': tryRequire + }); + + t.strictEqual( swapx, mock, 'returns expected value' ); + t.end(); + + function tryRequire() { + return mock; + } + + function mock() { + // Mock... + } +}); + +tape( 'if a native implementation is not available, the main export is a JavaScript implementation', opts, function test( t ) { + var swapx; + var main; + + main = require( './../lib/main.js' ); + + swapx = proxyquire( './../lib', { + '@stdlib/utils/try-require': tryRequire + }); + + t.strictEqual( swapx, main, 'returns expected value' ); + t.end(); + + function tryRequire() { + return new Error( 'Cannot find module' ); + } +}); diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/test/test.main.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/test/test.main.js new file mode 100644 index 000000000000..16451a4e2e87 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/test/test.main.js @@ -0,0 +1,439 @@ +/** +* @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 Float32Array = require( '@stdlib/array/float32' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var swapx = require( './../lib/main.js' ); + + +// FUNCTIONS // + +/** +* Returns a one-dimensional ndarray. +* +* @private +* @param {Float32Array} buffer - underlying data buffer +* @param {NonNegativeInteger} length - number of indexed elements +* @param {integer} stride - stride length +* @param {NonNegativeInteger} offset - index offset +* @returns {ndarray} one-dimensional ndarray +*/ +function vector( buffer, length, stride, offset ) { + return new ndarray( 'float32', buffer, [ length ], [ stride ], offset, 'row-major' ); +} + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof swapx, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 1', function test( t ) { + t.strictEqual( swapx.length, 1, 'has expected arity' ); + t.end(); +}); + +tape( 'the function adds a scalar constant to each element and assigns results to an output ndarray', function test( t ) { + var expected; + var actual; + var alpha; + var xbuf; + var wbuf; + var x; + var w; + + xbuf = new Float32Array([ + -2.0, // 0 + 1.0, // 1 + 3.0, // 2 + -6.0, // 3 + 4.0 // 4 + ]); + wbuf = new Float32Array([ + 0.0, // 0 + 0.0, // 1 + 0.0, // 2 + 0.0, // 3 + 0.0 // 4 + ]); + x = vector( xbuf, 5, 1, 0 ); + w = vector( wbuf, 5, 1, 0 ); + alpha = scalar2ndarray( 5.0, { + 'dtype': 'float32' + }); + + actual = swapx( [ x, w, alpha ] ); + t.strictEqual( actual, w, 'returns expected value' ); + + expected = new Float32Array([ + 3.0, // 5.0 + -2.0 + 6.0, // 5.0 + 1.0 + 8.0, // 5.0 + 3.0 + -1.0, // 5.0 + -6.0 + 9.0 // 5.0 + 4.0 + ]); + t.deepEqual( wbuf, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function copies the input ndarray to the output ndarray when alpha is zero', function test( t ) { + var expected; + var actual; + var alpha; + var xbuf; + var wbuf; + var x; + var w; + + xbuf = new Float32Array([ + -2.0, // 0 + 1.0, // 1 + 3.0, // 2 + -6.0, // 3 + 4.0 // 4 + ]); + wbuf = new Float32Array([ + 0.0, // 0 + 0.0, // 1 + 0.0, // 2 + 0.0, // 3 + 0.0 // 4 + ]); + x = vector( xbuf, 5, 1, 0 ); + w = vector( wbuf, 5, 1, 0 ); + alpha = scalar2ndarray( 0.0, { + 'dtype': 'float32' + }); + + actual = swapx( [ x, w, alpha ] ); + t.strictEqual( actual, w, 'returns expected value' ); + + expected = new Float32Array([ + -2.0, + 1.0, + 3.0, + -6.0, + 4.0 + ]); + t.deepEqual( wbuf, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports an input ndarray having a non-unit stride', function test( t ) { + var expected; + var actual; + var alpha; + var xbuf; + var wbuf; + var x; + var w; + + xbuf = new Float32Array([ + -2.0, // 0 + 1.0, + 3.0, // 1 + -6.0, + 4.0, // 2 + 0.0, + -1.0, // 3 + -3.0 + ]); + wbuf = new Float32Array([ + 0.0, // 0 + 0.0, // 1 + 0.0, // 2 + 0.0 // 3 + ]); + x = vector( xbuf, 4, 2, 0 ); + w = vector( wbuf, 4, 1, 0 ); + alpha = scalar2ndarray( 5.0, { + 'dtype': 'float32' + }); + + actual = swapx( [ x, w, alpha ] ); + t.strictEqual( actual, w, 'returns expected value' ); + + expected = new Float32Array([ + 3.0, // 5.0 + -2.0 + 8.0, // 5.0 + 3.0 + 9.0, // 5.0 + 4.0 + 4.0 // 5.0 + -1.0 + ]); + t.deepEqual( wbuf, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports an input ndarray having a negative stride', function test( t ) { + var expected; + var actual; + var alpha; + var xbuf; + var wbuf; + var x; + var w; + + xbuf = new Float32Array([ + -2.0, // 4 + 1.0, // 3 + 3.0, // 2 + -6.0, // 1 + 4.0 // 0 + ]); + wbuf = new Float32Array([ + 0.0, // 0 + 0.0, // 1 + 0.0, // 2 + 0.0, // 3 + 0.0 // 4 + ]); + x = vector( xbuf, 5, -1, 4 ); + w = vector( wbuf, 5, 1, 0 ); + alpha = scalar2ndarray( 5.0, { + 'dtype': 'float32' + }); + + actual = swapx( [ x, w, alpha ] ); + t.strictEqual( actual, w, 'returns expected value' ); + + expected = new Float32Array([ + 9.0, // 5.0 + 4.0 + -1.0, // 5.0 + -6.0 + 8.0, // 5.0 + 3.0 + 6.0, // 5.0 + 1.0 + 3.0 // 5.0 + -2.0 + ]); + t.deepEqual( wbuf, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports an output ndarray having a non-unit stride', function test( t ) { + var expected; + var actual; + var alpha; + var xbuf; + var wbuf; + var x; + var w; + + xbuf = new Float32Array([ + -2.0, // 0 + 1.0, // 1 + 3.0, // 2 + -6.0 // 3 + ]); + wbuf = new Float32Array([ + 0.0, // 0 + 0.0, + 0.0, // 1 + 0.0, + 0.0, // 2 + 0.0, + 0.0, // 3 + 0.0 + ]); + x = vector( xbuf, 4, 1, 0 ); + w = vector( wbuf, 4, 2, 0 ); + alpha = scalar2ndarray( 5.0, { + 'dtype': 'float32' + }); + + actual = swapx( [ x, w, alpha ] ); + t.strictEqual( actual, w, 'returns expected value' ); + + expected = new Float32Array([ + 3.0, // 5.0 + -2.0 + 0.0, + 6.0, // 5.0 + 1.0 + 0.0, + 8.0, // 5.0 + 3.0 + 0.0, + -1.0, // 5.0 + -6.0 + 0.0 + ]); + t.deepEqual( wbuf, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports an output ndarray having a negative stride', function test( t ) { + var expected; + var actual; + var alpha; + var xbuf; + var wbuf; + var x; + var w; + + xbuf = new Float32Array([ + -2.0, // 0 + 1.0, // 1 + 3.0, // 2 + -6.0, // 3 + 4.0 // 4 + ]); + wbuf = new Float32Array([ + 0.0, // 4 + 0.0, // 3 + 0.0, // 2 + 0.0, // 1 + 0.0 // 0 + ]); + x = vector( xbuf, 5, 1, 0 ); + w = vector( wbuf, 5, -1, 4 ); + alpha = scalar2ndarray( 5.0, { + 'dtype': 'float32' + }); + + actual = swapx( [ x, w, alpha ] ); + t.strictEqual( actual, w, 'returns expected value' ); + + expected = new Float32Array([ + 9.0, // 5.0 + 4.0 + -1.0, // 5.0 + -6.0 + 8.0, // 5.0 + 3.0 + 6.0, // 5.0 + 1.0 + 3.0 // 5.0 + -2.0 + ]); + t.deepEqual( wbuf, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports an input ndarray having a non-zero offset', function test( t ) { + var expected; + var actual; + var alpha; + var xbuf; + var wbuf; + var x; + var w; + + xbuf = new Float32Array([ + 1.0, + -2.0, + 3.0, // 0 + -6.0, // 1 + 4.0, // 2 + 0.0 + ]); + wbuf = new Float32Array([ + 0.0, // 0 + 0.0, // 1 + 0.0 // 2 + ]); + x = vector( xbuf, 3, 1, 2 ); + w = vector( wbuf, 3, 1, 0 ); + alpha = scalar2ndarray( 5.0, { + 'dtype': 'float32' + }); + + actual = swapx( [ x, w, alpha ] ); + t.strictEqual( actual, w, 'returns expected value' ); + + expected = new Float32Array([ + 8.0, // 5.0 + 3.0 + -1.0, // 5.0 + -6.0 + 9.0 // 5.0 + 4.0 + ]); + t.deepEqual( wbuf, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports an output ndarray having a non-zero offset', function test( t ) { + var expected; + var actual; + var alpha; + var xbuf; + var wbuf; + var x; + var w; + + xbuf = new Float32Array([ + -2.0, // 0 + 1.0, // 1 + 3.0 // 2 + ]); + wbuf = new Float32Array([ + 0.0, + 0.0, + 0.0, // 0 + 0.0, // 1 + 0.0, // 2 + 0.0 + ]); + x = vector( xbuf, 3, 1, 0 ); + w = vector( wbuf, 3, 1, 2 ); + alpha = scalar2ndarray( 5.0, { + 'dtype': 'float32' + }); + + actual = swapx( [ x, w, alpha ] ); + t.strictEqual( actual, w, 'returns expected value' ); + + expected = new Float32Array([ + 0.0, + 0.0, + 3.0, // 5.0 + -2.0 + 6.0, // 5.0 + 1.0 + 8.0, // 5.0 + 3.0 + 0.0 + ]); + t.deepEqual( wbuf, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns the output ndarray unchanged when the input ndarray is empty', function test( t ) { + var expected; + var actual; + var alpha; + var xbuf; + var wbuf; + var x; + var w; + + xbuf = new Float32Array( [] ); + wbuf = new Float32Array( [] ); + x = vector( xbuf, 0, 1, 0 ); + w = vector( wbuf, 0, 1, 0 ); + alpha = scalar2ndarray( 5.0, { + 'dtype': 'float32' + }); + + actual = swapx( [ x, w, alpha ] ); + t.strictEqual( actual, w, 'returns expected value' ); + + expected = new Float32Array( [] ); + t.deepEqual( wbuf, expected, 'returns expected value' ); + + t.end(); +}); diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/test/test.native.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/test/test.native.js new file mode 100644 index 000000000000..c595e1f5760c --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/swapx/test/test.native.js @@ -0,0 +1,448 @@ +/** +* @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 resolve = require( 'path' ).resolve; +var tape = require( 'tape' ); +var Float32Array = require( '@stdlib/array/float32' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var tryRequire = require( '@stdlib/utils/try-require' ); + + +// VARIABLES // + +var swapx = tryRequire( resolve( __dirname, './../lib/native.js' ) ); +var opts = { + 'skip': ( swapx instanceof Error ) +}; + + +// FUNCTIONS // + +/** +* Returns a one-dimensional ndarray. +* +* @private +* @param {Float32Array} buffer - underlying data buffer +* @param {NonNegativeInteger} length - number of indexed elements +* @param {integer} stride - stride length +* @param {NonNegativeInteger} offset - index offset +* @returns {ndarray} one-dimensional ndarray +*/ +function vector( buffer, length, stride, offset ) { + return new ndarray( 'float32', buffer, [ length ], [ stride ], offset, 'row-major' ); +} + + +// TESTS // + +tape( 'main export is a function', opts, function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof swapx, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 1', opts, function test( t ) { + t.strictEqual( swapx.length, 1, 'has expected arity' ); + t.end(); +}); + +tape( 'the function adds a scalar constant to each element and assigns results to an output ndarray', opts, function test( t ) { + var expected; + var actual; + var alpha; + var xbuf; + var wbuf; + var x; + var w; + + xbuf = new Float32Array([ + -2.0, // 0 + 1.0, // 1 + 3.0, // 2 + -6.0, // 3 + 4.0 // 4 + ]); + wbuf = new Float32Array([ + 0.0, // 0 + 0.0, // 1 + 0.0, // 2 + 0.0, // 3 + 0.0 // 4 + ]); + x = vector( xbuf, 5, 1, 0 ); + w = vector( wbuf, 5, 1, 0 ); + alpha = scalar2ndarray( 5.0, { + 'dtype': 'float32' + }); + + actual = swapx( [ x, w, alpha ] ); + t.strictEqual( actual, w, 'returns expected value' ); + + expected = new Float32Array([ + 3.0, // 5.0 + -2.0 + 6.0, // 5.0 + 1.0 + 8.0, // 5.0 + 3.0 + -1.0, // 5.0 + -6.0 + 9.0 // 5.0 + 4.0 + ]); + t.deepEqual( wbuf, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function copies the input ndarray to the output ndarray when alpha is zero', opts, function test( t ) { + var expected; + var actual; + var alpha; + var xbuf; + var wbuf; + var x; + var w; + + xbuf = new Float32Array([ + -2.0, // 0 + 1.0, // 1 + 3.0, // 2 + -6.0, // 3 + 4.0 // 4 + ]); + wbuf = new Float32Array([ + 0.0, // 0 + 0.0, // 1 + 0.0, // 2 + 0.0, // 3 + 0.0 // 4 + ]); + x = vector( xbuf, 5, 1, 0 ); + w = vector( wbuf, 5, 1, 0 ); + alpha = scalar2ndarray( 0.0, { + 'dtype': 'float32' + }); + + actual = swapx( [ x, w, alpha ] ); + t.strictEqual( actual, w, 'returns expected value' ); + + expected = new Float32Array([ + -2.0, + 1.0, + 3.0, + -6.0, + 4.0 + ]); + t.deepEqual( wbuf, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports an input ndarray having a non-unit stride', opts, function test( t ) { + var expected; + var actual; + var alpha; + var xbuf; + var wbuf; + var x; + var w; + + xbuf = new Float32Array([ + -2.0, // 0 + 1.0, + 3.0, // 1 + -6.0, + 4.0, // 2 + 0.0, + -1.0, // 3 + -3.0 + ]); + wbuf = new Float32Array([ + 0.0, // 0 + 0.0, // 1 + 0.0, // 2 + 0.0 // 3 + ]); + x = vector( xbuf, 4, 2, 0 ); + w = vector( wbuf, 4, 1, 0 ); + alpha = scalar2ndarray( 5.0, { + 'dtype': 'float32' + }); + + actual = swapx( [ x, w, alpha ] ); + t.strictEqual( actual, w, 'returns expected value' ); + + expected = new Float32Array([ + 3.0, // 5.0 + -2.0 + 8.0, // 5.0 + 3.0 + 9.0, // 5.0 + 4.0 + 4.0 // 5.0 + -1.0 + ]); + t.deepEqual( wbuf, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports an input ndarray having a negative stride', opts, function test( t ) { + var expected; + var actual; + var alpha; + var xbuf; + var wbuf; + var x; + var w; + + xbuf = new Float32Array([ + -2.0, // 4 + 1.0, // 3 + 3.0, // 2 + -6.0, // 1 + 4.0 // 0 + ]); + wbuf = new Float32Array([ + 0.0, // 0 + 0.0, // 1 + 0.0, // 2 + 0.0, // 3 + 0.0 // 4 + ]); + x = vector( xbuf, 5, -1, 4 ); + w = vector( wbuf, 5, 1, 0 ); + alpha = scalar2ndarray( 5.0, { + 'dtype': 'float32' + }); + + actual = swapx( [ x, w, alpha ] ); + t.strictEqual( actual, w, 'returns expected value' ); + + expected = new Float32Array([ + 9.0, // 5.0 + 4.0 + -1.0, // 5.0 + -6.0 + 8.0, // 5.0 + 3.0 + 6.0, // 5.0 + 1.0 + 3.0 // 5.0 + -2.0 + ]); + t.deepEqual( wbuf, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports an output ndarray having a non-unit stride', opts, function test( t ) { + var expected; + var actual; + var alpha; + var xbuf; + var wbuf; + var x; + var w; + + xbuf = new Float32Array([ + -2.0, // 0 + 1.0, // 1 + 3.0, // 2 + -6.0 // 3 + ]); + wbuf = new Float32Array([ + 0.0, // 0 + 0.0, + 0.0, // 1 + 0.0, + 0.0, // 2 + 0.0, + 0.0, // 3 + 0.0 + ]); + x = vector( xbuf, 4, 1, 0 ); + w = vector( wbuf, 4, 2, 0 ); + alpha = scalar2ndarray( 5.0, { + 'dtype': 'float32' + }); + + actual = swapx( [ x, w, alpha ] ); + t.strictEqual( actual, w, 'returns expected value' ); + + expected = new Float32Array([ + 3.0, // 5.0 + -2.0 + 0.0, + 6.0, // 5.0 + 1.0 + 0.0, + 8.0, // 5.0 + 3.0 + 0.0, + -1.0, // 5.0 + -6.0 + 0.0 + ]); + t.deepEqual( wbuf, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports an output ndarray having a negative stride', opts, function test( t ) { + var expected; + var actual; + var alpha; + var xbuf; + var wbuf; + var x; + var w; + + xbuf = new Float32Array([ + -2.0, // 0 + 1.0, // 1 + 3.0, // 2 + -6.0, // 3 + 4.0 // 4 + ]); + wbuf = new Float32Array([ + 0.0, // 4 + 0.0, // 3 + 0.0, // 2 + 0.0, // 1 + 0.0 // 0 + ]); + x = vector( xbuf, 5, 1, 0 ); + w = vector( wbuf, 5, -1, 4 ); + alpha = scalar2ndarray( 5.0, { + 'dtype': 'float32' + }); + + actual = swapx( [ x, w, alpha ] ); + t.strictEqual( actual, w, 'returns expected value' ); + + expected = new Float32Array([ + 9.0, // 5.0 + 4.0 + -1.0, // 5.0 + -6.0 + 8.0, // 5.0 + 3.0 + 6.0, // 5.0 + 1.0 + 3.0 // 5.0 + -2.0 + ]); + t.deepEqual( wbuf, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports an input ndarray having a non-zero offset', opts, function test( t ) { + var expected; + var actual; + var alpha; + var xbuf; + var wbuf; + var x; + var w; + + xbuf = new Float32Array([ + 1.0, + -2.0, + 3.0, // 0 + -6.0, // 1 + 4.0, // 2 + 0.0 + ]); + wbuf = new Float32Array([ + 0.0, // 0 + 0.0, // 1 + 0.0 // 2 + ]); + x = vector( xbuf, 3, 1, 2 ); + w = vector( wbuf, 3, 1, 0 ); + alpha = scalar2ndarray( 5.0, { + 'dtype': 'float32' + }); + + actual = swapx( [ x, w, alpha ] ); + t.strictEqual( actual, w, 'returns expected value' ); + + expected = new Float32Array([ + 8.0, // 5.0 + 3.0 + -1.0, // 5.0 + -6.0 + 9.0 // 5.0 + 4.0 + ]); + t.deepEqual( wbuf, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports an output ndarray having a non-zero offset', opts, function test( t ) { + var expected; + var actual; + var alpha; + var xbuf; + var wbuf; + var x; + var w; + + xbuf = new Float32Array([ + -2.0, // 0 + 1.0, // 1 + 3.0 // 2 + ]); + wbuf = new Float32Array([ + 0.0, + 0.0, + 0.0, // 0 + 0.0, // 1 + 0.0, // 2 + 0.0 + ]); + x = vector( xbuf, 3, 1, 0 ); + w = vector( wbuf, 3, 1, 2 ); + alpha = scalar2ndarray( 5.0, { + 'dtype': 'float32' + }); + + actual = swapx( [ x, w, alpha ] ); + t.strictEqual( actual, w, 'returns expected value' ); + + expected = new Float32Array([ + 0.0, + 0.0, + 3.0, // 5.0 + -2.0 + 6.0, // 5.0 + 1.0 + 8.0, // 5.0 + 3.0 + 0.0 + ]); + t.deepEqual( wbuf, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns the output ndarray unchanged when the input ndarray is empty', opts, function test( t ) { + var expected; + var actual; + var alpha; + var xbuf; + var wbuf; + var x; + var w; + + xbuf = new Float32Array( [] ); + wbuf = new Float32Array( [] ); + x = vector( xbuf, 0, 1, 0 ); + w = vector( wbuf, 0, 1, 0 ); + alpha = scalar2ndarray( 5.0, { + 'dtype': 'float32' + }); + + actual = swapx( [ x, w, alpha ] ); + t.strictEqual( actual, w, 'returns expected value' ); + + expected = new Float32Array( [] ); + t.deepEqual( wbuf, expected, 'returns expected value' ); + + t.end(); +});