Skip to content

Commit 9eae3de

Browse files
gururaj1512anandkaranubc
authored andcommitted
feat: add stats/strided/smeanors
Ref: stdlib-js#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: missing_dependencies - task: lint_c_examples status: missing_dependencies - task: lint_c_benchmarks status: missing_dependencies - 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 df812ab commit 9eae3de

34 files changed

+3481
-0
lines changed
Lines changed: 331 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,331 @@
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+
# smeanors
22+
23+
> Calculate the [arithmetic mean][arithmetic-mean] of a single-precision floating-point strided array using ordinary recursive summation.
24+
25+
<section class="intro">
26+
27+
The [arithmetic mean][arithmetic-mean] is defined as
28+
29+
<!-- <equation class="equation" label="eq:arithmetic_mean" align="center" raw="\mu = \frac{1}{n} \sum_{i=0}^{n-1} x_i" alt="Equation for the arithmetic mean."> -->
30+
31+
```math
32+
\mu = \frac{1}{n} \sum_{i=0}^{n-1} x_i
33+
```
34+
35+
<!-- <div class="equation" align="center" data-raw-text="\mu = \frac{1}{n} \sum_{i=0}^{n-1} x_i" data-equation="eq:arithmetic_mean">
36+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@6c9ade4d7509dfc873ce2dad241f92459a83313b/lib/node_modules/@stdlib/stats/strided/smeanors/docs/img/equation_arithmetic_mean.svg" alt="Equation for the arithmetic mean.">
37+
<br>
38+
</div> -->
39+
40+
<!-- </equation> -->
41+
42+
</section>
43+
44+
<!-- /.intro -->
45+
46+
<section class="usage">
47+
48+
## Usage
49+
50+
```javascript
51+
var smeanors = require( '@stdlib/stats/strided/smeanors' );
52+
```
53+
54+
#### smeanors( N, x, strideX )
55+
56+
Computes the [arithmetic mean][arithmetic-mean] of a single-precision floating-point strided array using ordinary recursive summation.
57+
58+
```javascript
59+
var Float32Array = require( '@stdlib/array/float32' );
60+
61+
var x = new Float32Array( [ 1.0, -2.0, 2.0 ] );
62+
63+
var v = smeanors( x.length, x, 1 );
64+
// returns ~0.3333
65+
```
66+
67+
The function has the following parameters:
68+
69+
- **N**: number of indexed elements.
70+
- **x**: input [`Float32Array`][@stdlib/array/float32].
71+
- **strideX**: stride length for `x`.
72+
73+
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to compute the [arithmetic mean][arithmetic-mean] of every other element in `x`,
74+
75+
```javascript
76+
var Float32Array = require( '@stdlib/array/float32' );
77+
78+
var x = new Float32Array( [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0 ] );
79+
80+
var v = smeanors( 4, x, 2 );
81+
// returns 1.25
82+
```
83+
84+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
85+
86+
<!-- eslint-disable stdlib/capitalized-comments -->
87+
88+
```javascript
89+
var Float32Array = require( '@stdlib/array/float32' );
90+
91+
var x0 = new Float32Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
92+
var x1 = new Float32Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
93+
94+
var v = smeanors( 4, x1, 2 );
95+
// returns 1.25
96+
```
97+
98+
#### smeanors.ndarray( N, x, strideX, offsetX )
99+
100+
Computes the [arithmetic mean][arithmetic-mean] of a single-precision floating-point strided array using ordinary recursive summation and alternative indexing semantics.
101+
102+
```javascript
103+
var Float32Array = require( '@stdlib/array/float32' );
104+
105+
var x = new Float32Array( [ 1.0, -2.0, 2.0 ] );
106+
107+
var v = smeanors.ndarray( x.length, x, 1, 0 );
108+
// returns ~0.33333
109+
```
110+
111+
The function has the following additional parameters:
112+
113+
- **offsetX**: starting index for `x`.
114+
115+
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 [arithmetic mean][arithmetic-mean] for every other element in `x` starting from the second element
116+
117+
```javascript
118+
var Float32Array = require( '@stdlib/array/float32' );
119+
120+
var x = new Float32Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
121+
122+
var v = smeanors.ndarray( 4, x, 2, 1 );
123+
// returns 1.25
124+
```
125+
126+
</section>
127+
128+
<!-- /.usage -->
129+
130+
<section class="notes">
131+
132+
## Notes
133+
134+
- If `N <= 0`, both functions return `NaN`.
135+
- Ordinary recursive summation (i.e., a "simple" sum) is performant, but can incur significant numerical error. If performance is paramount and error tolerated, using ordinary recursive summation to compute an arithmetic mean is acceptable; in all other cases, exercise due caution.
136+
137+
</section>
138+
139+
<!-- /.notes -->
140+
141+
<section class="examples">
142+
143+
## Examples
144+
145+
<!-- eslint no-undef: "error" -->
146+
147+
```javascript
148+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
149+
var smeanors = require( '@stdlib/stats/strided/smeanors' );
150+
151+
var x = discreteUniform( 10, -50, 50, {
152+
'dtype': 'float32'
153+
});
154+
console.log( x );
155+
156+
var v = smeanors( x.length, x, 1 );
157+
console.log( v );
158+
```
159+
160+
</section>
161+
162+
<!-- /.examples -->
163+
164+
<!-- C interface documentation. -->
165+
166+
* * *
167+
168+
<section class="c">
169+
170+
## C APIs
171+
172+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
173+
174+
<section class="intro">
175+
176+
</section>
177+
178+
<!-- /.intro -->
179+
180+
<!-- C usage documentation. -->
181+
182+
183+
<section class="usage">
184+
185+
### Usage
186+
187+
```c
188+
#include "stdlib/stats/strided/smeanors.h"
189+
```
190+
191+
#### stdlib_strided_smeanors( N, \*X, strideX )
192+
193+
Computes the [arithmetic mean][arithmetic-mean] of a single-precision floating-point strided array using ordinary recursive summation.
194+
195+
```c
196+
const float x[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f };
197+
198+
float v = stdlib_strided_smeanors( 4, x, 2 );
199+
// returns 4.0f
200+
```
201+
202+
The function accepts the following arguments:
203+
204+
- **N**: `[in] CBLAS_INT` number of indexed elements.
205+
- **X**: `[in] float*` input array.
206+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
207+
208+
```c
209+
float stdlib_strided_smeanors( const CBLAS_INT N, const float *X, const CBLAS_INT strideX );
210+
```
211+
212+
#### stdlib_strided_smeanors_ndarray( N, \*X, strideX, offsetX )
213+
214+
Computes the [arithmetic mean][arithmetic-mean] of a single-precision floating-point strided array using ordinary recursive summation and alternative indexing semantics.
215+
216+
```c
217+
const float x[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f };
218+
219+
float v = stdlib_strided_smeanors_ndarray( 4, x, 2, 0 );
220+
// returns 4.0f
221+
```
222+
223+
The function accepts the following arguments:
224+
225+
- **N**: `[in] CBLAS_INT` number of indexed elements.
226+
- **X**: `[in] float*` input array.
227+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
228+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
229+
230+
```c
231+
float stdlib_strided_smeanors_ndarray( const CBLAS_INT N, const float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX );
232+
```
233+
234+
</section>
235+
236+
<!-- /.usage -->
237+
238+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
239+
240+
<section class="notes">
241+
242+
</section>
243+
244+
<!-- /.notes -->
245+
246+
<!-- C API usage examples. -->
247+
248+
<section class="examples">
249+
250+
### Examples
251+
252+
```c
253+
#include "stdlib/stats/strided/smeanors.h"
254+
#include <stdio.h>
255+
256+
int main( void ) {
257+
// Create a strided array:
258+
const float x[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f };
259+
260+
// Specify the number of elements:
261+
const int N = 4;
262+
263+
// Specify the stride length:
264+
const int strideX = 2;
265+
266+
// Compute the arithmetic mean:
267+
float v = stdlib_strided_smeanors( N, x, strideX );
268+
269+
// Print the result:
270+
printf( "mean: %f\n", v );
271+
}
272+
```
273+
274+
</section>
275+
276+
<!-- /.examples -->
277+
278+
</section>
279+
280+
<!-- /.c -->
281+
282+
* * *
283+
284+
<section class="references">
285+
286+
</section>
287+
288+
<!-- /.references -->
289+
290+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
291+
292+
<section class="related">
293+
294+
* * *
295+
296+
## See Also
297+
298+
- <span class="package-name">[`@stdlib/stats/strided/dmeanors`][@stdlib/stats/strided/dmeanors]</span><span class="delimiter">: </span><span class="description">calculate the arithmetic mean of a double-precision floating-point strided array using ordinary recursive summation.</span>
299+
- <span class="package-name">[`@stdlib/stats/strided/meanors`][@stdlib/stats/strided/meanors]</span><span class="delimiter">: </span><span class="description">calculate the arithmetic mean of a strided array using ordinary recursive summation.</span>
300+
- <span class="package-name">[`@stdlib/stats/strided/smean`][@stdlib/stats/strided/smean]</span><span class="delimiter">: </span><span class="description">calculate the arithmetic mean of a single-precision floating-point strided array.</span>
301+
- <span class="package-name">[`@stdlib/stats/strided/snanmeanors`][@stdlib/stats/strided/snanmeanors]</span><span class="delimiter">: </span><span class="description">calculate the arithmetic mean of a single-precision floating-point strided array, ignoring NaN values and using ordinary recursive summation.</span>
302+
303+
</section>
304+
305+
<!-- /.related -->
306+
307+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
308+
309+
<section class="links">
310+
311+
[arithmetic-mean]: https://en.wikipedia.org/wiki/Arithmetic_mean
312+
313+
[@stdlib/array/float32]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/float32
314+
315+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
316+
317+
<!-- <related-links> -->
318+
319+
[@stdlib/stats/strided/dmeanors]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/dmeanors
320+
321+
[@stdlib/stats/strided/meanors]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/meanors
322+
323+
[@stdlib/stats/strided/smean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/smean
324+
325+
[@stdlib/stats/strided/snanmeanors]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/snanmeanors
326+
327+
<!-- </related-links> -->
328+
329+
</section>
330+
331+
<!-- /.links -->

0 commit comments

Comments
 (0)