View difference between Paste ID: tg2aHE6x and 9CN0k9Tx
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
    
54
    # CPython numpy version:
55
    #return bool(logical_and.reduce(equal(a1,a2).ravel()))
56
    
57
    # PyPy version:
58
    if a1.ndim == 0: # else bug with zero-size array indexation
59
        return a1 == a2
60-
    a1, a2 = a1.flat, a2.flat
60+
61-
    for i in range(Size):
61+
    f1, f2 = a1.flat, a2.flat
62-
        if a1[i] != a2[i]: return False
62+
    # TODO: replace xrange by range in Python3 
63-
    return True
63+
    for i in xrange(Size):
64
        if f1.next() != f2.next(): return False
65
    return True
66
67
if __name__ == '__main__':
68
    assert array_equal(1, 1) 
69
    assert array_equal([1, 2], [1, 2]) 
70
    assert array_equal(N.array([1, 2]), N.array([1, 2]))
71
    assert array_equal([1, 2], N.array([1, 2])) 
72
    assert not array_equal([1, 2], [1, 2, 3]) 
73
    assert array_equal(N.ones([2, 3, 4]), N.ones([2, 3, 4]))
74
    assert array_equal(N.ones([2, 3, 4]), N.ones([2, 3, 4], int))
75
    assert not array_equal(N.ones([2, 3, 4]), N.zeros([2, 3, 4], int))
76
    
77
    print('passed')
78
    
79
#from time import time
80
#n = 100000
81
#m = 100
82
#a = N.zeros(n)
83
#b = N.ones(n)
84
#t = time()
85
#for i in range(m):
86
#    N.array_equal(a, b)
87
#print('classic numpy array_equal time elapsed (on different arrays): %0.5f' % (time()-t))
88
#
89
#
90
#t = time()
91
#for i in range(m):
92
#    array_equal(a, b)
93
#print('Alternative array_equal time elapsed (on different arrays): %0.5f' % (time()-t))
94
#
95
#b = N.zeros(n)
96
#
97
#t = time()
98
#for i in range(m):
99
#    N.array_equal(a, b)
100
#print('classic numpy array_equal time elapsed (on same arrays): %0.5f' % (time()-t))
101
#
102
#t = time()
103
#for i in range(m):
104
#    array_equal(a, b)
105
#print('Alternative array_equal time elapsed (on same arrays): %0.5f' % (time()-t))