Skip to content

Commit fae3746

Browse files
committed
Add ndarray utility to convert a scalar to an ndarray
1 parent 08b1f46 commit fae3746

File tree

10 files changed

+1514
-0
lines changed

10 files changed

+1514
-0
lines changed
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2022 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+
# scalar2ndarray
22+
23+
> Convert a scalar value to a zero-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 scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
41+
```
42+
43+
#### scalar2ndarray( value\[, dtype] )
44+
45+
Returns a zero-dimensional [`ndarray`][@stdlib/ndarray/ctor] containing a provided scalar `value` and having a specified [data type][@stdlib/ndarray/dtypes].
46+
47+
```javascript
48+
var x = scalar2ndarray( 1.0 );
49+
// returns <ndarray>
50+
51+
var sh = x.shape;
52+
// returns []
53+
54+
var dt = x.dtype;
55+
// returns 'float64'
56+
57+
var v = x.get();
58+
// returns 1.0
59+
```
60+
61+
If `dtype` is not provided and `value`
62+
63+
- is a `number`, the default [data type][@stdlib/ndarray/dtypes] is `'float64'`.
64+
- is a complex number object, the default [data type][@stdlib/ndarray/dtypes] is `'complex128'`.
65+
- is any other value type, the default [data type][@stdlib/ndarray/dtypes] is `'generic'`.
66+
67+
To explicitly specify the [data type][@stdlib/ndarray/dtypes] of the returned [`ndarray`][@stdlib/ndarray/ctor], provide a `dtype` argument.
68+
69+
```javascript
70+
var x = scalar2ndarray( 1.0, 'float32' );
71+
// returns <ndarray>
72+
73+
var sh = x.shape;
74+
// returns []
75+
76+
var dt = x.dtype;
77+
// returns 'float32'
78+
79+
var v = x.get();
80+
// returns 1.0
81+
```
82+
83+
</section>
84+
85+
<!-- /.usage -->
86+
87+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
88+
89+
<section class="notes">
90+
91+
## Notes
92+
93+
- If `value` is a number and [`dtype`][@stdlib/ndarray/dtypes] is a complex [data type][@stdlib/ndarray/dtypes], the function returns a zero-dimensional [`ndarray`][@stdlib/ndarray/ctor] containing a complex number whose real component equals the provided scalar `value` and whose imaginary component is zero.
94+
95+
</section>
96+
97+
<!-- /.notes -->
98+
99+
<!-- Package usage examples. -->
100+
101+
<section class="examples">
102+
103+
## Examples
104+
105+
<!-- eslint no-undef: "error" -->
106+
107+
```javascript
108+
var dtypes = require( '@stdlib/ndarray/dtypes' );
109+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
110+
111+
// Get a list of data types:
112+
var dt = dtypes();
113+
114+
// Generate zero-dimensional arrays...
115+
var x;
116+
var i;
117+
for ( i = 0; i < dt.length; i++ ) {
118+
x = scalar2ndarray( i, dt[ i ] );
119+
console.log( x.get() );
120+
}
121+
```
122+
123+
</section>
124+
125+
<!-- /.examples -->
126+
127+
<!-- 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. -->
128+
129+
<section class="references">
130+
131+
</section>
132+
133+
<!-- /.references -->
134+
135+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
136+
137+
<section class="related">
138+
139+
</section>
140+
141+
<!-- /.related -->
142+
143+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
144+
145+
<section class="links">
146+
147+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib
148+
149+
[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib
150+
151+
</section>
152+
153+
<!-- /.links -->

0 commit comments

Comments
 (0)