如何解决将Numpy数组附加到另一个Numpy数组
我有一个NumPy数组,其中的符号用常数 a 和 b 进行符号求解。这是我的数组“ bounds_symbolic”中索引为(2,0)的单元格的示例:
-a*sqrt(1/(a**6*b**2+1))
我还有一个名为“ a_values”的数组,我想将其替换为“ bounds_symbolic”数组。我还将 b 值设置为1,我也想替换为该值。保持数组的第一行完好无缺。
换句话说,对于在“ 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]),test_functions[2]),test_functions[3]),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数组是最佳选择,因为它们可以帮助加快许多类型的计算。一旦开始混合sympy表达式,事情就会变得非常混乱。您还将失去numpy数组的所有速度优势。
除此之外,np.append
是一个very slow操作,因为它每次执行时都需要重新创建完整的数组。创建新的numpy数组时,建议先创建一个具有最终大小的空数组(例如,使用np.zeros()
)。
您还应该签出Python的list comprehension,因为它可以简化列表的创建。在“ pythonic”代码中,索引使用得尽可能少。当您习惯于其他编程语言时,列表理解可能看起来有些奇怪,但是您很快就会习惯它们,从那时起,您一定会更喜欢它们。
在您的示例代码中,numpy对于np.linspace
命令很有用,该命令创建一个数字数组(同样不需要使用np.array
进行转换)。最后,您可能希望将替换值转换为numpy数组。请注意,当solve
将为某些方程返回不同数量的解时,这是行不通的,因为numpy数组的所有元素都需要相等的大小。另请注意,可能需要从sympy的数值类型到numpy理解的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,1],alpha=0.5)
plt.legend()
plt.margins(x=0)
plt.xlabel('a')
plt.ylabel('bounds')
plt.show()
或已填充:
for i,:],color=color)
plt.fill_between(a_values,alpha=0.1)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。