Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from numpy import *
- def sum_of_indices(x, y, z):
- # What type are X, Y and Z ? Expect int or duck-type equivalent.
- # Getting 3 individual arrays
- print "Value of X is:"
- print x
- print "Type of X is:", type(x)
- return x + y + z
- a = fromfunction(sum_of_indices, (2, 2, 2))
- Value of X is:
- 0
- Type of X is: int
- Value of X is:
- 1
- Type of X is: int
- Value of X is:
- [[[ 0. 0.]
- [ 0. 0.]]
- [[ 1. 1.]
- [ 1. 1.]]]
- [[[ 0. 0.]
- [ 1. 1.]]
- [[ 0. 0.]
- [ 1. 1.]]]
- [[[ 0. 1.]
- [ 0. 1.]]
- [[ 0. 1.]
- [ 0. 1.]]]
- Type of X is: <type 'numpy.ndarray'>
- (array([[ 0., 0.], [ 1., 1.]]), array([[ 0., 1.], [ 0., 1.]]))
- def fromfunction(function, shape, **kwargs):
- dtype = kwargs.pop('dtype', float)
- args = indices(shape, dtype=dtype)
- return function(*args,**kwargs)
- >>> side = np.arange(2)
- >>> side
- array([0, 1])
- >>> x,y,z = np.meshgrid(side,side,side)
- >>> x
- array([[[0, 0],
- [1, 1]],
- [[0, 0],
- [1, 1]]])
- >>> x+y+z #Result of your code.
- array([[[0, 1],
- [1, 2]],
- [[1, 2],
- [2, 3]]])
- >>> a
- array([[[ 0., 1.], # 0+0+0, 0+0+1
- [ 1., 2.]], # 0+1+0, 0+1+1
- [[ 1., 2.], # 1+0+0, 1+0+1
- [ 2., 3.]]]) # 1+1+0, 1+1+1
- def fromfunction(function, shape, **kwargs):
- . . .
- args = indices(shape, dtype=dtype)
- return function(*args,**kwargs)
- array([[[ 0., 0.],
- [ 1., 1.]],
- [[ 1., 1.],
- [ 1., 1.]]])
- +
- array([[[ 0., 0.],
- [ 1., 1.]],
- [[ 0., 0.],
- [ 1., 1.]]])
- +
- array([[[ 0., 1.],
- [ 0., 1.]],
- [[ 0., 1.],
- [ 0., 1.]]])
- =
- array([[[ 1., 1.],
- [ 1., 2.]],
- [[ 1., 2.],
- [ 2., 3.]]])
- f([[0., 0.], [1., 1.]], [[0., 1.], [0., 1.]])
- m = fromfunction(f, shape)
- g = vectorize(f)
- m = fromfunction(g, shape)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement