What is the difference between numpy.shares_memory and numpy.may_share_memory?

Why does it exist numpy.may_share_memory?
What is the task of giving an accurate result?

Is an numpy.may_share_memoryobsolete method?
numpy.may_share_memorymay give false positives, but it does not give false negatives.

Does numpy.shares_memorynot one false positive and no false negation provide ?

I am using the numpy version 1.11.2.

Cm:

+6
source share
2 answers

Quoting the release notes for 1.11.0 :

np.shares_memory, , . np.may_share_memory .

, may_share_memory , , . , , , . (, ), . , shares_memory . , , . may_share_memory, , .

may_share_memory shares_memory, , numpy, .

may_share_memory:

max_work : int, optional

    Effort to spend on solving the overlap problem. See shares_memory for details. Default for may_share_memory is to do a bounds check.

shares_memory:

max_work : int, optional

    Effort to spend on solving the overlap problem (maximum number of candidate solutions to consider). The following special values are recognized:

    max_work=MAY_SHARE_EXACT (default)

        The problem is solved exactly. In this case, the function returns True only if there is an element shared between the arrays.
    max_work=MAY_SHARE_BOUNDS

        Only the memory bounds of a and b are checked.

, , , may_share_memory .

:

static PyObject *
array_shares_memory(PyObject *NPY_UNUSED(ignored), PyObject *args, PyObject *kwds)
{
    return array_shares_memory_impl(args, kwds, NPY_MAY_SHARE_EXACT, 1);
}


static PyObject *
array_may_share_memory(PyObject *NPY_UNUSED(ignored), PyObject *args, PyObject *kwds)
{
    return array_shares_memory_impl(args, kwds, NPY_MAY_SHARE_BOUNDS, 0);
}

static PyObject *
array_shares_memory_impl(PyObject *args, PyObject *kwds, Py_ssize_t default_max_work,
                         int raise_exceptions)
{}

, , shares_memory may_share_memory, , , . .

: , , array_shares_memory_impl, .


( ): , may_share_memory , . , , . , !

: :

>>> import numpy as np
>>> v = np.arange(6)
>>> x = v[::2]
>>> y = v[1::2]
>>> np.may_share_memory(x,y)
True
>>> np.shares_memory(x,y)
False
>>> np.may_share_memory(x,y,max_work=np.MAY_SHARE_EXACT)
False

, x y - . , ( , ). : , - . may_share_memory , , , .


, , solve_may_share_memory, , .

, 3 - , shares_memory (, , ).

+8

, :

http://scipy-cookbook.readthedocs.io/items/ViewsVsCopies.html
http://www.scipy-lectures.org/advanced/advanced_numpy/

, a b:

. NumPy ( ).

:

" "

1D-:

import numpy as np

x = np.arange(8, dtype=np.int8)
a = x[::3]
b = x[1::2]

:

1D array

1D . , 64- ( 8 ), (0 <= np.int8 < 256).

, a:

base_a + stride_a * x_a x_a - ( 0).

b:

base_b + stride_b * x_b x_b - ( 0).

, :

base_a + stride_a * x_a = base_b + stride_b * x_b

:

stride_a * x_a - stride_b * x_b = base_b - base_a

0 <= x_a < shape_a 0 <= x_b < shape_b.

b , , :

x_b' = shape_b - 1 - x_b

:

stride_a * x_a + stride_b * x_b = base_b + stride_b * (shape_b - 1) - base_a

:

3 x_a + 2 x_b = 7 (= 1 + 2 * (4 - 1))

0 <= x_a < 3 0 <= x_b < 4.

- x_a = 1 x_b = 2 ( x_b).

....

, 2D- XD (, 4 , ).

github comparaison NumPy.

...

+2

All Articles