From 4be3297310b904c71ebc27c8a374997ce49e242c Mon Sep 17 00:00:00 2001 From: Charles Harris Date: Wed, 12 Aug 2009 15:49:49 +0000 Subject: Make identity function faster. Closes ticket #1193. --- numpy/core/numeric.py | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) (limited to 'numpy/core/numeric.py') diff --git a/numpy/core/numeric.py b/numpy/core/numeric.py index 31b021b71..c961bcc0f 100644 --- a/numpy/core/numeric.py +++ b/numpy/core/numeric.py @@ -1692,15 +1692,9 @@ def identity(n, dtype=None): [ 0., 0., 1.]]) """ - a = array([1]+n*[0],dtype=dtype) - b = empty((n,n),dtype=dtype) - - # Note that this assignment depends on the convention that since the a - # array is shorter than the flattened b array, then the a array will - # be repeated until it is the appropriate size. Given a's construction, - # this nicely sets the diagonal to all ones. - b.flat = a - return b + a = zeros((n,n), dtype=dtype) + a.flat[::n+1] = 1 + return a def allclose(a, b, rtol=1.e-5, atol=1.e-8): """ -- cgit v1.2.1