-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat: add blas/ext/zero-to
#11296
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
feat: add blas/ext/zero-to
#11296
Changes from 4 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
aca200b
feat: add blas/ext/zero-to
headlessNode 25c5251
fix: apply suggestions from code review
headlessNode 335515c
Merge branch 'develop' into blas/nd-zeroto
headlessNode 1207752
docs: apply suggestions from code review
headlessNode 0b6502d
docs: document default order
kgryte 95ce2ac
docs: document default option value
kgryte 43b718c
style: fix line wrapping
kgryte fc2bc0e
docs: update description
kgryte a87608d
docs: update description
kgryte e4be223
test: fix tests
kgryte 9393997
test: remove assertion
kgryte c95d422
test: fix tests
kgryte f0cde91
test: clean-up tests
kgryte cc826e7
Apply suggestions from code review
kgryte 18b6617
style: add empty line
kgryte ba8fb2a
Apply suggestions from code review
kgryte 95470c9
docs: fix copy
kgryte ea8d269
docs: remove superfluous note
kgryte File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| <!-- | ||
|
|
||
| @license Apache-2.0 | ||
|
|
||
| Copyright (c) 2026 The Stdlib Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
|
|
||
| --> | ||
|
|
||
| # zeroTo | ||
|
|
||
| > 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. | ||
|
|
||
| <section class="usage"> | ||
|
|
||
| ## Usage | ||
|
|
||
| ```javascript | ||
| var zeroTo = require( '@stdlib/blas/ext/zero-to' ); | ||
| ``` | ||
|
|
||
| #### zeroTo( shape\[, options] ) | ||
|
|
||
| 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. | ||
|
|
||
| ```javascript | ||
| var x = zeroTo( [ 2, 3 ] ); | ||
| // returns <ndarray>[ [ 0.0, 1.0, 2.0 ], [ 0.0, 1.0, 2.0 ] ] | ||
| ``` | ||
|
|
||
| The function has the following parameters: | ||
|
|
||
| - **shape**: array shape. | ||
| - **options**: function options (_optional_). | ||
|
|
||
| The function accepts the following options: | ||
|
|
||
| - **dims**: list of dimensions over which to perform operation. If not provided, the function generates values along the last dimension. Default: `[-1]`. | ||
| - **dtype**: output [ndarray][@stdlib/ndarray/ctor] [data type][@stdlib/ndarray/dtypes]. Must be a numeric or "generic" [data type][@stdlib/ndarray/dtypes]. Default: `'float64'`. | ||
| - **order**: specifies whether an [ndarray][@stdlib/ndarray/ctor] is `'row-major'` (C-style) or `'column-major'` (Fortran-style). | ||
| - **mode**: specifies how to handle indices which exceed array dimensions (see [`ndarray`][@stdlib/ndarray/ctor]). Default: `'throw'`. | ||
| - **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 ]`. | ||
|
kgryte marked this conversation as resolved.
Outdated
|
||
|
|
||
| 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. | ||
|
|
||
| ```javascript | ||
| var x = zeroTo( [ 2, 2 ], { | ||
| 'dims': [ 0, 1 ] | ||
| }); | ||
| // returns <ndarray>[ [ 0.0, 1.0 ], [ 2.0, 3.0 ] ] | ||
| ``` | ||
|
|
||
| To specify the output [ndarray][@stdlib/ndarray/ctor] [data type][@stdlib/ndarray/dtypes], provide a `dtype` option. | ||
|
|
||
| ```javascript | ||
| var x = zeroTo( [ 3 ], { | ||
| 'dtype': 'float32' | ||
| }); | ||
| // returns <ndarray>[ 0.0, 1.0, 2.0 ] | ||
| ``` | ||
|
|
||
| #### zeroTo.assign( x\[, options] ) | ||
|
|
||
| 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. | ||
|
|
||
| ```javascript | ||
| var zeros = require( '@stdlib/ndarray/zeros' ); | ||
|
|
||
| var x = zeros( [ 2, 3 ] ); | ||
| // returns <ndarray>[ [ 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0 ] ] | ||
|
|
||
| var out = zeroTo.assign( x ); | ||
| // returns <ndarray>[ [ 0.0, 1.0, 2.0 ], [ 0.0, 1.0, 2.0 ] ] | ||
|
|
||
| var bool = ( x === out ); | ||
| // returns true | ||
| ``` | ||
|
|
||
| The function has the following parameters: | ||
|
|
||
| - **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have a numeric or "generic" [data type][@stdlib/ndarray/dtypes]. | ||
| - **options**: function options (_optional_). | ||
|
|
||
| The function accepts the following options: | ||
|
|
||
| - **dims**: list of dimensions over which to perform operation. If not provided, the function generates values along the last dimension. Default: `[-1]`. | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.usage --> | ||
|
|
||
| <section class="notes"> | ||
|
|
||
| ## Notes | ||
|
|
||
| - The input [ndarray][@stdlib/ndarray/ctor] is filled **in-place** (i.e., the input [ndarray][@stdlib/ndarray/ctor] is **mutated**). | ||
|
kgryte marked this conversation as resolved.
Outdated
|
||
| - 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. | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.notes --> | ||
|
|
||
| <section class="examples"> | ||
|
|
||
| ## Examples | ||
|
|
||
| <!-- eslint no-undef: "error" --> | ||
|
|
||
| ```javascript | ||
| var ndarray2array = require( '@stdlib/ndarray/to-array' ); | ||
| var zeroTo = require( '@stdlib/blas/ext/zero-to' ); | ||
|
|
||
| // Create a new ndarray: | ||
| var x = zeroTo( [ 5, 5 ], { | ||
| 'dtype': 'generic' | ||
| }); | ||
| console.log( ndarray2array( x ) ); | ||
|
|
||
| // Generate values over a specific dimension: | ||
| x = zeroTo( [ 5, 5 ], { | ||
| 'dtype': 'generic', | ||
| 'dims': [ 0 ] | ||
| }); | ||
| console.log( ndarray2array( x ) ); | ||
|
|
||
| // Generate values over all dimensions: | ||
| x = zeroTo( [ 5, 5 ], { | ||
| 'dtype': 'generic', | ||
| 'dims': [ 0, 1 ] | ||
| }); | ||
| console.log( ndarray2array( x ) ); | ||
| ``` | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.examples --> | ||
|
|
||
| <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> | ||
|
|
||
| <section class="related"> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.related --> | ||
|
|
||
| <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
|
||
| <section class="links"> | ||
|
|
||
| [@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor | ||
|
|
||
| [@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.links --> | ||
103 changes: 103 additions & 0 deletions
103
lib/node_modules/@stdlib/blas/ext/zero-to/benchmark/benchmark.assign.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| /** | ||
| * @license Apache-2.0 | ||
| * | ||
| * Copyright (c) 2026 The Stdlib Authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| 'use strict'; | ||
|
|
||
| // MODULES // | ||
|
|
||
| var bench = require( '@stdlib/bench' ); | ||
| var isnan = require( '@stdlib/math/base/assert/is-nan' ); | ||
| var pow = require( '@stdlib/math/base/special/pow' ); | ||
| var zeros = require( '@stdlib/ndarray/zeros' ); | ||
| var format = require( '@stdlib/string/format' ); | ||
| var pkg = require( './../package.json' ).name; | ||
| var assign = require( './../lib/assign.js' ); | ||
|
|
||
|
|
||
| // VARIABLES // | ||
|
|
||
| var options = { | ||
| 'dtype': 'float64' | ||
| }; | ||
|
|
||
|
|
||
| // FUNCTIONS // | ||
|
|
||
| /** | ||
| * Creates a benchmark function. | ||
| * | ||
| * @private | ||
| * @param {PositiveInteger} len - array length | ||
| * @returns {Function} benchmark function | ||
| */ | ||
| function createBenchmark( len ) { | ||
| var x = zeros( [ len ], options ); | ||
| return benchmark; | ||
|
|
||
| /** | ||
| * Benchmark function. | ||
| * | ||
| * @private | ||
| * @param {Benchmark} b - benchmark instance | ||
| */ | ||
| function benchmark( b ) { | ||
| var o; | ||
| var i; | ||
|
|
||
| b.tic(); | ||
| for ( i = 0; i < b.iterations; i++ ) { | ||
| o = assign( x ); | ||
| if ( typeof o !== 'object' ) { | ||
| b.fail( 'should return an ndarray' ); | ||
| } | ||
| } | ||
| b.toc(); | ||
| if ( isnan( x.get( i%len ) ) ) { | ||
| b.fail( 'should not return NaN' ); | ||
| } | ||
| b.pass( 'benchmark finished' ); | ||
| b.end(); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| // MAIN // | ||
|
|
||
| /** | ||
| * Main execution sequence. | ||
| * | ||
| * @private | ||
| */ | ||
| function main() { | ||
| var len; | ||
| var min; | ||
| var max; | ||
| var f; | ||
| var i; | ||
|
|
||
| min = 1; // 10^min | ||
| max = 6; // 10^max | ||
|
|
||
| for ( i = min; i <= max; i++ ) { | ||
| len = pow( 10, i ); | ||
| f = createBenchmark( len ); | ||
| bench( format( '%s:assign:dtype=%s,len=%d', pkg, options.dtype, len ), f ); | ||
| } | ||
| } | ||
|
|
||
| main(); |
101 changes: 101 additions & 0 deletions
101
lib/node_modules/@stdlib/blas/ext/zero-to/benchmark/benchmark.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| /** | ||
| * @license Apache-2.0 | ||
| * | ||
| * Copyright (c) 2026 The Stdlib Authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| 'use strict'; | ||
|
|
||
| // MODULES // | ||
|
|
||
| var bench = require( '@stdlib/bench' ); | ||
| var isnan = require( '@stdlib/math/base/assert/is-nan' ); | ||
| var pow = require( '@stdlib/math/base/special/pow' ); | ||
| var format = require( '@stdlib/string/format' ); | ||
| var pkg = require( './../package.json' ).name; | ||
| var zeroTo = require( './../lib' ); | ||
|
|
||
|
|
||
| // VARIABLES // | ||
|
|
||
| var options = { | ||
| 'dtype': 'float64' | ||
| }; | ||
|
|
||
|
|
||
| // FUNCTIONS // | ||
|
|
||
| /** | ||
| * Creates a benchmark function. | ||
| * | ||
| * @private | ||
| * @param {PositiveInteger} len - array length | ||
| * @returns {Function} benchmark function | ||
| */ | ||
| function createBenchmark( len ) { | ||
| return benchmark; | ||
|
|
||
| /** | ||
| * Benchmark function. | ||
| * | ||
| * @private | ||
| * @param {Benchmark} b - benchmark instance | ||
| */ | ||
| function benchmark( b ) { | ||
| var o; | ||
| var i; | ||
|
|
||
| b.tic(); | ||
| for ( i = 0; i < b.iterations; i++ ) { | ||
| o = zeroTo( [ len ], options ); | ||
| if ( typeof o !== 'object' ) { | ||
| b.fail( 'should return an ndarray' ); | ||
| } | ||
| } | ||
| b.toc(); | ||
| if ( isnan( o.get( i%len ) ) ) { | ||
| b.fail( 'should not return NaN' ); | ||
| } | ||
| b.pass( 'benchmark finished' ); | ||
| b.end(); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| // MAIN // | ||
|
|
||
| /** | ||
| * Main execution sequence. | ||
| * | ||
| * @private | ||
| */ | ||
| function main() { | ||
| var len; | ||
| var min; | ||
| var max; | ||
| var f; | ||
| var i; | ||
|
|
||
| min = 1; // 10^min | ||
| max = 6; // 10^max | ||
|
|
||
| for ( i = min; i <= max; i++ ) { | ||
| len = pow( 10, i ); | ||
| f = createBenchmark( len ); | ||
| bench( format( '%s:dtype=%s,len=%d', pkg, options.dtype, len ), f ); | ||
| } | ||
| } | ||
|
|
||
| main(); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.