Re: [sympy] Re: pattern replacement for complex numbers and manual factorization

2014-07-13 Thread Aaron Meurer
On Thu, Jul 3, 2014 at 5:44 PM, F. B. franz.bona...@gmail.com wrote: On Thursday, July 3, 2014 11:46:21 PM UTC+2, Aaron Meurer wrote: On Mon, Jun 23, 2014 at 10:02 AM, F. B. franz@gmail.com wrote: I suggest to keep the Mathics implementation of pattern matching as a reference. It

Re: [sympy] Re: pattern replacement for complex numbers and manual factorization

2014-07-03 Thread Aaron Meurer
On Mon, Jun 23, 2014 at 10:02 AM, F. B. franz.bona...@gmail.com wrote: I suggest to keep the Mathics implementation of pattern matching as a reference. It should be compatible with Mathematica, but, unlike Mathematica, we can access Mathics' source code. If you think you can use mathics code

Re: [sympy] Re: pattern replacement for complex numbers and manual factorization

2014-07-03 Thread F. B.
On Thursday, July 3, 2014 11:46:21 PM UTC+2, Aaron Meurer wrote: On Mon, Jun 23, 2014 at 10:02 AM, F. B. franz@gmail.com javascript: wrote: I suggest to keep the Mathics implementation of pattern matching as a reference. It should be compatible with Mathematica, but, unlike

[sympy] Re: pattern replacement for complex numbers and manual factorization

2014-06-26 Thread Chris Smith
With existing methods you can do the following: First, to share a long expression, try filldedent. from sympy.utilities.misc import filldedent print filldedent(eq) -2*g*psi^ss_1*conjugate(psi^ss_1) - 2*g*psi^ss_2*conjugate(psi^ss_2) + omega_2 - (-k + k_2)**2/(2*m) That can be sympified by

Re: [sympy] Re: pattern replacement for complex numbers and manual factorization

2014-06-23 Thread F. B.
I suggest to keep the Mathics implementation of pattern matching as a reference. It should be compatible with Mathematica, but, unlike Mathematica, we can access Mathics' source code. On Sunday, June 22, 2014 9:29:29 PM UTC+2, Aaron Meurer wrote: Thanks for writing this up. My

Re: [sympy] Re: pattern replacement for complex numbers and manual factorization

2014-06-23 Thread F. B.
On Monday, June 23, 2014 5:02:27 PM UTC+2, F. B. wrote: I mean, you can use the alternatives to match more patterns at the same time. Something like: x_**n_ | x_ which would match both Symbol(x) and Pow(x, n). This is also an example in which default values could be useful. In case

Re: [sympy] Re: pattern replacement for complex numbers and manual factorization

2014-06-22 Thread Aaron Meurer
Thanks for writing this up. My comments/questions: - You say, For a mathematically sensitive pattern matcher, there should be support to construct advanced patterns, such as OR expressions along the pattern tree. Can you give an example of this? What would the syntax look like? Is it just the

Re: [sympy] Re: pattern replacement for complex numbers and manual factorization

2014-06-15 Thread F. B.
I wrote some ideas: https://github.com/sympy/sympy/wiki/Proposal-for-a-new-pattern-matching On Monday, June 9, 2014 9:36:50 AM UTC+2, F. B. wrote: On Sunday, June 8, 2014 3:38:23 AM UTC+2, Matthew wrote: I think that SymPy needs a better term rewriting system. And also a better pattern

Re: [sympy] Re: pattern replacement for complex numbers and manual factorization

2014-06-09 Thread F. B.
On Sunday, June 8, 2014 3:38:23 AM UTC+2, Matthew wrote: I think that SymPy needs a better term rewriting system. And also a better pattern matcher. @Franz Are you interested in doing work in this area? Maybe the best we can do is to set up a page on SymPy's wiki to propose an

Re: [sympy] Re: pattern replacement for complex numbers and manual factorization

2014-06-07 Thread Christophe Bal
Hello. in Mathematica I can just do expr /.{x_*Conj[x_] - Abs[x]^2} With an easy to use treeview class, I do not think that it will be too hard to implement a method that can acheive this kind of using something like expr.replace(x_*Conj[x_], Abs[x]^2) Christophe BAL -- You received this

Re: [sympy] Re: pattern replacement for complex numbers and manual factorization

2014-06-07 Thread Andrei Berceanu
By the way, I discovered another counter-example. Expressions like a**2*conjugate(a) are not transformed to a*abs(a)**2 About the friendly treeview class, I agree and hope its coming soon :) is it in the works? On Saturday, June 7, 2014 7:04:22 PM UTC+2, Christophe Bal wrote: Hello. in

Re: [sympy] Re: pattern replacement for complex numbers and manual factorization

2014-06-07 Thread F. B.
The point is that the current pattern matcher only matches exact subtrees starting at one node, i.e. the tree of that node has to be matched entirely, while in many cases partial subtree matches would be desired. In any case the expression to_abs can be further simplified. Try something like

Re: [sympy] Re: pattern replacement for complex numbers and manual factorization

2014-06-07 Thread F. B.
Sorry instead of rl try to put lambda x: next(rl(x)) into the exhaust ( ... ) expression. -- 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

Re: [sympy] Re: pattern replacement for complex numbers and manual factorization

