~jtaylor/ubuntu/precise/python-numpy/multiarch-fix-818867

« back to all changes in this revision

Viewing changes to numpy/core/shape_base.py

  • Committer: Bazaar Package Importer
  • Author(s): Sandro Tosi
  • Date: 2010-10-07 10:19:13 UTC
  • mfrom: (7.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20101007101913-8b1kmt8ho4upcl9s
Tags: 1:1.4.1-5
* debian/patches/10_use_local_python.org_object.inv_sphinx.diff
  - fixed small typo in description
* debian/patches/changeset_r8364.diff
  - fix memory corruption (double free); thanks to Joseph Barillari for the
    report and to Michael Gilbert for pushing resolution; Closes: #581058

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
__all__ = ['atleast_1d','atleast_2d','atleast_3d','vstack','hstack']
 
2
 
 
3
import numeric as _nx
 
4
from numeric import array, asarray, newaxis
 
5
 
 
6
def atleast_1d(*arys):
 
7
    """
 
8
    Convert inputs to arrays with at least one dimension.
 
9
 
 
10
    Scalar inputs are converted to 1-dimensional arrays, whilst
 
11
    higher-dimensional inputs are preserved.
 
12
 
 
13
    Parameters
 
14
    ----------
 
15
    array1, array2, ... : array_like
 
16
        One or more input arrays.
 
17
 
 
18
    Returns
 
19
    -------
 
20
    ret : ndarray
 
21
        An array, or sequence of arrays, each with ``a.ndim >= 1``.
 
22
        Copies are made only if necessary.
 
23
 
 
24
    See Also
 
25
    --------
 
26
    atleast_2d, atleast_3d
 
27
 
 
28
    Examples
 
29
    --------
 
30
    >>> np.atleast_1d(1.0)
 
31
    array([ 1.])
 
32
 
 
33
    >>> x = np.arange(9.0).reshape(3,3)
 
34
    >>> np.atleast_1d(x)
 
35
    array([[ 0.,  1.,  2.],
 
36
           [ 3.,  4.,  5.],
 
37
           [ 6.,  7.,  8.]])
 
38
    >>> np.atleast_1d(x) is x
 
39
    True
 
40
 
 
41
    >>> np.atleast_1d(1, [3, 4])
 
42
    [array([1]), array([3, 4])]
 
43
 
 
44
    """
 
45
    res = []
 
46
    for ary in arys:
 
47
        res.append(array(ary,copy=False,subok=True,ndmin=1))
 
48
    if len(res) == 1:
 
49
        return res[0]
 
50
    else:
 
51
        return res
 
52
 
 
53
def atleast_2d(*arys):
 
54
    """
 
55
    View inputs as arrays with at least two dimensions.
 
56
 
 
57
    Parameters
 
58
    ----------
 
59
    array1, array2, ... : array_like
 
60
        One or more array-like sequences.  Non-array inputs are converted
 
61
        to arrays.  Arrays that already have two or more dimensions are
 
62
        preserved.
 
63
 
 
64
    Returns
 
65
    -------
 
66
    res, res2, ... : ndarray
 
67
        An array, or tuple of arrays, each with ``a.ndim >= 2``.
 
68
        Copies are avoided where possible, and views with two or more
 
69
        dimensions are returned.
 
70
 
 
71
    See Also
 
72
    --------
 
73
    atleast_1d, atleast_3d
 
74
 
 
75
    Examples
 
76
    --------
 
77
    >>> np.atleast_2d(3.0)
 
78
    array([[ 3.]])
 
79
 
 
80
    >>> x = np.arange(3.0)
 
81
    >>> np.atleast_2d(x)
 
82
    array([[ 0.,  1.,  2.]])
 
83
    >>> np.atleast_2d(x).base is x
 
84
    True
 
85
 
 
86
    >>> np.atleast_2d(1, [1, 2], [[1, 2]])
 
87
    [array([[1]]), array([[1, 2]]), array([[1, 2]])]
 
88
 
 
89
    """
 
90
    res = []
 
91
    for ary in arys:
 
92
        res.append(array(ary,copy=False,subok=True,ndmin=2))
 
93
    if len(res) == 1:
 
94
        return res[0]
 
95
    else:
 
96
        return res
 
97
 
 
98
def atleast_3d(*arys):
 
99
    """
 
100
    View inputs as arrays with at least three dimensions.
 
101
 
 
102
    Parameters
 
103
    ----------
 
104
    array1, array2, ... : array_like
 
105
        One or more array-like sequences.  Non-array inputs are converted
 
106
        to arrays. Arrays that already have three or more dimensions are
 
107
        preserved.
 
108
 
 
109
    Returns
 
110
    -------
 
111
    res1, res2, ... : ndarray
 
112
        An array, or tuple of arrays, each with ``a.ndim >= 3``.
 
113
        Copies are avoided where possible, and views with three or more
 
114
        dimensions are returned.  For example, a 1-D array of shape ``N``
 
115
        becomes a view of shape ``(1, N, 1)``.  A 2-D array of shape ``(M, N)``
 
116
        becomes a view of shape ``(M, N, 1)``.
 
117
 
 
118
    See Also
 
119
    --------
 
120
    atleast_1d, atleast_2d
 
121
 
 
122
    Examples
 
123
    --------
 
124
    >>> np.atleast_3d(3.0)
 
125
    array([[[ 3.]]])
 
126
 
 
127
    >>> x = np.arange(3.0)
 
128
    >>> np.atleast_3d(x).shape
 
129
    (1, 3, 1)
 
130
 
 
131
    >>> x = np.arange(12.0).reshape(4,3)
 
132
    >>> np.atleast_3d(x).shape
 
133
    (4, 3, 1)
 
134
    >>> np.atleast_3d(x).base is x
 
135
    True
 
136
 
 
137
    >>> for arr in np.atleast_3d([1, 2], [[1, 2]], [[[1, 2]]]):
 
138
    ...     print arr, arr.shape
 
139
    ...
 
140
    [[[1]
 
141
      [2]]] (1, 2, 1)
 
142
    [[[1]
 
143
      [2]]] (1, 2, 1)
 
144
    [[[1 2]]] (1, 1, 2)
 
145
 
 
146
    """
 
147
    res = []
 
148
    for ary in arys:
 
149
        ary = asarray(ary)
 
150
        if len(ary.shape) == 0:
 
151
            result = ary.reshape(1,1,1)
 
152
        elif len(ary.shape) == 1:
 
153
            result = ary[newaxis,:,newaxis]
 
154
        elif len(ary.shape) == 2:
 
155
            result = ary[:,:,newaxis]
 
156
        else:
 
157
            result = ary
 
158
        res.append(result)
 
159
    if len(res) == 1:
 
160
        return res[0]
 
161
    else:
 
162
        return res
 
163
 
 
164
 
 
165
def vstack(tup):
 
166
    """
 
167
    Stack arrays in sequence vertically (row wise).
 
168
 
 
169
    Take a sequence of arrays and stack them vertically to make a single
 
170
    array. Rebuild arrays divided by `vsplit`.
 
171
 
 
172
    Parameters
 
173
    ----------
 
174
    tup : sequence of ndarrays
 
175
        Tuple containing arrays to be stacked. The arrays must have the same
 
176
        shape along all but the first axis.
 
177
 
 
178
    Returns
 
179
    -------
 
180
    stacked : ndarray
 
181
        The array formed by stacking the given arrays.
 
182
 
 
183
    See Also
 
184
    --------
 
185
    hstack : Stack arrays in sequence horizontally (column wise).
 
186
    dstack : Stack arrays in sequence depth wise (along third dimension).
 
187
    concatenate : Join a sequence of arrays together.
 
188
    vsplit : Split array into a list of multiple sub-arrays vertically.
 
189
 
 
190
 
 
191
    Notes
 
192
    -----
 
193
    Equivalent to ``np.concatenate(tup, axis=0)``
 
194
 
 
195
    Examples
 
196
    --------
 
197
    >>> a = np.array([1, 2, 3])
 
198
    >>> b = np.array([2, 3, 4])
 
199
    >>> np.vstack((a,b))
 
200
    array([[1, 2, 3],
 
201
           [2, 3, 4]])
 
202
 
 
203
    >>> a = np.array([[1], [2], [3]])
 
204
    >>> b = np.array([[2], [3], [4]])
 
205
    >>> np.vstack((a,b))
 
206
    array([[1],
 
207
           [2],
 
208
           [3],
 
209
           [2],
 
210
           [3],
 
211
           [4]])
 
212
 
 
213
    """
 
214
    return _nx.concatenate(map(atleast_2d,tup),0)
 
215
 
 
216
def hstack(tup):
 
217
    """
 
218
    Stack arrays in sequence horizontally (column wise).
 
219
 
 
220
    Take a sequence of arrays and stack them horizontally to make
 
221
    a single array. Rebuild arrays divided by ``hsplit``.
 
222
 
 
223
    Parameters
 
224
    ----------
 
225
    tup : sequence of ndarrays
 
226
        All arrays must have the same shape along all but the second axis.
 
227
 
 
228
    Returns
 
229
    -------
 
230
    stacked : ndarray
 
231
        The array formed by stacking the given arrays.
 
232
 
 
233
    See Also
 
234
    --------
 
235
    vstack : Stack arrays in sequence vertically (row wise).
 
236
    dstack : Stack arrays in sequence depth wise (along third axis).
 
237
    concatenate : Join a sequence of arrays together.
 
238
    hsplit : Split array along second axis.
 
239
 
 
240
    Notes
 
241
    -----
 
242
    Equivalent to ``np.concatenate(tup, axis=1)``
 
243
 
 
244
    Examples
 
245
    --------
 
246
    >>> a = np.array((1,2,3))
 
247
    >>> b = np.array((2,3,4))
 
248
    >>> np.hstack((a,b))
 
249
    array([1, 2, 3, 2, 3, 4])
 
250
    >>> a = np.array([[1],[2],[3]])
 
251
    >>> b = np.array([[2],[3],[4]])
 
252
    >>> np.hstack((a,b))
 
253
    array([[1, 2],
 
254
           [2, 3],
 
255
           [3, 4]])
 
256
 
 
257
    """
 
258
    return _nx.concatenate(map(atleast_1d,tup),1)
 
259