Skip to content

Commit aca200b

Browse files
committed
feat: add blas/ext/zero-to
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent d479058 commit aca200b

File tree

10 files changed

+1207
-0
lines changed

10 files changed

+1207
-0
lines changed
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
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+
# zeroTo
22+
23+
> Fill an [ndarray][@stdlib/ndarray/ctor] with linearly spaced numeric elements which increment by `1` starting from zero.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var zeroTo = require( '@stdlib/blas/ext/zero-to' );
31+
```
32+
33+
#### zeroTo( x\[, options] )
34+
35+
Fills an [ndarray][@stdlib/ndarray/ctor] with linearly spaced numeric elements which increment by `1` starting from zero.
36+
37+
```javascript
38+
var array = require( '@stdlib/ndarray/array' );
39+
40+
var x = array( [ 0.0, 0.0, 0.0 ] );
41+
// returns <ndarray>[ 0.0, 0.0, 0.0 ]
42+
43+
var y = zeroTo( x );
44+
// returns <ndarray>[ 0.0, 1.0, 2.0 ]
45+
46+
var bool = ( x === y );
47+
// returns true
48+
```
49+
50+
The function has the following parameters:
51+
52+
- **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have a numeric or "generic" [data type][@stdlib/ndarray/dtypes].
53+
- **options**: function options (_optional_).
54+
55+
The function accepts the following options:
56+
57+
- **dims**: list of dimensions over which to perform operation. If not provided, the function performs the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor].
58+
59+
By default, the function performs the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor]. To perform the operation over specific dimensions, provide a `dims` option.
60+
61+
```javascript
62+
var array = require( '@stdlib/ndarray/array' );
63+
64+
var x = array( [ 0.0, 0.0, 0.0, 0.0 ], {
65+
'shape': [ 2, 2 ],
66+
'order': 'row-major'
67+
});
68+
// returns <ndarray>[ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ]
69+
70+
var y = zeroTo( x, {
71+
'dims': [ 1 ]
72+
});
73+
// returns <ndarray>[ [ 0.0, 1.0 ], [ 0.0, 1.0 ] ]
74+
```
75+
76+
</section>
77+
78+
<!-- /.usage -->
79+
80+
<section class="notes">
81+
82+
## Notes
83+
84+
- The input [ndarray][@stdlib/ndarray/ctor] is filled **in-place** (i.e., the input [ndarray][@stdlib/ndarray/ctor] is **mutated**).
85+
- 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.
86+
87+
</section>
88+
89+
<!-- /.notes -->
90+
91+
<section class="examples">
92+
93+
## Examples
94+
95+
<!-- eslint no-undef: "error" -->
96+
97+
```javascript
98+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
99+
var zeros = require( '@stdlib/ndarray/zeros' );
100+
var zeroTo = require( '@stdlib/blas/ext/zero-to' );
101+
102+
// Create a zeros-filled ndarray:
103+
var x = zeros( [ 5, 5 ], {
104+
'dtype': 'generic'
105+
});
106+
console.log( ndarray2array( x ) );
107+
108+
// Perform operation:
109+
zeroTo( x, {
110+
'dims': [ 1 ]
111+
});
112+
113+
// Print the results:
114+
console.log( ndarray2array( x ) );
115+
```
116+
117+
</section>
118+
119+
<!-- /.examples -->
120+
121+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
122+
123+
<section class="related">
124+
125+
</section>
126+
127+
<!-- /.related -->
128+
129+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
130+
131+
<section class="links">
132+
133+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
134+
135+
[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes
136+
137+
</section>
138+
139+
<!-- /.links -->
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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/array/zeros' );
27+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
28+
var format = require( '@stdlib/string/format' );
29+
var pkg = require( './../package.json' ).name;
30+
var zeroTo = require( './../lib' );
31+
32+
33+
// VARIABLES //
34+
35+
var options = {
36+
'dtype': 'float64'
37+
};
38+
39+
40+
// FUNCTIONS //
41+
42+
/**
43+
* Creates a benchmark function.
44+
*
45+
* @private
46+
* @param {PositiveInteger} len - array length
47+
* @returns {Function} benchmark function
48+
*/
49+
function createBenchmark( len ) {
50+
var x = zeros( len, options.dtype );
51+
x = new ndarray( options.dtype, x, [ len ], [ 1 ], 0, 'row-major' );
52+
53+
return benchmark;
54+
55+
/**
56+
* Benchmark function.
57+
*
58+
* @private
59+
* @param {Benchmark} b - benchmark instance
60+
*/
61+
function benchmark( b ) {
62+
var o;
63+
var i;
64+
65+
b.tic();
66+
for ( i = 0; i < b.iterations; i++ ) {
67+
o = zeroTo( x );
68+
if ( typeof o !== 'object' ) {
69+
b.fail( 'should return an ndarray' );
70+
}
71+
}
72+
b.toc();
73+
if ( isnan( o.get( i%len ) ) ) {
74+
b.fail( 'should not return NaN' );
75+
}
76+
b.pass( 'benchmark finished' );
77+
b.end();
78+
}
79+
}
80+
81+
82+
// MAIN //
83+
84+
/**
85+
* Main execution sequence.
86+
*
87+
* @private
88+
*/
89+
function main() {
90+
var len;
91+
var min;
92+
var max;
93+
var f;
94+
var i;
95+
96+
min = 1; // 10^min
97+
max = 6; // 10^max
98+
99+
for ( i = min; i <= max; i++ ) {
100+
len = pow( 10, i );
101+
f = createBenchmark( len );
102+
bench( format( '%s:dtype=%s,len=%d', pkg, options.dtype, len ), f );
103+
}
104+
}
105+
106+
main();
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
{{alias}}( x[, options] )
3+
Fills an ndarray with linearly spaced numeric elements which increment by
4+
1 starting from zero.
5+
6+
The function fills an ndarray in-place and thus mutates the input ndarray.
7+
8+
Parameters
9+
----------
10+
x: ndarray
11+
Input array. Must have a numeric or "generic" data type.
12+
13+
options: Object (optional)
14+
Function options.
15+
16+
options.dims: Array<integer> (optional)
17+
List of dimensions over which to perform operation. If not provided,
18+
the function performs the operation over all elements in a provided
19+
input ndarray.
20+
21+
Returns
22+
-------
23+
out: ndarray
24+
Input array.
25+
26+
Examples
27+
--------
28+
> var x = {{alias:@stdlib/ndarray/array}}( [ 0.0, 0.0, 0.0 ] );
29+
> var y = {{alias}}( x )
30+
<ndarray>[ 0.0, 1.0, 2.0 ]
31+
32+
See Also
33+
--------
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
// TypeScript Version: 4.1
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
import { ArrayLike } from '@stdlib/types/array';
24+
import { typedndarray, genericndarray } from '@stdlib/types/ndarray';
25+
26+
/**
27+
* Input array.
28+
*/
29+
type InputArray = typedndarray<number> | genericndarray<number>;
30+
31+
/**
32+
* Interface defining options.
33+
*/
34+
interface Options {
35+
/**
36+
* List of dimensions over which to perform operation.
37+
*/
38+
dims?: ArrayLike<number>;
39+
}
40+
41+
/**
42+
* Fills an ndarray with linearly spaced numeric elements which increment by 1 starting from zero.
43+
*
44+
* ## Notes
45+
*
46+
* - The input ndarray is filled **in-place** (i.e., the input ndarray is **mutated**).
47+
*
48+
* @param x - input ndarray
49+
* @param options - function options
50+
* @returns output ndarray
51+
*
52+
* @example
53+
* var array = require( '@stdlib/ndarray/array' );
54+
*
55+
* var x = array( [ 0.0, 0.0, 0.0 ] );
56+
* // returns <ndarray>[ 0.0, 0.0, 0.0 ]
57+
*
58+
* var y = zeroTo( x );
59+
* // returns <ndarray>[ 0.0, 1.0, 2.0 ]
60+
*/
61+
type ZeroTo = <T extends InputArray = InputArray>( x: T, options?: Options ) => T;
62+
63+
/**
64+
* Fills an ndarray with linearly spaced numeric elements which increment by 1 starting from zero.
65+
*
66+
* ## Notes
67+
*
68+
* - The input ndarray is filled **in-place** (i.e., the input ndarray is **mutated**).
69+
*
70+
* @param x - input ndarray
71+
* @param options - function options
72+
* @returns output ndarray
73+
*
74+
* @example
75+
* var array = require( '@stdlib/ndarray/array' );
76+
*
77+
* var x = array( [ 0.0, 0.0, 0.0 ] );
78+
* // returns <ndarray>[ 0.0, 0.0, 0.0 ]
79+
*
80+
* var y = zeroTo( x );
81+
* // returns <ndarray>[ 0.0, 1.0, 2.0 ]
82+
*/
83+
declare const zeroTo: ZeroTo;
84+
85+
86+
// EXPORTS //
87+
88+
export = zeroTo;

0 commit comments

Comments
 (0)