Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
188 changes: 188 additions & 0 deletions lib/node_modules/@stdlib/ndarray/broadcast-scalar-like/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
<!--

@license Apache-2.0

Copyright (c) 2026 The Stdlib Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->

# broadcastScalarLike

> Broadcast a scalar value to an [ndarray][@stdlib/ndarray/ctor] having the same shape and [data-type][@stdlib/ndarray/dtypes] as a provided input [ndarray][@stdlib/ndarray/ctor].

<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->

<section class="intro">

</section>

<!-- /.intro -->

<!-- Package usage documentation. -->

<section class="usage">

## Usage

```javascript
var broadcastScalarLike = require( '@stdlib/ndarray/broadcast-scalar-like' );
```

#### broadcastScalarLike( x, value\[, options] )

Broadcasts a scalar value to an [ndarray][@stdlib/ndarray/ctor] having the same shape and [data-type][@stdlib/ndarray/dtypes] as a provided input [ndarray][@stdlib/ndarray/ctor].

```javascript
var getDType = require( '@stdlib/ndarray/dtype' );
var empty = require( '@stdlib/ndarray/empty' );

var x = empty( [ 2, 2 ], {
'dtype': 'float32'
});
// returns <ndarray>

var y = broadcastScalarLike( x, 1.0 );
// returns <ndarray>[ [ 1.0, 1.0 ], [ 1.0, 1.0 ] ]

var dt = String( getDType( y ) );
// returns 'float32'
```

The function accepts the following arguments:

- **x**: input [ndarray][@stdlib/ndarray/ctor].
- **value**: scalar value.

The function accepts the following options:

- **dtype**: output array [data type][@stdlib/ndarray/dtypes]. By default, the output array has the same [data type][@stdlib/ndarray/dtypes] as the provided input ndarray.
- **shape**: output array shape. By default, the output array has the same shape as the provided input ndarray.
- **order**: array order (i.e., memory layout). Must be either `row-major` (C-style) or `column-major` (Fortran-style). By default, the output array has the same order as the provided input ndarray.
- **readonly**: boolean indicating whether an array should be **read-only**. Default: `false`.

To explicitly specify the [data type][@stdlib/ndarray/dtypes] of the returned [ndarray][@stdlib/ndarray/ctor], provide a `dtype` option.

```javascript
var getDType = require( '@stdlib/ndarray/dtype' );
var zeros = require( '@stdlib/ndarray/zeros' );

var x = zeros( [ 2, 2 ] );
// returns <ndarray>[ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ]

var y = broadcastScalarLike( x, 1.0, {
'dtype': 'float32'
});
// returns <ndarray>[ [ 1.0, 1.0 ], [ 1.0, 1.0 ] ]

var dt = String( getDType( y ) );
// returns 'float32'
```

To explicitly specify the shape of the returned [ndarray][@stdlib/ndarray/ctor], provide a `shape` option.

```javascript
var zeros = require( '@stdlib/ndarray/zeros' );

var x = zeros( [ 2, 2 ] );
// returns <ndarray>[ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ]

var y = broadcastScalarLike( x, 1.0, {
'shape': [ 3, 3 ]
});
// returns <ndarray>[ [ 1.0, 1.0, 1.0 ], [ 1.0, 1.0, 1.0 ], [ 1.0, 1.0, 1.0 ] ]
```

</section>

<!-- /.usage -->

<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="notes">

## Notes

- If `value` is a number and the resolved [data type][@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.
- The function does not guard against precision loss when `value` is a number and the resolved [data type][@stdlib/ndarray/dtypes] is an integer [data type][@stdlib/ndarray/dtypes].

</section>

<!-- /.notes -->

<!-- Package usage examples. -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```javascript
var dtypes = require( '@stdlib/ndarray/dtypes' );
var empty = require( '@stdlib/ndarray/empty' );
var broadcastScalarLike = require( '@stdlib/ndarray/broadcast-scalar-like' );

// Get a list of data types:
var dt = dtypes( 'integer_and_generic' );

// Create an input array:
var x = empty( [ 2, 2 ] );

// Generate broadcasted arrays...
var y;
var i;
for ( i = 0; i < dt.length; i++ ) {
y = broadcastScalarLike( x, i, {
'dtype': dt[ i ]
});
console.log( y.get( 0, 1 ) );
}
```

</section>

<!-- /.examples -->

<!-- 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. -->

<section class="references">

</section>

<!-- /.references -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor

[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes

<!-- <related-links> -->

<!-- </related-links> -->

</section>

<!-- /.links -->
Loading
Loading