Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit cfe870b

Browse files
headlessNodekgrytestdlib-bot
authored
feat: add ndarray/base/nullary-strided1d-dispatch
PR-URL: #7821 Ref: #2656 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Co-authored-by: stdlib-bot <noreply@stdlib.io>
1 parent adde2e9 commit cfe870b

File tree

14 files changed

+1956
-0
lines changed

14 files changed

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

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /