[英]Appending Numpy Array with Another Numpy Array
我有一個 NumPy 數組,方程式以符號方式求解,常量a和b 。 這是我的數組“bounds_symbolic”中索引 (2,0) 處的單元格示例:
-a*sqrt(1/(a**6*b**2+1))
我還有一個名為“a_values”的數組,我想將其代入我的“bounds_symbolic”數組。 我還將b值設置為 1,我也想將其替換。保持 arrays 的第一行完好無損也很好。
換句話說,對於在“bounds_symbolic”中索引為 (2,0) 的單元格,我想將我所有的a和b值代入方程,同時擴展列以包含代入的方程。 然后我想對整個“bounds_symbolic”數組執行此操作。
這是我到目前為止的代碼:
import sympy
import numpy as np
a, b, x, y = sympy.symbols("a b x y")
# Equation of the ellipse solved for y
ellipse = sympy.sqrt((b ** 2) * (1 - ((x ** 2) / (a ** 2))))
# Functions to be tested
test_functions = np.array(
[(a * b * x), (((a * b) ** 2) * x), (((a * b) ** 3) * x), (((a * b) ** 4) * x), (((a * b) ** 5) * x)])
# Equating ellipse and test_functions so their intersection can be symbolically solved for
equate = np.array(
[sympy.Eq(ellipse, test_functions[0]), sympy.Eq(ellipse, test_functions[1]), sympy.Eq(ellipse, test_functions[2]),
sympy.Eq(ellipse, test_functions[3]), sympy.Eq(ellipse, test_functions[4])])
# Calculating the intersection points of the ellipse and the testing functions
# Array that holds the bounds of the integral solved symbolically
bounds_symbolic = np.array([])
for i in range(0, 5):
bounds_symbolic = np.append(bounds_symbolic, sympy.solve(equate[i], x))
# Array of a-values to plug into the bounds of the integral
a_values = np.array(np.linspace(-10, 10, 201))
# Setting b equal to a constant of 1
b = 1
integrand = np.array([])
for j in range(0, 5):
integrand = np.append(integrand, (ellipse - test_functions[j]))
# New array with a-values substituted into the bounds
bounds_a = bounds_symbolic
# for j in range(0, 5):
# bounds_a = np.append[:, ]
謝謝!
Numpy arrays 是處理純數值數據時的最佳選擇,它們可以幫助加速多種類型的計算。 一旦開始混合 sympy 表達式,事情就會變得非常混亂。 您還將失去 numpy arrays 的所有速度優勢。
除此之外, np.append
是一個非常慢的操作,因為它需要在每次執行時重新創建完整的數組。 當創建一個新的 numpy 數組時,推薦的方法是首先創建一個已經具有最終大小的空數組(例如使用np.zeros()
)。
您還應該查看 Python 的列表理解,因為它簡化了列表的創建。 在“pythonic”代碼中,盡可能少地使用索引。 當你習慣了其他編程語言時,列表理解可能看起來有點奇怪,但你很快就會習慣它們,從那時起你肯定會喜歡它們。
在您的示例代碼中, numpy 對於np.linspace
命令很有用,它創建一個數字數組(同樣不需要使用np.array
轉換它們)。 最后,您可能希望將替換值轉換為 numpy 數組。 請注意,當solve
會為某些方程返回不同數量的解時,這將不起作用,因為 numpy arrays 需要其所有元素的大小相同。 另請注意,可能需要從 sympy 的數值類型顯式轉換為dtype
可以理解的數據類型。 (Sympy 通常以更高的精度工作,不關心速度的損失。)
另請注意,如果您分配b = 1
,則會創建一個新變量並丟失指向 sympy 符號的變量。 建議使用其他名稱。 只寫b = 1
不會改變符號的值。 您需要subs
來用值替換符號。
總而言之,您的代碼可能如下所示:
import sympy
import numpy as np
a, b, x, y = sympy.symbols("a b x y")
# Equation of the ellipse solved for y
ellipse = sympy.sqrt((b ** 2) * (1 - ((x ** 2) / (a ** 2))))
# Functions to be tested
test_functions = [a * b * x, ((a * b) ** 2) * x, ((a * b) ** 3) * x, ((a * b) ** 4) * x, ((a * b) ** 5) * x]
# Equating ellipse and test_functions so their intersection can be symbolically solved for
# Array that holds the bounds of the integral solved symbolically
bounds_symbolic = [sympy.solve(sympy.Eq(ellipse, fun), x) for fun in test_functions]
# Array of a-values to plug into the bounds of the integral
a_values = np.linspace(-10, 10, 201)
# Setting b equal to a constant of 1
b_val = 1
# New array with a-values substituted into the bounds
bounds_a = [[[bound.subs({a: a_val, b: b_val}) for bound in bounds]
for bounds in bounds_symbolic]
for a_val in a_values]
bounds_a = np.array(bounds_a, dtype='float') # shape: (201, 5, 2)
結果數組的值可以用於繪圖:
import matplotlib.pyplot as plt
for i, (test_func, color) in enumerate(zip(test_functions, plt.cm.Set1.colors)):
plt.plot(a_values, bounds_a[:, i, 0], color=color, label=test_func)
plt.plot(a_values, bounds_a[:, i, 1], color=color, alpha=0.5)
plt.legend()
plt.margins(x=0)
plt.xlabel('a')
plt.ylabel('bounds')
plt.show()
或填寫:
for i, (test_func, color) in enumerate(zip(test_functions, plt.cm.Set1.colors)):
plt.plot(a_values, bounds_a[:, i, :], color=color)
plt.fill_between(a_values, bounds_a[:, i, 0], bounds_a[:, i, 1], color=color, alpha=0.1)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.