-
-
Notifications
You must be signed in to change notification settings - Fork 804
feat: add accessor protocol and refactor stats/base/nanmskmax
#6161
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -44,28 +44,25 @@ Computes the maximum value of a strided array `x` according to a `mask`, ignorin | |||||
var x = [ 1.0, -2.0, 4.0, 2.0, NaN ]; | ||||||
var mask = [ 0, 0, 1, 0, 0 ]; | ||||||
|
||||||
var v = nanmskmax( x.length, x, 1, mask, 1 ); | ||||||
var v = nanmskmax( 5, x, 1, mask, 1 ); | ||||||
// returns 2.0 | ||||||
``` | ||||||
|
||||||
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`. | ||||||
- **mask**: mask [`Array`][mdn-array] or [`typed array`][mdn-typed-array]. If a `mask` array element is `0`, the corresponding element in `x` is considered valid and **included** in computation. If a `mask` array element is `1`, the corresponding element in `x` is considered invalid/missing and **excluded** from computation. | ||||||
- **strideMask**: index increment for `mask`. | ||||||
- **strideMask**: stride length for `mask`. | ||||||
|
||||||
The `N` and `stride` parameters determine which elements are accessed at runtime. For example, to compute the maximum value of every other element in `x`, | ||||||
The `N` and stride parameters determine which elements int the strided arrays are accessed at runtime. For example, to compute the maximum value of every other element in `x`, | ||||||
|
||||||
```javascript | ||||||
var floor = require( '@stdlib/math/base/special/floor' ); | ||||||
|
||||||
var x = [ 1.0, 2.0, -7.0, -2.0, 4.0, 3.0, 5.0, 6.0 ]; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
var mask = [ 0, 0, 0, 0, 0, 0, 1, 1 ]; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
var N = floor( x.length / 2 ); | ||||||
|
||||||
var v = nanmskmax( N, x, 2, mask, 2 ); | ||||||
var v = nanmskmax( 4, x, 2, mask, 2 ); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
We should explicitly traverse over the |
||||||
// returns 4.0 | ||||||
``` | ||||||
|
||||||
|
@@ -76,17 +73,14 @@ Note that indexing is relative to the first index. To introduce offsets, use [`t | |||||
```javascript | ||||||
var Float64Array = require( '@stdlib/array/float64' ); | ||||||
var Uint8Array = require( '@stdlib/array/uint8' ); | ||||||
var floor = require( '@stdlib/math/base/special/floor' ); | ||||||
|
||||||
var x0 = new Float64Array( [ 2.0, 1.0, -2.0, -2.0, 3.0, 4.0, 5.0, 6.0 ] ); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same comment regarding |
||||||
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element | ||||||
|
||||||
var mask0 = new Uint8Array( [ 0, 0, 0, 0, 0, 0, 1, 1 ] ); | ||||||
var mask1 = new Uint8Array( mask0.buffer, mask0.BYTES_PER_ELEMENT*1 ); // start at 2nd element | ||||||
|
||||||
var N = floor( x0.length / 2 ); | ||||||
|
||||||
var v = nanmskmax( N, x1, 2, mask1, 2 ); | ||||||
var v = nanmskmax( 4, x1, 2, mask1, 2 ); | ||||||
// returns 4.0 | ||||||
``` | ||||||
|
||||||
|
@@ -98,7 +92,7 @@ Computes the maximum value of a strided array according to a `mask`, ignoring `N | |||||
var x = [ 1.0, -2.0, 4.0, 2.0, NaN ]; | ||||||
var mask = [ 0, 0, 1, 0, 0 ]; | ||||||
|
||||||
var v = nanmskmax.ndarray( x.length, x, 1, 0, mask, 1, 0 ); | ||||||
var v = nanmskmax.ndarray( 5, x, 1, 0, mask, 1, 0 ); | ||||||
// returns 2.0 | ||||||
``` | ||||||
|
||||||
|
@@ -107,16 +101,13 @@ The function has the following additional parameters: | |||||
- **offsetX**: starting index for `x`. | ||||||
- **offsetMask**: starting index for `mask`. | ||||||
|
||||||
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 value for every other value in `x` starting from the second value | ||||||
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameters support indexing semantics based on a starting indices. For example, to calculate the maximum value for every other value in `x` starting from the second value | ||||||
|
||||||
```javascript | ||||||
var floor = require( '@stdlib/math/base/special/floor' ); | ||||||
|
||||||
var x = [ 2.0, 1.0, -2.0, -2.0, 3.0, 4.0, 5.0, 6.0 ]; | ||||||
var mask = [ 0, 0, 0, 0, 0, 0, 1, 1 ]; | ||||||
var N = floor( x.length / 2 ); | ||||||
var x = [ 2.0, 1.0, -2.0, -2.0, 3.0, 4.0, 5.0, 6.0, NaN, NaN ]; | ||||||
var mask = [ 0, 0, 0, 0, 0, 0, 1, 1, 0, 0 ]; | ||||||
|
||||||
var v = nanmskmax.ndarray( N, x, 2, 1, mask, 2, 1 ); | ||||||
var v = nanmskmax.ndarray( 5, x, 2, 1, mask, 2, 1 ); | ||||||
// returns 4.0 | ||||||
``` | ||||||
|
||||||
|
@@ -130,6 +121,7 @@ var v = nanmskmax.ndarray( N, x, 2, 1, mask, 2, 1 ); | |||||
|
||||||
- If `N <= 0`, both functions return `NaN`. | ||||||
- Depending on the environment, the typed versions ([`dnanmskmax`][@stdlib/stats/base/dnanmskmax], [`snanmskmax`][@stdlib/stats/base/snanmskmax], 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]). | ||||||
|
||||||
</section> | ||||||
|
||||||
|
@@ -142,31 +134,18 @@ var v = nanmskmax.ndarray( N, x, 2, 1, mask, 2, 1 ); | |||||
<!-- eslint no-undef: "error" --> | ||||||
|
||||||
```javascript | ||||||
var randu = require( '@stdlib/random/base/randu' ); | ||||||
var round = require( '@stdlib/math/base/special/round' ); | ||||||
var Float64Array = require( '@stdlib/array/float64' ); | ||||||
var Uint8Array = require( '@stdlib/array/uint8' ); | ||||||
var uniform = require( '@stdlib/random/array/uniform' ); | ||||||
var bernoulli = require( '@stdlib/random/array/bernoulli' ); | ||||||
var nanmskmax = require( '@stdlib/stats/base/nanmskmax' ); | ||||||
|
||||||
var mask; | ||||||
var x; | ||||||
var i; | ||||||
|
||||||
x = new Float64Array( 10 ); | ||||||
mask = new Uint8Array( x.length ); | ||||||
for ( i = 0; i < x.length; i++ ) { | ||||||
if ( randu() < 0.2 ) { | ||||||
mask[ i ] = 1; | ||||||
} else { | ||||||
mask[ i ] = 0; | ||||||
} | ||||||
if ( randu() < 0.1 ) { | ||||||
x[ i ] = NaN; | ||||||
} else { | ||||||
x[ i ] = round( (randu()*100.0) - 50.0 ); | ||||||
} | ||||||
} | ||||||
var x = uniform( 10, -50.0, 50.0, { | ||||||
'dtype': 'float64' | ||||||
}); | ||||||
console.log( x ); | ||||||
|
||||||
var mask = bernoulli( x.length, 0.2, { | ||||||
'dtype': 'uint8' | ||||||
}); | ||||||
console.log( mask ); | ||||||
|
||||||
var v = nanmskmax( x.length, x, 1, mask, 1 ); | ||||||
|
@@ -215,6 +194,8 @@ console.log( v ); | |||||
|
||||||
[@stdlib/stats/base/snanmskmax]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/snanmskmax | ||||||
|
||||||
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor | ||||||
|
||||||
<!-- </related-links> --> | ||||||
|
||||||
</section> | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,8 +3,8 @@ | |
Computes the maximum value of a strided array according to a mask and | ||
ignoring `NaN` values. | ||
|
||
The `N` and `stride` parameters determine which elements 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 offsets, use a typed | ||
array views. | ||
|
@@ -26,13 +26,13 @@ | |
Input array. | ||
|
||
strideX: integer | ||
Index increment for `x`. | ||
Stride length for `x`. | ||
|
||
mask: Array<number>|TypedArray | ||
Mask array. | ||
|
||
strideMask: integer | ||
Index increment for `mask`. | ||
Stride length for `mask`. | ||
|
||
Returns | ||
------- | ||
|
@@ -47,22 +47,21 @@ | |
> {{alias}}( x.length, x, 1, mask, 1 ) | ||
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, 4.0 ]; | ||
> mask = [ 0, 0, 0, 0, 0, 0, 1 ]; | ||
> var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 ); | ||
> {{alias}}( N, x, 2, mask, 2 ) | ||
> {{alias}}( 3, x, 2, mask, 2 ) | ||
2.0 | ||
|
||
// Using view offsets: | ||
> var x0 = new {{alias:@stdlib/array/float64}}( [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0, 4.0 ] ); | ||
> var x1 = new {{alias:@stdlib/array/float64}}( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); | ||
> var mask0 = new {{alias:@stdlib/array/uint8}}( [ 0, 0, 0, 0, 0, 0, 1 ] ); | ||
> var mask1 = new {{alias:@stdlib/array/uint8}}( mask0.buffer, mask0.BYTES_PER_ELEMENT*1 ); | ||
> N = {{alias:@stdlib/math/base/special/floor}}( x0.length / 2 ); | ||
> {{alias}}( N, x1, 2, mask1, 2 ) | ||
> {{alias}}( 3, x1, 2, mask1, 2 ) | ||
2.0 | ||
|
||
|
||
{{alias}}.ndarray( N, x, strideX, offsetX, mask, strideMask, offsetMask ) | ||
Computes the maximum value of a strided array according to a mask, | ||
ignoring `NaN` values and using alternative indexing semantics. | ||
|
@@ -80,7 +79,7 @@ | |
Input array. | ||
|
||
strideX: integer | ||
Index increment for `x`. | ||
Stride length for `x`. | ||
|
||
offsetX: integer | ||
Starting index for `x`. | ||
|
@@ -89,7 +88,7 @@ | |
Mask array. | ||
|
||
strideMask: integer | ||
Index increment for `mask`. | ||
Stride length for `mask`. | ||
|
||
offsetMask: integer | ||
Starting index for `mask`. | ||
|
@@ -104,14 +103,13 @@ | |
// Standard Usage: | ||
> var x = [ 1.0, -2.0, 2.0, 4.0, NaN ]; | ||
> var mask = [ 0, 0, 0, 1, 0 ]; | ||
> {{alias}}.ndarray( x.length, x, 1, 0, mask, 1, 0 ) | ||
> {{alias}}.ndarray( 5, x, 1, 0, mask, 1, 0 ) | ||
2.0 | ||
|
||
// Using offset parameter: | ||
> x = [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0, 4.0 ]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same comment regarding |
||
> mask = [ 0, 0, 0, 0, 0, 0, 1 ]; | ||
> var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 ); | ||
> {{alias}}.ndarray( N, x, 2, 1, mask, 2, 1 ) | ||
> {{alias}}.ndarray( 3, x, 2, 1, mask, 2, 1 ) | ||
2.0 | ||
|
||
See Also | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.