pymel.util.arrays.Array¶
- class Array(...)¶
A generic n-dimensional array class using nested lists for storage.
Arrays can be built from numeric values, iterables, nested lists or other Array instances
>>> Array() Array([]) >>> Array(2) Array([2]) >>> A = Array([[1, 2], [3, 4]]) >>> print A.formated() [[1, 2], [3, 4]] >>> A = Array([1, 2], [3, 4]) >>> print A.formated() [[1, 2], [3, 4]] >>> A = Array([[1, 2]]) >>> print A.formated() [[1, 2]] >>> A = Array([1], [2], [3]) >>> print A.formated() [[1], [2], [3]] >>> A = Array([[[1], [2], [3]]]) >>> print A.formated() [[[1], [2], [3]]]
You can query some Array characteristics with the properties shape, ndim (number of dimensions) and size, the total number of numeric components
>>> A = Array(range(1, 10), shape=(3, 3)) >>> print A.formated() [[1, 2, 3], [4, 5, 6], [7, 8, 9]] >>> A.shape (3, 3) >>> A.ndim 2 >>> A.size 9
Arrays are stored as nested lists and derive from the ‘list’ class.
>>> A.data [Array([1, 2, 3]), Array([4, 5, 6]), Array([7, 8, 9])] >>> list(A) [Array([1, 2, 3]), Array([4, 5, 6]), Array([7, 8, 9])]
Initialization from another Array does a shallow copy, not a deepcopy, unless the Array argument is resized / reshaped.
>>> B = Array(A) >>> print B.formated() [[1, 2, 3], [4, 5, 6], [7, 8, 9]] >>> B == A True >>> B is A False >>> B[0] is A[0] True >>> C = Array([A]) >>> print C.formated() [[[1, 2, 3], [4, 5, 6], [7, 8, 9]]] >>> C[0] is A True >>> C[0,0] is A[0] True
You can pass optional shape information at creation with the keyword arguments shape, ndim and size. The provided data will be expanded to fit the desirable shape, either repeating it if it’s a valid sub-array of the requested shape, or padding it with the Array default value (0 unless defined otherwise in an Array sub-class).
Value will be repeated if it is a valid sub-array of the Array requested
>>> A = Array(1, shape=(2, 2)) >>> print A.formated() [[1, 1], [1, 1]]
It will be padded otherwise, with the Array class default value
>>> A = Array(1, 2, shape=(4,)) >>> print A.formated() [1, 2, 0, 0]
Or a combination of both, first pad it to a valid sub-array then repeat it
>>> A = Array(1, 2, shape=(3, 3)) >>> print A.formated() [[1, 2, 0], [1, 2, 0], [1, 2, 0]]
Repeat can occur in any dimension
>>> A = Array([1, 2, 3], shape=(3, 3)) >>> print A.formated() [[1, 2, 3], [1, 2, 3], [1, 2, 3]]
TODO : #>>> A = Array([[1], [2], [3]], shape=(3, 3)) #>>> print A.formated() #[[1, 1, 1], # [2, 2, 2], # [3, 3, 3]]
To avoid repetition, you can use a nested list of the desired number of dimensions
>>> A = Array([1,2,3], shape=(3, 3)) >>> print A.formated() [[1, 2, 3], [1, 2, 3], [1, 2, 3]] >>> A = Array([[1,2,3]], shape=(3, 3)) >>> print A.formated() [[1, 2, 3], [0, 0, 0], [0, 0, 0]]
If sub-array and requested array have same number of dimensions, padding with row / columns will be used (useful for the MatrixN sub-class or Array)
>>> A = Array(range(1, 10), shape=(3, 3)) >>> print A.formated() [[1, 2, 3], [4, 5, 6], [7, 8, 9]] >>> B = Array(A, shape=(4, 4)) >>> print B.formated() [[1, 2, 3, 0], [4, 5, 6, 0], [7, 8, 9, 0], [0, 0, 0, 0]]
Initialization will not allow to truncate data, if you provide more arguments than the requested array shape can fit, it will raise an exception. Use an explicit trim / resize or item indexing if you want to extract a sub-array
>>> A = Array([1, 2, 3, 4, 5], shape=(2, 2)) Traceback (most recent call last): ... TypeError: cannot initialize a Array of shape (2, 2) from [1, 2, 3, 4, 5] of shape (5,), as it would truncate data or reduce the number of dimensions
- T¶
The transposed array
- __abs__() <==> abs(a)¶
Element-wise absolute value of a.
>>> A = Array([[complex(1, 2), complex(2, 3)], [complex(4, 5), complex(6, 7)]]) >>> print abs(A).formated() [[2.2360679775, 3.60555127546], [6.40312423743, 9.21954445729]] >>> A = Array(-1, 2, -3) >>> print repr(abs(A)) Array([1, 2, 3])
- __add__(other)¶
a.__add__(b) <==> a+b
Returns the result of the element wise addition of a and b if b is convertible to Array, adds b to every component of a if b is a single numeric value
Note : when the operands are 2 Arrays of different shapes, both are cast to the shape of largest size if possible. Created components are filled with class default value.
Related : See the Array.__coerce__ method
>>> A = Array(range(4), shape=(2, 2)) >>> print (A).formated() [[0, 1], [2, 3]] >>> print (A+1).formated() [[1, 2], [3, 4]] >>> print (A+[1, 2]).formated() [[1, 3], [3, 5]] >>> A = Array(range(9), shape=(3, 3)) >>> M = MatrixN(range(10, 50, 10), shape=(2, 2)) >>> print (A+M).formated() [[10, 21, 2], [33, 44, 5], [6, 7, 8]] >>> print clsname(A+M) MatrixN >>> A = Array(range(10, 50, 10), shape=(2, 2)) >>> M = MatrixN(range(9), shape=(3, 3)) >>> print (A+M).formated() [[10, 21, 2], [33, 44, 5], [6, 7, 8]] >>> print clsname(A+M) MatrixN
- __coerce__(other)¶
coerce(a, b) -> (a1, b1)
Return a tuple consisting of the two numeric arguments converted to a common type and shape, using the same rules as used by arithmetic operations. If coercion is not possible, return NotImplemented.
b is cast to Array when possible
>>> A = Array(range(1, 10), shape=(3, 3)) >>> print A.formated() [[1, 2, 3], [4, 5, 6], [7, 8, 9]] >>> nA, nB = coerce(A, 1) >>> print nA.formated() [[1, 2, 3], [4, 5, 6], [7, 8, 9]] >>> print nB.formated() [[1, 1, 1], [1, 1, 1], [1, 1, 1]] >>> nA, nB = coerce(A, [1, 2, 3]) >>> print nA.formated() [[1, 2, 3], [4, 5, 6], [7, 8, 9]] >>> print nB.formated() [[1, 2, 3], [1, 2, 3], [1, 2, 3]]
Arguments can only be expanded, not truncated to avoid silent loss of data.
>>> A = Array(range(4), shape=(2, 2)) >>> nA, nB = coerce(A, [1, 2, 3, 4, 5]) Traceback (most recent call last): ... TypeError: number coercion failed
TODO : would be more explicit to get : TypeError: Array of shape (2, 2) and Array of shape (5,) cannot be converted to an common Array instance of same shape
Arrays of dissimular shape are cast to same shape when possible, smallest size is cast to largest
>>> A = Array(range(1, 10), shape=(3, 3)) >>> B = Array(range(1, 5), shape=(2, 2)) >>> print A.formated() [[1, 2, 3], [4, 5, 6], [7, 8, 9]] >>> print B.formated() [[1, 2], [3, 4]] >>> nA, nB = coerce(A, B) >>> print nA.formated() [[1, 2, 3], [4, 5, 6], [7, 8, 9]] >>> print nB.formated() [[1, 2, 0], [3, 4, 0], [0, 0, 0]]
When coerce(x, y) is not doable, it defers to coerce(y, x)
>>> nB, nA = coerce(B, A) >>> print nA.formated() [[1, 2, 3], [4, 5, 6], [7, 8, 9]] >>> print nB.formated() [[1, 2, 0], [3, 4, 0], [0, 0, 0]]
- And does not raise an excepetion like :
- Traceback (most recent call last):
- ...
TypeError: Array of shape (2, 2) and Array of shape (3, 3) cannot be converted to an common Array instance of same shape
as it could be expected without this __coerce__ mechanism.
When mixing Array derived types, result are cast to the first base class of either argument that accepts both shapes, ie ‘deepest’ derived class is tried first, MatrixN before Array, etc.
>>> A = Array(range(1, 10), shape=(3, 3)) >>> M = MatrixN(range(1, 10), shape=(3, 3)) >>> print A.formated() [[1, 2, 3], [4, 5, 6], [7, 8, 9]] >>> print M.formated() [[1, 2, 3], [4, 5, 6], [7, 8, 9]] >>> nA, nM = coerce(A, M) >>> print repr(nA) MatrixN([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) >>> print repr(nM) MatrixN([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) >>> nM, nA = coerce(M, A) >>> print repr(nA) MatrixN([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) >>> print repr(nM) MatrixN([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
This allows to implement a common behavior for element-wise arithmetics between Arrays of same or dissimilar shapes, Arrays and types derived from Arrays, Arrays and numerics or iterables of numerics.
All operators on Arrays that take 2 operands and work element-wise follow the following rules :
Operands are cast to Array when possible
>>> A = Array(range(4), shape=(2, 2)) >>> print (A).formated() [[0, 1], [2, 3]] >>> print (A+1).formated() [[1, 2], [3, 4]] >>> print (A+[1, 2]).formated() [[1, 3], [3, 5]]
Operands can only be expanded, not truncated to avoid silent loss of data.
>>> print (A+[1, 2, 3, 4, 5]).formated() Traceback (most recent call last): ... TypeError: unsupported operand type(s) for +: 'Array' and 'list'
- TODO : it would be more explicit to get more specific error messages, like :
- TypeError: Array of shape (2, 2) and Array of shape (5,) cannot be converted to an common Array instance of same shape
Arrays of dissimilar shape are cast to same shape by Array.__coerce__ if possible.
>>> A = Array(range(9), shape=(3, 3)) >>> B = Array(range(10, 50, 10), shape=(2, 2)) >>> print (A+B).formated() [[10, 21, 2], [33, 44, 5], [6, 7, 8]] >>> print clsname(A+B) Array
As Array.__coerce__ cannot truncate data, it will defer to the other operand’s __coerce__ if it exists, then to its ‘right operation’ (here __radd__) method if it exists and is defined for an Array left operand.
>>> A = Array(range(10, 50, 10), shape=(2, 2)) >>> B = Array(range(9), shape=(3, 3)) >>> print (A+B).formated() [[10, 21, 2], [33, 44, 5], [6, 7, 8]] >>> print clsname(A+B) Array
Result is cast to the first applicable Array herited type of either operand
>>> A = Array(range(9), shape=(3, 3)) >>> M = MatrixN(range(10, 50, 10), shape=(2, 2)) >>> print (A+M).formated() [[10, 21, 2], [33, 44, 5], [6, 7, 8]] >>> print clsname(A+M) MatrixN >>> print (M+A).formated() [[10, 21, 2], [33, 44, 5], [6, 7, 8]] >>> print clsname(M+A) MatrixN
>>> A = Array(range(10, 50, 10), shape=(2, 2)) >>> M = MatrixN(range(9), shape=(3, 3)) >>> print (A+M).formated() [[10, 21, 2], [33, 44, 5], [6, 7, 8]] >>> print clsname(A+M) MatrixN >>> print (M+A).formated() [[10, 21, 2], [33, 44, 5], [6, 7, 8]] >>> print clsname(M+A) MatrixN
Here result is cast to Array as a MatrixN can’t have 3 dimensions
>>> A = Array(range(10, 190, 10), shape=(2, 3, 3)) >>> M = MatrixN(range(9), shape=(3, 3)) >>> print (A+M).formated() [[[10, 21, 32], [43, 54, 65], [76, 87, 98]], [[100, 111, 122], [133, 144, 155], [166, 177, 188]]] >>> print clsname(A+M) Array >>> print (M+A).formated() [[[10, 21, 32], [43, 54, 65], [76, 87, 98]], [[100, 111, 122], [133, 144, 155], [166, 177, 188]]] >>> print clsname(M+A) Array
There are cases where no type coercion is possible, as it would truncate data or reduce number of dimensions in either way, use an explicit conversion (trim, size, etc.) in that case :
>>> A = Array(range(8), shape=(2, 2, 2)) >>> M = MatrixN(range(9), shape=(3, 3)) >>> print (A+M).formated() Traceback (most recent call last): ... TypeError: unsupported operand type(s) for +: 'Array' and 'MatrixN'
TODO : return some more explicit messages in these cases
- __contains__(value)¶
a.__contains__(b) <==> b in a
Returns True if at least one of the sub-Arrays of a (down to individual components) is equal to b, False otherwise
>>> A = Array(list(range(1, 6))+list(range(4, 0, -1)), shape=(3, 3)) >>> print A.formated() [[1, 2, 3], [4, 5, 4], [3, 2, 1]] >>> 5 in A True >>> [1, 2, 3] in A True >>> [1, 2] in A False >>> Array([[1, 2], [4, 5]]) in A False
This behavior is unlike numpy arrays (where it would return True), but like builtin list
>>> A in A False
TODO : #>>> [1, 4, 3] in A #True #>>> [[1], [4], [3]] in A #True
- __delitem__(index)¶
a.__delitem__(index) <==> del a[index]
Delete elements that match index from the Array.
Note : as opposed to a.strip(index), do not collapse dimensions of the Array that end up with only one sub-array.
>>> A = Array(xrange(1, 28), shape=(3, 3, 3)) >>> print A.formated() [[[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[10, 11, 12], [13, 14, 15], [16, 17, 18]], [[19, 20, 21], [22, 23, 24], [25, 26, 27]]] >>> A.shape (3, 3, 3) >>> S = A[0] >>> del A[1] >>> print A.formated() [[[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[19, 20, 21], [22, 23, 24], [25, 26, 27]]] >>> A.shape (2, 3, 3) >>> S == A[0] True >>> S is A[0] True >>> del A[-1] >>> print A.formated() [[[1, 2, 3], [4, 5, 6], [7, 8, 9]]] >>> A.shape (1, 3, 3) >>> del A[None, None, 1:3] >>> print A.formated() [[[1], [4], [7]]] >>> A.shape (1, 3, 1) >>> del A[None, 1:3] >>> print A.formated() [[[1]]] >>> A.shape (1, 1, 1) >>> del A[-1] >>> print A.formated() [] >>> A.shape (0,)
- __delslice__(start)¶
deprecated and __setitem__ should accept slices anyway
- __div__(other)¶
a.__div__(b) <==> a/b The division operator (/) is implemented by these methods. The __truediv__() method is used when __future__.division is in effect, otherwise __div__() is used. Returns the result of the element wise division of a by b if b is convertible to Array, divides every component of a by b if b is a single numeric value
- __eq__(other)¶
a.__equ__(b) <==> a == b
Equivalence operator, will only work for exact same type of a and b, check isEquivalent method to have it convert a and b to a common type (if possible).
>>> Array(range(4), shape=(4)) == Array(range(4), shape=(1, 4)) False >>> Array(range(4), shape=(2, 2)) == Array(range(4), shape=(2, 2)) True >>> Array(range(4), shape=(2, 2)) == MatrixN(range(4), shape=(2, 2)) False
- __floordiv__(other)¶
a.__floordiv__(b) <==> a//b Returns the result of the element wise floor division of a by b if b is convertible to Array, performs floor division of every component of a by b if b is a single numeric value
- __getitem__(index)¶
a.__getitem__(index) <==> a[index]
Get Array element from either a single (integer) or multiple (tuple) indices, supports slices.
Note : __getitem__ returns reference that can be modified unless the sub-array had to be reconstructed.
>>> A = Array(range(1, 10), shape=(3, 3)) >>> print A.formated() [[1, 2, 3], [4, 5, 6], [7, 8, 9]] >>> print repr(A[0]) Array([1, 2, 3]) >>> print repr(A[-1]) Array([7, 8, 9]) >>> print repr(A[0, 0]) 1 >>> print repr(A[-1, -1]) 9
Multiple indices and slices are supported :
>>> B = A[0:2, 0:2] >>> print B.formated() [[1, 2], [4, 5]]
When sub-arrays are not broken / rebuilt by requested indexing, a reference is returned :
>>> B = A[0:2] >>> print B.formated() [[1, 2, 3], [4, 5, 6]] >>> B[0] == A[0] True >>> B[0] is A[0] True
Missing indices are equivalent to slice(None), noted ‘:’, but as with list, a[:] returns a copy of a, not a reference to a.
>>> B = A[0:2, :] >>> print B.formated() [[1, 2, 3], [4, 5, 6]] >>> B[0] == A[0] True >>> B[0] is A[0] False
When sub-arrays are rebuilt, result is a copy.
>>> B = A[:, 0:2] >>> print B.formated() [[1, 2], [4, 5], [7, 8]] >>> print repr(B[:,0]) Array([1, 4, 7]) >>> B[:,0] == A[:, 0] True >>> B[:,0] is A[:, 0] False
Use __setindex__ to change the value of an indexed element in that case
>>> A[:, 0:2] += 10 >>> print A.formated() [[11, 12, 3], [14, 15, 6], [17, 18, 9]]
- __getslice__(start, end)¶
Deprecated and __getitem__ should accept slices anyway
- __iadd__(other)¶
a.__iadd__(b) <==> a += b
In place addition of a and b, see __add__, result must fit a’s type
>>> A = Array(range(9), shape=(3, 3)) >>> M = MatrixN(range(10, 50, 10), shape=(2, 2)) >>> A += M >>> print A.formated() [[10, 21, 2], [33, 44, 5], [6, 7, 8]] >>> print clsname(A) Array >>> A = Array(range(9), shape=(3, 3)) >>> M = MatrixN(range(10, 50, 10), shape=(2, 2)) >>> M += A >>> print M.formated() [[10, 21, 2], [33, 44, 5], [6, 7, 8]] >>> print clsname(M) MatrixN
Result must be castable to the type of a
>>> A = Array(range(12), shape=(2, 3, 2)) >>> M = MatrixN(range(9), shape=(3, 3)) >>> B = M + A >>> print B.formated() [[[0, 2], [4, 6], [8, 10]], [[12, 14], [16, 9], [10, 11]]] >>> print clsname(B) Array >>> M += A Traceback (most recent call last): ... TypeError: cannot cast a Array of shape (2, 3, 2) to a MatrixN of shape (2, 6), as it would truncate data or reduce the number of dimensions
- __idiv__(other)¶
a.__idiv__(b) <==> a /= b The division operator (/) is implemented by these methods. The __truediv__() method is used when __future__.division is in effect, otherwise __div__() is used. In place division of a by b, see __div__, result must fit a’s type
- __ifloordiv__(other)¶
a.__ifloordiv__(b) <==> a //= b In place true division of a by b, see __floordiv__, result must fit a’s type
- __imod__(other)¶
a.__imod__(b) <==> a %= b In place modulo of a by b, see __mod__, result must fit a’s type
- __imul__(other)¶
a.__imul__(b) <==> a *= b In place multiplication of a and b, see __mul__, result must fit a’s type
- __invert__()¶
a.__invert__() <==> ~a
Element-wise invert of a, as with ‘~’, operator ‘invert’
>>> A = Array(range(4), shape=(2, 2)) >>> print (~A).formated() [[-1, -2], [-3, -4]]
- __ipow__(other, modulo=None)¶
a.__ipow__(b[, modulo]) <==> a**=b or a = (a**b) % modulo In place elevation to power of a by b, see __pow__, result must fit a’s type
- __isub__(other)¶
a.__isub__(b) <==> a -= b In place substraction of a and b, see __sub__, result must fit a’s type
- __iter__(*args, **kwargs) <==> iter(a, *args, **kwargs)¶
Default Array storage class iterator, operates on first axis only
>>> A = Array(range(1, 10), shape=(3, 3)) >>> print A.formated() [[1, 2, 3], [4, 5, 6], [7, 8, 9]] >>> [a for a in A] [Array([1, 2, 3]), Array([4, 5, 6]), Array([7, 8, 9])]
- __itruediv__(other)¶
a.__itruediv__(b) <==> a /= b In place true division of a by b, see __truediv__, result must fit a’s type
- __len__() <==> len(a)¶
Length of the first dimension of the array, ie len of the array considered as the top level list, thus len(a) == a.shape[0].
>>> Array(shape=(3, 2)).__len__() 3
- __mod__(other)¶
a.__mod__(b) <==> a%b Returns the result of the element wise modulo of a by b if b is convertible to Array, performs modulo of every component of a by b if b is a single numeric value
- __mul__(other)¶
a.__mul__(b) <==> a*b Returns the result of the element wise multiplication of a and b if b is convertible to Array, multiplies every component of a by b if b is a single numeric value
- __ne__(other)¶
a.__ne__(b) <==> a != b
a.__ne__(b) returns not a.__equ__(b).
>>> Array(range(4), shape=(4)) != Array(range(4), shape=(1, 4)) True >>> Array(range(4), shape=(2, 2)) != Array(range(4), shape=(2, 2)) False >>> Array(range(4), shape=(2, 2)) != MatrixN(range(4), shape=(2, 2)) True
- __neg__()¶
a.__neg__() <==> -a
Element-wise negation of a
>>> A = Array(range(4), shape=(2, 2)) >>> print (-A).formated() [[0, -1], [-2, -3]]
- __neq__(other)¶
a.__ne__(b) <==> a != b
a.__ne__(b) returns not a.__equ__(b).
>>> Array(range(4), shape=(4)) != Array(range(4), shape=(1, 4)) True >>> Array(range(4), shape=(2, 2)) != Array(range(4), shape=(2, 2)) False >>> Array(range(4), shape=(2, 2)) != MatrixN(range(4), shape=(2, 2)) True
- __pos__()¶
a.__pos__() <==> +a
Element-wise positive of a
>>> A = Array(range(4), shape=(2, 2)) >>> print (+A).formated() [[0, 1], [2, 3]]
- __pow__(other, modulo=None)¶
a.__pow__(b[, modulo]) <==> a**b or (a**b) % modulo With two arguments, equivalent to a**b. With three arguments, equivalent to (a**b) % modulo, but may be more efficient (e.g. for longs). Returns the result of the element wise elevation to power of a by b if b is convertible to Array, elevates every component of a to power b if b is a single numeric value
- __radd__(other)¶
a.__radd__(b) <==> b+a
Returns the result of the element wise addition of a and b if b is convertible to Array, adds b to every component of a if b is a single numeric value
Note : when the operands are 2 Arrays of different shapes, both are cast to the shape of largest size if possible. Created components are filled with class default value.
Related : See the Array.__coerce__ method
>>> A = Array(range(4), shape=(2, 2)) >>> print (A).formated() [[0, 1], [2, 3]] >>> print (1+A).formated() [[1, 2], [3, 4]] >>> print ([1, 2]+A).formated() [[1, 3], [3, 5]] >>> A = Array(range(9), shape=(3, 3)) >>> M = MatrixN(range(10, 50, 10), shape=(2, 2)) >>> print (M+A).formated() [[10, 21, 2], [33, 44, 5], [6, 7, 8]] >>> print clsname(M+A) MatrixN >>> A = Array(range(10, 50, 10), shape=(2, 2)) >>> M = MatrixN(range(9), shape=(3, 3)) >>> print (M+A).formated() [[10, 21, 2], [33, 44, 5], [6, 7, 8]] >>> print clsname(M+A) MatrixN
- __rdiv__(other)¶
a.__rdiv__(b) <==> b/a The division operator (/) is implemented by these methods. The __truediv__() method is used when __future__.division is in effect, otherwise __div__() is used. Returns the result of the element wise division of b by a if b is convertible to Array, replaces every component c of a by b/c if b is a single numeric value
- __reduce__()¶
__reduce__ is defined to allow pickling of Arrays
- __rfloordiv__(other)¶
a.__rfloordiv__(b) <==> b//a Returns the result of the element wise floor division of b by a if b is convertible to Array, replaces every component c of a by b//c if b is a single numeric value
- __rmod__(other)¶
a.__rmod__(b) <==> b%a Returns the result of the element wise modulo of b by a if b is convertible to Array, replaces every component c of a by b%c if b is a single numeric value
- __rmul__(other)¶
a.__mul__(b) <==> b*a Returns the result of the element wise multiplication of a and b if b is convertible to Array, multiplies every component of a by b if b is a single numeric value
- __round__([ndigits]) <==> round(a[, ndigits])¶
Element-wise round to given precision in decimal digits (default 0 digits). This always returns an Array of floating point numbers. Precision may be negative.
>>> A = Array([1.0/x for x in range(1, 10)], shape=(3, 3)) >>> print round(A, 2).formated() [[1.0, 0.5, 0.33], [0.25, 0.2, 0.17], [0.14, 0.13, 0.11]]
- __rpow__(other)¶
a.__rpow__(b[, modulo]) <==> b**a or (b**a) % modulo With two arguments, equivalent to b**a. With three arguments, equivalent to (b**a) % modulo, but may be more efficient (e.g. for longs). Returns the result of the element wise elevation to power of b by a if b is convertible to Array, replaces every component c of a by b elevated to power c if b is a single numeric value
- __rsub__(other)¶
a.__rsub__(b) <==> b-a Returns the result of the element wise substraction of a from b if b is convertible to Array, replace every component c of a by b-c if b is a single numeric value
- __rtruediv__(other)¶
a.__rtruediv__(b) <==> b/a The division operator (/) is implemented by these methods. The __rtruediv__() method is used when __future__.division is in effect, otherwise __rdiv__() is used. Returns the result of the element wise true division of b by a if b is convertible to Array, replaces every component c of a by b/c if b is a single numeric value
- __setitem__(index, value)¶
a.__setitem__(index, value) <==> a[index] = value
Set Array element from either a single (integer) or multiple (tuple) indices, supports slices.
Note : if value is not reshaped / resized, it’s a reference to value that is set at the indexed element, use an explicit deepcopy
>>> A = Array(range(1, 10), shape=(3, 3)) >>> print A.formated() [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
If value doesn’t have to be rebuilt, the indexed elements will hold a reference to value, otherwise a copy
>>> S = Array([0, 0, 0]) >>> A[0] = S >>> print A.formated() [[0, 0, 0], [4, 5, 6], [7, 8, 9]] >>> A[0] == S True >>> A[0] is S True >>> A[:, 2] = S >>> print A.formated() [[0, 0, 0], [4, 5, 0], [7, 8, 0]] >>> A[:, 2] == S True >>> A[:, 2] is S False
Multiple indices and slices are supported :
>>> A[0] = [2, 4, 6] >>> print A.formated() [[2, 4, 6], [4, 5, 0], [7, 8, 0]] >>> A[1, 1] = 10 >>> print A.formated() [[2, 4, 6], [4, 10, 0], [7, 8, 0]] >>> A[:, -1] = [7, 8, 9] >>> print A.formated() [[2, 4, 7], [4, 10, 8], [7, 8, 9]] >>> A[:, 0:2] += 10 >>> print A.formated() [[12, 14, 7], [14, 20, 8], [17, 18, 9]]
Value is expanded / repeated as necessary to fit the indexed sub-array
>>> A[0:2, 0:2] = 1 >>> print A.formated() [[1, 1, 7], [1, 1, 8], [17, 18, 9]] >>> A[1:3, :] = [1, 2] >>> print A.formated() [[1, 1, 7], [1, 2, 0], [1, 2, 0]] >>> A[0:2, 1:3] = [1, 2] >>> print A.formated() [[1, 1, 2], [1, 1, 2], [1, 2, 0]] >>> A[0:2, 1:3] = [[1], [2]] >>> print A.formated() [[1, 1, 0], [1, 2, 0], [1, 2, 0]]
It cannot be truncated however
>>> A[0] = [1, 2, 3, 4] Traceback (most recent call last): ... ValueError: shape mismatch between value(s) and Array components or sub Arrays designated by the indexing
- __setslice__(start, end, value)¶
Deprecated and __setitem__ should accept slices anyway
- __sub__(other)¶
a.__sub__(b) <==> a-b Returns the result of the element wise substraction of b from a if b is convertible to Array, substracts b from every component of a if b is a single numeric value
- __truediv__(other)¶
a.__truediv__(b) <==> a/b The division operator (/) is implemented by these methods. The __truediv__() method is used when __future__.division is in effect, otherwise __div__() is used. Returns the result of the element wise true division of a by b if b is convertible to Array, performs true division of every component of a by b if b is a single numeric value
- all([axis0[, axis1[, ...]]]) <=> all(a, axis=(axis0, axis1, ...))¶
Returns True if all the components of iterable a evaluate to True. If axis are specified will return an Array of all(x) for x in a.axisiter(*axis).
>>> A = Array([[True,True,True],[False,True,False]]) >>> print A.formated() [[True, True, True], [False, True, False]] >>> A.all() False >>> A.all(0, 1) False >>> A.all(0) Array([False, True, False]) >>> A.all(1) Array([True, False])
- any([axis0[, axis1[, ...]]]) <=> any(a, axis=(axis0, axis1, ...))¶
Returns True if any of the components of iterable a evaluate to True. If axis are specified will return an Array of any(x) for x in a.axisiter(*axis).
>>> A = Array([[False,True,True],[False,True,False]]) >>> print A.formated() [[False, True, True], [False, True, False]] >>> A.any() True >>> A.any(0, 1) True >>> A.any(0) Array([False, True, True]) >>> A.any(1) Array([True, True])
- apicls¶
alias of list
- append(b[, axis=0])¶
Modifies a by appending b at its end, as iterated on axis.
Note : does not work as list append and appends a copy (deepcopy) of b, not a reference to b. However a is appended in place.
Examples:
>>> A = Array([]) >>> print repr(A) Array([]) >>> A.append(1) >>> print A.formated() [1] >>> A.append(2) >>> print A.formated() [1, 2] >>> A = Array([A]) >>> print A.formated() [[1, 2]] >>> A.append([4, 5], axis=0) >>> print A.formated() [[1, 2], [4, 5]] >>> A.append([3, 6], axis=1) >>> print A.formated() [[1, 2, 3], [4, 5, 6]] >>> A.append([7, 8, 9]) >>> print A.formated() [[1, 2, 3], [4, 5, 6], [7, 8, 9]] >>> B = Array([A]) >>> B.append(A) >>> print B.formated() [[[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[1, 2, 3], [4, 5, 6], [7, 8, 9]]] >>> B[0] == B[1] True >>> B[0] is B[1] False >>> A == B[0] True >>> A is B[0] True >>> B.append([0, 0, 0], axis=1) >>> print B.formated() [[[1, 2, 3], [4, 5, 6], [7, 8, 9], [0, 0, 0]], [[1, 2, 3], [4, 5, 6], [7, 8, 9], [0, 0, 0]]] >>> B.append([0, 0, 0, 1], axis=2) >>> print B.formated() [[[1, 2, 3, 0], [4, 5, 6, 0], [7, 8, 9, 0], [0, 0, 0, 1]], [[1, 2, 3, 0], [4, 5, 6, 0], [7, 8, 9, 0], [0, 0, 0, 1]]]
- appended(other, axis=0)¶
a.appended(b[, axis=0]) –> Array
Returns the Array obtained by appending b at the end of a as iterated on axis.
Note : returns a deepcopy of a.appends(b[, axis=0]).
Examples:
>>> A = Array([]) >>> print repr(A) Array([]) >>> A = A.appended(1) >>> print A.formated() [1] >>> A = A.appended(2) >>> print A.formated() [1, 2] >>> A = Array([A]) >>> print A.formated() [[1, 2]] >>> A = A.appended([4, 5], axis=0) >>> print A.formated() [[1, 2], [4, 5]] >>> A = A.appended([3, 6], axis=1) >>> print A.formated() [[1, 2, 3], [4, 5, 6]] >>> A = A.appended([7, 8, 9]) >>> print A.formated() [[1, 2, 3], [4, 5, 6], [7, 8, 9]] >>> B = Array([A]).appended(A) >>> print B.formated() [[[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[1, 2, 3], [4, 5, 6], [7, 8, 9]]] >>> B[0] == B[1] True >>> B[0] is B[1] False >>> A == B[0] True >>> A is B[0] False >>> B = B.appended([0, 0, 0], axis=1) >>> print B.formated() [[[1, 2, 3], [4, 5, 6], [7, 8, 9], [0, 0, 0]], [[1, 2, 3], [4, 5, 6], [7, 8, 9], [0, 0, 0]]] >>> B = B.appended([0, 0, 0, 1], axis=2) >>> print B.formated() [[[1, 2, 3, 0], [4, 5, 6, 0], [7, 8, 9, 0], [0, 0, 0, 1]], [[1, 2, 3, 0], [4, 5, 6, 0], [7, 8, 9, 0], [0, 0, 0, 1]]]
- assign(value)¶
a.assign(b) –> Array
Assigns the value of b to a, equivalent to using the data property : a.data = b. Besides changing a’s value, it also returns the new a to conform to Maya’s api assign.
Note: assign acts as a shallow copy
>>> A = Array(range(1, 5), shape=(2, 2)) >>> B = Array() >>> B.assign(A) Array([[1, 2], [3, 4]]) >>> print B.formated() [[1, 2], [3, 4]] >>> B == A True >>> B is A False >>> B[0] is A[0] True
- axisiter(*args)¶
a.axisiter([axis1[, axis2[, ...]]]) –> ArrayIter
Returns an iterator using a specific axis or list of ordered axis. It is equivalent to transposing the Array using these ordered axis and iterating on the new Array for the remaining sub array dimension
Note : ArrayIter ierators support __len__, __getitem__ and __setitem__
>>> A = Array(range(1, 10), shape=(3, 3)) >>> print A.formated() [[1, 2, 3], [4, 5, 6], [7, 8, 9]] >>> [a for a in A] [Array([1, 2, 3]), Array([4, 5, 6]), Array([7, 8, 9])] >>> [a for a in A.axisiter(0)] [Array([1, 2, 3]), Array([4, 5, 6]), Array([7, 8, 9])] >>> [a for a in A.axisiter(1)] [Array([1, 4, 7]), Array([2, 5, 8]), Array([3, 6, 9])] >>> [a for a in A.axisiter(0,1)] [1, 2, 3, 4, 5, 6, 7, 8, 9] >>> [a for a in A.axisiter(1,0)] [1, 4, 7, 2, 5, 8, 3, 6, 9]
- blend(b[, weight=0.5]) <==> blend(a, b[, weights=0.5])¶
Returns the result of blending from Array instance u to v according to either a scalar weight where blend will yield a*(1-weight) + b*weight Array, or a an iterable of independent weights.
>>> A = Array(0, shape=(2, 2)) >>> print A.formated() [[0, 0], [0, 0]] >>> B = Array(1, shape=(2, 2)) >>> print B.formated() [[1, 1], [1, 1]] >>> print A.blend(B, weight=0.5).formated() [[0.5, 0.5], [0.5, 0.5]] >>> print blend(A, B).formated() [[0.5, 0.5], [0.5, 0.5]] >>> print blend(A, B, weight=[x/4.0 for x in range(4)]).formated() [[0.0, 0.25], [0.5, 0.75]] >>> print blend(A, B, weight=[[0.0, 0.25],[0.75, 1.0]]).formated() [[0.0, 0.25], [0.75, 1.0]]
- clamp([low=0[, high=1]]) <==> clamp (a, low, high)¶
Returns the result of clamping each component of a between low and high if low and high are scalars, or the corresponding components of low and high if low and high are sequences of scalars
>>> A = Array(range(4), shape=(2, 2)) >>> print A.formated() [[0, 1], [2, 3]] >>> print A.clamp(1, 2).formated() [[1, 1], [2, 2]] >>> print clamp(A, 1, 2).formated() [[1, 1], [2, 2]] >>> print clamp(A, 0.0, [x/4.0 for x in range(4)]).formated() [[0, 0.25], [0.5, 0.75]]
- conjugate() <==> conjugate(a)¶
Returns the element-wise complex.conjugate() of the Array.
>>> A = Array([[complex(1, 2), complex(2, 3)], [complex(4, 5), complex(6, 7)]]) >>> print A.formated() [[(1+2j), (2+3j)], [(4+5j), (6+7j)]] >>> print A.conjugate().formated() [[(1-2j), (2-3j)], [(4-5j), (6-7j)]] >>> print conjugate(A).formated() [[(1-2j), (2-3j)], [(4-5j), (6-7j)]] >>> A = Array(range(1, 5), shape=(2, 2)) >>> print conjugate(A).formated() [[1, 2], [3, 4]]
- copy() <==> copy.copy(a)¶
Returns a shallow copy of a
>>> A = Array(range(1, 10), shape=(3, 3)) >>> B = A.copy() >>> print B.formated() [[1, 2, 3], [4, 5, 6], [7, 8, 9]] >>> print B == A True >>> print B is A False >>> print B[0] == A[0] True >>> print B[0] is A[0] True
- count(value)¶
a.count(b) –> int
Returns the number of occurrences of b in a.
>>> A = Array(list(range(1, 6))+list(range(4, 0, -1)), shape=(3, 3)) >>> print A.formated() [[1, 2, 3], [4, 5, 4], [3, 2, 1]] >>> A.count(5) 1 >>> A.count(4) 2 >>> A.count([1, 2, 3]) 1 >>> A.count([1, 2]) 0
- data¶
The nested list storage for the Array data
- deepcopy() <==> copy.deepcopy(a)¶
Returns a deep copy of a
>>> A = Array(range(1, 10), shape=(3, 3)) >>> B = A.deepcopy() >>> print B.formated() [[1, 2, 3], [4, 5, 6], [7, 8, 9]] >>> print B == A True >>> print B is A False >>> print B[0] == A[0] True >>> print B[0] is A[0] False
- deleted(*args)¶
a.deleted(index) –> Array
Returns a copy (deepcopy) of a with the elements designated by index deleted, as in a.__delitem__(index).
Note : as opposed to a.stripped(index), do not collapse dimensions of the Array that end up with only one sub-array.
>>> A = Array(xrange(1, 28), shape=(3, 3, 3)) >>> print A.formated() [[[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[10, 11, 12], [13, 14, 15], [16, 17, 18]], [[19, 20, 21], [22, 23, 24], [25, 26, 27]]] >>> A.shape (3, 3, 3) >>> B = A.deleted(1) >>> print B.formated() [[[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[19, 20, 21], [22, 23, 24], [25, 26, 27]]] >>> B.shape (2, 3, 3) >>> B[0] == A[0] True >>> B[0] is A[0] False >>> B = B.deleted(-1) >>> print B.formated() [[[1, 2, 3], [4, 5, 6], [7, 8, 9]]] >>> B.shape (1, 3, 3) >>> B = B.deleted(None, None, slice(1, 3)) >>> print B.formated() [[[1], [4], [7]]] >>> B.shape (1, 3, 1) >>> B = B.deleted((None, slice(1, 3))) >>> print B.formated() [[[1]]] >>> B.shape (1, 1, 1) >>> B = B.deleted(-1) >>> print B.formated() [] >>> B.shape (0,)
- dist(b, axis0, axis1, ...) <==> dist(a, b[, axis=(axis0, axis1, ...)])¶
Returns the distance between a and b, ie length(b-a, axis)
>>> A = Array([[0.5, 0.5, -0.707],[0.707, -0.707, 0.0]]) >>> print A.formated() [[0.5, 0.5, -0.707], [0.707, -0.707, 0.0]] >>> B = Array([[0.51, 0.49, -0.71],[0.71, -0.70, 0.0]]) >>> print B.formated() [[0.51, 0.49, -0.71], [0.71, -0.7, 0.0]] >>> A.dist(B) 0.016340134638368205 >>> A.dist(B, 0, 1) 0.016340134638368205 >>> A.dist(B, 0) Array([0.0144568322948, 0.00761577310586]) >>> A.dist(B, 1) Array([0.0104403065089, 0.0122065556157, 0.003])
- distanceTo(b) <==> a.dist(b)¶
Equivalent to the dist method, for compatibility with Maya’s API. Does not take axis arguements
- extend(other)¶
a.vstack(b) <==> a.stack(b, axis=0)
Modifies a by concatenating b at its end, as iterated on first axis. For a 2 dimensional Array/MatrixN, it stacks a and b vertically
>>> A = Array([[1, 2], [3, 4]]) >>> print A.formated() [[1, 2], [3, 4]] >>> A.vstack([[5, 6]]) >>> print A.formated() [[1, 2], [3, 4], [5, 6]]
- extended(other)¶
a.vstacked(b) <==> a.stacked(b, axis=0)
Returns the Array obtained by concatenating a and b on first axis. For a 2 dimensional Array/MatrixN, it stacks a and b vertically.
>>> A = Array([[1, 2], [3, 4]]) >>> print A.formated() [[1, 2], [3, 4]] >>> A = A.vstacked([[5, 6]]) >>> print A.formated() [[1, 2], [3, 4], [5, 6]]
- fill([value])¶
Fills the array in place with the given value, if no value is given a is filled with the default class values
Note : value is copied (deepcopy) as many times as it is inserted in a, not referenced.
Examples:
>>> A = Array(shape=(3, 3)) >>> print A.formated() [[0, 0, 0], [0, 0, 0], [0, 0, 0]] >>> A.fill(10) >>> print A.formated() [[10, 10, 10], [10, 10, 10], [10, 10, 10]] >>> A.fill() >>> print A.formated() [[0, 0, 0], [0, 0, 0], [0, 0, 0]] >>> A.fill([1, 2]) >>> print A.formated() [[1, 2, 0], [1, 2, 0], [1, 2, 0]] >>> A.fill([1, 2, 3]) >>> print A.formated() [[1, 2, 3], [1, 2, 3], [1, 2, 3]] >>> A[0] == A[-1] True >>> A[0] is A[-1] False
- filled(value=None)¶
a.filled([value]) –> Array
Returns a copy (deepcopy) of a, filled with value for a’s shape. If no value is given, a is filled with the class default. value will be expended with the class default values to the nearest matching sub array of a, then repeated. value can’t be truncated and will raise an error if of a size superior to the size of the nearest matching sub array of the class, to avoid improper casts.
Note : value is copied (deepcopy) as many times as it is inserted in a, not referenced.
Examples:
>>> Array(shape=(5,)).filled([0, 1, 2]) Array([0, 1, 2, 0, 0]) >>> Array(shape=(5,)).filled(2) Array([2, 2, 2, 2, 2]) >>> print Array(shape=(2, 2)).filled(1).formated() [[1, 1], [1, 1]] >>> A = Array(shape=(3, 3)).filled([1, 2, 3]) >>> print A.formated() [[1, 2, 3], [1, 2, 3], [1, 2, 3]] >>> A[0] == A[-1] True >>> A[0] is A[-1] False >>> A = Array(shape=(3, 3)).filled([1, 2]) >>> print A.formated() [[1, 2, 0], [1, 2, 0], [1, 2, 0]] >>> Array(shape=(2, 2)).filled([1, 2, 3]) Traceback (most recent call last): ... ValueError: value of shape (3,) cannot be fit in a Array of shape (2, 2), some data would be lost
- fit(b)¶
Fits the Array b in a. For every component of a that exists in b (there is a component of same coordinates in b), replace it with the value of the corresponding component in b. Both Arrays a and b must have same number of dimensions.
Note : copies (deepcopy) of b sub-arrays are fit in a, not references, but modification of a is done in-place.
>>> A = Array(range(1, 10), shape=(3, 3)) >>> print A.formated() [[1, 2, 3], [4, 5, 6], [7, 8, 9]] >>> B = Array(shape=(4, 3)) >>> print B.formated() [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]] >>> S = B[-1] >>> B.fit(A) >>> print B.formated() [[1, 2, 3], [4, 5, 6], [7, 8, 9], [0, 0, 0]] >>> B[0] == A[0] True >>> B[0] is A[0] False >>> S == B[-1] True >>> S is B[-1] True >>> B = Array(shape=(4, 4)) >>> B.fit(A) >>> print B.formated() [[1, 2, 3, 0], [4, 5, 6, 0], [7, 8, 9, 0], [0, 0, 0, 0]] >>> B = Array(shape=(2, 2)) >>> B.fit(A) >>> print B.formated() [[1, 2], [4, 5]]
- fitted(other)¶
a.fitted(b) –> Array
Returns the result of fitting the Array b in a. For every component of a that exists in b (there is a component of same coordinates in b), replace it with the value of the corresponding component in b. Both Arrays a and b must have same number of dimensions.
Note : returns a copy (deepcopy) of a.fit(b)
>>> A = Array(range(1, 10), shape=(3, 3)) >>> print A.formated() [[1, 2, 3], [4, 5, 6], [7, 8, 9]] >>> B = Array(shape=(4, 3)) >>> print B.formated() [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]] >>> C = B.fitted(A) >>> print C.formated() [[1, 2, 3], [4, 5, 6], [7, 8, 9], [0, 0, 0]] >>> C[0] == A[0] True >>> C[0] is A[0] False >>> C[-1] == B[-1] True >>> C[-1] is B[-1] False >>> B = Array(shape=(4, 4)).fitted(A) >>> print B.formated() [[1, 2, 3, 0], [4, 5, 6, 0], [7, 8, 9, 0], [0, 0, 0, 0]] >>> B = Array(shape=(2, 2)).fitted(A) >>> print B.formated() [[1, 2], [4, 5]]
- flat¶
a.flat –> ArrayIter
Flat iterator on all components of the Array
Note : ArrayIter iterators support __len__, __getitem__ and __setitem__
>>> A = Array(range(1, 10), shape=(3, 3)) >>> print A.formated() [[1, 2, 3], [4, 5, 6], [7, 8, 9]] >>> [a for a in A] [Array([1, 2, 3]), Array([4, 5, 6]), Array([7, 8, 9])] >>> [a for a in A.flat] [1, 2, 3, 4, 5, 6, 7, 8, 9] >>> A.flat[5:10] = [4, 3, 2, 1] >>> print A.formated() [[1, 2, 3], [4, 5, 4], [3, 2, 1]]
- formated()¶
a.formated() –> str
Returns a string representing a formated output of Array a
>>> A = Array(range(1, 10), shape=(3, 3)) >>> print A.formated() [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
- get()¶
a.get() –> Tuple
Returns a’s internally stored value as a nested tuple, a raw dump of the stored numeric components.
>>> A = Array(range(1, 5), shape=(2, 2)) >>> print A.get() ((1, 2), (3, 4))
- hstack(b) <==> a.stack(b, axis=-1)¶
Modifies a by concatenating b at its end, as iterated on last axis. For a 2 dimensional Array/MatrixN, it stacks a and b horizontally.
>>> A = Array([[1, 2], [4, 5]]) >>> print A.formated() [[1, 2], [4, 5]] >>> A.hstack([[3], [6]]) >>> print A.formated() [[1, 2, 3], [4, 5, 6]]
- hstacked(b) <==> a.stacked(b, axis=-1)¶
Returns the Array obtained by concatenating a and b on last axis. For a 2 dimensional Array/MatrixN, it stacks a and b horizontally.
>>> A = Array([[1, 2], [4, 5]]) >>> print A.formated() [[1, 2], [4, 5]] >>> A = A.hstacked([[3], [6]]) >>> print A.formated() [[1, 2, 3], [4, 5, 6]]
- imag()¶
a.real() <==> real(a)
Returns the element-wise complex imaginary part of the Array.
>>> A = Array([[complex(1, 2), complex(2, 3)], [complex(4, 5), complex(6, 7)]]) >>> print A.formated() [[(1+2j), (2+3j)], [(4+5j), (6+7j)]] >>> print A.imag().formated() [[2.0, 3.0], [5.0, 7.0]] >>> print imag(A).formated() [[2.0, 3.0], [5.0, 7.0]] >>> A = Array(range(1, 5), shape=(2, 2)) >>> print imag(A).formated() [[0, 0], [0, 0]]
- index(value)¶
a.index(b) –> int or tuple
Returns the index of the first occurrence of b in a.
>>> A = Array(list(range(1, 6))+list(range(4, 0, -1)), shape=(3, 3)) >>> print A.formated() [[1, 2, 3], [4, 5, 4], [3, 2, 1]] >>> A.index(5) (1, 1) >>> A.index(4) (1, 0) >>> A.index([1, 2, 3]) (0,) >>> A.index([1, 2]) Traceback (most recent call last): ... ValueError: Array.index(x): x not in Array
- isEquivalent(other, tol=9.313225746154785e-10)¶
a.isEquivalent(b[, tol]) –> bool
Returns True if both arguments have same shape and distance between both Array arguments is inferior or equal to tol.
>>> A = Array([[0.5,0.5,-0.707],[0.707,-0.707,0]]) >>> B = Array([[0.51,0.49,-0.71],[0.71,-0.70,0]]) >>> C = Array([[0.501,0.499,-0.706],[0.706,-0.708,0.01]]) >>> A.dist(B) 0.016340134638368205 >>> A.dist(C) 0.010246950765959599 >>> A.isEquivalent(C, 0.015) True >>> A.isEquivalent(B, 0.015) False >>> A.isEquivalent(B, 0.020) True
- length(axis0, axis1, ...) <==> length(a[, axis=(axis0, axis1, ...)])¶
Returns length of a, sqrt(a*a) or the square root of the sum of x*x for x in a if a is an iterable of numeric values. If a is an Array and axis are specified will return a list of length(x) for x in a.axisiter(*axis).
>>> A = Array([[0.5,0.5,-0.707],[0.707,-0.707,0.0]]) >>> print A.formated() [[0.5, 0.5, -0.707], [0.707, -0.707, 0.0]] >>> round(A.length(), 7) 1.4140534 >>> round(A.length(0,1), 7) 1.4140534 >>> A.length(0) Array([0.99992449715, 0.999848988598]) >>> A.length(1) Array([0.865938219505, 0.865938219505, 0.707])
- max([axis0[, axis1[, ...[, key=func]]]]) <==> max(a[, key=func[, axis=(axis0, axis1, ...)]])¶
Returns the greatest component of a. If axis are specified will return an Array of element-wise max(x) for x in a.axisiter(*axis).
>>> A = Array([[6,3,4],[1,5,0.5]]) >>> print A.formated() [[6, 3, 4], [1, 5, 0.5]] >>> A.max() 6 >>> A.max(0,1) 6 >>> A.max(0) Array([6, 5, 4]) >>> A.max(1) Array([6, 5])
- min([axis0[, axis1[, ...[, key=func]]]]) <==> min(a[, key=func[, axis=(axis0, axis1, ...)]])¶
Returns the smallest component of a. If axis are specified will return an Array of element-wise min(x) for x in a.axisiter(*axis).
>>> A = Array([[6,3,4],[1,5,0.5]]) >>> print A.formated() [[6, 3, 4], [1, 5, 0.5]] >>> A.min() 0.5 >>> A.min(0, 1) 0.5 >>> A.min(0) Array([1, 3, 0.5]) >>> A.min(1) Array([3, 0.5])
- ndim¶
Number of dimensions of the Array
- normal(axis0, axis1, ...) <==> normal(a[, axis=(axis0, axis1, ...)])¶
Returns a normalized copy of self: self/self.length(axis0, axis1, ...).
>>> A = Array([[0.5,0.5,-0.707],[0.707,-0.707,0.0]]) >>> print A.formated() [[0.5, 0.5, -0.707], [0.707, -0.707, 0.0]] >>> print A.normal().formated() [[0.353593437318, 0.353593437318, -0.499981120367], [0.499981120367, -0.499981120367, 0.0]] >>> print A.normal(0,1).formated() [[0.353593437318, 0.353593437318, -0.499981120367], [0.499981120367, -0.499981120367, 0.0]] >>> print A.normal(0).formated() [[0.5, 0.5, -0.707], [0.707, -0.707, 0.0]] >>> print A.normal(1).formated() [[0.577408397894, 0.577408397894, -1.0], [0.816455474623, -0.816455474623, 0.0]]
- normalize(*args)¶
Performs an in place normalization of self
- prod([axis0[, axis1[, ...[, start=0]]]]) <=> prod(a, start=start, axis=(axis0, axis1, ...))¶
Returns the product of all the components of a, an iterable of values that support the mul operator, times start. If axis are specified will return an Array of prod(x) for x in a.axisiter(*axis).
>>> A = Array([[1,2,3],[4,5,6]]) >>> print A.formated() [[1, 2, 3], [4, 5, 6]] >>> A.prod() 720 >>> A.prod(0, 1) 720 >>> A.prod(0) Array([4, 10, 18]) >>> A.prod(1) Array([6, 120])
- ravel() <==> Array(a.flat)¶
Returns that Array flattened as to a one-dimensional array.
>>> A = Array(range(1, 10), shape=(3, 3)) >>> print repr(A.ravel()) Array([1, 2, 3, 4, 5, 6, 7, 8, 9])
- real() <==> real(a)¶
Returns the element-wise complex real part of the Array.
>>> A = Array([[complex(1, 2), complex(2, 3)], [complex(4, 5), complex(6, 7)]]) >>> print A.formated() [[(1+2j), (2+3j)], [(4+5j), (6+7j)]] >>> print A.real().formated() [[1.0, 2.0], [4.0, 6.0]] >>> print real(A).formated() [[1.0, 2.0], [4.0, 6.0]] >>> A = Array(range(1, 5), shape=(2, 2)) >>> print real(A).formated() [[1, 2], [3, 4]]
- reshape(shape=None)¶
a.reshaped(shape) <==> a.shape = shape
Performs in-place reshape of array a according to the shape argument without changing the Array’s size (total number of components).
Note : as opposed to trim, reshape will reshuffle components and thus not preserve sub-arrays identity.
Examples :
>>> A = Array(range(1, 17), shape=(4, 4)) >>> print A.formated() [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]] >>> S = A[0] >>> A.reshape(shape=(2, 2, 4)) >>> print A.formated() [[[1, 2, 3, 4], [5, 6, 7, 8]], [[9, 10, 11, 12], [13, 14, 15, 16]]] >>> S == A[0, 0] True >>> S is A[0, 0] False
- reshaped(shape=None)¶
a.reshaped(shape) –> Array
Returns a copy the Array as reshaped according to the shape argument, without changing the Array’s size (total number of components)
Examples :
>>> A = Array(range(1, 17), shape=(4, 4)) >>> print A.formated() [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]] >>> B = A.reshaped(shape=(2, 2, 4)) >>> print B.formated() [[[1, 2, 3, 4], [5, 6, 7, 8]], [[9, 10, 11, 12], [13, 14, 15, 16]]] >>> A[0] == B[0, 0] True >>> A[0] is B[0, 0] False
- resize([shape[, value]])¶
Performs in-place resize of array a according to the shape argument. An optional value argument can be passed and will be used to fill the newly created components if the resize results in a size increase, otherwise the Array class default values are used.
Note : as opposed to trim, resize will reshuffle components and thus not preserve sub-arrays identity.
Examples :
>>> A = Array(range(1, 17), shape=(4, 4)) >>> print A.formated() [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]] >>> S = A[0] >>> A.resize(shape=(2, 2, 4)) >>> print A.formated() [[[1, 2, 3, 4], [5, 6, 7, 8]], [[9, 10, 11, 12], [13, 14, 15, 16]]] >>> S == A[0, 0] True >>> S is A[0, 0] False >>> A.resize(shape=(2, 3, 3)) >>> print A.formated() [[[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[10, 11, 12], [13, 14, 15], [16, 0, 0]]] >>> A.resize(shape=(4, 5), value=1) >>> print A.formated() [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 0, 0, 1, 1]]
- resized(shape=None, value=None)¶
a.resized([shape [, value]]) –> Array
Returns a copy of the Array resized according to the shape argument. An optional value argument can be passed and will be used to fill the extra components of the new Array if the resize results in a size increase, otherwise the Array class default values are used.
Examples :
>>> A = Array(range(1, 17), shape=(4, 4)) >>> print A.formated() [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]] >>> B = A.resized(shape=(2, 2, 4)) >>> print B.formated() [[[1, 2, 3, 4], [5, 6, 7, 8]], [[9, 10, 11, 12], [13, 14, 15, 16]]] >>> A[0] == B[0, 0] True >>> A[0] is B[0, 0] False >>> B = B.resized(shape=(2, 3, 3)) >>> print B.formated() [[[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[10, 11, 12], [13, 14, 15], [16, 0, 0]]] >>> B = B.resized(shape=(4, 5), value=1) >>> print B.formated() [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 0, 0, 1, 1]]
- shape¶
a.shape – tuple
Shape of the Array (number of dimensions and number of components in each dimension).
It can be queried, or set to change the Array’s shape similarly to the reshape method.
>>> A = Array(range(1, 17), shape=(4, 4)) >>> print A.formated() [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]] >>> S = A[0] >>> A.shape=(2, 2, 4) >>> print A.formated() [[[1, 2, 3, 4], [5, 6, 7, 8]], [[9, 10, 11, 12], [13, 14, 15, 16]]] >>> A.shape=(4, 4) >>> print A.formated() [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
Related : see Array.reshape method.
- size¶
Total size of the Array (number of individual components)
- sqlength(axis0, axis1, ...) <==> sqlength(a[, axis=(axis0, axis1, ...)])¶
Returns square length of a, ie a*a or the sum of x*x for x in a if a is an iterable of numeric values. If a is an Array and axis are specified will return a list of sqlength(x) for x in a.axisiter(*axis).
>>> A = Array([[0.5,0.5,-0.707],[0.707,-0.707,0.0]]) >>> print A.formated() [[0.5, 0.5, -0.707], [0.707, -0.707, 0.0]] >>> A.sqlength() 1.999547 >>> A.sqlength(0,1) 1.999547 >>> A.sqlength(0) Array([0.999849, 0.999698]) >>> A.sqlength(1) Array([0.749849, 0.749849, 0.499849])
- stack(other, axis=0)¶
a.stack(b[, axis=0]) –> Array
Modifies a by concatenating b at its end, as iterated on axis.
Note : stacks a copy (deepcopy) of b, not a reference to b. However a is modified in place.
Examples:
>>> A = Array([]) >>> print repr(A) Array([]) >>> A.stack([1]) >>> print A.formated() [1] >>> A.stack([2]) >>> print A.formated() [1, 2] >>> A = Array([A]) >>> print A.formated() [[1, 2]] >>> A.stack([[4, 5]], axis=0) >>> print A.formated() [[1, 2], [4, 5]] >>> A.stack([[3], [6]], axis=1) >>> print A.formated() [[1, 2, 3], [4, 5, 6]] >>> A.stack([[7, 8, 9]]) >>> print A.formated() [[1, 2, 3], [4, 5, 6], [7, 8, 9]] >>> B = Array([A]) >>> B.stack(B) >>> print B.formated() [[[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[1, 2, 3], [4, 5, 6], [7, 8, 9]]] >>> B[0] == B[1] True >>> B[0] is B[1] False >>> A == B[0] True >>> A is B[0] True >>> B.stack([[[0, 0, 0]], [[0, 0, 0]]], axis=1) >>> print B.formated() [[[1, 2, 3], [4, 5, 6], [7, 8, 9], [0, 0, 0]], [[1, 2, 3], [4, 5, 6], [7, 8, 9], [0, 0, 0]]] >>> B.stack([[[0], [0], [0], [1]], [[0], [0], [0], [1]]], axis=2) >>> print B.formated() [[[1, 2, 3, 0], [4, 5, 6, 0], [7, 8, 9, 0], [0, 0, 0, 1]], [[1, 2, 3, 0], [4, 5, 6, 0], [7, 8, 9, 0], [0, 0, 0, 1]]]
- stacked(other, axis=0)¶
a.stacked(b[, axis=0]) –> Array
Returns the Array obtained by concatenating a and b on axis.
Note : returns a deepcopy of a.stack(b[, axis=0]).
Examples:
>>> A = Array([]) >>> print repr(A) Array([]) >>> A = A.stacked([1]) >>> print A.formated() [1] >>> A = A.stacked([2]) >>> print A.formated() [1, 2] >>> A = Array([A]) >>> print A.formated() [[1, 2]] >>> A = A.stacked([[4, 5]], axis=0) >>> print A.formated() [[1, 2], [4, 5]] >>> A = A.stacked([[3], [6]], axis=1) >>> print A.formated() [[1, 2, 3], [4, 5, 6]] >>> A = A.stacked([[7, 8, 9]]) >>> print A.formated() [[1, 2, 3], [4, 5, 6], [7, 8, 9]] >>> B = Array([A]) >>> B = B.stacked(B) >>> print B.formated() [[[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[1, 2, 3], [4, 5, 6], [7, 8, 9]]] >>> B[0] == B[1] True >>> B[0] is B[1] False >>> A == B[0] True >>> A is B[0] False >>> B = B.stacked([[[0, 0, 0]], [[0, 0, 0]]], axis=1) >>> print B.formated() [[[1, 2, 3], [4, 5, 6], [7, 8, 9], [0, 0, 0]], [[1, 2, 3], [4, 5, 6], [7, 8, 9], [0, 0, 0]]] >>> B = B.stacked([[[0], [0], [0], [1]], [[0], [0], [0], [1]]], axis=2) >>> print B.formated() [[[1, 2, 3, 0], [4, 5, 6, 0], [7, 8, 9, 0], [0, 0, 0, 1]], [[1, 2, 3, 0], [4, 5, 6, 0], [7, 8, 9, 0], [0, 0, 0, 1]]]
- strip(index)¶
Strip the elements designated by index from a.
Note : as opposed to a.__delete__(index), will collapse dimensions of the Array that end up with only one sub-array.
>>> A = Array(xrange(1, 28), shape=(3, 3, 3)) >>> print A.formated() [[[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[10, 11, 12], [13, 14, 15], [16, 17, 18]], [[19, 20, 21], [22, 23, 24], [25, 26, 27]]] >>> A.shape (3, 3, 3) >>> S = A[0] >>> A.strip(1) >>> print A.formated() [[[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[19, 20, 21], [22, 23, 24], [25, 26, 27]]] >>> S == A[0] True >>> S is A[0] True >>> A.strip(-1) >>> print A.formated() [[1, 2, 3], [4, 5, 6], [7, 8, 9]] >>> S == A True >>> S is A False >>> S[0] == A[0] True >>> S[0] is A[0] True >>> A.strip(None, slice(1,3)) >>> print A.formated() [[1], [4], [7]] >>> A.strip(-1) >>> print A.formated() [[1], [4]] >>> A.strip(-1) >>> print A.formated() [1] >>> A.strip(-1) >>> print A.formated() []
- stripped(*args)¶
a.stripped(index) –> Array
Returns a copy (deepcopy) of a with the elements designated by index stripped, as in a.strip(index)
Note : as opposed to a.deleted(index), will collapse dimensions of the Array that end up with only one sub-array.
>>> A = Array(xrange(1, 28), shape=(3, 3, 3)) >>> print A.formated() [[[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[10, 11, 12], [13, 14, 15], [16, 17, 18]], [[19, 20, 21], [22, 23, 24], [25, 26, 27]]] >>> A.shape (3, 3, 3) >>> B = A.stripped(1) >>> print B.formated() [[[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[19, 20, 21], [22, 23, 24], [25, 26, 27]]] >>> B[0] == A[0] True >>> B[0] is A[0] False >>> B = B.stripped(-1) >>> print B.formated() [[1, 2, 3], [4, 5, 6], [7, 8, 9]] >>> B == A[0] True >>> B is A[0] False >>> B[0] == A[0, 0] True >>> B[0] is A[0,0] False >>> B = B.stripped(None, slice(1,3)) >>> print B.formated() [[1], [4], [7]] >>> B = B.stripped(-1) >>> print B.formated() [[1], [4]] >>> B = B.stripped(-1) >>> print B.formated() [1] >>> B = B.stripped(-1) >>> print B.formated() []
- subiter(dim=None)¶
a.subiter([dim=None]) –> ArrayIter
Returns an iterator on all sub Arrays for a specific sub Array number of dimension.
a.subiter(0) is equivalent to a.flat: lista sub-arrays of dimension 0, ie components a.subiter() is equivalent to self.subiter(self.ndim-1) and thus to self.__iter__()
Note : ArrayIter iterators support __len__, __getitem__ and __setitem__
>>> A = Array(range(1, 28), shape=(3, 3, 3)) >>> print A.formated() [[[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[10, 11, 12], [13, 14, 15], [16, 17, 18]], [[19, 20, 21], [22, 23, 24], [25, 26, 27]]] >>> [a for a in A.subiter(0)] [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27] >>> [a for a in A.subiter(1)] [Array([1, 2, 3]), Array([4, 5, 6]), Array([7, 8, 9]), Array([10, 11, 12]), Array([13, 14, 15]), Array([16, 17, 18]), Array([19, 20, 21]), Array([22, 23, 24]), Array([25, 26, 27])] >>> [a for a in A.subiter(2)] [Array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), Array([[10, 11, 12], [13, 14, 15], [16, 17, 18]]), Array([[19, 20, 21], [22, 23, 24], [25, 26, 27]])] >>> [a for a in A.subiter(3)] Traceback (most recent call last): ... ValueError: can only iterate for a sub-dimension inferior to Array's number of dimensions 3
- sum([axis0[, axis1[, ...[, start=0]]]]) <=> sum(a, start=start, axis=(axis0, axis1, ...))¶
Returns the sum of all the components of a, plus start. If axis are specified will return an Array of sum(x) for x in a.axisiter(*axis), else will sum on all axis of a.
>>> A = Array([[1,2,3],[4,5,6]]) >>> print A.formated() [[1, 2, 3], [4, 5, 6]] >>> A.sum() 21 >>> A.sum(0, 1) 21 >>> A.sum(0) Array([5, 7, 9]) >>> A.sum(1) Array([6, 15])
- tolist()¶
a.tolist() –> list
Returns that Array converted to a nested list
>>> A = Array(range(1, 10), shape=(3, 3)) >>> print repr(A) Array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) >>> print repr(list(A)) [Array([1, 2, 3]), Array([4, 5, 6]), Array([7, 8, 9])] >>> print repr(A.tolist()) [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
- totuple()¶
a.totuple() –> tuple
Returns that Array converted to a nested tuple
>>> A = Array(range(1, 10), shape=(3, 3)) >>> print repr(A) Array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) >>> print repr(tuple(A)) (Array([1, 2, 3]), Array([4, 5, 6]), Array([7, 8, 9])) >>> print repr(A.totuple()) ((1, 2, 3), (4, 5, 6), (7, 8, 9))
- transpose(*args)¶
a.transpose([axis0[, axis1[, ...]]]) –> Array
Returns a reordered / transposed along the specified axes. If no axes are given,or None is passed, switches the complete axes order. For a 2-d array, this is the usual matrix transpose.
>>> A = Array(range(18), shape=(2,3,3)) >>> print A.formated() [[[0, 1, 2], [3, 4, 5], [6, 7, 8]], [[9, 10, 11], [12, 13, 14], [15, 16, 17]]] >>> print A.transpose().formated() [[[0, 9], [3, 12], [6, 15]], [[1, 10], [4, 13], [7, 16]], [[2, 11], [5, 14], [8, 17]]] >>> print A.transpose(0,2,1).formated() [[[0, 3, 6], [1, 4, 7], [2, 5, 8]], [[9, 12, 15], [10, 13, 16], [11, 14, 17]]]
>>> B=MatrixN(range(9), shape=(3, 3)) >>> print B.formated() [[0, 1, 2], [3, 4, 5], [6, 7, 8]] >>> print B.transpose().formated() [[0, 3, 6], [1, 4, 7], [2, 5, 8]]
- trim(shape)¶
Performs in-place trimming of array a to given shape. An optional value argument can be passed and will be used to fill the newly created components if the resize results in a size increase.
Note : a is modified in-place
>>> A = Array(range(1, 10), shape=(3, 3)) >>> print A.formated() [[1, 2, 3], [4, 5, 6], [7, 8, 9]] >>> S = A[0] >>> A.trim(shape=(4, 3)) >>> print A.formated() [[1, 2, 3], [4, 5, 6], [7, 8, 9], [0, 0, 0]] >>> S == A[0] True >>> S is A[0] True >>> A.trim(shape=(4, 4)) >>> print A.formated() [[1, 2, 3, 0], [4, 5, 6, 0], [7, 8, 9, 0], [0, 0, 0, 0]] >>> A.trim(shape=(2, 2)) >>> print A.formated() [[1, 2], [4, 5]]
- trimmed(shape=None, value=None)¶
a.trimmed([shape [, value]]) –> Array
Returns the Array as “trimmed”, re-sized according to the shape argument. The difference with a resize is that each dimension will be resized individually, thus the shape argument must have the same number of dimensions as the Array a. A value of -1 or None for a shape dimension size will leave it unchanged. An optional value argument can be passed and will be used to fill the newly created components if the trimmed results in a size increase, otherwise the class default values will be used to fill new components
Note : returns a copy (deepcopy) of a.trim([shape [, value]])
>>> A = Array(range(1, 10), shape=(3, 3)) >>> print A.formated() [[1, 2, 3], [4, 5, 6], [7, 8, 9]] >>> B = A.trimmed(shape=(4, 3)) >>> print B.formated() [[1, 2, 3], [4, 5, 6], [7, 8, 9], [0, 0, 0]] >>> B[0] == A[0] True >>> B[0] is A[0] False >>> B = A.trimmed(shape=(4, 4)) >>> print B.formated() [[1, 2, 3, 0], [4, 5, 6, 0], [7, 8, 9, 0], [0, 0, 0, 0]] >>> B = A.trimmed(shape=(2, 2)) >>> print B.formated() [[1, 2], [4, 5]]
- vstack(b) <==> a.stack(b, axis=0)¶
Modifies a by concatenating b at its end, as iterated on first axis. For a 2 dimensional Array/MatrixN, it stacks a and b vertically
>>> A = Array([[1, 2], [3, 4]]) >>> print A.formated() [[1, 2], [3, 4]] >>> A.vstack([[5, 6]]) >>> print A.formated() [[1, 2], [3, 4], [5, 6]]
- vstacked(b) <==> a.stacked(b, axis=0)¶
Returns the Array obtained by concatenating a and b on first axis. For a 2 dimensional Array/MatrixN, it stacks a and b vertically.
>>> A = Array([[1, 2], [3, 4]]) >>> print A.formated() [[1, 2], [3, 4]] >>> A = A.vstacked([[5, 6]]) >>> print A.formated() [[1, 2], [3, 4], [5, 6]]