|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2024 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 | +# Vector |
| 22 | + |
| 23 | +> Create a vector (i.e., a one-dimensional [ndarray][@stdlib/ndarray/ctor]). |
| 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 vector = require( '@stdlib/ndarray/vector/ctor' ); |
| 41 | +``` |
| 42 | + |
| 43 | +#### vector( \[dtype]\[, options] ) |
| 44 | + |
| 45 | +Returns a one-dimensional [ndarray][@stdlib/ndarray/ctor] having a specified [data type][@stdlib/ndarray/dtypes]. |
| 46 | + |
| 47 | +```javascript |
| 48 | +var getDType = require( '@stdlib/ndarray/dtype' ); |
| 49 | +var numel = require( '@stdlib/ndarray/numel' ); |
| 50 | + |
| 51 | +var arr = vector(); |
| 52 | +// returns <ndarray> |
| 53 | + |
| 54 | +var len = numel( arr ); |
| 55 | +// returns 0 |
| 56 | + |
| 57 | +var dt = getDType( arr ); |
| 58 | +// returns 'float64' |
| 59 | +``` |
| 60 | + |
| 61 | +By default, the function returns an [ndarray][@stdlib/ndarray/ctor] having a [`float64`][@stdlib/ndarray/dtypes] data type. To specify an alternative [data type][@stdlib/ndarray/dtypes], provide a `dtype` argument. |
| 62 | + |
| 63 | +```javascript |
| 64 | +var getDType = require( '@stdlib/ndarray/dtype' ); |
| 65 | +var numel = require( '@stdlib/ndarray/numel' ); |
| 66 | + |
| 67 | +var arr = vector( 'int32' ); |
| 68 | +// returns <ndarray> |
| 69 | + |
| 70 | +var len = numel( arr ); |
| 71 | +// returns 0 |
| 72 | + |
| 73 | +var dt = getDType( arr ); |
| 74 | +// returns 'int32' |
| 75 | +``` |
| 76 | + |
| 77 | +The function accepts the following options: |
| 78 | + |
| 79 | +- **order**: specifies whether an [ndarray][@stdlib/ndarray/ctor] is `'row-major'` (C-style) or `'column-major'` (Fortran-style). Default: `'row-major'`. |
| 80 | +- **mode**: specifies how to handle indices which exceed array dimensions (see [`ndarray`][@stdlib/ndarray/ctor]). Default: `'throw'`. |
| 81 | +- **readonly**: boolean indicating whether an array should be **read-only**. Default: `false`. |
| 82 | + |
| 83 | +#### vector( length\[, dtype]\[, options] ) |
| 84 | + |
| 85 | +Returns a one-dimensional [ndarray][@stdlib/ndarray/ctor] having a specified `length`. |
| 86 | + |
| 87 | +```javascript |
| 88 | +var numel = require( '@stdlib/ndarray/numel' ); |
| 89 | + |
| 90 | +var arr1 = vector( 5 ); |
| 91 | +// returns <ndarray> |
| 92 | + |
| 93 | +var len1 = numel( arr1 ); |
| 94 | +// returns 5 |
| 95 | + |
| 96 | +var arr2 = vector( 5, 'uint8' ); |
| 97 | +// returns <ndarray> |
| 98 | + |
| 99 | +var len2 = numel( arr2 ); |
| 100 | +// returns 5 |
| 101 | +``` |
| 102 | + |
| 103 | +#### vector( obj\[, dtype]\[, options] ) |
| 104 | + |
| 105 | +Creates a one-dimensional [ndarray][@stdlib/ndarray/ctor] from an array-like object or iterable. |
| 106 | + |
| 107 | +```javascript |
| 108 | +var numel = require( '@stdlib/ndarray/numel' ); |
| 109 | + |
| 110 | +var arr1 = vector( [ 0.5, 0.5, 0.5 ] ); |
| 111 | +// returns <ndarray> |
| 112 | + |
| 113 | +var len1 = numel( arr1 ); |
| 114 | +// returns 3 |
| 115 | + |
| 116 | +var arr2 = vector( [ 0.5, 0.5, 0.5 ], 'float32' ); |
| 117 | +// returns <ndarray> |
| 118 | + |
| 119 | +var len2 = numel( arr2 ); |
| 120 | +// returns 3 |
| 121 | +``` |
| 122 | + |
| 123 | +#### vector( buffer\[, byteOffset\[, length]]\[, dtype]\[, options] ) |
| 124 | + |
| 125 | +Returns a one-dimensional [ndarray][@stdlib/ndarray/ctor] view of an [`ArrayBuffer`][@stdlib/array/buffer]. |
| 126 | + |
| 127 | +```javascript |
| 128 | +var ArrayBuffer = require( '@stdlib/array/buffer' ); |
| 129 | +var getDType = require( '@stdlib/ndarray/dtype' ); |
| 130 | +var numel = require( '@stdlib/ndarray/numel' ); |
| 131 | + |
| 132 | +var buf = new ArrayBuffer( 32 ); |
| 133 | + |
| 134 | +var arr1 = vector( buf ); |
| 135 | +// returns <ndarray> |
| 136 | + |
| 137 | +var len1 = numel( arr1 ); |
| 138 | +// returns 4 |
| 139 | + |
| 140 | +var dt1 = getDType( arr1 ); |
| 141 | +// returns 'float64' |
| 142 | + |
| 143 | +var arr2 = vector( buf, 'float32' ); |
| 144 | +// returns <ndarray> |
| 145 | + |
| 146 | +var len2 = numel( arr2 ); |
| 147 | +// returns 8 |
| 148 | + |
| 149 | +var dt2 = getDType( arr2 ); |
| 150 | +// returns 'float32' |
| 151 | + |
| 152 | +var arr3 = vector( buf, 16 ); |
| 153 | +// returns <ndarray> |
| 154 | + |
| 155 | +var len3 = numel( arr3 ); |
| 156 | +// returns 2 |
| 157 | + |
| 158 | +var dt3 = getDType( arr3 ); |
| 159 | +// returns 'float64' |
| 160 | + |
| 161 | +var arr4 = vector( buf, 16, 'float32' ); |
| 162 | +// returns <ndarray> |
| 163 | + |
| 164 | +var len4 = numel( arr4 ); |
| 165 | +// returns 4 |
| 166 | + |
| 167 | +var dt4 = getDType( arr4 ); |
| 168 | +// returns 'float32' |
| 169 | + |
| 170 | +var arr5 = vector( buf, 16, 1 ); |
| 171 | +// returns <ndarray> |
| 172 | + |
| 173 | +var len5 = numel( arr5 ); |
| 174 | +// returns 1 |
| 175 | + |
| 176 | +var dt5 = getDType( arr5 ); |
| 177 | +// returns 'float64' |
| 178 | + |
| 179 | +var arr6 = vector( buf, 10, 4, 'int16' ); |
| 180 | +// returns <ndarray> |
| 181 | + |
| 182 | +var len6 = numel( arr6 ); |
| 183 | +// returns 4 |
| 184 | + |
| 185 | +var dt6 = getDType( arr6 ); |
| 186 | +// returns 'int16' |
| 187 | +``` |
| 188 | + |
| 189 | +</section> |
| 190 | + |
| 191 | +<!-- /.usage --> |
| 192 | + |
| 193 | +<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 194 | + |
| 195 | +<section class="notes"> |
| 196 | + |
| 197 | +</section> |
| 198 | + |
| 199 | +<!-- /.notes --> |
| 200 | + |
| 201 | +<!-- Package usage examples. --> |
| 202 | + |
| 203 | +<section class="examples"> |
| 204 | + |
| 205 | +## Examples |
| 206 | + |
| 207 | +<!-- eslint no-undef: "error" --> |
| 208 | + |
| 209 | +```javascript |
| 210 | +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); |
| 211 | +var cartesianProduct = require( '@stdlib/array/cartesian-product' ); |
| 212 | +var unzip = require( '@stdlib/utils/unzip' ); |
| 213 | +var dtypes = require( '@stdlib/ndarray/dtypes' ); |
| 214 | +var getShape = require( '@stdlib/ndarray/shape' ); |
| 215 | +var logEachMap = require( '@stdlib/console/log-each-map' ); |
| 216 | +var vector = require( '@stdlib/ndarray/vector/ctor' ); |
| 217 | + |
| 218 | +// Create an array of random array lengths: |
| 219 | +var lens = discreteUniform( 10, 5, 15, { |
| 220 | + 'dtype': 'int32' |
| 221 | +}); |
| 222 | + |
| 223 | +// Resolve a list of supported ndarray date types: |
| 224 | +var dts = dtypes(); |
| 225 | + |
| 226 | +// Create length-dtype pairs: |
| 227 | +var pairs = cartesianProduct( lens, dts ); |
| 228 | + |
| 229 | +// Split the pairs into individual arguments: |
| 230 | +var args = unzip( pairs ); |
| 231 | + |
| 232 | +// Define a callback to create a vector and return the vector shape: |
| 233 | +function clbk( len, dtype ) { |
| 234 | + var x = vector( len, dtype ); |
| 235 | + return getShape( x ); |
| 236 | +} |
| 237 | + |
| 238 | +// Apply the callback and print the results: |
| 239 | +logEachMap( 'len: %2d. dtype: %10s. shape: [%d].', args[ 0 ], args[ 1 ], clbk ); |
| 240 | +``` |
| 241 | + |
| 242 | +</section> |
| 243 | + |
| 244 | +<!-- /.examples --> |
| 245 | + |
| 246 | +<!-- 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. --> |
| 247 | + |
| 248 | +<section class="references"> |
| 249 | + |
| 250 | +</section> |
| 251 | + |
| 252 | +<!-- /.references --> |
| 253 | + |
| 254 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 255 | + |
| 256 | +<section class="related"> |
| 257 | + |
| 258 | +</section> |
| 259 | + |
| 260 | +<!-- /.related --> |
| 261 | + |
| 262 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 263 | + |
| 264 | +<section class="links"> |
| 265 | + |
| 266 | +[@stdlib/array/buffer]: https://github.com/stdlib-js/array-buffer |
| 267 | + |
| 268 | +[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/ndarray/tree/main/ctor |
| 269 | + |
| 270 | +[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/ndarray/tree/main/dtypes |
| 271 | + |
| 272 | +</section> |
| 273 | + |
| 274 | +<!-- /.links --> |
0 commit comments