diff --git a/lib/node_modules/@stdlib/stats/base/cumin/README.md b/lib/node_modules/@stdlib/stats/base/cumin/README.md index 85c7af9fd962..7e921f0805f8 100644 --- a/lib/node_modules/@stdlib/stats/base/cumin/README.md +++ b/lib/node_modules/@stdlib/stats/base/cumin/README.md @@ -52,11 +52,11 @@ The function has the following parameters: - **N**: number of indexed elements. - **x**: input [`Array`][mdn-array] or [`typed array`][mdn-typed-array]. -- **strideX**: index increment for `x`. +- **strideX**: stride length for `x`. - **y**: output [`Array`][mdn-array] or [`typed array`][mdn-typed-array]. -- **strideY**: index increment for `y`. +- **strideY**: stride length 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 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 minimum 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 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 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 @@ cumin.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 ([`dcumin`][@stdlib/stats/base/dcumin], [`scumin`][@stdlib/stats/base/scumin], 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 @@ cumin.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 Float64Array = require( '@stdlib/array/float64' ); +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); var cumin = require( '@stdlib/stats/base/cumin' ); -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 ); @@ -187,6 +182,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/cumin/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/base/cumin/benchmark/benchmark.js index 7c972b5823b1..fdf908048f80 100644 --- a/lib/node_modules/@stdlib/stats/base/cumin/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/stats/base/cumin/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 uniform = require( '@stdlib/random/array/uniform' ); +var zeros = require( '@stdlib/array/zeros' ); +var gfill = require( '@stdlib/blas/ext/base/gfill' ); var pow = require( '@stdlib/math/base/special/pow' ); var pkg = require( './../package.json' ).name; var cumin = require( './../lib/cumin.js' ); +// VARIABLES // + +var options = { + 'dtype': 'generic' +}; + + // FUNCTIONS // /** @@ -38,35 +47,26 @@ var cumin = require( './../lib/cumin.js' ); * @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 = cumin( 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/cumin/benchmark/benchmark.ndarray.js b/lib/node_modules/@stdlib/stats/base/cumin/benchmark/benchmark.ndarray.js index dee59c9dd59d..306c1b485b45 100644 --- a/lib/node_modules/@stdlib/stats/base/cumin/benchmark/benchmark.ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/cumin/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 cumin = require( './../lib/ndarray.js' ); +// VARIABLES // + +var options = { + 'dtype': 'generic' +}; + + // FUNCTIONS // /** @@ -38,35 +47,26 @@ var cumin = 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 = cumin( 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/cumin/docs/repl.txt b/lib/node_modules/@stdlib/stats/base/cumin/docs/repl.txt index 996129585498..f64ec69acd5e 100644 --- a/lib/node_modules/@stdlib/stats/base/cumin/docs/repl.txt +++ b/lib/node_modules/@stdlib/stats/base/cumin/docs/repl.txt @@ -2,8 +2,8 @@ {{alias}}( N, x, strideX, y, strideY ) Computes the cumulative minimum 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. @@ -19,13 +19,13 @@ Input array. strideX: integer - Index increment for `x`. + Stride length for `x`. y: Array|TypedArray Output array. strideY: integer - Index increment for `y`. + Stride length for `y`. Returns ------- @@ -40,11 +40,10 @@ > {{alias}}( x.length, x, 1, y, 1 ) [ 1.0, -2.0, -2.0 ] - // Using `N` and `stride` parameters: + // 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, -2.0, 0.0, -2.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, -2.0 ] > y0 [ 0.0, 0.0, 0.0, -2.0, -2.0, -2.0 ] + {{alias}}.ndarray( N, x, strideX, offsetX, y, strideY, offsetY ) Computes the cumulative minimum of a strided array using alternative indexing semantics. @@ -75,7 +74,7 @@ Input array. strideX: integer - Index increment for `x`. + Stride length for `x`. offsetX: integer Starting index for `x`. @@ -84,7 +83,7 @@ Output array. strideY: integer - Index increment for `y`. + Stride length for `y`. offsetY: integer Starting index for `y`. @@ -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, -2.0, -2.0, -2.0 ] See Also diff --git a/lib/node_modules/@stdlib/stats/base/cumin/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/base/cumin/docs/types/index.d.ts index 7a515ccd2eb2..09820cdf5a93 100644 --- a/lib/node_modules/@stdlib/stats/base/cumin/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/stats/base/cumin/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 `cumin`. @@ -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 { * cumin( x.length, x, 1, y, 1 ); * // y => [ 1.0, 1.0, 2.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 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 { * cumin.ndarray( x.length, x, 1, 0, y, 1, 0 ); * // y => [ 1.0, 1.0, 2.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/cumin/docs/types/test.ts b/lib/node_modules/@stdlib/stats/base/cumin/docs/types/test.ts index f3499705e717..1674a62a02e6 100644 --- a/lib/node_modules/@stdlib/stats/base/cumin/docs/types/test.ts +++ b/lib/node_modules/@stdlib/stats/base/cumin/docs/types/test.ts @@ -16,6 +16,7 @@ * limitations under the License. */ +import AccessorArray = require( '@stdlib/array/base/accessor' ); import cumin = require( './index' ); @@ -27,6 +28,7 @@ import cumin = require( './index' ); const y = new Float64Array( 10 ); cumin( x.length, x, 1, y, 1 ); // $ExpectType NumericArray + cumin( 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... @@ -124,6 +126,7 @@ import cumin = require( './index' ); const y = new Float64Array( 10 ); cumin.ndarray( x.length, x, 1, 0, y, 1, 0 ); // $ExpectType NumericArray + cumin.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/cumin/examples/index.js b/lib/node_modules/@stdlib/stats/base/cumin/examples/index.js index 4c39b122ab37..5eb3f90a3ec0 100644 --- a/lib/node_modules/@stdlib/stats/base/cumin/examples/index.js +++ b/lib/node_modules/@stdlib/stats/base/cumin/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 discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); var Float64Array = require( '@stdlib/array/float64' ); var cumin = 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 ); -} +var x = discreteUniform( 10, 0, 100, { + 'dtype': 'float64' +}); +var y = new Float64Array( x.length ); console.log( x ); console.log( y ); diff --git a/lib/node_modules/@stdlib/stats/base/cumin/lib/accessors.js b/lib/node_modules/@stdlib/stats/base/cumin/lib/accessors.js new file mode 100644 index 000000000000..3711a4d5c3b4 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/cumin/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 isNegativeZero = require( '@stdlib/math/base/assert/is-negative-zero' ); + + +// MAIN // + +/** +* Computes the cumulative minimum 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 ]; +* +* cumin( 4, arraylike2object( toAccessorArray( x ) ), 2, 1, arraylike2object( toAccessorArray( y ) ), 1, 0 ); +* // y => [ 1.0, -2.0, -2.0, -2.0, 0.0, 0.0, 0.0, 0.0 ]; +*/ +function cumin( 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 = xget( xbuf, ix ); + yset( ybuf, iy, min ); + + iy += strideY; + i = 1; + if ( isnan( min ) === false ) { + for ( i; i < N; i++ ) { + ix += strideX; + v = xget( xbuf, ix ); + if ( isnan( v ) ) { + min = v; + break; + } + if ( v < min || ( v === min && isNegativeZero( v ) ) ) { + 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 = cumin; diff --git a/lib/node_modules/@stdlib/stats/base/cumin/lib/cumin.js b/lib/node_modules/@stdlib/stats/base/cumin/lib/cumin.js index 8cef7fb42a15..478e9f58fde1 100644 --- a/lib/node_modules/@stdlib/stats/base/cumin/lib/cumin.js +++ b/lib/node_modules/@stdlib/stats/base/cumin/lib/cumin.js @@ -20,8 +20,8 @@ // MODULES // -var isnan = require( '@stdlib/math/base/assert/is-nan' ); -var isNegativeZero = require( '@stdlib/math/base/assert/is-negative-zero' ); +var stride2offset = require( '@stdlib/strided/base/stride2offset' ); +var ndarray = require( './ndarray.js' ); // MAIN // @@ -45,52 +45,9 @@ var isNegativeZero = require( '@stdlib/math/base/assert/is-negative-zero' ); * // returns [ 1.0, -2.0, -2.0 ] */ function cumin( 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 = x[ ix ]; - y[ iy ] = min; - - iy += strideY; - i = 1; - if ( isnan( min ) === false ) { - for ( i; i < N; i++ ) { - ix += strideX; - v = x[ ix ]; - if ( isnan( v ) ) { - min = v; - break; - } - if ( v < min || ( v === min && isNegativeZero( v ) ) ) { - 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/cumin/lib/ndarray.js b/lib/node_modules/@stdlib/stats/base/cumin/lib/ndarray.js index e46c1fb3fc9e..66d90655dfc8 100644 --- a/lib/node_modules/@stdlib/stats/base/cumin/lib/ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/cumin/lib/ndarray.js @@ -22,6 +22,8 @@ var isnan = require( '@stdlib/math/base/assert/is-nan' ); var isNegativeZero = require( '@stdlib/math/base/assert/is-negative-zero' ); +var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +var accessors = require( './accessors.js' ); // MAIN // @@ -31,33 +33,38 @@ var isNegativeZero = require( '@stdlib/math/base/assert/is-negative-zero' ); * * @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 = cumin( N, x, 2, 1, y, 1, 0 ); +* var v = cumin( 4, x, 2, 1, y, 1, 0 ); * // returns [ 1.0, -2.0, -2.0, -2.0, 0.0, 0.0, 0.0, 0.0 ] */ function cumin( 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/cumin/test/test.cumin.js b/lib/node_modules/@stdlib/stats/base/cumin/test/test.cumin.js index f1b0c567bf1c..fc55de351e5f 100644 --- a/lib/node_modules/@stdlib/stats/base/cumin/test/test.cumin.js +++ b/lib/node_modules/@stdlib/stats/base/cumin/test/test.cumin.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 isNegativeZero = require( '@stdlib/math/base/assert/is-negative-zero' ); +var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); var Float64Array = require( '@stdlib/array/float64' ); var cumin = require( './../lib/cumin.js' ); @@ -113,6 +113,78 @@ tape( 'the function computes the cumulative minimum', function test( t ) { t.end(); }); +tape( 'the function computes the cumulative minimum (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 ]; + cumin( x.length, toAccessorArray( x ), 1, toAccessorArray( y ), 1 ); + + expected = [ + 1.0, + -2.0, + -2.0, + -4.0, + -4.0 + ]; + t.deepEqual( y, expected, 'returns expected value' ); + + x = [ 0.0, -0.0, 0.0 ]; + y = [ 0.0, 0.0, 0.0 ]; + cumin( x.length, toAccessorArray( x ), 1, toAccessorArray( y ), 1 ); + + expected = [ + 0.0, + -0.0, + -0.0 + ]; + for ( i = 0; i < y.length; i++ ) { + if ( isNegativeZero( expected[ i ] ) ) { + t.strictEqual( isNegativeZero( y[ i ] ), true, 'returns expected value. i: ' + i ); + } else { + t.strictEqual( y[ i ], expected[ i ], true, 'returns expected value. i: ' + i ); + } + } + + x = [ NaN ]; + y = [ 0.0 ]; + cumin( 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 ]; + cumin( 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 ]; + cumin( 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; @@ -127,6 +199,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 = cumin( 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; @@ -176,6 +262,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; + + cumin( N, toAccessorArray( x ), 2, toAccessorArray( y ), 1 ); + + expected = [ 1.0, 1.0, -5.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; @@ -206,6 +322,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; + + cumin( N, toAccessorArray( x ), 1, toAccessorArray( y ), 2 ); + + expected = [ 1.0, 0.0, -2.0, 0.0, -2.0 ]; + t.deepEqual( y, expected, 'returns expected value' ); + + t.end(); +}); + tape( 'the function supports negative strides', function test( t ) { var expected; var x; @@ -236,6 +382,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; + + cumin( N, toAccessorArray( x ), -2, toAccessorArray( y ), -1 ); + + expected = [ -5.0, -5.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; @@ -268,6 +444,38 @@ 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; + + cumin( N, toAccessorArray( x ), 2, toAccessorArray( y ), -1 ); + + expected = [ -3.0, -3.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; @@ -298,7 +506,7 @@ 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 ); + N = 3; cumin( N, x1, -2, y1, 1 ); expected = new Float64Array( [ 0.0, 0.0, 0.0, 6.0, 4.0, 2.0 ] ); @@ -306,3 +514,42 @@ tape( 'the function supports view offsets', function test( t ) { 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; + var N; + + // 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 + + N = 3; + + cumin( N, 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' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/stats/base/cumin/test/test.ndarray.js b/lib/node_modules/@stdlib/stats/base/cumin/test/test.ndarray.js index fd05810c1a1c..c667915cf7f3 100644 --- a/lib/node_modules/@stdlib/stats/base/cumin/test/test.ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/cumin/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 isNegativeZero = require( '@stdlib/math/base/assert/is-negative-zero' ); var cumin = require( './../lib/ndarray.js' ); @@ -112,6 +112,78 @@ tape( 'the function calculates the cumulative minimum', function test( t ) { t.end(); }); +tape( 'the function calculates the cumulative minimum (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 ]; + cumin( x.length, toAccessorArray( x ), 1, 0, toAccessorArray( y ), 1, 0 ); + + expected = [ + 1.0, + -2.0, + -2.0, + -4.0, + -4.0 + ]; + t.deepEqual( y, expected, 'returns expected value' ); + + x = [ 0.0, -0.0, 0.0 ]; + y = [ 0.0, 0.0, 0.0 ]; + cumin( x.length, toAccessorArray( x ), 1, 0, toAccessorArray( y ), 1, 0 ); + + expected = [ + 0.0, + -0.0, + -0.0 + ]; + for ( i = 0; i < y.length; i++ ) { + if ( isNegativeZero( expected[ i ] ) ) { + t.strictEqual( isNegativeZero( y[ i ] ), true, 'returns expected value. i: ' + i ); + } else { + t.strictEqual( y[ i ], expected[ i ], true, 'returns expected value. i: ' + i ); + } + } + + x = [ NaN ]; + y = [ 0.0 ]; + cumin( 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 ]; + cumin( 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 ]; + cumin( 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; @@ -126,6 +198,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 = cumin( 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; @@ -175,6 +261,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; + + cumin( N, toAccessorArray( x ), 2, 0, toAccessorArray( y ), 1, 0 ); + + expected = [ 1.0, 1.0, -5.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; @@ -205,6 +321,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; + + cumin( N, toAccessorArray( x ), 1, 0, toAccessorArray( y ), 2, 0 ); + + expected = [ 1.0, 0.0, -2.0, 0.0, -2.0 ]; + t.deepEqual( y, expected, 'returns expected value' ); + + t.end(); +}); + tape( 'the function supports negative strides', function test( t ) { var expected; var x; @@ -235,6 +381,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; + + cumin( N, toAccessorArray( x ), -2, x.length-1, toAccessorArray( y ), -1, 2 ); + + expected = [ -5.0, -5.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 N; @@ -261,7 +437,7 @@ tape( 'the function supports an `x` offset', function test( t ) { 0.0, 0.0 ]; - N = floor( x.length / 2 ); + N = 4; cumin( N, x, 2, 1, y, 1, 0 ); @@ -271,6 +447,42 @@ tape( 'the function supports an `x` offset', function test( t ) { t.end(); }); +tape( 'the function supports an `x` offset (accessor)', function test( t ) { + var expected; + var N; + 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 + ]; + N = 4; + + cumin( N, toAccessorArray( x ), 2, 1, toAccessorArray( y ), 1, 0 ); + + expected = [ 1.0, -2.0, -2.0, -2.0, 0.0, 0.0, 0.0, 0.0 ]; + t.deepEqual( y, expected, 'returns expected value' ); + + t.end(); +}); + tape( 'the function supports a `y` offset', function test( t ) { var expected; var N; @@ -297,7 +509,7 @@ tape( 'the function supports a `y` offset', function test( t ) { 0.0, 0.0 // 3 ]; - N = floor( x.length / 2 ); + N = 4; cumin( N, x, 1, 0, y, 2, 1 ); @@ -307,6 +519,42 @@ tape( 'the function supports a `y` offset', function test( t ) { t.end(); }); +tape( 'the function supports a `y` offset (accessor)', function test( t ) { + var expected; + var N; + 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 + ]; + N = 4; + + cumin( N, toAccessorArray( x ), 1, 0, toAccessorArray( y ), 2, 1 ); + + expected = [ 0.0, 2.0, 0.0, 1.0, 0.0, 1.0, 0.0, -2.0 ]; + t.deepEqual( y, expected, 'returns expected value' ); + + t.end(); +}); + tape( 'the function supports complex access patterns', function test( t ) { var expected; var x; @@ -338,3 +586,35 @@ 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; + + cumin( N, toAccessorArray( x ), 2, 0, toAccessorArray( y ), -1, 2 ); + + expected = [ -3.0, -3.0, 1.0, 0.0, 0.0, 0.0 ]; + t.deepEqual( y, expected, 'returns expected value' ); + + t.end(); +});