Skip to content

Commit

Permalink
Merge pull request #26130 from eendebakpt/atleast_1d
Browse files Browse the repository at this point in the history
ENH: Optimize performance of np.atleast_1d
  • Loading branch information
ngoldbaum committed May 6, 2024
2 parents f9e8fd5 + f6cb2a9 commit 0310e17
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
16 changes: 16 additions & 0 deletions benchmarks/benchmarks/bench_shape_base.py
Expand Up @@ -152,3 +152,19 @@ def time_scalar_kron(self):

def time_mat_kron(self):
np.kron(self.large_mat, self.large_mat)

class AtLeast1D(Benchmark):
"""Benchmarks for np.atleast_1d"""

def setup(self):
self.x = np.array([1, 2, 3])
self.zero_d = np.float64(1.)

def time_atleast_1d(self):
np.atleast_1d(self.x, self.x, self.x)

def time_atleast_1d_reshape(self):
np.atleast_1d(self.zero_d, self.zero_d, self.zero_d)

def time_atleast_1d_single_argument(self):
np.atleast_1d(self.x)
18 changes: 9 additions & 9 deletions numpy/_core/shape_base.py
Expand Up @@ -60,18 +60,18 @@ def atleast_1d(*arys):
(array([1]), array([3, 4]))
"""
if len(arys) == 1:
result = asanyarray(arys[0])
if result.ndim == 0:
result = result.reshape(1)
return result
res = []
for ary in arys:
ary = asanyarray(ary)
if ary.ndim == 0:
result = ary.reshape(1)
else:
result = ary
result = asanyarray(ary)
if result.ndim == 0:
result = result.reshape(1)
res.append(result)
if len(res) == 1:
return res[0]
else:
return tuple(res)
return tuple(res)


def _atleast_2d_dispatcher(*arys):
Expand Down

0 comments on commit 0310e17

Please sign in to comment.