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

« back to all changes in this revision

Viewing changes to numpy/lib/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
 
           'column_stack','row_stack', 'dstack','array_split','split','hsplit',
 
1
__all__ = ['column_stack','row_stack', 'dstack','array_split','split','hsplit',
3
2
           'vsplit','dsplit','apply_over_axes','expand_dims',
4
3
           'apply_along_axis', 'kron', 'tile', 'get_array_wrap']
5
4
 
7
6
from numpy.core.numeric import asarray, zeros, newaxis, outer, \
8
7
     concatenate, isscalar, array, asanyarray
9
8
from numpy.core.fromnumeric import product, reshape
 
9
from numpy.core import hstack, vstack, atleast_3d
10
10
 
11
11
def apply_along_axis(func1d,axis,arr,*args):
12
12
    """
249
249
        axis = axis + len(shape) + 1
250
250
    return a.reshape(shape[:axis] + (1,) + shape[axis:])
251
251
 
252
 
 
253
 
def atleast_1d(*arys):
254
 
    """
255
 
    Convert inputs to arrays with at least one dimension.
256
 
 
257
 
    Scalar inputs are converted to 1-dimensional arrays, whilst
258
 
    higher-dimensional inputs are preserved.
259
 
 
260
 
    Parameters
261
 
    ----------
262
 
    array1, array2, ... : array_like
263
 
        One or more input arrays.
264
 
 
265
 
    Returns
266
 
    -------
267
 
    ret : ndarray
268
 
        An array, or sequence of arrays, each with ``a.ndim >= 1``.
269
 
        Copies are made only if necessary.
270
 
 
271
 
    See Also
272
 
    --------
273
 
    atleast_2d, atleast_3d
274
 
 
275
 
    Examples
276
 
    --------
277
 
    >>> np.atleast_1d(1.0)
278
 
    array([ 1.])
279
 
 
280
 
    >>> x = np.arange(9.0).reshape(3,3)
281
 
    >>> np.atleast_1d(x)
282
 
    array([[ 0.,  1.,  2.],
283
 
           [ 3.,  4.,  5.],
284
 
           [ 6.,  7.,  8.]])
285
 
    >>> np.atleast_1d(x) is x
286
 
    True
287
 
 
288
 
    >>> np.atleast_1d(1, [3, 4])
289
 
    [array([1]), array([3, 4])]
290
 
 
291
 
    """
292
 
    res = []
293
 
    for ary in arys:
294
 
        res.append(array(ary,copy=False,subok=True,ndmin=1))
295
 
    if len(res) == 1:
296
 
        return res[0]
297
 
    else:
298
 
        return res
299
 
 
300
 
def atleast_2d(*arys):
301
 
    """
302
 
    View inputs as arrays with at least two dimensions.
303
 
 
304
 
    Parameters
305
 
    ----------
306
 
    array1, array2, ... : array_like
307
 
        One or more array-like sequences.  Non-array inputs are converted
308
 
        to arrays.  Arrays that already have two or more dimensions are
309
 
        preserved.
310
 
 
311
 
    Returns
312
 
    -------
313
 
    res, res2, ... : ndarray
314
 
        An array, or tuple of arrays, each with ``a.ndim >= 2``.
315
 
        Copies are avoided where possible, and views with two or more
316
 
        dimensions are returned.
317
 
 
318
 
    See Also
319
 
    --------
320
 
    atleast_1d, atleast_3d
321
 
 
322
 
    Examples
323
 
    --------
324
 
    >>> np.atleast_2d(3.0)
325
 
    array([[ 3.]])
326
 
 
327
 
    >>> x = np.arange(3.0)
328
 
    >>> np.atleast_2d(x)
329
 
    array([[ 0.,  1.,  2.]])
330
 
    >>> np.atleast_2d(x).base is x
331
 
    True
332
 
 
333
 
    >>> np.atleast_2d(1, [1, 2], [[1, 2]])
334
 
    [array([[1]]), array([[1, 2]]), array([[1, 2]])]
335
 
 
336
 
    """
337
 
    res = []
338
 
    for ary in arys:
339
 
        res.append(array(ary,copy=False,subok=True,ndmin=2))
340
 
    if len(res) == 1:
341
 
        return res[0]
342
 
    else:
343
 
        return res
344
 
 
345
 
def atleast_3d(*arys):
346
 
    """
347
 
    View inputs as arrays with at least three dimensions.
348
 
 
349
 
    Parameters
350
 
    ----------
351
 
    array1, array2, ... : array_like
352
 
        One or more array-like sequences.  Non-array inputs are converted
353
 
        to arrays. Arrays that already have three or more dimensions are
354
 
        preserved.
355
 
 
356
 
    Returns
357
 
    -------
358
 
    res1, res2, ... : ndarray
359
 
        An array, or tuple of arrays, each with ``a.ndim >= 3``.
360
 
        Copies are avoided where possible, and views with three or more
