Skip to content

Latest commit

 

History

History
188 lines (119 loc) · 5.6 KB

File metadata and controls

188 lines (119 loc) · 5.6 KB

broadcastScalarLike

Broadcast a scalar value to an ndarray having the same shape and data-type as a provided input ndarray.

Usage

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

broadcastScalarLike( x, value[, options] )

Broadcasts a scalar value to an ndarray having the same shape and data-type as a provided input ndarray.

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.
  • value: scalar value.

The function accepts the following options:

  • dtype: output array data type. By default, the output array has the same data type 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 of the returned ndarray, provide a dtype option.

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, provide a shape option.

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 ] ]

Notes

  • If value is a number and the resolved data type is a complex data type, the function returns an ndarray 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 is an integer data type.

Examples

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 ) );
}