|
| 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 | +# scalar2array |
| 22 | + |
| 23 | +> Create a single-element array containing a provided scalar value. |
| 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 scalar2array = require( '@stdlib/array/from-scalar' ); |
| 41 | +``` |
| 42 | + |
| 43 | +#### scalar2array( value\[, dtype] ) |
| 44 | + |
| 45 | +Returns a single-element array containing a provided scalar value. |
| 46 | + |
| 47 | +```javascript |
| 48 | +var x = scalar2array( 3.0 ); |
| 49 | +// returns <Float64Array>[ 3.0 ] |
| 50 | +``` |
| 51 | + |
| 52 | +If not provided a `dtype` argument and `value` |
| 53 | + |
| 54 | +- is a `number`, the default [data type][@stdlib/array/dtypes] is the [default][@stdlib/array/defaults] real-valued floating-point data type. |
| 55 | +- is a complex number object of a known data type, the data type is the same as the provided value. |
| 56 | +- is a complex number object of an unknown data type, the default [data type][@stdlib/array/dtypes] is the [default][@stdlib/array/defaults] complex-valued floating-point data type. |
| 57 | +- is any other value type, the default [data type][@stdlib/array/dtypes] is `'generic'`. |
| 58 | + |
| 59 | +To explicitly specify the [data type][@stdlib/array/dtypes] of the returned array, provide a `dtype` argument. |
| 60 | + |
| 61 | +```javascript |
| 62 | +var x = scalar2array( 3.0, 'float32' ); |
| 63 | +// returns <Float32Array>[ 3.0 ] |
| 64 | +``` |
| 65 | + |
| 66 | +</section> |
| 67 | + |
| 68 | +<!-- /.usage --> |
| 69 | + |
| 70 | +<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 71 | + |
| 72 | +<section class="notes"> |
| 73 | + |
| 74 | +## Notes |
| 75 | + |
| 76 | +- If `value` is a number and the `dtype` argument is a complex [data type][@stdlib/array/dtypes], the function returns a complex number array containing a complex number whose real component equals the provided scalar `value` and whose imaginary component is zero. |
| 77 | +- The function does **not** guard against precision loss when `value` is a number and the `dtype` argument is an integer [data type][@stdlib/array/dtypes]. |
| 78 | + |
| 79 | +</section> |
| 80 | + |
| 81 | +<!-- /.notes --> |
| 82 | + |
| 83 | +<!-- Package usage examples. --> |
| 84 | + |
| 85 | +<section class="examples"> |
| 86 | + |
| 87 | +## Examples |
| 88 | + |
| 89 | +<!-- eslint no-undef: "error" --> |
| 90 | + |
| 91 | +```javascript |
| 92 | +var Complex128 = require( '@stdlib/complex/float64' ); |
| 93 | +var array2scalar = require( '@stdlib/array/from-scalar' ); |
| 94 | + |
| 95 | +var x = array2scalar( 3.0 ); |
| 96 | +// returns <Float64Array>[ 3.0 ] |
| 97 | + |
| 98 | +x = array2scalar( 3, 'int32' ); |
| 99 | +// returns <Int32Array>[ 3 ] |
| 100 | + |
| 101 | +x = array2scalar( new Complex128( 3.0, 4.0 ) ); |
| 102 | +// returns <Complex128Array> |
| 103 | + |
| 104 | +x = array2scalar( {} ); |
| 105 | +// returns [ {} ] |
| 106 | +``` |
| 107 | + |
| 108 | +</section> |
| 109 | + |
| 110 | +<!-- /.examples --> |
| 111 | + |
| 112 | +<!-- 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. --> |
| 113 | + |
| 114 | +<section class="references"> |
| 115 | + |
| 116 | +</section> |
| 117 | + |
| 118 | +<!-- /.references --> |
| 119 | + |
| 120 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 121 | + |
| 122 | +<section class="related"> |
| 123 | + |
| 124 | +</section> |
| 125 | + |
| 126 | +<!-- /.related --> |
| 127 | + |
| 128 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 129 | + |
| 130 | +<section class="links"> |
| 131 | + |
| 132 | +[@stdlib/array/dtypes]: https://github.com/stdlib-js/array/tree/main/dtypes |
| 133 | + |
| 134 | +[@stdlib/array/defaults]: https://github.com/stdlib-js/array/tree/main/defaults |
| 135 | + |
| 136 | +</section> |
| 137 | + |
| 138 | +<!-- /.links --> |
0 commit comments