Skip to content

Commit 23e187a

Browse files
authored
feat: add blas/ext/one-to
PR-URL: #11513 Reviewed-by: Athan Reines <kgryte@gmail.com> Closes: stdlib-js/metr-issue-tracker#248
1 parent e616ec3 commit 23e187a

File tree

17 files changed

+2275
-0
lines changed

17 files changed

+2275
-0
lines changed
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 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+
# oneTo
22+
23+
> Return a new [ndarray][@stdlib/ndarray/ctor] filled with linearly spaced numeric elements which increment by `1` starting from one along one or more [ndarray][@stdlib/ndarray/ctor] dimensions.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var oneTo = require( '@stdlib/blas/ext/one-to' );
31+
```
32+
33+
#### oneTo( shape\[, options] )
34+
35+
Returns a new [ndarray][@stdlib/ndarray/ctor] filled with linearly spaced numeric elements which increment by `1` starting from one along one or more [ndarray][@stdlib/ndarray/ctor] dimensions.
36+
37+
```javascript
38+
var x = oneTo( [ 2, 3 ] );
39+
// returns <ndarray>[ [ 1.0, 2.0, 3.0 ], [ 1.0, 2.0, 3.0 ] ]
40+
```
41+
42+
The function has the following parameters:
43+
44+
- **shape**: array shape.
45+
- **options**: function options (_optional_).
46+
47+
The function accepts the following options:
48+
49+
- **dims**: list of dimensions over which to perform operation. If not provided, the function generates values along the last dimension. Default: `[-1]`.
50+
- **dtype**: output [ndarray][@stdlib/ndarray/ctor] [data type][@stdlib/ndarray/dtypes]. Must be a numeric or "generic" [data type][@stdlib/ndarray/dtypes]. Default: `'float64'`.
51+
- **order**: specifies whether an [ndarray][@stdlib/ndarray/ctor] is `'row-major'` (C-style) or `'column-major'` (Fortran-style). Default: `'row-major'`.
52+
- **mode**: specifies how to handle indices which exceed array dimensions (see [`ndarray`][@stdlib/ndarray/ctor]). Default: `'throw'`.
53+
- **submode**: a mode array which specifies for each dimension how to handle subscripts which exceed array dimensions (see [`ndarray`][@stdlib/ndarray/ctor]). If provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. Default: `[ options.mode ]`.
54+
55+
By default, the function generates values along the last dimension of an output [ndarray][@stdlib/ndarray/ctor]. To perform the operation over specific dimensions, provide a `dims` option.
56+
57+
```javascript
58+
var x = oneTo( [ 2, 2 ], {
59+
'dims': [ 0, 1 ]
60+
});
61+
// returns <ndarray>[ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ]
62+
```
63+
64+
To specify the output [ndarray][@stdlib/ndarray/ctor] [data type][@stdlib/ndarray/dtypes], provide a `dtype` option.
65+
66+
```javascript
67+
var x = oneTo( [ 3 ], {
68+
'dtype': 'float32'
69+
});
70+
// returns <ndarray>[ 1.0, 2.0, 3.0 ]
71+
```
72+
73+
#### oneTo.assign( x\[, options] )
74+
75+
Fills an [ndarray][@stdlib/ndarray/ctor] with linearly spaced numeric elements which increment by `1` starting from one along one or more [ndarray][@stdlib/ndarray/ctor] dimensions.
76+
77+
```javascript
78+
var zeros = require( '@stdlib/ndarray/zeros' );
79+
80+
var x = zeros( [ 2, 3 ] );
81+
// returns <ndarray>[ [ 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0 ] ]
82+
83+
var out = oneTo.assign( x );
84+
// returns <ndarray>[ [ 1.0, 2.0, 3.0 ], [ 1.0, 2.0, 3.0 ] ]
85+
86+
var bool = ( x === out );
87+
// returns true
88+
```
89+
90+
The function has the following parameters:
91+
92+
- **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have a numeric or "generic" [data type][@stdlib/ndarray/dtypes].
93+
- **options**: function options (_optional_).
94+
95+
The function accepts the following options:
96+
97+
- **dims**: list of dimensions over which to perform operation. If not provided, the function generates values along the last dimension. Default: `[-1]`.
98+
99+
</section>
100+
101+
<!-- /.usage -->
102+
103+
<section class="notes">
104+
105+
## Notes
106+
107+
- The function iterates over [ndarray][@stdlib/ndarray/ctor] elements according to the memory layout of the input [ndarray][@stdlib/ndarray/ctor]. Accordingly, performance degradation is possible when operating over multiple dimensions of a large non-contiguous multi-dimensional input [ndarray][@stdlib/ndarray/ctor]. In such scenarios, one may want to copy an input [ndarray][@stdlib/ndarray/ctor] to contiguous memory before performing the operation.
108+
109+
</section>
110+
111+
<!-- /.notes -->
112+
113+
<section class="examples">
114+
115+
## Examples
116+
117+
<!-- eslint no-undef: "error" -->
118+
119+
```javascript
120+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
121+
var oneTo = require( '@stdlib/blas/ext/one-to' );
122+
123+
// Create a new ndarray:
124+
var x = oneTo( [ 5, 5 ], {
125+
'dtype': 'generic'
126+
});
127+
console.log( ndarray2array( x ) );
128+
129+
// Generate values over a specific dimension:
130+
x = oneTo( [ 5, 5 ], {
131+
'dtype': 'generic',
132+
'dims': [ 0 ]
133+
});
134+
console.log( ndarray2array( x ) );
135+
136+
// Generate values over all dimensions:
137+
x = oneTo( [ 5, 5 ], {
138+
'dtype': 'generic',
139+
'dims': [ 0, 1 ]
140+
});
141+
console.log( ndarray2array( x ) );
142+
```
143+
144+
</section>
145+
146+
<!-- /.examples -->
147+
148+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
149+
150+
<section class="related">
151+
152+
</section>
153+
154+
<!-- /.related -->
155+
156+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
157+
158+
<section class="links">
159+
160+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
161+
162+
[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes
163+
164+
</section>
165+
166+
<!-- /.links -->
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 zeros = require( '@stdlib/ndarray/zeros' );
27+
var format = require( '@stdlib/string/format' );
28+
var pkg = require( './../package.json' ).name;
29+
var assign = require( './../lib/assign.js' );
30+
31+
32+
// VARIABLES //
33+
34+
var options = {
35+
'dtype': 'float64'
36+
};
37+
38+
39+
// FUNCTIONS //
40+
41+
/**
42+
* Creates a benchmark function.
43+
*
44+
* @private
45+
* @param {PositiveInteger} len - array length
46+
* @returns {Function} benchmark function
47+
*/
48+
function createBenchmark( len ) {
49+
var x = zeros( [ len ], options );
50+
return benchmark;
51+
52+
/**
53+
* Benchmark function.
54+
*
55+
* @private
56+
* @param {Benchmark} b - benchmark instance
57+
*/
58+
function benchmark( b ) {
59+
var o;
60+
var i;
61+
62+
b.tic();
63+
for ( i = 0; i < b.iterations; i++ ) {
64+
o = assign( x );
65+
if ( typeof o !== 'object' ) {
66+
b.fail( 'should return an ndarray' );
67+
}
68+
}
69+
b.toc();
70+
if ( isnan( x.get( i%len ) ) ) {
71+
b.fail( 'should not return NaN' );
72+
}
73+
b.pass( 'benchmark finished' );
74+
b.end();
75+
}
76+
}
77+
78+
79+
// MAIN //
80+
81+
/**
82+
* Main execution sequence.
83+
*
84+
* @private
85+
*/
86+
function main() {
87+
var len;
88+
var min;
89+
var max;
90+
var f;
91+
var i;
92+
93+
min = 1; // 10^min
94+
max = 6; // 10^max
95+
96+
for ( i = min; i <= max; i++ ) {
97+
len = pow( 10, i );
98+
f = createBenchmark( len );
99+
bench( format( '%s:assign:dtype=%s,len=%d', pkg, options.dtype, len ), f );
100+
}
101+
}
102+
103+
main();
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 format = require( '@stdlib/string/format' );
27+
var pkg = require( './../package.json' ).name;
28+
var oneTo = require( './../lib' );
29+
30+
31+
// VARIABLES //
32+
33+
var options = {
34+
'dtype': 'float64'
35+
};
36+
37+
38+
// FUNCTIONS //
39+
40+
/**
41+
* Creates a benchmark function.
42+
*
43+
* @private
44+
* @param {PositiveInteger} len - array length
45+
* @returns {Function} benchmark function
46+
*/
47+
function createBenchmark( len ) {
48+
return benchmark;
49+
50+
/**
51+
* Benchmark function.
52+
*
53+
* @private
54+
* @param {Benchmark} b - benchmark instance
55+
*/
56+
function benchmark( b ) {
57+
var o;
58+
var i;
59+
60+
b.tic();
61+
for ( i = 0; i < b.iterations; i++ ) {
62+
o = oneTo( [ len ], options );
63+
if ( typeof o !== 'object' ) {
64+
b.fail( 'should return an ndarray' );
65+
}
66+
}
67+
b.toc();
68+
if ( isnan( o.get( i%len ) ) ) {
69+
b.fail( 'should not return NaN' );
70+
}
71+
b.pass( 'benchmark finished' );
72+
b.end();
73+
}
74+
}
75+
76+
77+
// MAIN //
78+
79+
/**
80+
* Main execution sequence.
81+
*
82+
* @private
83+
*/
84+
function main() {
85+
var len;
86+
var min;
87+
var max;
88+
var f;
89+
var i;
90+
91+
min = 1; // 10^min
92+
max = 6; // 10^max
93+
94+
for ( i = min; i <= max; i++ ) {
95+
len = pow( 10, i );
96+
f = createBenchmark( len );
97+
bench( format( '%s:dtype=%s,len=%d', pkg, options.dtype, len ), f );
98+
}
99+
}
100+
101+
main();

0 commit comments

Comments
 (0)