Skip to content

Latest commit

 

History

History
166 lines (108 loc) · 5.45 KB

File metadata and controls

166 lines (108 loc) · 5.45 KB

oneTo

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

Usage

var oneTo = require( '@stdlib/blas/ext/one-to' );

oneTo( shape[, options] )

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

var x = oneTo( [ 2, 3 ] );
// returns <ndarray>[ [ 1.0, 2.0, 3.0 ], [ 1.0, 2.0, 3.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). Default: 'row-major'.
  • 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 function 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 = oneTo( [ 2, 2 ], {
    'dims': [ 0, 1 ]
});
// returns <ndarray>[ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ]

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

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

oneTo.assign( x[, options] )

Fills an ndarray with linearly spaced numeric elements which increment by 1 starting from one 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 = oneTo.assign( x );
// returns <ndarray>[ [ 1.0, 2.0, 3.0 ], [ 1.0, 2.0, 3.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 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 oneTo = require( '@stdlib/blas/ext/one-to' );

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

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

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