在 Wolfram Mathematica 中,我经常使用
有没有更短、更紧凑的方法来做这件事?
Table[]
函数。
Table
函数示例:
i1^2 + i3^3
可以是任何参数 (i1
,i3
) 的函数。{i1, 1, 9, 2},
5,
{i3, 7, 3, -.5}
是参数空间。
更多详情请参阅文档: https://reference.wolfram.com/language/ref/Table.html
在 Python 中,我可以使用一系列 for 循环来完成这个操作,代码如下:
import numpy as np
def f(i1,i3):
return(i1**2+i3**3)
arr=np.array([[[
f(i1,i3)
for i3 in np.arange(7,2.5,-.5)]
for i2 in np.arange(5)]
for i1 in np.arange(1,11,2)]
)
print(arr)
有没有更短、更紧凑的方法来做这件事?
sympy
,它可能有类似的功能。 - Barmarnp.array(list(map(f, np.dstack(np.meshgrid(a, c)).reshape((len(a)*len(c), 2))))).reshape((len(c), len(a)))
,但你需要复制每个内部数组五次。其中a, c
分别对应于i3, i1
的范围。你还需要稍微更新一下f
函数。 - tobiaslist(map(lambda i1: list(map(lambda i3: i1**2 + (i3/2)**2, range(7*2,3*2-1, -1))), range(1, 9+1, 2)))
。 - Well...