Skip to content

Commit 6bc5ee5

Browse files
headlessNodekgryte
andauthored
feat: add ndarray/broadcast-scalar
PR-URL: #9916 Closes: stdlib-js/metr-issue-tracker#166 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent e8b00ab commit 6bc5ee5

File tree

10 files changed

+1847
-0
lines changed

10 files changed

+1847
-0
lines changed
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 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+
# broadcastScalar
22+
23+
> Broadcast a scalar value to an [ndarray][@stdlib/ndarray/ctor] of a specified shape.
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 broadcastScalar = require( '@stdlib/ndarray/broadcast-scalar' );
41+
```
42+
43+
#### broadcastScalar( value, shape\[, options] )
44+
45+
Broadcasts a scalar value to an [ndarray][@stdlib/ndarray/ctor] of a specified shape.
46+
47+
```javascript
48+
var getDType = require( '@stdlib/ndarray/dtype' );
49+
50+
var x = broadcastScalar( 1.0, [ 2, 2 ] );
51+
// returns <ndarray>[ [ 1.0, 1.0 ], [ 1.0, 1.0 ] ]
52+
53+
var dt = String( getDType( x ) );
54+
// returns 'float64'
55+
```
56+
57+
The function accepts the following arguments:
58+
59+
- **value**: scalar value.
60+
- **shape**: shape of the output [ndarray][@stdlib/ndarray/ctor].
61+
62+
The function accepts the following options:
63+
64+
- **dtype**: output array [data type][@stdlib/ndarray/dtypes].
65+
- **order**: array order (i.e., memory layout). Must be either `row-major` (C-style) or `column-major` (Fortran-style). Default: `'row-major'`.
66+
- **readonly**: boolean indicating whether an array should be **read-only**. Default: `false`.
67+
68+
If a `dtype` option is not provided and `value`
69+
70+
- is a number, the default [data type][@stdlib/ndarray/dtypes] is the [default][@stdlib/ndarray/defaults] real-valued floating-point data type.
71+
- is a boolean, the default [data type][@stdlib/ndarray/dtypes] is the [default][@stdlib/ndarray/defaults] boolean data type.
72+
- is a complex number object of a known data type, the data type is the same as the provided value.
73+
- is a complex number object of an unknown data type, the default [data type][@stdlib/ndarray/dtypes] is the [default][@stdlib/ndarray/defaults] complex-valued floating-point data type.
74+
- is any other value type, the default [data type][@stdlib/ndarray/dtypes] is `'generic'`.
75+
76+
To explicitly specify the [data type][@stdlib/ndarray/dtypes] of the returned [ndarray][@stdlib/ndarray/ctor], provide a `dtype` option.
77+
78+
```javascript
79+
var getDType = require( '@stdlib/ndarray/dtype' );
80+
81+
var x = broadcastScalar( 1.0, [ 2, 2 ], {
82+
'dtype': 'float32'
83+
});
84+
// returns <ndarray>[ [ 1.0, 1.0 ], [ 1.0, 1.0 ] ]
85+
86+
var dt = String( getDType( x ) );
87+
// returns 'float32'
88+
```
89+
90+
</section>
91+
92+
<!-- /.usage -->
93+
94+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
95+
96+
<section class="notes">
97+
98+
## Notes
99+
100+
- If `value` is a number and [`dtype`][@stdlib/ndarray/dtypes] is a complex [data type][@stdlib/ndarray/dtypes], the function returns an [ndarray][@stdlib/ndarray/ctor] containing a complex number whose real component equals the provided scalar `value` and whose imaginary component is zero.
101+
- The function does not guard against precision loss when `value` is a number and the `dtype` argument is an integer [data type][@stdlib/ndarray/dtypes].
102+
103+
</section>
104+
105+
<!-- /.notes -->
106+
107+
<!-- Package usage examples. -->
108+
109+
<section class="examples">
110+
111+
## Examples
112+
113+
<!-- eslint no-undef: "error" -->
114+
115+
```javascript
116+
var dtypes = require( '@stdlib/ndarray/dtypes' );
117+
var broadcastScalar = require( '@stdlib/ndarray/broadcast-scalar' );
118+
119+
// Get a list of data types:
120+
var dt = dtypes( 'integer_and_generic' );
121+
122+
// Generate zero-dimensional arrays...
123+
var x;
124+
var i;
125+
for ( i = 0; i < dt.length; i++ ) {
126+
x = broadcastScalar( i, [ 2, 2 ], {
127+
'dtype': dt[ i ]
128+
});
129+
console.log( x.get( 0, 1 ) );
130+
}
131+
```
132+
133+
</section>
134+
135+
<!-- /.examples -->
136+
137+
<!-- 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. -->
138+
139+
<section class="references">
140+
141+
</section>
142+
143+
<!-- /.references -->
144+
145+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
146+
147+
<section class="related">
148+
149+
</section>
150+
151+
<!-- /.related -->
152+
153+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
154+
155+
<section class="links">
156+
157+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
158+
159+
[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes
160+
161+
[@stdlib/ndarray/defaults]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/defaults
162+
163+
<!-- <related-links> -->
164+
165+
<!-- </related-links> -->
166+
167+
</section>
168+
169+
<!-- /.links -->

0 commit comments

Comments
 (0)