Skip to content

Commit 4d75c13

Browse files
committed
feat: add stats/strided/maxsorted
Ref: #4797 --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent 3e14f52 commit 4d75c13

File tree

16 files changed

+1794
-0
lines changed

16 files changed

+1794
-0
lines changed
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
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+
# maxsorted
22+
23+
> Calculate the maximum value of a sorted strided array.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var maxsorted = require( '@stdlib/stats/strided/maxsorted' );
37+
```
38+
39+
#### maxsorted( N, x, strideX )
40+
41+
Computes the maximum value of a sorted strided array `x`.
42+
43+
```javascript
44+
var x = [ 1.0, 2.0, 3.0 ];
45+
var N = x.length;
46+
47+
var v = maxsorted( N, x, 1 );
48+
// returns 3.0
49+
50+
x = [ 3.0, 2.0, 1.0 ];
51+
N = x.length;
52+
53+
v = maxsorted( N, x, 1 );
54+
// returns 3.0
55+
```
56+
57+
The function has the following parameters:
58+
59+
- **N**: number of indexed elements.
60+
- **x**: sorted input [`Array`][mdn-array] or [`typed array`][mdn-typed-array].
61+
- **strideX**: stride length for `x`.
62+
63+
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to compute the maximum value of every other element in `x`,
64+
65+
```javascript
66+
var x = [ 1.0, 2.0, 2.0, -7.0, 3.0, 3.0, 4.0, 2.0 ];
67+
68+
var v = maxsorted( 4, x, 2 );
69+
// returns 4.0
70+
```
71+
72+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
73+
74+
<!-- eslint-disable stdlib/capitalized-comments -->
75+
76+
```javascript
77+
var Float64Array = require( '@stdlib/array/float64' );
78+
79+
var x0 = new Float64Array( [ 2.0, 1.0, 2.0, 2.0, -2.0, 2.0, 3.0, 4.0 ] );
80+
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
81+
82+
var v = maxsorted( 4, x1, 2 );
83+
// returns 4.0
84+
```
85+
86+
#### maxsorted.ndarray( N, x, strideX, offsetX )
87+
88+
Computes the maximum value of a sorted strided array using alternative indexing semantics.
89+
90+
```javascript
91+
var x = [ 1.0, 2.0, 3.0 ];
92+
93+
var v = maxsorted.ndarray( x.length, x, 1, 0 );
94+
// returns 3.0
95+
```
96+
97+
The function has the following additional parameters:
98+
99+
- **offsetX**: starting index for `x`.
100+
101+
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
102+
103+
```javascript
104+
var x = [ 2.0, 1.0, 2.0, 2.0, -2.0, 2.0, 3.0, 4.0 ];
105+
106+
var v = maxsorted.ndarray( 4, x, 2, 1 );
107+
// returns 4.0
108+
```
109+
110+
</section>
111+
112+
<!-- /.usage -->
113+
114+
<section class="notes">
115+
116+
## Notes
117+
118+
- If `N <= 0`, both functions return `NaN`.
119+
- The input strided array must be sorted in either **strictly** ascending or descending order.
120+
- 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]).
121+
122+
</section>
123+
124+
<!-- /.notes -->
125+
126+
<section class="examples">
127+
128+
## Examples
129+
130+
<!-- eslint no-undef: "error" -->
131+
132+
```javascript
133+
var linspace = require( '@stdlib/array/base/linspace' );
134+
var maxsorted = require( '@stdlib/stats/strided/maxsorted' );
135+
136+
var x = linspace( -50.0, 50.0, 10 );
137+
console.log( x );
138+
139+
var v = maxsorted( x.length, x, 1 );
140+
console.log( v );
141+
```
142+
143+
</section>
144+
145+
<!-- /.examples -->
146+
147+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
148+
149+
<section class="related">
150+
151+
* * *
152+
153+
## See Also
154+
155+
- <span class="package-name">[`@stdlib/stats/strided/dmaxsorted`][@stdlib/stats/strided/dmaxsorted]</span><span class="delimiter">: </span><span class="description">calculate the maximum value of a sorted double-precision floating-point strided array.</span>
156+
- <span class="package-name">[`@stdlib/stats/base/max`][@stdlib/stats/base/max]</span><span class="delimiter">: </span><span class="description">calculate the maximum value of a strided array.</span>
157+
- <span class="package-name">[`@stdlib/stats/base/minsorted`][@stdlib/stats/base/minsorted]</span><span class="delimiter">: </span><span class="description">calculate the minimum value of a sorted strided array.</span>
158+
- <span class="package-name">[`@stdlib/stats/strided/smaxsorted`][@stdlib/stats/strided/smaxsorted]</span><span class="delimiter">: </span><span class="description">calculate the maximum value of a sorted single-precision floating-point strided array.</span>
159+
160+
</section>
161+
162+
<!-- /.related -->
163+
164+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
165+
166+
<section class="links">
167+
168+
[mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
169+
170+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
171+
172+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
173+
174+
<!-- <related-links> -->
175+
176+
[@stdlib/stats/strided/dmaxsorted]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/dmaxsorted
177+
178+
[@stdlib/stats/base/max]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/max
179+
180+
[@stdlib/stats/base/minsorted]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/minsorted
181+
182+
[@stdlib/stats/strided/smaxsorted]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/smaxsorted
183+
184+
<!-- </related-links> -->
185+
186+
</section>
187+
188+
<!-- /.links -->
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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 linspace = require( '@stdlib/array/base/linspace' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var pkg = require( './../package.json' ).name;
28+
var maxsorted = require( './../lib/maxsorted.js' );
29+
30+
31+
// FUNCTIONS //
32+
33+
/**
34+
* Creates a benchmark function.
35+
*
36+
* @private
37+
* @param {PositiveInteger} len - array length
38+
* @returns {Function} benchmark function
39+
*/
40+
function createBenchmark( len ) {
41+
var x = linspace( 0.0, len, len );
42+
return benchmark;
43+
44+
function benchmark( b ) {
45+
var v;
46+
var i;
47+
48+
b.tic();
49+
for ( i = 0; i < b.iterations; i++ ) {
50+
x[ 0 ] = i;
51+
v = maxsorted( x.length, x, 1 );
52+
if ( isnan( v ) ) {
53+
b.fail( 'should not return NaN' );
54+
}
55+
}
56+
b.toc();
57+
if ( isnan( v ) ) {
58+
b.fail( 'should not return NaN' );
59+
}
60+
b.pass( 'benchmark finished' );
61+
b.end();
62+
}
63+
}
64+
65+
66+
// MAIN //
67+
68+
/**
69+
* Main execution sequence.
70+
*
71+
* @private
72+
*/
73+
function main() {
74+
var len;
75+
var min;
76+
var max;
77+
var f;
78+
var i;
79+
80+
min = 1; // 10^min
81+
max = 6; // 10^max
82+
83+
for ( i = min; i <= max; i++ ) {
84+
len = pow( 10, i );
85+
f = createBenchmark( len );
86+
bench( pkg+':len='+len, f );
87+
}
88+
}
89+
90+
main();
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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 pow = require( '@stdlib/math/base/special/pow' );
25+
var linspace = require( '@stdlib/array/base/linspace' );
26+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
27+
var pkg = require( './../package.json' ).name;
28+
var maxsorted = require( './../lib/ndarray.js' );
29+
30+
31+
// FUNCTIONS //
32+
33+
/**
34+
* Creates a benchmark function.
35+
*
36+
* @private
37+
* @param {PositiveInteger} len - array length
38+
* @returns {Function} benchmark function
39+
*/
40+
function createBenchmark( len ) {
41+
var x = linspace( 0.0, len, len );
42+
return benchmark;
43+
44+
function benchmark( b ) {
45+
var v;
46+
var i;
47+
48+
b.tic();
49+
for ( i = 0; i < b.iterations; i++ ) {
50+
x[ 0 ] = i;
51+
v = maxsorted( x.length, x, 1, 0 );
52+
if ( isnan( v ) ) {
53+
b.fail( 'should not return NaN' );
54+
}
55+
}
56+
b.toc();
57+
if ( isnan( v ) ) {
58+
b.fail( 'should not return NaN' );
59+
}
60+
b.pass( 'benchmark finished' );
61+
b.end();
62+
}
63+
}
64+
65+
66+
// MAIN //
67+
68+
/**
69+
* Main execution sequence.
70+
*
71+
* @private
72+
*/
73+
function main() {
74+
var len;
75+
var min;
76+
var max;
77+
var f;
78+
var i;
79+
80+
min = 1; // 10^min
81+
max = 6; // 10^max
82+
83+
for ( i = min; i <= max; i++ ) {
84+
len = pow( 10, i );
85+
f = createBenchmark( len );
86+
bench( pkg+':ndarray:len='+len, f );
87+
}
88+
}
89+
90+
main();

0 commit comments

Comments
 (0)