|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2020 The Stdlib Authors. |
| 6 | +
|
| 7 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +you may not use this file except in compliance with the License. |
| 9 | +You may obtain a copy of the License at |
| 10 | +
|
| 11 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +
|
| 13 | +Unless required by applicable law or agreed to in writing, software |
| 14 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +See the License for the specific language governing permissions and |
| 17 | +limitations under the License. |
| 18 | +
|
| 19 | +--> |
| 20 | + |
| 21 | +# nanmskmax |
| 22 | + |
| 23 | +> Calculate the maximum value of a strided array according to a mask, ignoring `NaN` values. |
| 24 | +
|
| 25 | +<section class="intro"> |
| 26 | + |
| 27 | +</section> |
| 28 | + |
| 29 | +<!-- /.intro --> |
| 30 | + |
| 31 | +<section class="usage"> |
| 32 | + |
| 33 | +## Usage |
| 34 | + |
| 35 | +```javascript |
| 36 | +var nanmskmax = require( '@stdlib/stats/strided/nanmskmax' ); |
| 37 | +``` |
| 38 | + |
| 39 | +#### nanmskmax( N, x, strideX, mask, strideMask ) |
| 40 | + |
| 41 | +Computes the maximum value of a strided array according to a `mask`, ignoring `NaN` values. |
| 42 | + |
| 43 | +```javascript |
| 44 | +var x = [ 1.0, -2.0, 4.0, 2.0, NaN ]; |
| 45 | +var mask = [ 0, 0, 1, 0, 0 ]; |
| 46 | + |
| 47 | +var v = nanmskmax( x.length, x, 1, mask, 1 ); |
| 48 | +// returns 2.0 |
| 49 | +``` |
| 50 | + |
| 51 | +The function has the following parameters: |
| 52 | + |
| 53 | +- **N**: number of indexed elements. |
| 54 | +- **x**: input [`Array`][mdn-array] or [`typed array`][mdn-typed-array]. |
| 55 | +- **strideX**: stride length for `x`. |
| 56 | +- **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. |
| 57 | +- **strideMask**: stride length for `mask`. |
| 58 | + |
| 59 | +The `N` and stride parameters determine which elements in the strided arrays are accessed at runtime. For example, to compute the maximum value of every other element in `x`, |
| 60 | + |
| 61 | +```javascript |
| 62 | +var x = [ 1.0, 2.0, -7.0, -2.0, 4.0, 3.0, 5.0, 6.0, NaN, NaN ]; |
| 63 | +var mask = [ 0, 0, 0, 0, 0, 0, 1, 1, 0, 0 ]; |
| 64 | + |
| 65 | +var v = nanmskmax( 5, x, 2, mask, 2 ); |
| 66 | +// returns 4.0 |
| 67 | +``` |
| 68 | + |
| 69 | +Note that indexing is relative to the first index. To introduce offsets, use [`typed array`][mdn-typed-array] views. |
| 70 | + |
| 71 | +<!-- eslint-disable stdlib/capitalized-comments, max-len --> |
| 72 | + |
| 73 | +```javascript |
| 74 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 75 | +var Uint8Array = require( '@stdlib/array/uint8' ); |
| 76 | + |
| 77 | +var x0 = new Float64Array( [ 2.0, 1.0, -2.0, -2.0, 3.0, 4.0, 5.0, 6.0, NaN, NaN ] ); |
| 78 | +var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element |
| 79 | + |
| 80 | +var mask0 = new Uint8Array( [ 0, 0, 0, 0, 0, 0, 1, 1, 0, 0 ] ); |
| 81 | +var mask1 = new Uint8Array( mask0.buffer, mask0.BYTES_PER_ELEMENT*1 ); // start at 2nd element |
| 82 | + |
| 83 | +var v = nanmskmax( 5, x1, 2, mask1, 2 ); |
| 84 | +// returns 4.0 |
| 85 | +``` |
| 86 | + |
| 87 | +#### nanmskmax.ndarray( N, x, strideX, offsetX, mask, strideMask, offsetMask ) |
| 88 | + |
| 89 | +Computes the maximum value of a strided array according to a `mask`, ignoring `NaN` values and using alternative indexing semantics. |
| 90 | + |
| 91 | +```javascript |
| 92 | +var x = [ 1.0, -2.0, 4.0, 2.0, NaN ]; |
| 93 | +var mask = [ 0, 0, 1, 0, 0 ]; |
| 94 | + |
| 95 | +var v = nanmskmax.ndarray( x.length, x, 1, 0, mask, 1, 0 ); |
| 96 | +// returns 2.0 |
| 97 | +``` |
| 98 | + |
| 99 | +The function has the following additional parameters: |
| 100 | + |
| 101 | +- **offsetX**: starting index for `x`. |
| 102 | +- **offsetMask**: starting index for `mask`. |
| 103 | + |
| 104 | +While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameters support indexing semantics based on starting indices. For example, to calculate the maximum value for every other element in `x` starting from the second element |
| 105 | + |
| 106 | +```javascript |
| 107 | +var x = [ 2.0, 1.0, -2.0, -2.0, 3.0, 4.0, 5.0, 6.0, NaN, NaN ]; |
| 108 | +var mask = [ 0, 0, 0, 0, 0, 0, 1, 1, 0, 0 ]; |
| 109 | + |
| 110 | +var v = nanmskmax.ndarray( 5, x, 2, 1, mask, 2, 1 ); |
| 111 | +// returns 4.0 |
| 112 | +``` |
| 113 | + |
| 114 | +</section> |
| 115 | + |
| 116 | +<!-- /.usage --> |
| 117 | + |
| 118 | +<section class="notes"> |
| 119 | + |
| 120 | +## Notes |
| 121 | + |
| 122 | +- If `N <= 0`, both functions return `NaN`. |
| 123 | +- Depending on the environment, the typed versions ([`dnanmskmax`][@stdlib/stats/strided/dnanmskmax], [`snanmskmax`][@stdlib/stats/strided/snanmskmax], etc.) are likely to be significantly more performant. |
| 124 | +- 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]). |
| 125 | + |
| 126 | +</section> |
| 127 | + |
| 128 | +<!-- /.notes --> |
| 129 | + |
| 130 | +<section class="examples"> |
| 131 | + |
| 132 | +## Examples |
| 133 | + |
| 134 | +<!-- eslint no-undef: "error" --> |
| 135 | + |
| 136 | +```javascript |
| 137 | +var uniform = require( '@stdlib/random/base/uniform' ); |
| 138 | +var bernoulli = require( '@stdlib/random/base/bernoulli' ); |
| 139 | +var filledarrayBy = require( '@stdlib/array/filled-by' ); |
| 140 | +var nanmskmax = require( '@stdlib/stats/strided/nanmskmax' ); |
| 141 | + |
| 142 | +function rand() { |
| 143 | + if ( bernoulli( 0.8 ) < 1 ) { |
| 144 | + return NaN; |
| 145 | + } |
| 146 | + return uniform( -50.0, 50.0 ); |
| 147 | +} |
| 148 | + |
| 149 | +var x = filledarrayBy( 10, 'float64', rand ); |
| 150 | +console.log( x ); |
| 151 | + |
| 152 | +var mask = filledarrayBy( x.length, 'uint8', bernoulli.factory( 0.2 ) ); |
| 153 | +console.log( mask ); |
| 154 | + |
| 155 | +var v = nanmskmax( x.length, x, 1, mask, 1 ); |
| 156 | +console.log( v ); |
| 157 | +``` |
| 158 | + |
| 159 | +</section> |
| 160 | + |
| 161 | +<!-- /.examples --> |
| 162 | + |
| 163 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 164 | + |
| 165 | +<section class="related"> |
| 166 | + |
| 167 | +* * * |
| 168 | + |
| 169 | +## See Also |
| 170 | + |
| 171 | +- <span class="package-name">[`@stdlib/stats/strided/dnanmskmax`][@stdlib/stats/strided/dnanmskmax]</span><span class="delimiter">: </span><span class="description">calculate the maximum value of a double-precision floating-point strided array according to a mask, ignoring NaN values.</span> |
| 172 | +- <span class="package-name">[`@stdlib/stats/strided/mskmax`][@stdlib/stats/strided/mskmax]</span><span class="delimiter">: </span><span class="description">calculate the maximum value of a strided array according to a mask.</span> |
| 173 | +- <span class="package-name">[`@stdlib/stats/strided/nanmax`][@stdlib/stats/strided/nanmax]</span><span class="delimiter">: </span><span class="description">calculate the maximum value of a strided array, ignoring NaN values.</span> |
| 174 | +- <span class="package-name">[`@stdlib/stats/strided/nanmskmin`][@stdlib/stats/strided/nanmskmin]</span><span class="delimiter">: </span><span class="description">calculate the minimum value of a strided array according to a mask, ignoring NaN values.</span> |
| 175 | +- <span class="package-name">[`@stdlib/stats/strided/snanmskmax`][@stdlib/stats/strided/snanmskmax]</span><span class="delimiter">: </span><span class="description">calculate the maximum value of a single-precision floating-point strided array according to a mask, ignoring NaN values.</span> |
| 176 | + |
| 177 | +</section> |
| 178 | + |
| 179 | +<!-- /.related --> |
| 180 | + |
| 181 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 182 | + |
| 183 | +<section class="links"> |
| 184 | + |
| 185 | +[mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array |
| 186 | + |
| 187 | +[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray |
| 188 | + |
| 189 | +[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor |
| 190 | + |
| 191 | +<!-- <related-links> --> |
| 192 | + |
| 193 | +[@stdlib/stats/strided/dnanmskmax]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/dnanmskmax |
| 194 | + |
| 195 | +[@stdlib/stats/strided/mskmax]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/mskmax |
| 196 | + |
| 197 | +[@stdlib/stats/strided/nanmax]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/nanmax |
| 198 | + |
| 199 | +[@stdlib/stats/strided/nanmskmin]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/nanmskmin |
| 200 | + |
| 201 | +[@stdlib/stats/strided/snanmskmax]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/snanmskmax |
| 202 | + |
| 203 | +<!-- </related-links> --> |
| 204 | + |
| 205 | +</section> |
| 206 | + |
| 207 | +<!-- /.links --> |
0 commit comments