Skip to content

Commit ddad44c

Browse files
committed
feat: add stats/strided/mediansorted
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 cb1f254 commit ddad44c

File tree

16 files changed

+1777
-0
lines changed

16 files changed

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

0 commit comments

Comments
 (0)