It looks to me like I haven’t fully understood how to use symbols.
I define b sub minus 1 and b sub minus 2 as
bm1, bm2 = symbols('b_{-1} b_{-2}')Then I define expression 129 as
ex129 = 2*F1*b1*bm1 + F1 + G1*bm1**2 + G1*bm2 - b1This reflects my expectation that I should use bm2 in the code, but the output via latex() will contain b_{-2}. However, when I look at ex129 in the debugger, it looks like this:
2*F_1*b_1*b_{-1} + F_1 + G_1*b_{-1}**2 + G_1*b_{-2} - b_1This is presumably why my subs() statement fails:
ex129a = ex129.subs(bm2, ex132a)I expect bm2 to be gone from ex129a, but it isn’t.
What is the correct way to do this?
The other problem is, even though
print(latex(ex129b))works fine,
print(latex(Eq(b1, ex129b)))produces the following error:
Message=SympifyError: [-(F_1 + G_1*b_{-1}**2 + G_1*b_{-2})/(2*F_1*b_{-1} - 1)]
Source=C:\data\Tom\Research\Hill\python\Ansatz3\solveTest1.py
StackTrace:
File "C:\data\Tom\Research\Hill\python\Ansatz3\solveTest1.py", line 48, in <module>
print(latex(Eq(b1, ex129b)))
Eq(b1, ex129b[0])
sol = solve(ex129a, b1)
ex129b = sol[0]
print('129 after solving for b1')
print(ex129b)
print(latex(ex129b))
print(latex(Eq(b1, ex129b)))ex129a = ex129.subs(bm2, ex132a)Dear Group,
I have been exploring some of the SymPy source files, and came across a parser. This puzzled me because I have always thought that SymPy didn't need its own parser because its syntax is consistent with Python, but then all the operators are overloaded as required.
I also discovered implicit multiplication in the code - so that xyz is parsed as x*y*z, but (presumably) sin is not parsed as s*i*n. Is this accessible somehow, or is it a relic of earlier ideas that have now been abandoned?
DavidSymPy indeed reuses Python's built in parser, but it has some extensions to the tokenizer to allow things like implicit multiplication (in sympy.parsing.sympy_parser). These things are not abandoned. You can use them via the parse_expr() function. See https://docs.sympy.org/latest/modules/parsing.html#sympy.parsing.sympy_parser.parse_expr
Thanks Aaron - that is an interesting feature that I knew nothing
about.
David