We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
cupy.ascontiguousarray behavior different from numpy
```py
>>> np.ascontiguousarray(np.array(3))
array([3])
>>> cp.ascontiguousarray(cp.array(3))
array(3)
```

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `cupy/core/__init__.py`
Content:
```
1 from cupy.core import core  # NOQA
2 from cupy.core import internal  # NOQA
3 
4 
5 # import class and function
6 from cupy.core._errors import _AxisError  # NOQA
7 from cupy.core._kernel import create_reduction_func  # NOQA
8 from cupy.core._kernel import create_ufunc  # NOQA
9 from cupy.core._kernel import ElementwiseKernel  # NOQA
10 from cupy.core._kernel import ReductionKernel  # NOQA
11 from cupy.core._kernel import ufunc  # NOQA
12 from cupy.core._routines_manipulation import array_split  # NOQA
13 from cupy.core._routines_manipulation import broadcast  # NOQA
14 from cupy.core._routines_manipulation import broadcast_to  # NOQA
15 from cupy.core._routines_manipulation import concatenate_method  # NOQA
16 from cupy.core._routines_manipulation import moveaxis  # NOQA
17 from cupy.core._routines_manipulation import rollaxis  # NOQA
18 from cupy.core._routines_manipulation import size  # NOQA'
19 from cupy.core._routines_math import absolute  # NOQA
20 from cupy.core._routines_math import add  # NOQA
21 from cupy.core._routines_math import angle  # NOQA
22 from cupy.core._routines_math import conj  # NOQA
23 from cupy.core._routines_math import divide  # NOQA
24 from cupy.core._routines_math import floor_divide  # NOQA
25 from cupy.core._routines_math import imag  # NOQA
26 from cupy.core._routines_math import multiply  # NOQA
27 from cupy.core._routines_math import negative  # NOQA
28 from cupy.core._routines_math import power  # NOQA
29 from cupy.core._routines_math import real  # NOQA
30 from cupy.core._routines_math import remainder  # NOQA
31 from cupy.core._routines_math import sqrt  # NOQA
32 from cupy.core._routines_math import subtract  # NOQA
33 from cupy.core._routines_math import true_divide  # NOQA
34 from cupy.core._routines_statistics import nanmax  # NOQA
35 from cupy.core._routines_statistics import nanmin  # NOQA
36 from cupy.core.core import array  # NOQA
37 from cupy.core.core import ascontiguousarray  # NOQA
38 from cupy.core.core import asfortranarray  # NOQA
39 from cupy.core.core import bitwise_and  # NOQA
40 from cupy.core.core import bitwise_or  # NOQA
41 from cupy.core.core import bitwise_xor  # NOQA
42 from cupy.core.core import create_comparison  # NOQA
43 from cupy.core.core import divmod  # NOQA
44 from cupy.core.core import dot  # NOQA
45 from cupy.core.core import elementwise_copy  # NOQA
46 from cupy.core.core import elementwise_copy_where  # NOQA
47 from cupy.core.core import equal  # NOQA
48 from cupy.core.core import greater  # NOQA
49 from cupy.core.core import greater_equal  # NOQA
50 from cupy.core.core import invert  # NOQA
51 from cupy.core.core import left_shift  # NOQA
52 from cupy.core.core import less  # NOQA
53 from cupy.core.core import less_equal  # NOQA
54 from cupy.core.core import matmul  # NOQA
55 from cupy.core.core import ndarray  # NOQA
56 from cupy.core.core import not_equal  # NOQA
57 from cupy.core.core import right_shift  # NOQA
58 from cupy.core.core import tensordot_core  # NOQA
59 from cupy.core.dlpack import fromDlpack  # NOQA
60 from cupy.core.internal import complete_slice  # NOQA
61 from cupy.core.internal import get_size  # NOQA
62 from cupy.core.raw import RawKernel  # NOQA
63
```

