Skip to content

Commit fc042e3

Browse files
committed
feat: add ndarray/base/binary-reduce-strided1d-dispatch-factory
1 parent 5ef479c commit fc042e3

File tree

12 files changed

+2082
-0
lines changed

12 files changed

+2082
-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) 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+
# binaryStrided1dDispatchFactory
22+
23+
> Create a function for performing binary reduction on two input ndarrays.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
<!-- eslint-disable id-length -->
30+
31+
```javascript
32+
var binaryStrided1dDispatchFactory = require( '@stdlib/ndarray/base/binary-reduce-strided1d-dispatch-factory' );
33+
```
34+
35+
#### binaryStrided1dDispatchFactory( table, idtypes, odtypes, policies )
36+
37+
Returns a function for performing binary reduction on two input ndarrays.
38+
39+
<!-- eslint-disable id-length -->
40+
41+
```javascript
42+
var base = require( '@stdlib/blas/base/ndarray/gdot' );
43+
44+
var table = {
45+
'default': base
46+
};
47+
48+
var dtypes = [ 'float64', 'float32', 'generic' ];
49+
var policies = {
50+
'output': 'same',
51+
'casting': 'none'
52+
};
53+
54+
var binary = binaryStrided1dDispatchFactory( table, [ dtypes, dtypes ], dtypes, policies );
55+
```
56+
57+
The function has the following parameters:
58+
59+
- **table**: strided binary reduction function dispatch table. Must have the following properties:
60+
61+
- **default**: default strided binary reduction function which should be invoked when provided ndarrays have data types which do not have a corresponding specialized implementation.
62+
63+
A dispatch table may have the following additional properties:
64+
65+
- **types**: one-dimensional list of ndarray data types describing specialized input ndarray argument signatures. Only the input ndarray argument data types should be specified. Output ndarray and additional input ndarray argument data types should be omitted and are not considered during dispatch. The length of `types` must equal twice the number of strided functions specified by `fcns` (i.e., for every pair of input ndarray data types, there must be a corresponding strided binary reduction function in `fcns`).
66+
- **fcns**: list of strided binary reduction functions which are specific to specialized input ndarray argument signatures.
67+
68+
- **idtypes**: list containing lists of supported input data types for each input ndarray argument.
69+
70+
- **odtypes**: list of supported output data types.
71+
72+
- **policies**: dispatch policies. Must have the following properties:
73+
74+
- **output**: output data type [policy][@stdlib/ndarray/output-dtype-policies].
75+
- **casting**: input ndarray casting [policy][@stdlib/ndarray/input-casting-policies].
76+
77+
#### binary( x, y\[, ...args]\[, options] )
78+
79+
Performs a binary reduction on two provided input ndarrays.
80+
81+
<!-- eslint-disable id-length -->
82+
83+
```javascript
84+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
85+
var base = require( '@stdlib/blas/base/ndarray/gdot' );
86+
87+
var table = {
88+
'default': base
89+
};
90+
91+
var dtypes = [ 'float64', 'float32', 'generic' ];
92+
var policies = {
93+
'output': 'same',
94+
'casting': 'none'
95+
};
96+
97+
var binary = binaryStrided1dDispatchFactory( table, [ dtypes, dtypes ], dtypes, policies );
98+
99+
var xbuf = [ 4.0, 2.0, -3.0, 5.0, -1.0 ];
100+
var x = new ndarray( 'generic', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' );
101+
102+
var ybuf = [ 2.0, 6.0, -1.0, -4.0, 8.0 ];
103+
var y = new ndarray( 'generic', ybuf, [ ybuf.length ], [ 1 ], 0, 'row-major' );
104+
105+
var z = binary( x, y );
106+
// returns <ndarray>
107+
108+
var v = z.get();
109+
// returns -5.0
110+
```
111+
112+
The function has the following parameters:
113+
114+
- **x**: first input ndarray.
115+
- **y**: second input ndarray.
116+
- **...args**: additional input ndarray arguments (_optional_).
117+
- **options**: function options (_optional_).
118+
119+
The function accepts the following options:
120+
121+
- **dims**: list of dimensions over which to perform a binary reduction.
122+
- **dtype**: output ndarray data type. Setting this option, overrides the output data type policy.
123+
- **keepdims**: boolean indicating whether the reduced dimensions should be included in the returned ndarray as singleton dimensions. Default: `false`.
124+
125+
By default, the function returns an ndarray having a data type determined by the output data type policy. To override the default behavior, set the `dtype` option.
126+
127+
<!-- eslint-disable id-length -->
128+
129+
```javascript
130+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
131+
var base = require( '@stdlib/blas/base/ndarray/gdot' );
132+
var getDType = require( '@stdlib/ndarray/dtype' );
133+
134+
var table = {
135+
'default': base
136+
};
137+
138+
var dtypes = [ 'float64', 'float32', 'generic' ];
139+
var policies = {
140+
'output': 'same',
141+
'casting': 'none'
142+
};
143+
144+
var binary = binaryStrided1dDispatchFactory( table, [ dtypes, dtypes ], dtypes, policies );
145+
146+
var xbuf = [ 4.0, 2.0, -3.0, 5.0, -1.0 ];
147+
var x = new ndarray( 'generic', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' );
148+
149+
var ybuf = [ 2.0, 6.0, -1.0, -4.0, 8.0 ];
150+
var y = new ndarray( 'generic', ybuf, [ ybuf.length ], [ 1 ], 0, 'row-major' );
151+
152+
var z = binary( x, y, {
153+
'dtype': 'float64'
154+
});
155+
// returns <ndarray>
156+
157+
var dt = getDType( z );
158+
// returns 'float64'
159+
```
160+
161+
#### binary.assign( x, y\[, ...args], out\[, options] )
162+
163+
Performs a binary reduction on two provided input ndarrays and assigns results to a provided output ndarray.
164+
165+
<!-- eslint-disable id-length -->
166+
167+
```javascript
168+
var base = require( '@stdlib/blas/base/ndarray/gdot' );
169+
var dtypes = require( '@stdlib/ndarray/dtypes' );
170+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
171+
172+
var idt = dtypes( 'real_and_generic' );
173+
var odt = idt;
174+
var policies = {
175+
'output': 'same',
176+
'casting': 'none'
177+
};
178+
179+
var table = {
180+
'default': base
181+
};
182+
var binary = binaryStrided1dDispatchFactory( table, [ idt, idt ], odt, policies );
183+
184+
var xbuf = [ 4.0, 2.0, -3.0, 5.0, -1.0 ];
185+
var x = new ndarray( 'generic', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' );
186+
187+
var ybuf = [ 2.0, 6.0, -1.0, -4.0, 8.0 ];
188+
var y = new ndarray( 'generic', ybuf, [ ybuf.length ], [ 1 ], 0, 'row-major' );
189+
190+
var zbuf = [ 0.0 ];
191+
var z = new ndarray( 'generic', zbuf, [], [ 0 ], 0, 'row-major' );
192+
193+
var out = binary.assign( x, y, z );
194+
// returns <ndarray>
195+
196+
var v = out.get();
197+
// returns -5.0
198+
199+
var bool = ( out === z );
200+
// returns true
201+
```
202+
203+
The method has the following parameters:
204+
205+
- **x**: first input ndarray.
206+
- **y**: second input ndarray.
207+
- **args**: additional input ndarray arguments (_optional_).
208+
- **out**: output ndarray.
209+
- **options**: function options (_optional_).
210+
211+
The method accepts the following options:
212+
213+
- **dims**: list of dimensions over which to perform a binary reduction.
214+
215+
</section>
216+
217+
<!-- /.usage -->
218+
219+
<section class="notes">
220+
221+
## Notes
222+
223+
- A strided binary reduction function should have the following signature:
224+
225+
```text
226+
f( arrays )
227+
```
228+
229+
where
230+
231+
- **arrays**: array containing two input ndarrays, followed by any additional ndarray arguments.
232+
233+
- The output data type policy only applies to the function returned by the main function. For the `assign` method, the output ndarray is allowed to have any supported output data type.
234+
235+
</section>
236+
237+
<!-- /.notes -->
238+
239+
<section class="examples">
240+
241+
## Examples
242+
243+
<!-- eslint-disable id-length, max-len -->
244+
245+
<!-- eslint no-undef: "error" -->
246+
247+
```javascript
248+
var ddot = require( '@stdlib/blas/base/ndarray/ddot' );
249+
var sdot = require( '@stdlib/blas/base/ndarray/sdot' );
250+
var base = require( '@stdlib/blas/base/ndarray/gdot' );
251+
var uniform = require( '@stdlib/random/array/uniform' );
252+
var dtypes = require( '@stdlib/ndarray/dtypes' );
253+
var dtype = require( '@stdlib/ndarray/dtype' );
254+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
255+
var ndarray = require( '@stdlib/ndarray/ctor' );
256+
var binaryStrided1dDispatchFactory = require( '@stdlib/ndarray/base/binary-reduce-strided1d-dispatch-factory' );
257+
258+
// Define the supported input and output data types:
259+
var idt = dtypes( 'real_and_generic' );
260+
var odt = dtypes( 'real_and_generic' );
261+
262+
// Define dispatch policies:
263+
var policies = {
264+
'output': 'same',
265+
'casting': 'none'
266+
};
267+
268+
// Define a dispatch table:
269+
var table = {
270+
'types': [
271+
'float64', 'float64', // first and second input
272+
'float32', 'float32' // first and second input
273+
],
274+
'fcns': [
275+
ddot,
276+
sdot
277+
],
278+
'default': base
279+
};
280+
281+
// Create an interface for performing a binary reduction:
282+
var dot = binaryStrided1dDispatchFactory( table, [ idt, idt ], odt, policies );
283+
284+
// Generate arrays of random numbers:
285+
var xbuf = uniform( 100, -1.0, 1.0, {
286+
'dtype': 'generic'
287+
});
288+
var ybuf = uniform( 100, -1.0, 1.0, {
289+
'dtype': 'generic'
290+
});
291+
292+
// Wrap in ndarrays:
293+
var x = new ndarray( 'generic', xbuf, [ 10, 10 ], [ 10, 1 ], 0, 'row-major' );
294+
var y = new ndarray( 'generic', ybuf, [ 10, 10 ], [ 10, 1 ], 0, 'row-major' );
295+
296+
// Perform a binary reduction:
297+
var z = dot( x, y, {
298+
'dims': [ 0 ]
299+
});
300+
301+
// Resolve the output array data type:
302+
var dt = dtype( z );
303+
console.log( dt );
304+
305+
// Print the results:
306+
console.log( ndarray2array( z ) );
307+
```
308+
309+
</section>
310+
311+
<!-- /.examples -->
312+
313+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
314+
315+
<section class="related">
316+
317+
</section>
318+
319+
<!-- /.related -->
320+
321+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
322+
323+
<section class="links">
324+
325+
[@stdlib/ndarray/output-dtype-policies]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/output-dtype-policies
326+
327+
[@stdlib/ndarray/input-casting-policies]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/input-casting-policies
328+
329+
</section>
330+
331+
<!-- /.links -->

0 commit comments

Comments
 (0)