|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2025 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 | +# copy |
| 22 | + |
| 23 | +> Copy an input [ndarray][@stdlib/ndarray/ctor] to a new [ndarray][@stdlib/ndarray/ctor] having the same shape and [data type][@stdlib/ndarray/dtypes]. |
| 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 copy = require( '@stdlib/ndarray/copy' ); |
| 41 | +``` |
| 42 | + |
| 43 | +#### copy( x\[, options] ) |
| 44 | + |
| 45 | +Copies an input [ndarray][@stdlib/ndarray/ctor] to a new [ndarray][@stdlib/ndarray/ctor] having the same shape and [data type][@stdlib/ndarray/dtypes]. |
| 46 | + |
| 47 | +```javascript |
| 48 | +var getShape = require( '@stdlib/ndarray/shape' ); |
| 49 | +var zeros = require( '@stdlib/ndarray/zeros' ); |
| 50 | + |
| 51 | +var x = zeros( [ 2, 2 ] ); |
| 52 | +// returns <ndarray> |
| 53 | + |
| 54 | +var y = copy( x ); |
| 55 | +// returns <ndarray> |
| 56 | + |
| 57 | +var sh = getShape( y ); |
| 58 | +// returns [ 2, 2 ] |
| 59 | +``` |
| 60 | + |
| 61 | +The function supports the following `options`: |
| 62 | + |
| 63 | +- **dtype**: output [ndarray][@stdlib/ndarray/ctor] [data type][@stdlib/ndarray/dtypes]. Overrides the input ndarray's inferred [data type][@stdlib/ndarray/dtypes]. |
| 64 | +- **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. |
| 65 | +- **mode**: specifies how to handle indices which exceed array dimensions (see [`ndarray`][@stdlib/ndarray/ctor]). Default: `'throw'`. |
| 66 | +- **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 ]`. |
| 67 | + |
| 68 | +To override either the `dtype` or `order`, specify the corresponding option. For example, to override the inferred [data type][@stdlib/ndarray/dtypes], |
| 69 | + |
| 70 | +```javascript |
| 71 | +var getShape = require( '@stdlib/ndarray/shape' ); |
| 72 | +var getDtype = require( '@stdlib/ndarray/dtype' ); |
| 73 | +var zeros = require( '@stdlib/ndarray/zeros' ); |
| 74 | + |
| 75 | +var x = zeros( [ 2, 2 ] ); |
| 76 | +// returns <ndarray> |
| 77 | + |
| 78 | +var dt = String( getDtype( x ) ); |
| 79 | +// returns 'float64' |
| 80 | + |
| 81 | +var y = copy( x, { |
| 82 | + 'dtype': 'float32' |
| 83 | +}); |
| 84 | +// returns <ndarray> |
| 85 | + |
| 86 | +var sh = getShape( y ); |
| 87 | +// returns [ 2, 2 ] |
| 88 | + |
| 89 | +dt = String( getDtype( y ) ); |
| 90 | +// returns 'float32' |
| 91 | +``` |
| 92 | + |
| 93 | +</section> |
| 94 | + |
| 95 | +<!-- /.usage --> |
| 96 | + |
| 97 | +<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 98 | + |
| 99 | +<section class="notes"> |
| 100 | + |
| 101 | +## Notes |
| 102 | + |
| 103 | +- The function performs a full copy in which an ndarray's underlying data is copied to a new underlying data buffer. |
| 104 | + |
| 105 | +</section> |
| 106 | + |
| 107 | +<!-- /.notes --> |
| 108 | + |
| 109 | +<!-- Package usage examples. --> |
| 110 | + |
| 111 | +<section class="examples"> |
| 112 | + |
| 113 | +## Examples |
| 114 | + |
| 115 | +<!-- eslint no-undef: "error" --> |
| 116 | + |
| 117 | +```javascript |
| 118 | +var uniform = require( '@stdlib/random/uniform' ); |
| 119 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 120 | +var copy = require( '@stdlib/ndarray/copy' ); |
| 121 | + |
| 122 | +var x = uniform( [ 5, 2 ], -10.0, 10.0, { |
| 123 | + 'dtype': 'generic' |
| 124 | +}); |
| 125 | +console.log( ndarray2array( x ) ); |
| 126 | + |
| 127 | +var y = copy( x ); |
| 128 | +console.log( ndarray2array( y ) ); |
| 129 | +``` |
| 130 | + |
| 131 | +</section> |
| 132 | + |
| 133 | +<!-- /.examples --> |
| 134 | + |
| 135 | +<!-- 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. --> |
| 136 | + |
| 137 | +<section class="references"> |
| 138 | + |
| 139 | +</section> |
| 140 | + |
| 141 | +<!-- /.references --> |
| 142 | + |
| 143 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 144 | + |
| 145 | +<section class="related"> |
| 146 | + |
| 147 | +</section> |
| 148 | + |
| 149 | +<!-- /.related --> |
| 150 | + |
| 151 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 152 | + |
| 153 | +<section class="links"> |
| 154 | + |
| 155 | +[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/ndarray/tree/main/ctor |
| 156 | + |
| 157 | +[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/ndarray/tree/main/dtypes |
| 158 | + |
| 159 | +<!-- <related-links> --> |
| 160 | + |
| 161 | +<!-- </related-links> --> |
| 162 | + |
| 163 | +</section> |
| 164 | + |
| 165 | +<!-- /.links --> |
0 commit comments