Skip to content

Commit f609398

Browse files
committed
feat: add ndarray/base/nullary-strided1d-dispatch
1 parent b44815f commit f609398

File tree

14 files changed

+2055
-0
lines changed

14 files changed

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

0 commit comments

Comments
 (0)