本文整理汇总了Python中sympy.symbols方法的典型用法代码示例。如果您正苦于以下问题:Python sympy.symbols方法的具体用法?Python sympy.symbols怎么用?Python sympy.symbols使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sympy
的用法示例。
在下文中一共展示了sympy.symbols方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: diff_inference
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import symbols [as 别名]
def diff_inference():
r,s=symbols('r,s')
la1,la2,lb1,lb2=symbols('l^a_1,l^a_2,l^b_1,l^b_2')
la1=(1-s)/2
la2=(1+s)/2
lb1=(1-r)/2
lb2=(1+r)/2
N1=la1*lb1
N2=la1*lb2
N3=la2*lb1
N4=la2*lb2
N=Matrix([[N1,0,N2,0,N3,0,N4,0],
[0,N1,0,N2,0,N3,0,N4]])
示例2: simplify_power
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import symbols [as 别名]
def simplify_power(value, sample_args, context=None):
"""E.g., "Simplify ((x**2)**3/x**4)**2/x**3."."""
del value # unused
if context is None:
context = composition.Context()
entropy, sample_args = sample_args.peel()
variable = sympy.symbols(context.pop(), positive=True)
unsimplified = polynomials.sample_messy_power(variable, entropy)
answer = unsimplified.sympy()
template = random.choice([
'Simplify {unsimplified} assuming {variable} is positive.',
])
return example.Problem(
example.question(
context, template, unsimplified=unsimplified, variable=variable),
answer)
示例3: test_construct_lazy
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import symbols [as 别名]
def test_construct_lazy(self):
# adapted from https://gist.github.com/raddy/bd0e977dc8437a4f8276
spot, strike, vol, dte, rate, cp = sy.symbols('spot strike vol dte rate cp')
T = dte / 260.
N = syNormal('N', 0.0, 1.0)
d1 = (sy.ln(spot / strike) + (0.5 * vol ** 2) * T) / (vol * sy.sqrt(T))
d2 = d1 - vol * sy.sqrt(T)
TimeValueExpr = sy.exp(-rate * T) * (cp * spot * cdf(N)(cp * d1) - cp * strike * cdf(N)(cp * d2))
PriceClass = ts.construct_lazy(TimeValueExpr)
price = PriceClass(spot=210.59, strike=205, vol=14.04, dte=4, rate=.2175, cp=-1)
x = price.evaluate()()
assert price.evaluate()() == x
price.strike = 210
assert x != price.evaluate()()
示例4: bessel_basis
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import symbols [as 别名]
def bessel_basis(n, k):
zeros = Jn_zeros(n, k)
normalizer = []
for order in range(n):
normalizer_tmp = []
for i in range(k):
normalizer_tmp += [0.5 * Jn(zeros[order, i], order + 1)**2]
normalizer_tmp = 1 / np.array(normalizer_tmp)**0.5
normalizer += [normalizer_tmp]
f = spherical_bessel_formulas(n)
x = sym.symbols('x')
bess_basis = []
for order in range(n):
bess_basis_tmp = []
for i in range(k):
bess_basis_tmp += [
sym.simplify(normalizer[order][i] *
f[order].subs(x, zeros[order, i] * x))
]
bess_basis += [bess_basis_tmp]
return bess_basis
示例5: associated_legendre_polynomials
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import symbols [as 别名]
def associated_legendre_polynomials(k, zero_m_only=True):
z = sym.symbols('z')
P_l_m = [[0] * (j + 1) for j in range(k)]
P_l_m[0][0] = 1
if k > 0:
P_l_m[1][0] = z
for j in range(2, k):
P_l_m[j][0] = sym.simplify(((2 * j - 1) * z * P_l_m[j - 1][0] -
(j - 1) * P_l_m[j - 2][0]) / j)
if not zero_m_only:
for i in range(1, k):
P_l_m[i][i] = sym.simplify((1 - 2 * i) * P_l_m[i - 1][i - 1])
if i + 1 < k:
P_l_m[i + 1][i] = sym.simplify(
(2 * i + 1) * z * P_l_m[i][i])
for j in range(i + 2, k):
P_l_m[j][i] = sym.simplify(
((2 * j - 1) * z * P_l_m[j - 1][i] -
(i + j - 1) * P_l_m[j - 2][i]) / (j - i))
return P_l_m
示例6: test_jscode_Indexed
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import symbols [as 别名]
def test_jscode_Indexed():
from sympy.tensor import IndexedBase, Idx
from sympy import symbols
i, j, k, n, m, o = symbols('i j k n m o', integer=True)
p = JavascriptCodePrinter()
p._not_c = set()
x = IndexedBase('x')[Idx(j, n)]
assert p._print_Indexed(x) == 'x[j]'
A = IndexedBase('A')[Idx(i, m), Idx(j, n)]
assert p._print_Indexed(A) == 'A[%s]' % str(j + n*i)
B = IndexedBase('B')[Idx(i, m), Idx(j, n), Idx(k, o)]
assert p._print_Indexed(B) == 'B[%s]' % str(k + i*n*o + j*o)
assert p._not_c == set()
示例7: test_jscode_loops_matrix_vector
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import symbols [as 别名]
def test_jscode_loops_matrix_vector():
n, m = symbols('n m', integer=True)
A = IndexedBase('A')
x = IndexedBase('x')
y = IndexedBase('y')
i = Idx('i', m)
j = Idx('j', n)
s = (
'for (var i=0; i<m; i++){\n'
' y[i] = 0;\n'
'}\n'
'for (var i=0; i<m; i++){\n'
' for (var j=0; j<n; j++){\n'
' y[i] = y[i] + A[i*n + j]*x[j];\n'
' }\n'
'}'
)
c = jscode(A[i, j]*x[j], assign_to=y[i])
assert c == s
示例8: test_dummy_loops
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import symbols [as 别名]
def test_dummy_loops():
# the following line could also be
# [Dummy(s, integer=True) for s in 'im']
# or [Dummy(integer=True) for s in 'im']
i, m = symbols('i m', integer=True, cls=Dummy)
x = IndexedBase('x')
y = IndexedBase('y')
i = Idx(i, m)
expected = (
'for (var i_%(icount)i=0; i_%(icount)i<m_%(mcount)i; i_%(icount)i++){\n'
' y[i_%(icount)i] = x[i_%(icount)i];\n'
'}'
) % {'icount': i.label.dummy_index, 'mcount': m.dummy_index}
code = jscode(x[i], assign_to=y[i])
assert code == expected
示例9: test_jscode_loops_add
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import symbols [as 别名]
def test_jscode_loops_add():
from sympy.tensor import IndexedBase, Idx
from sympy import symbols
n, m = symbols('n m', integer=True)
A = IndexedBase('A')
x = IndexedBase('x')
y = IndexedBase('y')
z = IndexedBase('z')
i = Idx('i', m)
j = Idx('j', n)
s = (
'for (var i=0; i<m; i++){\n'
' y[i] = x[i] + z[i];\n'
'}\n'
'for (var i=0; i<m; i++){\n'
' for (var j=0; j<n; j++){\n'
' y[i] = y[i] + A[i*n + j]*x[j];\n'
' }\n'
'}'
)
c = jscode(A[i, j]*x[j] + x[i] + z[i], assign_to=y[i])
assert c == s
示例10: test_ccode_Indexed
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import symbols [as 别名]
def test_ccode_Indexed():
from sympy.tensor import IndexedBase, Idx
from sympy import symbols
n, m, o = symbols('n m o', integer=True)
i, j, k = Idx('i', n), Idx('j', m), Idx('k', o)
p = CCodePrinter()
p._not_c = set()
x = IndexedBase('x')[j]
assert p._print_Indexed(x) == 'x[j]'
A = IndexedBase('A')[i, j]
assert p._print_Indexed(A) == 'A[%s]' % (m*i+j)
B = IndexedBase('B')[i, j, k]
assert p._print_Indexed(B) == 'B[%s]' % (i*o*m+j*o+k)
assert p._not_c == set()
示例11: test_dummy_loops
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import symbols [as 别名]
def test_dummy_loops():
# the following line could also be
# [Dummy(s, integer=True) for s in 'im']
# or [Dummy(integer=True) for s in 'im']
i, m = symbols('i m', integer=True, cls=Dummy)
x = IndexedBase('x')
y = IndexedBase('y')
i = Idx(i, m)
expected = (
'for (int i_%(icount)i=0; i_%(icount)i<m_%(mcount)i; i_%(icount)i++){\n'
' y[i_%(icount)i] = x[i_%(icount)i];\n'
'}'
) % {'icount': i.label.dummy_index, 'mcount': m.dummy_index}
code = ccode(x[i], assign_to=y[i])
assert code == expected
示例12: test_ccode_loops_add
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import symbols [as 别名]
def test_ccode_loops_add():
from sympy.tensor import IndexedBase, Idx
from sympy import symbols
n, m = symbols('n m', integer=True)
A = IndexedBase('A')
x = IndexedBase('x')
y = IndexedBase('y')
z = IndexedBase('z')
i = Idx('i', m)
j = Idx('j', n)
s = (
'for (int i=0; i<m; i++){\n'
' y[i] = x[i] + z[i];\n'
'}\n'
'for (int i=0; i<m; i++){\n'
' for (int j=0; j<n; j++){\n'
' y[i] = y[i] + A[%s]*x[j];\n' % (i*n + j) +\
' }\n'
'}'
)
c = ccode(A[i, j]*x[j] + x[i] + z[i], assign_to=y[i])
assert c == s
示例13: __init__
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import symbols [as 别名]
def __init__(self, in_vars, out_vars):
"""Initialize physics layer.
Args:
in_vars: str, a string of input variable names separated by space.
E.g., 'x y t' for the three variables x, y and t.
out_vars: str, a string of output variable names separated by space.
E.g., 'u v p' for the three variables u, v and p.
"""
self.in_vars = sympy.symbols(in_vars)
self.out_vars = sympy.symbols(out_vars)
if not isinstance(self.in_vars, tuple): self.in_vars = (self.in_vars,)
if not isinstance(self.out_vars, tuple): self.out_vars = (self.out_vars,)
self.n_in = len(self.in_vars)
self.n_out = len(self.out_vars)
self.all_vars = list(self.in_vars) + list(self.out_vars)
self.eqns_raw = {} # raw string equations
self.eqns_fn = {} # lambda function for the equations
self.forward_method = None
示例14: _sympy_to_native
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import symbols [as 别名]
def _sympy_to_native(symbol):
try:
if symbol.is_Integer:
return int(symbol)
if symbol.is_Float:
return float(symbol)
if symbol.is_Complex:
return complex(symbol)
return symbol
except Exception as ex:
raise RuntimeError(
"Could not convert sympy symbol to native type."
"It may be due to misinterpretation of some symbols by sympy."
"Try to use sympy expressions as gate parameters' values "
"explicitly."
) from ex
示例15: derivatives_in_spherical_coordinates
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import symbols [as 别名]
def derivatives_in_spherical_coordinates():
Print_Function()
X = (r,th,phi) = symbols('r theta phi')
curv = [[r*cos(phi)*sin(th),r*sin(phi)*sin(th),r*cos(th)],[1,r,r*sin(th)]]
(er,eth,ephi,grad) = MV.setup('e_r e_theta e_phi',metric='[1,1,1]',coords=X,curv=curv)
f = MV('f','scalar',fct=True)
A = MV('A','vector',fct=True)
B = MV('B','grade2',fct=True)
print('f =',f)
print('A =',A)
print('B =',B)
print('grad*f =',grad*f)
print('grad|A =',grad|A)
print('-I*(grad^A) =',-MV.I*(grad^A))
print('grad^B =',grad^B)
return