Skip to content

Commit

Permalink
Merge pull request #22313 from rossbar/deprecate-fastCopyAndTranspose
Browse files Browse the repository at this point in the history
DEP: Deprecate fastCopyAndTranspose
  • Loading branch information
mattip committed Oct 7, 2022
2 parents 384c13e + 06c380c commit ae6a352
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 41 deletions.
10 changes: 10 additions & 0 deletions doc/release/upcoming_changes/22313.deprecation.rst
@@ -0,0 +1,10 @@
Deprecate fastCopyAndTranspose and PyArray_CopyAndTranspose
-----------------------------------------------------------

The ``numpy.fastCopyAndTranspose`` function has been deprecated. Use the
corresponding copy and transpose methods directly::

arr.T.copy()

The underlying C function ``PyArray_CopyAndTranspose`` has also been
deprecated from the NumPy C-API.
9 changes: 8 additions & 1 deletion numpy/core/_add_newdocs.py
Expand Up @@ -1674,7 +1674,14 @@
""")

add_newdoc('numpy.core', 'fastCopyAndTranspose',
"""_fastCopyAndTranspose(a)""")
"""
fastCopyAndTranspose(a)
.. deprecated:: 1.24
fastCopyAndTranspose is deprecated and will be removed. Use the copy and
transpose methods instead, e.g. ``arr.T.copy()``
""")

add_newdoc('numpy.core.multiarray', 'correlate',
"""cross_correlate(a,v, mode=0)""")
Expand Down
4 changes: 2 additions & 2 deletions numpy/core/multiarray.py
Expand Up @@ -14,7 +14,7 @@
# do not change them. issue gh-15518
# _get_ndarray_c_version is semi-public, on purpose not added to __all__
from ._multiarray_umath import (
_fastCopyAndTranspose, _flagdict, from_dlpack, _insert, _reconstruct,
fastCopyAndTranspose, _flagdict, from_dlpack, _insert, _reconstruct,
_vec_string, _ARRAY_API, _monotonicity, _get_ndarray_c_version,
_get_madvise_hugepage, _set_madvise_hugepage,
_get_promotion_state, _set_promotion_state,
Expand All @@ -24,7 +24,7 @@
'_ARRAY_API', 'ALLOW_THREADS', 'BUFSIZE', 'CLIP', 'DATETIMEUNITS',
'ITEM_HASOBJECT', 'ITEM_IS_POINTER', 'LIST_PICKLE', 'MAXDIMS',
'MAY_SHARE_BOUNDS', 'MAY_SHARE_EXACT', 'NEEDS_INIT', 'NEEDS_PYAPI',
'RAISE', 'USE_GETITEM', 'USE_SETITEM', 'WRAP', '_fastCopyAndTranspose',
'RAISE', 'USE_GETITEM', 'USE_SETITEM', 'WRAP',
'_flagdict', 'from_dlpack', '_insert', '_reconstruct', '_vec_string',
'_monotonicity', 'add_docstring', 'arange', 'array', 'asarray',
'asanyarray', 'ascontiguousarray', 'asfortranarray', 'bincount',
Expand Down
2 changes: 1 addition & 1 deletion numpy/core/numeric.py
Expand Up @@ -8,7 +8,7 @@
import numpy as np
from . import multiarray
from .multiarray import (
_fastCopyAndTranspose as fastCopyAndTranspose, ALLOW_THREADS,
fastCopyAndTranspose, ALLOW_THREADS,
BUFSIZE, CLIP, MAXDIMS, MAY_SHARE_BOUNDS, MAY_SHARE_EXACT, RAISE,
WRAP, arange, array, asarray, asanyarray, ascontiguousarray,
asfortranarray, broadcast, can_cast, compare_chararrays,
Expand Down
13 changes: 11 additions & 2 deletions numpy/core/src/multiarray/multiarraymodule.c
Expand Up @@ -1139,6 +1139,15 @@ PyArray_MatrixProduct2(PyObject *op1, PyObject *op2, PyArrayObject* out)
NPY_NO_EXPORT PyObject *
PyArray_CopyAndTranspose(PyObject *op)
{
/* DEPRECATED 2022-09-30, NumPy 1.24 - see gh-22313 */
if (DEPRECATE(
"fastCopyAndTranspose and the underlying C function "
"PyArray_CopyAndTranspose have been deprecated.\n\n"
"Use the transpose method followed by a C-order copy instead, "
"e.g. ``arr.T.copy()``") < 0) {
return NULL;
}

PyArrayObject *arr, *tmp, *ret;
int i;
npy_intp new_axes_values[NPY_MAXDIMS];
Expand Down Expand Up @@ -2986,7 +2995,7 @@ array_fastCopyAndTranspose(PyObject *NPY_UNUSED(dummy), PyObject *args)
{
PyObject *a0;

if (!PyArg_ParseTuple(args, "O:_fastCopyAndTranspose", &a0)) {
if (!PyArg_ParseTuple(args, "O:fastCopyAndTranspose", &a0)) {
return NULL;
}
return PyArray_Return((PyArrayObject *)PyArray_CopyAndTranspose(a0));
Expand Down Expand Up @@ -4396,7 +4405,7 @@ static struct PyMethodDef array_module_methods[] = {
{"c_einsum",
(PyCFunction)array_einsum,
METH_VARARGS|METH_KEYWORDS, NULL},
{"_fastCopyAndTranspose",
{"fastCopyAndTranspose",
(PyCFunction)array_fastCopyAndTranspose,
METH_VARARGS, NULL},
{"correlate",
Expand Down
32 changes: 14 additions & 18 deletions numpy/core/tests/test_api.py
Expand Up @@ -150,24 +150,20 @@ def test_array_impossible_casts(array):
np.array(rt, dtype="M8")


def test_fastCopyAndTranspose():
# 0D array
a = np.array(2)
b = np.fastCopyAndTranspose(a)
assert_equal(b, a.T)
assert_(b.flags.owndata)

# 1D array
a = np.array([3, 2, 7, 0])
b = np.fastCopyAndTranspose(a)
assert_equal(b, a.T)
assert_(b.flags.owndata)

# 2D array
a = np.arange(6).reshape(2, 3)
b = np.fastCopyAndTranspose(a)
assert_equal(b, a.T)
assert_(b.flags.owndata)
# TODO: remove when fastCopyAndTranspose deprecation expires
@pytest.mark.parametrize("a",
(
np.array(2), # 0D array
np.array([3, 2, 7, 0]), # 1D array
np.arange(6).reshape(2, 3) # 2D array
),
)
def test_fastCopyAndTranspose(a):
with pytest.deprecated_call():
b = np.fastCopyAndTranspose(a)
assert_equal(b, a.T)
assert b.flags.owndata


def test_array_astype():
a = np.arange(6, dtype='f4').reshape(2, 3)
Expand Down
18 changes: 2 additions & 16 deletions numpy/linalg/linalg.py
Expand Up @@ -21,7 +21,7 @@
from numpy.core import (
array, asarray, zeros, empty, empty_like, intc, single, double,
csingle, cdouble, inexact, complexfloating, newaxis, all, Inf, dot,
add, multiply, sqrt, fastCopyAndTranspose, sum, isfinite,
add, multiply, sqrt, sum, isfinite,
finfo, errstate, geterrobj, moveaxis, amin, amax, product, abs,
atleast_2d, intp, asanyarray, object_, matmul,
swapaxes, divide, count_nonzero, isnan, sign, argsort, sort,
Expand Down Expand Up @@ -158,10 +158,6 @@ def _commonType(*arrays):
return t, result_type


# _fastCopyAndTranpose assumes the input is 2D (as all the calls in here are).

_fastCT = fastCopyAndTranspose

def _to_native_byte_order(*arrays):
ret = []
for arr in arrays:
Expand All @@ -174,16 +170,6 @@ def _to_native_byte_order(*arrays):
else:
return ret

def _fastCopyAndTranspose(type, *arrays):
cast_arrays = ()
for a in arrays:
if a.dtype.type is not type:
a = a.astype(type)
cast_arrays = cast_arrays + (_fastCT(a),)
if len(cast_arrays) == 1:
return cast_arrays[0]
else:
return cast_arrays

def _assert_2d(*arrays):
for a in arrays:
Expand Down Expand Up @@ -1172,7 +1158,7 @@ def eigvalsh(a, UPLO='L'):

def _convertarray(a):
t, result_t = _commonType(a)
a = _fastCT(a.astype(t))
a = a.astype(t).T.copy()
return a, t, result_t


Expand Down
2 changes: 1 addition & 1 deletion numpy/tests/test_public_api.py
Expand Up @@ -43,7 +43,7 @@ def test_numpy_namespace():
'deprecate': 'numpy.lib.utils.deprecate',
'deprecate_with_doc': 'numpy.lib.utils.deprecate_with_doc',
'disp': 'numpy.lib.function_base.disp',
'fastCopyAndTranspose': 'numpy.core._multiarray_umath._fastCopyAndTranspose',
'fastCopyAndTranspose': 'numpy.core._multiarray_umath.fastCopyAndTranspose',
'get_array_wrap': 'numpy.lib.shape_base.get_array_wrap',
'get_include': 'numpy.lib.utils.get_include',
'recfromcsv': 'numpy.lib.npyio.recfromcsv',
Expand Down

0 comments on commit ae6a352

Please sign in to comment.