2014-06-07 Thread Matthew Rocklin
I think that SymPy needs a better term rewriting system. And also a better pattern matcher. @Franz Are you interested in doing work in this area? On Sat, Jun 7, 2014 at 10:39 AM, F. B. franz.bona...@gmail.com wrote: Sorry instead of rl try to put lambda x: next(rl(x)) into the exhaust (

Re: [sympy] Re: pattern replacement for complex numbers and manual factorization

2014-06-07 Thread Matthew Rocklin
Also I'm glad to see people using strategies. I've since pulled it out to an external package. Although this wouldn't support SymPy tree traversals like bottom_up without slightly more work. https://github.com/logpy/strategies On Sat, Jun 7, 2014 at 6:38 PM, Matthew Rocklin mrock...@gmail.com

[sympy] Re: pattern replacement for complex numbers and manual factorization

2014-06-06 Thread F. B.
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'),

[sympy] Re: pattern replacement for complex numbers and manual factorization

2014-06-06 Thread F. B.
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'),

[sympy] Re: pattern replacement for complex numbers and manual factorization

2014-06-06 Thread Andrei Berceanu
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

[sympy] Re: pattern replacement for complex numbers and manual factorization

2014-06-06 Thread F. B.
On Friday, June 6, 2014 12:22:14 PM UTC+2, Andrei Berceanu wrote: 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) Yes, sorry, just remove the last parenthesis. Also, do you know how I can force the

[sympy] Re: pattern replacement for complex numbers and manual factorization

2014-06-06 Thread Andrei Berceanu
That fixed the error, however, the problem is now if I apply this function to expressions like -2*g*conjugate(psi^ss_1)*conjugate(psi^ss_2) I get -2*g*conjugate(psi^ss_1)*Abs(psi^ss_2)**2 so it looks like the pattern matching is working overtime, since it should leave the expression

[sympy] Re: pattern replacement for complex numbers and manual factorization

2014-06-06 Thread Andrei Berceanu
The unflatten_mul function factorized the 2, but not the g, i.e. it returns 2(g*|psi1|**2 + g*|psi2|**2) instead of 2g*(|psi1|**2 + |psi2|**2) On Friday, June 6, 2014 1:07:26 PM UTC+2, F. B. wrote: On Friday, June 6, 2014 12:22:14 PM UTC+2, Andrei Berceanu wrote: Tnx! I think there is

Re: [sympy] Re: pattern replacement for complex numbers and manual factorization

2014-06-06 Thread Christophe Bal
Hello. All the receipts in this dicussion look very interesting.Maybe all of this ones could be put in the official documentation. Christophe BAL 2014-06-06 13:34 GMT+02:00 Andrei Berceanu andreiberce...@gmail.com: The unflatten_mul function factorized the 2, but not the g, i.e. it returns

Re: [sympy] Re: pattern replacement for complex numbers and manual factorization

2014-06-06 Thread Andrei Berceanu
I agree, this could be helpful to many people. But first let's make sure we iron out all the bugs. And then I suppose one could re-write the expressions in a more user-friendly form? What do you propose, Chris? On Friday, June 6, 2014 2:16:15 PM UTC+2, Christophe Bal wrote: Hello. All the

[sympy] Re: pattern replacement for complex numbers and manual factorization

2014-06-06 Thread F. B.
Try to use this one: from sympy.unify import unify def to_abs(node): if not isinstance(node, Mul): return node zm1, zm2 = symbols('zm1, zm2') m = unify(node, zm1 * zm2 * conjugate(zm2), {}, variables=[zm1, zm2]) try: m = next(m) except: return node

[sympy] Re: pattern replacement for complex numbers and manual factorization

2014-06-06 Thread F. B.
On Friday, June 6, 2014 1:34:04 PM UTC+2, Andrei Berceanu wrote: The unflatten_mul function factorized the 2, but not the g, i.e. it returns 2(g*|psi1|**2 + g*|psi2|**2) instead of 2g*(|psi1|**2 + |psi2|**2) Try to do so: def get_unflattener(m): m_args = list(m.args)

Re: [sympy] Re: pattern replacement for complex numbers and manual factorization

2014-06-06 Thread Christophe Bal
I have no special ideas to propose except to start with the examples in this discussion. Maybe different working methods can be shown. 2014-06-06 14:23 GMT+02:00 Andrei Berceanu andreiberce...@gmail.com: I agree, this could be helpful to many people. But first let's make sure we iron out all

Re: [sympy] Re: pattern replacement for complex numbers and manual factorization

2014-06-06 Thread F. B.
I think that SymPy needs a better term rewriting system. And also a better pattern matcher. -- 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

Re: [sympy] Re: pattern replacement for complex numbers and manual factorization

2014-06-06 Thread Christophe Bal
A easy to use treeview will be a great tool. No ? 2014-06-06 19:52 GMT+02:00 F. B. franz.bona...@gmail.com: I think that SymPy needs a better term rewriting system. And also a better pattern matcher. -- You received this message because you are subscribed to the Google Groups sympy group.

Re: [sympy] Re: pattern replacement for complex numbers and manual factorization

2014-06-06 Thread Andrei Berceanu
Well yes, but that doesn´t change the fact that in Mathematica I can just do expr /.{x_*Conj[x_] - Abs[x]^2} and it just works! On Friday, June 6, 2014 8:59:56 PM UTC+2, Christophe Bal wrote: A easy to use treeview will be a great tool. No ? 2014-06-06 19:52 GMT+02:00 F. B.

Re: [sympy] Re: pattern replacement for complex numbers and manual factorization

2014-06-06 Thread Andrei Berceanu
By the way, I just found another case where the modulus value squared substitution fails: a, b, c, d = symbols('a b c d') expr = a*b*conjugate(a)*conjugate(b) + c*d*conjugate(c)*conjugate(d) bottom_up(chain(rebuild, to_abs))(expr) -- a*conjugate(a)*Abs(b)**2 + c*conjugate(c)*Abs(d)**2 instead