Skip to content

Commit 483630f

Browse files
headlessNodekgryte
andauthored
feat: add ndarray/base/broadcast-scalar-like
PR-URL: #9893 Closes: stdlib-js/metr-issue-tracker#159 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 65aa64a commit 483630f

10 files changed

Lines changed: 1762 additions & 0 deletions

File tree

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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+
# broadcastScalarLike
22+
23+
> Broadcast a scalar value to an [ndarray][@stdlib/ndarray/base/ctor] having the same shape and [data type][@stdlib/ndarray/dtypes] as a provided input [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 broadcastScalarLike = require( '@stdlib/ndarray/base/broadcast-scalar-like' );
41+
```
42+
43+
#### broadcastScalarLike( x, value )
44+
45+
Broadcasts a scalar value to an [ndarray][@stdlib/ndarray/base/ctor] having the same shape and [data type][@stdlib/ndarray/dtypes] as a provided input [ndarray][@stdlib/ndarray/base/ctor].
46+
47+
```javascript
48+
var zeros = require( '@stdlib/ndarray/base/zeros' );
49+
var getDType = require( '@stdlib/ndarray/dtype' );
50+
51+
var x = zeros( 'float32', [ 2, 2 ], 'row-major' );
52+
// returns <ndarray>[ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ]
53+
54+
var y = broadcastScalarLike( x, 1.0 );
55+
// returns <ndarray>[ [ 1.0, 1.0 ], [ 1.0, 1.0 ] ]
56+
57+
var dt = String( getDType( y ) );
58+
// returns 'float32'
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+
- Along with data type, shape, and order, the function infers the "class" of the returned [ndarray][@stdlib/ndarray/base/ctor] from the provided [ndarray][@stdlib/ndarray/base/ctor]. 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].
72+
- If `value` is a number and the [data type][@stdlib/ndarray/dtypes] of the input ndarray is a complex [data type][@stdlib/ndarray/dtypes], the function returns an [ndarray][@stdlib/ndarray/base/ctor] containing a complex number whose real component equals the provided scalar `value` and whose imaginary component is zero.
73+
- The returned [ndarray][@stdlib/ndarray/base/ctor] is a view on an [ndarray][@stdlib/ndarray/base/ctor] data buffer containing a single element. The view is **not** contiguous. As more than one element of a returned view may refer to the same memory location, writing to the view may affect multiple elements. If you need to write to the returned [ndarray][@stdlib/ndarray/base/ctor], copy the [ndarray][@stdlib/ndarray/base/ctor] **before** performing operations which may mutate elements.
74+
75+
</section>
76+
77+
<!-- /.notes -->
78+
79+
<!-- Package usage examples. -->
80+
81+
<section class="examples">
82+
83+
## Examples
84+
85+
<!-- eslint no-undef: "error" -->
86+
87+
```javascript
88+
var dtypes = require( '@stdlib/ndarray/dtypes' );
89+
var empty = require( '@stdlib/ndarray/base/empty' );
90+
var broadcastScalarLike = require( '@stdlib/ndarray/base/broadcast-scalar-like' );
91+
92+
// Get a list of data types:
93+
var dt = dtypes( 'integer_and_generic' );
94+
95+
// Generate broadcasted arrays...
96+
var x;
97+
var y;
98+
var i;
99+
for ( i = 0; i < dt.length; i++ ) {
100+
x = empty( dt[ i ], [ 2, 2 ], 'row-major' );
101+
y = broadcastScalarLike( x, i );
102+
console.log( y.get( 0, 0 ) );
103+
}
104+
```
105+
106+
</section>
107+
108+
<!-- /.examples -->
109+
110+
<!-- 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. -->
111+
112+
<section class="references">
113+
114+
</section>
115+
116+
<!-- /.references -->
117+
118+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
119+
120+
<section class="related">
121+
122+
</section>
123+
124+
<!-- /.related -->
125+
126+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
127+
128+
<section class="links">
129+
130+
[@stdlib/ndarray/base/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/ctor
131+
132+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
133+
134+
[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes
135+
136+
</section>
137+
138+
<!-- /.links -->

0 commit comments

Comments
 (0)