comparison DEPENDENCIES/mingw32/Python27/Lib/site-packages/numpy/lib/stride_tricks.py @ 87:2a2c65a20a8b

Add Python libs and headers
author Chris Cannam
date Wed, 25 Feb 2015 14:05:22 +0000
parents
children
comparison
equal deleted inserted replaced
86:413a9d26189e 87:2a2c65a20a8b
1 """
2 Utilities that manipulate strides to achieve desirable effects.
3
4 An explanation of strides can be found in the "ndarray.rst" file in the
5 NumPy reference guide.
6
7 """
8 from __future__ import division, absolute_import, print_function
9
10 import numpy as np
11
12 __all__ = ['broadcast_arrays']
13
14 class DummyArray(object):
15 """Dummy object that just exists to hang __array_interface__ dictionaries
16 and possibly keep alive a reference to a base array.
17 """
18
19 def __init__(self, interface, base=None):
20 self.__array_interface__ = interface
21 self.base = base
22
23 def as_strided(x, shape=None, strides=None):
24 """ Make an ndarray from the given array with the given shape and strides.
25 """
26 interface = dict(x.__array_interface__)
27 if shape is not None:
28 interface['shape'] = tuple(shape)
29 if strides is not None:
30 interface['strides'] = tuple(strides)
31 array = np.asarray(DummyArray(interface, base=x))
32 # Make sure dtype is correct in case of custom dtype
33 if array.dtype.kind == 'V':
34 array.dtype = x.dtype
35 return array
36
37 def broadcast_arrays(*args):
38 """
39 Broadcast any number of arrays against each other.
40
41 Parameters
42 ----------
43 `*args` : array_likes
44 The arrays to broadcast.
45
46 Returns
47 -------
48 broadcasted : list of arrays
49 These arrays are views on the original arrays. They are typically
50 not contiguous. Furthermore, more than one element of a
51 broadcasted array may refer to a single memory location. If you
52 need to write to the arrays, make copies first.
53
54 Examples
55 --------
56 >>> x = np.array([[1,2,3]])
57 >>> y = np.array([[1],[2],[3]])
58 >>> np.broadcast_arrays(x, y)
59 [array([[1, 2, 3],
60 [1, 2, 3],
61 [1, 2, 3]]), array([[1, 1, 1],
62 [2, 2, 2],
63 [3, 3, 3]])]
64
65 Here is a useful idiom for getting contiguous copies instead of
66 non-contiguous views.
67
68 >>> [np.array(a) for a in np.broadcast_arrays(x, y)]
69 [array([[1, 2, 3],
70 [1, 2, 3],
71 [1, 2, 3]]), array([[1, 1, 1],
72 [2, 2, 2],
73 [3, 3, 3]])]
74
75 """
76 args = [np.asarray(_m) for _m in args]
77 shapes = [x.shape for x in args]
78 if len(set(shapes)) == 1:
79 # Common case where nothing needs to be broadcasted.
80 return args
81 shapes = [list(s) for s in shapes]
82 strides = [list(x.strides) for x in args]
83 nds = [len(s) for s in shapes]
84 biggest = max(nds)
85 # Go through each array and prepend dimensions of length 1 to each of
86 # the shapes in order to make the number of dimensions equal.
87 for i in range(len(args)):
88 diff = biggest - nds[i]
89 if diff > 0:
90 shapes[i] = [1] * diff + shapes[i]
91 strides[i] = [0] * diff + strides[i]
92 # Chech each dimension for compatibility. A dimension length of 1 is
93 # accepted as compatible with any other length.
94 common_shape = []
95 for axis in range(biggest):
96 lengths = [s[axis] for s in shapes]
97 unique = set(lengths + [1])
98 if len(unique) > 2:
99 # There must be at least two non-1 lengths for this axis.
100 raise ValueError("shape mismatch: two or more arrays have "
101 "incompatible dimensions on axis %r." % (axis,))
102 elif len(unique) == 2:
103 # There is exactly one non-1 length. The common shape will take
104 # this value.
105 unique.remove(1)
106 new_length = unique.pop()
107 common_shape.append(new_length)
108 # For each array, if this axis is being broadcasted from a
109 # length of 1, then set its stride to 0 so that it repeats its
110 # data.
111 for i in range(len(args)):
112 if shapes[i][axis] == 1:
113 shapes[i][axis] = new_length
114 strides[i][axis] = 0
115 else:
116 # Every array has a length of 1 on this axis. Strides can be
117 # left alone as nothing is broadcasted.
118 common_shape.append(1)
119
120 # Construct the new arrays.
121 broadcasted = [as_strided(x, shape=sh, strides=st) for (x, sh, st) in
122 zip(args, shapes, strides)]
123 return broadcasted