ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 28 Apr 2016 08:54:42 +0200Slow conversion of symbolic expression to sympyhttps://ask.sagemath.org/question/33140/slow-conversion-of-symbolic-expression-to-sympy/I need to convert symbolic expressions to sympy for code generation using codegen. The symbolic expressions are very long and right now it could take hours to finish the sage to sympy conversion. Any idea to speed things up? Thanks. Tue, 19 Apr 2016 03:39:19 +0200https://ask.sagemath.org/question/33140/slow-conversion-of-symbolic-expression-to-sympy/Comment by B r u n o for <p>I need to convert symbolic expressions to sympy for code generation using codegen. The symbolic expressions are very long and right now it could take hours to finish the sage to sympy conversion. Any idea to speed things up? Thanks. </p>
https://ask.sagemath.org/question/33140/slow-conversion-of-symbolic-expression-to-sympy/?comment=33141#post-id-33141Could you post an example of what you are trying? It is very hard to come up with advices without knowing what exactly you are doing.Tue, 19 Apr 2016 09:48:10 +0200https://ask.sagemath.org/question/33140/slow-conversion-of-symbolic-expression-to-sympy/?comment=33141#post-id-33141Comment by Liang for <p>I need to convert symbolic expressions to sympy for code generation using codegen. The symbolic expressions are very long and right now it could take hours to finish the sage to sympy conversion. Any idea to speed things up? Thanks. </p>
https://ask.sagemath.org/question/33140/slow-conversion-of-symbolic-expression-to-sympy/?comment=33153#post-id-33153Here's a short example. It takes about 40 seconds to finish the conversion.
C=matrix(SR, 3, 3, var('C11, C12, C13, C21, C22, C23, C31, C32, C33'))
F=matrix(SR, 3, 3, var('F11, F12, F13, F21, F22, F23, F31, F32, F33'))
Cg=matrix(SR, 3, 3, var('Cg11, Cg12, Cg13, Cg21, Cg22, Cg23, Cg31, Cg32, Cg33'))
Fg=matrix(SR, 3, 3, var('Fg11, Fg12, Fg13, Fg21, Fg22, Fg23, Fg31, Fg32, Fg33'))
Fg_inv=Fg.inverse()
Ce=Fg_inv.T*C*Fg_inv
Fe=F*Fg_inv
Cedet=Ce.det()
Je=sqrt(Cedet)
W=(Je-1)^2
S_PK=Matrix(SR, 3,3, jacobian(W, C.list()).list())
S=Fe*S_PK*Fe.T*Je**(-1)
CF=F.T*F
out1=S[0,0].substitute([(C.list())[i]==(CF.list())[i] for i in xrange(9)])._sympy_()
enter code hereWed, 20 Apr 2016 07:07:37 +0200https://ask.sagemath.org/question/33140/slow-conversion-of-symbolic-expression-to-sympy/?comment=33153#post-id-33153Answer by B r u n o for <p>I need to convert symbolic expressions to sympy for code generation using codegen. The symbolic expressions are very long and right now it could take hours to finish the sage to sympy conversion. Any idea to speed things up? Thanks. </p>
https://ask.sagemath.org/question/33140/slow-conversion-of-symbolic-expression-to-sympy/?answer=33223#post-id-33223As I understand, the slowness is not related to the conversion to Sympy. The point is that your expressions become huge, and that's why Sage becomes slow to perform operations (be it conversion or anything else). The solution is to simplify your expression while doing the computations. Below is an example of such simplifications you can add (the complete computation takes 1.27s on my laptop). Note that there may be better strategies (apply `simplify_rational` to more, or on the contrary less, expressions to optimize computation time). At least, I obtain reasonable computation times:
C = matrix(SR, 3, 3, var('C11, C12, C13, C21, C22, C23, C31, C32, C33'))
F = matrix(SR, 3, 3, var('F11, F12, F13, F21, F22, F23, F31, F32, F33'))
Cg = matrix(SR, 3, 3, var('Cg11, Cg12, Cg13, Cg21, Cg22, Cg23, Cg31, Cg32, Cg33'))
Fg = matrix(SR, 3, 3, var('Fg11, Fg12, Fg13, Fg21, Fg22, Fg23, Fg31, Fg32, Fg33'))
Fg_inv = Fg.inverse().simplify_rational()
Ce = (Fg_inv.T*C*Fg_inv).simplify_rational()
Fe = (F*Fg_inv).simplify_rational()
Cedet = Ce.det().simplify_rational()
Je = sqrt(Cedet)
W = ((Je-1)^2).expand().simplify_rational()
S_PK = matrix(SR, 3,3, jacobian(W, C.list()).list()).simplify_rational()
S = Fe*S_PK*Fe.T*Je**(-1)
CF = F.T*F
S00 = S[0,0].substitute([(C.list())[i]==(CF.list())[i] for i in xrange(9)])
out1 = S00._sympy_()
Mon, 25 Apr 2016 12:04:37 +0200https://ask.sagemath.org/question/33140/slow-conversion-of-symbolic-expression-to-sympy/?answer=33223#post-id-33223Comment by Liang for <p>As I understand, the slowness is not related to the conversion to Sympy. The point is that your expressions become huge, and that's why Sage becomes slow to perform operations (be it conversion or anything else). The solution is to simplify your expression while doing the computations. Below is an example of such simplifications you can add (the complete computation takes 1.27s on my laptop). Note that there may be better strategies (apply <code>simplify_rational</code> to more, or on the contrary less, expressions to optimize computation time). At least, I obtain reasonable computation times:</p>
<pre><code>C = matrix(SR, 3, 3, var('C11, C12, C13, C21, C22, C23, C31, C32, C33'))
F = matrix(SR, 3, 3, var('F11, F12, F13, F21, F22, F23, F31, F32, F33'))
Cg = matrix(SR, 3, 3, var('Cg11, Cg12, Cg13, Cg21, Cg22, Cg23, Cg31, Cg32, Cg33'))
Fg = matrix(SR, 3, 3, var('Fg11, Fg12, Fg13, Fg21, Fg22, Fg23, Fg31, Fg32, Fg33'))
Fg_inv = Fg.inverse().simplify_rational()
Ce = (Fg_inv.T*C*Fg_inv).simplify_rational()
Fe = (F*Fg_inv).simplify_rational()
Cedet = Ce.det().simplify_rational()
Je = sqrt(Cedet)
W = ((Je-1)^2).expand().simplify_rational()
S_PK = matrix(SR, 3,3, jacobian(W, C.list()).list()).simplify_rational()
S = Fe*S_PK*Fe.T*Je**(-1)
CF = F.T*F
S00 = S[0,0].substitute([(C.list())[i]==(CF.list())[i] for i in xrange(9)])
out1 = S00._sympy_()
</code></pre>
https://ask.sagemath.org/question/33140/slow-conversion-of-symbolic-expression-to-sympy/?comment=33251#post-id-33251This works great. Thank you!Thu, 28 Apr 2016 08:54:42 +0200https://ask.sagemath.org/question/33140/slow-conversion-of-symbolic-expression-to-sympy/?comment=33251#post-id-33251