Skip to content

Commit a889189

Browse files
feat: add stats/strided/wasm/dmeanwd
PR-URL: #7188 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]>
1 parent 2b194d1 commit a889189

34 files changed

+4407
-1
lines changed

lib/node_modules/@stdlib/stats/strided/dmeanwd/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: 295 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,295 @@
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+
# dmeanwd
22+
23+
> Compute the [arithmetic mean][arithmetic-mean] of a double-precision floating-point strided array using Welford's algorithm.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var dmeanwd = require( '@stdlib/stats/strided/wasm/dmeanwd' );
31+
```
32+
33+
#### dmeanwd.main( N, x, strideX )
34+
35+
Computes the [arithmetic mean][arithmetic-mean] of a double-precision floating-point strided array using Welford's algorithm.
36+
37+
```javascript
38+
var Float64Array = require( '@stdlib/array/float64' );
39+
40+
var x = new Float64Array( [ 1.0, -2.0, 2.0 ] );
41+
42+
var y = dmeanwd.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 [`Float64Array`][@stdlib/array/float64].
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 Float64Array = require( '@stdlib/array/float64' );
56+
57+
var x = new Float64Array( [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0 ] );
58+
59+
var y = dmeanwd.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 Float64Array = require( '@stdlib/array/float64' );
69+
70+
var x0 = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
71+
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
72+
73+
var y = dmeanwd.main( 4, x1, 2 );
74+
// returns 1.25
75+
```
76+
77+
#### dmeanwd.ndarray( N, x, strideX, offsetX )
78+
79+
Computes the [arithmetic mean][arithmetic-mean] of a double-precision floating-point strided array using Welford's algorithm and alternative indexing semantics.
80+
81+
```javascript
82+
var Float64Array = require( '@stdlib/array/float64' );
83+
84+
var x = new Float64Array( [ 1.0, -2.0, 2.0 ] );
85+
86+
var y = dmeanwd.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 Float64Array = require( '@stdlib/array/float64' );
98+
99+
var x = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
100+
101+
var y = dmeanwd.ndarray( 4, x, 2, 1 );
102+
// returns 1.25
103+
```
104+
105+
* * *
106+
107+
### Module
108+
109+
#### dmeanwd.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 dmeanwd.Module( mem );
126+
// returns <Module>
127+
128+
// Initialize the routine:
129+
mod.initializeSync();
130+
```
131+
132+
#### dmeanwd.Module.prototype.main( N, xp, sx )
133+
134+
Computes the [arithmetic mean][arithmetic-mean] of a double-precision floating-point strided array using Welford's algorithm.
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 dmeanwd.Module( mem );
151+
// returns <Module>
152+
153+
// Initialize the routine:
154+
mod.initializeSync();
155+
156+
// Define a vector data type:
157+
var dtype = 'float64';
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 [`Float64Array`][@stdlib/array/float64] pointer (i.e., byte offset).
177+
- **sx**: stride length for `x`.
178+
179+
#### dmeanwd.Module.prototype.ndarray( N, alpha, xp, sx, ox )
180+
181+
Computes the [arithmetic mean][arithmetic-mean] of a double-precision floating-point strided array using Welford's algorithm and alternative indexing semantics.
182+
183+
<!-- eslint-disable node/no-sync -->
184+
185+
```javascript
186+
var Memory = require( '@stdlib/wasm/memory' );
187+
var oneTo = require( '@stdlib/array/one-to' );
188+
var zeros = require( '@stdlib/array/zeros' );
189+
190+
// Create a new memory instance with an initial size of 10 pages (640KiB) and a maximum size of 100 pages (6.4MiB):
191+
var mem = new Memory({
192+
'initial': 10,
193+
'maximum': 100
194+
});
195+
196+
// Create a new routine:
197+
var mod = new dmeanwd.Module( mem );
198+
// returns <Module>
199+
200+
// Initialize the routine:
201+
mod.initializeSync();
202+
203+
// Define a vector data type:
204+
var dtype = 'float64';
205+
206+
// Specify a vector length:
207+
var N = 3;
208+
209+
// Define a pointer (i.e., byte offset) for storing the input vector:
210+
var xptr = 0;
211+
212+
// Write vector values to module memory:
213+
mod.write( xptr, oneTo( N, dtype ) );
214+
215+
// Perform computation:
216+
var y = mod.ndarray( N, xptr, 1, 0 );
217+
// returns 2.0
218+
```
219+
220+
The function has the following additional parameters:
221+
222+
- **ox**: starting index for `x`.
223+
224+
</section>
225+
226+
<!-- /.usage -->
227+
228+
<section class="notes">
229+
230+
* * *
231+
232+
## Notes
233+
234+
- If `N <= 0`, both `main` and `ndarray` methods return `0.0`.
235+
- This package implements routines using WebAssembly. When provided arrays which are not allocated on a `dmeanwd` 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/dmeanwd`][@stdlib/stats/strided/dmeanwd]. 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/dmeanwd`][@stdlib/stats/strided/dmeanwd]. 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.
236+
237+
</section>
238+
239+
<!-- /.notes -->
240+
241+
<section class="examples">
242+
243+
* * *
244+
245+
## Examples
246+
247+
<!-- eslint no-undef: "error" -->
248+
249+
```javascript
250+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
251+
var dmeanwd = require( '@stdlib/stats/strided/wasm/dmeanwd' );
252+
253+
var opts = {
254+
'dtype': 'float64'
255+
};
256+
var x = discreteUniform( 10, 0, 100, opts );
257+
console.log( x );
258+
259+
var y = dmeanwd.ndarray( x.length, x, 1, 0 );
260+
console.log( y );
261+
```
262+
263+
</section>
264+
265+
<!-- /.examples -->
266+
267+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
268+
269+
<section class="related">
270+
271+
</section>
272+
273+
<!-- /.related -->
274+
275+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
276+
277+
<section class="links">
278+
279+
[arithmetic-mean]: https://en.wikipedia.org/wiki/Arithmetic_mean
280+
281+
[@stdlib/array/float64]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/float64
282+
283+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
284+
285+
[@stdlib/wasm/memory]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/wasm/memory
286+
287+
[@stdlib/wasm/module-wrapper]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/wasm/module-wrapper
288+
289+
[@stdlib/stats/strided/dmeanwd]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/dmeanwd
290+
291+
<!-- </related-links> -->
292+
293+
</section>
294+
295+
<!-- /.links -->

0 commit comments

Comments
 (0)