Create a two-dimensional ndarray with ones on the kth diagonal and zeros elsewhere
var eye = require( '@stdlib/ndarray/base/eye' );Returns a two-dimensional ndarray with ones on the kth diagonal and zeros elsewhere.
var getShape = require( '@stdlib/ndarray/shape' );
var getDType = require( '@stdlib/ndarray/dtype' );
var arr = eye( 'float64', 3, 3, 0, 'row-major');
// returns <ndarray>[ [ 1.0, 0.0, 0.0 ], [ 0.0, 1.0, 0.0 ], [ 0.0, 0.0, 1.0 ] ]
var sh = getShape( arr );
// returns [ 3, 3 ]
var dt = String( getDType( arr ) );
// returns 'float64'The function accepts the following arguments:
- If the provided data type is complex, the function returns an ndarray whose kth diagonal is filled with complex numbers whose real component equals one and imaginary component equals zero.
- If
kis greater than the number of columns or less than the number of negative rows, the function returns an ndarray filled with zeros.
var eye = require( '@stdlib/ndarray/base/eye' );
var ndarray2array = require( '@stdlib/ndarray/to-array' );
var arr = eye( 'float64', 3, 3, 1, 'row-major');
console.log( ndarray2array( arr ) );
arr = eye( 'complex64', 3, 3, 1, 'row-major');
console.log( ndarray2array( arr ) );