Skip to content

Latest commit

 

History

History
136 lines (80 loc) · 3.77 KB

File metadata and controls

136 lines (80 loc) · 3.77 KB

eye

Create a two-dimensional ndarray with ones on the kth diagonal and zeros elsewhere

Usage

var eye = require( '@stdlib/ndarray/base/eye' );

eye( dtype, rows, cols, k, order )

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:

  • dtype: underlying data type.
  • rows: number of rows.
  • cols: number of columns.
  • k: diagonal index. positive refers to upper diagonal, and negative refers to lower diagonal.
  • order: specifies whether an ndarray is 'row-major' (C-style) or 'column-major' (Fortran-style).

Notes

  • 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 k is greater than the number of columns or less than the number of negative rows, the function returns an ndarray filled with zeros.

Examples

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