diff --git a/lib/node_modules/@stdlib/simulate/strided/cosine-wave/README.md b/lib/node_modules/@stdlib/simulate/strided/cosine-wave/README.md new file mode 100644 index 000000000000..c8eb02544e5a --- /dev/null +++ b/lib/node_modules/@stdlib/simulate/strided/cosine-wave/README.md @@ -0,0 +1,193 @@ + + +# cosineWave + +> Generate a cosine wave in a strided array. + + + +
+ +A cosine waveform is represented by the following equation + + + +```math +f(t; \tau, a, \varphi) = a \cdot \cos \frac{2\pi(t-\varphi)}{\tau} +``` + + + + + +where `τ` is the period, `a` is the peak amplitude, and `φ` is the phase offset. + + + +
+ + + + + +
+ +## Usage + +```javascript +var cosineWave = require( '@stdlib/simulate/strided/cosine-wave' ); +``` + +#### cosineWave( N, period, amplitude, phaseOffset, x, strideX ) + +Generates a cosine wave in a strided array. + +```javascript +var x = [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ]; + +cosineWave( x.length, 4, 1.0, 0, x, 1 ); +// x => [ 1.0, 0.0, -1.0, 0.0, 1.0, 0.0 ] +``` + +The function has the following parameters: + +- **N**: number of indexed elements. +- **period**: period (i.e., the number of elements before a cosine wave repeats). +- **amplitude**: peak amplitude. +- **phaseOffset**: phase offset (in units of elements; zero-based). A negative offset translates a waveform to the left. A positive offset translates a waveform to the right. +- **x**: input [`Array`][mdn-array] or [`typed array`][mdn-typed-array]. +- **strideX**: stride length. + +The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to generate a cosine wave in every other element: + +```javascript +var x = [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ]; + +cosineWave( 3, 4, 1.0, 0, x, 2 ); +// x => [ 1.0, -2.0, 0.0, -4.0, -1.0, -6.0 ] +``` + +Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views. + +```javascript +var Float64Array = require( '@stdlib/array/float64' ); + +// Initial array... +var x0 = new Float64Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] ); + +// Create an offset view... +var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element + +// Generate a cosine wave in every other element... +cosineWave( x1.length, 4, 1.0, 0, x1, 2 ); +// x0 => [ 1.0, 1.0, 3.0, 0.0, 5.0, -1.0 ] +``` + +#### cosineWave.ndarray( N, period, amplitude, phaseOffset, x, strideX, offsetX ) + +Generates a cosine wave in a strided array using alternative indexing semantics. + +```javascript +var x = [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ]; + +cosineWave.ndarray( x.length, 4, 1.0, 0, x, 1, 0 ); +// x => [ 1.0, 0.0, -1.0, 0.0, 1.0, 0.0 ] +``` + +The function has the following additional parameters: + +- **offsetX**: starting index. + +While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameter supports indexing semantics based on a starting index. For example, to access only the last three elements: + +```javascript +var x = [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ]; + +cosineWave.ndarray( 3, 4, 1.0, 0, x, 1, x.length-3 ); +// x => [ 1.0, -2.0, 3.0, 1.0, 0.0, -1.0 ] +``` + +
+ + + +
+ +## Notes + +- If `N <= 0`, both functions return `x` unchanged. +- Both functions support array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor]). + +
+ + + +
+ +## Examples + + + +```javascript +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var cosineWave = require( '@stdlib/simulate/strided/cosine-wave' ); + +var x = discreteUniform( 10, -100, 100, { + 'dtype': 'float64' +}); +console.log( x ); + +cosineWave( x.length, 4, 1.0, 0, x, 1 ); +console.log( x ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/simulate/strided/cosine-wave/benchmark/benchmark.js b/lib/node_modules/@stdlib/simulate/strided/cosine-wave/benchmark/benchmark.js new file mode 100644 index 000000000000..c475340937c7 --- /dev/null +++ b/lib/node_modules/@stdlib/simulate/strided/cosine-wave/benchmark/benchmark.js @@ -0,0 +1,103 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var uniform = require( '@stdlib/random/array/uniform' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var cosineWave = require( './../lib/main.js' ); + + +// VARIABLES // + +var options = { + 'dtype': 'generic' +}; + + +// FUNCTIONS // + +/** +* Create a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var x = uniform( len, -100, 100, options ); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var y; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + y = cosineWave( x.length, 4, 1.0, 0, x, 1 ); + if ( isnan( y[ i%x.length ] ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( y[ 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( format( '%s:len=%d', pkg, len ), f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/simulate/strided/cosine-wave/benchmark/benchmark.ndarray.js b/lib/node_modules/@stdlib/simulate/strided/cosine-wave/benchmark/benchmark.ndarray.js new file mode 100644 index 000000000000..5a1635b77099 --- /dev/null +++ b/lib/node_modules/@stdlib/simulate/strided/cosine-wave/benchmark/benchmark.ndarray.js @@ -0,0 +1,103 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var uniform = require( '@stdlib/random/array/uniform' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var cosineWave = require( './../lib/ndarray.js' ); + + +// VARIABLES // + +var options = { + 'dtype': 'generic' +}; + + +// FUNCTIONS // + +/** +* Create a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var x = uniform( len, -100, 100, options ); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var y; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + y = cosineWave( x.length, 4, 1.0, 0, x, 1, 0 ); + if ( isnan( y[ i%x.length ] ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( y[ 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( format( '%s:ndarray:len=%d', pkg, len ), f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/simulate/strided/cosine-wave/docs/repl.txt b/lib/node_modules/@stdlib/simulate/strided/cosine-wave/docs/repl.txt new file mode 100644 index 000000000000..6916dd0b8c2a --- /dev/null +++ b/lib/node_modules/@stdlib/simulate/strided/cosine-wave/docs/repl.txt @@ -0,0 +1,113 @@ + +{{alias}}( N, period, amplitude, phaseOffset, x, strideX ) + Generates a cosine wave in a strided array. + + The `N` and stride parameters determine which elements in the strided array + are accessed at runtime. + + Indexing is relative to the first index. To introduce an offset, use typed + array views. + + If `N <= 0`, the function returns `x` unchanged. + + Parameters + ---------- + N: integer + Number of indexed elements. + + period: integer + Period (i.e., the number of elements before a cosine wave repeats). + + amplitude: number + Peak amplitude. + + phaseOffset: number + Phase offset (in units of elements; zero-based). A negative offset + translates a waveform to the left. A positive offset translates a + waveform to the right. + + x: Array|TypedArray + Input array. + + strideX: integer + Stride length. + + Returns + ------- + x: Array|TypedArray + Input array. + + Examples + -------- + // Standard Usage: + > var x = [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ]; + > {{alias}}( x.length, 4, 1.0, 0, x, 1 ) + [ 1.0, 0.0, -1.0, 0.0, 1.0, 0.0 ] + + // Using `N` and stride parameters: + > x = [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ]; + > {{alias}}( 3, 4, 1.0, 0, x, 2 ) + [ 1.0, -2.0, 0.0, -4.0, -1.0, -6.0 ] + + // Using view offsets: + > var x0 = new {{alias:@stdlib/array/float64}}( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] ); + > var x1 = new {{alias:@stdlib/array/float64}}( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); + > {{alias}}( 3, 4, 1.0, 0, x1, 2 ) + [ 1.0, 3.0, 0.0, 5.0, -1.0 ] + > x0 + [ 1.0, 1.0, 3.0, 0.0, 5.0, -1.0 ] + + +{{alias}}.ndarray( N, period, amplitude, phaseOffset, x, strideX, offsetX ) + Generates a cosine wave in a strided array using alternative indexing + semantics. + + While typed array views mandate a view offset based on the underlying + buffer, the offset parameter supports indexing semantics based on a + starting index. + + Parameters + ---------- + N: integer + Number of indexed elements. + + period: integer + Period (i.e., the number of elements before a cosine wave repeats). + + amplitude: number + Peak amplitude. + + phaseOffset: number + Phase offset (in units of elements; zero-based). A negative offset + translates a waveform to the left. A positive offset translates a + waveform to the right. + + x: Array|TypedArray + Input array. + + strideX: integer + Stride length. + + offsetX: integer + Starting index. + + Returns + ------- + x: Array|TypedArray + Input array. + + Examples + -------- + // Standard Usage: + > var x = [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ]; + > {{alias}}( x.length, 4, 1.0, 0, x, 1, 0 ) + [ 1.0, 0.0, -1.0, 0.0, 1.0, 0.0 ] + + // Using an index offset: + > x = [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ]; + > {{alias}}.ndarray( 3, 4, 1.0, 0, x, 2, 1 ) + [ 1.0, 1.0, 3.0, 0.0, 5.0, -1.0 ] + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/simulate/strided/cosine-wave/docs/types/index.d.ts b/lib/node_modules/@stdlib/simulate/strided/cosine-wave/docs/types/index.d.ts new file mode 100644 index 000000000000..386699337b0e --- /dev/null +++ b/lib/node_modules/@stdlib/simulate/strided/cosine-wave/docs/types/index.d.ts @@ -0,0 +1,114 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 4.1 + +/// + +import { NumericArray, Collection, AccessorArrayLike } from '@stdlib/types/array'; + +/** +* Input array. +*/ +type InputArray = NumericArray | Collection | AccessorArrayLike; + +/** +* Interface describing `cosineWave`. +*/ +interface Routine { + /** + * Generates a cosine wave in a strided array. + * + * ## Notes + * + * - A negative phase offset translates a waveform to the left. A positive phase offset translates a waveform to the right. + * + * @param N - number of indexed elements + * @param period - period (i.e., the number of elements before a cosine wave repeats) + * @param amplitude - peak amplitude + * @param phaseOffset - phase offset (in units of elements; zero-based) + * @param x - input array + * @param strideX - stride length + * @returns `x` + * + * @example + * var x = [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ]; + * + * cosineWave( x.length, 4, 1.0, 0, x, 1 ); + * // x => [ 1.0, 0.0, -1.0, 0.0, 1.0, 0.0 ] + */ + ( N: number, period: number, amplitude: number, phaseOffset: number, x: T, strideX: number ): T; + + /** + * Generates a cosine wave in a strided array. + * + * ## Notes + * + * - A negative phase offset translates a waveform to the left. A positive phase offset translates a waveform to the right. + * + * @param N - number of indexed elements + * @param period - period (i.e., the number of elements before a cosine wave repeats) + * @param amplitude - peak amplitude + * @param phaseOffset - phase offset (in units of elements; zero-based) + * @param x - input array + * @param strideX - stride length + * @param offsetX - starting index + * @returns `x` + * + * @example + * var x = [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ]; + * + * cosineWave.ndarray( x.length, 4, 1.0, 0, x, 1, 0 ); + * // x => [ 1.0, 0.0, -1.0, 0.0, 1.0, 0.0 ] + */ + ndarray( N: number, period: number, amplitude: number, phaseOffset: number, x: T, strideX: number, offsetX: number ): T; +} + +/** +* Generates a cosine wave in a strided array. +* +* ## Notes +* +* - A negative phase offset translates a waveform to the left. A positive phase offset translates a waveform to the right. +* +* @param N - number of indexed elements +* @param period - period (i.e., the number of elements before a cosine wave repeats) +* @param amplitude - peak amplitude +* @param phaseOffset - phase offset (in units of elements; zero-based) +* @param x - input array +* @param strideX - stride length +* @returns `x` +* +* @example +* var x = [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ]; +* +* cosineWave( x.length, 4, 1.0, 0, x, 1 ); +* // x => [ 1.0, 0.0, -1.0, 0.0, 1.0, 0.0 ] +* +* @example +* var x = [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ]; +* +* cosineWave.ndarray( x.length, 4, 1.0, 0, x, 1, 0 ); +* // x => [ 1.0, 0.0, -1.0, 0.0, 1.0, 0.0 ] +*/ +declare var cosineWave: Routine; + + +// EXPORTS // + +export = cosineWave; diff --git a/lib/node_modules/@stdlib/simulate/strided/cosine-wave/docs/types/test.ts b/lib/node_modules/@stdlib/simulate/strided/cosine-wave/docs/types/test.ts new file mode 100644 index 000000000000..e77364f957a0 --- /dev/null +++ b/lib/node_modules/@stdlib/simulate/strided/cosine-wave/docs/types/test.ts @@ -0,0 +1,250 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import AccessorArray = require( '@stdlib/array/base/accessor' ); +import cosineWave = require( './index' ); + + +// TESTS // + +// The function returns a numeric array... +{ + const x = new Float64Array( 10 ); + + cosineWave( x.length, 4, 1.0, 0, x, 1 ); // $ExpectType Float64Array + cosineWave( x.length, 4, 1.0, 0, new AccessorArray( x ), 1 ); // $ExpectType AccessorArray +} + +// The compiler throws an error if the function is provided a first argument which is not a number... +{ + const x = new Float64Array( 10 ); + + cosineWave( '10', 4, 1.0, 0, x, 1 ); // $ExpectError + cosineWave( true, 4, 1.0, 0, x, 1 ); // $ExpectError + cosineWave( false, 4, 1.0, 0, x, 1 ); // $ExpectError + cosineWave( null, 4, 1.0, 0, x, 1 ); // $ExpectError + cosineWave( undefined, 4, 1.0, 0, x, 1 ); // $ExpectError + cosineWave( [], 4, 1.0, 0, x, 1 ); // $ExpectError + cosineWave( {}, 4, 1.0, 0, x, 1 ); // $ExpectError + cosineWave( ( x: number ): number => x, 4, 1.0, 0, x, 1 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not a number... +{ + const x = new Float64Array( 10 ); + + cosineWave( x.length, '10', 1.0, 0, x, 1 ); // $ExpectError + cosineWave( x.length, true, 1.0, 0, x, 1 ); // $ExpectError + cosineWave( x.length, false, 1.0, 0, x, 1 ); // $ExpectError + cosineWave( x.length, null, 1.0, 0, x, 1 ); // $ExpectError + cosineWave( x.length, undefined, 1.0, 0, x, 1 ); // $ExpectError + cosineWave( x.length, [], 1.0, 0, x, 1 ); // $ExpectError + cosineWave( x.length, {}, 1.0, 0, x, 1 ); // $ExpectError + cosineWave( x.length, ( x: number ): number => x, 1.0, 0, x, 1 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a third argument which is not a number... +{ + const x = new Float64Array( 10 ); + + cosineWave( x.length, 4, '10', 0, x, 1 ); // $ExpectError + cosineWave( x.length, 4, true, 0, x, 1 ); // $ExpectError + cosineWave( x.length, 4, false, 0, x, 1 ); // $ExpectError + cosineWave( x.length, 4, null, 0, x, 1 ); // $ExpectError + cosineWave( x.length, 4, undefined, 0, x, 1 ); // $ExpectError + cosineWave( x.length, 4, [], 0, x, 1 ); // $ExpectError + cosineWave( x.length, 4, {}, 0, x, 1 ); // $ExpectError + cosineWave( x.length, 4, ( x: number ): number => x, 0, x, 1 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a fourth argument which is not a number... +{ + const x = new Float64Array( 10 ); + + cosineWave( x.length, 4, 1.0, '10', x, 1 ); // $ExpectError + cosineWave( x.length, 4, 1.0, true, x, 1 ); // $ExpectError + cosineWave( x.length, 4, 1.0, false, x, 1 ); // $ExpectError + cosineWave( x.length, 4, 1.0, null, x, 1 ); // $ExpectError + cosineWave( x.length, 4, 1.0, undefined, x, 1 ); // $ExpectError + cosineWave( x.length, 4, 1.0, [], x, 1 ); // $ExpectError + cosineWave( x.length, 4, 1.0, {}, x, 1 ); // $ExpectError + cosineWave( x.length, 4, 1.0, ( x: number ): number => x, x, 1 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a fifth argument which is not a numeric array... +{ + const x = new Float64Array( 10 ); + + cosineWave( x.length, 4, 1.0, 0, 10, 1 ); // $ExpectError + cosineWave( x.length, 4, 1.0, 0, '10', 1 ); // $ExpectError + cosineWave( x.length, 4, 1.0, 0, true, 1 ); // $ExpectError + cosineWave( x.length, 4, 1.0, 0, false, 1 ); // $ExpectError + cosineWave( x.length, 4, 1.0, 0, null, 1 ); // $ExpectError + cosineWave( x.length, 4, 1.0, 0, undefined, 1 ); // $ExpectError + cosineWave( x.length, 4, 1.0, 0, [ '1' ], 1 ); // $ExpectError + cosineWave( x.length, 4, 1.0, 0, {}, 1 ); // $ExpectError + cosineWave( x.length, 4, 1.0, 0, ( x: number ): number => x, 1 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a sixth argument which is not a number... +{ + const x = new Float64Array( 10 ); + + cosineWave( x.length, 4, 1.0, 0, x, '10' ); // $ExpectError + cosineWave( x.length, 4, 1.0, 0, x, true ); // $ExpectError + cosineWave( x.length, 4, 1.0, 0, x, false ); // $ExpectError + cosineWave( x.length, 4, 1.0, 0, x, null ); // $ExpectError + cosineWave( x.length, 4, 1.0, 0, x, undefined ); // $ExpectError + cosineWave( x.length, 4, 1.0, 0, x, [] ); // $ExpectError + cosineWave( x.length, 4, 1.0, 0, x, {} ); // $ExpectError + cosineWave( x.length, 4, 1.0, 0, x, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = new Float64Array( 10 ); + + cosineWave(); // $ExpectError + cosineWave( x.length ); // $ExpectError + cosineWave( x.length, 4 ); // $ExpectError + cosineWave( x.length, 4, 1.0 ); // $ExpectError + cosineWave( x.length, 4, 1.0, 0 ); // $ExpectError + cosineWave( x.length, 4, 1.0, 0, x ); // $ExpectError + cosineWave( x.length, 4, 1.0, 0, x, 1, 2 ); // $ExpectError +} + +// Attached to main export is an `ndarray` method which returns a numeric array... +{ + const x = new Float64Array( 10 ); + + cosineWave.ndarray( x.length, 4, 1.0, 0, x, 1, 0 ); // $ExpectType Float64Array + cosineWave.ndarray( x.length, 4, 1.0, 0, new AccessorArray( x ), 1, 0 ); // $ExpectType AccessorArray +} + +// The compiler throws an error if the `ndarray` method is provided a first argument which is not a number... +{ + const x = new Float64Array( 10 ); + + cosineWave.ndarray( '10', 4, 1.0, 0, x, 1, 0 ); // $ExpectError + cosineWave.ndarray( true, 4, 1.0, 0, x, 1, 0 ); // $ExpectError + cosineWave.ndarray( false, 4, 1.0, 0, x, 1, 0 ); // $ExpectError + cosineWave.ndarray( null, 4, 1.0, 0, x, 1, 0 ); // $ExpectError + cosineWave.ndarray( undefined, 4, 1.0, 0, x, 1, 0 ); // $ExpectError + cosineWave.ndarray( [], 4, 1.0, 0, x, 1, 0 ); // $ExpectError + cosineWave.ndarray( {}, 4, 1.0, 0, x, 1, 0 ); // $ExpectError + cosineWave.ndarray( ( x: number ): number => x, 4, 1.0, 0, x, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a second argument which is not a number... +{ + const x = new Float64Array( 10 ); + + cosineWave.ndarray( x.length, '10', 1.0, 0, x, 1, 0 ); // $ExpectError + cosineWave.ndarray( x.length, true, 1.0, 0, x, 1, 0 ); // $ExpectError + cosineWave.ndarray( x.length, false, 1.0, 0, x, 1, 0 ); // $ExpectError + cosineWave.ndarray( x.length, null, 1.0, 0, x, 1, 0 ); // $ExpectError + cosineWave.ndarray( x.length, undefined, 1.0, 0, x, 1, 0 ); // $ExpectError + cosineWave.ndarray( x.length, [], 1.0, 0, x, 1, 0 ); // $ExpectError + cosineWave.ndarray( x.length, {}, 1.0, 0, x, 1, 0 ); // $ExpectError + cosineWave.ndarray( x.length, ( x: number ): number => x, 1.0, 0, x, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a third argument which is not a number... +{ + const x = new Float64Array( 10 ); + + cosineWave.ndarray( x.length, 4, '10', 0, x, 1, 0 ); // $ExpectError + cosineWave.ndarray( x.length, 4, true, 0, x, 1, 0 ); // $ExpectError + cosineWave.ndarray( x.length, 4, false, 0, x, 1, 0 ); // $ExpectError + cosineWave.ndarray( x.length, 4, null, 0, x, 1, 0 ); // $ExpectError + cosineWave.ndarray( x.length, 4, undefined, 0, x, 1, 0 ); // $ExpectError + cosineWave.ndarray( x.length, 4, [ '1' ], 0, x, 1, 0 ); // $ExpectError + cosineWave.ndarray( x.length, 4, {}, 0, x, 1, 0 ); // $ExpectError + cosineWave.ndarray( x.length, 4, ( x: number ): number => x, 0, x, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a fourth argument which is not a number... +{ + const x = new Float64Array( 10 ); + + cosineWave.ndarray( x.length, 4, 1.0, '10', x, 1, 0 ); // $ExpectError + cosineWave.ndarray( x.length, 4, 1.0, true, x, 1, 0 ); // $ExpectError + cosineWave.ndarray( x.length, 4, 1.0, false, x, 1, 0 ); // $ExpectError + cosineWave.ndarray( x.length, 4, 1.0, null, x, 1, 0 ); // $ExpectError + cosineWave.ndarray( x.length, 4, 1.0, undefined, x, 1, 0 ); // $ExpectError + cosineWave.ndarray( x.length, 4, 1.0, [], x, 1, 0 ); // $ExpectError + cosineWave.ndarray( x.length, 4, 1.0, {}, x, 1, 0 ); // $ExpectError + cosineWave.ndarray( x.length, 4, 1.0, ( x: number ): number => x, x, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a fifth argument which is not a numeric array... +{ + const x = new Float64Array( 10 ); + + cosineWave.ndarray( x.length, 4, 1.0, 0, 10, 1, 0 ); // $ExpectError + cosineWave.ndarray( x.length, 4, 1.0, 0, '10', 1, 0 ); // $ExpectError + cosineWave.ndarray( x.length, 4, 1.0, 0, true, 1, 0 ); // $ExpectError + cosineWave.ndarray( x.length, 4, 1.0, 0, false, 1, 0 ); // $ExpectError + cosineWave.ndarray( x.length, 4, 1.0, 0, null, 1, 0 ); // $ExpectError + cosineWave.ndarray( x.length, 4, 1.0, 0, undefined, 1, 0 ); // $ExpectError + cosineWave.ndarray( x.length, 4, 1.0, 0, [ '1' ], 1, 0 ); // $ExpectError + cosineWave.ndarray( x.length, 4, 1.0, 0, {}, 1, 0 ); // $ExpectError + cosineWave.ndarray( x.length, 4, 1.0, 0, ( x: number ): number => x, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a sixth argument which is not a number... +{ + const x = new Float64Array( 10 ); + + cosineWave.ndarray( x.length, 4, 1.0, 0, x, '10', 0 ); // $ExpectError + cosineWave.ndarray( x.length, 4, 1.0, 0, x, true, 0 ); // $ExpectError + cosineWave.ndarray( x.length, 4, 1.0, 0, x, false, 0 ); // $ExpectError + cosineWave.ndarray( x.length, 4, 1.0, 0, x, null, 0 ); // $ExpectError + cosineWave.ndarray( x.length, 4, 1.0, 0, x, undefined, 0 ); // $ExpectError + cosineWave.ndarray( x.length, 4, 1.0, 0, x, [], 0 ); // $ExpectError + cosineWave.ndarray( x.length, 4, 1.0, 0, x, {}, 0 ); // $ExpectError + cosineWave.ndarray( x.length, 4, 1.0, 0, x, ( x: number ): number => x, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a seventh argument which is not a number... +{ + const x = new Float64Array( 10 ); + + cosineWave.ndarray( x.length, 4, 1.0, 0, x, 1, '10' ); // $ExpectError + cosineWave.ndarray( x.length, 4, 1.0, 0, x, 1, true ); // $ExpectError + cosineWave.ndarray( x.length, 4, 1.0, 0, x, 1, false ); // $ExpectError + cosineWave.ndarray( x.length, 4, 1.0, 0, x, 1, null ); // $ExpectError + cosineWave.ndarray( x.length, 4, 1.0, 0, x, 1, undefined ); // $ExpectError + cosineWave.ndarray( x.length, 4, 1.0, 0, x, 1, [] ); // $ExpectError + cosineWave.ndarray( x.length, 4, 1.0, 0, x, 1, {} ); // $ExpectError + cosineWave.ndarray( x.length, 4, 1.0, 0, x, 1, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided an unsupported number of arguments... +{ + const x = new Float64Array( 10 ); + + cosineWave.ndarray(); // $ExpectError + cosineWave.ndarray( x.length ); // $ExpectError + cosineWave.ndarray( x.length, 4 ); // $ExpectError + cosineWave.ndarray( x.length, 4, 1.0 ); // $ExpectError + cosineWave.ndarray( x.length, 4, 1.0, 0 ); // $ExpectError + cosineWave.ndarray( x.length, 4, 1.0, 0, x ); // $ExpectError + cosineWave.ndarray( x.length, 4, 1.0, 0, x, 1 ); // $ExpectError + cosineWave.ndarray( x.length, 4, 1.0, 0, x, 1, 0, 2 ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/simulate/strided/cosine-wave/examples/index.js b/lib/node_modules/@stdlib/simulate/strided/cosine-wave/examples/index.js new file mode 100644 index 000000000000..33ec29bd8026 --- /dev/null +++ b/lib/node_modules/@stdlib/simulate/strided/cosine-wave/examples/index.js @@ -0,0 +1,30 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var cosineWave = require( './../lib' ); + +var x = discreteUniform( 10, -100, 100, { + 'dtype': 'float64' +}); +console.log( x ); + +cosineWave( x.length, 4, 1.0, 0, x, 1 ); +console.log( x ); diff --git a/lib/node_modules/@stdlib/simulate/strided/cosine-wave/lib/accessors.js b/lib/node_modules/@stdlib/simulate/strided/cosine-wave/lib/accessors.js new file mode 100644 index 000000000000..70a4080e1af2 --- /dev/null +++ b/lib/node_modules/@stdlib/simulate/strided/cosine-wave/lib/accessors.js @@ -0,0 +1,78 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var cospi = require( '@stdlib/math/base/special/cospi' ); + + +// MAIN // + +/** +* Generates a cosine wave in a strided array. +* +* @private +* @param {PositiveInteger} N - number of indexed elements +* @param {PositiveInteger} period - period (i.e., the number of elements before a cosine wave repeats) +* @param {NonNegativeNumber} amplitude - peak amplitude +* @param {integer} phaseOffset - phase offset (in units of elements; zero-based). A negative offset translates a waveform to the left. A positive offset translates a waveform to the right +* @param {NumericArray} x - input array +* @param {integer} strideX - stride length +* @param {NonNegativeInteger} offsetX - starting index +* @returns {Object} input array object +* +* @example +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +* var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +* +* var x = toAccessorArray( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0, 7.0, -8.0 ] ); +* +* var v = cosineWave( 4, 4, 1.0, 0, arraylike2object( x ), 2, 1 ); +* // returns {...} +*/ +function cosineWave( N, period, amplitude, phaseOffset, x, strideX, offsetX ) { + var theta; + var xbuf; + var set; + var ix; + var i; + var s; + + // Cache reference to array data: + xbuf = x.data; + + // Cache reference to the element accessor: + set = x.accessors[ 1 ]; + + ix = offsetX; + theta = -phaseOffset; + s = 2.0 / period; + for ( i = 0; i < N; i++ ) { + set( xbuf, ix, amplitude * cospi( s * theta ) ); + theta += 1.0; + ix += strideX; + } + return x; +} + + +// EXPORTS // + +module.exports = cosineWave; diff --git a/lib/node_modules/@stdlib/simulate/strided/cosine-wave/lib/index.js b/lib/node_modules/@stdlib/simulate/strided/cosine-wave/lib/index.js new file mode 100644 index 000000000000..71fd0f174dc1 --- /dev/null +++ b/lib/node_modules/@stdlib/simulate/strided/cosine-wave/lib/index.js @@ -0,0 +1,57 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Generate a cosine wave in a strided array. +* +* @module @stdlib/simulate/strided/cosine-wave +* +* @example +* var cosineWave = require( '@stdlib/simulate/strided/cosine-wave' ); +* +* var x = [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ]; +* +* cosineWave( x.length, 4, 1.0, 0, x, 1 ); +* // x => [ 1.0, 0.0, -1.0, 0.0, 1.0, 0.0 ] +* +* @example +* var cosineWave = require( '@stdlib/simulate/strided/cosine-wave' ); +* +* var x = [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ]; +* +* cosineWave.ndarray( x.length, 4, 1.0, 0, x, 1, 0 ); +* // x => [ 1.0, 0.0, -1.0, 0.0, 1.0, 0.0 ] +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var main = require( './main.js' ); +var ndarray = require( './ndarray.js' ); + + +// MAIN // + +setReadOnly( main, 'ndarray', ndarray ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/simulate/strided/cosine-wave/lib/main.js b/lib/node_modules/@stdlib/simulate/strided/cosine-wave/lib/main.js new file mode 100644 index 000000000000..e9f270625c15 --- /dev/null +++ b/lib/node_modules/@stdlib/simulate/strided/cosine-wave/lib/main.js @@ -0,0 +1,53 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var stride2offset = require( '@stdlib/strided/base/stride2offset' ); +var ndarray = require( './ndarray.js' ); + + +// MAIN // + +/** +* Generates a cosine wave in a strided array. +* +* @param {PositiveInteger} N - number of indexed elements +* @param {PositiveInteger} period - period (i.e., the number of elements before a cosine wave repeats) +* @param {NonNegativeNumber} amplitude - peak amplitude +* @param {integer} phaseOffset - phase offset (in units of elements; zero-based). A negative offset translates a waveform to the left. A positive offset translates a waveform to the right +* @param {NumericArray} x - input array +* @param {integer} strideX - stride length +* @returns {NumericArray} input array +* +* @example +* var x = [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ]; +* +* cosineWave( x.length, 4, 1.0, 0, x, 1 ); +* // x => [ 1.0, 0.0, -1.0, 0.0, 1.0, 0.0 ] +*/ +function cosineWave( N, period, amplitude, phaseOffset, x, strideX ) { + return ndarray( N, period, amplitude, phaseOffset, x, strideX, stride2offset( N, strideX ) ); // eslint-disable-line max-len +} + + +// EXPORTS // + +module.exports = cosineWave; diff --git a/lib/node_modules/@stdlib/simulate/strided/cosine-wave/lib/ndarray.js b/lib/node_modules/@stdlib/simulate/strided/cosine-wave/lib/ndarray.js new file mode 100644 index 000000000000..2551673148d1 --- /dev/null +++ b/lib/node_modules/@stdlib/simulate/strided/cosine-wave/lib/ndarray.js @@ -0,0 +1,113 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +var cospi = require( '@stdlib/math/base/special/cospi' ); +var accessors = require( './accessors.js' ); + + +// VARIABLES // + +var M = 5; + + +// MAIN // + +/** +* Generates a cosine wave in a strided array. +* +* @param {PositiveInteger} N - number of indexed elements +* @param {PositiveInteger} period - period (i.e., the number of elements before a cosine wave repeats) +* @param {NonNegativeNumber} amplitude - peak amplitude +* @param {integer} phaseOffset - phase offset (in units of elements; zero-based). A negative offset translates a waveform to the left. A positive offset translates a waveform to the right +* @param {NumericArray} x - input array +* @param {integer} strideX - stride length +* @param {NonNegativeInteger} offsetX - starting index +* @returns {NumericArray} input array +* +* @example +* var x = [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ]; +* +* cosineWave( x.length, 4, 1.0, 0, x, 1, 0 ); +* // x => [ 1.0, 0.0, -1.0, 0.0, 1.0, 0.0 ] +*/ +function cosineWave( N, period, amplitude, phaseOffset, x, strideX, offsetX ) { + var theta; + var ix; + var m; + var s; + var o; + var i; + + if ( N <= 0 || amplitude < 0.0 || period <= 0 ) { + return x; + } + o = arraylike2object( x ); + if ( o.accessorProtocol ) { + return accessors( N, period, amplitude, phaseOffset, o, strideX, offsetX ); // eslint-disable-line max-len + } + ix = offsetX; + theta = -phaseOffset; + s = 2.0 / period; + + // Use loop unrolling if the stride is equal to `1`... + if ( strideX === 1 ) { + m = N % M; + + // If we have a remainder, run a clean-up loop... + if ( m > 0 ) { + for ( i = 0; i < m; i++ ) { + x[ ix ] = amplitude * cospi( s * theta ); + theta += 1.0; + ix += strideX; + } + } + if ( N < M ) { + return x; + } + for ( i = m; i < N; i += M ) { + x[ ix ] = amplitude * cospi( s * theta ); + theta += 1.0; + x[ ix+1 ] = amplitude * cospi( s * theta ); + theta += 1.0; + x[ ix+2 ] = amplitude * cospi( s * theta ); + theta += 1.0; + x[ ix+3 ] = amplitude * cospi( s * theta ); + theta += 1.0; + x[ ix+4 ] = amplitude * cospi( s * theta ); + theta += 1.0; + ix += M; + } + return x; + } + for ( i = 0; i < N; i++ ) { + x[ ix ] = amplitude * cospi( s * theta ); + theta += 1.0; + ix += strideX; + } + return x; +} + + +// EXPORTS // + +module.exports = cosineWave; diff --git a/lib/node_modules/@stdlib/simulate/strided/cosine-wave/package.json b/lib/node_modules/@stdlib/simulate/strided/cosine-wave/package.json new file mode 100644 index 000000000000..48408fcf0b93 --- /dev/null +++ b/lib/node_modules/@stdlib/simulate/strided/cosine-wave/package.json @@ -0,0 +1,70 @@ +{ + "name": "@stdlib/simulate/strided/cosine-wave", + "version": "0.0.0", + "description": "Generate a cosine wave in a strided array.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "simulate", + "simulation", + "sine", + "sin", + "cos", + "cosine", + "sinusoid", + "wave", + "signal", + "processing", + "waveform", + "periodic", + "strided", + "array", + "ndarray" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/simulate/strided/cosine-wave/test/test.js b/lib/node_modules/@stdlib/simulate/strided/cosine-wave/test/test.js new file mode 100644 index 000000000000..e6079ee2a9b1 --- /dev/null +++ b/lib/node_modules/@stdlib/simulate/strided/cosine-wave/test/test.js @@ -0,0 +1,38 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var cosineWave = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof cosineWave, '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 cosineWave.ndarray, 'function', 'method is a function' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/simulate/strided/cosine-wave/test/test.main.js b/lib/node_modules/@stdlib/simulate/strided/cosine-wave/test/test.main.js new file mode 100644 index 000000000000..7821a805ed28 --- /dev/null +++ b/lib/node_modules/@stdlib/simulate/strided/cosine-wave/test/test.main.js @@ -0,0 +1,391 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Float64Array = require( '@stdlib/array/float64' ); +var cospi = require( '@stdlib/math/base/special/cospi' ); +var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +var cosineWave = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof cosineWave, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 6', function test( t ) { + t.strictEqual( cosineWave.length, 6, 'has expected arity' ); + t.end(); +}); + +tape( 'the function generates a cosine wave in a strided array', function test( t ) { + var expected; + var x; + + x = [ + 4.0, + 2.0, + -3.0, + 5.0, + -1.0, + 2.0, + -5.0, + 6.0 + ]; + expected = [ + 1.0, + 0.0, + -1.0, + 0.0, + 1.0, + 0.0, + -1.0, + 0.0 + ]; + + cosineWave( x.length, 4, 1.0, 0, x, 1 ); + t.deepEqual( x, expected, 'returns expected value' ); + + x = [ 1.0, 2.0 ]; + expected = [ 2.0, 0.0 ]; + + cosineWave( x.length, 4, 2.0, 0, x, 1 ); + t.deepEqual( x, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function generates a cosine wave in a strided array (accessors)', function test( t ) { + var expected; + var x; + + x = [ + 4.0, + 2.0, + -3.0, + 5.0, + -1.0, + 2.0, + -5.0, + 6.0 + ]; + expected = [ + 1.0, + 0.0, + -1.0, + 0.0, + 1.0, + 0.0, + -1.0, + 0.0 + ]; + + cosineWave( x.length, 4, 1.0, 0, toAccessorArray( x ), 1 ); + t.deepEqual( x, expected, 'returns expected value' ); + + x = [ 1.0, 2.0 ]; + expected = [ 2.0, 0.0 ]; + + cosineWave( x.length, 4, 2.0, 0, toAccessorArray( x ), 1 ); + t.deepEqual( x, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a reference to the input array', function test( t ) { + var out; + var x; + + x = [ 1.0, 2.0, 3.0, 4.0, 5.0 ]; + out = cosineWave( x.length, 4, 1.0, 0, x, 1 ); + + t.strictEqual( out, x, 'same reference' ); + t.end(); +}); + +tape( 'if provided an `N` parameter less than or equal to `0`, the function returns `x` unchanged', function test( t ) { + var expected; + var x; + + x = [ 3.0, -4.0, 1.0 ]; + expected = [ 3.0, -4.0, 1.0 ]; + + cosineWave( 0, 4, 1.0, 0, x, 1 ); + t.deepEqual( x, expected, 'returns expected value' ); + + cosineWave( -4, 4, 1.0, 0, x, 1 ); + t.deepEqual( x, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'if `period` equals `0`, the function returns `x` unchanged', function test( t ) { + var expected; + var x; + + x = [ 3.0, -4.0, 1.0, 15.0, 4.0, 3.0 ]; + expected = [ 3.0, -4.0, 1.0, 15.0, 4.0, 3.0 ]; + + cosineWave( x.length, 0, 1.0, 0, x, 1 ); + t.deepEqual( x, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying a period', function test( t ) { + var expected; + var x; + + x = [ + 2.0, + -3.0, + -5.0, + 7.0, + 6.0 + ]; + expected = [ + 1.0, + -1.0, + 1.0, + -1.0, + 1.0 + ]; + + cosineWave( x.length, 2, 1.0, 0, x, 1 ); + t.deepEqual( x, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports specifying amplitude', function test( t ) { + var expected; + var x; + + x = [ + 2.0, + -3.0, + -5.0, + 7.0, + 6.0 + ]; + expected = [ + 3.5, + 0.0, + -3.5, + 0.0, + 3.5 + ]; + + cosineWave( x.length, 4, 3.5, 0, x, 1 ); + t.deepEqual( x, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports specifying a phaseOffset', function test( t ) { + var expected; + var x; + + x = [ + 2.0, + -3.0, + -5.0, + 7.0, + 6.0 + ]; + expected = [ + 0.0, + 1.0, + 0.0, + -1.0, + 0.0 + ]; + + cosineWave( x.length, 4, 1.0, 1, x, 1 ); + t.deepEqual( x, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports specifying a stride', function test( t ) { + var expected; + var x; + + x = [ + 2.0, // 0 + -3.0, + -5.0, // 1 + 7.0, + 6.0 // 2 + ]; + expected = [ + 1.0, // 0 + -3.0, + 0.0, // 1 + 7.0, + -1.0 // 2 + ]; + + cosineWave( 3, 4, 1.0, 0, x, 2 ); + t.deepEqual( x, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports specifying a stride', function test( t ) { + var expected; + var x; + + x = [ + 2.0, // 0 + -3.0, + -5.0, // 1 + 7.0, + 6.0 // 2 + ]; + expected = [ + 1.0, // 0 + -3.0, + 0.0, // 1 + 7.0, + -1.0 // 2 + ]; + + cosineWave( 3, 4, 1.0, 0, toAccessorArray( x ), 2 ); + t.deepEqual( x, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports specifying a negative stride', function test( t ) { + var expected; + var x; + + x = [ + 2.0, // 2 + -3.0, + -5.0, // 1 + 7.0, + 6.0 // 0 + ]; + expected = [ + -1.0, // 2 + -3.0, + 0.0, // 1 + 7.0, + 1.0 // 0 + ]; + + cosineWave( 3, 4, 1.0, 0, x, -2 ); + t.deepEqual( x, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports specifying a negative stride', function test( t ) { + var expected; + var x; + + x = [ + 2.0, // 2 + -3.0, + -5.0, // 1 + 7.0, + 6.0 // 0 + ]; + expected = [ + -1.0, // 2 + -3.0, + 0.0, // 1 + 7.0, + 1.0 // 0 + ]; + + cosineWave( 3, 4, 1.0, 0, toAccessorArray( x ), -2 ); + t.deepEqual( x, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports view offsets', function test( t ) { + var expected; + var x0; + var x1; + + x0 = new Float64Array([ + 1.0, + 2.0, // 0 + 3.0, + 4.0, // 1 + 5.0, + 6.0 // 2 + ]); + expected = new Float64Array([ + 1.0, + 1.0, // 0 + 3.0, + 0.0, // 1 + 5.0, + -1.0 // 2 + ]); + + x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); + + cosineWave( 3, 4, 1.0, 0, x1, 2 ); + t.deepEqual( x0, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'if `stride` is equal to `1`, the function efficiently generates a cosine wave in a strided array', function test( t ) { + var phaseOffset; + var amplitude; + var expected; + var period; + var theta; + var s; + var x; + var i; + + phaseOffset = 0; + amplitude = 1.0; + period = 4; + s = 2.0 / period; + theta = -phaseOffset; + x = new Float64Array( 100 ); + expected = new Float64Array( x.length ); + for ( i = 0; i < x.length; i++ ) { + x[ i ] = i; + expected[ i ] = amplitude * cospi( s * theta ); + theta += 1.0; + } + cosineWave( x.length, 4, 1.0, 0, x, 1 ); + t.deepEqual( x, expected, 'returns expected value' ); + + theta = -phaseOffset; + x = new Float64Array( 240 ); + expected = new Float64Array( x.length ); + for ( i = 0; i < x.length; i++ ) { + x[ i ] = i; + expected[ i ] = amplitude * cospi( s * theta ); + theta += 1.0; + } + cosineWave( x.length, 4, 1.0, 0, x, 1 ); + t.deepEqual( x, expected, 'returns expected value' ); + + t.end(); +}); diff --git a/lib/node_modules/@stdlib/simulate/strided/cosine-wave/test/test.ndarray.js b/lib/node_modules/@stdlib/simulate/strided/cosine-wave/test/test.ndarray.js new file mode 100644 index 000000000000..571b1a4b3bb7 --- /dev/null +++ b/lib/node_modules/@stdlib/simulate/strided/cosine-wave/test/test.ndarray.js @@ -0,0 +1,414 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Float64Array = require( '@stdlib/array/float64' ); +var cospi = require( '@stdlib/math/base/special/cospi' ); +var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +var cosineWave = require( './../lib/ndarray.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof cosineWave, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 7', function test( t ) { + t.strictEqual( cosineWave.length, 7, 'has expected arity' ); + t.end(); +}); + +tape( 'the function generates a cosine wave in a strided array', function test( t ) { + var expected; + var x; + + x = [ + 4.0, + 2.0, + -3.0, + 5.0, + -1.0, + 2.0, + -5.0, + 6.0 + ]; + expected = [ + 1.0, + 0.0, + -1.0, + 0.0, + 1.0, + 0.0, + -1.0, + 0.0 + ]; + + cosineWave( x.length, 4, 1.0, 0, x, 1, 0 ); + t.deepEqual( x, expected, 'returns expected value' ); + + x = [ 1.0, 2.0 ]; + expected = [ 2.0, 0.0 ]; + + cosineWave( x.length, 4, 2.0, 0, x, 1, 0 ); + t.deepEqual( x, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function generates a cosine wave in a strided array (accessors)', function test( t ) { + var expected; + var x; + + x = [ + 4.0, + 2.0, + -3.0, + 5.0, + -1.0, + 2.0, + -5.0, + 6.0 + ]; + expected = [ + 1.0, + 0.0, + -1.0, + 0.0, + 1.0, + 0.0, + -1.0, + 0.0 + ]; + + cosineWave( x.length, 4, 1.0, 0, toAccessorArray( x ), 1, 0 ); + t.deepEqual( x, expected, 'returns expected value' ); + + x = [ 1.0, 2.0 ]; + expected = [ 2.0, 0.0 ]; + + cosineWave( x.length, 4, 2.0, 0, toAccessorArray( x ), 1, 0 ); + t.deepEqual( x, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a reference to the input array', function test( t ) { + var out; + var x; + + x = [ 1.0, 2.0, 3.0, 4.0, 5.0 ]; + out = cosineWave( x.length, 4, 1.0, 0, x, 1, 0 ); + + t.strictEqual( out, x, 'same reference' ); + t.end(); +}); + +tape( 'if provided an `N` parameter less than or equal to `0`, the function returns `x` unchanged', function test( t ) { + var expected; + var x; + + x = [ 3.0, -4.0, 1.0 ]; + expected = [ 3.0, -4.0, 1.0 ]; + + cosineWave( 0, 4, 1.0, 0, x, 1, 0 ); + t.deepEqual( x, expected, 'returns expected value' ); + + cosineWave( -4, 4, 1.0, 0, x, 1, 0 ); + t.deepEqual( x, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'if `period` equals `0`, the function returns `x` unchanged', function test( t ) { + var expected; + var x; + + x = [ 3.0, -4.0, 1.0, 15.0, 4.0, 3.0 ]; + expected = [ 3.0, -4.0, 1.0, 15.0, 4.0, 3.0 ]; + + cosineWave( x.length, 0, 1.0, 0, x, 1, 0 ); + t.deepEqual( x, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying a period', function test( t ) { + var expected; + var x; + + x = [ + 2.0, + -3.0, + -5.0, + 7.0, + 6.0 + ]; + expected = [ + 1.0, + -1.0, + 1.0, + -1.0, + 1.0 + ]; + + cosineWave( x.length, 2, 1.0, 0, x, 1, 0 ); + t.deepEqual( x, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports specifying amplitude', function test( t ) { + var expected; + var x; + + x = [ + 2.0, + -3.0, + -5.0, + 7.0, + 6.0 + ]; + expected = [ + 3.5, + 0.0, + -3.5, + 0.0, + 3.5 + ]; + + cosineWave( x.length, 4, 3.5, 0, x, 1, 0 ); + t.deepEqual( x, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports specifying a phaseOffset', function test( t ) { + var expected; + var x; + + x = [ + 2.0, + -3.0, + -5.0, + 7.0, + 6.0 + ]; + expected = [ + 0.0, + 1.0, + 0.0, + -1.0, + 0.0 + ]; + + cosineWave( x.length, 4, 1.0, 1, x, 1, 0 ); + t.deepEqual( x, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports specifying a stride', function test( t ) { + var expected; + var x; + + x = [ + 2.0, // 0 + -3.0, + -5.0, // 1 + 7.0, + 6.0 // 2 + ]; + expected = [ + 1.0, // 0 + -3.0, + 0.0, // 1 + 7.0, + -1.0 // 2 + ]; + + cosineWave( 3, 4, 1.0, 0, x, 2, 0 ); + t.deepEqual( x, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports specifying a stride', function test( t ) { + var expected; + var x; + + x = [ + 2.0, // 0 + -3.0, + -5.0, // 1 + 7.0, + 6.0 // 2 + ]; + expected = [ + 1.0, // 0 + -3.0, + 0.0, // 1 + 7.0, + -1.0 // 2 + ]; + + cosineWave( 3, 4, 1.0, 0, toAccessorArray( x ), 2, 0 ); + t.deepEqual( x, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports specifying a negative stride', function test( t ) { + var expected; + var x; + + x = [ + 2.0, // 2 + -3.0, + -5.0, // 1 + 7.0, + 6.0 // 0 + ]; + expected = [ + -1.0, // 2 + -3.0, + 0.0, // 1 + 7.0, + 1.0 // 0 + ]; + + cosineWave( 3, 4, 1.0, 0, x, -2, x.length-1 ); + t.deepEqual( x, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports specifying a negative stride', function test( t ) { + var expected; + var x; + + x = [ + 2.0, // 2 + -3.0, + -5.0, // 1 + 7.0, + 6.0 // 0 + ]; + expected = [ + -1.0, // 2 + -3.0, + 0.0, // 1 + 7.0, + 1.0 // 0 + ]; + + cosineWave( 3, 4, 1.0, 0, toAccessorArray( x ), -2, x.length-1 ); + t.deepEqual( x, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports an offset parameter', function test( t ) { + var expected; + var x; + + x = [ + 1.0, + 2.0, // 0 + 3.0, + 4.0, // 1 + 5.0, + 6.0 // 2 + ]; + expected = [ + 1.0, + 1.0, // 0 + 3.0, + 0.0, // 1 + 5.0, + -1.0 // 2 + ]; + + cosineWave( 3, 4, 1.0, 0, x, 2, 1 ); + t.deepEqual( x, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports an offset parameter', function test( t ) { + var expected; + var x; + + x = [ + 1.0, + 2.0, // 0 + 3.0, + 4.0, // 1 + 5.0, + 6.0 // 2 + ]; + expected = [ + 1.0, + 1.0, // 0 + 3.0, + 0.0, // 1 + 5.0, + -1.0 // 2 + ]; + + cosineWave( 3, 4, 1.0, 0, toAccessorArray( x ), 2, 1 ); + t.deepEqual( x, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'if `stride` is equal to `1`, the function efficiently generates a cosine wave in a strided array', function test( t ) { + var phaseOffset; + var amplitude; + var expected; + var period; + var theta; + var s; + var x; + var i; + + phaseOffset = 0; + amplitude = 1.0; + period = 4; + s = 2.0 / period; + theta = -phaseOffset; + x = new Float64Array( 100 ); + expected = new Float64Array( x.length ); + for ( i = 0; i < x.length; i++ ) { + x[ i ] = i; + expected[ i ] = amplitude * cospi( s * theta ); + theta += 1.0; + } + cosineWave( x.length, 4, 1.0, 0, x, 1, 0 ); + t.deepEqual( x, expected, 'returns expected value' ); + + theta = -phaseOffset; + x = new Float64Array( 240 ); + expected = new Float64Array( x.length ); + for ( i = 0; i < x.length; i++ ) { + x[ i ] = i; + expected[ i ] = amplitude * cospi( s * theta ); + theta += 1.0; + } + cosineWave( x.length, 4, 1.0, 0, x, 1, 0 ); + t.deepEqual( x, expected, 'returns expected value' ); + + t.end(); +});