361
 
        dimensions are returned.  For example, a 1-D array of shape ``N``
362
 
        becomes a view of shape ``(1, N, 1)``.  A 2-D array of shape ``(M, N)``
363
 
        becomes a view of shape ``(M, N, 1)``.
364
 
 
365
 
    See Also
366
 
    --------
367
 
    atleast_1d, atleast_2d
368
 
 
369
 
    Examples
370
 
    --------
371
 
    >>> np.atleast_3d(3.0)
372
 
    array([[[ 3.]]])
373
 
 
374
 
    >>> x = np.arange(3.0)
375
 
    >>> np.atleast_3d(x).shape
376
 
    (1, 3, 1)
377
 
 
378
 
    >>> x = np.arange(12.0).reshape(4,3)
379
 
    >>> np.atleast_3d(x).shape
380
 
    (4, 3, 1)
381
 
    >>> np.atleast_3d(x).base is x
382
 
    True
383
 
 
384
 
    >>> for arr in np.atleast_3d([1, 2], [[1, 2]], [[[1, 2]]]):
385
 
    ...     print arr, arr.shape
386
 
    ...
387
 
    [[[1]
388
 
      [2]]] (1, 2, 1)
389
 
    [[[1]
390
 
      [2]]] (1, 2, 1)
391
 
    [[[1 2]]] (1, 1, 2)
392
 
 
393
 
    """
394
 
    res = []
395
 
    for ary in arys:
396
 
        ary = asarray(ary)
397
 
        if len(ary.shape) == 0:
398
 
            result = ary.reshape(1,1,1)
399
 
        elif len(ary.shape) == 1:
400
 
            result = ary[newaxis,:,newaxis]
401
 
        elif len(ary.shape) == 2:
402
 
            result = ary[:,:,newaxis]
403
 
        else:
404
 
            result = ary
405
 
        res.append(result)
406
 
    if len(res) == 1:
407
 
        return res[0]
408
 
    else:
409
 
        return res
410
 
 
411
 
 
412
 
def vstack(tup):
413
 
    """
414
 
    Stack arrays in sequence vertically (row wise).
415
 
 
416
 
    Take a sequence of arrays and stack them vertically to make a single
417
 
    array. Rebuild arrays divided by `vsplit`.
418
 
 
419
 
    Parameters
420
 
    ----------
421
 
    tup : sequence of ndarrays
422
 
        Tuple containing arrays to be stacked. The arrays must have the same
423
 
        shape along all but the first axis.
424
 
 
425
 
    Returns
426
 
    -------
427
 
    stacked : ndarray
428
 
        The array formed by stacking the given arrays.
429
 
 
430
 
    See Also
431
 
    --------
432
 
    hstack : Stack arrays in sequence horizontally (column wise).
433
 
    dstack : Stack arrays in sequence depth wise (along third dimension).
434
 
    concatenate : Join a sequence of arrays together.
435
 
    vsplit : Split array into a list of multiple sub-arrays vertically.
436
 
 
437
 
 
438
 
    Notes
439
 
    -----
440
 
    Equivalent to ``np.concatenate(tup, axis=0)``
441
 
 
442
 
    Examples
443
 
    --------
444
 
    >>> a = np.array([1, 2, 3])
445
 
    >>> b = np.array([2, 3, 4])
446
 
    >>> np.vstack((a,b))
447
 
    array([[1, 2, 3],
448
 
           [2, 3, 4]])
449
 
 
450
 
    >>> a = np.array([[1], [2], [3]])
451
 
    >>> b = np.array([[2], [3], [4]])
452
 
    >>> np.vstack((a,b))
453
 
    array([[1],
454
 
           [2],
455
 
           [3],
456
 
           [2],
457
 
           [3],
458
 
           [4]])
459
 
 
460
 
    """
461
 
    return _nx.concatenate(map(atleast_2d,tup),0)
462
 
 
463
 
def hstack(tup):
464
 
    """
465
 
    Stack arrays in sequence horizontally (column wise).
466
 
 
467
 
    Take a sequence of arrays and stack them horizontally to make
468
 
    a single array. Rebuild arrays divided by ``hsplit``.
469
 
 
470
 
    Parameters
471
 
    ----------
472
 
    tup : sequence of ndarrays
473
 
        All arrays must have the same shape along all but the second axis.
474
 
 
475
 
    Returns
476
 
    -------
477
 
    stacked : ndarray
478
 
        The array formed by stacking the given arrays.
479
 
 
480
 
    See Also
481
 
    --------
482
 
    vstack : Stack arrays in sequence vertically (row wise).
483
 
    dstack : Stack arrays in sequence depth wise (along third axis).
484
 
    concatenate : Join a sequence of arrays together.
485
 
    hsplit : Split array along second axis.
486
 
 
487
 
    Notes
488
 
    -----
489
 
    Equivalent to ``np.concatenate(tup, axis=1)``
