All files main.js

100% Statements 139/139
100% Branches 19/19
100% Functions 2/2
100% Lines 139/139

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 1401x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 19x 19x 19x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 12x 12x 44x 56x 10x 10x 34x 56x 9x 9x 56x 25x 25x 25x 34x 34x 34x 34x 34x 34x 34x 34x 56x 11x 56x 13x 13x 24x 24x 24x 56x 11x 11x 56x 13x 13x 24x 56x 33x 33x 33x 24x 24x 56x 1x 1x  
/**
* @license Apache-2.0
*
* Copyright (c) 2026 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
 
'use strict';
 
// MODULES //
 
var isAccessorArray = require( '@stdlib/array/base/assert/is-accessor-array' );
var accessorSetter = require( '@stdlib/array/base/accessor-setter' );
var isComplexDataType = require( '@stdlib/ndarray/base/assert/is-complex-floating-point-data-type' );
var isInteger = require( '@stdlib/assert/is-integer' );
var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' );
var ctors = require( '@stdlib/complex/ctors' );
var max = require( '@stdlib/math/base/special/max' );
var min = require( '@stdlib/math/base/special/min' );
var format = require( '@stdlib/string/format' );
var zeros = require( '@stdlib/ndarray/base/zeros' );
 
 
// FUNCTIONS //
 
/**
* Default buffer setter function.
*
* @private
* @param {ArrayBufferLike} buf - array buffer
* @param {Number} idx - array index
* @param {Number|ComplexLike} v - value
*/
function setter( buf, idx, v ) {
	buf[ idx ] = v;
}
 
 
// MAIN //
 
/**
* Returns a two-dimensional ndarray with ones on the kth diagonal and zeros elsewhere.
*
* @param {*} dtype - output array data type
* @param {NonNegativeInteger} rows - output array number of rows
* @param {NonNegativeInteger} cols - output array number of columns
* @param {IntegerNumber} k - diagonal index. positive refers to upper diagonal, and negative refers to lower diagonal
* @param {string} order - memory layout (either row-major or column-major)
* @throws {TypeError} first argument must be a recognized data type
* @throws {TypeError} second and third arguments must be non-negative integers
* @throws {TypeError} fourth argument must be an integer
* @returns {ndarray} ndarray
*
* @example
* 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'
*/
function eye( dtype, rows, cols, k, order ) {
	var ctor;
	var out;
	var len;
	var buf;
	var set;
	var sr;
	var sc;
	var ix;
	var dx;
	var i;
	var v;
	var r;
	var c;
 
	if ( !isNonNegativeInteger( rows ) || !isNonNegativeInteger( cols ) ) {
		throw new TypeError( format('invalid arguments. Second and third arguments must be non-negative integers. Rows: %s, Columns: %s.', rows, cols) );
	}
 
	if ( !isInteger( k ) ) {
		throw new TypeError( format('invalid arguments. The fourth argument must be an integer. Diagonal index: %s.', k) );
	}
 
	if ( k < 0 ) {
		r = -k;
		c = 0;
	} else {
		r = 0;
		c = k;
	}
 
	out = zeros( dtype, [ rows, cols ], order );
 
	sr = out.strides[ 0 ];
	sc = out.strides[ 1 ];
	ix = out.offset + (r * sr) + (c * sc); // offset + (row * row_stride) + (col * col_stride)
	dx = sr + sc;
 
	if ( isAccessorArray( out.data ) ) {
		set = accessorSetter( dtype );
	} else {
		set = setter;
	}
	buf = out.data;
	len = max( 0, min( rows - r, cols - c ) );
 
	if ( isComplexDataType( dtype ) ) {
		ctor = ctors( dtype );
		v = new ctor( 1.0, 0.0 );
	} else {
		v = 1.0;
	}
 
	for ( i = 0; i < len; i++ ) {
		set( buf, ix, v );
		ix += dx;
	}
 
	return out;
}
 
module.exports = eye;