Skip to content

Commit c42febe

Browse files
gururaj1512kgryte
andauthored
feat: add ndarray/base/binary-reduce-strided1d
PR-URL: #7813 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent cae1beb commit c42febe

Some content is hidden

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

55 files changed

+14324
-0
lines changed
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
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+
# binaryReduceStrided1d
22+
23+
> Perform a reduction over a list of specified dimensions in two input ndarrays via a one-dimensional strided array binary reduction function 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 binaryReduceStrided1d = require( '@stdlib/ndarray/base/binary-reduce-strided1d' );
37+
```
38+
39+
#### binaryReduceStrided1d( fcn, arrays, dims\[, options] )
40+
41+
Performs a reduction over a list of specified dimensions in two input ndarrays via a one-dimensional strided array binary reduction function 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 gdot = require( '@stdlib/blas/base/ndarray/gdot' );
49+
50+
// Create data buffers:
51+
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 ] );
52+
var ybuf = 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 ] );
53+
var zbuf = new Float64Array( [ 0.0, 0.0, 0.0 ] );
54+
55+
// Define the array shapes:
56+
var xsh = [ 1, 3, 2, 2 ];
57+
var ysh = [ 1, 3, 2, 2 ];
58+
var zsh = [ 1, 3 ];
59+
60+
// Define the array strides:
61+
var sx = [ 12, 4, 2, 1 ];
62+
var sy = [ 12, 4, 2, 1 ];
63+
var sz = [ 3, 1 ];
64+
65+
// Define the index offsets:
66+
var ox = 0;
67+
var oy = 0;
68+
var oz = 0;
69+
70+
// Create input ndarray-like objects:
71+
var x = {
72+
'dtype': 'float64',
73+
'data': xbuf,
74+
'shape': xsh,
75+
'strides': sx,
76+
'offset': ox,
77+
'order': 'row-major'
78+
};
79+
var y = {
80+
'dtype': 'float64',
81+
'data': ybuf,
82+
'shape': ysh,
83+
'strides': sy,
84+
'offset': oy,
85+
'order': 'row-major'
86+
};
87+
88+
// Create an output ndarray-like object:
89+
var z = {
90+
'dtype': 'float64',
91+
'data': zbuf,
92+
'shape': zsh,
93+
'strides': sz,
94+
'offset': oz,
95+
'order': 'row-major'
96+
};
97+
98+
// Perform a reduction:
99+
binaryReduceStrided1d( gdot, [ x, y, z ], [ 2, 3 ] );
100+
101+
var arr = ndarray2array( z.data, z.shape, z.strides, z.offset, z.order );
102+
// returns [ [ 30.0, 174.0, 446.0 ] ]
103+
```
104+
105+
The function accepts the following arguments:
106+
107+
- **fcn**: function which will be applied to two one-dimensional subarrays and should reduce them to a single scalar value.
108+
- **arrays**: array-like object containing two input ndarrays and one output ndarray, followed by any additional ndarray arguments.
109+
- **dims**: list of dimensions over which to perform a reduction.
110+
- **options**: function options which are passed through to `fcn` (_optional_).
111+
112+
Each provided ndarray should be an object with the following properties:
113+
114+
- **dtype**: data type.
115+
- **data**: data buffer.
116+
- **shape**: dimensions.
117+
- **strides**: stride lengths.
118+
- **offset**: index offset.
119+
- **order**: specifies whether an ndarray is row-major (C-style) or column major (Fortran-style).
120+
121+
#### TODO: document factory method
122+
123+
</section>
124+
125+
<!-- /.usage -->
126+
127+
<section class="notes">
128+
129+
## Notes
130+
131+
- The output ndarray and any additional ndarray arguments are expected to have the same dimensions as the non-reduced dimensions of the input ndarrays. When calling the reduction function, any additional ndarray arguments are provided as zero-dimensional ndarray-like objects.
132+
133+
- The reduction function is expected to have the following signature:
134+
135+
```text
136+
fcn( arrays[, options] )
137+
```
138+
139+
where
140+
141+
- **arrays**: array containing a one-dimensional subarray for each input ndarray and any additional ndarray arguments as zero-dimensional ndarrays.
142+
- **options**: function options (_optional_).
143+
144+
- 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.
145+
146+
</section>
147+
148+
<!-- /.notes -->
149+
150+
<section class="examples">
151+
152+
## Examples
153+
154+
<!-- eslint no-undef: "error" -->
155+
156+
```javascript
157+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
158+
var zeros = require( '@stdlib/array/base/zeros' );
159+
var ndarray2array = require( '@stdlib/ndarray/base/to-array' );
160+
var gdot = require( '@stdlib/blas/base/ndarray/gdot' );
161+
var binaryReduceStrided1d = require( '@stdlib/ndarray/base/binary-reduce-strided1d' );
162+
163+
var N = 10;
164+
var x = {
165+
'dtype': 'generic',
166+
'data': discreteUniform( N, -5, 5, {
167+
'dtype': 'generic'
168+
}),
169+
'shape': [ 1, 5, 2 ],
170+
'strides': [ 10, 2, 1 ],
171+
'offset': 0,
172+
'order': 'row-major'
173+
};
174+
var y = {
175+
'dtype': 'generic',
176+
'data': discreteUniform( N, -5, 5, {
177+
'dtype': 'generic'
178+
}),
179+
'shape': [ 1, 5, 2 ],
180+
'strides': [ 10, 2, 1 ],
181+
'offset': 0,
182+
'order': 'row-major'
183+
};
184+
var z = {
185+
'dtype': 'generic',
186+
'data': zeros( 2 ),
187+
'shape': [ 1, 2 ],
188+
'strides': [ 2, 1 ],
189+
'offset': 0,
190+
'order': 'row-major'
191+
};
192+
193+
binaryReduceStrided1d( gdot, [ x, y, z ], [ 1 ] );
194+
195+
console.log( ndarray2array( x.data, x.shape, x.strides, x.offset, x.order ) );
196+
console.log( ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ) );
197+
console.log( ndarray2array( z.data, z.shape, z.strides, z.offset, z.order ) );
198+
```
199+
200+
</section>
201+
202+
<!-- /.examples -->
203+
204+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
205+
206+
<section class="related">
207+
208+
</section>
209+
210+
<!-- /.related -->
211+
212+
<section class="links">
213+
214+
</section>
215+
216+
<!-- /.links -->
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
2+
{{alias}}( fcn, arrays, dims[, options] )
3+
Performs a reduction over a list of specified dimensions in two input
4+
ndarrays via a one-dimensional strided array binary reduction function
5+
and assigns results to a provided output ndarray.
6+
7+
Each provided "ndarray" should be an object with the following properties:
8+
9+
- dtype: data type.
10+
- data: data buffer.
11+
- shape: dimensions.
12+
- strides: stride lengths.
13+
- offset: index offset.
14+
- order: specifies whether an ndarray is row-major (C-style) or column-major
15+
(Fortran-style).
16+
17+
The output ndarray and any additional ndarray arguments are expected to have
18+
the same dimensions as the non-reduced dimensions of the input ndarray. When
19+
calling the reduction function, any additional ndarray arguments are
20+
provided as zero-dimensional ndarray-like objects.
21+
22+
Parameters
23+
----------
24+
fcn: Function
25+
Binary reduction function which will be applied to two one-dimensional
26+
subarrays and should reduce them to a single scalar value. The function
27+
should have the following signature:
28+
29+
fcn( arrays[, options] )
30+
31+
where
32+
33+
- arrays: array containing a one-dimensional subarrays for each input
34+
ndarray and any additional ndarray arguments as zero-dimensional
35+
ndarrays.
36+
- options: function options.
37+
38+
arrays: ArrayLikeObject<ndarray>
39+
Array-like object containing two input ndarrays and one output ndarray,
40+
followed by any additional ndarray arguments.
41+
42+
dims: Array<integer>
43+
List of dimensions over which to perform a reduction.
44+
45+
options: Object (optional)
46+
Function options.
47+
48+
Examples
49+
--------
50+
// Define ndarray data and meta data...
51+
> var gdot = require( '@stdlib/blas/base/ndarray/gdot' );
52+
> var xbuf = new {{alias:@stdlib/array/float64}}( [ 1.0, 2.0, 3.0, 4.0 ] );
53+
> var ybuf = new {{alias:@stdlib/array/float64}}( [ 1.0, 2.0, 3.0, 4.0 ] );
54+
> var zbuf = new {{alias:@stdlib/array/float64}}( [ 0.0 ] );
55+
> var dtype = 'float64';
56+
> var shx = [ 2, 2 ];
57+
> var shy = [ 2, 2 ];
58+
> var shz = [];
59+
> var sx = [ 2, 1 ];
60+
> var sy = [ 2, 1 ];
61+
> var sz = [ 0 ];
62+
> var ox = 0;
63+
> var oy = 0;
64+
> var oz = 0;
65+
> var order = 'row-major';
66+
67+
// Create ndarray objects...
68+
> var x = {
69+
... 'dtype': dtype,
70+
... 'data': xbuf,
71+
... 'shape': shx,
72+
... 'strides': sx,
73+
... 'offset': ox,
74+
... 'order': order
75+
... };
76+
> var y = {
77+
... 'dtype': dtype,
78+
... 'data': ybuf,
79+
... 'shape': shy,
80+
... 'strides': sy,
81+
... 'offset': oy,
82+
... 'order': order
83+
... };
84+
> var z = {
85+
... 'dtype': dtype,
86+
... 'data': zbuf,
87+
... 'shape': shz,
88+
... 'strides': sz,
89+
... 'offset': oz,
90+
... 'order': order
91+
... };
92+
> {{alias}}( gdot, [ x, y, z ], [ 0, 1 ] );
93+
> z.data
94+
<Float64Array>[ 30.0 ]
95+
96+
See Also
97+
--------
98+
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
22+
var zeros = require( '@stdlib/array/base/zeros' );
23+
var ndarray2array = require( '@stdlib/ndarray/base/to-array' );
24+
var gdot = require( '@stdlib/blas/base/ndarray/gdot' );
25+
var binaryReduceStrided1d = require( './../lib' );
26+
27+
var N = 10;
28+
var x = {
29+
'dtype': 'generic',
30+
'data': discreteUniform( N, -5, 5, {
31+
'dtype': 'generic'
32+
}),
33+
'shape': [ 1, 5, 2 ],
34+
'strides': [ 10, 2, 1 ],
35+
'offset': 0,
36+
'order': 'row-major'
37+
};
38+
var y = {
39+
'dtype': 'generic',
40+
'data': discreteUniform( N, -5, 5, {
41+
'dtype': 'generic'
42+
}),
43+
'shape': [ 1, 5, 2 ],
44+
'strides': [ 10, 2, 1 ],
45+
'offset': 0,
46+
'order': 'row-major'
47+
};
48+
var z = {
49+
'dtype': 'generic',
50+
'data': zeros( 2 ),
51+
'shape': [ 1, 2 ],
52+
'strides': [ 2, 1 ],
53+
'offset': 0,
54+
'order': 'row-major'
55+
};
56+
57+
binaryReduceStrided1d( gdot, [ x, y, z ], [ 1 ] );
58+
59+
console.log( ndarray2array( x.data, x.shape, x.strides, x.offset, x.order ) );
60+
console.log( ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ) );
61+
console.log( ndarray2array( z.data, z.shape, z.strides, z.offset, z.order ) );

0 commit comments

Comments
 (0)