-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat: add ndarray/base/atleast1d
#11540
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
Changes from all commits
Commits
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
130 changes: 130 additions & 0 deletions
130
lib/node_modules/@stdlib/ndarray/base/atleast1d/README.md
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,130 @@ | ||
| <!-- | ||
|
|
||
| @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. | ||
|
|
||
| --> | ||
|
|
||
| # atleast1d | ||
|
|
||
| > Convert a list of values (scalars and/or ndarrays) to ndarrays having at least one dimension. | ||
|
|
||
| <section class="intro"> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.intro --> | ||
|
|
||
| <section class="usage"> | ||
|
|
||
| ## Usage | ||
|
|
||
| ```javascript | ||
| var atleast1d = require( '@stdlib/ndarray/base/atleast1d' ); | ||
| ``` | ||
|
|
||
| #### atleast1d( arrays ) | ||
|
|
||
| Converts a list of values (scalars and/or ndarrays) to ndarrays having at least one dimension. | ||
|
|
||
| ```javascript | ||
| var array = require( '@stdlib/ndarray/array' ); | ||
| var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); | ||
|
|
||
| var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ] ] ); | ||
| // returns <ndarray>[ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ] ] | ||
|
|
||
| var y = scalar2ndarray( 3.14 ); | ||
| // returns <ndarray>[ 3.14 ] | ||
|
|
||
| var out = atleast1d( [ x, y ] ); | ||
| // returns [ <ndarray>, <ndarray> ] | ||
| ``` | ||
|
|
||
| The function accepts the following arguments: | ||
|
|
||
| - **arrays**: array-like object containing a list of scalars and/or ndarrays. | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.usage --> | ||
|
|
||
| <section class="notes"> | ||
|
|
||
| ## Notes | ||
|
|
||
| - If a provided ndarray has fewer than one dimension, the returned ndarray is a view on the input ndarray data buffer. The view is typically **not** contiguous. As more than one element of a returned view may refer to the same memory location, writing to the view may affect multiple elements. If you need to write to the returned ndarray, copy the ndarray **before** performing operations which may mutate elements. | ||
|
|
||
| - The returned ndarray is a "base" [ndarray][@stdlib/ndarray/base/ctor], and, thus, the returned [ndarray][@stdlib/ndarray/base/ctor] does not perform bounds checking or afford any of the guarantees of the non-base [ndarray][@stdlib/ndarray/ctor] constructor. The primary intent of this function is to broadcast an ndarray-like object within internal implementations and to do so with minimal overhead. | ||
|
|
||
| - If provided a scalar value (i.e., a non-ndarray) and that value | ||
|
|
||
| - is a number, the returned ndarray will have the [default][@stdlib/ndarray/defaults] real-valued floating-point data type. | ||
| - is a boolean, the returned ndarray will have the [default][@stdlib/ndarray/defaults] boolean data type. | ||
| - is a complex number object of a known data type, the data type of the returned ndarray will be the same as the provided value. | ||
| - is a complex number object of an unknown data type, the returned ndarray will have the [default][@stdlib/ndarray/defaults] complex-valued floating-point data type. | ||
| - is any other value type, the returned ndarray will have a `'generic'` data type. | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.notes --> | ||
|
|
||
| <section class="examples"> | ||
|
|
||
| ## Examples | ||
|
|
||
| <!-- eslint no-undef: "error" --> | ||
|
|
||
| ```javascript | ||
| var discreteUniform = require( '@stdlib/random/discrete-uniform' ); | ||
| var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); | ||
| var ndarray2array = require( '@stdlib/ndarray/to-array' ); | ||
| var atleast1d = require( '@stdlib/ndarray/base/atleast1d' ); | ||
|
|
||
| var x = discreteUniform( [ 2, 2, 2 ], -100, 100 ); | ||
| console.log( ndarray2array( x ) ); | ||
|
|
||
| var y = scalar2ndarray( 3.14 ); | ||
| console.log( ndarray2array( y ) ); | ||
|
|
||
| var out = atleast1d( [ x, y ] ); | ||
| console.log( ndarray2array( out[ 0 ] ) ); | ||
| console.log( ndarray2array( out[ 1 ] ) ); | ||
| ``` | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.examples --> | ||
|
|
||
| <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> | ||
|
|
||
| <section class="related"> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.related --> | ||
|
|
||
| <section class="links"> | ||
|
|
||
| [@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor | ||
|
|
||
| [@stdlib/ndarray/base/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/ctor | ||
|
|
||
| [@stdlib/ndarray/defaults]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/defaults | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.links --> | ||
103 changes: 103 additions & 0 deletions
103
lib/node_modules/@stdlib/ndarray/base/atleast1d/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,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 Float64Array = require( '@stdlib/array/float64' ); | ||
| var ndarrayBase = require( '@stdlib/ndarray/base/ctor' ); | ||
| var ndarray = require( '@stdlib/ndarray/ctor' ); | ||
| var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); | ||
| var format = require( '@stdlib/string/format' ); | ||
| var pkg = require( './../package.json' ).name; | ||
| var atleast1d = require( './../lib' ); | ||
|
|
||
|
|
||
| // MAIN // | ||
|
|
||
| bench( format( '%s::base_ndarray', pkg ), function benchmark( b ) { | ||
| var values; | ||
| var buffer; | ||
| var offset; | ||
| var dtype; | ||
| var order; | ||
| var out; | ||
| var i; | ||
|
|
||
| dtype = 'float64'; | ||
| buffer = new Float64Array( 12 ); | ||
| offset = 0; | ||
| order = 'row-major'; | ||
|
|
||
| values = [ | ||
| ndarrayBase( dtype, buffer, [ 3, 2, 2 ], [ 4, 2, 1 ], offset, order ), | ||
| ndarrayBase( dtype, buffer, [ 4, 3 ], [ 3, 1 ], offset, order ), | ||
| ndarrayBase( dtype, buffer, [], [ 0 ], offset, order ) | ||
| ]; | ||
|
|
||
| b.tic(); | ||
| for ( i = 0; i < b.iterations; i++ ) { | ||
| out = atleast1d( [ 10.0, values[ i%values.length ] ] ); | ||
| if ( typeof out !== 'object' ) { | ||
| b.fail( 'should return an object' ); | ||
| } | ||
| } | ||
| b.toc(); | ||
| if ( !isndarrayLike( out[ 0 ] ) || !isndarrayLike( out[ 1 ] ) ) { | ||
| b.fail( 'should return an array of ndarrays' ); | ||
| } | ||
| b.pass( 'benchmark finished' ); | ||
| b.end(); | ||
| }); | ||
|
|
||
| bench( format( '%s::ndarray', pkg ), function benchmark( b ) { | ||
| var values; | ||
| var buffer; | ||
| var offset; | ||
| var dtype; | ||
| var order; | ||
| var out; | ||
| var i; | ||
|
|
||
| dtype = 'float64'; | ||
| buffer = new Float64Array( 12 ); | ||
| offset = 0; | ||
| order = 'row-major'; | ||
|
|
||
| values = [ | ||
| ndarray( dtype, buffer, [ 3, 2, 2 ], [ 4, 2, 1 ], offset, order ), | ||
| ndarray( dtype, buffer, [ 4, 3 ], [ 3, 1 ], offset, order ), | ||
| ndarray( dtype, buffer, [], [ 0 ], offset, order ) | ||
| ]; | ||
|
|
||
| b.tic(); | ||
| for ( i = 0; i < b.iterations; i++ ) { | ||
| out = atleast1d( [ 10.0, values[ i%values.length ] ] ); | ||
| if ( typeof out !== 'object' ) { | ||
| b.fail( 'should return an object' ); | ||
| } | ||
| } | ||
| b.toc(); | ||
| if ( !isndarrayLike( out[ 0 ] ) || !isndarrayLike( out[ 1 ] ) ) { | ||
| b.fail( 'should return an array of ndarrays' ); | ||
| } | ||
| b.pass( 'benchmark finished' ); | ||
| b.end(); | ||
| }); |
26 changes: 26 additions & 0 deletions
26
lib/node_modules/@stdlib/ndarray/base/atleast1d/docs/repl.txt
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,26 @@ | ||
|
|
||
| {{alias}}( arrays ) | ||
| Converts a list of values (scalars and/or ndarrays) to ndarrays having at | ||
| least one dimension. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| arrays: ArrayLikeObject | ||
| List of scalars and/or ndarrays. | ||
|
|
||
| Returns | ||
| ------- | ||
| out: Array<ndarray> | ||
| List of ndarrays. | ||
|
|
||
| Examples | ||
| -------- | ||
| > var x = {{alias:@stdlib/ndarray/array}}( [ [ [ 1, 2 ] ], [ [ 3, 4 ] ] ] ) | ||
| <ndarray>[ [ [ 1, 2 ] ], [ [ 3, 4 ] ] ] | ||
| > var y = {{alias:@stdlib/ndarray/from-scalar}}( 3.14 ) | ||
| <ndarray>[ 3.14 ] | ||
| > var out = {{alias}}( [ x, y ] ) | ||
| [ <ndarray>, <ndarray> ] | ||
|
|
||
| See Also | ||
| -------- |
50 changes: 50 additions & 0 deletions
50
lib/node_modules/@stdlib/ndarray/base/atleast1d/docs/types/index.d.ts
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,50 @@ | ||
| /* | ||
| * @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. | ||
| */ | ||
|
|
||
| // TypeScript Version: 4.1 | ||
|
|
||
| /// <reference types="@stdlib/types"/> | ||
|
|
||
| import { ArrayLike } from '@stdlib/types/array'; | ||
| import { ndarray } from '@stdlib/types/ndarray'; | ||
|
|
||
| /** | ||
| * Converts a list of values (scalars and/or ndarrays) to ndarrays having at least one dimension. | ||
| * | ||
| * @param arrays - array-like object containing a list of scalars and/or ndarrays | ||
| * @returns an array of ndarrays | ||
| * | ||
| * @example | ||
| * var array = require( '@stdlib/ndarray/array' ); | ||
| * var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); | ||
| * | ||
| * var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ] ] ); | ||
| * // returns <ndarray>[ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ] ] | ||
| * | ||
| * var y = scalar2ndarray( 3.14 ); | ||
| * // returns <ndarray>[ 3.14 ] | ||
| * | ||
| * var out = atleast1d( [ x, y ] ); | ||
| * // returns [ <ndarray>, <ndarray> ] | ||
| */ | ||
| declare function atleast1d( arrays: ArrayLike<any> ): Array<ndarray>; | ||
|
|
||
|
|
||
| // EXPORTS // | ||
|
|
||
| export = atleast1d; | ||
52 changes: 52 additions & 0 deletions
52
lib/node_modules/@stdlib/ndarray/base/atleast1d/docs/types/test.ts
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,52 @@ | ||
| /* | ||
| * @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. | ||
| */ | ||
|
|
||
| import zeros = require( '@stdlib/ndarray/zeros' ); | ||
| import atleast1d = require( './index' ); | ||
|
|
||
|
|
||
| // TESTS // | ||
|
|
||
| // The function returns an array of ndarrays... | ||
| { | ||
| const x = zeros( [ 2, 2 ] ); | ||
| const y = zeros( [ 2, 2, 2 ] ); | ||
|
|
||
| atleast1d( [ x ] ); // $ExpectType ndarray[] | ||
| atleast1d( [ x, y ] ); // $ExpectType ndarray[] | ||
| atleast1d( [ x, y, x ] ); // $ExpectType ndarray[] | ||
| } | ||
|
|
||
| // The compiler throws an error if the function is provided a first argument which is not an array... | ||
| { | ||
| atleast1d( 5 ); // $ExpectError | ||
| atleast1d( true ); // $ExpectError | ||
| atleast1d( false ); // $ExpectError | ||
| atleast1d( null ); // $ExpectError | ||
| atleast1d( {} ); // $ExpectError | ||
| atleast1d( ( x: number ): number => x ); // $ExpectError | ||
| } | ||
|
|
||
| // The compiler throws an error if the function is provided an unsupported number of arguments... | ||
| { | ||
| const x = zeros( [ 2, 2 ] ); | ||
| const y = zeros( [ 2, 2, 2 ] ); | ||
|
|
||
| atleast1d(); // $ExpectError | ||
| atleast1d( [ x, y ], {} ); // $ExpectError | ||
| } |
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.