490
 
 
491
 
    Examples
492
 
    --------
493
 
    >>> a = np.array((1,2,3))
494
 
    >>> b = np.array((2,3,4))
495
 
    >>> np.hstack((a,b))
496
 
    array([1, 2, 3, 2, 3, 4])
497
 
    >>> a = np.array([[1],[2],[3]])
498
 
    >>> b = np.array([[2],[3],[4]])
499
 
    >>> np.hstack((a,b))
500
 
    array([[1, 2],
501
 
           [2, 3],
502
 
           [3, 4]])
503
 
 
504
 
    """
505
 
    return _nx.concatenate(map(atleast_1d,tup),1)
506
 
 
507
252
row_stack = vstack
508
253
 
509
254
def column_stack(tup):
892
637
        raise ValueError, 'vsplit only works on arrays of 3 or more dimensions'
893
638
    return split(ary,indices_or_sections,2)
894
639
 
 
640
def get_array_prepare(*args):
 
641
    """Find the wrapper for the array with the highest priority.
 
642
 
 
643
    In case of ties, leftmost wins. If no wrapper is found, return None
 
644
    """
 
645
    wrappers = [(getattr(x, '__array_priority__', 0), -i,
 
646
                 x.__array_prepare__) for i, x in enumerate(args)
 
647
                                   if hasattr(x, '__array_prepare__')]
 
648
    wrappers.sort()
 
649
    if wrappers:
 
650
        return wrappers[-1][-1]
 
651
    return None
 
652
 
895
653
def get_array_wrap(*args):
896
654
    """Find the wrapper for the array with the highest priority.
897
655
 
975
733
    True
976
734
 
977
735
    """
978
 
    wrapper = get_array_wrap(a, b)
979
736
    b = asanyarray(b)
980
737
    a = array(a,copy=False,subok=True,ndmin=b.ndim)
981
738
    ndb, nda = b.ndim, a.ndim
998
755
    axis = nd-1
999
756
    for _ in xrange(nd):
1000
757
        result = concatenate(result, axis=axis)
 
758
    wrapper = get_array_prepare(a, b)
 
759
    if wrapper is not None:
 
760
        result = wrapper(result)
 
761
    wrapper = get_array_wrap(a, b)
1001
762
    if wrapper is not None:
1002
763
        result = wrapper(result)
1003
764
    return result
1007
768
    """
1008
769
    Construct an array by repeating A the number of times given by reps.
1009
770
 
 
771
    If `reps` has length ``d``, the result will have dimension of
 
772
    ``max(d, A.ndim)``.
 
773
 
 
774
    If ``A.ndim < d``, `A` is promoted to be d-dimensional by prepending new
 
775
    axes. So a shape (3,) array is promoted to (1, 3) for 2-D replication,
 
776
    or shape (1, 1, 3) for 3-D replication. If this is not the desired
 
777
    behavior, promote `A` to d-dimensions manually before calling this
 
778
    function.
 
779
 
 
780
    If ``A.ndim > d``, `reps` is promoted to `A`.ndim by pre-pending 1's to it.
 
781
    Thus for an `A` of shape (2, 3, 4, 5), a `reps` of (2, 2) is treated as
 
782
    (1, 1, 2, 2).
 
783
 
1010
784
    Parameters
1011
785
    ----------
1012
786
    A : array_like
1017
791
    Returns
1018
792
    -------
1019
793
    c : ndarray
1020
 
        The output array.
 
794
        The tiled output array.
1021
795
 
1022
796
    See Also
1023
797
    --------
1024
 
    repeat
1025
 
 
1026
 
    Notes
1027
 
    -----
1028
 
    If `reps` has length d, the result will have dimension of max(d, `A`.ndim).
1029
 
 
1030
 
    If `A`.ndim < d, `A` is promoted to be d-dimensional by prepending new
1031
 
    axes. So a shape (3,) array is promoted to (1,3) for 2-D replication,
1032
 
    or shape (1,1,3) for 3-D replication. If this is not the desired behavior,
1033
 
    promote `A` to d-dimensions manually before calling this function.
1034
 
 
1035
 
    If `A`.ndim > d, `reps` is promoted to `A`.ndim by pre-pending 1's to it.
1036
 
    Thus for an `A` of shape (2,3,4,5), a `reps` of (2,2) is treated as
1037
 
    (1,1,2,2).
 
798
    repeat : Repeat elements of an array.
1038
799
 
1039
800
    Examples
1040
801
    --------
1046
807
           [0, 1, 2, 0, 1, 2]])
1047
808
    >>> np.tile(a, (2, 1, 2))
1048
809
    array([[[0, 1, 2, 0, 1, 2]],
1049
 
    <BLANKLINE>
1050
810
           [[0, 1, 2, 0, 1, 2]]])
1051
811
 
1052
812
    >>> b = np.array([[1, 2], [3, 4]])