Skip to content

Commit 724283b

Browse files
headlessNodekgrytestdlib-bot
authored
feat: add ndarray/base/nullary-strided1d-dispatch-factory
PR-URL: #7828 Ref: #2656 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Co-authored-by: stdlib-bot <[email protected]>
1 parent 699c224 commit 724283b

File tree

11 files changed

+1324
-0
lines changed

11 files changed

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

0 commit comments

Comments
 (0)