Skip to content

Commit 2232c76

Browse files
feat: wasm implementation for stats/strided/dsmeanwd
--- 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 59fda0c commit 2232c76

34 files changed

+4420
-1
lines changed

lib/node_modules/@stdlib/stats/strided/dsmeanwd/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
]
8181
},
8282
{
83-
"task": "",
83+
"task": "build",
8484
"wasm": true,
8585
"src": [
8686
"./src/main.c"
Lines changed: 296 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,296 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 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+
# dsmeanwd
22+
23+
> Calculate the [arithmetic mean][arithmetic-mean] of a single-precision floating-point strided array using Welford's algorithm with extended accumulation and returning an extended precision result.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var dsmeanwd = require( '@stdlib/stats/strided/wasm/dsmeanwd' );
31+
```
32+
33+
#### dsmeanwd.main( N, x, strideX )
34+
35+
Computes the [arithmetic mean][arithmetic-mean] of a single-precision floating-point strided array `x` using Welford's algorithm with extended accumulation and returning an extended precision result.
36+
37+
```javascript
38+
var Float32Array = require( '@stdlib/array/float32' );
39+
40+
var x = new Float32Array( [ 1.0, -2.0, 2.0 ] );
41+
42+
var y = dsmeanwd.main( x.length, x, 1 );
43+
// returns ~0.3333
44+
```
45+
46+
The function has the following parameters:
47+
48+
- **N**: number of indexed elements.
49+
- **x**: input [`Float32Array`][@stdlib/array/float32].
50+
- **strideX**: stride length for `x`.
51+
52+
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to access every other element in `x`,
53+
54+
```javascript
55+
var Float32Array = require( '@stdlib/array/float32' );
56+
57+
var x = new Float32Array( [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0 ] );
58+
59+
var y = dsmeanwd.main( 4, x, 2 );
60+
// returns 1.25
61+
```
62+
63+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
64+
65+
<!-- eslint-disable stdlib/capitalized-comments -->
66+
67+
```javascript
68+
var Float32Array = require( '@stdlib/array/float32' );
69+
70+
var x0 = new Float32Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
71+
var x1 = new Float32Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
72+
73+
var y = dsmeanwd.main( 4, x1, 2 );
74+
// returns 1.25
75+
```
76+
77+
#### dsmeanwd.ndarray( N, x, strideX, offsetX )
78+
79+
Computes the [arithmetic mean][arithmetic-mean] of a single-precision floating-point strided array using Welford's algorithm with extended accumulation and alternative indexing semantics and returning an extended precision result.
80+
81+
```javascript
82+
var Float32Array = require( '@stdlib/array/float32' );
83+
84+
var x = new Float32Array( [ 1.0, -2.0, 2.0 ] );
85+
86+
var y = dsmeanwd.ndarray( x.length, x, 1, 0 );
87+
// returns ~0.3333
88+
```
89+
90+
The function has the following additional parameters:
91+
92+
- **offsetX**: starting index for `x`.
93+
94+
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 access every other element starting from the second element:
95+
96+
```javascript
97+
var Float32Array = require( '@stdlib/array/float32' );
98+
99+
var x = new Float32Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
100+
101+
var y = dsmeanwd.ndarray( 4, x, 2, 1 );
102+
// returns 1.25
103+
```
104+
105+
* * *
106+
107+
### Module
108+
109+
#### dsmeanwd.Module( memory )
110+
111+
Returns a new WebAssembly [module wrapper][@stdlib/wasm/module-wrapper] instance which uses the provided WebAssembly [memory][@stdlib/wasm/memory] instance as its underlying memory.
112+
113+
<!-- eslint-disable node/no-sync -->
114+
115+
```javascript
116+
var Memory = require( '@stdlib/wasm/memory' );
117+
118+
// Create a new memory instance with an initial size of 10 pages (640KiB) and a maximum size of 100 pages (6.4MiB):
119+
var mem = new Memory({
120+
'initial': 10,
121+
'maximum': 100
122+
});
123+
124+
// Create a new routine:
125+
var mod = new dsmeanwd.Module( mem );
126+
// returns <Module>
127+
128+
// Initialize the routine:
129+
mod.initializeSync();
130+
```
131+
132+
#### dsmeanwd.Module.prototype.main( N, xp, sx )
133+
134+
Computes the [arithmetic mean][arithmetic-mean] of a single-precision floating-point strided array `x` using Welford's algorithm with extended accumulation and returning an extended precision result.
135+
136+
<!-- eslint-disable node/no-sync -->
137+
138+
```javascript
139+
var Memory = require( '@stdlib/wasm/memory' );
140+
var oneTo = require( '@stdlib/array/one-to' );
141+
var zeros = require( '@stdlib/array/zeros' );
142+
143+
// Create a new memory instance with an initial size of 10 pages (640KiB) and a maximum size of 100 pages (6.4MiB):
144+
var mem = new Memory({
145+
'initial': 10,
146+
'maximum': 100
147+
});
148+
149+
// Create a new routine:
150+
var mod = new dsmeanwd.Module( mem );
151+
// returns <Module>
152+
153+
// Initialize the routine:
154+
mod.initializeSync();
155+
156+
// Define a vector data type:
157+
var dtype = 'float32';
158+
159+
// Specify a vector length:
160+
var N = 3;
161+
162+
// Define a pointer (i.e., byte offset) for storing the input vector:
163+
var xptr = 0;
164+
165+
// Write vector values to module memory:
166+
mod.write( xptr, oneTo( N, dtype ) );
167+
168+
// Perform computation:
169+
var y = mod.main( N, xptr, 1 );
170+
// returns 2.0
171+
```
172+
173+
The function has the following parameters:
174+
175+
- **N**: number of indexed elements.
176+
- **xp**: input [`Float32Array`][@stdlib/array/float32] pointer (i.e., byte offset).
177+
- **sx**: stride length for `x`.
178+
179+
#### dsmeanwd.Module.prototype.ndarray( N, alpha, xp, sx, ox )
180+
181+
Computes the [arithmetic mean][arithmetic-mean] of a single-precision floating-point strided array using Welford's algorithm with extended accumulation and alternative indexing semantics and returning an extended precision result.
182+
183+
<!-- eslint-disable node/no-sync -->
184+
185+
```javascript
186+
var dsmeanwd = require( '@stdlib/stats/strided/wasm/dsmeanwd' );
187+
var Memory = require( '@stdlib/wasm/memory' );
188+
var oneTo = require( '@stdlib/array/one-to' );
189+
var zeros = require( '@stdlib/array/zeros' );
190+
191+
// Create a new memory instance with an initial size of 10 pages (640KiB) and a maximum size of 100 pages (6.4MiB):
192+
var mem = new Memory({
193+
'initial': 10,
194+
'maximum': 100
195+
});
196+
197+
// Create a new routine:
198+
var mod = new dsmeanwd.Module( mem );
199+
// returns <Module>
200+
201+
// Initialize the routine:
202+
mod.initializeSync();
203+
204+
// Define a vector data type:
205+
var dtype = 'float32';
206+
207+
// Specify a vector length:
208+
var N = 3;
209+
210+
// Define a pointer (i.e., byte offset) for storing the input vector:
211+
var xptr = 0;
212+
213+
// Write vector values to module memory:
214+
mod.write( xptr, oneTo( N, dtype ) );
215+
216+
// Perform computation:
217+
var y = mod.ndarray( N, xptr, 1, 0 );
218+
// returns 2.0
219+
```
220+
221+
The function has the following additional parameters:
222+
223+
- **ox**: starting index for `x`.
224+
225+
</section>
226+
227+
<!-- /.usage -->
228+
229+
<section class="notes">
230+
231+
* * *
232+
233+
## Notes
234+
235+
- If `N <= 0`, both `main` and `ndarray` methods return `0.0`.
236+
- This package implements routines using WebAssembly. When provided arrays which are not allocated on a `dsmeanwd` module memory instance, data must be explicitly copied to module memory prior to computation. Data movement may entail a performance cost, and, thus, if you are using arrays external to module memory, you should prefer using [`@stdlib/stats/strided/dsmeanwd`][@stdlib/stats/strided/dsmeanwd]. However, if working with arrays which are allocated and explicitly managed on module memory, you can achieve better performance when compared to the pure JavaScript implementations found in [`@stdlib/stats/strided/dsmeanwd`][@stdlib/stats/strided/dsmeanwd]. Beware that such performance gains may come at the cost of additional complexity when having to perform manual memory management. Choosing between implementations depends heavily on the particular needs and constraints of your application, with no one choice universally better than the other.
237+
238+
</section>
239+
240+
<!-- /.notes -->
241+
242+
<section class="examples">
243+
244+
* * *
245+
246+
## Examples
247+
248+
<!-- eslint no-undef: "error" -->
249+
250+
```javascript
251+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
252+
var dsmeanwd = require( '@stdlib/stats/strided/wasm/dsmeanwd' );
253+
254+
var opts = {
255+
'dtype': 'float32'
256+
};
257+
var x = discreteUniform( 10, 0, 100, opts );
258+
console.log( x );
259+
260+
var y = dsmeanwd.ndarray( x.length, x, 1, 0 );
261+
console.log( y );
262+
```
263+
264+
</section>
265+
266+
<!-- /.examples -->
267+
268+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
269+
270+
<section class="related">
271+
272+
</section>
273+
274+
<!-- /.related -->
275+
276+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
277+
278+
<section class="links">
279+
280+
[arithmetic-mean]: https://en.wikipedia.org/wiki/Arithmetic_mean
281+
282+
[@stdlib/array/float32]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/float32
283+
284+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
285+
286+
[@stdlib/wasm/memory]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/wasm/memory
287+
288+
[@stdlib/wasm/module-wrapper]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/wasm/module-wrapper
289+
290+
[@stdlib/stats/strided/dsmeanwd]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/dsmeanwd
291+
292+
<!-- </related-links> -->
293+
294+
</section>
295+
296+
<!-- /.links -->

0 commit comments

Comments
 (0)