一、概述
def pythongrid(function, inputs, xRange, yRange, resolution): """ pythongrid: A function that takes a Python function and evaluates it over a range of x and y values, and then returns a grid of the result. function: the Python function to evaluate inputs: a string specifying the input variables of the function, separated by commas xRange: a list of [lower, upper] bounds for the x-axis yRange: a list of [lower, upper] bounds for the y-axis resolution: the number of points along each axis to evaluate the function at returns: a grid of the results, where grid[i][j] = function(x, y) at x = xRange[0] + i * dx and y = yRange[0] + j * dy where dx = (xRange[1] - xRange[0]) / (resolution - 1) and dy = (yRange[1] - yRange[0]) / (resolution - 1) """
PythonGrid函數是一個允許用戶給定一個Python函數,並在給定的輸入範圍內計算函數值的工具函數。它生成一個二維數組,格點上的每個元素都是函數在該點上的值。該函數的輸入包括函數本身、輸入變量名、輸入變量的交換範圍和分辨率。這個函數返回一個生成的網格,表示函數在輸入範圍內的值的二維數組。
二、函數參數
1.函數參數
該函數參數包括一個Python函數和輸入變量:
def my_function(x, y): return x**2 + y**2 grid = pythongrid(my_function, "x, y", [-1, 1], [-1, 1], 10)
在上面的示例中,Python函數“my_function”接受兩個參數x和y,並返回它們的和的平方。通過將此函數傳遞給“pythongrid”函數,可以生成一個10×10的數組,該數組在-1到1的範圍內採樣每個軸10個點,並評估該函數。
2.輸入變量
“pythongrid”函數的輸入變量通過一個字符串指定,將所有輸入參數列表在“inputs”變量中給出,並用逗號隔開。下面顯示了一個具有兩個輸入參數x和y的示例字符串:
“`
inputs = “x, y”
grid = pythongrid(my_function, inputs, [-1, 1], [-1, 1], 10)
“`
3.輸入範圍
“pythongrid”函數採樣在輸入變量範圍內進行評估。可以通過兩個包含每個輸入變量的下限和上限的列表來定義輸入範圍。例如,下面的代碼將在x=0到2和y=1到3的範圍內對my_function函數進行評估:
“`
x_range = [0, 2]
y_range = [1, 3]
grid = pythongrid(my_function, inputs, x_range, y_range, 10)
“`
4.分辨率
“pythongrid”函數生成彌補輸入範圍的網格,其中節點均勻地採樣每個軸,每個軸都在相同數量的點上(即在該軸上的分辨率)。分辨率二元組(num_x, num_y)指定在 x 和 y 方向上的計算點數,即格點數。例如,在下面的代碼片段中,將計算4×6的網格:
“`
grid = pythongrid(my_function, inputs, [0, 4], [0, 6], (4, 6))
“`
三、示例代碼
1.三維函數的調用
以下代碼示例調用一個三維函數,並生成一個200×200的數據網格:
import numpy as np from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt def my_function_3d(x, y, z): return 2 * np.sin(x) * np.cos(y) + 3 * np.cos(z) - x**2 - y**2 - z**2 inputs_3d = "x, y, z" x_range_3d = [-5, 5] y_range_3d = [-5, 5] z_range_3d = [-5, 5] resolution_3d = 200 grid_3d = pythongrid(my_function_3d, inputs_3d, x_range_3d, y_range_3d, z_range_3d, resolution_3d) # Plotting the result fig = plt.figure() ax = fig.add_subplot(111, projection='3d') X, Y = np.meshgrid(np.linspace(x_range_3d[0], x_range_3d[1], resolution_3d), np.linspace(y_range_3d[0], y_range_3d[1], resolution_3d)) ax.plot_surface(X, Y, grid_3d, cmap='viridis') plt.show()
2.皮克梯形函數的調用
以下代碼示例調用一個離散的函數,並使用“pythongrid”函數插值。這個函數叫做皮克梯形函數,是處理圖像和數字信號處理中常見的一個例子。
import numpy as np def create_peak_ranges(dimensions): """ create_peak_ranges : A function that creates amplitude and range values for peaks of Pick's Triangle. It takes only one input variable, dimensions, which is the number of dimensions being used in the function. dimensions: the number of dimensions being used returns: - ranges: a list of amplitude values for each peak - limits: a list of tuples consisting of lower/upper bounds for each dimension of the grid over which to evaluate this function """ ranges = [1 + 2 * i for i in range(dimensions)] limits = [(0, 0.5 * i) for i in ranges] return ranges, limits def create_pick_list(dimensions): """ create_pick_list : A function to create a list of the x-values at the peak points of the Pick's function. It takes only one input variable, dimensions, which is the number of dimensions being used. dimensions: the number of dimensions being used returns: a list of the x-values at the peak points of the function """ # Create all the necessary data structures. num_points = (dimensions ** 2 + dimensions) / 2 pick_list = np.zeros((num_points, dimensions)) start = 0 for i in range(1, dimensions + 1): for j in range(1, i + 1): pick_list[start, dimensions - i] = j / float(i + 1) pick_list[start, dimensions - j] = 1.0 / (i + 1) start += 1 return pick_list def picks_function(inputs): """ picks_function : A function that contains the actual logic to create the Pick's function. It takes only one input variable which is an array of individual input variables. inputs: an array of individual input variables. returns: the result of evaluating Pick's function with the given inputs.""" peaks, limits = create_peak_ranges(len(inputs)) pick_list = create_pick_list(len(inputs)) num_points = len(pick_list) values = [1000.0 * dimensions for dimensions in inputs] for i in range(num_points): peak = pick_list[i] for j in range(len(inputs)): values[j] *= np.sinc(peak[j] * (inputs[j] - limits[j][0])) values[j] *= np.sinc(peak[j] * (limits[j][1] - inputs[j])) values = [abs(value / 1000.0 ** dimensions) for value, dimensions in zip(values, peaks)] result = np.prod(values) - 0.5 return result # Evaluating the function, and plotting the result inputs = "x, y" x_range = [-5, 5] y_range = [-5, 5] resolution = 50 grid = pythongrid(picks_function, inputs, x_range, y_range, resolution) fig = plt.figure() ax = fig.add_subplot(111, projection='3d') X, Y = np.meshgrid(np.linspace(x_range[0], x_range[1], resolution), np.linspace(y_range[0], y_range[1], resolution)) ax.plot_surface(X, Y, grid, cmap='viridis') plt.show()
四、總結
PythonGrid函數是一個非常有用的工具函數,使用戶可以計算輸入函數在定義的網格上的值,並插值結果。本文介紹了Pythongrid函數的功能,函數參數以及兩個使用示例。如有遺漏,歡迎補充和修正!
原創文章,作者:RIWG,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/136561.html