Skip to content

Latest commit

 

History

History
167 lines (109 loc) · 5.58 KB

File metadata and controls

167 lines (109 loc) · 5.58 KB

zeroTo

Return a new ndarray filled with linearly spaced numeric elements which increment by 1 starting from zero along one or more ndarray dimensions.

Usage

var zeroTo = require( '@stdlib/blas/ext/zero-to' );

zeroTo( shape[, options] )

Returns a new ndarray filled with linearly spaced numeric elements which increment by 1 starting from zero along one or more ndarray dimensions.

var x = zeroTo( [ 2, 3 ] );
// returns <ndarray>[ [ 0.0, 1.0, 2.0 ], [ 0.0, 1.0, 2.0 ] ]

The function has the following parameters:

  • shape: array shape.
  • options: function options (optional).

The function accepts the following options:

  • dims: list of dimensions over which to perform operation. If not provided, the function generates values along the last dimension. Default: [-1].
  • dtype: output ndarray data type. Must be a numeric or "generic" data type. Default: 'float64'.
  • order: specifies whether an ndarray is 'row-major' (C-style) or 'column-major' (Fortran-style).
  • mode: specifies how to handle indices which exceed array dimensions (see ndarray). Default: 'throw'.
  • submode: a mode array which specifies for each dimension how to handle subscripts which exceed array dimensions (see ndarray). If provided fewer modes than dimensions, the constructor recycles modes using modulo arithmetic. Default: [ options.mode ].

By default, the function generates values along the last dimension of an output ndarray. To perform the operation over specific dimensions, provide a dims option.

var x = zeroTo( [ 2, 2 ], {
    'dims': [ 0, 1 ]
});
// returns <ndarray>[ [ 0.0, 1.0 ], [ 2.0, 3.0 ] ]

To specify the output ndarray data type, provide a dtype option.

var x = zeroTo( [ 3 ], {
    'dtype': 'float32'
});
// returns <ndarray>[ 0.0, 1.0, 2.0 ]

zeroTo.assign( x[, options] )

Fills an ndarray with linearly spaced numeric elements which increment by 1 starting from zero along one or more ndarray dimensions.

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

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

var out = zeroTo.assign( x );
// returns <ndarray>[ [ 0.0, 1.0, 2.0 ], [ 0.0, 1.0, 2.0 ] ]

var bool = ( x === out );
// returns true

The function has the following parameters:

  • x: input ndarray. Must have a numeric or "generic" data type.
  • options: function options (optional).

The function accepts the following options:

  • dims: list of dimensions over which to perform operation. If not provided, the function generates values along the last dimension. Default: [-1].

Notes

  • The input ndarray is filled in-place (i.e., the input ndarray is mutated).
  • The function iterates over ndarray elements according to the memory layout of the input ndarray. Accordingly, performance degradation is possible when operating over multiple dimensions of a large non-contiguous multi-dimensional input ndarray. In such scenarios, one may want to copy an input ndarray to contiguous memory before performing the operation.

Examples

var ndarray2array = require( '@stdlib/ndarray/to-array' );
var zeroTo = require( '@stdlib/blas/ext/zero-to' );

// Create a new ndarray:
var x = zeroTo( [ 5, 5 ], {
    'dtype': 'generic'
});
console.log( ndarray2array( x ) );

// Generate values over a specific dimension:
x = zeroTo( [ 5, 5 ], {
    'dtype': 'generic',
    'dims': [ 0 ]
});
console.log( ndarray2array( x ) );

// Generate values over all dimensions:
x = zeroTo( [ 5, 5 ], {
    'dtype': 'generic',
    'dims': [ 0, 1 ]
});
console.log( ndarray2array( x ) );