Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Optimize performance of np.atleast_1d #26130

Merged
merged 4 commits into from May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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