From 0013b10d47e3388b12bf77c9ca4f2b150c24d1a4 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sat, 20 Apr 2024 01:14:21 +0000 Subject: [PATCH] Auto-generated commit --- base/srot/README.md | 204 +++++++ base/srot/benchmark/benchmark.js | 103 ++++ base/srot/benchmark/benchmark.native.js | 108 ++++ base/srot/benchmark/benchmark.ndarray.js | 103 ++++ .../benchmark/benchmark.ndarray.native.js | 108 ++++ base/srot/benchmark/c/Makefile | 146 +++++ base/srot/benchmark/c/benchmark.length.c | 154 ++++++ base/srot/benchmark/fortran/Makefile | 141 +++++ .../srot/benchmark/fortran/benchmark.length.f | 216 ++++++++ base/srot/binding.gyp | 265 ++++++++++ base/srot/docs/repl.txt | 135 +++++ base/srot/docs/types/index.d.ts | 113 ++++ base/srot/docs/types/test.ts | 309 +++++++++++ base/srot/examples/c/Makefile | 146 +++++ base/srot/examples/c/example.c | 45 ++ base/srot/examples/index.js | 35 ++ base/srot/include.gypi | 70 +++ base/srot/include/stdlib/blas/base/srot.h | 41 ++ .../include/stdlib/blas/base/srot_cblas.h | 41 ++ .../include/stdlib/blas/base/srot_fortran.h | 41 ++ base/srot/lib/index.js | 72 +++ base/srot/lib/main.js | 35 ++ base/srot/lib/native.js | 35 ++ base/srot/lib/ndarray.js | 72 +++ base/srot/lib/ndarray.native.js | 71 +++ base/srot/lib/srot.js | 87 +++ base/srot/lib/srot.native.js | 58 ++ base/srot/manifest.json | 385 ++++++++++++++ base/srot/package.json | 75 +++ base/srot/src/Makefile | 70 +++ base/srot/src/addon.c | 48 ++ base/srot/src/srot.c | 69 +++ base/srot/src/srot.f | 105 ++++ base/srot/src/srot_cblas.c | 35 ++ base/srot/src/srot_f.c | 35 ++ base/srot/test/test.js | 82 +++ base/srot/test/test.ndarray.js | 490 +++++++++++++++++ base/srot/test/test.ndarray.native.js | 499 ++++++++++++++++++ base/srot/test/test.srot.js | 401 ++++++++++++++ base/srot/test/test.srot.native.js | 410 ++++++++++++++ 40 files changed, 5658 insertions(+) create mode 100644 base/srot/README.md create mode 100644 base/srot/benchmark/benchmark.js create mode 100644 base/srot/benchmark/benchmark.native.js create mode 100644 base/srot/benchmark/benchmark.ndarray.js create mode 100644 base/srot/benchmark/benchmark.ndarray.native.js create mode 100644 base/srot/benchmark/c/Makefile create mode 100644 base/srot/benchmark/c/benchmark.length.c create mode 100644 base/srot/benchmark/fortran/Makefile create mode 100644 base/srot/benchmark/fortran/benchmark.length.f create mode 100644 base/srot/binding.gyp create mode 100644 base/srot/docs/repl.txt create mode 100644 base/srot/docs/types/index.d.ts create mode 100644 base/srot/docs/types/test.ts create mode 100644 base/srot/examples/c/Makefile create mode 100644 base/srot/examples/c/example.c create mode 100644 base/srot/examples/index.js create mode 100644 base/srot/include.gypi create mode 100644 base/srot/include/stdlib/blas/base/srot.h create mode 100644 base/srot/include/stdlib/blas/base/srot_cblas.h create mode 100644 base/srot/include/stdlib/blas/base/srot_fortran.h create mode 100644 base/srot/lib/index.js create mode 100644 base/srot/lib/main.js create mode 100644 base/srot/lib/native.js create mode 100644 base/srot/lib/ndarray.js create mode 100644 base/srot/lib/ndarray.native.js create mode 100644 base/srot/lib/srot.js create mode 100644 base/srot/lib/srot.native.js create mode 100644 base/srot/manifest.json create mode 100644 base/srot/package.json create mode 100644 base/srot/src/Makefile create mode 100644 base/srot/src/addon.c create mode 100644 base/srot/src/srot.c create mode 100644 base/srot/src/srot.f create mode 100644 base/srot/src/srot_cblas.c create mode 100644 base/srot/src/srot_f.c create mode 100644 base/srot/test/test.js create mode 100644 base/srot/test/test.ndarray.js create mode 100644 base/srot/test/test.ndarray.native.js create mode 100644 base/srot/test/test.srot.js create mode 100644 base/srot/test/test.srot.native.js diff --git a/base/srot/README.md b/base/srot/README.md new file mode 100644 index 000000000..86804b605 --- /dev/null +++ b/base/srot/README.md @@ -0,0 +1,204 @@ + + +# srot + +> Apply a plane rotation. + +
+ +This BLAS level 1 routine applies a real plane rotation to real single-precision floating-point vectors. The plane rotation is applied to `N` points, where the points to be rotated are contained in vectors `x` and `y` and where the cosine and sine of the angle of rotation are `c` and `s`, respectively. The operation is as follows: + + + + + +where `x_i` and `y_i` are the individual elements on which the rotation is applied. + +
+ + + +
+ +## Usage + +```javascript +var srot = require( '@stdlib/blas/base/srot' ); +``` + +#### srot( N, x, strideX, y, strideY, c, s ) + +Applies a plane rotation. + +```javascript +var Float32Array = require( '@stdlib/array/float32' ); + +var x = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); +var y = new Float32Array( [ 6.0, 7.0, 8.0, 9.0, 10.0 ] ); + +srot( x.length, x, 1, y, 1, 0.8, 0.6 ); +// x => [ ~4.4, ~5.8, ~7.2, ~8.6, 10.0 ] +// y => [ ~4.2, ~4.4, ~4.6, ~4.8, 5.0 ] +``` + +The function has the following parameters: + +- **N**: number of indexed elements. +- **x**: first input [`Float32Array`][mdn-float32array]. +- **strideX**: index increment for `x`. +- **y**: second input [`Float32Array`][mdn-float32array]. +- **strideY**: index increment for `y`. +- **c**: cosine of the angle of rotation. +- **s**: sine of the angle of rotation. + +The `N` and stride parameters determine how values in the strided arrays are accessed at runtime. For example, to apply a plane rotation to every other element, + +```javascript +var Float32Array = require( '@stdlib/array/float32' ); + +var x = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); +var y = new Float32Array( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); + +srot( 3, x, 2, y, 2, 0.8, 0.6 ); +// x => [ ~5.0, 2.0, ~7.8, 4.0, ~10.6, 6.0 ] +// y => [ 5.0, 8.0, ~5.4, 10.0, ~5.8, 12.0 ] +``` + +Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views. + + + +```javascript +var Float32Array = require( '@stdlib/array/float32' ); + +// Initial arrays... +var x0 = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); +var y0 = new Float32Array( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); + +// Create offset views... +var x1 = new Float32Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element +var y1 = new Float32Array( y0.buffer, y0.BYTES_PER_ELEMENT*3 ); // start at 4th element + +srot( 3, x1, 1, y1, 1, 0.8, 0.6 ); +// x0 => [ 1.0, ~7.6, 9.0, ~10.4, 5.0, 6.0 ] +// y0 => [ 7.0, 8.0, 9.0, ~6.8, 7.0, ~7.2 ] +``` + +#### srot.ndarray( N, x, strideX, offsetX, y, strideY, offsetY, c, s ) + +Applies a plane rotation using alternative indexing semantics. + +```javascript +var Float32Array = require( '@stdlib/array/float32' ); + +var x = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); +var y = new Float32Array( [ 6.0, 7.0, 8.0, 9.0, 10.0 ] ); + +srot.ndarray( 4, x, 1, 1, y, 1, 1, 0.8, 0.6 ); +// x => [ 1.0, ~5.8, ~7.2, ~8.6, 10.0 ] +// y => [ 6.0, ~4.4, ~4.6, ~4.8, 5.0 ] +``` + +The function has the following additional parameters: + +- **offsetX**: starting index for `x`. +- **offsetY**: starting index for `y`. + +While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameters support indexing semantics based on starting indices. For example, to apply a plane rotation to every other element starting from third element,..., + +```javascript +var Float32Array = require( '@stdlib/array/float32' ); + +var x = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); +var y = new Float32Array( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); + +srot.ndarray( 2, x, 2, 2, y, 2, 2, 0.8, 0.6 ); +// x => [ 1.0, 2.0, ~7.8, 4.0, ~10.6, 6.0 ] +// y => [ 7.0, 8.0, ~5.4, 10.0, ~5.8, 12.0 ] +``` + +
+ + + +
+ +## Notes + +- If `N <= 0`, both functions leave `x` and `y` unchanged. +- `srot()` corresponds to the [BLAS][blas] level 1 function [`srot`][srot]. + +
+ + + +
+ +## Examples + + + +```javascript +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var srot = require( '@stdlib/blas/base/srot' ); + +var opts = { + 'dtype': 'float32' +}; +var x = discreteUniform( 10, 0, 500, opts ); +console.log( x ); + +var y = discreteUniform( x.length, 0, 255, opts ); +console.log( y ); + +// Applies a plane rotation : +srot( x.length, x, 1, y, 1, 0.8, 0.6 ); +console.log( x ); +console.log( y ); +``` + +
+ + + + + + + + + + + + + + diff --git a/base/srot/benchmark/benchmark.js b/base/srot/benchmark/benchmark.js new file mode 100644 index 000000000..f7f7b87c6 --- /dev/null +++ b/base/srot/benchmark/benchmark.js @@ -0,0 +1,103 @@ +/** +* @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 uniform = require( '@stdlib/random/array/uniform' ); +var isnanf = require( '@stdlib/math/base/assert/is-nanf' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var pkg = require( './../package.json' ).name; +var srot = require( './../lib/srot.js' ); + + +// VARIABLES // + +var options = { + 'dtype': 'float32' +}; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var x = uniform( len, -100.0, 100.0, options ); + var y = uniform( len, -100.0, 100.0, options ); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var z; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + z = srot( x.length, x, 1, y, 1, 0.8, 0.6 ); + if ( isnanf( z[ i%x.length ] ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnanf( z[ i%x.length ] ) ) { + 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/base/srot/benchmark/benchmark.native.js b/base/srot/benchmark/benchmark.native.js new file mode 100644 index 000000000..a95ce1c22 --- /dev/null +++ b/base/srot/benchmark/benchmark.native.js @@ -0,0 +1,108 @@ +/** +* @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 resolve = require( 'path' ).resolve; +var bench = require( '@stdlib/bench' ); +var uniform = require( '@stdlib/random/array/uniform' ); +var isnanf = require( '@stdlib/math/base/assert/is-nanf' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var tryRequire = require( '@stdlib/utils/try-require' ); +var pkg = require( './../package.json' ).name; + + +// VARIABLES // + +var srot = tryRequire( resolve( __dirname, './../lib/native.js' ) ); +var opts = { + 'skip': ( srot instanceof Error ) +}; +var options = { + 'dtype': 'float32' +}; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var x = uniform( len, -100.0, 100.0, options ); + var y = uniform( len, -100.0, 100.0, options ); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var z; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + z = srot( x.length, x, 1, y, 1, 0.8, 0.6 ); + if ( isnanf( z[ i%x.length ] ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnanf( z[ i%x.length ] ) ) { + 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+'::native:len='+len, opts, f ); + } +} + +main(); diff --git a/base/srot/benchmark/benchmark.ndarray.js b/base/srot/benchmark/benchmark.ndarray.js new file mode 100644 index 000000000..b4f0e71e9 --- /dev/null +++ b/base/srot/benchmark/benchmark.ndarray.js @@ -0,0 +1,103 @@ +/** +* @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 uniform = require( '@stdlib/random/array/uniform' ); +var isnanf = require( '@stdlib/math/base/assert/is-nanf' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var pkg = require( './../package.json' ).name; +var srot = require( './../lib/ndarray.js' ); + + +// VARIABLES // + +var options = { + 'dtype': 'float32' +}; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var x = uniform( len, -100.0, 100.0, options ); + var y = uniform( len, -100.0, 100.0, options ); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var z; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + z = srot( x.length, x, 1, 0, y, 1, 0, 0.8, 0.6 ); + if ( isnanf( z[ i%x.length ] ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnanf( z[ i%x.length ] ) ) { + 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+':ndarray:len='+len, f ); + } +} + +main(); diff --git a/base/srot/benchmark/benchmark.ndarray.native.js b/base/srot/benchmark/benchmark.ndarray.native.js new file mode 100644 index 000000000..938ee6d36 --- /dev/null +++ b/base/srot/benchmark/benchmark.ndarray.native.js @@ -0,0 +1,108 @@ +/** +* @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 resolve = require( 'path' ).resolve; +var bench = require( '@stdlib/bench' ); +var uniform = require( '@stdlib/random/array/uniform' ); +var isnanf = require( '@stdlib/math/base/assert/is-nanf' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var tryRequire = require( '@stdlib/utils/try-require' ); +var pkg = require( './../package.json' ).name; + + +// VARIABLES // + +var srot = tryRequire( resolve( __dirname, './../lib/ndarray.native.js' ) ); +var opts = { + 'skip': ( srot instanceof Error ) +}; +var options = { + 'dtype': 'float32' +}; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var x = uniform( len, -100.0, 100.0, options ); + var y = uniform( len, -100.0, 100.0, options ); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var z; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + z = srot( x.length, x, 1, 0, y, 1, 0, 0.8, 0.6 ); + if ( isnanf( z[ i%x.length ] ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnanf( z[ i%x.length ] ) ) { + 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+'::native:ndarray:len='+len, opts, f ); + } +} + +main(); diff --git a/base/srot/benchmark/c/Makefile b/base/srot/benchmark/c/Makefile new file mode 100644 index 000000000..9f97140e7 --- /dev/null +++ b/base/srot/benchmark/c/Makefile @@ -0,0 +1,146 @@ +#/ +# @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. +#/ + +# 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/base/srot/benchmark/c/benchmark.length.c b/base/srot/benchmark/c/benchmark.length.c new file mode 100644 index 000000000..e5652eff8 --- /dev/null +++ b/base/srot/benchmark/c/benchmark.length.c @@ -0,0 +1,154 @@ +/** +* @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. +*/ + +/** +* Benchmark `srot`. +*/ +#include "stdlib/blas/base/srot.h" +#include +#include +#include +#include +#include + +#define NAME "srot" +#define ITERATIONS 10000000 +#define REPEATS 3 +#define MIN 1 +#define MAX 6 + +/** +* Prints the TAP version. +*/ +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 +*/ +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 +*/ +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 +*/ +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 [0,1]. +* +* @return random number +*/ +float rand_float( void ) { + int r = rand(); + return (float)r / ( (float)RAND_MAX + 1.0f ); +} + +/** +* Runs a benchmark. +* +* @param iterations number of iterations +* @param len array length +* @return elapsed time in seconds +*/ +double benchmark( int iterations, int len ) { + double elapsed; + float x[ len ]; + float y[ len ]; + double t; + int i; + + for ( i = 0; i < len; i++ ) { + x[ i ] = ( rand_float()*200.0f ) - 100.0f; + y[ i ] = ( rand_float()*200.0f ) - 100.0f; + } + t = tic(); + for ( i = 0; i < iterations; i++ ) { + c_srot( len, x, 1, y, 1, 0.8f, 0.6f ); + if ( y[ 0 ] != y[ 0 ] ) { + printf( "should not return NaN\n" ); + break; + } + } + elapsed = tic() - t; + if ( y[ 0 ] != y[ 0 ] ) { + printf( "should not return NaN\n" ); + } + 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/base/srot/benchmark/fortran/Makefile b/base/srot/benchmark/fortran/Makefile new file mode 100644 index 000000000..d9d9ff6b6 --- /dev/null +++ b/base/srot/benchmark/fortran/Makefile @@ -0,0 +1,141 @@ +#/ +# @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. +#/ + +# 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 Fortran source files: +ifdef FORTRAN_COMPILER + FC := $(FORTRAN_COMPILER) +else + FC := gfortran +endif + +# Define the command-line options when compiling Fortran files: +FFLAGS ?= \ + -std=f95 \ + -ffree-form \ + -O3 \ + -Wall \ + -Wextra \ + -Wno-compare-reals \ + -Wimplicit-interface \ + -fno-underscoring \ + -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 ?= + +# List of Fortran source files: +SOURCE_FILES ?= ../../src/srot.f + +# List of Fortran targets: +f_targets := benchmark.length.out + + +# RULES # + +#/ +# Compiles Fortran source files. +# +# @param {string} SOURCE_FILES - list of Fortran source files +# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop`) +# @param {string} [FORTRAN_COMPILER] - Fortran compiler +# @param {string} [FFLAGS] - Fortran compiler flags +# @param {(string|void)} [fPIC] - compiler flag indicating whether to generate position independent code +# +# @example +# make +# +# @example +# make all +#/ +all: $(f_targets) + +.PHONY: all + +#/ +# Compiles Fortran source files. +# +# @private +# @param {string} SOURCE_FILES - list of Fortran source files +# @param {(string|void)} INCLUDE - list of includes (e.g., `-I /foo/bar -I /beep/boop`) +# @param {string} FC - Fortran compiler +# @param {string} FFLAGS - Fortran compiler flags +# @param {(string|void)} fPIC - compiler flag indicating whether to generate position independent code +#/ +$(f_targets): %.out: %.f + $(QUIET) $(FC) $(FFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< + +#/ +# Runs compiled benchmarks. +# +# @example +# make run +#/ +run: $(f_targets) + $(QUIET) ./$< + +.PHONY: run + +#/ +# Removes generated files. +# +# @example +# make clean +#/ +clean: + $(QUIET) -rm -f *.o *.out + +.PHONY: clean diff --git a/base/srot/benchmark/fortran/benchmark.length.f b/base/srot/benchmark/fortran/benchmark.length.f new file mode 100644 index 000000000..2c94450ad --- /dev/null +++ b/base/srot/benchmark/fortran/benchmark.length.f @@ -0,0 +1,216 @@ +!> +! @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. +!< + +!> Benchmark `srot`. +! +! ## Notes +! +! - Written in "free form" Fortran 95. +! +!< +program bench + implicit none + ! .. + ! Local constants: + character(4), parameter :: name = 'srot' ! if changed, be sure to adjust length + integer, parameter :: iterations = 10000000 + integer, parameter :: repeats = 3 + integer, parameter :: min = 1 + integer, parameter :: max = 6 + ! .. + ! Run the benchmarks: + call main() + ! .. + ! Functions: +contains + ! .. + ! Prints the TAP version. + ! .. + subroutine print_version() + print '(A)', 'TAP version 13' + end subroutine print_version + ! .. + ! Prints the TAP summary. + ! + ! @param {integer} total - total number of tests + ! @param {integer} passing - total number of passing tests + ! .. + subroutine print_summary( total, passing ) + ! .. + ! Scalar arguments: + integer, intent(in) :: total, passing + ! .. + ! Local variables: + character(len=999) :: str, tmp + ! .. + ! Intrinsic functions: + intrinsic adjustl, trim + ! .. + print '(A)', '#' + ! .. + write (str, '(I15)') total ! TAP plan + tmp = adjustl( str ) + print '(A,A)', '1..', trim( tmp ) + ! .. + print '(A,A)', '# total ', trim( tmp ) + ! .. + write (str, '(I15)') passing + tmp = adjustl( str ) + print '(A,A)', '# pass ', trim( tmp ) + ! .. + print '(A)', '#' + print '(A)', '# ok' + end subroutine print_summary + ! .. + ! Prints benchmarks results. + ! + ! @param {integer} iterations - number of iterations + ! @param {double} elapsed - elapsed time in seconds + ! .. + subroutine print_results( iterations, elapsed ) + ! .. + ! Scalar arguments: + double precision, intent(in) :: elapsed + integer, intent(in) :: iterations + ! .. + ! Local variables: + double precision :: rate + character(len=999) :: str, tmp + ! .. + ! Intrinsic functions: + intrinsic dble, adjustl, trim + ! .. + rate = dble( iterations ) / elapsed + ! .. + print '(A)', ' ---' + ! .. + write (str, '(I15)') iterations + tmp = adjustl( str ) + print '(A,A)', ' iterations: ', trim( tmp ) + ! .. + write (str, '(f0.9)') elapsed + tmp = adjustl( str ) + print '(A,A)', ' elapsed: ', trim( tmp ) + ! .. + write( str, '(f0.9)') rate + tmp = adjustl( str ) + print '(A,A)', ' rate: ', trim( tmp ) + ! .. + print '(A)', ' ...' + end subroutine print_results + ! .. + ! Runs a benchmark. + ! + ! @param {integer} iterations - number of iterations + ! @param {integer} len - array length + ! @return {double} elapsed time in seconds + ! .. + double precision function benchmark( iterations, len ) + ! .. + ! External functions: + interface + subroutine srot( N, sx, strideX, sy, strideY, c, s ) + real :: sx(*), sy(*), c, s + integer :: strideX, strideY, N + end subroutine srot + end interface + ! .. + ! Scalar arguments: + integer, intent(in) :: iterations, len + ! .. + ! Local scalars: + double precision :: elapsed, r + real :: t1, t2 + integer :: i + ! .. + ! Local arrays: + real, allocatable :: x(:), y(:) + ! .. + ! Intrinsic functions: + intrinsic random_number, cpu_time + ! .. + ! Allocate arrays: + allocate( x(len), y(len) ) + ! .. + do i = 1, len + call random_number( r ) + x( i ) = ( real(r)*200.0 ) - 100.0 + y( i ) = ( real(r)*200.0 ) - 100.0 + end do + ! .. + call cpu_time( t1 ) + ! .. + do i = 1, iterations + call srot( len, x, 1, y, 1, 0.8, 0.6 ); + if ( y( 1 ) /= y( 1 ) ) then + print '(A)', 'should not return NaN' + exit + end if + end do + ! .. + call cpu_time( t2 ) + ! .. + elapsed = t2 - t1 + ! .. + if ( y( 1 ) /= y( 1 ) ) then + print '(A)', 'should not return NaN' + end if + ! .. + ! Deallocate arrays: + deallocate( x, y ) + ! .. + benchmark = elapsed + return + end function benchmark + ! .. + ! Main execution sequence. + ! .. + subroutine main() + ! .. + ! Local variables: + integer :: count, iter, len, i, j + double precision :: elapsed + character(len=999) :: str, tmp + ! .. + ! Intrinsic functions: + intrinsic adjustl, trim + ! .. + call print_version() + count = 0 + do i = min, max + len = 10**i + iter = iterations / 10**(i-1) + do j = 1, repeats + count = count + 1 + ! .. + write (str, '(I15)') len + tmp = adjustl( str ) + print '(A,A,A,A)', '# fortran::', name, ':len=', trim( tmp ) + ! .. + elapsed = benchmark( iter, len ) + ! .. + call print_results( iter, elapsed ) + ! .. + write (str, '(I15)') count + tmp = adjustl( str ) + print '(A,A,A)', 'ok ', trim( tmp ), ' benchmark finished' + end do + end do + call print_summary( count, count ) + end subroutine main +end program bench \ No newline at end of file diff --git a/base/srot/binding.gyp b/base/srot/binding.gyp new file mode 100644 index 000000000..02a2799da --- /dev/null +++ b/base/srot/binding.gyp @@ -0,0 +1,265 @@ +# @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. + +# 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/base/srot/docs/repl.txt b/base/srot/docs/repl.txt new file mode 100644 index 000000000..d8ff34aae --- /dev/null +++ b/base/srot/docs/repl.txt @@ -0,0 +1,135 @@ + +{{alias}}( N, x, strideX, y, strideY, c, s ) + Applies a plane rotation. + + The `N` and stride parameters determine how values in the strided arrays are + accessed at runtime. + + Indexing is relative to the first index. To introduce an offset, use typed + array views. + + If `N` is less than or equal to `0`, the vectors are unchanged. + + Parameters + ---------- + N: integer + Number of indexed elements. + + x: Float32Array + First input array. + + strideX: integer + Index increment for `x`. + + y: Float32Array + Second input array. + + strideY: integer + Index increment for `y`. + + c: number + Cosine of the angle of rotation. + + s: number + Sine of the angle of rotation. + + Returns + ------- + y: Float32Array + Second input array. + + Examples + -------- + // Standard Usage: + > var x = new {{alias:@stdlib/array/float32}}( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); + > var y = new {{alias:@stdlib/array/float32}}( [ 6.0, 7.0, 8.0, 9.0, 10.0 ] ); + > {{alias}}( x.length, x, 1, y, 1, 0.8, 0.6 ); + > x + [ ~4.4, ~5.8, ~7.2, ~8.6, 10.0 ] + > y + [ ~4.2, ~4.4, ~4.6, ~4.8, 5.0 ] + + // Advanced Indexing: + > x = new {{alias:@stdlib/array/float32}}( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + > y = new {{alias:@stdlib/array/float32}}( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); + > {{alias}}( 3, x, 2, y, 2, 0.8, 0.6 ); + > x + [ 5.0, 2.0, ~7.8, 4.0, ~10.6, 6.0 ] + > y + [ 5.0, 8.0, ~5.4, 10.0, ~5.8, 12.0 ] + + // Using typed array views: + > var x0 = new {{alias:@stdlib/array/float64}}( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + > var y0 = new {{alias:@stdlib/array/float64}}( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); + > var x1 = new {{alias:@stdlib/array/float64}}( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); + > var y1 = new {{alias:@stdlib/array/float64}}( y0.buffer, y0.BYTES_PER_ELEMENT*3 ); + > {{alias}}( 3, x1, 1, y1, 1, 0.8, 0.6 ); + > x0 + [ 1.0, ~7.6, 9.0, ~10.4, 5.0, 6.0 ] + > y0 + [ 7.0, 8.0, 9.0, ~6.8, 7.0, ~7.2 ] + + +{{alias}}.ndarray( N, x, strideX, offsetX, y, strideY, offsetY, c, s ) + Applies a plane rotation using alternative indexing semantics. + + While typed array views mandate a view offset based on the underlying + buffer, the offset parameters support indexing semantics based on starting + indices. + + Parameters + ---------- + N: integer + Number of indexed elements. + + x: Float32Array + First input array. + + strideX: integer + Index increment for `x`. + + offsetX: integer + Starting index for `x`. + + y: Float32Array + Second input array. + + strideY: integer + Index increment for `y`. + + offsetY: integer + Starting index for `y`. + + c: number + Cosine of the angle of rotation. + + s: number + Sine of the angle of rotation. + + Returns + ------- + y: Float32Array + Second input array. + + Examples + -------- + // Standard Usage: + > var x = new {{alias:@stdlib/array/float32}}( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); + > var y = new {{alias:@stdlib/array/float32}}( [ 6.0, 7.0, 8.0, 9.0, 10.0 ] ); + > {{alias}}.ndarray( 4, x, 1, 1, y, 1, 1, 0.8, 0.6 ); + > x + [ 1.0, ~5.8, ~7.2, ~8.6, 10.0 ] + > y + [ 6.0, ~4.4, ~4.6, ~4.8, 5.0 ] + + // Advanced Indexing: + > x = new {{alias:@stdlib/array/float32}}( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + > y = new {{alias:@stdlib/array/float32}}( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); + > {{alias}}.ndarray( 2, x, 2, 2, y, 2, 2, 0.8, 0.6 ); + > x + [ 1.0, 2.0, ~7.8, 4.0, ~10.6, 6.0 ] + > y + [ 7.0, 8.0, ~5.4, 10.0, ~5.8, 12.0 ] + + See Also + -------- diff --git a/base/srot/docs/types/index.d.ts b/base/srot/docs/types/index.d.ts new file mode 100644 index 000000000..221f6abd4 --- /dev/null +++ b/base/srot/docs/types/index.d.ts @@ -0,0 +1,113 @@ +/* +* @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 + +/** +* Interface describing `srot`. +*/ +interface Routine { + /** + * Applies a plane rotation. + * + * @param N - number of indexed elements + * @param x - first input array + * @param strideX - `x` stride length + * @param y - second input array + * @param strideY - `y` stride length + * @param c - cosine of the angle of rotation + * @param s - sine of the angle of rotation + * @returns `y` + * + * @example + * var Float32Array = require( '@stdlib/array/float32' ); + * + * var x = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); + * var y = new Float32Array( [ 6.0, 7.0, 8.0, 9.0, 10.0 ] ); + * + * srot( x.length, x, 1, y, 1, 0.8, 0.6 ); + * // x => [ ~4.4, ~5.8, ~7.2, ~8.6, 10.0 ] + * // y => [ ~4.2, ~4.4, ~4.6, ~4.8, 5.0 ] + */ + ( N: number, x: Float32Array, strideX: number, y: Float32Array, strideY: number, c: number, s: number ): Float32Array; + + /** + * Applies a plane rotation using alternative indexing semantics. + * + * @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` + * @param c - cosine of the angle of rotation + * @param s - sine of the angle of rotation + * @returns `y` + * + * @example + * var Float32Array = require( '@stdlib/array/float32' ); + * + * var x = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + * var y = new Float32Array( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); + * + * srot.ndarray( 3, x, 2, 1, y, 2, 1, 0.8, 0.6 ); + * // x => [ 1.0, 6.4, 3.0, 9.2, 5.0, 12.0 ] + * // y => [ 7.0, 5.2, 9.0, 5.6, 11.0, ~6.0 ] + */ + ndarray( N: number, x: Float32Array, strideX: number, offsetX: number, y: Float32Array, strideY: number, offsetY: number, c: number, s: number ): Float32Array; +} + +/** +* Applies a plane rotation. +* +* @param N - number of indexed elements +* @param x - first input array +* @param strideX - `x` stride length +* @param y - second input array +* @param strideY - `y` stride length +* @param c - cosine of the angle of rotation +* @param s - sine of the angle of rotation +* @returns `y` +* +* @example +* var Float32Array = require( '@stdlib/array/float32' ); +* +* var x = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); +* var y = new Float32Array( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); +* +* srot( 3, x, 2, y, 2, 0.8, 0.6 ); +* // x => [ 5.0, 2.0, ~7.8, 4.0, ~10.6, 6.0 ] +* // y => [ 5.0, 8.0, ~5.4, 10.0, ~5.8, 12.0 ] +* +* @example +* var Float32Array = require( '@stdlib/array/float32' ); +* +* var x = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); +* var y = new Float32Array( [ 6.0, 7.0, 8.0, 9.0, 10.0 ] ); +* +* srot.ndarray( 4, x, 1, 1, y, 1, 1, 0.8, 0.6 ); +* // x => [ 1.0, ~5.8, ~7.2, ~8.6, 10.0 ] +* // y => [ 6.0, ~4.4, ~4.6, ~4.8, 5.0 ] +*/ +declare var srot: Routine; + + +// EXPORTS // + +export = srot; diff --git a/base/srot/docs/types/test.ts b/base/srot/docs/types/test.ts new file mode 100644 index 000000000..0a292af24 --- /dev/null +++ b/base/srot/docs/types/test.ts @@ -0,0 +1,309 @@ +/* +* @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. +*/ + +import srot = require( './index' ); + + +// TESTS // + +// The function returns a Float32Array... +{ + const x = new Float32Array( 10 ); + const y = new Float32Array( 10 ); + + srot( x.length, x, 1, y, 1, 1.0, 0.0 ); // $ExpectType Float32Array +} + +// The compiler throws an error if the function is provided a first argument which is not a number... +{ + const x = new Float32Array( 10 ); + const y = new Float32Array( 10 ); + + srot( '10', x, 1, y, 1, 1.0, 0.0 ); // $ExpectError + srot( true, x, 1, y, 1, 1.0, 0.0 ); // $ExpectError + srot( false, x, 1, y, 1, 1.0, 0.0 ); // $ExpectError + srot( null, x, 1, y, 1, 1.0, 0.0 ); // $ExpectError + srot( undefined, x, 1, y, 1, 1.0, 0.0 ); // $ExpectError + srot( [], x, 1, y, 1, 1.0, 0.0 ); // $ExpectError + srot( {}, x, 1, y, 1, 1.0, 0.0 ); // $ExpectError + srot( ( x: number ): number => x, x, 1, y, 1, 1.0, 0.0 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not a Float32Array... +{ + const x = new Float32Array( 10 ); + const y = new Float32Array( 10 ); + + srot( x.length, 10, 1, y, 1, 1.0, 0.0 ); // $ExpectError + srot( x.length, '10', 1, y, 1, 1.0, 0.0 ); // $ExpectError + srot( x.length, true, 1, y, 1, 1.0, 0.0 ); // $ExpectError + srot( x.length, false, 1, y, 1, 1.0, 0.0 ); // $ExpectError + srot( x.length, null, 1, y, 1, 1.0, 0.0 ); // $ExpectError + srot( x.length, undefined, 1, y, 1, 1.0, 0.0 ); // $ExpectError + srot( x.length, [], 1, y, 1, 1.0, 0.0 ); // $ExpectError + srot( x.length, {}, 1, y, 1, 1.0, 0.0 ); // $ExpectError + srot( x.length, ( x: number ): number => x, 1, y, 1, 1.0, 0.0 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a third argument which is not a number... +{ + const x = new Float32Array( 10 ); + const y = new Float32Array( 10 ); + + srot( x.length, x, '10', y, 1, 1.0, 0.0 ); // $ExpectError + srot( x.length, x, true, y, 1, 1.0, 0.0 ); // $ExpectError + srot( x.length, x, false, y, 1, 1.0, 0.0 ); // $ExpectError + srot( x.length, x, null, y, 1, 1.0, 0.0 ); // $ExpectError + srot( x.length, x, undefined, y, 1, 1.0, 0.0 ); // $ExpectError + srot( x.length, x, [], y, 1, 1.0, 0.0 ); // $ExpectError + srot( x.length, x, {}, y, 1, 1.0, 0.0 ); // $ExpectError + srot( x.length, x, ( x: number ): number => x, y, 1, 1.0, 0.0 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a fourth argument which is not a Float32Array... +{ + const x = new Float32Array( 10 ); + + srot( x.length, x, 1, 10, 1, 1.0, 0.0 ); // $ExpectError + srot( x.length, x, 1, '10', 1, 1.0, 0.0 ); // $ExpectError + srot( x.length, x, 1, true, 1, 1.0, 0.0 ); // $ExpectError + srot( x.length, x, 1, false, 1, 1.0, 0.0 ); // $ExpectError + srot( x.length, x, 1, null, 1, 1.0, 0.0 ); // $ExpectError + srot( x.length, x, 1, undefined, 1, 1.0, 0.0 ); // $ExpectError + srot( x.length, x, 1, [], 1, 1.0, 0.0 ); // $ExpectError + srot( x.length, x, 1, {}, 1, 1.0, 0.0 ); // $ExpectError + srot( x.length, x, 1, ( x: number ): number => x, 1, 1.0, 0.0 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a fifth argument which is not a number... +{ + const x = new Float32Array( 10 ); + const y = new Float32Array( 10 ); + + srot( x.length, x, 1, y, '10', 1.0, 0.0 ); // $ExpectError + srot( x.length, x, 1, y, true, 1.0, 0.0 ); // $ExpectError + srot( x.length, x, 1, y, false, 1.0, 0.0 ); // $ExpectError + srot( x.length, x, 1, y, null, 1.0, 0.0 ); // $ExpectError + srot( x.length, x, 1, y, undefined, 1.0, 0.0 ); // $ExpectError + srot( x.length, x, 1, y, [], 1.0, 0.0 ); // $ExpectError + srot( x.length, x, 1, y, {}, 1.0, 0.0 ); // $ExpectError + srot( x.length, x, 1, y, ( x: number ): number => x, 1.0, 0.0 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a sixth argument which is not a number... +{ + const x = new Float32Array( 10 ); + const y = new Float32Array( 10 ); + + srot( x.length, x, 1, y, 1, '10', 0.0 ); // $ExpectError + srot( x.length, x, 1, y, 1, true, 0.0 ); // $ExpectError + srot( x.length, x, 1, y, 1, false, 0.0 ); // $ExpectError + srot( x.length, x, 1, y, 1, null, 0.0 ); // $ExpectError + srot( x.length, x, 1, y, 1, undefined, 0.0 ); // $ExpectError + srot( x.length, x, 1, y, 1, [], 0.0 ); // $ExpectError + srot( x.length, x, 1, y, 1, {}, 0.0 ); // $ExpectError + srot( x.length, x, 1, y, 1, ( x: number ): number => x, 0.0 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a seventh argument which is not a number... +{ + const x = new Float32Array( 10 ); + const y = new Float32Array( 10 ); + + srot( x.length, x, 1, y, 1, 1.0, '10' ); // $ExpectError + srot( x.length, x, 1, y, 1, 1.0, true ); // $ExpectError + srot( x.length, x, 1, y, 1, 1.0, false ); // $ExpectError + srot( x.length, x, 1, y, 1, 1.0, null ); // $ExpectError + srot( x.length, x, 1, y, 1, 1.0, undefined ); // $ExpectError + srot( x.length, x, 1, y, 1, 1.0, [] ); // $ExpectError + srot( x.length, x, 1, y, 1, 1.0, {} ); // $ExpectError + srot( x.length, x, 1, y, 1, 1.0, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = new Float32Array( 10 ); + const y = new Float32Array( 10 ); + + srot(); // $ExpectError + srot( x.length ); // $ExpectError + srot( x.length, x ); // $ExpectError + srot( x.length, x, 1 ); // $ExpectError + srot( x.length, x, 1, y ); // $ExpectError + srot( x.length, x, 1, y, 1, 1.0 ); // $ExpectError + srot( x.length, x, 1, y, 1, 1.0, 0.0, 10 ); // $ExpectError +} + +// Attached to main export is an `ndarray` method which returns a Float32Array... +{ + const x = new Float32Array( 10 ); + const y = new Float32Array( 10 ); + + srot.ndarray( x.length, x, 1, 0, y, 1, 0, 1.0, 0.0 ); // $ExpectType Float32Array +} + +// The compiler throws an error if the `ndarray` method is provided a first argument which is not a number... +{ + const x = new Float32Array( 10 ); + const y = new Float32Array( 10 ); + + srot.ndarray( '10', x, 1, 0, y, 1, 0, 1.0, 0.0 ); // $ExpectError + srot.ndarray( true, x, 1, 0, y, 1, 0, 1.0, 0.0 ); // $ExpectError + srot.ndarray( false, x, 1, 0, y, 1, 0, 1.0, 0.0 ); // $ExpectError + srot.ndarray( null, x, 1, 0, y, 1, 0, 1.0, 0.0 ); // $ExpectError + srot.ndarray( undefined, x, 1, 0, y, 1, 0, 1.0, 0.0 ); // $ExpectError + srot.ndarray( [], x, 1, 0, y, 1, 0, 1.0, 0.0 ); // $ExpectError + srot.ndarray( {}, x, 1, 0, y, 1, 0, 1.0, 0.0 ); // $ExpectError + srot.ndarray( ( x: number ): number => x, x, 1, 0, y, 1, 0, 1.0, 0.0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a second argument which is not a Float32Array... +{ + const x = new Float32Array( 10 ); + const y = new Float32Array( 10 ); + + srot.ndarray( x.length, 10, 1, 0, y, 1, 0, 1.0, 0.0 ); // $ExpectError + srot.ndarray( x.length, '10', 1, 0, y, 1, 0, 1.0, 0.0 ); // $ExpectError + srot.ndarray( x.length, true, 1, 0, y, 1, 0, 1.0, 0.0 ); // $ExpectError + srot.ndarray( x.length, false, 1, 0, y, 1, 0, 1.0, 0.0 ); // $ExpectError + srot.ndarray( x.length, null, 1, 0, y, 1, 0, 1.0, 0.0 ); // $ExpectError + srot.ndarray( x.length, undefined, 1, 0, y, 1, 0, 1.0, 0.0 ); // $ExpectError + srot.ndarray( x.length, [], 1, 0, y, 1, 0, 1.0, 0.0 ); // $ExpectError + srot.ndarray( x.length, {}, 1, 0, y, 1, 0, 1.0, 0.0 ); // $ExpectError + srot.ndarray( x.length, ( x: number ): number => x, 1, 0, y, 1, 0, 1.0, 0.0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a third argument which is not a number... +{ + const x = new Float32Array( 10 ); + const y = new Float32Array( 10 ); + + srot.ndarray( x.length, x, '10', 0, y, 1, 0, 1.0, 0.0 ); // $ExpectError + srot.ndarray( x.length, x, true, 0, y, 1, 0, 1.0, 0.0 ); // $ExpectError + srot.ndarray( x.length, x, false, 0, y, 1, 0, 1.0, 0.0 ); // $ExpectError + srot.ndarray( x.length, x, null, 0, y, 1, 0, 1.0, 0.0 ); // $ExpectError + srot.ndarray( x.length, x, undefined, 0, y, 1, 0, 1.0, 0.0 ); // $ExpectError + srot.ndarray( x.length, x, [], 0, y, 1, 0, 1.0, 0.0 ); // $ExpectError + srot.ndarray( x.length, x, {}, 0, y, 1, 0, 1.0, 0.0 ); // $ExpectError + srot.ndarray( x.length, x, ( x: number ): number => x, 0, y, 1, 0, 1.0, 0.0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a fourth argument which is not a number... +{ + const x = new Float32Array( 10 ); + const y = new Float32Array( 10 ); + + srot.ndarray( x.length, x, 1, '10', y, 1, 0, 1.0, 0.0 ); // $ExpectError + srot.ndarray( x.length, x, 1, true, y, 1, 0, 1.0, 0.0 ); // $ExpectError + srot.ndarray( x.length, x, 1, false, y, 1, 0, 1.0, 0.0 ); // $ExpectError + srot.ndarray( x.length, x, 1, null, y, 1, 0, 1.0, 0.0 ); // $ExpectError + srot.ndarray( x.length, x, 1, undefined, y, 1, 0, 1.0, 0.0 ); // $ExpectError + srot.ndarray( x.length, x, 1, [], y, 1, 0, 1.0, 0.0 ); // $ExpectError + srot.ndarray( x.length, x, 1, {}, y, 1, 0, 1.0, 0.0 ); // $ExpectError + srot.ndarray( x.length, x, 1, ( x: number ): number => x, y, 1, 0, 1.0, 0.0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a fifth argument which is not a Float32Array... +{ + const x = new Float32Array( 10 ); + + srot.ndarray( x.length, x, 1, 0, 10, 1, 0, 1.0, 0.0 ); // $ExpectError + srot.ndarray( x.length, x, 1, 0, '10', 1, 0, 1.0, 0.0 ); // $ExpectError + srot.ndarray( x.length, x, 1, 0, true, 1, 0, 1.0, 0.0 ); // $ExpectError + srot.ndarray( x.length, x, 1, 0, false, 1, 0, 1.0, 0.0 ); // $ExpectError + srot.ndarray( x.length, x, 1, 0, null, 1, 0, 1.0, 0.0 ); // $ExpectError + srot.ndarray( x.length, x, 1, 0, undefined, 1, 0, 1.0, 0.0 ); // $ExpectError + srot.ndarray( x.length, x, 1, 0, [], 1, 0, 1.0, 0.0 ); // $ExpectError + srot.ndarray( x.length, x, 1, 0, {}, 1, 0, 1.0, 0.0 ); // $ExpectError + srot.ndarray( x.length, x, 1, 0, ( x: number ): number => x, 1, 0, 1.0, 0.0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a sixth argument which is not a number... +{ + const x = new Float32Array( 10 ); + const y = new Float32Array( 10 ); + + srot.ndarray( x.length, x, 1, 0, y, '10', 0, 1.0, 0.0 ); // $ExpectError + srot.ndarray( x.length, x, 1, 0, y, true, 0, 1.0, 0.0 ); // $ExpectError + srot.ndarray( x.length, x, 1, 0, y, false, 0, 1.0, 0.0 ); // $ExpectError + srot.ndarray( x.length, x, 1, 0, y, null, 0, 1.0, 0.0 ); // $ExpectError + srot.ndarray( x.length, x, 1, 0, y, undefined, 0, 1.0, 0.0 ); // $ExpectError + srot.ndarray( x.length, x, 1, 0, y, [], 0, 1.0, 0.0 ); // $ExpectError + srot.ndarray( x.length, x, 1, 0, y, {}, 0, 1.0, 0.0 ); // $ExpectError + srot.ndarray( x.length, x, 1, 0, y, ( x: number ): number => x, 0, 1.0, 0.0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a seventh argument which is not a number... +{ + const x = new Float32Array( 10 ); + const y = new Float32Array( 10 ); + + srot.ndarray( x.length, x, 1, 0, y, 1, '10', 1.0, 0.0 ); // $ExpectError + srot.ndarray( x.length, x, 1, 0, y, 1, true, 1.0, 0.0 ); // $ExpectError + srot.ndarray( x.length, x, 1, 0, y, 1, false, 1.0, 0.0 ); // $ExpectError + srot.ndarray( x.length, x, 1, 0, y, 1, null, 1.0, 0.0 ); // $ExpectError + srot.ndarray( x.length, x, 1, 0, y, 1, undefined, 1.0, 0.0 ); // $ExpectError + srot.ndarray( x.length, x, 1, 0, y, 1, [], 1.0, 0.0 ); // $ExpectError + srot.ndarray( x.length, x, 1, 0, y, 1, {}, 1.0, 0.0 ); // $ExpectError + srot.ndarray( x.length, x, 1, 0, y, 1, ( x: number ): number => x, 1.0, 0.0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a eighth argument which is not a number... +{ + const x = new Float32Array( 10 ); + const y = new Float32Array( 10 ); + + srot.ndarray( x.length, x, 1, 0, y, 1, 0, '10', 1.0 ); // $ExpectError + srot.ndarray( x.length, x, 1, 0, y, 1, 0, true, 1.0 ); // $ExpectError + srot.ndarray( x.length, x, 1, 0, y, 1, 0, false, 1.0 ); // $ExpectError + srot.ndarray( x.length, x, 1, 0, y, 1, 0, null, 1.0 ); // $ExpectError + srot.ndarray( x.length, x, 1, 0, y, 1, 0, undefined, 1.0 ); // $ExpectError + srot.ndarray( x.length, x, 1, 0, y, 1, 0, [], 1.0 ); // $ExpectError + srot.ndarray( x.length, x, 1, 0, y, 1, 0, {}, 1.0 ); // $ExpectError + srot.ndarray( x.length, x, 1, 0, y, 1, 0, ( x: number ): number => x, 1.0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a ninth argument which is not a number... +{ + const x = new Float32Array( 10 ); + const y = new Float32Array( 10 ); + + srot.ndarray( x.length, x, 1, 0, y, 1, 0, 1.0, '1.0' ); // $ExpectError + srot.ndarray( x.length, x, 1, 0, y, 1, 0, 1.0, true ); // $ExpectError + srot.ndarray( x.length, x, 1, 0, y, 1, 0, 1.0, false ); // $ExpectError + srot.ndarray( x.length, x, 1, 0, y, 1, 0, 1.0, null ); // $ExpectError + srot.ndarray( x.length, x, 1, 0, y, 1, 0, 1.0, undefined ); // $ExpectError + srot.ndarray( x.length, x, 1, 0, y, 1, 0, 1.0, [] ); // $ExpectError + srot.ndarray( x.length, x, 1, 0, y, 1, 0, 1.0, {} ); // $ExpectError + srot.ndarray( x.length, x, 1, 0, y, 1, 0, 1.0, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided an unsupported number of arguments... +{ + const x = new Float32Array( 10 ); + const y = new Float32Array( 10 ); + + srot.ndarray(); // $ExpectError + srot.ndarray( x.length ); // $ExpectError + srot.ndarray( x.length, x ); // $ExpectError + srot.ndarray( x.length, x, 1 ); // $ExpectError + srot.ndarray( x.length, x, 1, 0 ); // $ExpectError + srot.ndarray( x.length, x, 1, 0, y ); // $ExpectError + srot.ndarray( x.length, x, 1, 0, y, 1.0 ); // $ExpectError + srot.ndarray( x.length, x, 1, 0, y, 1.0, 0.0, 10 ); // $ExpectError +} diff --git a/base/srot/examples/c/Makefile b/base/srot/examples/c/Makefile new file mode 100644 index 000000000..6aed70daf --- /dev/null +++ b/base/srot/examples/c/Makefile @@ -0,0 +1,146 @@ +#/ +# @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. +#/ + +# 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/base/srot/examples/c/example.c b/base/srot/examples/c/example.c new file mode 100644 index 000000000..c96100acb --- /dev/null +++ b/base/srot/examples/c/example.c @@ -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. +*/ + +#include "stdlib/blas/base/srot.h" +#include + +int main( void ) { + // Create strided arrays: + float x[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f }; + float y[] = { 6.0f, 7.0f, 8.0f, 9.0f, 10.0f }; + + // Specify the number of elements: + const int N = 5; + + // Specify stride lengths: + const int strideX = 1; + const int strideY = 1; + + // Specify angle of rotation: + const float c = 0.8f; + const float s = 0.6f; + + // Apply plane rotation: + c_srot( N, x, strideX, y, strideY, c, s ); + + // Print the result: + for ( int i = 0; i < 5; i++ ) { + printf( "x[ %i ] = %f, y[ %i ] = %f\n", i, x[ i ], i, y[ i ] ); + } +} diff --git a/base/srot/examples/index.js b/base/srot/examples/index.js new file mode 100644 index 000000000..d35e7321c --- /dev/null +++ b/base/srot/examples/index.js @@ -0,0 +1,35 @@ +/** +* @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 srot = require( './../lib' ); + +var opts = { + 'dtype': 'float32' +}; +var x = discreteUniform( 10, 0, 500, opts ); +console.log( x ); + +var y = discreteUniform( x.length, 0, 255, opts ); +console.log( y ); + +srot( x.length, x, 1, y, 1, 0.8, 0.6 ); +console.log( x ); +console.log( y ); diff --git a/base/srot/include.gypi b/base/srot/include.gypi new file mode 100644 index 000000000..497aeca15 --- /dev/null +++ b/base/srot/include.gypi @@ -0,0 +1,70 @@ +# @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. + +# A GYP include file for building a Node.js native add-on. +# +# Note that nesting variables is required due to how GYP processes a configuration. Any variables defined within a nested 'variables' section is defined in the outer scope. Thus, conditions in the outer variable scope are free to use these variables without running into "variable undefined" errors. +# +# Main documentation: +# +# [1]: https://gyp.gsrc.io/docs/InputFormatReference.md +# [2]: https://gyp.gsrc.io/docs/UserDocumentation.md +# +# Variable nesting hacks: +# +# [3]: https://chromium.googlesource.com/external/skia/gyp/+/master/common_variables.gypi +# [4]: https://src.chromium.org/viewvc/chrome/trunk/src/build/common.gypi?revision=127004 +{ + # Define variables to be used throughout the configuration for all targets: + 'variables': { + 'variables': { + # Host BLAS library (to override -Dblas=): + 'blas%': '', + + # Path to BLAS library (to override -Dblas_dir=): + 'blas_dir%': '', + }, # end variables + + # Source directory: + 'src_dir': './src', + + # Include directories: + 'include_dirs': [ + '<@(blas_dir)', + ' [ ~4.4, ~5.8, ~7.2, ~8.6, 10.0 ] +* // y => [ ~4.2, ~4.4, ~4.6, ~4.8, 5.0 ] +* +* @example +* var Float32Array = require( '@stdlib/array/float32' ); +* var srot = require( '@stdlib/blas/base/srot' ); +* +* var x = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); +* var y = new Float32Array( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); +* +* srot.ndarray( 2, x, 2, 2, y, 2, 2, 0.8, 0.6 ); +* // x => [ 1.0, 2.0, ~7.8, 4.0, ~10.6, 6.0 ] +* // y => [ 7.0, 8.0, ~5.4, 10.0, ~5.8, 12.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 srot; +var tmp = tryRequire( join( __dirname, './native.js' ) ); +if ( isError( tmp ) ) { + srot = main; +} else { + srot = tmp; +} + + +// EXPORTS // + +module.exports = srot; + +// exports: { "ndarray": "srot.ndarray" } diff --git a/base/srot/lib/main.js b/base/srot/lib/main.js new file mode 100644 index 000000000..fe677692a --- /dev/null +++ b/base/srot/lib/main.js @@ -0,0 +1,35 @@ +/** +* @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 setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var srot = require( './srot.js' ); +var ndarray = require( './ndarray.js' ); + + +// MAIN // + +setReadOnly( srot, 'ndarray', ndarray ); + + +// EXPORTS // + +module.exports = srot; diff --git a/base/srot/lib/native.js b/base/srot/lib/native.js new file mode 100644 index 000000000..f80712cd4 --- /dev/null +++ b/base/srot/lib/native.js @@ -0,0 +1,35 @@ +/** +* @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 setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var srot = require( './srot.native.js' ); +var ndarray = require( './ndarray.native.js' ); + + +// MAIN // + +setReadOnly( srot, 'ndarray', ndarray ); + + +// EXPORTS // + +module.exports = srot; diff --git a/base/srot/lib/ndarray.js b/base/srot/lib/ndarray.js new file mode 100644 index 000000000..4f09d9ac6 --- /dev/null +++ b/base/srot/lib/ndarray.js @@ -0,0 +1,72 @@ +/** +* @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'; + +// MAIN // + +/** +* Applies a plane rotation. +* +* @param {PositiveInteger} N - number of indexed elements +* @param {Float32Array} x - first input array +* @param {integer} strideX - `x` stride length +* @param {NonNegativeInteger} offsetX - starting `x` index +* @param {Float32Array} y - second input array +* @param {integer} strideY - `y` stride length +* @param {NonNegativeInteger} offsetY - starting `y` index +* @param {number} c - cosine of the angle of rotation +* @param {number} s - sine of the angle of rotation +* @returns {Float32Array} `y` +* +* @example +* var Float32Array = require( '@stdlib/array/float32' ); +* +* var x = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); +* var y = new Float32Array( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); +* +* srot( 2, x, 2, 2, y, 2, 2, 0.8, 0.6 ); +* // x => [ 1.0, 2.0, ~7.8, 4.0, ~10.6, 6.0 ] +* // y => [ 7.0, 8.0, ~5.4, 10.0, ~5.8, 12.0 ] +*/ +function srot( N, x, strideX, offsetX, y, strideY, offsetY, c, s ) { + var tmp; + var ix; + var iy; + var i; + + if ( N <= 0 ) { + return y; + } + ix = offsetX; + iy = offsetY; + + for ( i = 0; i < N; i++ ) { + tmp = ( c * x[ ix ] ) + ( s * y[ iy ] ); + y[ iy ] = ( c * y[ iy ] ) - ( s * x[ ix ] ); + x[ ix ] = tmp; + ix += strideX; + iy += strideY; + } + return y; +} + + +// EXPORTS // + +module.exports = srot; diff --git a/base/srot/lib/ndarray.native.js b/base/srot/lib/ndarray.native.js new file mode 100644 index 000000000..def0c29f9 --- /dev/null +++ b/base/srot/lib/ndarray.native.js @@ -0,0 +1,71 @@ +/** +* @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 minViewBufferIndex = require( '@stdlib/strided/base/min-view-buffer-index' ); +var offsetView = require( '@stdlib/strided/base/offset-view' ); +var addon = require( './srot.native.js' ); + + +// MAIN // + +/** +* Applies a plane rotation. +* +* @param {PositiveInteger} N - number of indexed elements +* @param {Float32Array} x - input array +* @param {integer} strideX - `x` stride length +* @param {NonNegativeInteger} offsetX - starting `x` index +* @param {Float32Array} y - output array +* @param {integer} strideY - `y` stride length +* @param {NonNegativeInteger} offsetY - starting `y` index +* @param {number} c - cosine of the angle of rotation +* @param {number} s - sine of the angle of rotation +* @returns {Float32Array} `y` +* +* @example +* var Float32Array = require( '@stdlib/array/float32' ); +* +* var x = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); +* var y = new Float32Array( [ 6.0, 7.0, 8.0, 9.0, 10.0 ] ); +* +* srot( 4, x, 1, 1, y, 1, 1, 0.8, 0.6 ); +* // x => [ 1.0, ~5.8, ~7.2, ~8.6, 10.0 ] +* // y => [ 6.0, 4.4, ~4.6, ~4.8, 5.0 ] +*/ +function srot( N, x, strideX, offsetX, y, strideY, offsetY, c, s ) { + var viewX; + var viewY; + + offsetX = minViewBufferIndex( N, strideX, offsetX ); + offsetY = minViewBufferIndex( N, strideY, offsetY ); + + viewX = offsetView( x, offsetX ); + viewY = offsetView( y, offsetY ); + + addon( N, viewX, strideX, viewY, strideY, c, s ); + return y; +} + + +// EXPORTS // + +module.exports = srot; diff --git a/base/srot/lib/srot.js b/base/srot/lib/srot.js new file mode 100644 index 000000000..4e4809ec4 --- /dev/null +++ b/base/srot/lib/srot.js @@ -0,0 +1,87 @@ +/** +* @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'; + +// MAIN // + +/** +* Applies a plane rotation. +* +* @param {PositiveInteger} N - number of indexed elements +* @param {Float32Array} x - first input array +* @param {integer} strideX - `x` stride length +* @param {Float32Array} y - second input array +* @param {integer} strideY - `y` stride length +* @param {number} c - cosine of the angle of rotation +* @param {number} s - sine of the angle of rotation +* @returns {Float32Array} `y` +* +* @example +* var Float32Array = require( '@stdlib/array/float32' ); +* +* var x = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); +* var y = new Float32Array( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); +* +* srot( 3, x, 2, y, 2, 0.8, 0.6 ); +* // x => [ 5.0, 2.0, ~7.8, 4.0, ~10.6, 6.0 ] +* // y => [ 5.0, 8.0, ~5.4, 10.0, ~5.8, 12.0 ] +*/ +function srot( N, x, strideX, y, strideY, c, s ) { + var tmp; + var ix; + var iy; + var i; + + if ( N <= 0 ) { + return y; + } + // If both strides are equal to `1`... + if ( strideX === 1 && strideY === 1 ) { + for ( i = 0; i < N; i++ ) { + tmp = ( c * x[ i ] ) + ( s * y[ i ] ); + y[ i ] = ( c * y[ i ] ) - ( s * x[ i ] ); + x[ i ] = tmp; + } + return y; + } + // If both strides are not equal to `1`... + if ( strideX < 0 ) { + ix = ( 1 - N ) * strideX; + } else { + ix = 0; + } + if ( strideY < 0 ) { + iy = ( 1 - N ) * strideY; + } else { + iy = 0; + } + for ( i = 0; i < N; i++ ) { + tmp = ( c * x[ ix ] ) + ( s * y[ iy ] ); + y[ iy ] = ( c * y[ iy ] ) - ( s * x[ ix ] ); + x[ ix ] = tmp; + ix += strideX; + iy += strideY; + } + return y; +} + + +// EXPORTS // + +module.exports = srot; diff --git a/base/srot/lib/srot.native.js b/base/srot/lib/srot.native.js new file mode 100644 index 000000000..3d8390311 --- /dev/null +++ b/base/srot/lib/srot.native.js @@ -0,0 +1,58 @@ +/** +* @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 addon = require( './../src/addon.node' ); + + +// MAIN // + +/** +* Applies a plane rotation. +* +* @param {PositiveInteger} N - number of indexed elements +* @param {Float32Array} x - input array +* @param {integer} strideX - `x` stride length +* @param {Float32Array} y - output array +* @param {integer} strideY - `y` stride length +* @param {number} c - cosine of the angle of rotation +* @param {number} s - sine of the angle of rotation +* @returns {Float32Array} `y` +* +* @example +* var Float32Array = require( '@stdlib/array/float32' ); +* +* var x = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); +* var y = new Float32Array( [ 6.0, 7.0, 8.0, 9.0, 10.0 ] ); +* +* srot( x.length, x, 1, y, 1, 0.8, 0.6 ); +* // x => [ ~4.4, ~5.8, ~7.2, ~8.6, 10.0 ] +* // y => [ ~4.2, 4.4, 4.6, 4.8, 5.0 ] +*/ +function srot( N, x, strideX, y, strideY, c, s ) { + addon( N, x, strideX, y, strideY, c, s ); + return y; +} + + +// EXPORTS // + +module.exports = srot; diff --git a/base/srot/manifest.json b/base/srot/manifest.json new file mode 100644 index 000000000..e494b8d25 --- /dev/null +++ b/base/srot/manifest.json @@ -0,0 +1,385 @@ +{ + "options": { + "task": "build", + "os": "linux", + "blas": "", + "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", + "os": "linux", + "blas": "", + "wasm": false, + "src": [ + "./src/srot.f", + "./src/srot_f.c" + ], + "include": [ + "./include" + ], + "libraries": [], + "libpath": [], + "dependencies": [ + "@stdlib/napi/export", + "@stdlib/napi/argv", + "@stdlib/napi/argv-int64", + "@stdlib/napi/argv-strided-float32array", + "@stdlib/napi/argv-float" + ] + }, + { + "task": "benchmark", + "os": "linux", + "blas": "", + "wasm": false, + "src": [ + "./src/srot.c" + ], + "include": [ + "./include" + ], + "libraries": [], + "libpath": [], + "dependencies": [] + }, + { + "task": "examples", + "os": "linux", + "blas": "", + "wasm": false, + "src": [ + "./src/srot.c" + ], + "include": [ + "./include" + ], + "libraries": [], + "libpath": [], + "dependencies": [] + }, + + { + "task": "build", + "os": "linux", + "blas": "openblas", + "wasm": false, + "src": [ + "./src/srot_cblas.c" + ], + "include": [ + "./include" + ], + "libraries": [ + "-lopenblas", + "-lpthread" + ], + "libpath": [], + "dependencies": [ + "@stdlib/napi/export", + "@stdlib/napi/argv", + "@stdlib/napi/argv-int64", + "@stdlib/napi/argv-strided-float32array", + "@stdlib/napi/argv-float" + ] + }, + { + "task": "benchmark", + "os": "linux", + "blas": "openblas", + "wasm": false, + "src": [ + "./src/srot_cblas.c" + ], + "include": [ + "./include" + ], + "libraries": [ + "-lopenblas", + "-lpthread" + ], + "libpath": [], + "dependencies": [] + }, + { + "task": "examples", + "os": "linux", + "blas": "openblas", + "wasm": false, + "src": [ + "./src/srot_cblas.c" + ], + "include": [ + "./include" + ], + "libraries": [ + "-lopenblas", + "-lpthread" + ], + "libpath": [], + "dependencies": [] + }, + + { + "task": "build", + "os": "mac", + "blas": "", + "wasm": false, + "src": [ + "./src/srot.f", + "./src/srot_f.c" + ], + "include": [ + "./include" + ], + "libraries": [], + "libpath": [], + "dependencies": [ + "@stdlib/napi/export", + "@stdlib/napi/argv", + "@stdlib/napi/argv-int64", + "@stdlib/napi/argv-strided-float32array", + "@stdlib/napi/argv-float" + ] + }, + { + "task": "benchmark", + "os": "mac", + "blas": "", + "wasm": false, + "src": [ + "./src/srot.c" + ], + "include": [ + "./include" + ], + "libraries": [], + "libpath": [], + "dependencies": [] + }, + { + "task": "examples", + "os": "mac", + "blas": "", + "wasm": false, + "src": [ + "./src/srot.c" + ], + "include": [ + "./include" + ], + "libraries": [], + "libpath": [], + "dependencies": [] + }, + + { + "task": "build", + "os": "mac", + "blas": "apple_accelerate_framework", + "wasm": false, + "src": [ + "./src/srot_cblas.c" + ], + "include": [ + "./include" + ], + "libraries": [ + "-lblas" + ], + "libpath": [], + "dependencies": [ + "@stdlib/napi/export", + "@stdlib/napi/argv", + "@stdlib/napi/argv-int64", + "@stdlib/napi/argv-strided-float32array", + "@stdlib/napi/argv-float" + ] + }, + { + "task": "benchmark", + "os": "mac", + "blas": "apple_accelerate_framework", + "wasm": false, + "src": [ + "./src/srot_cblas.c" + ], + "include": [ + "./include" + ], + "libraries": [ + "-lblas" + ], + "libpath": [], + "dependencies": [] + }, + { + "task": "examples", + "os": "mac", + "blas": "apple_accelerate_framework", + "wasm": false, + "src": [ + "./src/srot_cblas.c" + ], + "include": [ + "./include" + ], + "libraries": [ + "-lblas" + ], + "libpath": [], + "dependencies": [] + }, + + { + "task": "build", + "os": "mac", + "blas": "openblas", + "wasm": false, + "src": [ + "./src/srot_cblas.c" + ], + "include": [ + "./include" + ], + "libraries": [ + "-lopenblas", + "-lpthread" + ], + "libpath": [], + "dependencies": [ + "@stdlib/napi/export", + "@stdlib/napi/argv", + "@stdlib/napi/argv-int64", + "@stdlib/napi/argv-strided-float32array", + "@stdlib/napi/argv-float" + ] + }, + { + "task": "benchmark", + "os": "mac", + "blas": "openblas", + "wasm": false, + "src": [ + "./src/srot_cblas.c" + ], + "include": [ + "./include" + ], + "libraries": [ + "-lopenblas", + "-lpthread" + ], + "libpath": [], + "dependencies": [] + }, + { + "task": "examples", + "os": "mac", + "blas": "openblas", + "wasm": false, + "src": [ + "./src/srot_cblas.c" + ], + "include": [ + "./include" + ], + "libraries": [ + "-lopenblas", + "-lpthread" + ], + "libpath": [], + "dependencies": [] + }, + + { + "task": "build", + "os": "win", + "blas": "", + "wasm": false, + "src": [ + "./src/srot.c" + ], + "include": [ + "./include" + ], + "libraries": [], + "libpath": [], + "dependencies": [ + "@stdlib/napi/export", + "@stdlib/napi/argv", + "@stdlib/napi/argv-int64", + "@stdlib/napi/argv-strided-float32array", + "@stdlib/napi/argv-float" + ] + }, + { + "task": "benchmark", + "os": "win", + "blas": "", + "wasm": false, + "src": [ + "./src/srot.c" + ], + "include": [ + "./include" + ], + "libraries": [], + "libpath": [], + "dependencies": [] + }, + { + "task": "examples", + "os": "win", + "blas": "", + "wasm": false, + "src": [ + "./src/srot.c" + ], + "include": [ + "./include" + ], + "libraries": [], + "libpath": [], + "dependencies": [] + }, + + { + "task": "build", + "os": "", + "blas": "", + "wasm": true, + "src": [ + "./src/srot.c" + ], + "include": [ + "./include" + ], + "libraries": [], + "libpath": [], + "dependencies": [] + } + ] +} diff --git a/base/srot/package.json b/base/srot/package.json new file mode 100644 index 000000000..16fc63258 --- /dev/null +++ b/base/srot/package.json @@ -0,0 +1,75 @@ +{ + "name": "@stdlib/blas/base/srot", + "version": "0.0.0", + "description": "Apply a plane rotation.", + "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", + "level 1", + "srot", + "rotation", + "linear", + "algebra", + "subroutines", + "vector", + "array", + "ndarray", + "float32", + "float", + "single", + "float32array" + ] +} diff --git a/base/srot/src/Makefile b/base/srot/src/Makefile new file mode 100644 index 000000000..bcf18aa46 --- /dev/null +++ b/base/srot/src/Makefile @@ -0,0 +1,70 @@ +#/ +# @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. +#/ + +# 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/base/srot/src/addon.c b/base/srot/src/addon.c new file mode 100644 index 000000000..a54ac2b26 --- /dev/null +++ b/base/srot/src/addon.c @@ -0,0 +1,48 @@ +/** +* @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. +*/ + +#include "stdlib/blas/base/srot.h" +#include "stdlib/napi/export.h" +#include "stdlib/napi/argv.h" +#include "stdlib/napi/argv_int64.h" +#include "stdlib/napi/argv_float.h" +#include "stdlib/napi/argv_strided_float32array.h" +#include + +/** +* Receives JavaScript callback invocation data. +* +* @private +* @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, 7 ); + STDLIB_NAPI_ARGV_INT64( env, N, argv, 0 ); + STDLIB_NAPI_ARGV_INT64( env, strideX, argv, 2 ); + STDLIB_NAPI_ARGV_INT64( env, strideY, argv, 4 ); + STDLIB_NAPI_ARGV_FLOAT( env, c, argv, 5 ); + STDLIB_NAPI_ARGV_FLOAT( env, s, argv, 6 ); + STDLIB_NAPI_ARGV_STRIDED_FLOAT32ARRAY( env, X, N, strideX, argv, 1 ); + STDLIB_NAPI_ARGV_STRIDED_FLOAT32ARRAY( env, Y, N, strideY, argv, 3 ); + c_srot( N, X, strideX, Y, strideY, c, s ); + return NULL; +} + +STDLIB_NAPI_MODULE_EXPORT_FCN( addon ) diff --git a/base/srot/src/srot.c b/base/srot/src/srot.c new file mode 100644 index 000000000..a81a8d755 --- /dev/null +++ b/base/srot/src/srot.c @@ -0,0 +1,69 @@ +/** +* @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. +*/ + +#include "stdlib/blas/base/srot.h" + +/** +* Applies a plane rotation. +* +* @param N number of indexed elements +* @param X input array +* @param strideX X stride length +* @param Y output array +* @param strideY Y stride length +* @param c cosine of the angle of rotation +* @param s sine of the angle of rotation +*/ +void c_srot( const int N, float *X, const int strideX, float *Y, const int strideY, const float c, const float s ) { + float tmp; + int ix; + int iy; + int i; + + if ( N <= 0 ) { + return; + } + // If both strides are equal to `1`... + if ( strideX == 1 && strideY == 1 ) { + for ( i = 0; i < N; i++ ) { + tmp = ( c * X[ i ] ) + ( s * Y[ i ] ); + Y[ i ] = ( c * Y[ i ] ) - ( s * X[ i ] ); + X[ i ] = tmp; + } + return; + } + // If both strides are not equal to `1`... + if ( strideX < 0 ) { + ix = ( 1 - N ) * strideX; + } else { + ix = 0; + } + if ( strideY < 0 ) { + iy = ( 1 - N ) * strideY; + } else { + iy = 0; + } + for ( i = 0; i < N; i++ ) { + tmp = ( c * X[ ix ] ) + ( s * Y[ iy ] ); + Y[ iy ] = ( c * Y[ iy ] ) - ( s * X[ ix ] ); + X[ ix ] = tmp; + ix += strideX; + iy += strideY; + } + return; +} diff --git a/base/srot/src/srot.f b/base/srot/src/srot.f new file mode 100644 index 000000000..9c52b0bec --- /dev/null +++ b/base/srot/src/srot.f @@ -0,0 +1,105 @@ +!> +! @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. +!< + +!> Applies a plane rotation. +! +! ## Notes +! +! * Modified version of reference BLAS level1 routine (version 3.7.0). Updated to "free form" Fortran 95. +! +! ## Authors +! +! * Univ. of Tennessee +! * Univ. of California Berkeley +! * Univ. of Colorado Denver +! * NAG Ltd. +! +! ## History +! +! * Jack Dongarra, linpack, 3/11/78. +! +! - modified 12/3/93, array(1) declarations changed to array(*) +! +! ## License +! +! From : +! +! > The reference BLAS is a freely-available software package. It is available from netlib via anonymous ftp and the World Wide Web. Thus, it can be included in commercial software packages (and has been). We only ask that proper credit be given to the authors. +! > +! > Like all software, it is copyrighted. It is not trademarked, but we do ask the following: +! > +! > * If you modify the source for these routines we ask that you change the name of the routine and comment the changes made to the original. +! > +! > * We will gladly answer any questions regarding the software. If a modification is done, however, it is the responsibility of the person who modified the routine to provide support. +! +! @param {integer} N - number of indexed elements +! @param {Array} sx - input array +! @param {integer} strideX - `sx` stride length +! @param {Array} sy - output array +! @param {integer} strideY - `sy` stride length +! @param {real} c - cosine of the angle of rotation +! @param {real} s - sine of the angle of rotation +!< +subroutine srot( N, sx, strideX, sy, strideY, c, s ) + implicit none + ! .. + ! Internal parameters: + integer, parameter :: sp=kind(0.0) ! single-precision + ! .. + ! Scalar arguments: + integer :: strideX, strideY, N + real(sp) :: c, s + ! .. + ! Array arguments: + real(sp) :: sx(*), sy(*) + ! .. + ! Local scalars: + integer :: i, ix, iy + real(sp) :: stemp + ! .. + if ( N <= 0 ) then + return + end if + ! .. + if ( strideX == 1 .AND. strideY == 1 ) then + do i = 1, N + stemp = ( c*sx(i) ) + ( s*sy(i) ) + sy( i ) = ( c*sy(i) ) - ( s*sx(i) ) + sx( i ) = stemp + end do + else + if ( strideX < 0 ) then + ix = ( 1 - N ) * strideX + 1 + else + ix = 1 + end if + if ( strideY < 0 ) then + iy = ( 1 - N ) * strideY + 1 + else + iy = 1 + end if + do i = 1, N + stemp = ( c*sx(ix) ) + ( s*sy(iy) ) + sy( iy ) = ( c*sy(iy) ) - ( s*sx(ix) ) + sx( ix ) = stemp + ix = ix + strideX + iy = iy + strideY + end do + end if + return +end subroutine srot \ No newline at end of file diff --git a/base/srot/src/srot_cblas.c b/base/srot/src/srot_cblas.c new file mode 100644 index 000000000..31e541943 --- /dev/null +++ b/base/srot/src/srot_cblas.c @@ -0,0 +1,35 @@ +/** +* @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. +*/ + +#include "stdlib/blas/base/srot.h" +#include "stdlib/blas/base/srot_cblas.h" + +/** +* Applies a plane rotation. +* +* @param N number of indexed elements +* @param X input array +* @param strideX X stride length +* @param Y output array +* @param strideY Y stride length +* @param c cosine of the angle of rotation +* @param s sine of the angle of rotation +*/ +void c_srot( const int N, float *X, const int strideX, float *Y, const int strideY, const float c, const float s ) { + cblas_srot( N, X, strideX, Y, strideY, c, s ); +} diff --git a/base/srot/src/srot_f.c b/base/srot/src/srot_f.c new file mode 100644 index 000000000..b74353e70 --- /dev/null +++ b/base/srot/src/srot_f.c @@ -0,0 +1,35 @@ +/** +* @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. +*/ + +#include "stdlib/blas/base/srot.h" +#include "stdlib/blas/base/srot_fortran.h" + +/** +* Applies a plane rotation. +* +* @param N number of indexed elements +* @param X input array +* @param strideX X stride length +* @param Y output array +* @param strideY Y stride length +* @param c cosine of the angle of rotation +* @param s sine of the angle of rotation +*/ +void c_srot( const int N, float *X, const int strideX, float *Y, const int strideY, const float c, const float s ) { + srot( &N, X, &strideX, Y, &strideY, &c, &s ); +} diff --git a/base/srot/test/test.js b/base/srot/test/test.js new file mode 100644 index 000000000..f247f60b0 --- /dev/null +++ b/base/srot/test/test.js @@ -0,0 +1,82 @@ +/** +* @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 proxyquire = require( 'proxyquire' ); +var isBrowser = require( '@stdlib/assert/is-browser' ); +var srot = require( './../lib' ); + + +// VARIABLES // + +var opts = { + 'skip': isBrowser +}; + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof srot, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method providing an ndarray interface', function test( t ) { + t.strictEqual( typeof srot.ndarray, 'function', 'method is a function' ); + t.end(); +}); + +tape( 'if a native implementation is available, the main export is the native implementation', opts, function test( t ) { + var srot = proxyquire( './../lib', { + '@stdlib/utils/try-require': tryRequire + }); + + t.strictEqual( srot, 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 srot; + var main; + + main = require( './../lib/srot.js' ); + + srot = proxyquire( './../lib', { + '@stdlib/utils/try-require': tryRequire + }); + + t.strictEqual( srot, main, 'returns expected value' ); + t.end(); + + function tryRequire() { + return new Error( 'Cannot find module' ); + } +}); diff --git a/base/srot/test/test.ndarray.js b/base/srot/test/test.ndarray.js new file mode 100644 index 000000000..e0420a2c1 --- /dev/null +++ b/base/srot/test/test.ndarray.js @@ -0,0 +1,490 @@ +/** +* @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 Float32Array = require( '@stdlib/array/float32' ); +var scopy = require( './../../../base/scopy' ).ndarray; +var EPS = require( '@stdlib/constants/float32/eps' ); +var abs = require( '@stdlib/math/base/special/abs' ); +var srot = require( './../lib/ndarray.js' ); + + +// FUNCTIONS // + +/** +* Tests for element-wise approximate equality. +* +* @private +* @param {Object} t - test object +* @param {Collection} actual - actual values +* @param {Collection} expected - expected values +* @param {number} rtol - relative tolerance +*/ +function isApprox( t, actual, expected, rtol ) { + var delta; + var tol; + var i; + + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + for ( i = 0; i < expected.length; i++ ) { + if ( actual[ i ] === expected[ i ] ) { + t.strictEqual( actual[ i ], expected[ i ], 'returns expected value' ); + } else { + delta = abs( actual[ i ] - expected[ i ] ); + tol = rtol * EPS * abs( expected[ i ] ); + t.ok( delta <= tol, 'within tolerance. actual: '+actual[ i ]+'. expected: '+expected[ i ]+'. delta: '+delta+'. tol: '+tol+'.' ); + } + } +} + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof srot, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 9', function test( t ) { + t.strictEqual( srot.length, 9, 'returns expected value' ); + t.end(); +}); + +tape( 'the function applies a plane rotation (sx=1, sy=1)', function test( t ) { + var xbuf; + var ybuf; + var out; + var xe; + var ye; + var ox; + var oy; + var N; + var x; + var y; + var i; + + N = [ 0, 1, 2, 4 ]; + + xbuf = [ 0.6, 0.1, -0.5, 0.8, 0.9, -0.3, -0.4 ]; + ybuf = [ 0.5, -0.9, 0.3, 0.7, -0.6, 0.2, 0.8 ]; + + ox = [ 0, 0, 0, 0 ]; + oy = [ 0, 0, 0, 0 ]; + + xe = [ + new Float32Array( [ 0.6, 0.1, -0.5, 0.8, 0.9, -0.3, -0.4 ] ), + new Float32Array( [ 0.78, 0.1, -0.5, 0.8, 0.9, -0.3, -0.4 ] ), + new Float32Array( [ 0.78, -0.46, -0.5, 0.8, 0.9, -0.3, -0.4 ] ), + new Float32Array( [ 0.78, -0.46, -0.22, 1.06, 0.9, -0.3, -0.4 ] ) + ]; + ye = [ + new Float32Array( [ 0.5, -0.9, 0.3, 0.7, -0.6, 0.2, 0.8 ] ), + new Float32Array( [ 0.04, -0.9, 0.3, 0.7, -0.6, 0.2, 0.8 ] ), + new Float32Array( [ 0.04, -0.78, 0.3, 0.7, -0.6, 0.2, 0.8 ] ), + new Float32Array( [ 0.04, -0.78, 0.54, 0.08, -0.6, 0.2, 0.8 ] ) + ]; + + for ( i = 0; i < N.length; i++ ) { + x = new Float32Array( xbuf ); + y = new Float32Array( ybuf ); + out = srot( N[ i ], x, 1, ox[ i ], y, 1, oy[ i ], 0.8, 0.6 ); + isApprox( t, x, xe[ i ], 6.0 ); + isApprox( t, y, ye[ i ], 6.0 ); + t.strictEqual( out, y, 'returns expected value' ); + } + t.end(); +}); + +tape( 'the function applies a plane rotation (sx=2, sy=-2)', function test( t ) { + var xbuf; + var ybuf; + var out; + var xe; + var ye; + var ox; + var oy; + var N; + var x; + var y; + var i; + + N = [ 0, 1, 2, 4 ]; + + xbuf = [ 0.6, 0.1, -0.5, 0.8, 0.9, -0.3, -0.4 ]; + ybuf = [ 0.5, -0.9, 0.3, 0.7, -0.6, 0.2, 0.8 ]; + + ox = [ 0, 0, 0, 0 ]; + oy = [ 0, 0, 2, 6 ]; + + xe = [ + new Float32Array( [ 0.6, 0.1, -0.5, 0.8, 0.9, -0.3, -0.4 ] ), + new Float32Array( [ 0.78, 0.1, -0.5, 0.8, 0.9, -0.3, -0.4 ] ), + new Float32Array( [ 0.66, 0.1, -0.1, 0.8, 0.9, -0.3, -0.4 ] ), + new Float32Array( [ 0.96, 0.1, -0.76, 0.8, 0.9, -0.3, -0.02 ] ) + ]; + ye = [ + new Float32Array( [ 0.5, -0.9, 0.3, 0.7, -0.6, 0.2, 0.8 ] ), + new Float32Array( [ 0.04, -0.9, 0.3, 0.7, -0.6, 0.2, 0.8 ] ), + new Float32Array( [ 0.7, -0.9, -0.12, 0.7, -0.6, 0.2, 0.8 ] ), + new Float32Array( [ 0.64, -0.9, -0.3, 0.7, -0.18, 0.2, 0.28 ] ) + ]; + + for ( i = 0; i < N.length; i++ ) { + x = new Float32Array( xbuf ); + y = new Float32Array( ybuf ); + out = srot( N[ i ], x, 2, ox[ i ], y, -2, oy[ i ], 0.8, 0.6 ); + isApprox( t, x, xe[ i ], 5.0 ); + isApprox( t, y, ye[ i ], 5.0 ); + t.strictEqual( out, y, 'returns expected value' ); + } + t.end(); +}); + +tape( 'the function applies a plane rotation (sx=-2, sy=1)', function test( t ) { + var xbuf; + var ybuf; + var out; + var xe; + var ye; + var ox; + var oy; + var N; + var x; + var y; + var i; + + N = [ 0, 1, 2, 4 ]; + + xbuf = [ 0.6, 0.1, -0.5, 0.8, 0.9, -0.3, -0.4 ]; + ybuf = [ 0.5, -0.9, 0.3, 0.7, -0.6, 0.2, 0.8 ]; + + ox = [ 0, 0, 2, 6 ]; + oy = [ 0, 0, 0, 0 ]; + + xe = [ + new Float32Array( [ 0.6, 0.1, -0.5, 0.8, 0.9, -0.3, -0.4 ] ), + new Float32Array( [ 0.78, 0.1, -0.5, 0.8, 0.9, -0.3, -0.4 ] ), + new Float32Array( [ -0.06, 0.1, -0.1, 0.8, 0.9, -0.3, -0.4 ] ), + new Float32Array( [ 0.9, 0.1, -0.22, 0.8, 0.18, -0.3, -0.02 ] ) + ]; + ye = [ + new Float32Array( [ 0.5, -0.9, 0.3, 0.7, -0.6, 0.2, 0.8 ] ), + new Float32Array( [ 0.04, -0.9, 0.3, 0.7, -0.6, 0.2, 0.8 ] ), + new Float32Array( [ 0.7, -1.08, 0.3, 0.7, -0.6, 0.2, 0.8 ] ), + new Float32Array( [ 0.64, -1.26, 0.54, 0.2, -0.6, 0.2, 0.8 ] ) + ]; + for ( i = 0; i < N.length; i++ ) { + x = new Float32Array( xbuf ); + y = new Float32Array( ybuf ); + out = srot( N[ i ], x, -2, ox[ i ], y, 1, oy[ i ], 0.8, 0.6 ); + isApprox( t, x, xe[ i ], 5.0 ); + isApprox( t, y, ye[ i ], 5.0 ); + t.strictEqual( out, y, 'returns expected value' ); + } + t.end(); +}); + +tape( 'the function applies a plane rotation (sx=-1, sy=-2)', function test( t ) { + var xbuf; + var ybuf; + var out; + var xe; + var ye; + var ox; + var oy; + var N; + var x; + var y; + var i; + + N = [ 0, 1, 2, 4 ]; + + xbuf = [ 0.6, 0.1, -0.5, 0.8, 0.9, -0.3, -0.4 ]; + ybuf = [ 0.5, -0.9, 0.3, 0.7, -0.6, 0.2, 0.8 ]; + + ox = [ 0, 0, 1, 3 ]; + oy = [ 0, 0, 2, 6 ]; + + xe = [ + new Float32Array( [ 0.6, 0.1, -0.5, 0.8, 0.9, -0.3, -0.4 ] ), + new Float32Array( [ 0.78, 0.1, -0.5, 0.8, 0.9, -0.3, -0.4 ] ), + new Float32Array( [ 0.78, 0.26, -0.5, 0.8, 0.9, -0.3, -0.4 ] ), + new Float32Array( [ 0.78, 0.26, -0.76, 1.12, 0.9, -0.3, -0.4 ] ) + ]; + ye = [ + new Float32Array( [ 0.5, -0.9, 0.3, 0.7, -0.6, 0.2, 0.8 ] ), + new Float32Array( [ 0.04, -0.9, 0.3, 0.7, -0.6, 0.2, 0.8 ] ), + new Float32Array( [ 0.04, -0.9, 0.18, 0.7, -0.6, 0.2, 0.8 ] ), + new Float32Array( [ 0.04, -0.9, 0.18, 0.7, -0.18, 0.2, 0.16 ] ) + ]; + + for ( i = 0; i < N.length; i++ ) { + x = new Float32Array( xbuf ); + y = new Float32Array( ybuf ); + out = srot( N[ i ], x, -1, ox[ i ], y, -2, oy[ i ], 0.8, 0.6 ); + isApprox( t, x, xe[ i ], 4.0 ); + isApprox( t, y, ye[ i ], 4.0 ); + t.strictEqual( out, y, 'returns expected value' ); + } + t.end(); +}); + +tape( 'the function supports an `x` stride', function test( t ) { + var xe; + var ye; + var x; + var y; + + x = new Float32Array([ + 1.0, // 0 + 2.0, + 3.0, // 1 + 4.0, + 5.0 + ]); + y = new Float32Array([ + 6.0, // 0 + 7.0, // 1 + 8.0, + 9.0, + 10.0 + ]); + + srot( 2, x, 2, 0, y, 1, 0, 0.8, 0.6 ); + + xe = new Float32Array( [ 4.4, 2.0, 6.6, 4.0, 5.0 ] ); + ye = new Float32Array( [ 4.2, 3.8, 8.0, 9.0, 10.0 ] ); + + isApprox( t, x, xe, 2.0 ); + isApprox( t, y, ye, 2.0 ); + + t.end(); +}); + +tape( 'the function supports an `x` offset', function test( t ) { + var xe; + var ye; + var x; + var y; + + x = new Float32Array([ + 1.0, + 2.0, // 0 + 3.0, // 1 + 4.0, + 5.0 + ]); + y = new Float32Array([ + 6.0, // 0 + 7.0, // 1 + 8.0, + 9.0, + 10.0 + ]); + + srot( 2, x, 1, 1, y, 1, 0, 0.8, 0.6 ); + + xe = new Float32Array( [ 1.0, 5.2, 6.6, 4.0, 5.0 ] ); + ye = new Float32Array( [ 3.6, 3.8, 8.0, 9.0, 10.0 ] ); + + isApprox( t, x, xe, 2.0 ); + isApprox( t, y, ye, 2.0 ); + + t.end(); +}); + +tape( 'the function supports a `y` stride', function test( t ) { + var xe; + var ye; + var x; + var y; + + x = new Float32Array([ + 1.0, // 0 + 2.0, // 1 + 3.0, // 2 + 4.0, + 5.0 + ]); + y = new Float32Array([ + 1.0, // 0 + 2.0, + 3.0, // 1 + 4.0, + 5.0 // 2 + ]); + + srot( 3, x, 1, 0, y, 2, 0, 0.0, -1.0 ); + + xe = new Float32Array( [ -1.0, -3.0, -5.0, 4.0, 5.0 ] ); + ye = new Float32Array( [ 1.0, 2.0, 2.0, 4.0, 3.0 ] ); + + isApprox( t, x, xe, 1.0 ); + isApprox( t, y, ye, 1.0 ); + + t.end(); +}); + +tape( 'the function supports a `y` offset', function test( t ) { + var xe; + var ye; + var x; + var y; + + x = new Float32Array([ + 1.0, // 0 + 2.0, // 1 + 3.0, + 4.0, + 5.0 + ]); + y = new Float32Array([ + 6.0, + 7.0, + 8.0, // 0 + 9.0, // 1 + 10.0 + ]); + + srot( 2, x, 1, 0, y, 1, 2, 0.8, 0.6 ); + + xe = new Float32Array( [ 5.6, 7.0, 3.0, 4.0, 5.0 ] ); + ye = new Float32Array( [ 6.0, 7.0, 5.8, 6.0, 10.0 ] ); + + isApprox( t, x, xe, 1.0 ); + isApprox( t, y, ye, 1.0 ); + + t.end(); +}); + +tape( 'the function returns a reference to the second input array', function test( t ) { + var out; + var x; + var y; + + x = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); + y = new Float32Array( [ 6.0, 7.0, 8.0, 9.0, 10.0 ] ); + + out = srot( x.length, x, 1, 0, y, 1, 0, 1, 0 ); + + t.strictEqual( out, y, 'returns expected value' ); + t.end(); +}); + +tape( 'if provided an `N` parameter less than or equal to `0`, the function leaves both input arrays unchanged', function test( t ) { + var xe; + var ye; + var x; + var y; + + x = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); + y = new Float32Array( [ 6.0, 7.0, 8.0, 9.0, 10.0 ] ); + + xe = new Float32Array( x.length ); + scopy( x.length, x, 1, 0, xe, 1, 0 ); + + ye = new Float32Array( y.length ); + scopy( y.length, y, 1, 0, ye, 1, 0 ); + + srot( -1, x, 1, 0, y, 1, 0, 0.8, 0.6 ); + t.deepEqual( x, xe, 'returns expected value' ); + t.deepEqual( y, ye, 'returns expected value' ); + + srot( 0, x, 1, 0, y, 1, 0, 0.8, 0.6 ); + t.deepEqual( x, xe, 'returns expected value' ); + t.deepEqual( y, ye, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports negative strides', function test( t ) { + var xe; + var ye; + var x; + var y; + + x = new Float32Array([ + 0.6, // 3 + 0.1, + -0.5, // 2 + 0.8, + 0.9, // 1 + -0.3, + -0.4 // 0 + ]); + y = new Float32Array([ + 0.5, // 0 + -0.9, // 1 + 0.3, // 2 + 0.7, // 3 + -0.6, + 0.2, + 0.8 + ]); + + srot( 4, x, -2, 6, y, 1, 0, 0.8, 0.6 ); + + xe = new Float32Array( [ 0.9, 0.1, -0.22, 0.8, 0.18, -0.3, -0.02 ] ); + ye = new Float32Array( [ 0.64, -1.26, 0.54, 0.2, -0.6, 0.2, 0.8 ] ); + + isApprox( t, x, xe, 5.0 ); + isApprox( t, y, ye, 5.0 ); + + t.end(); +}); + +tape( 'the function supports complex access patterns', function test( t ) { + var xe; + var ye; + var x; + var y; + + x = new Float32Array([ + 0.6, // 1 + 0.1, // 0 + -0.5, + 0.8, + 0.9, + -0.3, + -0.4 + ]); + y = new Float32Array([ + 0.5, // 1 + -0.9, + 0.3, // 0 + 0.7, + -0.6, + 0.2, + 0.8 + ]); + + srot( 2, x, -1, 1, y, -2, 2, 0.8, 0.6 ); + + xe = new Float32Array( [ 0.78, 0.26, -0.5, 0.8, 0.9, -0.3, -0.4 ] ); + ye = new Float32Array( [ 0.04, -0.9, 0.18, 0.7, -0.6, 0.2, 0.8 ] ); + + isApprox( t, x, xe, 5.0 ); + isApprox( t, y, ye, 5.0 ); + + t.end(); +}); diff --git a/base/srot/test/test.ndarray.native.js b/base/srot/test/test.ndarray.native.js new file mode 100644 index 000000000..eee3d7dbc --- /dev/null +++ b/base/srot/test/test.ndarray.native.js @@ -0,0 +1,499 @@ +/** +* @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 resolve = require( 'path' ).resolve; +var tape = require( 'tape' ); +var Float32Array = require( '@stdlib/array/float32' ); +var scopy = require( './../../../base/scopy' ).ndarray; +var EPS = require( '@stdlib/constants/float32/eps' ); +var abs = require( '@stdlib/math/base/special/abs' ); +var tryRequire = require( '@stdlib/utils/try-require' ); + + +// VARIABLES // + +var srot = tryRequire( resolve( __dirname, './../lib/ndarray.native.js' ) ); +var opts = { + 'skip': ( srot instanceof Error ) +}; + + +// FUNCTIONS // + +/** +* Tests for element-wise approximate equality. +* +* @private +* @param {Object} t - test object +* @param {Collection} actual - actual values +* @param {Collection} expected - expected values +* @param {number} rtol - relative tolerance +*/ +function isApprox( t, actual, expected, rtol ) { + var delta; + var tol; + var i; + + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + for ( i = 0; i < expected.length; i++ ) { + if ( actual[ i ] === expected[ i ] ) { + t.strictEqual( actual[ i ], expected[ i ], 'returns expected value' ); + } else { + delta = abs( actual[ i ] - expected[ i ] ); + tol = rtol * EPS * abs( expected[ i ] ); + t.ok( delta <= tol, 'within tolerance. actual: '+actual[ i ]+'. expected: '+expected[ i ]+'. delta: '+delta+'. tol: '+tol+'.' ); + } + } +} + + +// TESTS // + +tape( 'main export is a function', opts, function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof srot, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 9', opts, function test( t ) { + t.strictEqual( srot.length, 9, 'returns expected value' ); + t.end(); +}); + +tape( 'the function applies a plane rotation (sx=1, sy=1)', opts, function test( t ) { + var xbuf; + var ybuf; + var out; + var xe; + var ye; + var ox; + var oy; + var N; + var x; + var y; + var i; + + N = [ 0, 1, 2, 4 ]; + + xbuf = [ 0.6, 0.1, -0.5, 0.8, 0.9, -0.3, -0.4 ]; + ybuf = [ 0.5, -0.9, 0.3, 0.7, -0.6, 0.2, 0.8 ]; + + ox = [ 0, 0, 0, 0 ]; + oy = [ 0, 0, 0, 0 ]; + + xe = [ + new Float32Array( [ 0.6, 0.1, -0.5, 0.8, 0.9, -0.3, -0.4 ] ), + new Float32Array( [ 0.78, 0.1, -0.5, 0.8, 0.9, -0.3, -0.4 ] ), + new Float32Array( [ 0.78, -0.46, -0.5, 0.8, 0.9, -0.3, -0.4 ] ), + new Float32Array( [ 0.78, -0.46, -0.22, 1.06, 0.9, -0.3, -0.4 ] ) + ]; + ye = [ + new Float32Array( [ 0.5, -0.9, 0.3, 0.7, -0.6, 0.2, 0.8 ] ), + new Float32Array( [ 0.04, -0.9, 0.3, 0.7, -0.6, 0.2, 0.8 ] ), + new Float32Array( [ 0.04, -0.78, 0.3, 0.7, -0.6, 0.2, 0.8 ] ), + new Float32Array( [ 0.04, -0.78, 0.54, 0.08, -0.6, 0.2, 0.8 ] ) + ]; + + for ( i = 0; i < N.length; i++ ) { + x = new Float32Array( xbuf ); + y = new Float32Array( ybuf ); + out = srot( N[ i ], x, 1, ox[ i ], y, 1, oy[ i ], 0.8, 0.6 ); + isApprox( t, x, xe[ i ], 6.0 ); + isApprox( t, y, ye[ i ], 6.0 ); + t.strictEqual( out, y, 'returns expected value' ); + } + t.end(); +}); + +tape( 'the function applies a plane rotation (sx=2, sy=-2)', opts, function test( t ) { + var xbuf; + var ybuf; + var out; + var xe; + var ye; + var ox; + var oy; + var N; + var x; + var y; + var i; + + N = [ 0, 1, 2, 4 ]; + + xbuf = [ 0.6, 0.1, -0.5, 0.8, 0.9, -0.3, -0.4 ]; + ybuf = [ 0.5, -0.9, 0.3, 0.7, -0.6, 0.2, 0.8 ]; + + ox = [ 0, 0, 0, 0 ]; + oy = [ 0, 0, 2, 6 ]; + + xe = [ + new Float32Array( [ 0.6, 0.1, -0.5, 0.8, 0.9, -0.3, -0.4 ] ), + new Float32Array( [ 0.78, 0.1, -0.5, 0.8, 0.9, -0.3, -0.4 ] ), + new Float32Array( [ 0.66, 0.1, -0.1, 0.8, 0.9, -0.3, -0.4 ] ), + new Float32Array( [ 0.96, 0.1, -0.76, 0.8, 0.9, -0.3, -0.02 ] ) + ]; + ye = [ + new Float32Array( [ 0.5, -0.9, 0.3, 0.7, -0.6, 0.2, 0.8 ] ), + new Float32Array( [ 0.04, -0.9, 0.3, 0.7, -0.6, 0.2, 0.8 ] ), + new Float32Array( [ 0.7, -0.9, -0.12, 0.7, -0.6, 0.2, 0.8 ] ), + new Float32Array( [ 0.64, -0.9, -0.3, 0.7, -0.18, 0.2, 0.28 ] ) + ]; + + for ( i = 0; i < N.length; i++ ) { + x = new Float32Array( xbuf ); + y = new Float32Array( ybuf ); + out = srot( N[ i ], x, 2, ox[ i ], y, -2, oy[ i ], 0.8, 0.6 ); + isApprox( t, x, xe[ i ], 5.0 ); + isApprox( t, y, ye[ i ], 5.0 ); + t.strictEqual( out, y, 'returns expected value' ); + } + t.end(); +}); + +tape( 'the function applies a plane rotation (sx=-2, sy=1)', opts, function test( t ) { + var xbuf; + var ybuf; + var out; + var xe; + var ye; + var ox; + var oy; + var N; + var x; + var y; + var i; + + N = [ 0, 1, 2, 4 ]; + + xbuf = [ 0.6, 0.1, -0.5, 0.8, 0.9, -0.3, -0.4 ]; + ybuf = [ 0.5, -0.9, 0.3, 0.7, -0.6, 0.2, 0.8 ]; + + ox = [ 0, 0, 2, 6 ]; + oy = [ 0, 0, 0, 0 ]; + + xe = [ + new Float32Array( [ 0.6, 0.1, -0.5, 0.8, 0.9, -0.3, -0.4 ] ), + new Float32Array( [ 0.78, 0.1, -0.5, 0.8, 0.9, -0.3, -0.4 ] ), + new Float32Array( [ -0.06, 0.1, -0.1, 0.8, 0.9, -0.3, -0.4 ] ), + new Float32Array( [ 0.9, 0.1, -0.22, 0.8, 0.18, -0.3, -0.02 ] ) + ]; + ye = [ + new Float32Array( [ 0.5, -0.9, 0.3, 0.7, -0.6, 0.2, 0.8 ] ), + new Float32Array( [ 0.04, -0.9, 0.3, 0.7, -0.6, 0.2, 0.8 ] ), + new Float32Array( [ 0.7, -1.08, 0.3, 0.7, -0.6, 0.2, 0.8 ] ), + new Float32Array( [ 0.64, -1.26, 0.54, 0.2, -0.6, 0.2, 0.8 ] ) + ]; + for ( i = 0; i < N.length; i++ ) { + x = new Float32Array( xbuf ); + y = new Float32Array( ybuf ); + out = srot( N[ i ], x, -2, ox[ i ], y, 1, oy[ i ], 0.8, 0.6 ); + isApprox( t, x, xe[ i ], 5.0 ); + isApprox( t, y, ye[ i ], 5.0 ); + t.strictEqual( out, y, 'returns expected value' ); + } + t.end(); +}); + +tape( 'the function applies a plane rotation (sx=-1, sy=-2)', opts, function test( t ) { + var xbuf; + var ybuf; + var out; + var xe; + var ye; + var ox; + var oy; + var N; + var x; + var y; + var i; + + N = [ 0, 1, 2, 4 ]; + + xbuf = [ 0.6, 0.1, -0.5, 0.8, 0.9, -0.3, -0.4 ]; + ybuf = [ 0.5, -0.9, 0.3, 0.7, -0.6, 0.2, 0.8 ]; + + ox = [ 0, 0, 1, 3 ]; + oy = [ 0, 0, 2, 6 ]; + + xe = [ + new Float32Array( [ 0.6, 0.1, -0.5, 0.8, 0.9, -0.3, -0.4 ] ), + new Float32Array( [ 0.78, 0.1, -0.5, 0.8, 0.9, -0.3, -0.4 ] ), + new Float32Array( [ 0.78, 0.26, -0.5, 0.8, 0.9, -0.3, -0.4 ] ), + new Float32Array( [ 0.78, 0.26, -0.76, 1.12, 0.9, -0.3, -0.4 ] ) + ]; + ye = [ + new Float32Array( [ 0.5, -0.9, 0.3, 0.7, -0.6, 0.2, 0.8 ] ), + new Float32Array( [ 0.04, -0.9, 0.3, 0.7, -0.6, 0.2, 0.8 ] ), + new Float32Array( [ 0.04, -0.9, 0.18, 0.7, -0.6, 0.2, 0.8 ] ), + new Float32Array( [ 0.04, -0.9, 0.18, 0.7, -0.18, 0.2, 0.16 ] ) + ]; + + for ( i = 0; i < N.length; i++ ) { + x = new Float32Array( xbuf ); + y = new Float32Array( ybuf ); + out = srot( N[ i ], x, -1, ox[ i ], y, -2, oy[ i ], 0.8, 0.6 ); + isApprox( t, x, xe[ i ], 4.0 ); + isApprox( t, y, ye[ i ], 4.0 ); + t.strictEqual( out, y, 'returns expected value' ); + } + t.end(); +}); + +tape( 'the function supports an `x` stride', opts, function test( t ) { + var xe; + var ye; + var x; + var y; + + x = new Float32Array([ + 1.0, // 0 + 2.0, + 3.0, // 1 + 4.0, + 5.0 + ]); + y = new Float32Array([ + 6.0, // 0 + 7.0, // 1 + 8.0, + 9.0, + 10.0 + ]); + + srot( 2, x, 2, 0, y, 1, 0, 0.8, 0.6 ); + + xe = new Float32Array( [ 4.4, 2.0, 6.6, 4.0, 5.0 ] ); + ye = new Float32Array( [ 4.2, 3.8, 8.0, 9.0, 10.0 ] ); + + isApprox( t, x, xe, 2.0 ); + isApprox( t, y, ye, 2.0 ); + + t.end(); +}); + +tape( 'the function supports an `x` offset', opts, function test( t ) { + var xe; + var ye; + var x; + var y; + + x = new Float32Array([ + 1.0, + 2.0, // 0 + 3.0, // 1 + 4.0, + 5.0 + ]); + y = new Float32Array([ + 6.0, // 0 + 7.0, // 1 + 8.0, + 9.0, + 10.0 + ]); + + srot( 2, x, 1, 1, y, 1, 0, 0.8, 0.6 ); + + xe = new Float32Array( [ 1.0, 5.2, 6.6, 4.0, 5.0 ] ); + ye = new Float32Array( [ 3.6, 3.8, 8.0, 9.0, 10.0 ] ); + + isApprox( t, x, xe, 2.0 ); + isApprox( t, y, ye, 2.0 ); + + t.end(); +}); + +tape( 'the function supports a `y` stride', opts, function test( t ) { + var xe; + var ye; + var x; + var y; + + x = new Float32Array([ + 1.0, // 0 + 2.0, // 1 + 3.0, // 2 + 4.0, + 5.0 + ]); + y = new Float32Array([ + 1.0, // 0 + 2.0, + 3.0, // 1 + 4.0, + 5.0 // 2 + ]); + + srot( 3, x, 1, 0, y, 2, 0, 0.0, -1.0 ); + + xe = new Float32Array( [ -1.0, -3.0, -5.0, 4.0, 5.0 ] ); + ye = new Float32Array( [ 1.0, 2.0, 2.0, 4.0, 3.0 ] ); + + isApprox( t, x, xe, 1.0 ); + isApprox( t, y, ye, 1.0 ); + + t.end(); +}); + +tape( 'the function supports a `y` offset', opts, function test( t ) { + var xe; + var ye; + var x; + var y; + + x = new Float32Array([ + 1.0, // 0 + 2.0, // 1 + 3.0, + 4.0, + 5.0 + ]); + y = new Float32Array([ + 6.0, + 7.0, + 8.0, // 0 + 9.0, // 1 + 10.0 + ]); + + srot( 2, x, 1, 0, y, 1, 2, 0.8, 0.6 ); + + xe = new Float32Array( [ 5.6, 7.0, 3.0, 4.0, 5.0 ] ); + ye = new Float32Array( [ 6.0, 7.0, 5.8, 6.0, 10.0 ] ); + + isApprox( t, x, xe, 1.0 ); + isApprox( t, y, ye, 1.0 ); + + t.end(); +}); + +tape( 'the function returns a reference to the second input array', opts, function test( t ) { + var out; + var x; + var y; + + x = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); + y = new Float32Array( [ 6.0, 7.0, 8.0, 9.0, 10.0 ] ); + + out = srot( x.length, x, 1, 0, y, 1, 0, 1, 0 ); + + t.strictEqual( out, y, 'returns expected value' ); + t.end(); +}); + +tape( 'if provided an `N` parameter less than or equal to `0`, the function leaves both input arrays unchanged', opts, function test( t ) { + var xe; + var ye; + var x; + var y; + + x = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); + y = new Float32Array( [ 6.0, 7.0, 8.0, 9.0, 10.0 ] ); + + xe = new Float32Array( x.length ); + scopy( x.length, x, 1, 0, xe, 1, 0 ); + + ye = new Float32Array( y.length ); + scopy( y.length, y, 1, 0, ye, 1, 0 ); + + srot( -1, x, 1, 0, y, 1, 0, 0.8, 0.6 ); + t.deepEqual( x, xe, 'returns expected value' ); + t.deepEqual( y, ye, 'returns expected value' ); + + srot( 0, x, 1, 0, y, 1, 0, 0.8, 0.6 ); + t.deepEqual( x, xe, 'returns expected value' ); + t.deepEqual( y, ye, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports negative strides', opts, function test( t ) { + var xe; + var ye; + var x; + var y; + + x = new Float32Array([ + 0.6, // 3 + 0.1, + -0.5, // 2 + 0.8, + 0.9, // 1 + -0.3, + -0.4 // 0 + ]); + y = new Float32Array([ + 0.5, // 0 + -0.9, // 1 + 0.3, // 2 + 0.7, // 3 + -0.6, + 0.2, + 0.8 + ]); + + srot( 4, x, -2, 6, y, 1, 0, 0.8, 0.6 ); + + xe = new Float32Array( [ 0.9, 0.1, -0.22, 0.8, 0.18, -0.3, -0.02 ] ); + ye = new Float32Array( [ 0.64, -1.26, 0.54, 0.2, -0.6, 0.2, 0.8 ] ); + + isApprox( t, x, xe, 5.0 ); + isApprox( t, y, ye, 5.0 ); + + t.end(); +}); + +tape( 'the function supports complex access patterns', opts, function test( t ) { + var xe; + var ye; + var x; + var y; + + x = new Float32Array([ + 0.6, // 1 + 0.1, // 0 + -0.5, + 0.8, + 0.9, + -0.3, + -0.4 + ]); + y = new Float32Array([ + 0.5, // 1 + -0.9, + 0.3, // 0 + 0.7, + -0.6, + 0.2, + 0.8 + ]); + + srot( 2, x, -1, 1, y, -2, 2, 0.8, 0.6 ); + + xe = new Float32Array( [ 0.78, 0.26, -0.5, 0.8, 0.9, -0.3, -0.4 ] ); + ye = new Float32Array( [ 0.04, -0.9, 0.18, 0.7, -0.6, 0.2, 0.8 ] ); + + isApprox( t, x, xe, 5.0 ); + isApprox( t, y, ye, 5.0 ); + + t.end(); +}); diff --git a/base/srot/test/test.srot.js b/base/srot/test/test.srot.js new file mode 100644 index 000000000..3d49b9365 --- /dev/null +++ b/base/srot/test/test.srot.js @@ -0,0 +1,401 @@ +/** +* @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 Float32Array = require( '@stdlib/array/float32' ); +var scopy = require( './../../../base/scopy' ); +var EPS = require( '@stdlib/constants/float32/eps' ); +var abs = require( '@stdlib/math/base/special/abs' ); +var srot = require( './../lib/srot.js' ); + + +// FUNCTIONS // + +/** +* Tests for element-wise approximate equality. +* +* @private +* @param {Object} t - test object +* @param {Collection} actual - actual values +* @param {Collection} expected - expected values +* @param {number} rtol - relative tolerance +*/ +function isApprox( t, actual, expected, rtol ) { + var delta; + var tol; + var i; + + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + for ( i = 0; i < expected.length; i++ ) { + if ( actual[ i ] === expected[ i ] ) { + t.strictEqual( actual[ i ], expected[ i ], 'returns expected value' ); + } else { + delta = abs( actual[ i ] - expected[ i ] ); + tol = rtol * EPS * abs( expected[ i ] ); + t.ok( delta <= tol, 'within tolerance. actual: '+actual[ i ]+'. expected: '+expected[ i ]+'. delta: '+delta+'. tol: '+tol+'.' ); + } + } +} + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof srot, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 7', function test( t ) { + t.strictEqual( srot.length, 7, 'returns expected value' ); + t.end(); +}); + +tape( 'the function applies a plane rotation (sx=1, sy=1)', function test( t ) { + var xbuf; + var ybuf; + var out; + var xe; + var ye; + var N; + var x; + var y; + var i; + + N = [ 0, 1, 2, 4 ]; + + xbuf = [ 0.6, 0.1, -0.5, 0.8, 0.9, -0.3, -0.4 ]; + ybuf = [ 0.5, -0.9, 0.3, 0.7, -0.6, 0.2, 0.8]; + + xe = [ + new Float32Array( [ 0.6, 0.1, -0.5, 0.8, 0.9, -0.3, -0.4 ] ), + new Float32Array( [ 0.78, 0.1, -0.5, 0.8, 0.9, -0.3, -0.4 ] ), + new Float32Array( [ 0.78, -0.46, -0.5, 0.8, 0.9, -0.3, -0.4 ] ), + new Float32Array( [ 0.78, -0.46, -0.22, 1.06, 0.9, -0.3, -0.4 ] ) + ]; + ye = [ + new Float32Array( [ 0.5, -0.9, 0.3, 0.7, -0.6, 0.2, 0.8 ] ), + new Float32Array( [ 0.04, -0.9, 0.3, 0.7, -0.6, 0.2, 0.8 ] ), + new Float32Array( [ 0.04, -0.78, 0.3, 0.7, -0.6, 0.2, 0.8 ] ), + new Float32Array( [ 0.04, -0.78, 0.54, 0.08, -0.6, 0.2, 0.8 ] ) + ]; + for ( i = 0; i < N.length; i++ ) { + x = new Float32Array( xbuf ); + y = new Float32Array( ybuf ); + out = srot( N[ i ], x, 1, y, 1, 0.8, 0.6 ); + isApprox( t, x, xe[ i ], 6.0 ); + isApprox( t, y, ye[ i ], 6.0 ); + t.strictEqual( out, y, 'returns expected value' ); + } + t.end(); +}); + +tape( 'the function applies a plane rotation (sx=2, sy=-2)', function test( t ) { + var xbuf; + var ybuf; + var out; + var xe; + var ye; + var N; + var x; + var y; + var i; + + N = [ 0, 1, 2, 4 ]; + + xbuf = [ 0.6, 0.1, -0.5, 0.8, 0.9, -0.3, -0.4 ]; + ybuf = [ 0.5, -0.9, 0.3, 0.7, -0.6, 0.2, 0.8 ]; + + xe = [ + new Float32Array( [ 0.6, 0.1, -0.5, 0.8, 0.9, -0.3, -0.4 ] ), + new Float32Array( [ 0.78, 0.1, -0.5, 0.8, 0.9, -0.3, -0.4 ] ), + new Float32Array( [ 0.66, 0.1, -0.1, 0.8, 0.9, -0.3, -0.4 ] ), + new Float32Array( [ 0.96, 0.1, -0.76, 0.8, 0.9, -0.3, -0.02 ] ) + ]; + ye = [ + new Float32Array( [ 0.5, -0.9, 0.3, 0.7, -0.6, 0.2, 0.8 ] ), + new Float32Array( [ 0.04, -0.9, 0.3, 0.7, -0.6, 0.2, 0.8 ] ), + new Float32Array( [ 0.7, -0.9, -0.12, 0.7, -0.6, 0.2, 0.8 ] ), + new Float32Array( [ 0.64, -0.9, -0.3, 0.7, -0.18, 0.2, 0.28 ] ) + ]; + for ( i = 0; i < N.length; i++ ) { + x = new Float32Array( xbuf ); + y = new Float32Array( ybuf ); + out = srot( N[ i ], x, 2, y, -2, 0.8, 0.6 ); + isApprox( t, x, xe[ i ], 5.0 ); + isApprox( t, y, ye[ i ], 5.0 ); + t.strictEqual( out, y, 'returns expected value' ); + } + t.end(); +}); + +tape( 'the function applies a plane rotation (sx=-2, sy=1)', function test( t ) { + var xbuf; + var ybuf; + var out; + var xe; + var ye; + var N; + var x; + var y; + var i; + + N = [ 0, 1, 2, 4 ]; + + xbuf = [ 0.6, 0.1, -0.5, 0.8, 0.9, -0.3, -0.4 ]; + ybuf = [ 0.5, -0.9, 0.3, 0.7, -0.6, 0.2, 0.8]; + + xe = [ + new Float32Array( [ 0.6, 0.1, -0.5, 0.8, 0.9, -0.3, -0.4 ] ), + new Float32Array( [ 0.78, 0.1, -0.5, 0.8, 0.9, -0.3, -0.4 ] ), + new Float32Array( [ -0.06, 0.1, -0.1, 0.8, 0.9, -0.3, -0.4 ] ), + new Float32Array( [ 0.9, 0.1, -0.22, 0.8, 0.18, -0.3, -0.02 ] ) + ]; + ye = [ + new Float32Array( [ 0.5, -0.9, 0.3, 0.7, -0.6, 0.2, 0.8 ] ), + new Float32Array( [ 0.04, -0.9, 0.3, 0.7, -0.6, 0.2, 0.8 ] ), + new Float32Array( [ 0.7, -1.08, 0.3, 0.7, -0.6, 0.2, 0.8 ] ), + new Float32Array( [ 0.64, -1.26, 0.54, 0.2, -0.6, 0.2, 0.8 ] ) + ]; + for ( i = 0; i < N.length; i++ ) { + x = new Float32Array( xbuf ); + y = new Float32Array( ybuf ); + out = srot( N[ i ], x, -2, y, 1, 0.8, 0.6 ); + isApprox( t, x, xe[ i ], 5.0 ); + isApprox( t, y, ye[ i ], 5.0 ); + t.strictEqual( out, y, 'returns expected value' ); + } + t.end(); +}); + +tape( 'the function applies a plane rotation (sx=-1, sy=-2)', function test( t ) { + var xbuf; + var ybuf; + var out; + var xe; + var ye; + var N; + var x; + var y; + var i; + + N = [ 0, 1, 2, 4 ]; + + xbuf = [ 0.6, 0.1, -0.5, 0.8, 0.9, -0.3, -0.4 ]; + ybuf = [ 0.5, -0.9, 0.3, 0.7, -0.6, 0.2, 0.8 ]; + + xe = [ + new Float32Array( [ 0.6, 0.1, -0.5, 0.8, 0.9, -0.3, -0.4 ] ), + new Float32Array( [ 0.78, 0.1, -0.5, 0.8, 0.9, -0.3, -0.4 ] ), + new Float32Array( [ 0.78, 0.26, -0.5, 0.8, 0.9, -0.3, -0.4 ] ), + new Float32Array( [ 0.78, 0.26, -0.76, 1.12, 0.9, -0.3, -0.4 ] ) + ]; + ye = [ + new Float32Array( [ 0.5, -0.9, 0.3, 0.7, -0.6, 0.2, 0.8 ] ), + new Float32Array( [ 0.04, -0.9, 0.3, 0.7, -0.6, 0.2, 0.8 ] ), + new Float32Array( [ 0.04, -0.9, 0.18, 0.7, -0.6, 0.2, 0.8 ] ), + new Float32Array( [ 0.04, -0.9, 0.18, 0.7, -0.18, 0.2, 0.16 ] ) + ]; + for ( i = 0; i < N.length; i++ ) { + x = new Float32Array( xbuf ); + y = new Float32Array( ybuf ); + out = srot( N[ i ], x, -1, y, -2, 0.8, 0.6 ); + isApprox( t, x, xe[ i ], 5.0 ); + isApprox( t, y, ye[ i ], 5.0 ); + t.strictEqual( out, y, 'returns expected value' ); + } + t.end(); +}); + +tape( 'the function supports an `x` stride', function test( t ) { + var xe; + var ye; + var x; + var y; + + x = new Float32Array([ + 1.0, // 0 + 2.0, + 3.0, // 1 + 4.0, + 5.0 + ]); + y = new Float32Array([ + 6.0, // 0 + 7.0, // 1 + 8.0, + 9.0, + 10.0 + ]); + + srot( 2, x, 2, y, 1, 0.8, 0.6 ); + + xe = new Float32Array( [ 4.4, 2.0, 6.6, 4.0, 5.0 ] ); + ye = new Float32Array( [ 4.2, 3.8, 8.0, 9.0, 10.0 ] ); + + isApprox( t, x, xe, 2.0 ); + isApprox( t, y, ye, 2.0 ); + + t.end(); +}); + +tape( 'the function supports a `y` stride', function test( t ) { + var xe; + var ye; + var x; + var y; + + x = new Float32Array([ + 1.0, // 0 + 2.0, // 1 + 3.0, // 2 + 4.0, + 5.0 + ]); + y = new Float32Array([ + 1.0, // 0 + 2.0, + 3.0, // 1 + 4.0, + 5.0 // 2 + ]); + + srot( 3, x, 1, y, 2, 0.0, -1.0 ); + + xe = new Float32Array( [ -1.0, -3.0, -5.0, 4.0, 5.0 ] ); + ye = new Float32Array( [ 1.0, 2.0, 2.0, 4.0, 3.0 ] ); + t.deepEqual( x, xe, 'returns expected value' ); + t.deepEqual( y, ye, 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns a reference to the second input array', function test( t ) { + var out; + var x; + var y; + + x = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); + y = new Float32Array( [ 6.0, 7.0, 8.0, 9.0, 10.0 ] ); + + out = srot( x.length, x, 1, y, 1, 1, 0 ); + + t.strictEqual( out, y, 'returns expected value' ); + t.end(); +}); + +tape( 'if provided an `N` parameter less than or equal to `0`, the function leaves both input arrays unchanged', function test( t ) { + var xe; + var ye; + var x; + var y; + + x = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); + y = new Float32Array( [ 6.0, 7.0, 8.0, 9.0, 10.0 ] ); + + xe = new Float32Array( x.length ); + scopy( x.length, x, 1, xe, 1 ); + + ye = new Float32Array( y.length ); + scopy( y.length, y, 1, ye, 1 ); + + srot( -1, x, 1, y, 1, 0.8, 0.6 ); + t.deepEqual( x, xe, 'returns expected value' ); + t.deepEqual( y, ye, 'returns expected value' ); + + srot( 0, x, 1, y, 1, 0.8, 0.6 ); + t.deepEqual( x, xe, 'returns expected value' ); + t.deepEqual( y, ye, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports negative strides', function test( t ) { + var xe; + var ye; + var x; + var y; + + x = new Float32Array([ + 0.6, // 3 + 0.1, + -0.5, // 2 + 0.8, + 0.9, // 1 + -0.3, + -0.4 // 0 + ]); + y = new Float32Array([ + 0.5, // 0 + -0.9, // 1 + 0.3, // 2 + 0.7, // 3 + -0.6, + 0.2, + 0.8 + ]); + + srot( 4, x, -2, y, 1, 0.8, 0.6 ); + + xe = new Float32Array( [ 0.9, 0.1, -0.22, 0.8, 0.18, -0.3, -0.02 ] ); + ye = new Float32Array( [ 0.64, -1.26, 0.54, 0.2, -0.6, 0.2, 0.8 ] ); + + isApprox( t, x, xe, 5.0 ); + isApprox( t, y, ye, 5.0 ); + + t.end(); +}); + +tape( 'the function supports complex access patterns', function test( t ) { + var xe; + var ye; + var x; + var y; + + x = new Float32Array([ + 0.6, // 1 + 0.1, // 0 + -0.5, + 0.8, + 0.9, + -0.3, + -0.4 + ]); + y = new Float32Array([ + 0.5, // 1 + -0.9, + 0.3, // 0 + 0.7, + -0.6, + 0.2, + 0.8 + ]); + + srot( 2, x, -1, y, -2, 0.8, 0.6 ); + + xe = new Float32Array( [ 0.78, 0.26, -0.5, 0.8, 0.9, -0.3, -0.4 ] ); + ye = new Float32Array( [ 0.04, -0.9, 0.18, 0.7, -0.6, 0.2, 0.8 ] ); + + isApprox( t, x, xe, 10.0 ); + isApprox( t, y, ye, 10.0 ); + + t.end(); +}); diff --git a/base/srot/test/test.srot.native.js b/base/srot/test/test.srot.native.js new file mode 100644 index 000000000..976eac17f --- /dev/null +++ b/base/srot/test/test.srot.native.js @@ -0,0 +1,410 @@ +/** +* @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 resolve = require( 'path' ).resolve; +var tape = require( 'tape' ); +var Float32Array = require( '@stdlib/array/float32' ); +var scopy = require( './../../../base/scopy' ); +var EPS = require( '@stdlib/constants/float32/eps' ); +var abs = require( '@stdlib/math/base/special/abs' ); +var tryRequire = require( '@stdlib/utils/try-require' ); + + +// VARIABLES // + +var srot = tryRequire( resolve( __dirname, './../lib/srot.native.js' ) ); +var opts = { + 'skip': ( srot instanceof Error ) +}; + + +// FUNCTIONS // + +/** +* Tests for element-wise approximate equality. +* +* @private +* @param {Object} t - test object +* @param {Collection} actual - actual values +* @param {Collection} expected - expected values +* @param {number} rtol - relative tolerance +*/ +function isApprox( t, actual, expected, rtol ) { + var delta; + var tol; + var i; + + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + for ( i = 0; i < expected.length; i++ ) { + if ( actual[ i ] === expected[ i ] ) { + t.strictEqual( actual[ i ], expected[ i ], 'returns expected value' ); + } else { + delta = abs( actual[ i ] - expected[ i ] ); + tol = rtol * EPS * abs( expected[ i ] ); + t.ok( delta <= tol, 'within tolerance. actual: '+actual[ i ]+'. expected: '+expected[ i ]+'. delta: '+delta+'. tol: '+tol+'.' ); + } + } +} + + +// TESTS // + +tape( 'main export is a function', opts, function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof srot, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 7', opts, function test( t ) { + t.strictEqual( srot.length, 7, 'returns expected value' ); + t.end(); +}); + +tape( 'the function applies a plane rotation (sx=1, sy=1)', opts, function test( t ) { + var xbuf; + var ybuf; + var out; + var xe; + var ye; + var N; + var x; + var y; + var i; + + N = [ 0, 1, 2, 4 ]; + + xbuf = [ 0.6, 0.1, -0.5, 0.8, 0.9, -0.3, -0.4 ]; + ybuf = [ 0.5, -0.9, 0.3, 0.7, -0.6, 0.2, 0.8]; + + xe = [ + new Float32Array( [ 0.6, 0.1, -0.5, 0.8, 0.9, -0.3, -0.4 ] ), + new Float32Array( [ 0.78, 0.1, -0.5, 0.8, 0.9, -0.3, -0.4 ] ), + new Float32Array( [ 0.78, -0.46, -0.5, 0.8, 0.9, -0.3, -0.4 ] ), + new Float32Array( [ 0.78, -0.46, -0.22, 1.06, 0.9, -0.3, -0.4 ] ) + ]; + ye = [ + new Float32Array( [ 0.5, -0.9, 0.3, 0.7, -0.6, 0.2, 0.8 ] ), + new Float32Array( [ 0.04, -0.9, 0.3, 0.7, -0.6, 0.2, 0.8 ] ), + new Float32Array( [ 0.04, -0.78, 0.3, 0.7, -0.6, 0.2, 0.8 ] ), + new Float32Array( [ 0.04, -0.78, 0.54, 0.08, -0.6, 0.2, 0.8 ] ) + ]; + for ( i = 0; i < N.length; i++ ) { + x = new Float32Array( xbuf ); + y = new Float32Array( ybuf ); + out = srot( N[ i ], x, 1, y, 1, 0.8, 0.6 ); + isApprox( t, x, xe[ i ], 6.0 ); + isApprox( t, y, ye[ i ], 6.0 ); + t.strictEqual( out, y, 'returns expected value' ); + } + t.end(); +}); + +tape( 'the function applies a plane rotation (sx=2, sy=-2)', opts, function test( t ) { + var xbuf; + var ybuf; + var out; + var xe; + var ye; + var N; + var x; + var y; + var i; + + N = [ 0, 1, 2, 4 ]; + + xbuf = [ 0.6, 0.1, -0.5, 0.8, 0.9, -0.3, -0.4 ]; + ybuf = [ 0.5, -0.9, 0.3, 0.7, -0.6, 0.2, 0.8 ]; + + xe = [ + new Float32Array( [ 0.6, 0.1, -0.5, 0.8, 0.9, -0.3, -0.4 ] ), + new Float32Array( [ 0.78, 0.1, -0.5, 0.8, 0.9, -0.3, -0.4 ] ), + new Float32Array( [ 0.66, 0.1, -0.1, 0.8, 0.9, -0.3, -0.4 ] ), + new Float32Array( [ 0.96, 0.1, -0.76, 0.8, 0.9, -0.3, -0.02 ] ) + ]; + ye = [ + new Float32Array( [ 0.5, -0.9, 0.3, 0.7, -0.6, 0.2, 0.8 ] ), + new Float32Array( [ 0.04, -0.9, 0.3, 0.7, -0.6, 0.2, 0.8 ] ), + new Float32Array( [ 0.7, -0.9, -0.12, 0.7, -0.6, 0.2, 0.8 ] ), + new Float32Array( [ 0.64, -0.9, -0.3, 0.7, -0.18, 0.2, 0.28 ] ) + ]; + for ( i = 0; i < N.length; i++ ) { + x = new Float32Array( xbuf ); + y = new Float32Array( ybuf ); + out = srot( N[ i ], x, 2, y, -2, 0.8, 0.6 ); + isApprox( t, x, xe[ i ], 5.0 ); + isApprox( t, y, ye[ i ], 5.0 ); + t.strictEqual( out, y, 'returns expected value' ); + } + t.end(); +}); + +tape( 'the function applies a plane rotation (sx=-2, sy=1)', opts, function test( t ) { + var xbuf; + var ybuf; + var out; + var xe; + var ye; + var N; + var x; + var y; + var i; + + N = [ 0, 1, 2, 4 ]; + + xbuf = [ 0.6, 0.1, -0.5, 0.8, 0.9, -0.3, -0.4 ]; + ybuf = [ 0.5, -0.9, 0.3, 0.7, -0.6, 0.2, 0.8]; + + xe = [ + new Float32Array( [ 0.6, 0.1, -0.5, 0.8, 0.9, -0.3, -0.4 ] ), + new Float32Array( [ 0.78, 0.1, -0.5, 0.8, 0.9, -0.3, -0.4 ] ), + new Float32Array( [ -0.06, 0.1, -0.1, 0.8, 0.9, -0.3, -0.4 ] ), + new Float32Array( [ 0.9, 0.1, -0.22, 0.8, 0.18, -0.3, -0.02 ] ) + ]; + ye = [ + new Float32Array( [ 0.5, -0.9, 0.3, 0.7, -0.6, 0.2, 0.8 ] ), + new Float32Array( [ 0.04, -0.9, 0.3, 0.7, -0.6, 0.2, 0.8 ] ), + new Float32Array( [ 0.7, -1.08, 0.3, 0.7, -0.6, 0.2, 0.8 ] ), + new Float32Array( [ 0.64, -1.26, 0.54, 0.2, -0.6, 0.2, 0.8 ] ) + ]; + for ( i = 0; i < N.length; i++ ) { + x = new Float32Array( xbuf ); + y = new Float32Array( ybuf ); + out = srot( N[ i ], x, -2, y, 1, 0.8, 0.6 ); + isApprox( t, x, xe[ i ], 5.0 ); + isApprox( t, y, ye[ i ], 5.0 ); + t.strictEqual( out, y, 'returns expected value' ); + } + t.end(); +}); + +tape( 'the function applies a plane rotation (sx=-1, sy=-2)', opts, function test( t ) { + var xbuf; + var ybuf; + var out; + var xe; + var ye; + var N; + var x; + var y; + var i; + + N = [ 0, 1, 2, 4 ]; + + xbuf = [ 0.6, 0.1, -0.5, 0.8, 0.9, -0.3, -0.4 ]; + ybuf = [ 0.5, -0.9, 0.3, 0.7, -0.6, 0.2, 0.8 ]; + + xe = [ + new Float32Array( [ 0.6, 0.1, -0.5, 0.8, 0.9, -0.3, -0.4 ] ), + new Float32Array( [ 0.78, 0.1, -0.5, 0.8, 0.9, -0.3, -0.4 ] ), + new Float32Array( [ 0.78, 0.26, -0.5, 0.8, 0.9, -0.3, -0.4 ] ), + new Float32Array( [ 0.78, 0.26, -0.76, 1.12, 0.9, -0.3, -0.4 ] ) + ]; + ye = [ + new Float32Array( [ 0.5, -0.9, 0.3, 0.7, -0.6, 0.2, 0.8 ] ), + new Float32Array( [ 0.04, -0.9, 0.3, 0.7, -0.6, 0.2, 0.8 ] ), + new Float32Array( [ 0.04, -0.9, 0.18, 0.7, -0.6, 0.2, 0.8 ] ), + new Float32Array( [ 0.04, -0.9, 0.18, 0.7, -0.18, 0.2, 0.16 ] ) + ]; + for ( i = 0; i < N.length; i++ ) { + x = new Float32Array( xbuf ); + y = new Float32Array( ybuf ); + out = srot( N[ i ], x, -1, y, -2, 0.8, 0.6 ); + isApprox( t, x, xe[ i ], 5.0 ); + isApprox( t, y, ye[ i ], 5.0 ); + t.strictEqual( out, y, 'returns expected value' ); + } + t.end(); +}); + +tape( 'the function supports an `x` stride', opts, function test( t ) { + var xe; + var ye; + var x; + var y; + + x = new Float32Array([ + 1.0, // 0 + 2.0, + 3.0, // 1 + 4.0, + 5.0 + ]); + y = new Float32Array([ + 6.0, // 0 + 7.0, // 1 + 8.0, + 9.0, + 10.0 + ]); + + srot( 2, x, 2, y, 1, 0.8, 0.6 ); + + xe = new Float32Array( [ 4.4, 2.0, 6.6, 4.0, 5.0 ] ); + ye = new Float32Array( [ 4.2, 3.8, 8.0, 9.0, 10.0 ] ); + + isApprox( t, x, xe, 2.0 ); + isApprox( t, y, ye, 2.0 ); + + t.end(); +}); + +tape( 'the function supports a `y` stride', opts, function test( t ) { + var xe; + var ye; + var x; + var y; + + x = new Float32Array([ + 1.0, // 0 + 2.0, // 1 + 3.0, // 2 + 4.0, + 5.0 + ]); + y = new Float32Array([ + 1.0, // 0 + 2.0, + 3.0, // 1 + 4.0, + 5.0 // 2 + ]); + + srot( 3, x, 1, y, 2, 0.0, -1.0 ); + + xe = new Float32Array( [ -1.0, -3.0, -5.0, 4.0, 5.0 ] ); + ye = new Float32Array( [ 1.0, 2.0, 2.0, 4.0, 3.0 ] ); + t.deepEqual( x, xe, 'returns expected value' ); + t.deepEqual( y, ye, 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns a reference to the second input array', opts, function test( t ) { + var out; + var x; + var y; + + x = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); + y = new Float32Array( [ 6.0, 7.0, 8.0, 9.0, 10.0 ] ); + + out = srot( x.length, x, 1, y, 1, 1, 0 ); + + t.strictEqual( out, y, 'returns expected value' ); + t.end(); +}); + +tape( 'if provided an `N` parameter less than or equal to `0`, the function leaves both input arrays unchanged', opts, function test( t ) { + var xe; + var ye; + var x; + var y; + + x = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); + y = new Float32Array( [ 6.0, 7.0, 8.0, 9.0, 10.0 ] ); + + xe = new Float32Array( x.length ); + scopy( x.length, x, 1, xe, 1 ); + + ye = new Float32Array( y.length ); + scopy( y.length, y, 1, ye, 1 ); + + srot( -1, x, 1, y, 1, 0.8, 0.6 ); + t.deepEqual( x, xe, 'returns expected value' ); + t.deepEqual( y, ye, 'returns expected value' ); + + srot( 0, x, 1, y, 1, 0.8, 0.6 ); + t.deepEqual( x, xe, 'returns expected value' ); + t.deepEqual( y, ye, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports negative strides', opts, function test( t ) { + var xe; + var ye; + var x; + var y; + + x = new Float32Array([ + 0.6, // 3 + 0.1, + -0.5, // 2 + 0.8, + 0.9, // 1 + -0.3, + -0.4 // 0 + ]); + y = new Float32Array([ + 0.5, // 0 + -0.9, // 1 + 0.3, // 2 + 0.7, // 3 + -0.6, + 0.2, + 0.8 + ]); + + srot( 4, x, -2, y, 1, 0.8, 0.6 ); + + xe = new Float32Array( [ 0.9, 0.1, -0.22, 0.8, 0.18, -0.3, -0.02 ] ); + ye = new Float32Array( [ 0.64, -1.26, 0.54, 0.2, -0.6, 0.2, 0.8 ] ); + + isApprox( t, x, xe, 5.0 ); + isApprox( t, y, ye, 5.0 ); + + t.end(); +}); + +tape( 'the function supports complex access patterns', opts, function test( t ) { + var xe; + var ye; + var x; + var y; + + x = new Float32Array([ + 0.6, // 1 + 0.1, // 0 + -0.5, + 0.8, + 0.9, + -0.3, + -0.4 + ]); + y = new Float32Array([ + 0.5, // 1 + -0.9, + 0.3, // 0 + 0.7, + -0.6, + 0.2, + 0.8 + ]); + + srot( 2, x, -1, y, -2, 0.8, 0.6 ); + + xe = new Float32Array( [ 0.78, 0.26, -0.5, 0.8, 0.9, -0.3, -0.4 ] ); + ye = new Float32Array( [ 0.04, -0.9, 0.18, 0.7, -0.6, 0.2, 0.8 ] ); + + isApprox( t, x, xe, 10.0 ); + isApprox( t, y, ye, 10.0 ); + + t.end(); +});