|
| 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 | +# atleastnd |
| 22 | + |
| 23 | +> Convert a list of values (scalars and/or ndarrays) to ndarrays having at least a specified number of dimensions. |
| 24 | +
|
| 25 | +<section class="intro"> |
| 26 | + |
| 27 | +</section> |
| 28 | + |
| 29 | +<!-- /.intro --> |
| 30 | + |
| 31 | +<section class="usage"> |
| 32 | + |
| 33 | +## Usage |
| 34 | + |
| 35 | +```javascript |
| 36 | +var atleastnd = require( '@stdlib/ndarray/base/atleastnd' ); |
| 37 | +``` |
| 38 | + |
| 39 | +#### atleastnd( ndims, arrays ) |
| 40 | + |
| 41 | +Converts a list of values (scalars and/or ndarrays) to ndarrays having at least a specified number of dimensions. |
| 42 | + |
| 43 | +```javascript |
| 44 | +var array = require( '@stdlib/ndarray/array' ); |
| 45 | + |
| 46 | +var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ] ] ); |
| 47 | +// returns <ndarray>[ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ] ] |
| 48 | + |
| 49 | +var y = array( [ [ 5.0, 6.0 ], [ 7.0, 8.0 ] ] ); |
| 50 | +// returns <ndarray>[ [ 5.0, 6.0 ], [ 7.0, 8.0 ] ] |
| 51 | + |
| 52 | +var out = atleastnd( 3, [ x, y ] ); |
| 53 | +// returns [ <ndarray>, <ndarray> ] |
| 54 | +``` |
| 55 | + |
| 56 | +The function accepts the following arguments: |
| 57 | + |
| 58 | +- **ndims**: minimum number of dimensions. |
| 59 | +- **arrays**: array-like object containing a list of scalars and/or ndarrays. |
| 60 | + |
| 61 | +</section> |
| 62 | + |
| 63 | +<!-- /.usage --> |
| 64 | + |
| 65 | +<section class="notes"> |
| 66 | + |
| 67 | +## Notes |
| 68 | + |
| 69 | +- If a provided ndarray has fewer dimensions than `ndims`, 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. |
| 70 | + |
| 71 | +- 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. |
| 72 | + |
| 73 | +- If provided a scalar value (i.e., a non-ndarray) and that value |
| 74 | + |
| 75 | + - is a number, the returned ndarray will have the [default][@stdlib/ndarray/defaults] real-valued floating-point data type. |
| 76 | + - is a boolean, the returned ndarray will have the [default][@stdlib/ndarray/defaults] boolean data type. |
| 77 | + - 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. |
| 78 | + - 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. |
| 79 | + - is any other value type, the returned ndarray will have a `'generic'` data type. |
| 80 | + |
| 81 | +</section> |
| 82 | + |
| 83 | +<!-- /.notes --> |
| 84 | + |
| 85 | +<section class="examples"> |
| 86 | + |
| 87 | +## Examples |
| 88 | + |
| 89 | +<!-- eslint no-undef: "error" --> |
| 90 | + |
| 91 | +```javascript |
| 92 | +var discreteUniform = require( '@stdlib/random/discrete-uniform' ); |
| 93 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 94 | +var atleastnd = require( '@stdlib/ndarray/base/atleastnd' ); |
| 95 | + |
| 96 | +var x = discreteUniform( [ 2, 2, 2 ], -100, 100 ); |
| 97 | +// returns <ndarray> |
| 98 | + |
| 99 | +var y = discreteUniform( [ 5, 2 ], -100, 100 ); |
| 100 | +// returns <ndarray> |
| 101 | + |
| 102 | +var out = atleastnd( 3, [ x, y ] ); |
| 103 | +// returns [ <ndarray>, <ndarray> ] |
| 104 | + |
| 105 | +console.log( ndarray2array( out[ 0 ] ) ); |
| 106 | +console.log( ndarray2array( out[ 1 ] ) ); |
| 107 | +``` |
| 108 | + |
| 109 | +</section> |
| 110 | + |
| 111 | +<!-- /.examples --> |
| 112 | + |
| 113 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 114 | + |
| 115 | +<section class="related"> |
| 116 | + |
| 117 | +</section> |
| 118 | + |
| 119 | +<!-- /.related --> |
| 120 | + |
| 121 | +<section class="links"> |
| 122 | + |
| 123 | +[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/ndarray/tree/main/ctor |
| 124 | + |
| 125 | +[@stdlib/ndarray/base/ctor]: https://github.com/stdlib-js/ndarray/tree/main/base/ctor |
| 126 | + |
| 127 | +[@stdlib/ndarray/defaults]: https://github.com/stdlib-js/ndarray/tree/main/defaults |
| 128 | + |
| 129 | +</section> |
| 130 | + |
| 131 | +<!-- /.links --> |
0 commit comments