xnumpy.core module

xnumpy.core.anumerate(new_array, axis=0, start=0, step=1, dtype=None)[source]

Builds on expand_arange, which has the same shape as the original array. Specifies the increments to occur along the given axis, which by default is the zeroth axis. Provides mechanisms for changing the starting value and also the increment. :param new_array: array to enumerate :type new_array: numpy.ndarray :param start: starting point (0 by default). :type start: int :param step: size of steps to take between

value (1 by default).
Parameters:
  • axis (int) – axis to enumerate along (0 by default).
  • dtype (type) – type to use for enumerating.
Returns:

a view of a numpy arange with

tiling in various dimension.

Return type:

(numpy.ndarray)

Examples

>>> anumerate(
...     numpy.ones((4,5), dtype=numpy.uint64)
... )
array([[0, 0, 0, 0, 0],
       [1, 1, 1, 1, 1],
       [2, 2, 2, 2, 2],
       [3, 3, 3, 3, 3]], dtype=uint64)
>>> anumerate(
...     numpy.ones((4,5), dtype=numpy.uint64),
...     axis=0
... )
array([[0, 0, 0, 0, 0],
       [1, 1, 1, 1, 1],
       [2, 2, 2, 2, 2],
       [3, 3, 3, 3, 3]], dtype=uint64)
>>> anumerate(
...     numpy.ones((4,5), dtype=numpy.uint64),
...     axis=0,
...     start=1
... )
array([[1, 1, 1, 1, 1],
       [2, 2, 2, 2, 2],
       [3, 3, 3, 3, 3],
       [4, 4, 4, 4, 4]], dtype=uint64)
>>> anumerate(
...     numpy.ones((4,5), dtype=numpy.uint64),
...     axis=0,
...     start=1,
...     step=2
... )
array([[1, 1, 1, 1, 1],
       [3, 3, 3, 3, 3],
       [5, 5, 5, 5, 5],
       [7, 7, 7, 7, 7]], dtype=uint64)
>>> anumerate(
...     numpy.ones((4,5), dtype=numpy.uint64),
...     axis=1
... )
array([[0, 1, 2, 3, 4],
       [0, 1, 2, 3, 4],
       [0, 1, 2, 3, 4],
       [0, 1, 2, 3, 4]], dtype=uint64)
>>> anumerate(
...     numpy.ones((4,5), dtype=numpy.uint64),
...     axis=1,
...     start=1
... )
array([[1, 2, 3, 4, 5],
       [1, 2, 3, 4, 5],
       [1, 2, 3, 4, 5],
       [1, 2, 3, 4, 5]], dtype=uint64)
>>> anumerate(
...     numpy.ones((4,5), dtype=numpy.uint64),
...     axis=1,
...     start=1,
...     step=2
... )
array([[1, 3, 5, 7, 9],
       [1, 3, 5, 7, 9],
       [1, 3, 5, 7, 9],
       [1, 3, 5, 7, 9]], dtype=uint64)
xnumpy.core.expand(new_array, shape_after=(), shape_before=(), read_only=True)[source]

Tack on extra dimensions of the specified size to either side.

Behaves like NumPy tile except that it always returns a view not a copy. Though, it differs in that additional dimensions are added for repetition as opposed to repeating in the same one. Also, it allows repetitions to be specified before or after unlike tile. Though, will behave identical to tile if the keyword is not specified. Uses strides to trick NumPy into providing a view.

Parameters:
  • new_array (numpy.ndarray) – array to expand
  • shape_after (tuple or int) – size of dimensions to add before the array shape (if int will turn into tuple). Defaults to an empty tuple.
  • shape_before (tuple or int) – size of dimensions to add before the array shape (if int will turn into tuple). Defaults to an empty tuple.
  • read_only (bool) –
Returns:

a view of a numpy array with

tiling in various dimension.

Return type:

(numpy.ndarray)

Examples

>>> numpy.arange(6).reshape(2,3)
array([[0, 1, 2],
       [3, 4, 5]])
>>> expand(numpy.arange(6).reshape(2,3))
array([[0, 1, 2],
       [3, 4, 5]])
>>> a = numpy.arange(6).reshape(2,3); a is expand(a)
False
>>> expand(numpy.arange(6).reshape(2,3), 1)
array([[[0],
        [1],
        [2]],

       [[3],
        [4],
        [5]]])
>>> expand(numpy.arange(6).reshape(2,3), (1,))
array([[[0],
        [1],
        [2]],

       [[3],
        [4],
        [5]]])
>>> expand(numpy.arange(6).reshape((2,3)), shape_after=1)
array([[[0],
        [1],
        [2]],

       [[3],
        [4],
        [5]]])
