MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / identity

Function identity

matrix/matrix_operation.py:83–92  ·  view source on GitHub ↗

:param n: dimension for nxn matrix :type n: int :return: Identity matrix of shape [n, n] >>> identity(3) [[1, 0, 0], [0, 1, 0], [0, 0, 1]]

(n: int)

Source from the content-addressed store, hash-verified

81
82
83def identity(n: int) -> list[list[int]]:
84 """
85 :param n: dimension for nxn matrix
86 :type n: int
87 :return: Identity matrix of shape [n, n]
88 >>> identity(3)
89 [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
90 """
91 n = int(n)
92 return [[int(row == column) for column in range(n)] for row in range(n)]
93
94
95def transpose(

Callers 1

mainFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected