Tnx! I think there is an error in the line (unbalanced paranthesis): return node.xreplace ({e: S.One, conjugate(e): S.One})*abs(e)**2)
Also, do you know how I can force the factorization of the 2*g to get 2*g(|psi1|**2 + |psi2|**2)? On Friday, June 6, 2014 10:45:37 AM UTC+2, F. B. wrote: > > A better alternative: > > In [1]: from sympy import * > > In [2]: expr = eval("Add(Mul(Integer(-1), Integer(2), Symbol('g'), > Symbol('psi^ss_1'), conjugate(Symbol('psi^ss_1'))), Mul(Integer(-1), > Integer(2), Symbol('g'), Symbol('psi^ss_2'), > conjugate(Symbol('psi^ss_2'))), Symbol('omega_2'), Mul(Integer(-1), > Rational(1, 2), Pow(Symbol('m'), Integer(-1)), Pow(Add(Mul(Integer(-1), > Symbol('k')), Symbol('k_2')), Integer(2))))") > > In [3]: from sympy.strategies.traverse import bottom_up > > In [4]: from sympy.strategies import chain, rebuild > > In [5]: def to_abs (node ): > ...: if not isinstance(node, Mul): > ...: return node > ...: z = Wild ('z') > ...: w = Wild ('w') > ...: m = node .match (z * w *conjugate (w)) > ...: if w in m: > ...: e =m[w] > ...: return node.xreplace ({e: S.One, conjugate(e): S. > One})*abs(e)**2) > ...: return node > ...: > > In [6]: bottom_up(chain(rebuild, to_abs))(expr) > Out[6]: > 2 > 2 2 (k + k₂) > - 2⋅g⋅│ψ_1__ss│ - 2⋅g⋅│ψ_2__ss│ + ω₂ + ───────── > 4⋅m > > > > On Friday, June 6, 2014 9:13:15 AM UTC+2, F. B. wrote: >> >> As you have a string representation of your object, a non-sympy way is by >> using string regex: >> >> In [1]: expr = eval("Add(Mul(Integer(-1), Integer(2), Symbol('g'), >> Symbol('psi^ss_1'), conjugate(Symbol('psi^ss_1'))), Mul(Integer(-1), >> Integer(2), Symbol('g'), Symbol('psi^ss_2'), >> conjugate(Symbol('psi^ss_2'))), Symbol('omega_2'), Mul(Integer(-1), >> Rational(1, 2), Pow(Symbol('m'), Integer(-1)), Pow(Add(Mul(Integer(-1), >> Symbol('k')), Symbol('k_2')), Integer(2))))") >> >> In [2]: expr >> Out[2]: >> 2 >> _______ _______ (-k + k₂) >> - 2⋅g⋅ψ_1__ss⋅ψ_1__ss - 2⋅g⋅ψ_2__ss⋅ψ_2__ss + ω₂ - ────────── >> 2⋅m >> >> In [3]: import re >> >> In [4]: eval(re.sub(r"(?P<quote>Symbol\('[^']+'\)), >> +conjugate\((?P=quote)\)", r"abs(\1)**2", srepr(expr))) >> Out[4]: >> 2 >> 2 2 (-k + k₂) >> - 2⋅g⋅│ψ_1__ss│ - 2⋅g⋅│ψ_2__ss│ + ω₂ - ────────── >> 2⋅m >> >> SymPy also has its own system of pattern matching, but it looks like it >> needs to match whole expressions. >> >> On Thursday, June 5, 2014 11:48:17 PM UTC+2, Andrei Berceanu wrote: >>> >>> I have the following expression in sympy: >>> >>> "Add(Mul(Integer(-1), Integer(2), Symbol('g'), Symbol('psi^ss_1'), >>> conjugate(Symbol('psi^ss_1'))), Mul(Integer(-1), Integer(2), Symbol('g'), >>> Symbol('psi^ss_2'), conjugate(Symbol('psi^ss_2'))), Symbol('omega_2'), >>> Mul(Integer(-1), Rational(1, 2), Pow(Symbol('m'), Integer(-1)), >>> Pow(Add(Mul(Integer(-1), Symbol('k')), Symbol('k_2')), Integer(2))))" >>> >>> >>> (sorry for the long line, didnt know how else to paste it) >>> >>> Anyway, I would like to factor 2g in front of the 2 terms that contain >>> it (simplify doesnt do it for some reason) and also would like to replace >>> all occurrences of x*conjugate(x) by abs(x)**2. There are two such >>> occurrences and I tried to do expr.replace(a*conjugate(a), abs(a)**2) >>> without any luck. >>> >>> Could anyone please help? >>> >>> Tnx, >>> Andrei >>> >> -- You received this message because you are subscribed to the Google Groups "sympy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sympy+unsubscr...@googlegroups.com. To post to this group, send email to sympy@googlegroups.com. Visit this group at http://groups.google.com/group/sympy. To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/3c3aed38-a1e5-4aff-9f1f-282cbc8c495b%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.