Path: `cupy/statistics/order.py`
Content:
```
1 import warnings
2 
3 import cupy
4 from cupy import core
5 from cupy.core import _routines_statistics as _statistics
6 from cupy.core import fusion
7 from cupy.logic import content
8 
9 
10 def amin(a, axis=None, out=None, keepdims=False, dtype=None):
11     """Returns the minimum of an array or the minimum along an axis.
12 
13     .. note::
14 
15        When at least one element is NaN, the corresponding min value will be
16        NaN.
17 
18     Args:
19         a (cupy.ndarray): Array to take the minimum.
20         axis (int): Along which axis to take the minimum. The flattened array
21             is used by default.
22         out (cupy.ndarray): Output array.
23         keepdims (bool): If ``True``, the axis is remained as an axis of
24             size one.
25         dtype: Data type specifier.
26 
27     Returns:
28         cupy.ndarray: The minimum of ``a``, along the axis if specified.
29 
30     .. seealso:: :func:`numpy.amin`
31 
32     """
33     if fusion._is_fusing():
34         if keepdims:
35             raise NotImplementedError(
36                 'cupy.amin does not support `keepdims` in fusion yet.')
37         return fusion._call_reduction(_statistics.amin,
38                                       a, axis=axis, dtype=dtype, out=out)
39 
40     # TODO(okuta): check type
41     return a.min(axis=axis, dtype=dtype, out=out, keepdims=keepdims)
42 
43 
44 def amax(a, axis=None, out=None, keepdims=False, dtype=None):
45     """Returns the maximum of an array or the maximum along an axis.
46 
47     .. note::
48 
49        When at least one element is NaN, the corresponding min value will be
50        NaN.
51 
52     Args:
53         a (cupy.ndarray): Array to take the maximum.
54         axis (int): Along which axis to take the maximum. The flattened array
55             is used by default.
56         out (cupy.ndarray): Output array.
57         keepdims (bool): If ``True``, the axis is remained as an axis of
58             size one.
59         dtype: Data type specifier.
60 
61     Returns:
62         cupy.ndarray: The maximum of ``a``, along the axis if specified.
63 
64     .. seealso:: :func:`numpy.amax`
65 
66     """
67     if fusion._is_fusing():
68         if keepdims:
69             raise NotImplementedError(
70                 'cupy.amax does not support `keepdims` in fusion yet.')
71         return fusion._call_reduction(_statistics.amax,
72                                       a, axis=axis, dtype=dtype, out=out)
73 
74     # TODO(okuta): check type
75     return a.max(axis=axis, dtype=dtype, out=out, keepdims=keepdims)
76 
77 
78 def nanmin(a, axis=None, out=None, keepdims=False):
79     """Returns the minimum of an array along an axis ignoring NaN.
80 
81     When there is a slice whose elements are all NaN, a :class:`RuntimeWarning`
82     is raised and NaN is returned.
83 
84     Args:
85         a (cupy.ndarray): Array to take the minimum.
86         axis (int): Along which axis to take the minimum. The flattened array
87             is used by default.
88         out (cupy.ndarray): Output array.
89         keepdims (bool): If ``True``, the axis is remained as an axis of
90             size one.
91 
92     Returns:
93         cupy.ndarray: The minimum of ``a``, along the axis if specified.
94 
95     .. seealso:: :func:`numpy.nanmin`
96 
97     """
98     res = core.nanmin(a, axis=axis, out=out, keepdims=keepdims)
99     if content.isnan(res).any():
100         warnings.warn('All-NaN slice encountered', RuntimeWarning)
101     return res
102 
103 
104 def nanmax(a, axis=None, out=None, keepdims=False):
105     """Returns the maximum of an array along an axis ignoring NaN.
106 
107     When there is a slice whose elements are all NaN, a :class:`RuntimeWarning`
108     is raised and NaN is returned.
109 
110     Args:
111         a (cupy.ndarray): Array to take the maximum.
112         axis (int): Along which axis to take the maximum. The flattened array
113             is used by default.
114         out (cupy.ndarray): Output array.
115         keepdims (bool): If ``True``, the axis is remained as an axis of
116             size one.
117 
118     Returns:
119         cupy.ndarray: The maximum of ``a``, along the axis if specified.
120 
121     .. seealso:: :func:`numpy.nanmax`
122 
123     """
124     res = core.nanmax(a, axis=axis, out=out, keepdims=keepdims)
125     if content.isnan(res).any():
126         warnings.warn('All-NaN slice encountered', RuntimeWarning)
127     return res
128 
129 
130 # TODO(okuta): Implement ptp
131 
132 
133 def percentile(a, q, axis=None, out=None, interpolation='linear',
134                keepdims=False):
135     """Computes the q-th percentile of the data along the specified axis.
136 
137     Args:
138         a (cupy.ndarray): Array for which to compute percentiles.
139         q (float, tuple of floats or cupy.ndarray): Percentiles to compute
140             in the range between 0 and 100 inclusive.
141         axis (int or tuple of ints): Along which axis or axes to compute the
142             percentiles. The flattened array is used by default.
143         out (cupy.ndarray): Output array.
144         interpolation (str): Interpolation method when a quantile lies between
145             two data points. ``linear`` interpolation is used by default.
146             Supported interpolations are``lower``, ``higher``, ``midpoint``,
147             ``nearest`` and ``linear``.
148         keepdims (bool): If ``True``, the axis is remained as an axis of
149             size one.
150 
151     Returns:
152         cupy.ndarray: The percentiles of ``a``, along the axis if specified.
153 
154     .. seealso:: :func:`numpy.percentile`
155 
156     """
157     q = cupy.asarray(q, dtype=a.dtype)
158     if q.ndim == 0:
159         q = q[None]
160         zerod = True
161     else:
162         zerod = False
163     if q.ndim > 1:
164         raise ValueError('Expected q to have a dimension of 1.\n'
165                          'Actual: {0} != 1'.format(q.ndim))
166 
167     if keepdims:
168         if axis is None:
169             keepdim = (1,) * a.ndim
170         else:
171             keepdim = list(a.shape)
172             for ax in axis:
173                 keepdim[ax % a.ndim] = 1
174             keepdim = tuple(keepdim)
175 
176     # Copy a since we need it sorted but without modifying the original array
177     if isinstance(axis, int):
178         axis = axis,
179     if axis is None:
180         ap = a.flatten()
181         nkeep = 0
182     else:
183         # Reduce axes from a and put them last
184         axis = tuple(ax % a.ndim for ax in axis)
185         keep = set(range(a.ndim)) - set(axis)
186         nkeep = len(keep)
187         for i, s in enumerate(sorted(keep)):
188             a = a.swapaxes(i, s)
189         ap = a.reshape(a.shape[:nkeep] + (-1,)).copy()
190 
191     axis = -1
192     ap.sort(axis=axis)
193     Nx = ap.shape[axis]
194     indices = q * 0.01 * (Nx - 1.)  # percents to decimals
195 
196     if interpolation == 'lower':
197         indices = cupy.floor(indices).astype(cupy.int32)
198     elif interpolation == 'higher':
199         indices = cupy.ceil(indices).astype(cupy.int32)
200     elif interpolation == 'midpoint':
201         indices = 0.5 * (cupy.floor(indices) + cupy.ceil(indices))
202     elif interpolation == 'nearest':
203         # TODO(hvy): Implement nearest using around
204         raise ValueError('\'nearest\' interpolation is not yet supported. '
205                          'Please use any other interpolation method.')
206     elif interpolation == 'linear':
207         pass
208     else:
209         raise ValueError('Unexpected interpolation method.\n'
210                          'Actual: \'{0}\' not in (\'linear\', \'lower\', '
211                          '\'higher\', \'midpoint\')'.format(interpolation))
212 
213     if indices.dtype == cupy.int32:
214         ret = cupy.rollaxis(ap, axis)
215         ret = ret.take(indices, axis=0, out=out)
216     else:
217         if out is None:
218             ret = cupy.empty(ap.shape[:-1] + q.shape, dtype=cupy.float64)
219         else:
220             ret = cupy.rollaxis(out, 0, out.ndim)
221 
222         cupy.ElementwiseKernel(
223             'S idx, raw T a, raw int32 offset', 'U ret',
224             '''
225             ptrdiff_t idx_below = floor(idx);
226             U weight_above = idx - idx_below;
227 
228             ptrdiff_t offset_i = _ind.get()[0] * offset;
229             ret = a[offset_i + idx_below] * (1.0 - weight_above)
230               + a[offset_i + idx_below + 1] * weight_above;
231             ''',
232             'percentile_weightnening'
233         )(indices, ap, ap.shape[-1] if ap.ndim > 1 else 0, ret)
234         ret = cupy.rollaxis(ret, -1)  # Roll q dimension back to first axis
235 
236     if zerod:
237         ret = ret.squeeze(0)
238     if keepdims:
239         if q.size > 1:
240             keepdim = (-1,) + keepdim
241         ret = ret.reshape(keepdim)
242 
243     return cupy.ascontiguousarray(ret)
244
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/cupy/core/__init__.py b/cupy/core/__init__.py
--- a/cupy/core/__init__.py
+++ b/cupy/core/__init__.py
@@ -33,6 +33,8 @@
 from cupy.core._routines_math import true_divide  # NOQA
 from cupy.core._routines_statistics import nanmax  # NOQA
 from cupy.core._routines_statistics import nanmin  # NOQA
+from cupy.core.core import _internal_ascontiguousarray  # NOQA
+from cupy.core.core import _internal_asfortranarray  # NOQA
 from cupy.core.core import array  # NOQA
 from cupy.core.core import ascontiguousarray  # NOQA
 from cupy.core.core import asfortranarray  # NOQA
diff --git a/cupy/statistics/order.py b/cupy/statistics/order.py
--- a/cupy/statistics/order.py
+++ b/cupy/statistics/order.py
@@ -240,4 +240,4 @@
             keepdim = (-1,) + keepdim
         ret = ret.reshape(keepdim)
 
-    return cupy.ascontiguousarray(ret)
+    return core._internal_ascontiguousarray(ret)

{"golden_diff": "diff --git a/cupy/core/__init__.py b/cupy/core/__init__.py\n--- a/cupy/core/__init__.py\n+++ b/cupy/core/__init__.py\n@@ -33,6 +33,8 @@\n from cupy.core._routines_math import true_divide  # NOQA\n from cupy.core._routines_statistics import nanmax  # NOQA\n from cupy.core._routines_statistics import nanmin  # NOQA\n+from cupy.core.core import _internal_ascontiguousarray  # NOQA\n+from cupy.core.core import _internal_asfortranarray  # NOQA\n from cupy.core.core import array  # NOQA\n from cupy.core.core import ascontiguousarray  # NOQA\n from cupy.core.core import asfortranarray  # NOQA\ndiff --git a/cupy/statistics/order.py b/cupy/statistics/order.py\n--- a/cupy/statistics/order.py\n+++ b/cupy/statistics/order.py\n@@ -240,4 +240,4 @@\n             keepdim = (-1,) + keepdim\n         ret = ret.reshape(keepdim)\n \n-    return cupy.ascontiguousarray(ret)\n+    return core._internal_ascontiguousarray(ret)\n", "issue": "cupy.ascontiguousarray behavior different from numpy\n```py\r\n>>> np.ascontiguousarray(np.array(3))\r\narray([3])\r\n>>> cp.ascontiguousarray(cp.array(3))\r\narray(3)\r\n```\n", "before_files": [{"content": "from cupy.core import core  # NOQA\nfrom cupy.core import internal  # NOQA\n\n\n# import class and function\nfrom cupy.core._errors import _AxisError  # NOQA\nfrom cupy.core._kernel import create_reduction_func  # NOQA\nfrom cupy.core._kernel import create_ufunc  # NOQA\nfrom cupy.core._kernel import ElementwiseKernel  # NOQA\nfrom cupy.core._kernel import ReductionKernel  # NOQA\nfrom cupy.core._kernel import ufunc  # NOQA\nfrom cupy.core._routines_manipulation import array_split  # NOQA\nfrom cupy.core._routines_manipulation import broadcast  # NOQA\nfrom cupy.core._routines_manipulation import broadcast_to  # NOQA\nfrom cupy.core._routines_manipulation import concatenate_method  # NOQA\nfrom cupy.core._routines_manipulation import moveaxis  # NOQA\nfrom cupy.core._routines_manipulation import rollaxis  # NOQA\nfrom cupy.core._routines_manipulation import size  # NOQA'\nfrom cupy.core._routines_math import absolute  # NOQA\nfrom cupy.core._routines_math import add  # NOQA\nfrom cupy.core._routines_math import angle  # NOQA\nfrom cupy.core._routines_math import conj  # NOQA\nfrom cupy.core._routines_math import divide  # NOQA\nfrom cupy.core._routines_math import floor_divide  # NOQA\nfrom cupy.core._routines_math import imag  # NOQA\nfrom cupy.core._routines_math import multiply  # NOQA\nfrom cupy.core._routines_math import negative  # NOQA\nfrom cupy.core._routines_math import power  # NOQA\nfrom cupy.core._routines_math import real  # NOQA\nfrom cupy.core._routines_math import remainder  # NOQA\nfrom cupy.core._routines_math import sqrt  # NOQA\nfrom cupy.core._routines_math import subtract  # NOQA\nfrom cupy.core._routines_math import true_divide  # NOQA\nfrom cupy.core._routines_statistics import nanmax  # NOQA\nfrom cupy.core._routines_statistics import nanmin  # NOQA\nfrom cupy.core.core import array  # NOQA\nfrom cupy.core.core import ascontiguousarray  # NOQA\nfrom cupy.core.core import asfortranarray  # NOQA\nfrom cupy.core.core import bitwise_and  # NOQA\nfrom cupy.core.core import bitwise_or  # NOQA\nfrom cupy.core.core import bitwise_xor  # NOQA\nfrom cupy.core.core import create_comparison  # NOQA\nfrom cupy.core.core import divmod  # NOQA\nfrom cupy.core.core import dot  # NOQA\nfrom cupy.core.core import elementwise_copy  # NOQA\nfrom cupy.core.core import elementwise_copy_where  # NOQA\nfrom cupy.core.core import equal  # NOQA\nfrom cupy.core.core import greater  # NOQA\nfrom cupy.core.core import greater_equal  # NOQA\nfrom cupy.core.core import invert  # NOQA\nfrom cupy.core.core import left_shift  # NOQA\nfrom cupy.core.core import less  # NOQA\nfrom cupy.core.core import less_equal  # NOQA\nfrom cupy.core.core import matmul  # NOQA\nfrom cupy.core.core import ndarray  # NOQA\nfrom cupy.core.core import not_equal  # NOQA\nfrom cupy.core.core import right_shift  # NOQA\nfrom cupy.core.core import tensordot_core  # NOQA\nfrom cupy.core.dlpack import fromDlpack  # NOQA\nfrom cupy.core.internal import complete_slice  # NOQA\nfrom cupy.core.internal import get_size  # NOQA\nfrom cupy.core.raw import RawKernel  # NOQA\n", "path": "cupy/core/__init__.py"}, {"content": "import warnings\n\nimport cupy\nfrom cupy import core\nfrom cupy.core import _routines_statistics as _statistics\nfrom cupy.core import fusion\nfrom cupy.logic import content\n\n\ndef amin(a, axis=None, out=None, keepdims=False, dtype=None):\n    \"\"\"Returns the minimum of an array or the minimum along an axis.\n\n    .. note::\n\n       When at least one element is NaN, the corresponding min value will be\n       NaN.\n\n    Args:\n        a (cupy.ndarray): Array to take the minimum.\n        axis (int): Along which axis to take the minimum. The flattened array\n            is used by default.\n        out (cupy.ndarray): Output array.\n        keepdims (bool): If ``True``, the axis is remained as an axis of\n            size one.\n        dtype: Data type specifier.\n\n    Returns:\n        cupy.ndarray: The minimum of ``a``, along the axis if specified.\n\n    .. seealso:: :func:`numpy.amin`\n\n    \"\"\"\n    if fusion._is_fusing():\n        if keepdims:\n            raise NotImplementedError(\n                'cupy.amin does not support `keepdims` in fusion yet.')\n        return fusion._call_reduction(_statistics.amin,\n                                      a, axis=axis, dtype=dtype, out=out)\n\n    # TODO(okuta): check type\n    return a.min(axis=axis, dtype=dtype, out=out, keepdims=keepdims)\n\n\ndef amax(a, axis=None, out=None, keepdims=False, dtype=None):\n    \"\"\"Returns the maximum of an array or the maximum along an axis.\n\n    .. note::\n\n       When at least one element is NaN, the corresponding min value will be\n       NaN.\n\n    Args:\n        a (cupy.ndarray): Array to take the maximum.\n        axis (int): Along which axis to take the maximum. The flattened array\n            is used by default.\n        out (cupy.ndarray): Output array.\n        keepdims (bool): If ``True``, the axis is remained as an axis of\n            size one.\n        dtype: Data type specifier.\n\n    Returns:\n        cupy.ndarray: The maximum of ``a``, along the axis if specified.\n\n    .. seealso:: :func:`numpy.amax`\n\n    \"\"\"\n    if fusion._is_fusing():\n        if keepdims:\n            raise NotImplementedError(\n                'cupy.amax does not support `keepdims` in fusion yet.')\n        return fusion._call_reduction(_statistics.amax,\n                                      a, axis=axis, dtype=dtype, out=out)\n\n    # TODO(okuta): check type\n    return a.max(axis=axis, dtype=dtype, out=out, keepdims=keepdims)\n\n\ndef nanmin(a, axis=None, out=None, keepdims=False):\n    \"\"\"Returns the minimum of an array along an axis ignoring NaN.\n\n    When there is a slice whose elements are all NaN, a :class:`RuntimeWarning`\n    is raised and NaN is returned.\n\n    Args:\n        a (cupy.ndarray): Array to take the minimum.\n        axis (int): Along which axis to take the minimum. The flattened array\n            is used by default.\n        out (cupy.ndarray): Output array.\n        keepdims (bool): If ``True``, the axis is remained as an axis of\n            size one.\n\n    Returns:\n        cupy.ndarray: The minimum of ``a``, along the axis if specified.\n\n    .. seealso:: :func:`numpy.nanmin`\n\n    \"\"\"\n    res = core.nanmin(a, axis=axis, out=out, keepdims=keepdims)\n    if content.isnan(res).any():\n        warnings.warn('All-NaN slice encountered', RuntimeWarning)\n    return res\n\n\ndef nanmax(a, axis=None, out=None, keepdims=False):\n    \"\"\"Returns the maximum of an array along an axis ignoring NaN.\n\n    When there is a slice whose elements are all NaN, a :class:`RuntimeWarning`\n    is raised and NaN is returned.\n\n    Args:\n        a (cupy.ndarray): Array to take the maximum.\n        axis (int): Along which axis to take the maximum. The flattened array\n            is used by default.\n        out (cupy.ndarray): Output array.\n        keepdims (bool): If ``True``, the axis is remained as an axis of\n            size one.\n\n    Returns:\n        cupy.ndarray: The maximum of ``a``, along the axis if specified.\n\n    .. seealso:: :func:`numpy.nanmax`\n\n    \"\"\"\n    res = core.nanmax(a, axis=axis, out=out, keepdims=keepdims)\n    if content.isnan(res).any():\n        warnings.warn('All-NaN slice encountered', RuntimeWarning)\n    return res\n\n\n# TODO(okuta): Implement ptp\n\n\ndef percentile(a, q, axis=None, out=None, interpolation='linear',\n               keepdims=False):\n    \"\"\"Computes the q-th percentile of the data along the specified axis.\n\n    Args:\n        a (cupy.ndarray): Array for which to compute percentiles.\n        q (float, tuple of floats or cupy.ndarray): Percentiles to compute\n            in the range between 0 and 100 inclusive.\n        axis (int or tuple of ints): Along which axis or axes to compute the\n            percentiles. The flattened array is used by default.\n        out (cupy.ndarray): Output array.\n        interpolation (str): Interpolation method when a quantile lies between\n            two data points. ``linear`` interpolation is used by default.\n            Supported interpolations are``lower``, ``higher``, ``midpoint``,\n            ``nearest`` and ``linear``.\n        keepdims (bool): If ``True``, the axis is remained as an axis of\n            size one.\n\n    Returns:\n        cupy.ndarray: The percentiles of ``a``, along the axis if specified.\n\n    .. seealso:: :func:`numpy.percentile`\n\n    \"\"\"\n    q = cupy.asarray(q, dtype=a.dtype)\n    if q.ndim == 0:\n        q = q[None]\n        zerod = True\n    else:\n        zerod = False\n    if q.ndim > 1:\n        raise ValueError('Expected q to have a dimension of 1.\\n'\n                         'Actual: {0} != 1'.format(q.ndim))\n\n    if keepdims:\n        if axis is None:\n            keepdim = (1,) * a.ndim\n        else:\n            keepdim = list(a.shape)\n            for ax in axis:\n                keepdim[ax % a.ndim] = 1\n            keepdim = tuple(keepdim)\n\n    # Copy a since we need it sorted but without modifying the original array\n    if isinstance(axis, int):\n        axis = axis,\n    if axis is None:\n        ap = a.flatten()\n        nkeep = 0\n    else:\n        # Reduce axes from a and put them last\n        axis = tuple(ax % a.ndim for ax in axis)\n        keep = set(range(a.ndim)) - set(axis)\n        nkeep = len(keep)\n        for i, s in enumerate(sorted(keep)):\n            a = a.swapaxes(i, s)\n        ap = a.reshape(a.shape[:nkeep] + (-1,)).copy()\n\n    axis = -1\n    ap.sort(axis=axis)\n    Nx = ap.shape[axis]\n    indices = q * 0.01 * (Nx - 1.)  # percents to decimals\n\n    if interpolation == 'lower':\n        indices = cupy.floor(indices).astype(cupy.int32)\n    elif interpolation == 'higher':\n        indices = cupy.ceil(indices).astype(cupy.int32)\n    elif interpolation == 'midpoint':\n        indices = 0.5 * (cupy.floor(indices) + cupy.ceil(indices))\n    elif interpolation == 'nearest':\n        # TODO(hvy): Implement nearest using around\n        raise ValueError('\\'nearest\\' interpolation is not yet supported. '\n                         'Please use any other interpolation method.')\n    elif interpolation == 'linear':\n        pass\n    else:\n        raise ValueError('Unexpected interpolation method.\\n'\n                         'Actual: \\'{0}\\' not in (\\'linear\\', \\'lower\\', '\n                         '\\'higher\\', \\'midpoint\\')'.format(interpolation))\n\n    if indices.dtype == cupy.int32:\n        ret = cupy.rollaxis(ap, axis)\n        ret = ret.take(indices, axis=0, out=out)\n    else:\n        if out is None:\n            ret = cupy.empty(ap.shape[:-1] + q.shape, dtype=cupy.float64)\n        else:\n            ret = cupy.rollaxis(out, 0, out.ndim)\n\n        cupy.ElementwiseKernel(\n            'S idx, raw T a, raw int32 offset', 'U ret',\n            '''\n            ptrdiff_t idx_below = floor(idx);\n            U weight_above = idx - idx_below;\n\n            ptrdiff_t offset_i = _ind.get()[0] * offset;\n            ret = a[offset_i + idx_below] * (1.0 - weight_above)\n              + a[offset_i + idx_below + 1] * weight_above;\n            ''',\n            'percentile_weightnening'\n        )(indices, ap, ap.shape[-1] if ap.ndim > 1 else 0, ret)\n        ret = cupy.rollaxis(ret, -1)  # Roll q dimension back to first axis\n\n    if zerod:\n        ret = ret.squeeze(0)\n    if keepdims:\n        if q.size > 1:\n            keepdim = (-1,) + keepdim\n        ret = ret.reshape(keepdim)\n\n    return cupy.ascontiguousarray(ret)\n", "path": "cupy/statistics/order.py"}], "after_files": [{"content": "from cupy.core import core  # NOQA\nfrom cupy.core import internal  # NOQA\n\n\n# import class and function\nfrom cupy.core._errors import _AxisError  # NOQA\nfrom cupy.core._kernel import create_reduction_func  # NOQA\nfrom cupy.core._kernel import create_ufunc  # NOQA\nfrom cupy.core._kernel import ElementwiseKernel  # NOQA\nfrom cupy.core._kernel import ReductionKernel  # NOQA\nfrom cupy.core._kernel import ufunc  # NOQA\nfrom cupy.core._routines_manipulation import array_split  # NOQA\nfrom cupy.core._routines_manipulation import broadcast  # NOQA\nfrom cupy.core._routines_manipulation import broadcast_to  # NOQA\nfrom cupy.core._routines_manipulation import concatenate_method  # NOQA\nfrom cupy.core._routines_manipulation import moveaxis  # NOQA\nfrom cupy.core._routines_manipulation import rollaxis  # NOQA\nfrom cupy.core._routines_manipulation import size  # NOQA'\nfrom cupy.core._routines_math import absolute  # NOQA\nfrom cupy.core._routines_math import add  # NOQA\nfrom cupy.core._routines_math import angle  # NOQA\nfrom cupy.core._routines_math import conj  # NOQA\nfrom cupy.core._routines_math import divide  # NOQA\nfrom cupy.core._routines_math import floor_divide  # NOQA\nfrom cupy.core._routines_math import imag  # NOQA\nfrom cupy.core._routines_math import multiply  # NOQA\nfrom cupy.core._routines_math import negative  # NOQA\nfrom cupy.core._routines_math import power  # NOQA\nfrom cupy.core._routines_math import real  # NOQA\nfrom cupy.core._routines_math import remainder  # NOQA\nfrom cupy.core._routines_math import sqrt  # NOQA\nfrom cupy.core._routines_math import subtract  # NOQA\nfrom cupy.core._routines_math import true_divide  # NOQA\nfrom cupy.core._routines_statistics import nanmax  # NOQA\nfrom cupy.core._routines_statistics import nanmin  # NOQA\nfrom cupy.core.core import _internal_ascontiguousarray  # NOQA\nfrom cupy.core.core import _internal_asfortranarray  # NOQA\nfrom cupy.core.core import array  # NOQA\nfrom cupy.core.core import ascontiguousarray  # NOQA\nfrom cupy.core.core import asfortranarray  # NOQA\nfrom cupy.core.core import bitwise_and  # NOQA\nfrom cupy.core.core import bitwise_or  # NOQA\nfrom cupy.core.core import bitwise_xor  # NOQA\nfrom cupy.core.core import create_comparison  # NOQA\nfrom cupy.core.core import divmod  # NOQA\nfrom cupy.core.core import dot  # NOQA\nfrom cupy.core.core import elementwise_copy  # NOQA\nfrom cupy.core.core import elementwise_copy_where  # NOQA\nfrom cupy.core.core import equal  # NOQA\nfrom cupy.core.core import greater  # NOQA\nfrom cupy.core.core import greater_equal  # NOQA\nfrom cupy.core.core import invert  # NOQA\nfrom cupy.core.core import left_shift  # NOQA\nfrom cupy.core.core import less  # NOQA\nfrom cupy.core.core import less_equal  # NOQA\nfrom cupy.core.core import matmul  # NOQA\nfrom cupy.core.core import ndarray  # NOQA\nfrom cupy.core.core import not_equal  # NOQA\nfrom cupy.core.core import right_shift  # NOQA\nfrom cupy.core.core import tensordot_core  # NOQA\nfrom cupy.core.dlpack import fromDlpack  # NOQA\nfrom cupy.core.internal import complete_slice  # NOQA\nfrom cupy.core.internal import get_size  # NOQA\nfrom cupy.core.raw import RawKernel  # NOQA\n", "path": "cupy/core/__init__.py"}, {"content": "import warnings\n\nimport cupy\nfrom cupy import core\nfrom cupy.core import _routines_statistics as _statistics\nfrom cupy.core import fusion\nfrom cupy.logic import content\n\n\ndef amin(a, axis=None, out=None, keepdims=False, dtype=None):\n    \"\"\"Returns the minimum of an array or the minimum along an axis.\n\n    .. note::\n\n       When at least one element is NaN, the corresponding min value will be\n       NaN.\n\n    Args:\n        a (cupy.ndarray): Array to take the minimum.\n        axis (int): Along which axis to take the minimum. The flattened array\n            is used by default.\n        out (cupy.ndarray): Output array.\n        keepdims (bool): If ``True``, the axis is remained as an axis of\n            size one.\n        dtype: Data type specifier.\n\n    Returns:\n        cupy.ndarray: The minimum of ``a``, along the axis if specified.\n\n    .. seealso:: :func:`numpy.amin`\n\n    \"\"\"\n    if fusion._is_fusing():\n        if keepdims:\n            raise NotImplementedError(\n                'cupy.amin does not support `keepdims` in fusion yet.')\n        return fusion._call_reduction(_statistics.amin,\n                                      a, axis=axis, dtype=dtype, out=out)\n\n    # TODO(okuta): check type\n    return a.min(axis=axis, dtype=dtype, out=out, keepdims=keepdims)\n\n\ndef amax(a, axis=None, out=None, keepdims=False, dtype=None):\n    \"\"\"Returns the maximum of an array or the maximum along an axis.\n\n    .. note::\n\n       When at least one element is NaN, the corresponding min value will be\n       NaN.\n\n    Args:\n        a (cupy.ndarray): Array to take the maximum.\n        axis (int): Along which axis to take the maximum. The flattened array\n            is used by default.\n        out (cupy.ndarray): Output array.\n        keepdims (bool): If ``True``, the axis is remained as an axis of\n            size one.\n        dtype: Data type specifier.\n\n    Returns:\n        cupy.ndarray: The maximum of ``a``, along the axis if specified.\n\n    .. seealso:: :func:`numpy.amax`\n\n    \"\"\"\n    if fusion._is_fusing():\n        if keepdims:\n            raise NotImplementedError(\n                'cupy.amax does not support `keepdims` in fusion yet.')\n        return fusion._call_reduction(_statistics.amax,\n                                      a, axis=axis, dtype=dtype, out=out)\n\n    # TODO(okuta): check type\n    return a.max(axis=axis, dtype=dtype, out=out, keepdims=keepdims)\n\n\ndef nanmin(a, axis=None, out=None, keepdims=False):\n    \"\"\"Returns the minimum of an array along an axis ignoring NaN.\n\n    When there is a slice whose elements are all NaN, a :class:`RuntimeWarning`\n    is raised and NaN is returned.\n\n    Args:\n        a (cupy.ndarray): Array to take the minimum.\n        axis (int): Along which axis to take the minimum. The flattened array\n            is used by default.\n        out (cupy.ndarray): Output array.\n        keepdims (bool): If ``True``, the axis is remained as an axis of\n            size one.\n\n    Returns:\n        cupy.ndarray: The minimum of ``a``, along the axis if specified.\n\n    .. seealso:: :func:`numpy.nanmin`\n\n    \"\"\"\n    res = core.nanmin(a, axis=axis, out=out, keepdims=keepdims)\n    if content.isnan(res).any():\n        warnings.warn('All-NaN slice encountered', RuntimeWarning)\n    return res\n\n\ndef nanmax(a, axis=None, out=None, keepdims=False):\n    \"\"\"Returns the maximum of an array along an axis ignoring NaN.\n\n    When there is a slice whose elements are all NaN, a :class:`RuntimeWarning`\n    is raised and NaN is returned.\n\n    Args:\n        a (cupy.ndarray): Array to take the maximum.\n        axis (int): Along which axis to take the maximum. The flattened array\n            is used by default.\n        out (cupy.ndarray): Output array.\n        keepdims (bool): If ``True``, the axis is remained as an axis of\n            size one.\n\n    Returns:\n        cupy.ndarray: The maximum of ``a``, along the axis if specified.\n\n    .. seealso:: :func:`numpy.nanmax`\n\n    \"\"\"\n    res = core.nanmax(a, axis=axis, out=out, keepdims=keepdims)\n    if content.isnan(res).any():\n        warnings.warn('All-NaN slice encountered', RuntimeWarning)\n    return res\n\n\n# TODO(okuta): Implement ptp\n\n\ndef percentile(a, q, axis=None, out=None, interpolation='linear',\n               keepdims=False):\n    \"\"\"Computes the q-th percentile of the data along the specified axis.\n\n    Args:\n        a (cupy.ndarray): Array for which to compute percentiles.\n        q (float, tuple of floats or cupy.ndarray): Percentiles to compute\n            in the range between 0 and 100 inclusive.\n        axis (int or tuple of ints): Along which axis or axes to compute the\n            percentiles. The flattened array is used by default.\n        out (cupy.ndarray): Output array.\n        interpolation (str): Interpolation method when a quantile lies between\n            two data points. ``linear`` interpolation is used by default.\n            Supported interpolations are``lower``, ``higher``, ``midpoint``,\n            ``nearest`` and ``linear``.\n        keepdims (bool): If ``True``, the axis is remained as an axis of\n            size one.\n\n    Returns:\n        cupy.ndarray: The percentiles of ``a``, along the axis if specified.\n\n    .. seealso:: :func:`numpy.percentile`\n\n    \"\"\"\n    q = cupy.asarray(q, dtype=a.dtype)\n    if q.ndim == 0:\n        q = q[None]\n        zerod = True\n    else:\n        zerod = False\n    if q.ndim > 1:\n        raise ValueError('Expected q to have a dimension of 1.\\n'\n                         'Actual: {0} != 1'.format(q.ndim))\n\n    if keepdims:\n        if axis is None:\n            keepdim = (1,) * a.ndim\n        else:\n            keepdim = list(a.shape)\n            for ax in axis:\n                keepdim[ax % a.ndim] = 1\n            keepdim = tuple(keepdim)\n\n    # Copy a since we need it sorted but without modifying the original array\n    if isinstance(axis, int):\n        axis = axis,\n    if axis is None:\n        ap = a.flatten()\n        nkeep = 0\n    else:\n        # Reduce axes from a and put them last\n        axis = tuple(ax % a.ndim for ax in axis)\n        keep = set(range(a.ndim)) - set(axis)\n        nkeep = len(keep)\n        for i, s in enumerate(sorted(keep)):\n            a = a.swapaxes(i, s)\n        ap = a.reshape(a.shape[:nkeep] + (-1,)).copy()\n\n    axis = -1\n    ap.sort(axis=axis)\n    Nx = ap.shape[axis]\n    indices = q * 0.01 * (Nx - 1.)  # percents to decimals\n\n    if interpolation == 'lower':\n        indices = cupy.floor(indices).astype(cupy.int32)\n    elif interpolation == 'higher':\n        indices = cupy.ceil(indices).astype(cupy.int32)\n    elif interpolation == 'midpoint':\n        indices = 0.5 * (cupy.floor(indices) + cupy.ceil(indices))\n    elif interpolation == 'nearest':\n        # TODO(hvy): Implement nearest using around\n        raise ValueError('\\'nearest\\' interpolation is not yet supported. '\n                         'Please use any other interpolation method.')\n    elif interpolation == 'linear':\n        pass\n    else:\n        raise ValueError('Unexpected interpolation method.\\n'\n                         'Actual: \\'{0}\\' not in (\\'linear\\', \\'lower\\', '\n                         '\\'higher\\', \\'midpoint\\')'.format(interpolation))\n\n    if indices.dtype == cupy.int32:\n        ret = cupy.rollaxis(ap, axis)\n        ret = ret.take(indices, axis=0, out=out)\n    else:\n        if out is None:\n            ret = cupy.empty(ap.shape[:-1] + q.shape, dtype=cupy.float64)\n        else:\n            ret = cupy.rollaxis(out, 0, out.ndim)\n\n        cupy.ElementwiseKernel(\n            'S idx, raw T a, raw int32 offset', 'U ret',\n            '''\n            ptrdiff_t idx_below = floor(idx);\n            U weight_above = idx - idx_below;\n\n            ptrdiff_t offset_i = _ind.get()[0] * offset;\n            ret = a[offset_i + idx_below] * (1.0 - weight_above)\n              + a[offset_i + idx_below + 1] * weight_above;\n            ''',\n            'percentile_weightnening'\n        )(indices, ap, ap.shape[-1] if ap.ndim > 1 else 0, ret)\n        ret = cupy.rollaxis(ret, -1)  # Roll q dimension back to first axis\n\n    if zerod:\n        ret = ret.squeeze(0)\n    if keepdims:\n        if q.size > 1:\n            keepdim = (-1,) + keepdim\n        ret = ret.reshape(keepdim)\n\n    return core._internal_ascontiguousarray(ret)\n", "path": "cupy/statistics/order.py"}]}