numpy - evaluate function on a grid of points

后端 未结 5 908
孤街浪徒
孤街浪徒 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:34

    My two cents:

        import numpy as np
    
        x = np.linspace(0, 4, 10)
        y = np.linspace(-1, 1, 20)
    
        [X, Y] = np.meshgrid(x, y, indexing = 'ij', sparse = 'true')
    
        def func(x, y):
            return x*y/(x**2 + y**2 + 4)
            # I have defined a function of x and y.
    
       func(X, Y)
    

提交回复
热议问题