shape, type) |
shape, type) |
>>> z = zeros((3,3)) >>> print z [[0 0 0] [0 0 0] [0 0 0]] >>> o = ones([2,3]) >>> print o [[1 1 1] [1 1 1]]
>>> o = ones((2,3), Float32) >>> print o [[ 1. 1. 1.] [ 1. 1. 1.]]
first, limit=None, stride=1, type=None, shape=None) |
first, limit=None, stride=1, type=None, shape=None) |
>>> r = arange(10) >>> print r [0 1 2 3 4 5 6 7 8 9]
>>> big = reshape(arange(100),(10,10)) >>> print big [[ 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 28 29] [30 31 32 33 34 35 36 37 38 39] [40 41 42 43 44 45 46 47 48 49] [50 51 52 53 54 55 56 57 58 59] [60 61 62 63 64 65 66 67 68 69] [70 71 72 73 74 75 76 77 78 79] [80 81 82 83 84 85 86 87 88 89] [90 91 92 93 94 95 96 97 98 99]]
>>> print arange(10,-10,-2) [10 8 6 4 2 0 -2 -4 -6 -8]
>>> print arange(5.0) [ 0. 1. 2. 3. 4.] >>> print arange(0, 1, .2) [ 0. 0.2 0.4 0.6 0.8]
>>> a = array([[3]*5]*5) >>> print a [[3 3 3 3 3] [3 3 3 3 3] [3 3 3 3 3] [3 3 3 3 3] [3 3 3 3 3]]
>>> a = zeros([5,5]) + 3 >>> print a [[3 3 3 3 3] [3 3 3 3 3] [3 3 3 3 3] [3 3 3 3 3] [3 3 3 3 3]]
object, shape) |
>>> def dist(x,y): ... return (x-5)**2+(y-5)**2 # distance from (5,5) squared ... >>> m = fromfunction(dist, (10,10)) >>> print m [[50 41 34 29 26 25 26 29 34 41] [41 32 25 20 17 16 17 20 25 32] [34 25 18 13 10 9 10 13 18 25] [29 20 13 8 5 4 5 8 13 20] [26 17 10 5 2 1 2 5 10 17] [25 16 9 4 1 0 1 4 9 16] [26 17 10 5 2 1 2 5 10 17] [29 20 13 8 5 4 5 8 13 20] [34 25 18 13 10 9 10 13 18 25] [41 32 25 20 17 16 17 20 25 32]] >>> m = fromfunction(lambda i,j,k: 100*(i+1)+10*(j+1)+(k+1), (4,2,3)) >>> print m [[[111 112 113] [121 122 123]] [[211 212 213] [221 222 223]] [[311 312 313] [321 322 323]] [[411 412 413] [421 422 423]]]
m[3, 4]
in the first example above is the value of dist when
x=3
and y=4
. Similarly for the lambda function in the second
example, but with a rank-3 array. The implementation of
fromfunction consists of:
def fromfunction(function, dimensions): return apply(function, tuple(indices(dimensions)))
indices(dimensions)
. As described in the
definition of indices, this consists of arrays of indices which will be of
rank one less than that specified by dimensions. This means that the
function argument must accept the same number of arguments as there are
dimensions in dimensions, and that each argument will be an array of
the same shape as that specified by dimensions. Furthermore, the array
which is passed as the first argument corresponds to the indices of each
element in the resulting array along the first axis, that which is passed as
the second argument corresponds to the indices of each element in the
resulting array along the second axis, etc. A consequence of this is that
the function which is used with fromfunction will work as
expected only if it performs a separable computation on its arguments, and
expects its arguments to be indices along each axis. Thus, no logical
operation on the arguments can be performed, or any non-shape preserving
operation. Thus, the following will not work as expected:
>>> def buggy(test): ... if test > 4: return 1 ... else: return 0 ... >>> print fromfunction(buggy,(10,)) 1
>>> def notbuggy(test): # only works in Python 2.1 & later ... print test ... return where(test>4,1,0) ... >>> fromfunction(notbuggy,(10,)) [0 1 2 3 4 5 6 7 8 9] array([0, 0, 0, 0, 0, 1, 1, 1, 1, 1])
size) |
>>> print identity(5) [[1 0 0 0 0] [0 1 0 0 0] [0 0 1 0 0] [0 0 0 1 0] [0 0 0 0 1]]
Send comments to the NumArray community.