Skip to content

Commit 25c5251

Browse files
committed
fix: apply suggestions from code review
--- 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 aca200b commit 25c5251

17 files changed

Lines changed: 1779 additions & 624 deletions

File tree

lib/node_modules/@stdlib/blas/ext/zero-to/README.md

Lines changed: 76 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ limitations under the License.
2020

2121
# zeroTo
2222

23-
> Fill an [ndarray][@stdlib/ndarray/ctor] with linearly spaced numeric elements which increment by `1` starting from zero.
23+
> Return a new [ndarray][@stdlib/ndarray/ctor] filled with linearly spaced numeric elements which increment by `1` starting from zero along one or more [ndarray][@stdlib/ndarray/ctor] dimensions.
2424
2525
<section class="usage">
2626

@@ -30,49 +30,91 @@ limitations under the License.
3030
var zeroTo = require( '@stdlib/blas/ext/zero-to' );
3131
```
3232

33-
#### zeroTo( x\[, options] )
33+
#### zeroTo( shape\[, options] )
3434

35-
Fills an [ndarray][@stdlib/ndarray/ctor] with linearly spaced numeric elements which increment by `1` starting from zero.
35+
Returns a new [ndarray][@stdlib/ndarray/ctor] filled with linearly spaced numeric elements which increment by `1` starting from zero along one or more [ndarray][@stdlib/ndarray/ctor] dimensions.
3636

3737
```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 ]
38+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
4239

43-
var y = zeroTo( x );
44-
// returns <ndarray>[ 0.0, 1.0, 2.0 ]
40+
var x = zeroTo( [ 3 ] );
41+
// returns <ndarray>
4542

46-
var bool = ( x === y );
47-
// returns true
43+
var arr = ndarray2array( x );
44+
// returns [ 0.0, 1.0, 2.0 ]
4845
```
4946

5047
The function has the following parameters:
5148

52-
- **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have a numeric or "generic" [data type][@stdlib/ndarray/dtypes].
49+
- **shape**: array shape.
5350
- **options**: function options (_optional_).
5451

5552
The function accepts the following options:
5653

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].
54+
- **dims**: list of dimensions over which to perform operation. If not provided, the function generates values along the last dimension. Default: `[-1]`.
55+
- **dtype**: output [ndarray][@stdlib/ndarray/ctor] [data type][@stdlib/ndarray/dtypes]. Must be a numeric or "generic" [data type][@stdlib/ndarray/dtypes]. Default: `'float64'`.
56+
- **order**: specifies whether an [ndarray][@stdlib/ndarray/ctor] is `'row-major'` (C-style) or `'column-major'` (Fortran-style).
57+
- **mode**: specifies how to handle indices which exceed array dimensions (see [`ndarray`][@stdlib/ndarray/ctor]). Default: `'throw'`.
58+
- **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 constructor recycles modes using modulo arithmetic. Default: `[ options.mode ]`.
5859

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+
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.
6061

6162
```javascript
62-
var array = require( '@stdlib/ndarray/array' );
63+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
6364

