MCPcopy Create free account
hub / github.com/numpy/numpy / identity

Function identity

numpy/matlib.py:151–185  ·  view source on GitHub ↗

Returns the square identity matrix of given size. Parameters ---------- n : int Size of the returned identity matrix. dtype : data-type, optional Data-type of the output. Defaults to ``float``. Returns ------- out : matrix `n` x `n` matrix w

(n,dtype=None)

Source from the content-addressed store, hash-verified

149 return a
150
151def identity(n,dtype=None):
152 """
153 Returns the square identity matrix of given size.
154
155 Parameters
156 ----------
157 n : int
158 Size of the returned identity matrix.
159 dtype : data-type, optional
160 Data-type of the output. Defaults to ``float``.
161
162 Returns
163 -------
164 out : matrix
165 `n` x `n` matrix with its main diagonal set to one,
166 and all other elements zero.
167
168 See Also
169 --------
170 numpy.identity : Equivalent array function.
171 matlib.eye : More general matrix identity function.
172
173 Examples
174 --------
175 >>> import numpy.matlib
176 >>> np.matlib.identity(3, dtype=int)
177 matrix([[1, 0, 0],
178 [0, 1, 0],
179 [0, 0, 1]])
180
181 """
182 a = array([1]+n*[0], dtype=dtype)
183 b = empty((n, n), dtype=dtype)
184 b.flat = a
185 return b
186
187def eye(n,M=None, k=0, dtype=float, order='C'):
188 """

Callers

nothing calls this directly

Calls 2

emptyFunction · 0.70
arrayFunction · 0.50

Tested by

no test coverage detected