Im trying to map an array to a function. Then map that that array into another function but not sure how to deal with map type object or how to map multidimensional arrays.
import numpy as np
import sympy as sp
nn = np.arange(2)
n= len(nn)
x0=0
y0=0
r=10
num=11
def func1(i):
x1, y1 = x0 r * sp.cos(2 * sp.pi * i / n), y0 r * sp.sin(2 * sp.pi * i / n)
return np.array([sp.N(x1),sp.N(y1)])
def func2(x1,y1):
x, y = np.linspace(x0, x1, num), np.linspace(y0, y1, num)
return x,y
map1=map(func1,nn)
map2=map(func2,map1[0],map1[1])
Want expect out put to be an array [[0,1,2,3,4,5,6,7,8,9,10],[0,0,0,0,0,0,0,0,0,0,0]],[[0,-1,-2,-3,-4,-5,-6,-7,-8,-9,-10],[0,0,0,0,0,0,0,0,0,0,0]]
CodePudding user response:
I'd express this as a single loop, or list comprehension. The two functions are chained, not mapped
In [60]: def func1(i):
...: x1, y1 = x0 r * sp.cos(2 * sp.pi * i / n), y0 r * sp.sin(2 * sp
...: .pi * i / n)
...: print(x1,y1)
...: return np.array([int(x1),int(y1)])
...:
...: def func2(x1,y1):
...: x, y = np.linspace(x0, x1, num), np.linspace(y0, y1, num)
...: return x,y
...:
...:
In [61]: [func2(*func1(i)) for i in range(2)]
10 0
-10 0
Out[61]:
[(array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.]),
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])),
(array([ 0., -1., -2., -3., -4., -5., -6., -7., -8., -9., -10.]),
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]))]
In [62]: np.array(_, int)
Out[62]:
array([[[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]],
[[ 0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]])
To use map
:
np.array(list(map(lambda i: func2(*func1(i)), range(2))),int)
The use of sympy
is bit obscure, but apparently it's to get "exact" sin/cos values - but they still have to be converted to int
to be used by numpy
. Use of sympy
together with numpy
has lots of pitfalls.