64-
var x = array( [ 0.0, 0.0, 0.0, 0.0 ], {
65-
'shape': [ 2, 2 ],
66-
'order': 'row-major'
65+
var x = zeroTo( [ 2, 2 ], {
66+
'dims': [ 0, 1 ]
6767
});
68-
// returns <ndarray>[ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ]
68+
// returns <ndarray>
69+
70+
var arr = ndarray2array( x );
71+
// returns [ [ 0.0, 1.0 ], [ 2.0, 3.0 ] ]
72+
```
73+
74+
To specify the output [ndarray][@stdlib/ndarray/ctor] [data type][@stdlib/ndarray/dtypes], provide a `dtype` option.
75+
76+
```javascript
77+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
6978

70-
var y = zeroTo( x, {
71-
'dims': [ 1 ]
79+
var x = zeroTo( [ 3 ], {
80+
'dtype': 'float32'
7281
});
73-
// returns <ndarray>[ [ 0.0, 1.0 ], [ 0.0, 1.0 ] ]
82+
// returns <ndarray>
83+
84+
var arr = ndarray2array( x );
85+
// returns [ 0.0, 1.0, 2.0 ]
7486
```
7587

88+
#### zeroTo.assign( x\[, options] )
89+
90+
Fills an [ndarray][@stdlib/ndarray/ctor] with linearly spaced numeric elements which increment by `1` starting from zero along one or more [ndarray][@stdlib/ndarray/ctor] dimensions.
91+
92+
```javascript
93+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
94+
var zeros = require( '@stdlib/ndarray/zeros' );
95+
96+
var x = zeros( [ 3 ] );
97+
// returns <ndarray>
98+
99+
var out = zeroTo.assign( x );
100+
// returns <ndarray>
101+
102+
var bool = ( x === out );
103+
// returns true
104+
105+
var arr = ndarray2array( out );
106+
// returns [ 0.0, 1.0, 2.0 ]
107+
```
108+
109+
The function has the following parameters:
110+
111+
- **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have a numeric or "generic" [data type][@stdlib/ndarray/dtypes].
112+
- **options**: function options (_optional_).
113+
114+
The function accepts the following options:
115+
116+
- **dims**: list of dimensions over which to perform operation. If not provided, the function generates values along the last dimension. Default: `[-1]`.
117+
76118
</section>
77119

78120
<!-- /.usage -->
@@ -96,21 +138,26 @@ var y = zeroTo( x, {
96138

97139
```javascript
98140
var ndarray2array = require( '@stdlib/ndarray/to-array' );
99-
var zeros = require( '@stdlib/ndarray/zeros' );
100141
var zeroTo = require( '@stdlib/blas/ext/zero-to' );
101142

102-
// Create a zeros-filled ndarray:
103-
var x = zeros( [ 5, 5 ], {
143+
// Create a new ndarray:
144+
var x = zeroTo( [ 5, 5 ], {
104145
'dtype': 'generic'
105146
});
106147
console.log( ndarray2array( x ) );
107148

108-
// Perform operation:
109-
zeroTo( x, {
110-
'dims': [ 1 ]
149+
// Generate values over a specific dimension:
150+
x = zeroTo( [ 5, 5 ], {
151+
'dtype': 'generic',
152+
'dims': [ 0 ]
111153
});
154+
console.log( ndarray2array( x ) );
112155

113-
// Print the results:
156+
// Generate values over all dimensions:
157+
x = zeroTo( [ 5, 5 ], {
158+
'dtype': 'generic',
159+
'dims': [ 0, 1 ]
160+
});
114161
console.log( ndarray2array( x ) );
115162
```
116163

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();

lib/node_modules/@stdlib/blas/ext/zero-to/benchmark/benchmark.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
var bench = require( '@stdlib/bench' );
2424
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2525
var pow = require( '@stdlib/math/base/special/pow' );
26-
var zeros = require( '@stdlib/array/zeros' );
27-
var ndarray = require( '@stdlib/ndarray/base/ctor' );
2826
var format = require( '@stdlib/string/format' );
2927
var pkg = require( './../package.json' ).name;
3028
var zeroTo = require( './../lib' );
@@ -47,9 +45,6 @@ var options = {
4745
* @returns {Function} benchmark function
4846
*/
4947
function createBenchmark( len ) {
50-
var x = zeros( len, options.dtype );
51-
x = new ndarray( options.dtype, x, [ len ], [ 1 ], 0, 'row-major' );
52-
5348
return benchmark;
5449

5550
/**
@@ -64,7 +59,7 @@ function createBenchmark( len ) {
6459

6560
b.tic();
6661
for ( i = 0; i < b.iterations; i++ ) {
67-
o = zeroTo( x );
62+
o = zeroTo( [ len ], options );
6863
if ( typeof o !== 'object' ) {
6964
b.fail( 'should return an ndarray' );
7065
}
Lines changed: 69 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,66 @@
11

2-
{{alias}}( x[, options] )
2+
{{alias}}( shape[, options] )
3+
Returns a new ndarray filled with linearly spaced numeric elements which
4+
increment by 1 starting from zero along one or more ndarray dimensions.
5+
6+
Parameters
7+
----------
8+
shape: Array<integer>|integer
9+
Array shape.
10+
11+
options: Object (optional)
12+
Function options.
13+
14+
options.dims: Array<integer> (optional)
15+
List of dimensions over which to perform operation. If not provided,
16+
the function generates values along the last dimension. Default: [-1].
17+
18+
options.dtype: string (optional)
19+
Output ndarray data type. Must be a numeric or "generic" data type.
20+
Default: 'float64'.
21+
22+
options.order: string (optional)
23+
Specifies whether an array is row-major (C-style) or column-major
24+
(Fortran-style).
25+
26+
options.mode: string (optional)
27+
Specifies how to handle indices which exceed array dimensions. If equal
28+
to 'throw', an ndarray instance throws an error when an index exceeds
29+
array dimensions. If equal to 'normalize', an ndarray instance
30+
normalizes negative indices and throws an error when an index exceeds
31+
array dimensions. If equal to 'wrap', an ndarray instance wraps around
32+
indices exceeding array dimensions using modulo arithmetic. If equal to
33+
'clamp', an ndarray instance sets an index exceeding array dimensions
34+
to either `0` (minimum index) or the maximum index. Default: 'throw'.
35+
36+
options.submode: Array<string> (optional)
37+
Specifies how to handle subscripts which exceed array dimensions. If a
38+
mode for a corresponding dimension is equal to 'throw', an ndarray
39+
instance throws an error when a subscript exceeds array dimensions. If
40+
equal to 'normalize', an ndarray instance normalizes negative
41+
subscripts and throws an error when a subscript exceeds array
42+
dimensions. If equal to 'wrap', an ndarray instance wraps around
43+
subscripts exceeding array dimensions using modulo arithmetic. If equal
44+
to 'clamp', an ndarray instance sets a subscript exceeding array
45+
dimensions to either `0` (minimum index) or the maximum index. If the
46+
number of modes is fewer than the number of dimensions, the function
47+
recycles modes using modulo arithmetic. Default: [ options.mode ].
48+
49+
Returns
50+
-------
51+
out: ndarray
52+
Output array.
53+
54+
Examples
55+
--------
56+
> var out = {{alias}}( [ 3 ] );
57+
> {{alias:@stdlib/ndarray/to-array}}( out )
58+
[ 0.0, 1.0, 2.0 ]
59+
60+
61+
{{alias}}.assign( x[, options] )
362
Fills an ndarray with linearly spaced numeric elements which increment by
4-
1 starting from zero.
63+
1 starting from zero along one or more ndarray dimensions.
564

665
The function fills an ndarray in-place and thus mutates the input ndarray.
766

@@ -15,8 +74,8 @@
1574

1675
options.dims: Array<integer> (optional)
1776
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.
77+
the function generates values along the last dimension.
78+
Default: [-1].
2079

2180
Returns
2281
-------
@@ -25,9 +84,12 @@
2584

2685
Examples
2786
--------
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 ]
87+
> var x = {{alias:@stdlib/ndarray/zeros}}( [ 3 ] );
88+
> var out = {{alias}}.assign( x );
89+
> {{alias:@stdlib/ndarray/to-array}}( out )
90+
[ 0.0, 1.0, 2.0 ]
91+
> var bool = ( out === x )
92+
true
3193

3294
See Also
3395
--------

0 commit comments

Comments
 (0)