Skip to content

Commit 691639c

Browse files
committed
Add utility to convert a scalar to a "base" ndarray
1 parent aff396e commit 691639c

File tree

10 files changed

+1368
-0
lines changed

10 files changed

+1368
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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 provided scalar value to a zero-dimensional [ndarray][@stdlib/ndarray/base/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/base/from-scalar' );
41+
```
42+
43+
#### scalar2ndarray( value, dtype )
44+
45+
Returns a zero-dimensional [`ndarray`][@stdlib/ndarray/base/ctor] containing a provided scalar value and having a specified [data type][@stdlib/ndarray/dtypes].
46+
47+
```javascript
48+
var x = scalar2ndarray( 1.0, 'float64' );
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+
</section>
62+
63+
<!-- /.usage -->
64+
65+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
66+
67+
<section class="notes">
68+
69+
## Notes
70+
71+
- 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/base/ctor] containing a complex number whose real component equals the provided scalar `value` and whose imaginary component is zero.
72+
73+
</section>
74+
75+
<!-- /.notes -->
76+
77+
<!-- Package usage examples. -->
78+
79+
<section class="examples">
80+
81+
## Examples
82+
83+
<!-- eslint no-undef: "error" -->
84+
85+
```javascript
86+
var dtypes = require( '@stdlib/ndarray/dtypes' );
87+
var scalar2ndarray = require( '@stdlib/ndarray/base/from-scalar' );
88+
89+
// Get a list of data types:
90+
var dt = dtypes();
91+
92+
// Generate zero-dimensional arrays...
93+
var x;
94+
var i;
95+
for ( i = 0; i < dt.length; i++ ) {
96+
x = scalar2ndarray( i, dt[ i ] );
97+
console.log( x.get() );
98+
}
99+
```
100+
101+
</section>
102+
103+
<!-- /.examples -->
104+
105+
<!-- 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. -->
106+
107+
<section class="references">
108+
109+
</section>
110+
111+
<!-- /.references -->
112+
113+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
114+
115+
<section class="related">
116+
117+
</section>
118+
119+
<!-- /.related -->
120+
121+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
122+
123+
<section class="links">
124+
125+
[@stdlib/ndarray/base/ctor]: https://github.com/stdlib-js/stdlib
126+
127+
[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib
128+
129+
</section>
130+
131+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,265 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2022 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
25+
var Complex128 = require( '@stdlib/complex/float64' );
26+
var Complex64 = require( '@stdlib/complex/float32' );
27+
var pkg = require( './../package.json' ).name;
28+
var scalar2ndarray = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
bench( pkg+':dtype=float64', function benchmark( b ) {
34+
var x;
35+
var i;
36+
37+
b.tic();
38+
for ( i = 0; i < b.iterations; i++ ) {
39+
x = scalar2ndarray( i, 'float64' );
40+
if ( x.length !== 1 ) {
41+
b.fail( 'should have length 1' );
42+
}
43+
}
44+
b.toc();
45+
if ( !isndarrayLike( x ) ) {
46+
b.fail( 'should return an ndarray' );
47+
}
48+
b.pass( 'benchmark finished' );
49+
b.end();
50+
});
51+
52+
bench( pkg+':dtype=float32', function benchmark( b ) {
53+
var x;
54+
var i;
55+
56+
b.tic();
57+
for ( i = 0; i < b.iterations; i++ ) {
58+
x = scalar2ndarray( i, 'float32' );
59+
if ( x.length !== 1 ) {
60+
b.fail( 'should have length 1' );
61+
}
62+
}
63+
b.toc();
64+
if ( !isndarrayLike( x ) ) {
65+
b.fail( 'should return an ndarray' );
66+
}
67+
b.pass( 'benchmark finished' );
68+
b.end();
69+
});
70+
71+
bench( pkg+':dtype=complex128', function benchmark( b ) {
72+
var x;
73+
var v;
74+
var i;
75+
76+
v = new Complex128( 1.0, 2.0 );
77+
78+
b.tic();
79+
for ( i = 0; i < b.iterations; i++ ) {
80+
x = scalar2ndarray( v, 'complex128' );
81+
if ( x.length !== 1 ) {
82+
b.fail( 'should have length 1' );
83+
}
84+
}
85+
b.toc();
86+
if ( !isndarrayLike( x ) ) {
87+
b.fail( 'should return an ndarray' );
88+
}
89+
b.pass( 'benchmark finished' );
90+
b.end();
91+
});
92+
93+
bench( pkg+':dtype=complex64', function benchmark( b ) {
94+
var x;
95+
var v;
96+
var i;
97+
98+
v = new Complex64( 1.0, 2.0 );
99+
100+
b.tic();
101+
for ( i = 0; i < b.iterations; i++ ) {
102+
x = scalar2ndarray( v, 'complex64' );
103+
if ( x.length !== 1 ) {
104+
b.fail( 'should have length 1' );
105+
}
106+
}
107+
b.toc();
108+
if ( !isndarrayLike( x ) ) {
109+
b.fail( 'should return an ndarray' );
110+
}
111+
b.pass( 'benchmark finished' );
112+
b.end();
113+
});
114+
115+
bench( pkg+':dtype=int32', function benchmark( b ) {
116+
var x;
117+
var i;
118+
119+
b.tic();
120+
for ( i = 0; i < b.iterations; i++ ) {
121+
x = scalar2ndarray( i, 'int32' );
122+
if ( x.length !== 1 ) {
123+
b.fail( 'should have length 1' );
124+
}
125+
}
126+
b.toc();
127+
if ( !isndarrayLike( x ) ) {
128+
b.fail( 'should return an ndarray' );
129+
}
130+
b.pass( 'benchmark finished' );
131+
b.end();
132+
});
133+
134+
bench( pkg+':dtype=uint32', function benchmark( b ) {
135+
var x;
136+
var i;
137+
138+
b.tic();
139+
for ( i = 0; i < b.iterations; i++ ) {
140+
x = scalar2ndarray( i, 'uint32' );
141+
if ( x.length !== 1 ) {
142+
b.fail( 'should have length 1' );
143+
}
144+
}
145+
b.toc();
146+
if ( !isndarrayLike( x ) ) {
147+
b.fail( 'should return an ndarray' );
148+
}
149+
b.pass( 'benchmark finished' );
150+
b.end();
151+
});
152+
153+
bench( pkg+':dtype=int16', function benchmark( b ) {
154+
var x;
155+
var i;
156+
157+
b.tic();
158+
for ( i = 0; i < b.iterations; i++ ) {
159+
x = scalar2ndarray( i, 'int16' );
160+
if ( x.length !== 1 ) {
161+
b.fail( 'should have length 1' );
162+
}
163+
}
164+
b.toc();
165+
if ( !isndarrayLike( x ) ) {
166+
b.fail( 'should return an ndarray' );
167+
}
168+
b.pass( 'benchmark finished' );
169+
b.end();
170+
});
171+
172+
bench( pkg+':dtype=uint16', function benchmark( b ) {
173+
var x;
174+
var i;
175+
176+
b.tic();
177+
for ( i = 0; i < b.iterations; i++ ) {
178+
x = scalar2ndarray( i, 'uint16' );
179+
if ( x.length !== 1 ) {
180+
b.fail( 'should have length 1' );
181+
}
182+
}
183+
b.toc();
184+
if ( !isndarrayLike( x ) ) {
185+
b.fail( 'should return an ndarray' );
186+
}
187+
b.pass( 'benchmark finished' );
188+
b.end();
189+
});
190+
191+
bench( pkg+':dtype=int8', function benchmark( b ) {
192+
var x;
193+
var i;
194+
195+
b.tic();
196+
for ( i = 0; i < b.iterations; i++ ) {
197+
x = scalar2ndarray( i, 'int8' );
198+
if ( x.length !== 1 ) {
199+
b.fail( 'should have length 1' );
200+
}
201+
}
202+
b.toc();
203+
if ( !isndarrayLike( x ) ) {
204+
b.fail( 'should return an ndarray' );
205+
}
206+
b.pass( 'benchmark finished' );
207+
b.end();
208+
});
209+
210+
bench( pkg+':dtype=uint8', function benchmark( b ) {
211+
var x;
212+
var i;
213+
214+
b.tic();
215+
for ( i = 0; i < b.iterations; i++ ) {
216+
x = scalar2ndarray( i, 'uint8' );
217+
if ( x.length !== 1 ) {
218+
b.fail( 'should have length 1' );
219+
}
220+
}
221+
b.toc();
222+
if ( !isndarrayLike( x ) ) {
223+
b.fail( 'should return an ndarray' );
224+
}
225+
b.pass( 'benchmark finished' );
226+
b.end();
227+
});
228+
229+
bench( pkg+':dtype=uint8c', function benchmark( b ) {
230+
var x;
231+
var i;
232+
233+
b.tic();
234+
for ( i = 0; i < b.iterations; i++ ) {
235+
x = scalar2ndarray( i, 'uint8c' );
236+
if ( x.length !== 1 ) {
237+
b.fail( 'should have length 1' );
238+
}
239+
}
240+
b.toc();
241+
if ( !isndarrayLike( x ) ) {
242+
b.fail( 'should return an ndarray' );
243+
}
244+
b.pass( 'benchmark finished' );
245+
b.end();
246+
});
247+
248+
bench( pkg+':dtype=generic', function benchmark( b ) {
249+
var x;
250+
var i;
251+
252+
b.tic();
253+
for ( i = 0; i < b.iterations; i++ ) {
254+
x = scalar2ndarray( i, 'generic' );
255+
if ( x.length !== 1 ) {
256+
b.fail( 'should have length 1' );
257+
}
258+
}
259+
b.toc();
260+
if ( !isndarrayLike( x ) ) {
261+
b.fail( 'should return an ndarray' );
262+
}
263+
b.pass( 'benchmark finished' );
264+
b.end();
265+
});

0 commit comments

Comments
 (0)