Create an ndarray filled according to a callback function and having a specified shape and data type.
var fullBy = require( '@stdlib/ndarray/base/full-by' );Returns an ndarray filled according to a callback function and having a specified shape and data type.
var getDType = require( '@stdlib/ndarray/dtype' );
function clbk() {
return 10.0;
}
var arr = fullBy( 'float64', [ 2, 2 ], 'row-major', clbk );
// returns <ndarray>[ [ 10.0, 10.0 ], [ 10.0, 10.0 ] ]
var dt = String( getDType( arr ) );
// returns 'float64'This function accepts the following arguments:
- dtype: underlying data type.
- shape: array shape.
- order: specifies whether an ndarray is
'row-major'(C-style) or'column-major'(Fortran-style). - clbk: callback function.
- thisArg: callback function execution context (optional).
The callback function is provided the following arguments:
- indices: current array element indices.
To set the callback function execution context, provide a thisArg.
var getDType = require( '@stdlib/ndarray/dtype' );
function clbk( indices ) {
return this.value * ( indices[ 0 ] + indices[ 1 ] ); // eslint-disable-line no-invalid-this
}
var ctx = {
'value': 10.0
};
var arr = fullBy( 'float64', [ 2, 2 ], 'row-major', clbk, ctx );
// returns <ndarray>[ [ 0.0, 10.0 ], [ 10.0, 20.0 ] ]
var dt = String( getDType( arr ) );
// returns 'float64'var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
var ndarray2array = require( '@stdlib/ndarray/to-array' );
var dtypes = require( '@stdlib/ndarray/dtypes' );
var fullBy = require( '@stdlib/ndarray/base/full-by' );
// Get a list of data types:
var dt = dtypes( 'integer_and_generic' );
// Generate filled arrays...
var arr;
var i;
for ( i = 0; i < dt.length; i++ ) {
arr = fullBy( dt[ i ], [ 2, 2 ], 'row-major', discreteUniform( -100, 100 ) );
console.log( ndarray2array( arr ) );
}