Skip to content

Commit e1060bb

Browse files
committed
Auto-generated commit
1 parent da171d6 commit e1060bb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+13815
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
### Features
1212

13+
- [`b228b5e`](https://github.com/stdlib-js/stdlib/commit/b228b5e89da183b38160c6cd6f9908a07918dd09) - add `ndarray/base/unary-reduce-strided1d-by` [(#7214)](https://github.com/stdlib-js/stdlib/pull/7214)
1314
- [`67e9602`](https://github.com/stdlib-js/stdlib/commit/67e9602131f6e714d98de70ce6809cd9d327b02f) - add `someBy` to namespace
1415
- [`cc66d3b`](https://github.com/stdlib-js/stdlib/commit/cc66d3b709812126709d2d6c8232ffc2dbd8b576) - add `ndarray/some-by` [(#7145)](https://github.com/stdlib-js/stdlib/pull/7145)
1516
- [`e05819a`](https://github.com/stdlib-js/stdlib/commit/e05819abfe0fbf982469653a85add02159e8b122) - add `someBy` to namespace
@@ -432,6 +433,7 @@ A total of 17 issues were closed in this release:
432433

433434
<details>
434435

436+
- [`b228b5e`](https://github.com/stdlib-js/stdlib/commit/b228b5e89da183b38160c6cd6f9908a07918dd09) - **feat:** add `ndarray/base/unary-reduce-strided1d-by` [(#7214)](https://github.com/stdlib-js/stdlib/pull/7214) _(by Muhammad Haris, Athan Reines)_
435437
- [`3f6c0ff`](https://github.com/stdlib-js/stdlib/commit/3f6c0ffd9568665b54d0547b0550b2c96505bed6) - **fix:** address increment bug _(by Athan Reines)_
436438
- [`4d29349`](https://github.com/stdlib-js/stdlib/commit/4d29349017e6d62d7f72aa0c3a86a24fdd83e86a) - **fix:** address increment bug _(by Athan Reines)_
437439
- [`d1f5820`](https://github.com/stdlib-js/stdlib/commit/d1f58208fc95174fcfce03f00df278a8190578de) - **fix:** address increment bug _(by Athan Reines)_
Lines changed: 278 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,278 @@
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+
# unaryReduceStrided1dBy
22+
23+
> Perform a reduction over a list of specified dimensions in an input ndarray via a one-dimensional strided array reduction function accepting a callback and assign results to a provided output ndarray.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var unaryReduceStrided1dBy = require( '@stdlib/ndarray/base/unary-reduce-strided1d-by' );
37+
```
38+
39+
#### unaryReduceStrided1dBy( fcn, arrays, dims\[, options], clbk\[, thisArg] )
40+
41+
Performs a reduction over a list of specified dimensions in an input ndarray via a one-dimensional strided array reduction function accepting a callback and assigns results to a provided output ndarray.
42+
43+
<!-- eslint-disable max-len -->
44+
45+
```javascript
46+
var Float64Array = require( '@stdlib/array/float64' );
47+
var ndarray2array = require( '@stdlib/ndarray/base/to-array' );
48+
var maxBy = require( '@stdlib/stats/base/ndarray/max-by' );
49+
50+
// Define a callback function:
51+
function clbk( value ) {
52+
return value * 2.0;
53+
}
54+
55+
// Create data buffers:
56+
var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
57+
var ybuf = new Float64Array( [ 0.0, 0.0, 0.0 ] );
58+
59+
// Define the array shapes:
60+
var xsh = [ 1, 3, 2, 2 ];
61+
var ysh = [ 1, 3 ];
62+
63+
// Define the array strides:
64+
var sx = [ 12, 4, 2, 1 ];
65+
var sy = [ 3, 1 ];
66+
67+
// Define the index offsets:
68+
var ox = 0;
69+
var oy = 0;
70+
71+
// Create an input ndarray-like object:
72+
var x = {
73+
'dtype': 'float64',
74+
'data': xbuf,
75+
'shape': xsh,
76+
'strides': sx,
77+
'offset': ox,
78+
'order': 'row-major'
79+
};
80+
81+
// Create an output ndarray-like object:
82+
var y = {
83+
'dtype': 'float64',
84+
'data': ybuf,
85+
'shape': ysh,
86+
'strides': sy,
87+
'offset': oy,
88+
'order': 'row-major'
89+
};
90+
91+
// Perform a reduction:
92+
unaryReduceStrided1dBy( maxBy, [ x, y ], [ 2, 3 ], clbk );
93+
94+
var arr = ndarray2array( y.data, y.shape, y.strides, y.offset, y.order );
95+
// returns [ [ 8.0, 16.0, 24.0 ] ]
96+
```
97+
98+
The function accepts the following arguments:
99+
100+
- **fcn**: function which will be applied to a one-dimensional subarray and should reduce the subarray to a single scalar value.
101+
- **arrays**: array-like object containing one input ndarray and one output ndarray, followed by any additional ndarray arguments.
102+
- **dims**: list of dimensions over which to perform a reduction.
103+
- **options**: function options which are passed through to `fcn` (_optional_).
104+
- **clbk**: callback function.
105+
- **thisArg**: callback execution context (_optional_).
106+
107+
Each provided ndarray should be an object with the following properties:
108+
109+
- **dtype**: data type.
110+
- **data**: data buffer.
111+
- **shape**: dimensions.
112+
- **strides**: stride lengths.
113+
- **offset**: index offset.
114+
- **order**: specifies whether an ndarray is row-major (C-style) or column major (Fortran-style).
115+
116+
The invoked callback function is provided the following arguments:
117+
118+
- **value**: current array element.
119+
- **indices**: current array element indices.
120+
- **arr**: the input ndarray.
121+
122+
To set the callback execution context, provide a `thisArg`.
123+
124+
<!-- eslint-disable max-len -->
125+
126+
```javascript
127+
var Float64Array = require( '@stdlib/array/float64' );
128+
var ndarray2array = require( '@stdlib/ndarray/base/to-array' );
129+
var maxBy = require( '@stdlib/stats/base/ndarray/max-by' );
130+
131+
// Define a callback function:
132+
function clbk( value ) {
133+
this.count += 1;
134+
return value * 2.0;
135+
}
136+
137+
// Create data buffers:
138+
var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
139+
var ybuf = new Float64Array( [ 0.0, 0.0, 0.0 ] );
140+
141+
// Define the array shapes:
142+
var xsh = [ 1, 3, 2, 2 ];
143+
var ysh = [ 1, 3 ];
144+
145+
// Define the array strides:
146+
var sx = [ 12, 4, 2, 1 ];
147+
var sy = [ 3, 1 ];
148+
149+
// Define the index offsets:
150+
var ox = 0;
151+
var oy = 0;
152+
153+
// Create an input ndarray-like object:
154+
var x = {
155+
'dtype': 'float64',
156+
'data': xbuf,
157+
'shape': xsh,
158+
'strides': sx,
159+
'offset': ox,
160+
'order': 'row-major'
161+
};
162+
163+
// Create an output ndarray-like object:
164+
var y = {
165+
'dtype': 'float64',
166+
'data': ybuf,
167+
'shape': ysh,
168+
'strides': sy,
169+
'offset': oy,
170+
'order': 'row-major'
171+
};
172+
173+
// Define callback execution context:
174+
var ctx = {
175+
'count': 0
176+
};
177+
178+
// Perform a reduction:
179+
unaryReduceStrided1dBy( maxBy, [ x, y ], [ 2, 3 ], clbk, ctx );
180+
181+
var arr = ndarray2array( y.data, y.shape, y.strides, y.offset, y.order );
182+
// returns [ [ 8.0, 16.0, 24.0 ] ]
183+
184+
var count = ctx.count;
185+
// returns 12
186+
```
187+
188+
#### TODO: document factory method
189+
190+
</section>
191+
192+
<!-- /.usage -->
193+
194+
<section class="notes">
195+
196+
## Notes
197+
198+
- The output ndarray and any additional ndarray arguments are expected to have the same dimensions as the non-reduced dimensions of the input ndarray. When calling the reduction function, any additional ndarray arguments are provided as zero-dimensional ndarray-like objects.
199+
200+
- The reduction function is expected to have the following signature:
201+
202+
```text
203+
fcn( arrays[, options], clbk[, thisArg ] )
204+
```
205+
206+
where
207+
208+
- **arrays**: array containing a one-dimensional subarray of the input ndarray and any additional ndarray arguments as zero-dimensional ndarrays.
209+
- **options**: function options (_optional_).
210+
- **clbk**: callback function.
211+
- **thisArg**: callback execution context (_optional_).
212+
213+
- For very high-dimensional ndarrays which are non-contiguous, one should consider copying the underlying data to contiguous memory before performing a reduction in order to achieve better performance.
214+
215+
</section>
216+
217+
<!-- /.notes -->
218+
219+
<section class="examples">
220+
221+
## Examples
222+
223+
<!-- eslint no-undef: "error" -->
224+
225+
```javascript
226+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
227+
var zeros = require( '@stdlib/array/base/zeros' );
228+
var ndarray2array = require( '@stdlib/ndarray/base/to-array' );
229+
var maxBy = require( '@stdlib/stats/base/ndarray/max-by' );
230+
var unaryReduceStrided1dBy = require( '@stdlib/ndarray/base/unary-reduce-strided1d-by' );
231+
232+
function clbk( value ) {
233+
return value * 2.0;
234+
}
235+
236+
var N = 10;
237+
var x = {
238+
'dtype': 'generic',
239+
'data': discreteUniform( N, -5, 5, {
240+
'dtype': 'generic'
241+
}),
242+
'shape': [ 1, 5, 2 ],
243+
'strides': [ 10, 2, 1 ],
244+
'offset': 0,
245+
'order': 'row-major'
246+
};
247+
var y = {
248+
'dtype': 'generic',
249+
'data': zeros( 2 ),
250+
'shape': [ 1, 5 ],
251+
'strides': [ 5, 1 ],
252+
'offset': 0,
253+
'order': 'row-major'
254+
};
255+
256+
unaryReduceStrided1dBy( maxBy, [ x, y ], [ 2 ], clbk );
257+
258+
console.log( ndarray2array( x.data, x.shape, x.strides, x.offset, x.order ) );
259+
console.log( ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ) );
260+
```
261+
262+
</section>
263+
264+
<!-- /.examples -->
265+
266+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
267+
268+
<section class="related">
269+
270+
</section>
271+
272+
<!-- /.related -->
273+
274+
<section class="links">
275+
276+
</section>
277+
278+
<!-- /.links -->

0 commit comments

Comments
 (0)