From 4a084fa40c7bef0ce8e793b87eb0d3ee8addafb6 Mon Sep 17 00:00:00 2001 From: vivek maurya Date: Sun, 2 Feb 2025 10:10:56 +0000 Subject: [PATCH 1/3] refactor: updating js implementation stats/base/cumax --- .../@stdlib/stats/base/cumax/README.md | 24 +++----- .../stats/base/cumax/benchmark/benchmark.js | 32 +++++------ .../base/cumax/benchmark/benchmark.ndarray.js | 32 +++++------ .../@stdlib/stats/base/cumax/docs/repl.txt | 24 ++++---- .../stats/base/cumax/docs/types/index.d.ts | 8 +-- .../stats/base/cumax/examples/index.js | 16 ++---- .../@stdlib/stats/base/cumax/lib/cumax.js | 57 +++---------------- 7 files changed, 68 insertions(+), 125 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/base/cumax/README.md b/lib/node_modules/@stdlib/stats/base/cumax/README.md index c567f69b86b0..98ee71bedac2 100644 --- a/lib/node_modules/@stdlib/stats/base/cumax/README.md +++ b/lib/node_modules/@stdlib/stats/base/cumax/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 maximum 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 of every other element: ```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 maximum 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 maximum 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 ]; @@ -134,20 +134,14 @@ cumax.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 cumax = require( '@stdlib/stats/base/cumax' ); -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/cumax/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/base/cumax/benchmark/benchmark.js index 701d78302c97..4fb7038c0381 100644 --- a/lib/node_modules/@stdlib/stats/base/cumax/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/stats/base/cumax/benchmark/benchmark.js @@ -21,13 +21,22 @@ // MODULES // var bench = require( '@stdlib/bench' ); -var randu = require( '@stdlib/random/base/randu' ); +var uniform = require( '@stdlib/random/array/uniform' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); +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 cumax = require( './../lib/cumax.js' ); +// VARIABLES // + +var options = { + 'dtype': 'generic' +}; + + // FUNCTIONS // /** @@ -38,35 +47,26 @@ var cumax = require( './../lib/cumax.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 = cumax( 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/cumax/benchmark/benchmark.ndarray.js b/lib/node_modules/@stdlib/stats/base/cumax/benchmark/benchmark.ndarray.js index 4e414743dae8..8d01892bfb2e 100644 --- a/lib/node_modules/@stdlib/stats/base/cumax/benchmark/benchmark.ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/cumax/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 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 cumax = require( './../lib/ndarray.js' ); +// VARIABLES // + +var options = { + 'dtype': 'generic' +}; + + // FUNCTIONS // /** @@ -38,35 +47,26 @@ var cumax = 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 = cumax( 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/cumax/docs/repl.txt b/lib/node_modules/@stdlib/stats/base/cumax/docs/repl.txt index e6c887ba015d..76978187c295 100644 --- a/lib/node_modules/@stdlib/stats/base/cumax/docs/repl.txt +++ b/lib/node_modules/@stdlib/stats/base/cumax/docs/repl.txt @@ -2,8 +2,8 @@ {{alias}}( N, x, strideX, y, strideY ) Computes the cumulative maximum 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, 1.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, 1.0, 0.0, 2.0, 0.0 ] // Using view offsets: @@ -52,8 +51,7 @@ > 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 ] @@ -63,8 +61,8 @@ 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. + buffer, the offset parameters support indexing semantics based on starting + indices. Parameters ---------- @@ -75,7 +73,7 @@ Input array. strideX: integer - Index increment for `x`. + Stride length for `x`. offsetX: integer Starting index for `x`. @@ -84,7 +82,7 @@ Output array. strideY: integer - Index increment for `y`. + Stride length for `y`. offsetY: integer Starting index for `y`. diff --git a/lib/node_modules/@stdlib/stats/base/cumax/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/base/cumax/docs/types/index.d.ts index a6b953283ca8..56a466de797c 100644 --- a/lib/node_modules/@stdlib/stats/base/cumax/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/stats/base/cumax/docs/types/index.d.ts @@ -31,9 +31,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 @@ -72,9 +72,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/cumax/examples/index.js b/lib/node_modules/@stdlib/stats/base/cumax/examples/index.js index 187d6db5f9a1..2b23a097b7b0 100644 --- a/lib/node_modules/@stdlib/stats/base/cumax/examples/index.js +++ b/lib/node_modules/@stdlib/stats/base/cumax/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 cumax = 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/cumax/lib/cumax.js b/lib/node_modules/@stdlib/stats/base/cumax/lib/cumax.js index 90900e81abe6..dd4df5ce8f42 100644 --- a/lib/node_modules/@stdlib/stats/base/cumax/lib/cumax.js +++ b/lib/node_modules/@stdlib/stats/base/cumax/lib/cumax.js @@ -20,8 +20,8 @@ // MODULES // -var isnan = require( '@stdlib/math/base/assert/is-nan' ); -var isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' ); +var stride2offset = require( '@stdlib/strided/base/stride2offset' ); +var ndarray = require( './ndarray.js' ); // MAIN // @@ -31,9 +31,9 @@ var isPositiveZero = require( '@stdlib/math/base/assert/is-positive-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 {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 isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' ); * // returns [ 1.0, 1.0, 2.0 ] */ function cumax( N, x, strideX, y, strideY ) { - var max; - 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; - } - max = x[ ix ]; - y[ iy ] = max; - - iy += strideY; - i = 1; - if ( isnan( max ) === false ) { - for ( i; i < N; i++ ) { - ix += strideX; - v = x[ ix ]; - if ( isnan( v ) ) { - max = v; - break; - } - if ( v > max || ( v === max && isPositiveZero( v ) ) ) { - max = v; - } - y[ iy ] = max; - iy += strideY; - } - } - if ( isnan( max ) ) { - for ( i; i < N; i++ ) { - y[ iy ] = max; - iy += strideY; - } - } - return y; + var ix = stride2offset( N, strideX ); + var iy = stride2offset( N, strideY ); + return ndarray( N, x, strideX, ix, y, strideY, iy ); } From 45d3162ffb06907639f847dcf4bedf1ba1aebae2 Mon Sep 17 00:00:00 2001 From: vivek maurya Date: Sun, 2 Feb 2025 10:14:57 +0000 Subject: [PATCH 2/3] refactor: updating js implementation stats/base/cumax --- type: pre_push_report description: Results of running various checks prior to pushing changes. report: - task: run_javascript_examples status: passed - task: run_c_examples status: na - task: run_cpp_examples status: na - task: run_javascript_readme_examples status: passed - task: run_c_benchmarks status: na - task: run_cpp_benchmarks status: na - task: run_fortran_benchmarks status: na - task: run_javascript_benchmarks status: passed - task: run_julia_benchmarks status: na - task: run_python_benchmarks status: na - task: run_r_benchmarks status: na - task: run_javascript_tests status: na --- --- lib/node_modules/@stdlib/stats/base/cumax/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/stats/base/cumax/README.md b/lib/node_modules/@stdlib/stats/base/cumax/README.md index 98ee71bedac2..df363496041b 100644 --- a/lib/node_modules/@stdlib/stats/base/cumax/README.md +++ b/lib/node_modules/@stdlib/stats/base/cumax/README.md @@ -139,7 +139,7 @@ var Float64Array = require( '@stdlib/array/float64' ); var cumax = require( '@stdlib/stats/base/cumax' ); var x = discreteUniform( 10, 0, 100, { - 'dtype': 'float64' + 'dtype': 'float64' }); var y = new Float64Array( x.length ); console.log( x ); From d785fabc220f06ab5ae6f3b2597d7b00a92df7af Mon Sep 17 00:00:00 2001 From: vivek maurya Date: Sun, 2 Feb 2025 10:26:27 +0000 Subject: [PATCH 3/3] refactor: updating js implementation stats/base/cumax --- lib/node_modules/@stdlib/stats/base/cumax/docs/repl.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/node_modules/@stdlib/stats/base/cumax/docs/repl.txt b/lib/node_modules/@stdlib/stats/base/cumax/docs/repl.txt index 76978187c295..c93cf4731997 100644 --- a/lib/node_modules/@stdlib/stats/base/cumax/docs/repl.txt +++ b/lib/node_modules/@stdlib/stats/base/cumax/docs/repl.txt @@ -56,6 +56,7 @@ > 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 maximum of a strided array using alternative indexing semantics.