View difference between Paste ID: 9CN0k9Tx and i1psQSrt
SHOW: | | - or go back to the newest paste.
1
try:
2
    import numpypy as N
3
except:
4
    import numpy as N
5
    
6
def array_equal(a1, a2):
7
    """
8
    True if two arrays have the same shape and elements, False otherwise.
9
10
    Parameters
11
    ----------
12
    a1, a2 : array_like
13
        Input arrays.
14
15
    Returns
16
    -------
17
    b : bool
18
        Returns True if the arrays are equal.
19
20
    See Also
21
    --------
22
    allclose: Returns True if two arrays are element-wise equal within a
23
              tolerance.
24
    array_equiv: Returns True if input arrays are shape consistent and all
25
                 elements equal.
26
27
    Examples
28
    --------
29
    >>> np.array_equal([1, 2], [1, 2])
30
    True
31
    >>> np.array_equal(np.array([1, 2]), np.array([1, 2]))
32
    True
33
    >>> np.array_equal([1, 2], [1, 2, 3])
34
    False
35
    >>> np.array_equal([1, 2], [1, 4])
36
    False
37
38
    """
39
    try:
40
        #a1, a2 = asarray(a1), asarray(a2)
41
        #  N.asarray(v) doesn't work - asarray is unimplemented yet, using walkaround instead.
42
        # TODO: may be comparison of numpy array with sparse matrix, maybe take care of it? 
43
        # array(sparse matrix) in CPython numpy currently returns array of size one and dtype "object"
44
        if not isinstance(a1, N.ndarray):
45
            a1 = N.array(a1)
46
        if not isinstance(a2, N.ndarray):
47
            a2 = N.array(a2)
48
    except:
49
        return False
50
    if a1.shape != a2.shape:
51
        return False
52
    
53
    # CPython numpy version:
54
    #return bool(logical_and.reduce(equal(a1,a2).ravel()))
55
    
56
    # PyPy version:
57
    if a1.ndim == 0: # else bug with zero-size array indexation
58
        return a1 == a2
59
    Size = a1.size 
60
    a1, a2 = a1.flat, a2.flat
61
    for i in range(Size):
62
        if a1[i] != a2[i]: return False
63
    return True