diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2009-08-12 15:49:49 +0000 |
---|---|---|
committer | Charles Harris <charlesr.harris@gmail.com> | 2009-08-12 15:49:49 +0000 |
commit | 4be3297310b904c71ebc27c8a374997ce49e242c (patch) | |
tree | 78aa7464b9dcd25ba8976460f39c0d882c0d754a /numpy/core/numeric.py | |
parent | da19f3edc5c8995afc821ddc949b386e781e0b6a (diff) | |
download | numpy-4be3297310b904c71ebc27c8a374997ce49e242c.tar.gz |
Make identity function faster. Closes ticket #1193.
Diffstat (limited to 'numpy/core/numeric.py')
-rw-r--r-- | numpy/core/numeric.py | 12 |
1 files changed, 3 insertions, 9 deletions
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): """ |