本文整理汇总了Python中sympy.srepr函数的典型用法代码示例。如果您正苦于以下问题:Python srepr函数的具体用法?Python srepr怎么用?Python srepr使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了srepr函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: exact_match
def exact_match(test_expr, target_expr):
"""Test if the entered expression exactly matches the known expression.
This equality checking does not expand brackets or perform much simplification,
so is useful for checking if the submitted answer requires simplifying.
Testing is first done using '==' which checks that the order of symbols
matches and will not recognise 'x + 1' as equal to '1 + x'.
The 'srepr' method outputs sympy's internal representation in a canonical form
and thus, while performing no simplification, it allows ordering to be ignored
in exact match checking. These two forms are treated equivalently as 'exact'
matching.
Returns True if the sympy expressions have the same internal structure,
and False if not.
- 'test_expr' should be the untrusted sympy expression to check.
- 'target_expr' should be the trusted sympy expression to match against.
"""
print "[EXACT TEST]"
if test_expr == target_expr:
print "Exact Match (with '==')"
return True
elif sympy.srepr(test_expr) == sympy.srepr(target_expr):
print "Exact Match (with 'srepr')"
return True
else:
return False
示例2: __str__
def __str__(self):
asstr = "Objective: "
asstr += srepr(self.objective)
asstr += "\n\n"
asstr += "Subject to:\n"
for c in self._constraints:
asstr += srepr(c)
asstr += "\n"
return asstr
示例3: test_complex_space
def test_complex_space():
c1 = ComplexSpace(2)
assert isinstance(c1, ComplexSpace)
assert c1.dimension == 2
assert sstr(c1) == "C(2)"
assert srepr(c1) == "ComplexSpace(Integer(2))"
n = Symbol("n")
c2 = ComplexSpace(n)
assert isinstance(c2, ComplexSpace)
assert c2.dimension == n
assert sstr(c2) == "C(n)"
assert srepr(c2) == "ComplexSpace(Symbol('n'))"
assert c2.subs(n, 2) == ComplexSpace(2)
示例4: test_create_f
def test_create_f():
i, j, n, m = symbols('i,j,n,m')
o = Fd(i)
assert isinstance(o, CreateFermion)
o = o.subs(i, j)
assert o.atoms(Symbol) == {j}
o = Fd(1)
assert o.apply_operator(FKet([n])) == FKet([1, n])
assert o.apply_operator(FKet([n])) == -FKet([n, 1])
o = Fd(n)
assert o.apply_operator(FKet([])) == FKet([n])
vacuum = FKet([], fermi_level=4)
assert vacuum == FKet([], fermi_level=4)
i, j, k, l = symbols('i,j,k,l', below_fermi=True)
a, b, c, d = symbols('a,b,c,d', above_fermi=True)
p, q, r, s = symbols('p,q,r,s')
assert Fd(i).apply_operator(FKet([i, j, k], 4)) == FKet([j, k], 4)
assert Fd(a).apply_operator(FKet([i, b, k], 4)) == FKet([a, i, b, k], 4)
assert Dagger(B(p)).apply_operator(q) == q*CreateBoson(p)
assert repr(Fd(p)) == 'CreateFermion(p)'
assert srepr(Fd(p)) == "CreateFermion(Symbol('p'))"
assert latex(Fd(p)) == r'a^\dagger_{p}'
示例5: save_sympy_matrix
def save_sympy_matrix(matrix, filename):
"""Writes a matrix to file in the SymPy representation (srepr)."""
num_rows, num_cols = matrix.shape
with open(filename, 'w') as f:
f.write(str(num_rows) + "\n")
f.write(str(num_cols) + "\n")
for expr in matrix:
f.write(srepr(expr) + "\n")
示例6: test_fixed_bosonic_basis
def test_fixed_bosonic_basis():
b = FixedBosonicBasis(2, 2)
# assert b == [FockState((2, 0)), FockState((1, 1)), FockState((0, 2))]
state = b.state(1)
assert state == FockStateBosonKet((1, 1))
assert b.index(state) == 1
assert b.state(1) == b[1]
assert len(b) == 3
assert str(b) == '[FockState((2, 0)), FockState((1, 1)), FockState((0, 2))]'
assert repr(b) == '[FockState((2, 0)), FockState((1, 1)), FockState((0, 2))]'
assert srepr(b) == '[FockState((2, 0)), FockState((1, 1)), FockState((0, 2))]'
示例7: test_annihilate_f
def test_annihilate_f():
i, j, n, m = symbols('i,j,n,m')
o = F(i)
assert isinstance(o, AnnihilateFermion)
o = o.subs(i, j)
assert o.atoms(Symbol) == {j}
o = F(1)
assert o.apply_operator(FKet([1, n])) == FKet([n])
assert o.apply_operator(FKet([n, 1])) == -FKet([n])
o = F(n)
assert o.apply_operator(FKet([n])) == FKet([])
i, j, k, l = symbols('i,j,k,l', below_fermi=True)
a, b, c, d = symbols('a,b,c,d', above_fermi=True)
p, q, r, s = symbols('p,q,r,s')
assert F(i).apply_operator(FKet([i, j, k], 4)) == 0
assert F(a).apply_operator(FKet([i, b, k], 4)) == 0
assert F(l).apply_operator(FKet([i, j, k], 3)) == 0
assert F(l).apply_operator(FKet([i, j, k], 4)) == FKet([l, i, j, k], 4)
assert str(F(p)) == 'f(p)'
assert repr(F(p)) == 'AnnihilateFermion(p)'
assert srepr(F(p)) == "AnnihilateFermion(Symbol('p'))"
assert latex(F(p)) == 'a_{p}'
示例8: findForwarBackward
def findForwarBackward(formula):
fragments = findBrackets(formula)
for i in range(len(fragments)):
#formula = re.sub("(\w)"+re.escape(fragments[0]),r"\1_fragmentNb%d_"%i,formula)
formula = formula.replace(fragments[i],"_fragmentNb%d_"%i)
variables = unique(re.findall("_?[A-Za-z]\w*",formula))
command = ",".join(variables)+" = sympy.symbols(\"" + ",".join(variables) + "\")"
exec(command)
formula = sympy.expand(formula)
backward = []
forward = []
additiveBlocks = sympy.srepr(formula)
if additiveBlocks[0:3] == "Add":
additiveBlocks = sympy.sympify(additiveBlocks[4:len(additiveBlocks)-1])
else:
additiveBlocks = [sympy.sympify(additiveBlocks)]
for ee in additiveBlocks:
ee = str(sympy.simplify(ee))
ee = str(ee)
if ee[0] == "-":
ee = re.sub("^-","",ee)
backward.append(ee)
else:
forward.append(ee)
backward = "+".join(backward)
forward = "+".join(forward)
for i in range(len(fragments)):
backward = backward.replace("_fragmentNb%d_"%i,fragments[i])
forward = forward.replace("_fragmentNb%d_"%i,fragments[i])
# backward = re.sub("(?<!\w)fragmentNb%d(?!\w)"%i,fragments[i],backward)
# forward = re.sub("(?<!\w)fragmentNb%d(?!\w)"%i,fragments[i],forward)
return(forward,backward)
示例9: print
[ omega_x_terms_expr[theta_dot_expr], omega_x_terms_expr[psi_dot_expr], omega_x_terms_expr[phi_dot_expr] ] ] )
A_dot_expr = sympy.trigsimp(A_expr.diff(t_expr))
syms = [ theta_expr, psi_expr, phi_expr, theta_dot_expr, psi_dot_expr, phi_dot_expr ]
print("Dummifying sympy expressions...")
A_expr_dummy, A_expr_dummy_syms = sympyutils.dummify( A_expr, syms )
A_dot_expr_dummy, A_dot_expr_dummy_syms = sympyutils.dummify( A_dot_expr, syms )
print( "Saving sympy expressions...")
current_source_file_path = pathutils.get_current_source_file_path()
with open(current_source_file_path+"/data/sympy/quadrotorcamera3d_A_expr_dummy.dat", "w") as f: f.write(sympy.srepr(A_expr_dummy))
with open(current_source_file_path+"/data/sympy/quadrotorcamera3d_A_dot_expr_dummy.dat", "w") as f: f.write(sympy.srepr(A_dot_expr_dummy))
with open(current_source_file_path+"/data/sympy/quadrotorcamera3d_A_expr_dummy_syms.dat", "w") as f: f.write(sympy.srepr(sympy.Matrix(A_expr_dummy_syms)))
with open(current_source_file_path+"/data/sympy/quadrotorcamera3d_A_dot_expr_dummy_syms.dat", "w") as f: f.write(sympy.srepr(sympy.Matrix(A_dot_expr_dummy_syms)))
else:
print("Loading sympy expressions...")
current_source_file_path = pathutils.get_current_source_file_path()
with open(current_source_file_path+"/data/sympy/quadrotorcamera3d_A_expr_dummy.dat", "r") as f: A_expr_dummy = sympy.sympify(f.read())
with open(current_source_file_path+"/data/sympy/quadrotorcamera3d_A_dot_expr_dummy.dat", "r") as f: A_dot_expr_dummy = sympy.sympify(f.read())
with open(current_source_file_path+"/data/sympy/quadrotorcamera3d_A_expr_dummy_syms.dat", "r") as f: A_expr_dummy_syms = array(sympy.sympify(f.read())).squeeze()
示例10: Riemann
g = self.g
g_inv = self.g.inv()
scalar_curv = sympy.simplify(g_inv*Ricci)
scalar_curv = sympy.trace(scalar_curv)
self.scalar_curv = scalar_curv
return self.scalar_curv
if __name__ == "__main__":
import find_metric
k = -1
g,diff_form = find_metric.analytical(k) # k=0 gives flat space
R = Riemann(g,dim=3,sys_title="analytical")
print R.metric
from sympy import srepr
print srepr(R.system)
RC = R.find_Christoffel_tensor()
RR = R.find_Ricci_tensor()
scalarRR = R.find_scalar_curvature()
print "\nThe analytical curve element has the following metric for k=%.1f"%k
print g
print "\nThe Ricci tensor is given as"
print RR
print "\nand the scalar curvature is"
print scalarRR
"""
from sympy.abc import r,theta, phi, u,v
g,diff_form = find_metric.flat_sphere()
diff = [['dv*dv','dv*dw'],['dw*dv','dw*dw']]
示例11: _sympyrepr
def _sympyrepr(self, printer, *args):
return "RlpSum(" + str(self.query_symbols) + " in " + str(self.query) + ", " + srepr(self.expression) + ")"
示例12: test_hilbert_space
def test_hilbert_space():
hs = HilbertSpace()
assert isinstance(hs, HilbertSpace)
assert sstr(hs) == "H"
assert srepr(hs) == "HilbertSpace()"
示例13: test_issue1689
def test_issue1689():
assert srepr(S(1.0 + 0j)) == srepr(S(1.0)) == srepr(Real(1.0))
assert srepr(Real(1)) != srepr(Real(1.0))
示例14: print_conversions
def print_conversions(*modules):
for mod in modules:
for x in module_conversions(mod):
print '("' + mod.NAME + '", "%s", "%s"): "%s"' % (
x[0], x[1], srepr(x[2]))
示例15: test_issue1689
def test_issue1689():
assert srepr(S(1.0 + 0J)) == srepr(S(1.0)) == srepr(Float(1.0))