[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'), 
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(?PquoteSymbol\('[^']+'\)), 
+conjugate\((?P=quote)\), rabs(\1)**2, srepr(expr)))
Out[4]: 
  2
   22(-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.


[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'), 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
   22(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(?PquoteSymbol\('[^']+'\)), 
 +conjugate\((?P=quote)\), rabs(\1)**2, srepr(expr)))
 Out[4]: 
   2
22(-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/8ab4e3e4-4ec4-4268-bd4e-d3709b5246ab%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[sympy] Re: New user question: Expanding quotients of complex numbers

2014-06-06 Thread F. B.


On Friday, June 6, 2014 2:36:34 AM UTC+2, jeanbi...@gmail.com wrote:


 var('A,B,C,D,u,v,qi,qf')
 qi = 1/(u-I*v)
 qf = (A+B/qi)/(C+D/qi)


First of all, you don't need to declare *qi* and *qf* in var( ... ), 
because they get overwritten in the next expressions.

By the way, maybe you mean that A, B, C, D, u, v are real? What about this:

var('A,B,C,D,u,v', real=True)

expand(1/qf, complex=True) looks much nicer.

-- 
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/f4b4eb94-004a-4ea8-9f67-d717e049a6a6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[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 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
22(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(?PquoteSymbol\('[^']+'\)), 
 +conjugate\((?P=quote)\), rabs(\1)**2, srepr(expr)))
 Out[4]: 
   2
22(-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.


[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 factorization of the 2*g to get 
 2*g(|psi1|**2 + |psi2|**2)?


Try this one:

import collections

def unflatten_mul(node):
d = collections.defaultdict(lambda: [])
new_args = []
for arg in node.args:
if arg.args and arg.args[0].is_Number:
d[arg.args[0]].append(arg.func(*arg.args[1:]))
continue
new_args.append(arg)
print d
for key, item in d.items():
new_args.append(Mul(key, Add(*item), evaluate=False))
return node.func(*new_args, evaluate=False)


apply this function on *expr*, it should work.

-- 
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/1a5f7d9d-dbb1-4dc0-8cb3-4733adf65e79%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[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 unchanged :)



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 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 factorization of the 2*g to get 
 2*g(|psi1|**2 + |psi2|**2)?


 Try this one:

 import collections

 def unflatten_mul(node):
 d = collections.defaultdict(lambda: [])
 new_args = []
 for arg in node.args:
 if arg.args and arg.args[0].is_Number:
 d[arg.args[0]].append(arg.func(*arg.args[1:]))
 continue
 new_args.append(arg)
 print d
 for key, item in d.items():
 new_args.append(Mul(key, Add(*item), evaluate=False))
 return node.func(*new_args, evaluate=False)


 apply this function on *expr*, it should work.


-- 
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/b896e892-d217-472b-8bd7-0db4d6e154f1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[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 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 factorization of the 2*g to get 
 2*g(|psi1|**2 + |psi2|**2)?


 Try this one:

 import collections

 def unflatten_mul(node):
 d = collections.defaultdict(lambda: [])
 new_args = []
 for arg in node.args:
 if arg.args and arg.args[0].is_Number:
 d[arg.args[0]].append(arg.func(*arg.args[1:]))
 continue
 new_args.append(arg)
 print d
 for key, item in d.items():
 new_args.append(Mul(key, Add(*item), evaluate=False))
 return node.func(*new_args, evaluate=False)


 apply this function on *expr*, it should work.


-- 
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/4fd95b6b-043a-4bc5-8767-e9be9d83d9aa%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


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

 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 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 factorization of the 2*g to get
 2*g(|psi1|**2 + |psi2|**2)?


 Try this one:

 import collections

 def unflatten_mul(node):
 d = collections.defaultdict(lambda: [])
 new_args = []
 for arg in node.args:
 if arg.args and arg.args[0].is_Number:
 d[arg.args[0]].append(arg.func(*arg.args[1:]))
 continue
 new_args.append(arg)
 print d
 for key, item in d.items():
 new_args.append(Mul(key, Add(*item), evaluate=False))
 return node.func(*new_args, evaluate=False)


 apply this function on *expr*, it should work.

  --
 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/4fd95b6b-043a-4bc5-8767-e9be9d83d9aa%40googlegroups.com
 https://groups.google.com/d/msgid/sympy/4fd95b6b-043a-4bc5-8767-e9be9d83d9aa%40googlegroups.com?utm_medium=emailutm_source=footer
 .

 For more options, visit https://groups.google.com/d/optout.


-- 
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/CAAb4jG%3DLbR%3DzzMfUy4FRGGVeM_DP-486zRxwUyoBS9S6hhPz1Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


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 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 andreib...@gmail.com 
 javascript::

 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 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 factorization of the 2*g to get 
 2*g(|psi1|**2 + |psi2|**2)?


 Try this one:

 import collections

 def unflatten_mul(node):
 d = collections.defaultdict(lambda: [])
 new_args = []
 for arg in node.args:
 if arg.args and arg.args[0].is_Number:
 d[arg.args[0]].append(arg.func(*arg.args[1:]))
 continue
 new_args.append(arg)
 print d
 for key, item in d.items():
 new_args.append(Mul(key, Add(*item), evaluate=False))
 return node.func(*new_args, evaluate=False)


 apply this function on *expr*, it should work.

  -- 
 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+un...@googlegroups.com javascript:.
 To post to this group, send email to sy...@googlegroups.com javascript:
 .
 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/4fd95b6b-043a-4bc5-8767-e9be9d83d9aa%40googlegroups.com
  
 https://groups.google.com/d/msgid/sympy/4fd95b6b-043a-4bc5-8767-e9be9d83d9aa%40googlegroups.com?utm_medium=emailutm_source=footer
 .

 For more options, visit https://groups.google.com/d/optout.




-- 
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/1fd9343c-c5ba-4000-b64c-1d825da0aad8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[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
e = m[zm2]
return node.xreplace({e: S.One, conjugate(e): S.One})*abs(e)**2


I believe that the standard matcher matches too much, let's try the unify 
matcher, which is a structural matcher (and not a math-sensitive matcher).

On Friday, June 6, 2014 1:22:59 PM UTC+2, Andrei Berceanu wrote:

 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 unchanged :)



 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 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 factorization of the 2*g to get 
 2*g(|psi1|**2 + |psi2|**2)?


 Try this one:

 import collections

 def unflatten_mul(node):
 d = collections.defaultdict(lambda: [])
 new_args = []
 for arg in node.args:
 if arg.args and arg.args[0].is_Number:
 d[arg.args[0]].append(arg.func(*arg.args[1:]))
 continue
 new_args.append(arg)
 print d
 for key, item in d.items():
 new_args.append(Mul(key, Add(*item), evaluate=False))
 return node.func(*new_args, evaluate=False)


 apply this function on *expr*, it should work.



-- 
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/eae490f1-b891-437e-98b6-d0ef192d56dd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[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)
def unflattener(node):
new_args = []
sub_add_args = []
for arg in node.args:
common = [i for i in arg.args if i in m_args]
if set(common) == set(m_args):
sub_add_args.append(arg)
else:
new_args.append(arg)
sub_add = Add(*[i.func(*[j for j in i.args if j not in 
m_args]) for i in sub_add_args], evaluate=False)
return Add(*(new_args + [Mul(*(m_args + [sub_add]), evaluate
=False)]), evaluate=False)
return unflattener


get_unflattener(-2*g)(expr)
 

-- 
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/2e010cf4-7827-4d8c-9fe9-c485e0b77710%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


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 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 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 andreib...@gmail.com:

 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 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 factorization of the 2*g to get
 2*g(|psi1|**2 + |psi2|**2)?


 Try this one:

 import collections

 def unflatten_mul(node):
 d = collections.defaultdict(lambda: [])
 new_args = []
 for arg in node.args:
 if arg.args and arg.args[0].is_Number:
 d[arg.args[0]].append(arg.func(*arg.args[1:]))
 continue
 new_args.append(arg)
 print d
 for key, item in d.items():
 new_args.append(Mul(key, Add(*item), evaluate=False))
 return node.func(*new_args, evaluate=False)


 apply this function on *expr*, it should work.

  --
 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+un...@googlegroups.com.
 To post to this group, send email to sy...@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/4fd95b6b-043a-4bc5-8767-e9be9d83d9aa%40googlegroups.com
 https://groups.google.com/d/msgid/sympy/4fd95b6b-043a-4bc5-8767-e9be9d83d9aa%40googlegroups.com?utm_medium=emailutm_source=footer
 .

 For more options, visit https://groups.google.com/d/optout.


  --
 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/1fd9343c-c5ba-4000-b64c-1d825da0aad8%40googlegroups.com
 https://groups.google.com/d/msgid/sympy/1fd9343c-c5ba-4000-b64c-1d825da0aad8%40googlegroups.com?utm_medium=emailutm_source=footer
 .

 For more options, visit https://groups.google.com/d/optout.


-- 
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/CAAb4jGkM28eQ%3DjY7S8RheO8UEHMe5E6%3DmKr2kj%2B6vGDrGktSTg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


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 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/2e994baf-496e-4e9d-85f7-644c6a2fce08%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


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.
 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/2e994baf-496e-4e9d-85f7-644c6a2fce08%40googlegroups.com
 .
 For more options, visit https://groups.google.com/d/optout.


-- 
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/CAAb4jGkhPCCRvw0iJgZ1k%2B0hsxCC9iy8Z6FPhgcAEaCBWWkh6A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


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. franz@gmail.com javascript::

 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 sympy+un...@googlegroups.com javascript:.
 To post to this group, send email to sy...@googlegroups.com javascript:
 .
 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/2e994baf-496e-4e9d-85f7-644c6a2fce08%40googlegroups.com
 .
 For more options, visit https://groups.google.com/d/optout.




-- 
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/7bd93bb3-34bf-4abe-94b8-eb8b91f94123%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


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 of the expected

Abs(a)**2*Abs(b)**2 + Abs(c)**2*Abs(d)**2

It does what I want if I apply it two times, though.


On Friday, June 6, 2014 9:49:22 PM UTC+2, Andrei Berceanu wrote:

 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. franz@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.
 To unsubscribe from this group and stop receiving emails from it, send 
 an email to sympy+un...@googlegroups.com.
 To post to this group, send email to sy...@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/2e994baf-496e-4e9d-85f7-644c6a2fce08%40googlegroups.com
 .
 For more options, visit https://groups.google.com/d/optout.




-- 
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/e8234a53-25f7-491f-964c-78ffd7618165%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[sympy] modify latex representation of user-defined function

2014-06-06 Thread Andrei Berceanu
I define a function gamma with the following code:

from sympy import *
x = Symbol('x')
class gamma(Function): pass

Its latex representation is

print latex(gamma(x))

\Gamma\left(x\right)

whereas I would like it to be

\gamma\left(x\right)

i.e. lowercase instead of capital. 

How can I achieve this?



-- 
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/67ae1817-3568-42b6-970f-30ff9ecc6551%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[sympy] How can I call sympy by javascript with django / jquery or ajax

2014-06-06 Thread Peter
Hallo everybody,

I'm sorry for this question, but I'm not realy familar with Python. 
Normally I'm working with PHP and JavaScript, but for my recent project I 
have to integrate some symbolic math to a webpage.
In an Internet search, I came across Sympy. And sympy looks quite good.
My problem looks like this:

The user should enter a formula in a field.
The value of the imput field has to be the formula that should be evaluated.
The value of the field should be posted to sympy. (Am I right, that this 
will work with django oder a simple jquery call? Can anybody give me an 
example, how to do so?)
Sympy has to evaluate the posted formula.
The return value should be posted back to JavaScript.

Can anyone please tell me how I can implement this.

Thanks a lot!
Peter

-- 
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/fc323b5b-937c-4646-be5a-877601d31dae%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[sympy] Re: modify latex representation of user-defined function

2014-06-06 Thread Björn Dahlgren


On Friday, 6 June 2014 23:13:39 UTC+2, Andrei Berceanu wrote:

 I define a function gamma with the following code:

 from sympy import *
 x = Symbol('x')
 class gamma(Function): pass

 Its latex representation is

 print latex(gamma(x))

 \Gamma\left(x\right)

 whereas I would like it to be

 \gamma\left(x\right)

 i.e. lowercase instead of capital. 

 How can I achieve this?


This should do it:

class gamma(Function):
def _latex(self, printer):
return r'\gamma{\left(%s \right)}' % printer._print(self.args[0])

 

-- 
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/e4addabc-3010-4107-b270-9767ee113c5d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[sympy] Re: How can I call sympy by javascript with django / jquery or ajax

2014-06-06 Thread Björn Dahlgren


On Friday, 6 June 2014 23:18:19 UTC+2, Peter wrote:

 Hallo everybody,

 I'm sorry for this question, but I'm not realy familar with Python. 
 Normally I'm working with PHP and JavaScript, but for my recent project I 
 have to integrate some symbolic math to a webpage.
 In an Internet search, I came across Sympy. And sympy looks quite good.
 My problem looks like this:

 The user should enter a formula in a field.
 The value of the imput field has to be the formula that should be 
 evaluated.
 The value of the field should be posted to sympy. (Am I right, that this 
 will work with django oder a simple jquery call? Can anybody give me an 
 example, how to do so?)
 Sympy has to evaluate the posted formula.
 The return value should be posted back to JavaScript.

 Can anyone please tell me how I can implement this.

 Thanks a lot!
 Peter


You should look into sympy gamma.
If you just want a small hack in your django codebase I would look at:
http://docs.sympy.org/dev/modules/parsing.html

and then maybe return the results as latex and use mathjax to render it 
nicely on the client side.

 

-- 
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/e7d85f48-f576-4777-97d7-2e8b7fc538a4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [sympy] Writing Documentation for GA module

2014-06-06 Thread Alan Bromborsky
I am doing all check out of documentation locally.  The pdf file (also 
rst and html) is on my computer.  Note that I just upgraded my 
computer.  Before (Amd Phenom II, 6 cores) the Mathjax took 15 seconds 
to render.


On 06/06/2014 07:21 PM, Aaron Meurer wrote:

Did that include time downloading the pdf from the internet?

Aaron Meurer

On Fri, Jun 6, 2014 at 6:13 PM, Alan Bromborsky abro...@verizon.net wrote:

Locally on my computer (i7, 6 cores, 3.2 GHz) it took 8 seconds for the
mathjax to render.  The pdf file (928 kB) took less than a second to load
and display.
I also agree that the png display looks really bad.


On 06/06/2014 06:43 PM, Aaron Meurer wrote:

The png math looks terrible on high resolution displays, so I am -1
for that reason. It's also less accessible (e.g., screen readers
cannot do anything with it).

I don't think 15 seconds is that bad. You will wait longer than that
for a PDF to download.

Aaron Meurer

On Wed, Jun 4, 2014 at 8:49 AM, Alan Bromborsky abro...@verizon.net
wrote:

On 06/03/2014 11:40 AM, Ondřej Čertík wrote:

On Tue, Jun 3, 2014 at 6:16 AM, Alan Bromborsky abro...@verizon.net
wrote:

I am currently rewriting the documentation for the GA module and
because
of
the equations (more that even before) it has gotten ridiculous in
python-sphinx due to the speed of mathjax (it takes about 15 seconds to
render the documentation in Firefox ever time I open it) and the
limitations
of using latex macros in sphinx with mathjax rendering.

Indeed, I don't use mathjax for my notes in Sphinx precisely for the
reason you mentioned.

However, it is possible to use the latex png output, then it works
great. Maybe we
can switch to it with SymPy.


Would the following solution be acceptable -

Drastically simplify the sphinx documentation (basically limit it to
the
user interface for GA) and include all the explanatory math background
with
derivations and examples in a pdf document(s) (latex generated) linked
to
the sphinx documentation.

If we don't switch to the png printer in Sphinx, then I think this is
the only solution.


Question -

I know I can link pdf documents to the sphinx documents and I know I
can
further link other pdf documents to the primary pdf document if I open
the
primary pdf document in a pdf viewer like evince or acroread.  However,
if
the primary pdf document is opened in the Firefox pdf viewer (as would
be
done if the document is opened from the sphinx documentation) I cannot
link
to further pdf documents. Does anyone understand this problem and know
of
an
solution to it?

I think it's because Firefox is using its own pdf viewer, so perhaps
it doesn't support it.

Aaron, is there any argument against switching to the png based math?
That fixes all these problems.

Ondrej


More about link python-sphinx to pdf:

The following works (in the rst file) - :download:`GA
../LaTeX_docs/GA.pdf`
to open GA.pdf at the beginning.
The following does not work - :download:`GA
../LaTeX_docs/GA.pdf#page=12`
to open GA.pdf at page 12. However if I use - :download:`GA
../LaTeX_docs/GA.pdf`
in the rst file and then change - _downloads/GA.pdf
in the html file to - _downloads/GA.pdf#page=12
then the link does open on page 12. Do you know of anyway to code this
directly in the rst file?


--
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/538F23FE.6020307%40verizon.net.

For more options, visit https://groups.google.com/d/optout.


--
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/53924B23.9060405%40verizon.net.

For more options, visit https://groups.google.com/d/optout.


--
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/53924DE1.3050208%40verizon.net.
For more options, visit https://groups.google.com/d/optout.


[sympy] Re: How can I call sympy by javascript with django / jquery or ajax

2014-06-06 Thread Peter

Hi Björn,
In the meantime, I tried to install SympyGamma and the Google app engine, as 
it is described in the manual. Unfortunately I don't know now how I can 
check if everything works fine, because some steps were discribed for a 
localhost and not on a remote server.

Until today, I've never done a django query, could you please give an 
example, that I'm able to orient me at that example.

Many thanks for your help!
Peter


Am Freitag, 6. Juni 2014 23:56:01 UTC+2 schrieb Björn Dahlgren:



 On Friday, 6 June 2014 23:18:19 UTC+2, Peter wrote:

 Hallo everybody,

 I'm sorry for this question, but I'm not realy familar with Python. 
 Normally I'm working with PHP and JavaScript, but for my recent project I 
 have to integrate some symbolic math to a webpage.
 In an Internet search, I came across Sympy. And sympy looks quite good.
 My problem looks like this:

 The user should enter a formula in a field.
 The value of the imput field has to be the formula that should be 
 evaluated.
 The value of the field should be posted to sympy. (Am I right, that this 
 will work with django oder a simple jquery call? Can anybody give me an 
 example, how to do so?)
 Sympy has to evaluate the posted formula.
 The return value should be posted back to JavaScript.

 Can anyone please tell me how I can implement this.

 Thanks a lot!
 Peter


 You should look into sympy gamma.
 If you just want a small hack in your django codebase I would look at:
 http://docs.sympy.org/dev/modules/parsing.html

 and then maybe return the results as latex and use mathjax to render it 
 nicely on the client side.

  


-- 
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/38b6a055-ea01-4d44-88a5-aaad1c586711%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [sympy] Re: How can I call sympy by javascript with django / jquery or ajax

2014-06-06 Thread Alan Bromborsky
You might want to look at http://krum.rz.uni-mannheim.de/jas/ (Java 
Algebra System (JAS) Project)


On 06/06/2014 08:16 PM, Peter wrote:


Hi Björn,
In the meantime, 
ItriedtoinstallSympyGammaandtheGoogleappengine,asitisdescribedinthemanual. 
UnfortunatelyIdon'tknownowhowIcancheckifeverythingworks 
fine,becausesomestepswere discribed foralocalhostandnotonaremote server.


Untiltoday, I'veneverdoneadjangoquery,couldyoupleasegiveanexample,that 
I'm able to orient me at that example.


Manythanksforyourhelp!
Peter


Am Freitag, 6. Juni 2014 23:56:01 UTC+2 schrieb Björn Dahlgren:



On Friday, 6 June 2014 23:18:19 UTC+2, Peter wrote:

Hallo everybody,

I'm sorry for this question, but I'm not realy familar with
Python. Normally I'm working with PHP and JavaScript, but for
my recent project I have to integrate some symbolic math to a
webpage.
In an Internet search, I came across Sympy. And sympy looks
quite good.
My problem looks like this:

The user should enter a formula in a field.
The value of the imput field has to be the formula that should
be evaluated.
The value of the field should be posted to sympy. (Am I right,
that this will work with django oder a simple jquery call? Can
anybody give me an example, how to do so?)
Sympy has to evaluate the posted formula.
The return value should be posted back to JavaScript.

Can anyone please tell me how I can implement this.

Thanks a lot!
Peter


You should look into sympy gamma.
If you just want a small hack in your django codebase I would look at:
http://docs.sympy.org/dev/modules/parsing.html
http://docs.sympy.org/dev/modules/parsing.html

and then maybe return the results as latex and use mathjax to
render it nicely on the client side.

--
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 
mailto:sympy+unsubscr...@googlegroups.com.
To post to this group, send email to sympy@googlegroups.com 
mailto: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/38b6a055-ea01-4d44-88a5-aaad1c586711%40googlegroups.com 
https://groups.google.com/d/msgid/sympy/38b6a055-ea01-4d44-88a5-aaad1c586711%40googlegroups.com?utm_medium=emailutm_source=footer.

For more options, visit https://groups.google.com/d/optout.


--
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/53925D1A.4090803%40verizon.net.
For more options, visit https://groups.google.com/d/optout.


Re: [sympy] PRolog Equation Solving System

2014-06-06 Thread Aaron Meurer
These are all issues that Harsh should be addressing in his GSoC project.

Aaron Meurer

On Wed, Jun 4, 2014 at 6:56 AM, F. B. franz.bona...@gmail.com wrote:
 I had a look at SymPy, it looks like this:

 In [1]: solve(cos(3*x), x)
 Out[1]:
 ⎡π  π⎤
 ⎢─, ─⎥
 ⎣6  2⎦

 In [2]: solve(cos(n*x), x)
 Out[2]:
 ⎡ π   3⋅π⎤
 ⎢───, ───⎥
 ⎣2⋅n  2⋅n⎦


 It looks like the cos( ) solver just thinks that cos( ) can be zero at two
 points (pi/2 and 3*pi/2), then calls a solver to match its argument to those
 values.

 cos(3*x) == 0 misses thus 5*pi/2, which divided by 3 would give 5*pi/6, the
 missing solution.

 By the way, why did the solver implicitly assume that n != 0 ? If n == 0,
 there are no solutions to cos(n*x) == 0.

 Is there any representation for periodic infinite sets in SymPy?


 On Tuesday, June 3, 2014 6:03:26 PM UTC+2, Aaron Meurer wrote:

 It's getting stuck in the checking routine. Try check=False.

 If you do rewrite(exp), you get some solutions.

 Aaron Meurer

 On Mon, Jun 2, 2014 at 11:33 PM, Rathmann rathm...@gmail.com wrote:
  Interestingly, the problem that Richard Fateman uses to introduce his
  critique of Press (and other systems) doesn't look to be a happy one for
  Sympy.
 
  from sympy.abc import x
  from sympy import cos, solve
  solve(cos(x)+cos(3*x)+cos(5*x), x)
  At least in my (not quite up to date) tree, this gives a hang/infinite
  loop
 
  If you do the high-school level cleverness by hand, and replace it with
 
  solve(cos(3*x)*(1+2*cos(2*x)), x)
  [pi/6, pi/3, pi/2, 2*pi/3]
 
  which is better, although missing a root at 5pi/6
 
 
  On Monday, June 2, 2014 8:42:13 AM UTC-7, Aaron Meurer wrote:
 
  Ah, but the concept is sound, I think. One just needs to be more
  careful in the implementation.
 
  Given that this program was designed to solve high school algebra,
  it's not surprising that the author chose to ignore complex variables,
  for better or for worse.
 
  Aaron Meurer
 
  On Mon, Jun 2, 2014 at 6:15 AM, Richard Fateman fat...@gmail.com
  wrote:
  
  
   On Wednesday, May 28, 2014 9:16:46 PM UTC-7, Aaron Meurer wrote:
  
   How does it return invalid results? Does it not check if spurious
   solutions were introduced through multiplying both sides of an
   equation?
  
   yes.
  
   Also, if one is inclined to say that computer programs know things,
   then
   computer programs presumably are ignorant of things.
   In which case I think one would say that this program is ignorant of
   complex variables.
  
  
   --
   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+un...@googlegroups.com.
   To post to this group, send email to sy...@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/ebdacb2f-d41b-44cf-ba1d-9259ef825e1d%40googlegroups.com.
  
   For more options, visit https://groups.google.com/d/optout.
 
  --
  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+un...@googlegroups.com.
  To post to this group, send email to sy...@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/325f15d8-200a-4022-b21f-8286879941d1%40googlegroups.com.
 
  For more options, visit https://groups.google.com/d/optout.

 --
 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/fc862b0e-800c-4e5e-a2dd-a3f4151a61fa%40googlegroups.com.

 For more options, visit https://groups.google.com/d/optout.

-- 
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/CAKgW%3D6%2Bb8gz8gPrQ1-Kes1J-n5-r0GjJAkW78C-aGzn-YVNcdA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


[sympy] How can I call sympy by javascript with django / jquery or ajax

2014-06-06 Thread SAHIL SHEKHAWAT
Peter!
I dont understand what is it exactly that you want to do but still I would
like to help.
 On 7 Jun 2014 06:00, Alan Bromborsky abro...@verizon.net
javascript:_e(%7B%7D,'cvml','abro...@verizon.net'); wrote:

  In the meantime, I tried to install SympyGamma and the Google app engine,
 as it is described in the manual. Unfortunately I don't know now how I can
 check if everything works fine, because some steps were discribed for a
 localhost and not on a remote server.

 I guess you can ssh into your remote server and then just follow the steps
to install and while running server just add --host=0.0.0.0 to it to make
it available to other systems also BUT that will be a bad practice if this
project of your is big because that will still be running in dev mode. I
think the better solution of it is to upload it on  the Google App Engine
which also provides you many other options and is free for certain limit
which is enough for a small project.


 Until today, I've never done a django query, could you please give an
 example, that I'm able to orient me at that example.

 Just look at the code of SymPyGamma. If i an getting you correctly this is
all you need. In one sentence SympyGamma is awesome.

-- 
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/CA%2BOR%3Dbg6xL%2BDPxh0p0EU8aAhQ%2BjZtVxhsTLT_-xbpAoZgEgS-A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


[sympy] Re: Convert from a system of linear equations to a matrix

2014-06-06 Thread James Crist
I just answered this on gitter earlier today, but you can just take the 
jacobian of the system to get its matrix form. For example:

In [1]: from sympy import *

In [2]: a, b, c, d = symbols('a, b, c, d')

In [3]: x1, x2, x3, x4 = symbols('x1:5')

In [4]: x = Matrix([x1, x2, x3, x4])

In [5]: system = Matrix([a*x1 + b*x2 + c,
   ...: c*x1 + d*x3 + 2,
   ...: c*x3 + b*x4 + a])

In [6]: A = system.jacobian(x)

In [7]: B = A*x - system

In [8]: A
Out[8]: 
Matrix([
[a, b, 0, 0],
[c, 0, d, 0],
[0, 0, c, b]])

In [9]: B
Out[9]: 
Matrix([
[-c],
[-2],
[-a]])

In [10]: assert A*x - B == system

The functionality I'm adding for my GSoC for linearizing a system of 
equations will also be able to return these matrices in a convenient form. 
But it's not terribly difficult to solve for these arrangements using the 
current functionality.






On Thursday, June 5, 2014 4:22:52 PM UTC-5, Andrei Berceanu wrote:

 Was this implemented into sympy at any point? It could be the equivalent 
 of Mathematica's CoefficientArrays function.

 On Thursday, November 14, 2013 5:56:22 AM UTC+1, Chris Smith wrote:

 I forgot that as_independent, without the as_Add=True flag will treat 
 Muls differently. The following will be more robust:

 def eqs2matrix(eqs, syms, augment=False):
 
  s
 [x + 2*y == 4, 2*c + y/2 == 0]
  eqs2matrix(s, (x, c))
 (Matrix([
 [1, 0],
 [0, 2]]), Matrix([
 [-2*y + 4],
 [-y/2]]))
  eqs2matrix([2*c*(x+y)-4],(x, y))
 (Matrix([[2*c, 2*c]]), Matrix([[4]]))
 
 s = Matrix([si.lhs - si.rhs if isinstance(si, Equality) else si for 
 si in eqs])
 sym = syms
 j = s.jacobian(sym)
 rhs = -(s - j*Matrix(sym))
 rhs.simplify()
 if augment:
 j.col_insert(0, rhs)
 else:
 j = (j, rhs)
 return j



-- 
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/8fb2dae4-9f46-4c1b-b96f-83033278c27d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.