[英]How to substitute a SymPy symbol with an array of values?
I am currently have an array that has the bounds for an integral (bounds_symbolic).我目前有一个数组,它具有积分的界限 (bounds_symbolic)。 I am trying to substitute the symbolic bounds with an array of values.
我正在尝试用一组值替换符号边界。
At a high level, what I am trying to do is solve the following integral (here it is using the variable names):在高层次上,我想做的是解决以下积分(这里使用变量名):
And here is an example:这是一个例子:
example_integral example_integral
Here is my code thus far:到目前为止,这是我的代码:
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
equate = [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 = []
for i in range(0, 3):
bounds_symbolic.append(sympy.solve(equate[i], x))
# 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 = 1
integrand = []
for j in range(0, 3):
integrand.append(ellipse - test_functions[j])
# New array with a-values substituted into the bounds
bounds_a = bounds_symbolic
for j in range(0, 201):
bounds_a.subs(a, a_values[j])
When I run it, I get an error when I attempt to perform bounds_a.subs(a, a_values[j])
operation:当我运行它时,当我尝试执行
bounds_a.subs(a, a_values[j])
操作时出现错误:
AttributeError: 'list' object has no attribute 'subs'
What I would like to have happen is that the bounds_a
array has the same bounds that I solved for above, but with all of the "a" values substituted into it, as opposed to just having a symbolic "a".我想要发生的是
bounds_a
数组具有与我在上面解决的相同的界限,但是所有的“a”值都被代入其中,而不是仅仅有一个符号“a”。 I would also like to be able to substitute in a "b" value of 1.我还希望能够替换为 1 的“b”值。
How would I go about doing this?我将如何 go 这样做? Would it be better if I used a NumPy array instead of a list?
如果我使用 NumPy 数组而不是列表会更好吗?
Thank you!谢谢!
In an isympy
session with x
defined as a variable:在将
x
定义为变量的isympy
session 中:
In [51]: x
Out[51]: x
I can make a list of expressions:我可以列出一个表达式:
In [52]: alist = [2*x, 3*x, 4+x]
and perform subs
on each of them:并对他们每个人执行
subs
:
In [53]: [expr.subs({x:12}) for expr in alist]
Out[53]: [24, 36, 16]
and multiple subs:和多个潜艇:
In [54]: [[expr.subs({x:val}) for expr in alist] for val in [1,2,3]]
Out[54]: [[2, 3, 5], [4, 6, 6], [6, 9, 7]]
With lambdify
I can create a function that takes a numpy array as input (default is numpy
mode):使用
lambdify
,我可以创建一个 function,它将 numpy 数组作为输入(默认为numpy
模式):
In [61]: f = lambdify(x, alist)
In [62]: print(f.__doc__)
Created with lambdify. Signature:
func(x)
Expression:
[2*x, 3*x, x + 4]
Source code:
def _lambdifygenerated(x):
return ([2*x, 3*x, x + 4])
I can give f
a number:我可以给
f
一个数字:
In [63]: [f(i) for i in [1,2,3]]
Out[63]: [[2, 3, 5], [4, 6, 6], [6, 9, 7]]
or array:或数组:
In [64]: f(np.array([1,2,3]))
Out[64]: [array([2, 4, 6]), array([3, 6, 9]), array([5, 6, 7])]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.