|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2023 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 | +# emptyLike |
| 22 | + |
| 23 | +> Create an uninitialized [ndarray][@stdlib/ndarray/ctor] having the same shape and [data type][@stdlib/ndarray/dtypes] as a provided ndarray. |
| 24 | +
|
| 25 | +<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> |
| 26 | + |
| 27 | +<section class="intro"> |
| 28 | + |
| 29 | +</section> |
| 30 | + |
| 31 | +<!-- /.intro --> |
| 32 | + |
| 33 | +<!-- Package usage documentation. --> |
| 34 | + |
| 35 | +<section class="usage"> |
| 36 | + |
| 37 | +## Usage |
| 38 | + |
| 39 | +```javascript |
| 40 | +var emptyLike = require( '@stdlib/ndarray/empty-like' ); |
| 41 | +``` |
| 42 | + |
| 43 | +#### emptyLike( x\[, options] ) |
| 44 | + |
| 45 | +Creates an uninitialized [ndarray][@stdlib/ndarray/ctor] having the same shape and [data type][@stdlib/ndarray/dtypes] as a provided ndarray. |
| 46 | + |
| 47 | +```javascript |
| 48 | +var zeros = require( '@stdlib/ndarray/zeros' ); |
| 49 | + |
| 50 | +var x = zeros( [ 2, 2 ] ); |
| 51 | +// returns <ndarray> |
| 52 | + |
| 53 | +var y = emptyLike( x ); |
| 54 | +// returns <ndarray> |
| 55 | + |
| 56 | +var sh = y.shape; |
| 57 | +// returns [ 2, 2 ] |
| 58 | + |
| 59 | +var dt = y.dtype; |
| 60 | +// returns 'float64' |
| 61 | +``` |
| 62 | + |
| 63 | +The function supports the following `options`: |
| 64 | + |
| 65 | +- **dtype**: output [ndarray][@stdlib/ndarray/ctor] [data type][@stdlib/ndarray/dtypes]. Overrides the input ndarray's inferred [data type][@stdlib/ndarray/dtypes]. |
| 66 | +- **shape**: output [ndarray][@stdlib/ndarray/ctor] shape. Overrides the input ndarray's inferred shape. |
| 67 | +- **order**: specifies whether the output [ndarray][@stdlib/ndarray/ctor] should be `'row-major'` (C-style) or `'column-major'` (Fortran-style). Overrides the input ndarray's inferred order. |
| 68 | +- **mode**: specifies how to handle indices which exceed array dimensions (see [`ndarray`][@stdlib/ndarray/ctor]). Default: `'throw'`. |
| 69 | +- **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 ]`. |
| 70 | + |
| 71 | +To override either the `dtype`, `shape`, or `order`, specify the corresponding option. For example, to override the inferred [data type][@stdlib/ndarray/dtypes], |
| 72 | + |
| 73 | +```javascript |
| 74 | +var zeros = require( '@stdlib/ndarray/zeros' ); |
| 75 | + |
| 76 | +var x = zeros( [ 2, 2 ] ); |
| 77 | +// returns <ndarray> |
| 78 | + |
| 79 | +var dt = x.dtype; |
| 80 | +// returns 'float64' |
| 81 | + |
| 82 | +var y = emptyLike( x, { |
| 83 | + 'dtype': 'int32' |
| 84 | +}); |
| 85 | +// returns <ndarray> |
| 86 | + |
| 87 | +var sh = y.shape; |
| 88 | +// returns [ 2, 2 ] |
| 89 | + |
| 90 | +dt = y.dtype; |
| 91 | +// returns 'int32' |
| 92 | +``` |
| 93 | + |
| 94 | +</section> |
| 95 | + |
| 96 | +<!-- /.usage --> |
| 97 | + |
| 98 | +<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 99 | + |
| 100 | +<section class="notes"> |
| 101 | + |
| 102 | +## Notes |
| 103 | + |
| 104 | +- If the resolved output data type is `'generic'`, the function always returns a zero-filled array. |
| 105 | +- For returned [ndarrays][@stdlib/ndarray/ctor] whose underlying memory is **not** initialized, memory contents are unknown and may contain **sensitive** data. |
| 106 | + |
| 107 | +</section> |
| 108 | + |
| 109 | +<!-- /.notes --> |
| 110 | + |
| 111 | +<!-- Package usage examples. --> |
| 112 | + |
| 113 | +<section class="examples"> |
| 114 | + |
| 115 | +## Examples |
| 116 | + |
| 117 | +<!-- eslint no-undef: "error" --> |
| 118 | + |
| 119 | +```javascript |
| 120 | +var dtypes = require( '@stdlib/ndarray/dtypes' ); |
| 121 | +var zeros = require( '@stdlib/ndarray/zeros' ); |
| 122 | +var emptyLike = require( '@stdlib/ndarray/empty-like' ); |
| 123 | + |
| 124 | +// Get a list of data types: |
| 125 | +var dt = dtypes(); |
| 126 | + |
| 127 | +// Generate uninitialized arrays... |
| 128 | +var x; |
| 129 | +var y; |
| 130 | +var i; |
| 131 | +for ( i = 0; i < dt.length; i++ ) { |
| 132 | + x = zeros( [ 2, 2 ], { |
| 133 | + 'dtype': dt[ i ] |
| 134 | + }); |
| 135 | + y = emptyLike( x ); |
| 136 | + console.log( y.data ); |
| 137 | +} |
| 138 | +``` |
| 139 | + |
| 140 | +</section> |
| 141 | + |
| 142 | +<!-- /.examples --> |
| 143 | + |
| 144 | +<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 145 | + |
| 146 | +<section class="references"> |
| 147 | + |
| 148 | +</section> |
| 149 | + |
| 150 | +<!-- /.references --> |
| 151 | + |
| 152 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 153 | + |
| 154 | +<section class="related"> |
| 155 | + |
| 156 | +</section> |
| 157 | + |
| 158 | +<!-- /.related --> |
| 159 | + |
| 160 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 161 | + |
| 162 | +<section class="links"> |
| 163 | + |
| 164 | +[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib |
| 165 | + |
| 166 | +[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib |
| 167 | + |
| 168 | +</section> |
| 169 | + |
| 170 | +<!-- /.links --> |
0 commit comments