>>> expand(numpy.arange(6).reshape((2,3)), shape_after=(1,))
array([[[0],
        [1],
        [2]],

       [[3],
        [4],
        [5]]])
>>> expand(numpy.arange(6).reshape((2,3)), shape_before=1)
array([[[0, 1, 2],
        [3, 4, 5]]])
>>> expand(numpy.arange(6).reshape((2,3)), shape_before=(1,))
array([[[0, 1, 2],
        [3, 4, 5]]])
>>> expand(numpy.arange(6).reshape((2,3)), shape_before=(3,))
array([[[0, 1, 2],
        [3, 4, 5]],

       [[0, 1, 2],
        [3, 4, 5]],

       [[0, 1, 2],
        [3, 4, 5]]])
>>> expand(numpy.arange(6).reshape((2,3)), shape_after=(4,))
array([[[0, 0, 0, 0],
        [1, 1, 1, 1],
        [2, 2, 2, 2]],

       [[3, 3, 3, 3],
        [4, 4, 4, 4],
        [5, 5, 5, 5]]])
>>> expand(
...     numpy.arange(6).reshape((2,3)),
...     shape_before=(3,),
...     shape_after=(4,)
... )
array([[[[0, 0, 0, 0],
         [1, 1, 1, 1],
         [2, 2, 2, 2]],

        [[3, 3, 3, 3],
         [4, 4, 4, 4],
         [5, 5, 5, 5]]],


       [[[0, 0, 0, 0],
         [1, 1, 1, 1],
         [2, 2, 2, 2]],

        [[3, 3, 3, 3],
         [4, 4, 4, 4],
         [5, 5, 5, 5]]],


       [[[0, 0, 0, 0],
         [1, 1, 1, 1],
         [2, 2, 2, 2]],

        [[3, 3, 3, 3],
         [4, 4, 4, 4],
         [5, 5, 5, 5]]]])
>>> expand(numpy.arange(6).reshape((2,3)), shape_after = (4,3))
array([[[[0, 0, 0],
         [0, 0, 0],
         [0, 0, 0],
         [0, 0, 0]],

        [[1, 1, 1],
         [1, 1, 1],
         [1, 1, 1],
         [1, 1, 1]],

        [[2, 2, 2],
         [2, 2, 2],
         [2, 2, 2],
         [2, 2, 2]]],


       [[[3, 3, 3],
         [3, 3, 3],
         [3, 3, 3],
         [3, 3, 3]],

        [[4, 4, 4],
         [4, 4, 4],
         [4, 4, 4],
         [4, 4, 4]],

        [[5, 5, 5],
         [5, 5, 5],
         [5, 5, 5],
         [5, 5, 5]]]])
>>> expand(
...     numpy.arange(6).reshape((2,3)),
...     shape_before=(4,3),
... )
array([[[[0, 1, 2],
         [3, 4, 5]],

        [[0, 1, 2],
         [3, 4, 5]],

        [[0, 1, 2],
         [3, 4, 5]]],


       [[[0, 1, 2],
         [3, 4, 5]],

        [[0, 1, 2],
         [3, 4, 5]],

        [[0, 1, 2],
         [3, 4, 5]]],


       [[[0, 1, 2],
         [3, 4, 5]],

        [[0, 1, 2],
         [3, 4, 5]],

        [[0, 1, 2],
         [3, 4, 5]]],


       [[[0, 1, 2],
         [3, 4, 5]],

        [[0, 1, 2],
         [3, 4, 5]],

        [[0, 1, 2],
         [3, 4, 5]]]])
xnumpy.core.indices(shape, dtype=None)[source]

Provides a function similar to numpy.indices.

Very similar to numpy.indices except for the following.

  • Only returns a tuple of the index arrays for each dimension.
  • Returns an expanded view of a ``numpy.arange``s.

This result in drastic improvements in performance in terms of time, memory usage, and cache retrieval.

To get an equivalent result to numpy.indices, one merely need to call numpy.array(indices(shape, dtype=int)).

Parameters:
  • shape (tuple of ``int``s) – array to enumerate
  • dtype (type) – type to use for enumerating.
Returns:

a tuple of index arrays.

Return type:

tuple of ``numpy.ndarray``s

Examples

>>> indices((2, 3))  
(array([[0, 0, 0],
        [1, 1, 1]]),
 array([[0, 1, 2],
        [0, 1, 2]]))
xnumpy.core.permute_op(op, array_1, array_2)[source]

Takes two arrays and constructs a new array that contains the result of new_op on every permutation of elements in each array (like broadcasting).

Suppose that new_result contained the result, then one would find that the result of the following operation on the specific indices.

