本文整理汇总了Python中sympy.simplify方法的典型用法代码示例。如果您正苦于以下问题:Python sympy.simplify方法的具体用法?Python sympy.simplify怎么用?Python sympy.simplify使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sympy
的用法示例。
在下文中一共展示了sympy.simplify方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: generate_algebra_simplify_sample
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import simplify [as 别名]
def generate_algebra_simplify_sample(vlist, ops, min_depth, max_depth):
"""Randomly generate an algebra simplify dataset sample.
Given an input expression, produce the simplified expression.
See go/symbolic-math-dataset.
Args:
vlist: Variable list. List of chars that can be used in the expression.
ops: List of ExprOp instances. The allowed operators for the expression.
min_depth: Expression trees will not have a smaller depth than this. 0 means
there is just a variable. 1 means there is one operation.
max_depth: Expression trees will not have a larger depth than this. To make
all trees have the same depth, set this equal to `min_depth`.
Returns:
sample: String representation of the input.
target: String representation of the solution.
"""
depth = random.randrange(min_depth, max_depth + 1)
expr = random_expr(depth, vlist, ops)
sample = str(expr)
target = format_sympy_expr(sympy.simplify(sample))
return sample, target
示例2: testAlgebraInverse
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import simplify [as 别名]
def testAlgebraInverse(self):
dataset_objects = algorithmic_math.math_dataset_init(26)
counter = 0
for d in algorithmic_math.algebra_inverse(26, 0, 3, 10):
counter += 1
decoded_input = dataset_objects.int_decoder(d["inputs"])
solve_var, expression = decoded_input.split(":")
lhs, rhs = expression.split("=")
# Solve for the solve-var.
result = sympy.solve("%s-(%s)" % (lhs, rhs), solve_var)
target_expression = dataset_objects.int_decoder(d["targets"])
# Check that the target and sympy's solutions are equivalent.
self.assertEqual(
0, sympy.simplify(str(result[0]) + "-(%s)" % target_expression))
self.assertEqual(counter, 10)
示例3: testCalculusIntegrate
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import simplify [as 别名]
def testCalculusIntegrate(self):
dataset_objects = algorithmic_math.math_dataset_init(
8, digits=5, functions={"log": "L"})
counter = 0
for d in algorithmic_math.calculus_integrate(8, 0, 3, 10):
counter += 1
decoded_input = dataset_objects.int_decoder(d["inputs"])
var, expression = decoded_input.split(":")
target = dataset_objects.int_decoder(d["targets"])
for fn_name, fn_char in six.iteritems(dataset_objects.functions):
target = target.replace(fn_char, fn_name)
# Take the derivative of the target.
derivative = str(sympy.diff(target, var))
# Check that the derivative of the integral equals the input.
self.assertEqual(0, sympy.simplify("%s-(%s)" % (expression, derivative)))
self.assertEqual(counter, 10)
示例4: simplify_surd
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import simplify [as 别名]
def simplify_surd(value, sample_args, context=None):
"""E.g., "Simplify (2 + 5*sqrt(3))**2."."""
del value # unused
if context is None:
context = composition.Context()
entropy, sample_args = sample_args.peel()
while True:
base = random.randint(2, 20)
if sympy.Integer(base).is_prime:
break
num_primes_less_than_20 = 8
entropy -= math.log10(num_primes_less_than_20)
exp = _sample_surd(base, entropy, max_power=2, multiples_only=False)
simplified = sympy.expand(sympy.simplify(exp))
template = random.choice([
'Simplify {exp}.',
])
return example.Problem(
question=example.question(context, template, exp=exp),
answer=simplified)
示例5: bessel_basis
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import simplify [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
示例6: associated_legendre_polynomials
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import simplify [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
示例7: generate_algebra_simplify_sample
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import simplify [as 别名]
def generate_algebra_simplify_sample(vlist, ops, min_depth, max_depth):
"""Randomly generate an algebra simplify dataset sample.
Given an input expression, produce the simplified expression.
Args:
vlist: Variable list. List of chars that can be used in the expression.
ops: List of ExprOp instances. The allowed operators for the expression.
min_depth: Expression trees will not have a smaller depth than this. 0 means
there is just a variable. 1 means there is one operation.
max_depth: Expression trees will not have a larger depth than this. To make
all trees have the same depth, set this equal to `min_depth`.
Returns:
sample: String representation of the input.
target: String representation of the solution.
"""
depth = random.randrange(min_depth, max_depth + 1)
expr = random_expr(depth, vlist, ops)
sample = str(expr)
target = format_sympy_expr(sympy.simplify(sample))
return sample, target
示例8: get_equations
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import simplify [as 别名]
def get_equations(self):
"""
:return: Functions to calculate A, B and f given state x and input u
"""
f = sp.zeros(3, 1)
x = sp.Matrix(sp.symbols('x y theta', real=True))
u = sp.Matrix(sp.symbols('v w', real=True))
f[0, 0] = u[0, 0] * sp.cos(x[2, 0])
f[1, 0] = u[0, 0] * sp.sin(x[2, 0])
f[2, 0] = u[1, 0]
f = sp.simplify(f)
A = sp.simplify(f.jacobian(x))
B = sp.simplify(f.jacobian(u))
f_func = sp.lambdify((x, u), f, 'numpy')
A_func = sp.lambdify((x, u), A, 'numpy')
B_func = sp.lambdify((x, u), B, 'numpy')
return f_func, A_func, B_func
示例9: get_equations
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import simplify [as 别名]
def get_equations(self):
"""
:return: Functions to calculate A, B and f given state x and input u
"""
f = sp.zeros(6, 1)
x = sp.Matrix(sp.symbols('rx ry vx vy t w', real=True))
u = sp.Matrix(sp.symbols('gimbal T', real=True))
f[0, 0] = x[2, 0]
f[1, 0] = x[3, 0]
f[2, 0] = 1 / self.m * sp.sin(x[4, 0] + u[0, 0]) * u[1, 0]
f[3, 0] = 1 / self.m * (sp.cos(x[4, 0] + u[0, 0]) * u[1, 0] - self.m * self.g)
f[4, 0] = x[5, 0]
f[5, 0] = 1 / self.I * (-sp.sin(u[0, 0]) * u[1, 0] * self.r_T)
f = sp.simplify(f)
A = sp.simplify(f.jacobian(x))
B = sp.simplify(f.jacobian(u))
f_func = sp.lambdify((x, u), f, 'numpy')
A_func = sp.lambdify((x, u), A, 'numpy')
B_func = sp.lambdify((x, u), B, 'numpy')
return f_func, A_func, B_func
示例10: derivatives_in_spherical_coordinates
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import simplify [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)).simplify())
print('grad^B =',grad^B)
示例11: derivatives_in_spherical_coordinates
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import simplify [as 别名]
def derivatives_in_spherical_coordinates():
Print_Function()
X = (r,th,phi) = symbols('r theta phi')
s3d = Ga('e_r e_theta e_phi',g=[1,r**2,r**2*sin(th)**2],coords=X,norm=True)
(er,eth,ephi) = s3d.mv()
grad = s3d.grad
f = s3d.mv('f','scalar',f=True)
A = s3d.mv('A','vector',f=True)
B = s3d.mv('B','bivector',f=True)
print('f =',f)
print('A =',A)
print('B =',B)
print('grad*f =',grad*f)
print('grad|A =',grad|A)
print('-I*(grad^A) =',(-s3d.E()*(grad^A)).simplify())
print('grad^B =',grad^B)
示例12: test_check_generalized_BAC_CAB_formulas
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import simplify [as 别名]
def test_check_generalized_BAC_CAB_formulas(self):
a, b, c, d, e = Ga('a b c d e').mv()
assert str(a|(b*c)) == '-(a.c)*b + (a.b)*c'
assert str(a|(b^c)) == '-(a.c)*b + (a.b)*c'
assert str(a|(b^c^d)) == '(a.d)*b^c - (a.c)*b^d + (a.b)*c^d'
expr = (a|(b^c))+(c|(a^b))+(b|(c^a)) # = (a.b)*c - (b.c)*a - ((a.b)*c - (b.c)*a)
assert str(expr.simplify()) == '0'
assert str(a*(b^c)-b*(a^c)+c*(a^b)) == '3*a^b^c'
assert str(a*(b^c^d)-b*(a^c^d)+c*(a^b^d)-d*(a^b^c)) == '4*a^b^c^d'
assert str((a^b)|(c^d)) == '-(a.c)*(b.d) + (a.d)*(b.c)'
assert str(((a^b)|c)|d) == '-(a.c)*(b.d) + (a.d)*(b.c)'
assert str(Ga.com(a^b, c^d)) == '-(b.d)*a^c + (b.c)*a^d + (a.d)*b^c - (a.c)*b^d'
assert str((a|(b^c))|(d^e)) == '(-(a.b)*(c.e) + (a.c)*(b.e))*d + ((a.b)*(c.d) - (a.c)*(b.d))*e'
示例13: test_derivatives_in_spherical_coordinates
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import simplify [as 别名]
def test_derivatives_in_spherical_coordinates(self):
X = r, th, phi = symbols('r theta phi')
s3d = Ga('e_r e_theta e_phi', g=[1, r ** 2, r ** 2 * sin(th) ** 2], coords=X, norm=True)
er, eth, ephi = s3d.mv()
grad = s3d.grad
f = s3d.mv('f', 'scalar', f=True)
A = s3d.mv('A', 'vector', f=True)
B = s3d.mv('B', 'bivector', f=True)
assert str(f) == 'f'
assert str(A) == 'A__r*e_r + A__theta*e_theta + A__phi*e_phi'
assert str(B) == 'B__rtheta*e_r^e_theta + B__rphi*e_r^e_phi + B__thetaphi*e_theta^e_phi'
assert str(grad*f) == 'D{r}f*e_r + D{theta}f*e_theta/r + D{phi}f*e_phi/(r*sin(theta))'
assert str((grad|A).simplify()) == '(r*D{r}A__r + 2*A__r + A__theta/tan(theta) + D{theta}A__theta + D{phi}A__phi/sin(theta))/r'
assert str(-s3d.I()*(grad^A)) == '(A__phi/tan(theta) + D{theta}A__phi - D{phi}A__theta/sin(theta))*e_r/r + (-r*D{r}A__phi - A__phi + D{phi}A__r/sin(theta))*e_theta/r + (r*D{r}A__theta + A__theta - D{theta}A__r)*e_phi/r'
assert latex(grad) == r'\boldsymbol{e}_{r} \frac{\partial}{\partial r} + \boldsymbol{e}_{\theta } \frac{1}{r} \frac{\partial}{\partial \theta } + \boldsymbol{e}_{\phi } \frac{1}{r \sin{\left (\theta \right )}} \frac{\partial}{\partial \phi }'
assert latex(B|(eth^ephi)) == r'- B^{\theta \phi } {\left (r,\theta ,\phi \right )}'
assert str(grad^B) == '(r*D{r}B__thetaphi - B__rphi/tan(theta) + 2*B__thetaphi - D{theta}B__rphi + D{phi}B__rtheta/sin(theta))*e_r^e_theta^e_phi/r'
示例14: grad_log_norm_symbolic
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import simplify [as 别名]
def grad_log_norm_symbolic(self):
import sympy
D = self.dimension
X = self.eigenvalues_symbol
B = [1] * D
for d in range(D):
for dd in range(D):
if d != dd:
B[d] = B[d] * (X[d] - X[dd])
B = [1 / b for b in B]
p_D = sympy.pi ** D
tmp = [b * sympy.exp(x_) for x_, b in zip(X, B)]
tmp = sum(tmp)
symbolic_norm_for_bingham = 2 * p_D * tmp
return [
sympy.simplify(sympy.diff(
sympy.log(symbolic_norm_for_bingham),
x_
))
for x_ in X
]
示例15: simplifyEquation
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import simplify [as 别名]
def simplifyEquation(input_str):
"""
Only simplify the equation if there is no obvious problem
Equation is not simplified if it includes the following terms:
exp
log
"""
s_list = list()
# these are considered the "dangerous" operation that will
# overflow/underflow in np
s_list.append(len(input_str.atoms(exp)))
s_list.append(len(input_str.atoms(log)))
if np.sum(s_list) != 0:
# it is dangerous to simplify!
return input_str, True
else:
#TODO: Removed actual simplyify (do we need it?)
return input_str, False