From 99989f2c5da8d14d37e240e40b1f82f855c17bca Mon Sep 17 00:00:00 2001 From: Kaushikgtm <162317291+Kaushikgtm@users.noreply.github.com> Date: Wed, 26 Mar 2025 03:12:18 +0530 Subject: [PATCH 01/22] Create accessors.js Signed-off-by: Kaushikgtm <162317291+Kaushikgtm@users.noreply.github.com> --- .../stats/base/nanmaxabs/lib/accessors.js | 96 +++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 lib/node_modules/@stdlib/stats/base/nanmaxabs/lib/accessors.js diff --git a/lib/node_modules/@stdlib/stats/base/nanmaxabs/lib/accessors.js b/lib/node_modules/@stdlib/stats/base/nanmaxabs/lib/accessors.js new file mode 100644 index 000000000000..dcfbfdaae5d4 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/nanmaxabs/lib/accessors.js @@ -0,0 +1,96 @@ +/** +* @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 maximum absolute value of a strided array, ignoring `NaN` values. +* +* @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 +* @param {NonNegativeInteger} offsetX - starting index +* @returns {number} minimum value +* +* @example +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +* var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +* +* var x = toAccessorArray( [ 1.0, -2.0, NaN, 2.0 ] ); +* +* var v = nanminabs( 4, arraylike2object( x ), 1, 0 ); +* // returns 2.0 +*/ +function nanmaxabs( N, x, strideX, offsetX ) { + var xbuf; + var get; + var max; + var ix; + var v; + var i; + + // Cache reference to array data: + xbuf = x.data; + + // Cache a reference to the element accessor: + get = x.accessors[0]; + + if ( N === 1 || strideX === 0 ) { + return get( xbuf, offsetX ); + } + ix = offsetX; + for ( i = 0; i < N; i++ ) { + v = get( xbuf, ix ); + if ( !isnan( v ) ) { + max = abs( v ); + break; + } + ix += strideX; + } + if ( i === N ) { + return NaN; + } + i += 1; + for ( i; i < N; i++ ) { + ix += strideX; + v = get( xbuf, ix ); + if ( isnan( v ) ) { + continue; + } + v = abs( v ); + if ( v > max) { + max = abs( v ); + } + } + return max; +} + + +// EXPORTS // + +module.exports = nanmaxabs; From e94e01b750552a7639a69791798f4941017eb82d Mon Sep 17 00:00:00 2001 From: Kaushikgtm <162317291+Kaushikgtm@users.noreply.github.com> Date: Wed, 26 Mar 2025 03:14:44 +0530 Subject: [PATCH 02/22] Update nanmaxabs.js Signed-off-by: Kaushikgtm <162317291+Kaushikgtm@users.noreply.github.com> --- .../stats/base/nanmaxabs/lib/nanmaxabs.js | 50 +++---------------- 1 file changed, 6 insertions(+), 44 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/base/nanmaxabs/lib/nanmaxabs.js b/lib/node_modules/@stdlib/stats/base/nanmaxabs/lib/nanmaxabs.js index b3fddc634530..bd43c168bcb8 100644 --- a/lib/node_modules/@stdlib/stats/base/nanmaxabs/lib/nanmaxabs.js +++ b/lib/node_modules/@stdlib/stats/base/nanmaxabs/lib/nanmaxabs.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2020 The Stdlib Authors. +* 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. @@ -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,7 +31,7 @@ var abs = require( '@stdlib/math/base/special/abs' ); * * @param {PositiveInteger} N - number of indexed elements * @param {NumericArray} x - input array -* @param {integer} stride - stride length +* @param {integer} strideX - stride length * @returns {number} maximum absolute value * * @example @@ -41,46 +41,8 @@ var abs = require( '@stdlib/math/base/special/abs' ); * var v = nanmaxabs( N, x, 1 ); * // returns 2.0 */ -function nanmaxabs( N, x, stride ) { - var max; - var ix; - var v; - var i; - - if ( N <= 0 ) { - return NaN; - } - if ( N === 1 || stride === 0 ) { - return x[ 0 ]; - } - if ( stride < 0 ) { - ix = (1-N) * stride; - } else { - ix = 0; - } - for ( i = 0; i < N; i++ ) { - v = x[ ix ]; - if ( v === v ) { - break; - } - ix += stride; - } - if ( i === N ) { - return NaN; - } - max = abs( v ); - i += 1; - for ( i; i < N; i++ ) { - ix += stride; - v = abs( x[ ix ] ); - if ( isnan( v ) ) { - continue; - } - if ( v > max ) { - max = v; - } - } - return max; +function nanmaxabs( N, x, strideX ) { + return ndarray( N, x, strideX, stride2offset( N, strideX ) ); } From fb2f6aa96889cce26a07843281764e4839e212b8 Mon Sep 17 00:00:00 2001 From: Kaushikgtm <162317291+Kaushikgtm@users.noreply.github.com> Date: Wed, 26 Mar 2025 03:15:38 +0530 Subject: [PATCH 03/22] Update ndarray.js Signed-off-by: Kaushikgtm <162317291+Kaushikgtm@users.noreply.github.com> --- .../stats/base/nanmaxabs/lib/ndarray.js | 25 ++++++++++++------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/base/nanmaxabs/lib/ndarray.js b/lib/node_modules/@stdlib/stats/base/nanmaxabs/lib/ndarray.js index cc144dd7f0f8..7066d37d5d47 100644 --- a/lib/node_modules/@stdlib/stats/base/nanmaxabs/lib/ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/nanmaxabs/lib/ndarray.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2020 The Stdlib Authors. +* 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. @@ -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,8 +33,8 @@ var abs = require( '@stdlib/math/base/special/abs' ); * * @param {PositiveInteger} N - number of indexed elements * @param {NumericArray} x - input array -* @param {integer} stride - stride length -* @param {NonNegativeInteger} offset - starting index +* @param {integer} strideX - stride length +* @param {NonNegativeInteger} offsetX - starting index * @returns {number} maximum absolute value * * @example @@ -44,25 +46,30 @@ var abs = require( '@stdlib/math/base/special/abs' ); * var v = nanmaxabs( N, x, 2, 1 ); * // returns 4.0 */ -function nanmaxabs( N, x, stride, offset ) { +function nanmaxabs( N, x, strideX, offsetX ) { var max; var ix; + var o; var v; var i; if ( N <= 0 ) { return NaN; } - if ( N === 1 || stride === 0 ) { - return x[ offset ]; + o = arraylike2object( x ); + if ( o.accessorProtocol ) { + return accessors( N, o, strideX, offsetX ); } - ix = offset; + if ( N === 1 || strideX === 0 ) { + return x[ offsetX ]; + } + ix = offsetX; for ( i = 0; i < N; i++ ) { v = x[ ix ]; if ( v === v ) { break; } - ix += stride; + ix += strideX; } if ( i === N ) { return NaN; @@ -70,7 +77,7 @@ function nanmaxabs( N, x, stride, offset ) { max = abs( v ); i += 1; for ( i; i < N; i++ ) { - ix += stride; + ix += strideX; v = abs( x[ ix ] ); if ( isnan( v ) ) { continue; From 7fc681087894999a53e3f1877b53e44589cb1fa8 Mon Sep 17 00:00:00 2001 From: Kaushikgtm <162317291+Kaushikgtm@users.noreply.github.com> Date: Wed, 26 Mar 2025 03:17:18 +0530 Subject: [PATCH 04/22] Update README.md Signed-off-by: Kaushikgtm <162317291+Kaushikgtm@users.noreply.github.com> --- .../@stdlib/stats/base/nanmaxabs/README.md | 48 ++++++++----------- 1 file changed, 21 insertions(+), 27 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/base/nanmaxabs/README.md b/lib/node_modules/@stdlib/stats/base/nanmaxabs/README.md index b3499b7733da..bc54d5229e85 100644 --- a/lib/node_modules/@stdlib/stats/base/nanmaxabs/README.md +++ b/lib/node_modules/@stdlib/stats/base/nanmaxabs/README.md @@ -2,7 +2,7 @@ @license Apache-2.0 -Copyright (c) 2020 The Stdlib Authors. +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. @@ -36,7 +36,7 @@ limitations under the License. var nanmaxabs = require( '@stdlib/stats/base/nanmaxabs' ); ``` -#### nanmaxabs( N, x, stride ) +#### nanmaxabs( N, x, strideX ) Computes the maximum absolute value of a strided array `x`, ignoring `NaN` values. @@ -52,7 +52,7 @@ The function has the following parameters: - **N**: number of indexed elements. - **x**: input [`Array`][mdn-array] or [`typed array`][mdn-typed-array]. -- **stride**: index increment for `x`. +- **strideX**: stride length for `x`. The `N` and `stride` parameters determine which elements in `x` are accessed at runtime. For example, to compute the maximum absolute value of every other element in `x`, @@ -83,7 +83,7 @@ var v = nanmaxabs( N, x1, 2 ); // returns 4.0 ``` -#### nanmaxabs.ndarray( N, x, stride, offset ) +#### nanmaxabs.ndarray( N, x, strideX, offsetX ) Computes the maximum absolute value of a strided array, ignoring `NaN` values and using alternative indexing semantics. @@ -97,7 +97,7 @@ var v = nanmaxabs.ndarray( N, x, 1, 0 ); The function has the following additional parameters: -- **offset**: starting index for `x`. +- **offsetX**: starting index for `x`. 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 calculate the maximum absolute value for every other value in `x` starting from the second value @@ -120,7 +120,8 @@ var v = nanmaxabs.ndarray( N, x, 2, 1 ); ## Notes - If `N <= 0`, both functions return `NaN`. -- Depending on the environment, the typed versions ([`dnanmaxabs`][@stdlib/stats/strided/dnanmaxabs], [`snanmaxabs`][@stdlib/stats/base/snanmaxabs], 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]). +- Depending on the environment, the typed versions ([`dnanmaxabs`][@stdlib/stats/base/dnanmaxabs], [`snanmaxabs`][@stdlib/stats/base/snanmaxabs], etc.) are likely to be significantly more performant. @@ -133,22 +134,19 @@ var v = nanmaxabs.ndarray( N, x, 2, 1 ); ```javascript -var randu = require( '@stdlib/random/base/randu' ); -var round = require( '@stdlib/math/base/special/round' ); -var Float64Array = require( '@stdlib/array/float64' ); +var uniform = require( '@stdlib/random/base/uniform' ); +var filledarrayBy = require( '@stdlib/array/filled-by' ); +var bernoulli = require( '@stdlib/random/base/bernoulli' ); var nanmaxabs = require( '@stdlib/stats/base/nanmaxabs' ); -var x; -var i; - -x = new Float64Array( 10 ); -for ( i = 0; i < x.length; i++ ) { - if ( randu() < 0.2 ) { - x[ i ] = NaN; - } else { - x[ i ] = round( (randu()*100.0) - 50.0 ); +function rand() { + if ( bernoulli( 0.8 ) < 1 ) { + return NaN; } + return uniform( -50.0, 50.0 ); } + +var x = filledarrayBy( 10, 'float64', rand ); console.log( x ); var v = nanmaxabs( x.length, x, 1 ); @@ -167,10 +165,10 @@ console.log( v ); ## See Also -- [`@stdlib/stats/strided/dnanmaxabs`][@stdlib/stats/strided/dnanmaxabs]: calculate the maximum absolute value of a double-precision floating-point strided array, ignoring NaN values. +- [`@stdlib/stats/base/dnanmaxabs`][@stdlib/stats/base/dnanmaxabs]: calculate the maximum absolute value of a double-precision floating-point strided array, ignoring NaN values. - [`@stdlib/stats/base/maxabs`][@stdlib/stats/base/maxabs]: calculate the maximum absolute value of a strided array. +- [`@stdlib/stats/base/nanmaxabs`][@stdlib/stats/base/nanmaxabs]: calculate the maximum absolute value of a strided array, ignoring NaN values. - [`@stdlib/stats/base/nanmax`][@stdlib/stats/base/nanmax]: calculate the maximum value of a strided array, ignoring NaN values. -- [`@stdlib/stats/base/nanminabs`][@stdlib/stats/base/nanminabs]: calculate the minimum absolute value of a strided array, ignoring NaN values. - [`@stdlib/stats/base/snanmaxabs`][@stdlib/stats/base/snanmaxabs]: calculate the maximum absolute value of a single-precision floating-point strided array, ignoring NaN values. @@ -182,19 +180,15 @@ console.log( v ); @@ -185,11 +186,15 @@ console.log( v ); -[@stdlib/stats/base/dnanmaxabs]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/dnanmaxabs +[@stdlib/stats/strided/dnanmaxabs]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/dnanmaxabs + [@stdlib/stats/base/maxabs]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/maxabs -[@stdlib/stats/base/nanmaxabs]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/nanmaxabs + [@stdlib/stats/base/nanmax]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/nanmax -[@stdlib/stats/base/snanmaxabs]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/snanmaxabs + +[@stdlib/stats/base/nanminabs]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/nanminabs + +[@stdlib/stats/strided/snanmaxabs]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/snanmaxabs From c55c4911da597129e2564f79352a3722ed7fa89a Mon Sep 17 00:00:00 2001 From: Gautam Kaushik <162317291+Kaushikgtm@users.noreply.github.com> Date: Tue, 15 Apr 2025 00:39:58 +0530 Subject: [PATCH 21/22] Update README.md Signed-off-by: Gautam Kaushik <162317291+Kaushikgtm@users.noreply.github.com> --- lib/node_modules/@stdlib/stats/base/nanmaxabs/README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/node_modules/@stdlib/stats/base/nanmaxabs/README.md b/lib/node_modules/@stdlib/stats/base/nanmaxabs/README.md index 7153f438dc33..53c687ec162e 100644 --- a/lib/node_modules/@stdlib/stats/base/nanmaxabs/README.md +++ b/lib/node_modules/@stdlib/stats/base/nanmaxabs/README.md @@ -122,7 +122,6 @@ var v = nanmaxabs.ndarray( N, x, 2, 1 ); - If `N <= 0`, both functions return `NaN`. - Depending on the environment, the typed versions ([`dnanmaxabs`][@stdlib/stats/strided/dnanmaxabs], [`snanmaxabs`][@stdlib/stats/strided/snanmaxabs], 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]). -- Depending on the environment, the typed versions ([`dnanmaxabs`][@stdlib/stats/base/dnanmaxabs], [`snanmaxabs`][@stdlib/stats/base/snanmaxabs], etc.) are likely to be significantly more performant. From eaddead074107410c6060c7a73de9019c4e23661 Mon Sep 17 00:00:00 2001 From: Gautam Kaushik <162317291+Kaushikgtm@users.noreply.github.com> Date: Tue, 15 Apr 2025 00:41:12 +0530 Subject: [PATCH 22/22] Update README.md Signed-off-by: Gautam Kaushik <162317291+Kaushikgtm@users.noreply.github.com> --- lib/node_modules/@stdlib/stats/base/nanmaxabs/README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/node_modules/@stdlib/stats/base/nanmaxabs/README.md b/lib/node_modules/@stdlib/stats/base/nanmaxabs/README.md index 53c687ec162e..3a61a42d1c17 100644 --- a/lib/node_modules/@stdlib/stats/base/nanmaxabs/README.md +++ b/lib/node_modules/@stdlib/stats/base/nanmaxabs/README.md @@ -121,7 +121,6 @@ var v = nanmaxabs.ndarray( N, x, 2, 1 ); - If `N <= 0`, both functions return `NaN`. - Depending on the environment, the typed versions ([`dnanmaxabs`][@stdlib/stats/strided/dnanmaxabs], [`snanmaxabs`][@stdlib/stats/strided/snanmaxabs], 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]).