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/79033a9e-9cd6-4ec3-9b71-c992f034c424%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to