Skip to content

Commit

Permalink
Merge pull request numba#7700 from DhruvPatel01/np_ascontiguousarray_…
Browse files Browse the repository at this point in the history
…scalar

Support for scalar arguments in Np.ascontiguousarray
  • Loading branch information
sklam authored Jan 19, 2022
2 parents 0e2e32d + 4a464b0 commit ef1ba4c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
24 changes: 24 additions & 0 deletions numba/np/arrayobj.py
Original file line number Diff line number Diff line change
Expand Up @@ -4331,6 +4331,30 @@ def array_ascontiguousarray(context, builder, sig, args):
return _as_layout_array(context, builder, sig, args, output_layout='C')


@overload(np.ascontiguousarray)
def array_ascontiguousarray_scalar(a):
"""
This is an implementation for scalar.
For arrays, see `array_ascontiguousarray`.
"""
if isinstance(a, (types.Number, types.Boolean,)):
def impl(a):
return np.ascontiguousarray(np.array(a))
return impl


@overload(np.asfortranarray)
def array_asfortranarray_scalar(a):
"""
This is an implementation for scalar.
For arrays, see `array_asfortranarray`.
"""
if isinstance(a, (types.Number, types.Boolean,)):
def impl(a):
return np.asfortranarray(np.array(a))
return impl


@lower_builtin("array.astype", types.Array, types.DTypeSpec)
@lower_builtin("array.astype", types.Array, types.StringLiteral)
def array_astype(context, builder, sig, args):
Expand Down
11 changes: 11 additions & 0 deletions numba/tests/test_array_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -596,11 +596,22 @@ def test_array_copy(self):
def test_np_copy(self):
self.check_layout_dependent_func(np_copy)

def check_ascontiguousarray_scalar(self, pyfunc):
def check_scalar(x):
cres = compile_isolated(pyfunc, (typeof(x), ))
expected = pyfunc(x)
got = cres.entry_point(x)
self.assertPreciseEqual(expected, got)
for x in [42, 42.0, 42j, np.float32(42), np.float64(42), True]:
check_scalar(x)

def test_np_asfortranarray(self):
self.check_layout_dependent_func(np_asfortranarray)
self.check_ascontiguousarray_scalar(np_asfortranarray)

def test_np_ascontiguousarray(self):
self.check_layout_dependent_func(np_ascontiguousarray)
self.check_ascontiguousarray_scalar(np_ascontiguousarray)

def check_np_frombuffer_allocated(self, pyfunc):
def run(shape):
Expand Down

0 comments on commit ef1ba4c

Please sign in to comment.