diff --git a/lib/node_modules/@stdlib/stats/base/cuminabs/README.md b/lib/node_modules/@stdlib/stats/base/cuminabs/README.md index 209988aec339..268a22870aed 100644 --- a/lib/node_modules/@stdlib/stats/base/cuminabs/README.md +++ b/lib/node_modules/@stdlib/stats/base/cuminabs/README.md @@ -56,7 +56,7 @@ The function has the following parameters: - **y**: output [`Array`][mdn-array] or [`typed array`][mdn-typed-array]. - **strideY**: index increment for `y`. -The `N` and `stride` parameters determine which elements in `x` and `y` are accessed at runtime. For example, to compute the cumulative minimum absolute value of every other element in `x`, +The `N` and stride parameters determine which elements in the strided arrays are accessed at runtime. For example, to compute the cumulative maximum absolute value of every other element in `x`, ```javascript var x = [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0 ]; @@ -102,7 +102,7 @@ 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`, `offsetX` and `offsetY` parameters support indexing semantics based on a starting indices. For example, to calculate the cumulative minimum absolute value of every other value in `x` starting from the second value and to store in the last `N` elements of `y` starting from the last element +While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, offset parameters support indexing semantics based on a starting indices. For example, to calculate the cumulative minimum absolute value of every other value in `x` starting from the second value and to store in the last `N` elements of `y` starting from the last element ```javascript var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ]; @@ -122,6 +122,7 @@ cuminabs.ndarray( 4, x, 2, 1, y, -1, y.length-1 ); - If `N <= 0`, both functions return `y` unchanged. - Depending on the environment, the typed versions ([`dcuminabs`][@stdlib/stats/strided/dcuminabs], [`scuminabs`][@stdlib/stats/strided/scuminabs], etc.) are likely to be significantly more performant. +- 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]). @@ -134,20 +135,14 @@ cuminabs.ndarray( 4, x, 2, 1, y, -1, y.length-1 ); ```javascript -var randu = require( '@stdlib/random/base/randu' ); -var round = require( '@stdlib/math/base/special/round' ); +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); var Float64Array = require( '@stdlib/array/float64' ); var cuminabs = require( '@stdlib/stats/base/cuminabs' ); -var y; -var x; -var i; - -x = new Float64Array( 10 ); -y = new Float64Array( x.length ); -for ( i = 0; i < x.length; i++ ) { - x[ i ] = round( randu()*100.0 ); -} +var x = discreteUniform( 10, 0, 100, { + 'dtype': 'float64' +}); +var y = new Float64Array( x.length ); console.log( x ); console.log( y ); @@ -188,6 +183,8 @@ console.log( y ); [mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array +[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor + [mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray diff --git a/lib/node_modules/@stdlib/stats/base/cuminabs/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/base/cuminabs/benchmark/benchmark.js index ef5edfecb9b2..722f2756d807 100644 --- a/lib/node_modules/@stdlib/stats/base/cuminabs/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/stats/base/cuminabs/benchmark/benchmark.js @@ -21,13 +21,22 @@ // MODULES // var bench = require( '@stdlib/bench' ); -var randu = require( '@stdlib/random/base/randu' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var pow = require( '@stdlib/math/base/special/pow' ); +var uniform = require( '@stdlib/random/array/uniform' ); +var zeros = require( '@stdlib/array/zeros' ); +var gfill = require( '@stdlib/blas/ext/base/gfill' ); var pkg = require( './../package.json' ).name; var cuminabs = require( './../lib' ); +// VARIABLES // + +var options = { + 'dtype': 'generic' +}; + + // FUNCTIONS // /** @@ -38,35 +47,26 @@ var cuminabs = require( './../lib' ); * @returns {Function} benchmark function */ function createBenchmark( len ) { - var y; - var x; - var i; - - x = []; - y = []; - for ( i = 0; i < len; i++ ) { - x.push( ( randu()*20.0 ) - 10.0 ); - y.push( 0.0 ); - } + var x = uniform( len, -10, 10, options ); + var y = zeros( len, options.dtype ); return benchmark; function benchmark( b ) { var v; var i; - for ( i = 0; i < len; i++ ) { - y[ i ] = 0.0; - } + gfill( len, 0.0, y, 1 ); + b.tic(); for ( i = 0; i < b.iterations; i++ ) { x[ 0 ] += 1.0; v = cuminabs( x.length, x, 1, y, 1 ); - if ( isnan( v[ i%len ] ) ) { + if ( isnan( v[ i % len ] ) ) { b.fail( 'should not return NaN' ); } } b.toc(); - if ( isnan( v[ i%len ] ) ) { + if ( isnan( v[ i % len ] ) ) { b.fail( 'should not return NaN' ); } b.pass( 'benchmark finished' ); diff --git a/lib/node_modules/@stdlib/stats/base/cuminabs/benchmark/benchmark.ndarray.js b/lib/node_modules/@stdlib/stats/base/cuminabs/benchmark/benchmark.ndarray.js index af00fc89932e..a4844b9e2b9a 100644 --- a/lib/node_modules/@stdlib/stats/base/cuminabs/benchmark/benchmark.ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/cuminabs/benchmark/benchmark.ndarray.js @@ -21,13 +21,22 @@ // MODULES // var bench = require( '@stdlib/bench' ); -var randu = require( '@stdlib/random/base/randu' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var pow = require( '@stdlib/math/base/special/pow' ); +var uniform = require( '@stdlib/random/array/uniform' ); +var zeros = require( '@stdlib/array/zeros' ); +var gfill = require( '@stdlib/blas/ext/base/gfill' ); var pkg = require( './../package.json' ).name; var cuminabs = require( './../lib/ndarray.js' ); +// VARIABLES // + +var options = { + 'dtype': 'generic' +}; + + // FUNCTIONS // /** @@ -38,35 +47,26 @@ var cuminabs = require( './../lib/ndarray.js' ); * @returns {Function} benchmark function */ function createBenchmark( len ) { - var x; - var y; - var i; - - x = []; - y = []; - for ( i = 0; i < len; i++ ) { - x.push( ( randu()*20.0 ) - 10.0 ); - y.push( 0.0 ); - } + var x = uniform( len, -10, 10, options ); + var y = zeros( len, options.dtype ); return benchmark; function benchmark( b ) { var v; var i; - for ( i = 0; i < len; i++ ) { - y[ i ] = 0.0; - } + gfill( len, 0.0, y, 1 ); + b.tic(); for ( i = 0; i < b.iterations; i++ ) { x[ 0 ] += 1.0; v = cuminabs( x.length, x, 1, 0, y, 1, 0 ); - if ( isnan( v[ i%len ] ) ) { + if ( isnan( v[ i % len ] ) ) { b.fail( 'should not return NaN' ); } } b.toc(); - if ( isnan( v[ i%len ] ) ) { + if ( isnan( v[ i % len ] ) ) { b.fail( 'should not return NaN' ); } b.pass( 'benchmark finished' ); diff --git a/lib/node_modules/@stdlib/stats/base/cuminabs/docs/repl.txt b/lib/node_modules/@stdlib/stats/base/cuminabs/docs/repl.txt index 8881fca3325e..e2cf6c7237b6 100644 --- a/lib/node_modules/@stdlib/stats/base/cuminabs/docs/repl.txt +++ b/lib/node_modules/@stdlib/stats/base/cuminabs/docs/repl.txt @@ -2,8 +2,8 @@ {{alias}}( N, x, strideX, y, strideY ) Computes the cumulative minimum absolute value of a strided array. - The `N` and `stride` parameters determine which elements in `x` and `y` are - accessed at runtime. + The `N` and stride parameters determine which elements in the strided arrays + are accessed at runtime. Indexing is relative to the first index. To introduce an offset, use a typed array view. @@ -43,8 +43,7 @@ // Using `N` and `stride` parameters: > x = [ -2.0, 1.0, 1.0, -5.0, 2.0, -1.0 ]; > y = [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ]; - > var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 ); - > {{alias}}( N, x, 2, y, 2 ) + > {{alias}}( 3, x, 2, y, 2 ) [ 2.0, 0.0, 1.0, 0.0, 1.0, 0.0 ] // Using view offsets: @@ -52,12 +51,12 @@ > var y0 = new {{alias:@stdlib/array/float64}}( x0.length ); > 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 ); - > N = {{alias:@stdlib/math/base/special/floor}}( x0.length / 2 ); - > {{alias}}( N, x1, 2, y1, 1 ) + > {{alias}}( 3, x1, 2, y1, 1 ) [ 2.0, 2.0, 1.0 ] > y0 [ 0.0, 0.0, 0.0, 2.0, 2.0, 1.0 ] + {{alias}}.ndarray( N, x, strideX, offsetX, y, strideY, offsetY ) Computes the cumulative minimum absolute value of a strided array using alternative indexing semantics. @@ -105,8 +104,7 @@ // Advanced indexing: > x = [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0 ]; > y = [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ]; - > var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 ); - > {{alias}}.ndarray( N, x, 2, 1, y, -1, y.length-1 ) + > {{alias}}.ndarray( 3, x, 2, 1, y, -1, y.length-1 ) [ 0.0, 0.0, 0.0, 1.0, 2.0, 2.0 ] See Also diff --git a/lib/node_modules/@stdlib/stats/base/cuminabs/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/base/cuminabs/docs/types/index.d.ts index efef24911f98..becc70e98334 100644 --- a/lib/node_modules/@stdlib/stats/base/cuminabs/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/stats/base/cuminabs/docs/types/index.d.ts @@ -20,7 +20,17 @@ /// -import { NumericArray } from '@stdlib/types/array'; +import { NumericArray, Collection, AccessorArrayLike } from '@stdlib/types/array'; + +/** +* Input array. +*/ +type InputArray = NumericArray | Collection | AccessorArrayLike; + +/** +* Output array. +*/ +type OutputArray = NumericArray | Collection | AccessorArrayLike; /** * Interface describing `cuminabs`. @@ -31,9 +41,9 @@ interface Routine { * * @param N - number of indexed elements * @param x - input array - * @param strideX - `x` stride length + * @param strideX - stride length for `x` * @param y - output array - * @param strideY - `y` stride length + * @param strideY - stride length for `y` * @returns output array * * @example @@ -43,17 +53,17 @@ interface Routine { * cuminabs( x.length, x, 1, y, 1 ); * // y => [ 1.0, 1.0, 1.0 ] */ - ( N: number, x: NumericArray, strideX: number, y: NumericArray, strideY: number ): NumericArray; + ( N: number, x: InputArray, strideX: number, y: T, strideY: number ): T; /** * Computes the cumulative minimum absolute value of a strided array using alternative indexing semantics. * * @param N - number of indexed elements * @param x - input array - * @param strideX - `x` stride length + * @param strideX - stride length for `x` * @param offsetX - starting index for `x` * @param y - output array - * @param strideY - `y` stride length + * @param strideY - stride length for `y` * @param offsetY - starting index for `y` * @returns output array * @@ -64,7 +74,7 @@ interface Routine { * cuminabs.ndarray( x.length, x, 1, 0, y, 1, 0 ); * // y => [ 1.0, 1.0, 1.0 ] */ - ndarray( N: number, x: NumericArray, strideX: number, offsetX: number, y: NumericArray, strideY: number, offsetY: number ): NumericArray; + ndarray( N: number, x: InputArray, strideX: number, offsetX: number, y: T, strideY: number, offsetY: number ): T; } /** @@ -72,9 +82,9 @@ interface Routine { * * @param N - number of indexed elements * @param x - input array -* @param strideX - `x` stride length +* @param strideX - stride length for `x` * @param y - output array -* @param strideY - `y` stride length +* @param strideY - stride length for `y` * @returns output array * * @example diff --git a/lib/node_modules/@stdlib/stats/base/cuminabs/docs/types/test.ts b/lib/node_modules/@stdlib/stats/base/cuminabs/docs/types/test.ts index 6b6d7af70bf7..5ef54b1a9efb 100644 --- a/lib/node_modules/@stdlib/stats/base/cuminabs/docs/types/test.ts +++ b/lib/node_modules/@stdlib/stats/base/cuminabs/docs/types/test.ts @@ -16,6 +16,7 @@ * limitations under the License. */ +import AccessorArray = require( '@stdlib/array/base/accessor' ); import cuminabs = require( './index' ); @@ -26,7 +27,8 @@ import cuminabs = require( './index' ); const x = new Float64Array( 10 ); const y = new Float64Array( 10 ); - cuminabs( x.length, x, 1, y, 1 ); // $ExpectType NumericArray + cuminabs( x.length, x, 1, y, 1 ); // $ExpectType Float64Array + cuminabs( x.length, new AccessorArray( x ), 1, new AccessorArray( y ), 1 ); // $ExpectType AccessorArray } // The compiler throws an error if the function is provided a first argument which is not a number... @@ -123,7 +125,8 @@ import cuminabs = require( './index' ); const x = new Float64Array( 10 ); const y = new Float64Array( 10 ); - cuminabs.ndarray( x.length, x, 1, 0, y, 1, 0 ); // $ExpectType NumericArray + cuminabs.ndarray( x.length, x, 1, 0, y, 1, 0 ); // $ExpectType Float64Array + cuminabs.ndarray( x.length, new AccessorArray( x ), 1, 0, new AccessorArray( y ), 1, 0 ); // $ExpectType AccessorArray } // The compiler throws an error if the `ndarray` method is provided a first argument which is not a number... diff --git a/lib/node_modules/@stdlib/stats/base/cuminabs/examples/index.js b/lib/node_modules/@stdlib/stats/base/cuminabs/examples/index.js index 90ddc21deb45..86de6dd0e4c4 100644 --- a/lib/node_modules/@stdlib/stats/base/cuminabs/examples/index.js +++ b/lib/node_modules/@stdlib/stats/base/cuminabs/examples/index.js @@ -18,20 +18,14 @@ 'use strict'; -var randu = require( '@stdlib/random/base/randu' ); -var round = require( '@stdlib/math/base/special/round' ); var Float64Array = require( '@stdlib/array/float64' ); +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); var cuminabs = require( './../lib' ); -var y; -var x; -var i; - -x = new Float64Array( 10 ); -y = new Float64Array( x.length ); -for ( i = 0; i < x.length; i++ ) { - x[ i ] = round( (randu()*100.0) - 50.0 ); -} +var x = discreteUniform( 10, -50, 50, { + 'dtype': 'float64' +}); +var y = new Float64Array( x.length ); console.log( x ); console.log( y ); diff --git a/lib/node_modules/@stdlib/stats/base/cuminabs/lib/accessors.js b/lib/node_modules/@stdlib/stats/base/cuminabs/lib/accessors.js new file mode 100644 index 000000000000..c944a20f5a9c --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/cuminabs/lib/accessors.js @@ -0,0 +1,110 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 isnan = require( '@stdlib/math/base/assert/is-nan' ); +var abs = require( '@stdlib/math/base/special/abs' ); + + +// MAIN // + +/** +* Computes the cumulative minimum absolute value of a strided array. +* +* @private +* @param {PositiveInteger} N - number of indexed elements +* @param {Object} x - input array object +* @param {Collection} x.data - input array data +* @param {Array} x.accessors - array element accessors +* @param {integer} strideX - stride length for `x` +* @param {NonNegativeInteger} offsetX - starting index for `x` +* @param {Object} y - output array object +* @param {Collection} y.data - output array data +* @param {Array} y.accessors - array element accessors +* @param {integer} strideY - stride length for `y` +* @param {NonNegativeInteger} offsetY - starting index for `y` +* @returns {Object} output array object +* +* @example +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +* var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +* +* var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ]; +* var y = [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ]; +* +* cuminabs( 4, arraylike2object( toAccessorArray( x ) ), 2, 1, arraylike2object( toAccessorArray( y ) ), 1, 0 ); +* // y => [ 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0 ]; +*/ +function cuminabs( N, x, strideX, offsetX, y, strideY, offsetY ) { + var xbuf; + var ybuf; + var xget; + var yset; + var min; + var ix; + var iy; + var v; + var i; + + // Cache references to array data: + xbuf = x.data; + ybuf = y.data; + + // Cache references to element accessors: + xget = x.accessors[ 0 ]; + yset = y.accessors[ 1 ]; + + ix = offsetX; + iy = offsetY; + + min = abs( xget( xbuf, ix ) ); + yset( ybuf, iy, min ); + + iy += strideY; + i = 1; + if ( isnan( min ) === false ) { + for ( i; i < N; i++ ) { + ix += strideX; + v = abs( xget( xbuf, ix ) ); + if ( isnan( v ) ) { + min = v; + break; + } + if ( v < min ) { + min = v; + } + yset( ybuf, iy, min ); + iy += strideY; + } + } + if ( isnan( min ) ) { + for ( i; i < N; i++ ) { + yset( ybuf, iy, min ); + iy += strideY; + } + } + return y; +} + + +// EXPORTS // + +module.exports = cuminabs; diff --git a/lib/node_modules/@stdlib/stats/base/cuminabs/lib/main.js b/lib/node_modules/@stdlib/stats/base/cuminabs/lib/main.js index 9b6dffe2ddf3..6503d1d5acc6 100644 --- a/lib/node_modules/@stdlib/stats/base/cuminabs/lib/main.js +++ b/lib/node_modules/@stdlib/stats/base/cuminabs/lib/main.js @@ -20,8 +20,8 @@ // MODULES // -var isnan = require( '@stdlib/math/base/assert/is-nan' ); -var abs = require( '@stdlib/math/base/special/abs' ); +var stride2offset = require( '@stdlib/strided/base/stride2offset' ); +var ndarray = require( './ndarray.js' ); // MAIN // @@ -31,9 +31,9 @@ var abs = require( '@stdlib/math/base/special/abs' ); * * @param {PositiveInteger} N - number of indexed elements * @param {NumericArray} x - input array -* @param {integer} strideX - `x` stride length +* @param {integer} strideX - stride length for `x` * @param {NumericArray} y - output array -* @param {integer} strideY - `y` stride length +* @param {integer} strideY - stride length for `y` * @returns {NumericArray} output array * * @example @@ -45,52 +45,9 @@ var abs = require( '@stdlib/math/base/special/abs' ); * // returns [ 1.0, 1.0, 1.0 ] */ function cuminabs( N, x, strideX, y, strideY ) { - var min; - var ix; - var iy; - var v; - var i; - - if ( N <= 0 ) { - return y; - } - if ( strideX < 0 ) { - ix = (1-N) * strideX; - } else { - ix = 0; - } - if ( strideY < 0 ) { - iy = (1-N) * strideY; - } else { - iy = 0; - } - min = abs( x[ ix ] ); - y[ iy ] = min; - - iy += strideY; - i = 1; - if ( isnan( min ) === false ) { - for ( i; i < N; i++ ) { - ix += strideX; - v = abs( x[ ix ] ); - if ( isnan( v ) ) { - min = v; - break; - } - if ( v < min ) { - min = v; - } - y[ iy ] = min; - iy += strideY; - } - } - if ( isnan( min ) ) { - for ( i; i < N; i++ ) { - y[ iy ] = min; - iy += strideY; - } - } - return y; + var ix = stride2offset( N, strideX ); + var iy = stride2offset( N, strideY ); + return ndarray( N, x, strideX, ix, y, strideY, iy ); } diff --git a/lib/node_modules/@stdlib/stats/base/cuminabs/lib/ndarray.js b/lib/node_modules/@stdlib/stats/base/cuminabs/lib/ndarray.js index 73ed1d9c851a..7970d017c29b 100644 --- a/lib/node_modules/@stdlib/stats/base/cuminabs/lib/ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/cuminabs/lib/ndarray.js @@ -22,6 +22,8 @@ var isnan = require( '@stdlib/math/base/assert/is-nan' ); var abs = require( '@stdlib/math/base/special/abs' ); +var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +var accessors = require( './accessors.js' ); // MAIN // @@ -31,33 +33,38 @@ var abs = require( '@stdlib/math/base/special/abs' ); * * @param {PositiveInteger} N - number of indexed elements * @param {NumericArray} x - input array -* @param {integer} strideX - `x` stride length +* @param {integer} strideX - stride length for `x` * @param {NonNegativeInteger} offsetX - starting index for `x` * @param {NumericArray} y - output array -* @param {integer} strideY - `y` stride length +* @param {integer} strideY - stride length for `y` * @param {NonNegativeInteger} offsetY - starting index for `y` * @returns {NumericArray} output array * * @example -* var floor = require( '@stdlib/math/base/special/floor' ); -* * var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ]; * var y = [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ]; -* var N = floor( x.length / 2 ); * -* var v = cuminabs( N, x, 2, 1, y, 1, 0 ); +* var v = cuminabs( 4, x, 2, 1, y, 1, 0 ); * // returns [ 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0 ] */ function cuminabs( N, x, strideX, offsetX, y, strideY, offsetY ) { var min; var ix; var iy; + var ox; + var oy; var v; var i; if ( N <= 0 ) { return y; } + ox = arraylike2object( x ); + oy = arraylike2object( y ); + if ( ox.accessorProtocol || oy.accessorProtocol ) { + accessors( N, ox, strideX, offsetX, oy, strideY, offsetY ); + return y; + } ix = offsetX; iy = offsetY; diff --git a/lib/node_modules/@stdlib/stats/base/cuminabs/test/test.main.js b/lib/node_modules/@stdlib/stats/base/cuminabs/test/test.main.js index 1924c34fe64a..3d2aeaa3f12a 100644 --- a/lib/node_modules/@stdlib/stats/base/cuminabs/test/test.main.js +++ b/lib/node_modules/@stdlib/stats/base/cuminabs/test/test.main.js @@ -21,9 +21,9 @@ // MODULES // var tape = require( 'tape' ); -var floor = require( '@stdlib/math/base/special/floor' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' ); +var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); var Float64Array = require( '@stdlib/array/float64' ); var cuminabs = require( './../lib/main.js' ); @@ -109,6 +109,74 @@ tape( 'the function computes the cumulative minimum absolute value', function te t.end(); }); +tape( 'the function computes the cumulative minimum absolute value (accessor)', function test( t ) { + var expected; + var x; + var y; + var i; + + x = [ 1.0, -2.0, 3.0, -4.0, 5.0 ]; + y = [ 0.0, 0.0, 0.0, 0.0, 0.0 ]; + cuminabs( x.length, toAccessorArray( x ), 1, toAccessorArray( y ), 1 ); + + expected = [ + 1.0, + 1.0, + 1.0, + 1.0, + 1.0 + ]; + t.deepEqual( y, expected, 'returns expected value' ); + + x = [ -0.0, 0.0, -0.0 ]; + y = [ -0.0, -0.0, -0.0 ]; + cuminabs( x.length, toAccessorArray( x ), 1, toAccessorArray( y ), 1 ); + + expected = [ + 0.0, + 0.0, + 0.0 + ]; + for ( i = 0; i < y.length; i++ ) { + t.strictEqual( isPositiveZero( y[ i ] ), true, 'returns expected value. i: ' + i ); + } + + x = [ NaN ]; + y = [ 0.0 ]; + cuminabs( x.length, toAccessorArray( x ), 1, toAccessorArray( y ), 1 ); + + for ( i = 0; i < y.length; i++ ) { + t.strictEqual( isnan( y[ i ] ), true, 'returns expected value. i: ' + i ); + } + + x = [ NaN, NaN ]; + y = [ 0.0, 0.0 ]; + cuminabs( x.length, toAccessorArray( x ), 1, toAccessorArray( y ), 1 ); + + for ( i = 0; i < y.length; i++ ) { + t.strictEqual( isnan( y[ i ] ), true, 'returns expected value. i: ' + i ); + } + + x = [ 1.0, NaN, 3.0, NaN ]; + y = [ 0.0, 0.0, 0.0, 0.0 ]; + cuminabs( x.length, toAccessorArray( x ), 1, toAccessorArray( y ), 1 ); + + expected = [ + 1.0, + NaN, + NaN, + NaN + ]; + for ( i = 0; i < y.length; i++ ) { + if ( isnan( expected[ i ] ) ) { + t.strictEqual( isnan( y[ i ] ), true, 'returns expected value. i: ' + i ); + } else { + t.strictEqual( y[ i ], expected[ i ], true, 'returns expected value. i: ' + i ); + } + } + t.end(); +}); + tape( 'the function returns a reference to the output array', function test( t ) { var out; var x; @@ -123,6 +191,20 @@ tape( 'the function returns a reference to the output array', function test( t ) t.end(); }); +tape( 'the function returns a reference to the output array (accessor)', function test( t ) { + var out; + var x; + var y; + + x = toAccessorArray( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); + y = toAccessorArray( [ 0.0, 0.0, 0.0, 0.0, 0.0 ] ); + + out = cuminabs( x.length, x, 1, y, 1 ); + t.strictEqual( out, y, 'same reference' ); + + t.end(); +}); + tape( 'if provided an `N` parameter less than or equal to `0`, the function returns `y` unchanged', function test( t ) { var expected; var x; @@ -172,6 +254,36 @@ tape( 'the function supports an `x` stride', function test( t ) { t.end(); }); +tape( 'the function supports an `x` stride (accessor)', function test( t ) { + var expected; + var x; + var y; + var N; + + x = [ + 1.0, // 0 + -2.0, + 3.0, // 1 + 4.0, + -5.0 // 2 + ]; + y = [ + 0.0, // 0 + 0.0, // 1 + 0.0, // 2 + 0.0, + 0.0 + ]; + N = 3; + + cuminabs( N, toAccessorArray( x ), 2, toAccessorArray( y ), 1 ); + + expected = [ 1.0, 1.0, 1.0, 0.0, 0.0 ]; + t.deepEqual( y, expected, 'returns expected value' ); + + t.end(); +}); + tape( 'the function supports a `y` stride', function test( t ) { var expected; var x; @@ -202,6 +314,36 @@ tape( 'the function supports a `y` stride', function test( t ) { t.end(); }); +tape( 'the function supports a `y` stride (accessor)', function test( t ) { + var expected; + var x; + var y; + var N; + + x = [ + 1.0, // 0 + -2.0, // 1 + 3.0, // 2 + 4.0, + 5.0 + ]; + y = [ + 0.0, // 0 + 0.0, + 0.0, // 1 + 0.0, + 0.0 // 2 + ]; + N = 3; + + cuminabs( N, toAccessorArray( x ), 1, toAccessorArray( y ), 2 ); + + expected = [ 1.0, 0.0, 1.0, 0.0, 1.0 ]; + t.deepEqual( y, expected, 'returns expected value' ); + + t.end(); +}); + tape( 'the function supports negative strides', function test( t ) { var expected; var x; @@ -232,6 +374,36 @@ tape( 'the function supports negative strides', function test( t ) { t.end(); }); +tape( 'the function supports negative strides (accessor)', function test( t ) { + var expected; + var x; + var y; + var N; + + x = [ + 1.0, // 2 + -2.0, + 3.0, // 1 + 4.0, + -5.0 // 0 + ]; + y = [ + 0.0, // 2 + 0.0, // 1 + 0.0, // 0 + 0.0, + 0.0 + ]; + N = 3; + + cuminabs( N, toAccessorArray( x ), -2, toAccessorArray( y ), -1 ); + + expected = [ 1.0, 3.0, 5.0, 0.0, 0.0 ]; + t.deepEqual( y, expected, 'returns expected value' ); + + t.end(); +}); + tape( 'the function supports complex access patterns', function test( t ) { var expected; var x; @@ -264,13 +436,44 @@ tape( 'the function supports complex access patterns', function test( t ) { t.end(); }); +tape( 'the function supports complex access patterns (accessor)', function test( t ) { + var expected; + var x; + var y; + var N; + + x = [ + 1.0, // 0 + 2.0, + -3.0, // 1 + 4.0, + 5.0, // 2 + 6.0 + ]; + y = [ + 0.0, // 2 + 0.0, // 1 + 0.0, // 0 + 0.0, + 0.0, + 0.0 + ]; + N = 3; + + cuminabs( N, toAccessorArray( x ), 2, toAccessorArray( y ), -1 ); + + expected = [ 1.0, 1.0, 1.0, 0.0, 0.0, 0.0 ]; + t.deepEqual( y, expected, 'returns expected value' ); + + t.end(); +}); + tape( 'the function supports view offsets', function test( t ) { var expected; var x0; var y0; var x1; var y1; - var N; // Initial arrays... x0 = new Float64Array([ @@ -294,9 +497,43 @@ tape( 'the function supports view offsets', function test( t ) { x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // begin at 2nd element y1 = new Float64Array( y0.buffer, y0.BYTES_PER_ELEMENT*3 ); // begin at the 4th element - N = floor( x0.length / 2 ); + cuminabs( 3, x1, -2, y1, 1 ); + expected = new Float64Array( [ 0.0, 0.0, 0.0, 6.0, 4.0, 2.0 ] ); + + t.deepEqual( y0, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports view offsets (accessor)', function test( t ) { + var expected; + var x0; + var y0; + var x1; + var y1; + + // Initial arrays... + x0 = new Float64Array([ + 1.0, + 2.0, // 2 + 3.0, + 4.0, // 1 + 5.0, + 6.0 // 0 + ]); + y0 = new Float64Array([ + 0.0, + 0.0, + 0.0, + 0.0, // 0 + 0.0, // 1 + 0.0 // 2 + ]); + + // Create offset views... + x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // begin at 2nd element + y1 = new Float64Array( y0.buffer, y0.BYTES_PER_ELEMENT*3 ); // begin at the 4th element - cuminabs( N, x1, -2, y1, 1 ); + cuminabs( 3, toAccessorArray( x1 ), -2, toAccessorArray( y1 ), 1 ); expected = new Float64Array( [ 0.0, 0.0, 0.0, 6.0, 4.0, 2.0 ] ); t.deepEqual( y0, expected, 'returns expected value' ); diff --git a/lib/node_modules/@stdlib/stats/base/cuminabs/test/test.ndarray.js b/lib/node_modules/@stdlib/stats/base/cuminabs/test/test.ndarray.js index 188e85d243a7..da983a7e5c4a 100644 --- a/lib/node_modules/@stdlib/stats/base/cuminabs/test/test.ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/cuminabs/test/test.ndarray.js @@ -21,8 +21,8 @@ // MODULES // var tape = require( 'tape' ); -var floor = require( '@stdlib/math/base/special/floor' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); var isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' ); var cuminabs = require( './../lib/ndarray.js' ); @@ -108,6 +108,74 @@ tape( 'the function calculates the cumulative minimum absolute value', function t.end(); }); +tape( 'the function computes the cumulative minimum absolute value (accessor)', function test( t ) { + var expected; + var x; + var y; + var i; + + x = [ 1.0, -2.0, 3.0, -4.0, 5.0 ]; + y = [ 0.0, 0.0, 0.0, 0.0, 0.0 ]; + cuminabs( x.length, toAccessorArray( x ), 1, 0, toAccessorArray( y ), 1, 0 ); + + expected = [ + 1.0, + 1.0, + 1.0, + 1.0, + 1.0 + ]; + t.deepEqual( y, expected, 'returns expected value' ); + + x = [ -0.0, 0.0, -0.0 ]; + y = [ -0.0, -0.0, -0.0 ]; + cuminabs( x.length, toAccessorArray( x ), 1, 0, toAccessorArray( y ), 1, 0 ); + + expected = [ + 0.0, + 0.0, + 0.0 + ]; + for ( i = 0; i < y.length; i++ ) { + t.strictEqual( isPositiveZero( y[ i ] ), true, 'returns expected value. i: ' + i ); + } + + x = [ NaN ]; + y = [ 0.0 ]; + cuminabs( x.length, toAccessorArray( x ), 1, 0, toAccessorArray( y ), 1, 0 ); + + for ( i = 0; i < y.length; i++ ) { + t.strictEqual( isnan( y[ i ] ), true, 'returns expected value. i: ' + i ); + } + + x = [ NaN, NaN ]; + y = [ 0.0, 0.0 ]; + cuminabs( x.length, toAccessorArray( x ), 1, 0, toAccessorArray( y ), 1, 0 ); + + for ( i = 0; i < y.length; i++ ) { + t.strictEqual( isnan( y[ i ] ), true, 'returns expected value. i: ' + i ); + } + + x = [ 1.0, NaN, 3.0, NaN ]; + y = [ 0.0, 0.0, 0.0, 0.0 ]; + cuminabs( x.length, toAccessorArray( x ), 1, 0, toAccessorArray( y ), 1, 0 ); + + expected = [ + 1.0, + NaN, + NaN, + NaN + ]; + for ( i = 0; i < y.length; i++ ) { + if ( isnan( expected[ i ] ) ) { + t.strictEqual( isnan( y[ i ] ), true, 'returns expected value. i: ' + i ); + } else { + t.strictEqual( y[ i ], expected[ i ], true, 'returns expected value. i: ' + i ); + } + } + t.end(); +}); + tape( 'the function returns a reference to the output array', function test( t ) { var out; var x; @@ -122,6 +190,20 @@ tape( 'the function returns a reference to the output array', function test( t ) t.end(); }); +tape( 'the function returns a reference to the output array (accessor)', function test( t ) { + var out; + var x; + var y; + + x = toAccessorArray( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); + y = toAccessorArray( [ 0.0, 0.0, 0.0, 0.0, 0.0 ] ); + + out = cuminabs( x.length, x, 1, 0, y, 1, 0 ); + t.strictEqual( out, y, 'same reference' ); + + t.end(); +}); + tape( 'if provided an `N` parameter less than or equal to `0`, the function returns `y` unchanged', function test( t ) { var expected; var x; @@ -171,6 +253,36 @@ tape( 'the function supports an `x` stride', function test( t ) { t.end(); }); +tape( 'the function supports an `x` stride (accessor)', function test( t ) { + var expected; + var x; + var y; + var N; + + x = [ + 1.0, // 0 + -2.0, + 3.0, // 1 + 4.0, + -5.0 // 2 + ]; + y = [ + 0.0, // 0 + 0.0, // 1 + 0.0, // 2 + 0.0, + 0.0 + ]; + N = 3; + + cuminabs( N, toAccessorArray( x ), 2, 0, toAccessorArray( y ), 1, 0 ); + + expected = [ 1.0, 1.0, 1.0, 0.0, 0.0 ]; + t.deepEqual( y, expected, 'returns expected value' ); + + t.end(); +}); + tape( 'the function supports a `y` stride', function test( t ) { var expected; var x; @@ -201,6 +313,36 @@ tape( 'the function supports a `y` stride', function test( t ) { t.end(); }); +tape( 'the function supports a `y` stride (accessor)', function test( t ) { + var expected; + var x; + var y; + var N; + + x = [ + 1.0, // 0 + -2.0, // 1 + 3.0, // 2 + 4.0, + 5.0 + ]; + y = [ + 0.0, // 0 + 0.0, + 0.0, // 1 + 0.0, + 0.0 // 2 + ]; + N = 3; + + cuminabs( N, toAccessorArray( x ), 1, 0, toAccessorArray( y ), 2, 0 ); + + expected = [ 1.0, 0.0, 1.0, 0.0, 1.0 ]; + t.deepEqual( y, expected, 'returns expected value' ); + + t.end(); +}); + tape( 'the function supports negative strides', function test( t ) { var expected; var x; @@ -231,9 +373,72 @@ tape( 'the function supports negative strides', function test( t ) { t.end(); }); -tape( 'the function supports an `x` offset', function test( t ) { +tape( 'the function supports negative strides (accessor)', function test( t ) { var expected; + var x; + var y; var N; + + x = [ + 1.0, // 2 + -2.0, + 3.0, // 1 + 4.0, + -5.0 // 0 + ]; + y = [ + 0.0, // 2 + 0.0, // 1 + 0.0, // 0 + 0.0, + 0.0 + ]; + N = 3; + + cuminabs( N, toAccessorArray( x ), -2, x.length-1, toAccessorArray( y ), -1, 2 ); + + expected = [ 1.0, 3.0, 5.0, 0.0, 0.0 ]; + t.deepEqual( y, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports an `x` offset', function test( t ) { + var expected; + var x; + var y; + + x = [ + 2.0, + 1.0, // 0 + 2.0, + -2.0, // 1 + -2.0, + 2.0, // 2 + 3.0, + 4.0 // 3 + ]; + y = [ + 0.0, // 0 + 0.0, // 1 + 0.0, // 2 + 0.0, // 3 + 0.0, + 0.0, + 0.0, + 0.0 + ]; + + cuminabs( 4, x, 2, 1, y, 1, 0 ); + + expected = [ 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0 ]; + t.deepEqual( y, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports an `x` offset (accessor)', function test( t ) { + var expected; var x; var y; @@ -257,9 +462,8 @@ tape( 'the function supports an `x` offset', function test( t ) { 0.0, 0.0 ]; - N = floor( x.length / 2 ); - cuminabs( N, x, 2, 1, y, 1, 0 ); + cuminabs( 4, toAccessorArray( x ), 2, 1, toAccessorArray( y ), 1, 0 ); expected = [ 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0 ]; t.deepEqual( y, expected, 'returns expected value' ); @@ -269,7 +473,6 @@ tape( 'the function supports an `x` offset', function test( t ) { tape( 'the function supports a `y` offset', function test( t ) { var expected; - var N; var x; var y; @@ -293,9 +496,42 @@ tape( 'the function supports a `y` offset', function test( t ) { 0.0, 0.0 // 3 ]; - N = floor( x.length / 2 ); - cuminabs( N, x, 1, 0, y, 2, 1 ); + cuminabs( 4, x, 1, 0, y, 2, 1 ); + + expected = [ 0.0, 2.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0 ]; + t.deepEqual( y, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports a `y` offset (accessor)', function test( t ) { + var expected; + var x; + var y; + + x = [ + 2.0, // 0 + 1.0, // 1 + 2.0, // 2 + -2.0, // 3 + -2.0, + 2.0, + 3.0, + 4.0 + ]; + y = [ + 0.0, + 0.0, // 0 + 0.0, + 0.0, // 1 + 0.0, + 0.0, // 2 + 0.0, + 0.0 // 3 + ]; + + cuminabs( 4, toAccessorArray( x ), 1, 0, toAccessorArray( y ), 2, 1 ); expected = [ 0.0, 2.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0 ]; t.deepEqual( y, expected, 'returns expected value' ); @@ -307,7 +543,6 @@ tape( 'the function supports complex access patterns', function test( t ) { var expected; var x; var y; - var N; x = [ 1.0, // 0 @@ -325,9 +560,38 @@ tape( 'the function supports complex access patterns', function test( t ) { 0.0, 0.0 ]; - N = 3; - cuminabs( N, x, 2, 0, y, -1, 2 ); + cuminabs( 3, x, 2, 0, y, -1, 2 ); + + expected = [ 1.0, 1.0, 1.0, 0.0, 0.0, 0.0 ]; + t.deepEqual( y, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports complex access patterns (accessor)', function test( t ) { + var expected; + var x; + var y; + + x = [ + 1.0, // 0 + 2.0, + -3.0, // 1 + 4.0, + 5.0, // 2 + 6.0 + ]; + y = [ + 0.0, // 2 + 0.0, // 1 + 0.0, // 0 + 0.0, + 0.0, + 0.0 + ]; + + cuminabs( 3, toAccessorArray( x ), 2, 0, toAccessorArray( y ), -1, 2 ); expected = [ 1.0, 1.0, 1.0, 0.0, 0.0, 0.0 ]; t.deepEqual( y, expected, 'returns expected value' );