numpy - evaluate function on a grid of points

后端 未结 5 909
孤街浪徒
孤街浪徒 2020-12-08 20:09

What is a good way to produce a numpy array containing the values of a function evaluated on an n-dimensional grid of points?

For example, suppose I want to evaluate

5条回答
  •  悲哀的现实
    2020-12-08 20:33

    import numpy as np
    
    def func(x, y):
        return np.sin(y * x)
    
    xaxis = np.linspace(0, 4, 10)
    yaxis = np.linspace(-1, 1, 20)
    x, y = np.meshgrid(xaxis, yaxis)
    result = func(x, y)
    

提交回复
热议问题