Skip to content

Commit 19e6a35

Browse files
committed
feat: add stats/strided/selmax
1 parent 25c05e1 commit 19e6a35

File tree

15 files changed

+2185
-0
lines changed

15 files changed

+2185
-0
lines changed
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
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+
# selmax
22+
23+
> Calculate the maximum value of a strided array by selecting elements according to a mask.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var selmax = require( '@stdlib/stats/strided/selmax' );
37+
```
38+
39+
#### selmax( N, x, strideX, mask, strideMask )
40+
41+
Computes the maximum value of a strided array by selecting elements according to a mask.
42+
43+
```javascript
44+
var x = [ 1.0, -2.0, 4.0, 2.0 ];
45+
var mask = [ 1, 1, 0, 1 ];
46+
47+
var v = selmax( 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 `true`, the corresponding element in `x` is considered valid and **included** in computation. If a `mask` array element is `false`, 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 ];
63+
var mask = [ 1, 1, 1, 1, 1, 1, 0, 0 ];
64+
65+
var v = selmax( 4, 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 -->
72+
73+
```javascript
74+
var Float64Array = require( '@stdlib/array/float64' );
75+
var BooleanArray = require( '@stdlib/array/bool' );
76+
77+
var x0 = new Float64Array( [ 2.0, 1.0, -2.0, -2.0, 3.0, 4.0, 5.0, 6.0 ] );
78+
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 );
79+
80+
var mask0 = new BooleanArray([
81+
true, true, true, true, true, true, false, false
82+
]);
83+
var mask1 = new BooleanArray( mask0.buffer, mask0.BYTES_PER_ELEMENT*1 );
84+
85+
var v = selmax( 4, x1, 2, mask1, 2 );
86+
// returns 4.0
87+
```
88+
89+
#### selmax.ndarray( N, x, strideX, offsetX, mask, strideMask, offsetMask )
90+
91+
Computes the maximum value of a strided array by selecting elements according to a mask and using alternative indexing semantics.
92+
93+
```javascript
94+
var x = [ 1.0, -2.0, 4.0, 2.0 ];
95+
var mask = [ 1, 1, 0, 1 ];
96+
97+
var v = selmax.ndarray( x.length, x, 1, 0, mask, 1, 0 );
98+
// returns 2.0
99+
```
100+
101+
The function has the following additional parameters:
102+
103+
- **offsetX**: starting index for `x`.
104+
- **offsetMask**: starting index for `mask`.
105+
106+
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 value in `x` starting from the second value
107+
108+
```javascript
109+
var x = [ 2.0, 1.0, -2.0, -2.0, 3.0, 4.0, 5.0, 6.0 ];
110+
var mask = [ 1, 1, 1, 1, 1, 1, 0, 0 ];
111+
112+
var v = selmax.ndarray( 4, x, 2, 1, mask, 2, 1 );
113+
// returns 4.0
114+
```
115+
116+
</section>
117+
118+
<!-- /.usage -->
119+
120+
<section class="notes">
121+
122+
## Notes
123+
124+
- If `N <= 0`, both functions return `NaN`.
125+
- 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]).
126+
127+
</section>
128+
129+
<!-- /.notes -->
130+
131+
<section class="examples">
132+
133+
## Examples
134+
135+
<!-- eslint no-undef: "error" -->
136+
137+
```javascript
138+
var uniform = require( '@stdlib/random/array/uniform' );
139+
var bernoulli = require( '@stdlib/random/array/bernoulli' );
140+
var selmax = require( '@stdlib/stats/strided/selmax' );
141+
142+
var x = uniform( 10, -50.0, 50.0, {
143+
'dtype': 'float64'
144+
});
145+
console.log( x );
146+
147+
var mask = bernoulli( x.length, 0.8, {
148+
'dtype': 'generic'
149+
});
150+
console.log( mask );
151+
152+
var v = selmax( x.length, x, 1, mask, 1 );
153+
console.log( v );
154+
```
155+
156+
</section>
157+
158+
<!-- /.examples -->
159+
160+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
161+
162+
<section class="related">
163+
164+
* * *
165+
166+
## See Also
167+
168+
- <span class="package-name">[`@stdlib/stats/strided/max`][@stdlib/stats/strided/max]</span><span class="delimiter">: </span><span class="description">calculate the maximum value of a strided array.</span>
169+
- <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>
170+
- <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>
171+
172+
</section>
173+
174+
<!-- /.related -->
175+
176+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
177+
178+
<section class="links">
179+
180+
[mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
181+
182+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
183+
184+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
185+
186+
<!-- <related-links> -->
187+
188+
[@stdlib/stats/strided/max]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/max
189+
190+
[@stdlib/stats/strided/mskmax]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/mskmax
191+
192+
[@stdlib/stats/strided/nanmax]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/nanmax
193+
194+
<!-- </related-links> -->
195+
196+
</section>
197+
198+
<!-- /.links -->
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2020 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var uniform = require( '@stdlib/random/array/uniform' );
26+
var bernoulli = require( '@stdlib/random/array/bernoulli' );
27+
var pow = require( '@stdlib/math/base/special/pow' );
28+
var format = require( '@stdlib/string/format' );
29+
var pkg = require( './../package.json' ).name;
30+
var selmax = require( './../lib' );
31+
32+
33+
// VARIABLES //
34+
35+
var options = {
36+
'dtype': 'generic'
37+
};
38+
39+
40+
// FUNCTIONS //
41+
42+
/**
43+
* Creates a benchmark function.
44+
*
45+
* @private
46+
* @param {PositiveInteger} len - array length
47+
* @returns {Function} benchmark function
48+
*/
49+
function createBenchmark( len ) {
50+
var mask = bernoulli( len, 0.8, options );
51+
var x = uniform( len, -10.0, 10.0, options );
52+
return benchmark;
53+
54+
/**
55+
* Benchmark function.
56+
*
57+
* @private
58+
* @param {Benchmark} b - benchmark instance
59+
*/
60+
function benchmark( b ) {
61+
var v;
62+
var i;
63+
64+
b.tic();
65+
for ( i = 0; i < b.iterations; i++ ) {
66+
v = selmax( x.length, x, 1, mask, 1 );
67+
if ( isnan( v ) ) {
68+
b.fail( 'should not return NaN' );
69+
}
70+
}
71+
b.toc();
72+
if ( isnan( v ) ) {
73+
b.fail( 'should not return NaN' );
74+
}
75+
b.pass( 'benchmark finished' );
76+
b.end();
77+
}
78+
}
79+
80+
81+
// MAIN //
82+
83+
/**
84+
* Main execution sequence.
85+
*
86+
* @private
87+
*/
88+
function main() {
89+
var len;
90+
var min;
91+
var max;
92+
var f;
93+
var i;
94+
95+
min = 1; // 10^min
96+
max = 6; // 10^max
97+
98+
for ( i = min; i <= max; i++ ) {
99+
len = pow( 10, i );
100+
f = createBenchmark( len );
101+
bench( format( '%s:len=%d', pkg, len ), f );
102+
}
103+
}
104+
105+
main();

0 commit comments

Comments
 (0)