Skip to content

Commit 458483f

Browse files
committed
feat: add ndarray/base/nullary-strided1d-dispatch-factory
1 parent 0e9bfc8 commit 458483f

File tree

11 files changed

+1613
-0
lines changed

11 files changed

+1613
-0
lines changed
Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
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+
# nullaryStrided1dDispatchFactory
22+
23+
> Create a function for applying a strided function to an output ndarray.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
<!-- eslint-disable id-length -->
30+
31+
```javascript
32+
var nullaryStrided1dDispatchFactory = require( '@stdlib/ndarray/base/nullary-strided1d-dispatch-factory' );
33+
```
34+
35+
#### nullaryStrided1dDispatchFactory( table, odtypes, policies\[, options] )
36+
37+
Returns a function for applying a strided function to an output ndarray.
38+
39+
<!-- eslint-disable id-length -->
40+
41+
```javascript
42+
var base = require( '@stdlib/blas/ext/base/ndarray/gsorthp' );
43+
44+
var table = {
45+
'default': base
46+
};
47+
48+
var dtypes = [ 'float64', 'float32', 'generic' ];
49+
var policies = {
50+
'output': 'same'
51+
};
52+
53+
var nullary = nullaryStrided1dDispatchFactory( table, [ dtypes ], policies );
54+
```
55+
56+
The function has the following parameters:
57+
58+
- **table**: strided function dispatch table. Must have the following properties:
59+
60+
- **default**: default strided function which should be invoked when provided ndarrays have data types which do not have a corresponding specialized implementation.
61+
62+
A dispatch table may have the following additional properties:
63+
64+
- **types**: one-dimensional list of ndarray data types describing specialized output ndarray argument signatures. Only the output ndarray argument data types should be specified. Additional ndarray argument data types should be omitted and are not considered during dispatch. The length of `types` must equal the number of strided functions specified by `fcns`.
65+
- **fcns**: list of strided functions which are specific to specialized output ndarray argument signatures.
66+
67+
- **odtypes**: list containing lists of supported output data types for each input ndarray argument.
68+
69+
- **policies**: dispatch policies. Must have the following properties:
70+
71+
- **output**: output data type [policy][@stdlib/ndarray/output-dtype-policies].
72+
73+
- **options**: function options (_optional_).
74+
75+
The function supports the following options:
76+
77+
- **strictTraversalOrder**: boolean specifying whether the order of element traversal must match the memory layout order of an input ndarray. Default: `false`.
78+
79+
#### unary.assign( x\[, ...args]\[, options] )
80+
81+
Applies a strided function and assigns results to a provided output ndarray.
82+
83+
<!-- eslint-disable id-length -->
84+
85+
```javascript
86+
var base = require( '@stdlib/blas/ext/base/ndarray/gsorthp' );
87+
var dtypes = require( '@stdlib/ndarray/dtypes' );
88+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
89+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
90+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
91+
92+
var odt = dtypes( 'real_and_generic' );
93+
var policies = {
94+
'output': 'same'
95+
};
96+
97+
var table = {
98+
'default': base
99+
};
100+
var nullary = nullaryStrided1dDispatchFactory( table, [ odt ], policies );
101+
102+
var xbuf = [ -1.0, 2.0, -3.0 ];
103+
var x = new ndarray( 'generic', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' );
104+
105+
var o = scalar2ndarray( 1.0, {
106+
'dtype': 'generic'
107+
});
108+
109+
var out = nullary.assign( x, o );
110+
// returns <ndarray>
111+
112+
var arr = ndarray2array( out );
113+
// returns [ -3.0, -1.0, 2.0 ]
114+
115+
var bool = ( out === x );
116+
// returns true
117+
```
118+
119+
The method has the following parameters:
120+
121+
- **x**: output ndarray.
122+
- **args**: additional output ndarray arguments (_optional_).
123+
- **options**: function options (_optional_).
124+
125+
The method accepts the following options:
126+
127+
- **dims**: list of dimensions over which to perform operation.
128+
129+
</section>
130+
131+
<!-- /.usage -->
132+
133+
<section class="notes">
134+
135+
## Notes
136+
137+
- A strided function should have the following signature:
138+
139+
```text
140+
f( arrays )
141+
```
142+
143+
where
144+
145+
- **arrays**: array containing an output ndarray, followed by any additional ndarray arguments.
146+
147+
- 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.
148+
149+
</section>
150+
151+
<!-- /.notes -->
152+
153+
<section class="examples">
154+
155+
## Examples
156+
157+
<!-- eslint-disable id-length, max-len, array-element-newline -->
158+
159+
<!-- eslint no-undef: "error" -->
160+
161+
```javascript
162+
var dsorthp = require( '@stdlib/blas/ext/base/ndarray/dsorthp' );
163+
var ssorthp = require( '@stdlib/blas/ext/base/ndarray/ssorthp' );
164+
var base = require( '@stdlib/blas/ext/base/ndarray/gsorthp' );
165+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
166+
var dtypes = require( '@stdlib/ndarray/dtypes' );
167+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
168+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
169+
var ndarray = require( '@stdlib/ndarray/ctor' );
170+
var nullaryStrided1dDispatchFactory = require( '@stdlib/ndarray/base/nullary-strided1d-dispatch-factory' );
171+
172+
// Define the supported output data types:
173+
var odt = dtypes( 'all' );
174+
175+
// Define dispatch policies:
176+
var policies = {
177+
'output': 'same'
178+
};
179+
180+
// Define a dispatch table:
181+
var table = {
182+
'types': [
183+
'float64',
184+
'float32'
185+
],
186+
'fcns': [
187+
dsorthp,
188+
ssorthp
189+
],
190+
'default': base
191+
};
192+
193+
// Create an interface for performing an operation:
194+
var sorthp = nullaryStrided1dDispatchFactory( table, [ odt ], policies );
195+
196+
// Generate an array of random numbers:
197+
var xbuf = discreteUniform( 25, -10, 10, {
198+
'dtype': 'generic'
199+
});
200+
201+
// Wrap in an ndarray:
202+
var x = new ndarray( 'generic', xbuf, [ 5, 5 ], [ 5, 1 ], 0, 'row-major' );
203+
console.log( ndarray2array( x ) );
204+
205+
var o = scalar2ndarray( 1.0, {
206+
'dtype': 'generic'
207+
});
208+
209+
// Perform operation:
210+
sorthp.assign( x, o, {
211+
'dims': [ 0 ]
212+
});
213+
214+
// Print the results:
215+
console.log( ndarray2array( x ) );
216+
```
217+
218+
</section>
219+
220+
<!-- /.examples -->
221+
222+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
223+
224+
<section class="related">
225+
226+
</section>
227+
228+
<!-- /.related -->
229+
230+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
231+
232+
<section class="links">
233+
234+
[@stdlib/ndarray/output-dtype-policies]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/output-dtype-policies
235+
236+
</section>
237+
238+
<!-- /.links -->
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var pow = require( '@stdlib/math/base/special/pow' );
26+
var dtypes = require( '@stdlib/array/dtypes' );
27+
var uniform = require( '@stdlib/random/array/uniform' );
28+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
29+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
30+
var gsorthp = require( '@stdlib/blas/ext/base/ndarray/gsorthp' );
31+
var pkg = require( './../package.json' ).name;
32+
var factory = require( './../lib' );
33+
34+
35+
// FUNCTIONS //
36+
37+
/**
38+
* Creates a benchmark function.
39+
*
40+
* @private
41+
* @param {PositiveInteger} len - array length
42+
* @returns {Function} benchmark function
43+
*/
44+
function createBenchmark( len ) {
45+
var policies;
46+
var nullary;
47+
var table;
48+
var dt;
49+
var x;
50+
var o;
51+
52+
table = {
53+
'default': gsorthp
54+
};
55+
dt = dtypes( 'all' );
56+
policies = {
57+
'output': 'same'
58+
};
59+
nullary = factory( table, [ dt ], policies );
60+
61+
x = uniform( len, -50.0, 50.0, {
62+
'dtype': 'float64'
63+
});
64+
x = new ndarray( 'float64', x, [ len ], [ 1 ], 0, 'row-major' );
65+
o = scalar2ndarray( 1.0, {
66+
'dtype': 'generic'
67+
});
68+
69+
return benchmark;
70+
71+
/**
72+
* Benchmark function.
73+
*
74+
* @private
75+
* @param {Benchmark} b - benchmark instance
76+
*/
77+
function benchmark( b ) {
78+
var o;
79+
var i;
80+
81+
b.tic();
82+
for ( i = 0; i < b.iterations; i++ ) {
83+
o = nullary.assign( x, o );
84+
if ( typeof o !== 'object' ) {
85+
b.fail( 'should return an ndarray' );
86+
}
87+
}
88+
b.toc();
89+
if ( isnan( o.get( i%len ) ) ) {
90+
b.fail( 'should not return NaN' );
91+
}
92+
b.pass( 'benchmark finished' );
93+
b.end();
94+
}
95+
}
96+
97+
98+
// MAIN //
99+
100+
/**
101+
* Main execution sequence.
102+
*
103+
* @private
104+
*/
105+
function main() {
106+
var len;
107+
var min;
108+
var max;
109+
var f;
110+
var i;
111+
112+
min = 1; // 10^min
113+
max = 6; // 10^max
114+
115+
for ( i = min; i <= max; i++ ) {
116+
len = pow( 10, i );
117+
f = createBenchmark( len );
118+
bench( pkg+':assign:len='+len, f );
119+
}
120+
}
121+
122+
main();

0 commit comments

Comments
 (0)