new_op( array_1[ i_1_1, i_1_2, ... ],
array_2[ i_2_1, i_2_2, ... ] )

would be found in new_result as shown

new_result[ i_1_1, i_1_2, ..., i_2_1, i_2_2, ... ]

Parameters:new_array (numpy.ndarray) – array to add the singleton axis to.
Returns:
a numpy array with the
singleton axis added at the end.
Return type:(numpy.ndarray)

Examples

>>> import operator
>>> permute_op(
...     operator.add, numpy.ones((1,3)), numpy.eye(2)
... ).shape
(1, 3, 2, 2)
>>> permute_op(
...     operator.add, numpy.ones((2,2)), numpy.eye(2)
... ).shape
(2, 2, 2, 2)
>>> permute_op(
...     operator.add, numpy.ones((2,2)), numpy.eye(2)
... )
array([[[[ 2.,  1.],
         [ 1.,  2.]],

        [[ 2.,  1.],
         [ 1.,  2.]]],


       [[[ 2.,  1.],
         [ 1.,  2.]],

        [[ 2.,  1.],
         [ 1.,  2.]]]])
>>> permute_op(
...     operator.sub, numpy.ones((2,2)), numpy.eye(2)
... )
array([[[[ 0.,  1.],
         [ 1.,  0.]],

        [[ 0.,  1.],
         [ 1.,  0.]]],


       [[[ 0.,  1.],
         [ 1.,  0.]],

        [[ 0.,  1.],
         [ 1.,  0.]]]])
>>> permute_op(
...     operator.sub, numpy.ones((2,2)), numpy.fliplr(numpy.eye(2))
... )
array([[[[ 1.,  0.],
         [ 0.,  1.]],

        [[ 1.,  0.],
         [ 0.,  1.]]],


       [[[ 1.,  0.],
         [ 0.,  1.]],

        [[ 1.,  0.],
         [ 0.,  1.]]]])
>>> permute_op(
...     operator.sub, numpy.zeros((2,2)), numpy.eye(2)
... )
array([[[[-1.,  0.],
         [ 0., -1.]],

        [[-1.,  0.],
         [ 0., -1.]]],


       [[[-1.,  0.],
         [ 0., -1.]],

        [[-1.,  0.],
         [ 0., -1.]]]])
xnumpy.core.product(arrays)[source]

Takes the cartesian product between the elements in each array.

Parameters:arrays (collections.Sequence of numpy.ndarrays) – A sequence of 1-D arrays or a 2-D array.
Returns:
an array
containing the result of the cartesian product of each array.
Return type:(numpy.ndarray)

Examples

>>> product([numpy.arange(2), numpy.arange(3)])
array([[0, 0],
       [0, 1],
       [0, 2],
       [1, 0],
       [1, 1],
       [1, 2]])
>>> product([
...     numpy.arange(2, dtype=float),
...     numpy.arange(3)
... ])
array([[ 0.,  0.],
       [ 0.,  1.],
       [ 0.,  2.],
       [ 1.,  0.],
       [ 1.,  1.],
       [ 1.,  2.]])
>>> product([
...     numpy.arange(2),
...     numpy.arange(3),
...     numpy.arange(4)
... ])
array([[0, 0, 0],
       [0, 0, 1],
       [0, 0, 2],
       [0, 0, 3],
       [0, 1, 0],
       [0, 1, 1],
       [0, 1, 2],
       [0, 1, 3],
       [0, 2, 0],
       [0, 2, 1],
       [0, 2, 2],
       [0, 2, 3],
       [1, 0, 0],
       [1, 0, 1],
       [1, 0, 2],
       [1, 0, 3],
       [1, 1, 0],
       [1, 1, 1],
       [1, 1, 2],
       [1, 1, 3],
       [1, 2, 0],
       [1, 2, 1],
       [1, 2, 2],
       [1, 2, 3]])
>>> product(numpy.diag((1, 2, 3)))
array([[1, 0, 0],
       [1, 0, 0],
       [1, 0, 3],
       [1, 2, 0],
       [1, 2, 0],
       [1, 2, 3],
       [1, 0, 0],
       [1, 0, 0],
       [1, 0, 3],
       [0, 0, 0],
       [0, 0, 0],
       [0, 0, 3],
       [0, 2, 0],
       [0, 2, 0],
       [0, 2, 3],
       [0, 0, 0],
       [0, 0, 0],
       [0, 0, 3],
       [0, 0, 0],
       [0, 0, 0],
       [0, 0, 3],
       [0, 2, 0],
       [0, 2, 0],
       [0, 2, 3],
       [0, 0, 0],
       [0, 0, 0],
       [0, 0, 3]])