×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

Python NumPy: Evaluate function on a grid of points

In this tutorial, we will learn how to evaluate function on a grid of points in Python NumPy? By Pranit Sharma Last updated : May 05, 2023

Suppose that we are defining a function that accepts an array of points (say x and y), we need way to produce a NumPy array containing the values of this function evaluated on an n-dimensional grid of points.

How to evaluate function on a grid of points in NumPy?

To evaluate a function on a grid of points in NumPy, there is a much faster, clearer and shorter approach which is to use numpy.linspace() method, it will create an x and y array of points. Since this will create a 1D array, we need to add a new axis to this array while calling the function so that it acts as a grid of points.

Let us understand with the help of an example,

Python program to evaluate function on a grid of points in NumPy

# Import numpy
import numpy as np

# Creating array of points
x = np.linspace(0, 4, 10)
y = np.linspace(-1, 1, 20)

# Display original array of points
print("x array of points:\n",x,"\n")
print("y array of points:\n",y,"\n")

# Defining a function
def fun(x,y):
    return np.sin(y * x)

# Calling function fun on grid of points
res = fun(x[:,None], y[None,:])

# Display result
print("Result\n:\n",res)

Output

x array of points:
 [0.         0.44444444 0.88888889 1.33333333 1.77777778 2.22222222
 2.66666667 3.11111111 3.55555556 4.        ] 

y array of points:
 [-1.         -0.89473684 -0.78947368 -0.68421053 -0.57894737 -0.47368421
 -0.36842105 -0.26315789 -0.15789474 -0.05263158  0.05263158  0.15789474
  0.26315789  0.36842105  0.47368421  0.57894737  0.68421053  0.78947368
  0.89473684  1.        ] 

Result
:
 [[-0.         -0.         -0.         -0.         -0.         -0.
  -0.         -0.         -0.         -0.          0.          0.
   0.          0.          0.          0.          0.          0.
   0.          0.        ]
 [-0.42995636 -0.38726275 -0.34372169 -0.29942845 -0.25447998 -0.20897462
  -0.16301197 -0.11669259 -0.07011786 -0.02338968  0.02338968  0.07011786
   0.11669259  0.16301197  0.20897462  0.25447998  0.29942845  0.34372169
   0.38726275  0.42995636]
 [-0.77637192 -0.71408881 -0.64555852 -0.57138061 -0.492204   -0.40872137
  -0.32166307 -0.23179071 -0.13989055 -0.04676656  0.04676656  0.13989055
   0.23179071  0.32166307  0.40872137  0.492204    0.57138061  0.64555852
   0.71408881  0.77637192]
 [-0.9719379  -0.9294733  -0.86872962 -0.79090146 -0.69751938 -0.59041986
  -0.4717091  -0.34372169 -0.20897462 -0.07011786  0.07011786  0.20897462
   0.34372169  0.4717091   0.59041986  0.69751938  0.79090146  0.86872962
   0.9294733   0.9719379 ]
 [-0.9786557  -0.99980306 -0.98604004 -0.93784722 -0.85690736 -0.74604665
  -0.60913605 -0.4509561  -0.27703001 -0.09343078  0.09343078  0.27703001
   0.4509561   0.60913605  0.74604665  0.85690736  0.93784722  0.98604004
   0.99980306  0.9786557 ]
 [-0.79522006 -0.91410234 -0.9831947  -0.99873379 -0.9598732  -0.86872962
  -0.73026751 -0.55202873 -0.34372169 -0.11669259  0.11669259  0.34372169
   0.55202873  0.73026751  0.86872962  0.9598732   0.99873379  0.9831947
   0.91410234  0.79522006]
 [-0.45727263 -0.6857457  -0.86054034 -0.96797406 -0.99963723 -0.95305133
  -0.83186299 -0.64555852 -0.40872137 -0.13989055  0.13989055  0.40872137
   0.64555852  0.83186299  0.95305133  0.99963723  0.96797406  0.86054034
   0.6857457   0.45727263]
 [-0.03047682 -0.35037076 -0.63302322 -0.84839063 -0.97358123 -0.99528832
  -0.91120462 -0.73026751 -0.4717091  -0.16301197  0.16301197  0.4717091
   0.73026751  0.91120462  0.99528832  0.97358123  0.84839063  0.63302322
   0.35037076  0.03047682]
 [ 0.40224065  0.03968347 -0.32836787 -0.65095676 -0.88342083 -0.9935755
  -0.96616987 -0.80499825 -0.5323748  -0.18604419  0.18604419  0.5323748
   0.80499825  0.96616987  0.9935755   0.88342083  0.65095676  0.32836787
  -0.03968347 -0.40224065]
 [ 0.7568025   0.42354465  0.01630136 -0.39378948 -0.73509255 -0.9479885
  -0.99528832 -0.86872962 -0.59041986 -0.20897462  0.20897462  0.59041986
   0.86872962  0.99528832  0.9479885   0.73509255  0.39378948 -0.01630136
  -0.42354465 -0.7568025 ]]

In this example, we have used the following Python basic topics that you should learn:

Python NumPy Programs »

Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.