Skip to content

Commit b2dcaa0

Browse files
committed
feat: add ndarray/base/from-scalar-like
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent 7dc5c14 commit b2dcaa0

File tree

10 files changed

+2086
-0
lines changed

10 files changed

+2086
-0
lines changed
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/ctor
135+
136+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
137+
138+
[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes
139+
140+
</section>
141+
142+
<!-- /.links -->

0 commit comments

Comments
 (0)