|
| 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 | +# scalar2ndarrayLike |
| 22 | + |
| 23 | +> Convert a scalar value to a zero-dimensional ndarray having the same [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 scalar2ndarrayLike = require( '@stdlib/ndarray/base/from-scalar-like' ); |
| 41 | +``` |
| 42 | + |
| 43 | +#### scalar2ndarrayLike( x, value ) |
| 44 | + |
| 45 | +Returns a zero-dimensional ndarray containing a provided scalar `value` and having the same [data type][@stdlib/ndarray/dtypes] as a provided ndarray. |
| 46 | + |
| 47 | +```javascript |
| 48 | +var zeros = require( '@stdlib/ndarray/base/zeros' ); |
| 49 | + |
| 50 | +var x = zeros( 'float32', [ 2, 2 ], 'row-major' ); |
| 51 | +// returns <ndarray> |
| 52 | + |
| 53 | +var y = scalar2ndarrayLike( x, 1.0 ); |
| 54 | +// returns <ndarray> |
| 55 | + |
| 56 | +var sh = y.shape; |
| 57 | +// returns [] |
| 58 | + |
| 59 | +var dt = y.dtype; |
| 60 | +// returns 'float32' |
| 61 | + |
| 62 | +var v = y.get(); |
| 63 | +// returns 1.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 | +- Along with data type and order, the function infers the "class" of the returned ndarray from the provided ndarray. For example, if provided a "base" [ndarray][@stdlib/ndarray/base/ctor], the function returns a base [ndarray][@stdlib/ndarray/base/ctor]. If provided a non-base [ndarray][@stdlib/ndarray/ctor], the function returns a non-base [ndarray][@stdlib/ndarray/ctor]. |
| 77 | +- If `value` is a number and a provided ndarray has a complex [data type][@stdlib/ndarray/dtypes], the function returns a zero-dimensional ndarray containing a complex number whose real component equals the provided scalar `value` and whose imaginary component is zero. |
| 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 dtypes = require( '@stdlib/ndarray/dtypes' ); |
| 93 | +var empty = require( '@stdlib/ndarray/base/empty' ); |
| 94 | +var scalar2ndarrayLike = require( '@stdlib/ndarray/base/from-scalar-like' ); |
| 95 | + |
| 96 | +// Get a list of data types: |
| 97 | +var dt = dtypes(); |
| 98 | + |
| 99 | +// Generate zero-dimensional arrays... |
| 100 | +var x; |
| 101 | +var y; |
| 102 | +var i; |
| 103 | +for ( i = 0; i < dt.length; i++ ) { |
| 104 | + x = empty( dt[ i ], [ 2, 2 ], 'row-major' ); |
| 105 | + y = scalar2ndarrayLike( x, i ); |
| 106 | + console.log( y.get() ); |
| 107 | +} |
| 108 | +``` |
| 109 | + |
| 110 | +</section> |
| 111 | + |
| 112 | +<!-- /.examples --> |
| 113 | + |
| 114 | +<!-- 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. --> |
| 115 | + |
| 116 | +<section class="references"> |
| 117 | + |
| 118 | +</section> |
| 119 | + |
| 120 | +<!-- /.references --> |
| 121 | + |
| 122 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 123 | + |
| 124 | +<section class="related"> |
| 125 | + |
| 126 | +</section> |
| 127 | + |
| 128 | +<!-- /.related --> |
| 129 | + |
| 130 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 131 | + |
| 132 | +<section class="links"> |
| 133 | + |
| 134 | +[@stdlib/ndarray/base/ctor]: https://github.com/stdlib-js/ndarray/tree/main/base/ctor |
| 135 | + |
| 136 | +[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/ndarray/tree/main/ctor |
| 137 | + |
| 138 | +[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/ndarray/tree/main/dtypes |
| 139 | + |
| 140 | +</section> |
| 141 | + |
| 142 | +<!-- /.links --> |
0 commit comments