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, 17 Aug 2017 00:09:24 +0200How to get sage to keep the same form as an expression from sympy?https://ask.sagemath.org/question/38492/how-to-get-sage-to-keep-the-same-form-as-an-expression-from-sympy/ I have this expression:
$$-\frac{2}{3} t_{1} + \frac{2}{3} y_{1}$$
and would like to rewrite it as follows:
$$\frac{2}{3} (-t_1 + y_1)$$
Using sympy, I can get something close:
import sympy as sp
var('y1 t1')
expr1 = -2/3*t1 + 2/3*y1
sp.factor(sp.sympify(expr1))
This gives:
𝟸*(⎯𝚝𝟷+𝚢𝟷)/𝟹
But when I convert it back to Sage:
sp.factor(sp.sympify(expr1))._sage_()
The result reverts to `expr1`.
How can I get the call to `_sage_()` not do any rewrites on the sympy expression?Wed, 09 Aug 2017 01:31:19 +0200https://ask.sagemath.org/question/38492/how-to-get-sage-to-keep-the-same-form-as-an-expression-from-sympy/Comment by dan_fulea for <p>I have this expression:</p>
<p>$$-\frac{2}{3} t_{1} + \frac{2}{3} y_{1}$$</p>
<p>and would like to rewrite it as follows:</p>
<p>$$\frac{2}{3} (-t_1 + y_1)$$</p>
<p>Using sympy, I can get something close:</p>
<pre><code>import sympy as sp
var('y1 t1')
expr1 = -2/3*t1 + 2/3*y1
sp.factor(sp.sympify(expr1))
</code></pre>
<p>This gives:</p>
<pre><code>𝟸*(⎯𝚝𝟷+𝚢𝟷)/𝟹
</code></pre>
<p>But when I convert it back to Sage:</p>
<pre><code>sp.factor(sp.sympify(expr1))._sage_()
</code></pre>
<p>The result reverts to <code>expr1</code>.</p>
<p>How can I get the call to <code>_sage_()</code> not do any rewrites on the sympy expression?</p>
https://ask.sagemath.org/question/38492/how-to-get-sage-to-keep-the-same-form-as-an-expression-from-sympy/?comment=38499#post-id-38499Is the following (purely) algebraic framework ok?
sage: R.<y,t> = PolynomialRing(QQ)
sage: E = -2/3*t +2/3*y
sage: E
2/3*y - 2/3*t
sage: E.factor()
(2/3) * (y - t)Thu, 10 Aug 2017 00:14:46 +0200https://ask.sagemath.org/question/38492/how-to-get-sage-to-keep-the-same-form-as-an-expression-from-sympy/?comment=38499#post-id-38499Answer by mforets for <p>I have this expression:</p>
<p>$$-\frac{2}{3} t_{1} + \frac{2}{3} y_{1}$$</p>
<p>and would like to rewrite it as follows:</p>
<p>$$\frac{2}{3} (-t_1 + y_1)$$</p>
<p>Using sympy, I can get something close:</p>
<pre><code>import sympy as sp
var('y1 t1')
expr1 = -2/3*t1 + 2/3*y1
sp.factor(sp.sympify(expr1))
</code></pre>
<p>This gives:</p>
<pre><code>𝟸*(⎯𝚝𝟷+𝚢𝟷)/𝟹
</code></pre>
<p>But when I convert it back to Sage:</p>
<pre><code>sp.factor(sp.sympify(expr1))._sage_()
</code></pre>
<p>The result reverts to <code>expr1</code>.</p>
<p>How can I get the call to <code>_sage_()</code> not do any rewrites on the sympy expression?</p>
https://ask.sagemath.org/question/38492/how-to-get-sage-to-keep-the-same-form-as-an-expression-from-sympy/?answer=38507#post-id-38507in the symbolic ring you can do:
sage: var('y1, t1');
sage: (y1-t1).mul(2/3, hold=true)
2/3*(-t1 + y1)
i'm afraid there's no easy out-of-the-box way to tune the exact order in which the expressions are displayed. of course, sage provides all functions needed to get the information, such as `variables()`, `coefficients()`, and so on.
moreover, it becomes less clear what would such a function do. what if the expression involves more than two terms? lexicographic order is provided in a polynomial ring, an answer which has been given elsewhere by dan_fulea.
hope that helps!Fri, 11 Aug 2017 19:23:36 +0200https://ask.sagemath.org/question/38492/how-to-get-sage-to-keep-the-same-form-as-an-expression-from-sympy/?answer=38507#post-id-38507Comment by ensaba for <p>in the symbolic ring you can do:</p>
<pre><code>sage: var('y1, t1');
sage: (y1-t1).mul(2/3, hold=true)
2/3*(-t1 + y1)
</code></pre>
<p>i'm afraid there's no easy out-of-the-box way to tune the exact order in which the expressions are displayed. of course, sage provides all functions needed to get the information, such as <code>variables()</code>, <code>coefficients()</code>, and so on. </p>
<p>moreover, it becomes less clear what would such a function do. what if the expression involves more than two terms? lexicographic order is provided in a polynomial ring, an answer which has been given elsewhere by dan_fulea.</p>
<p>hope that helps!</p>
https://ask.sagemath.org/question/38492/how-to-get-sage-to-keep-the-same-form-as-an-expression-from-sympy/?comment=38548#post-id-38548If I type `(y1-t1).mul(2/3, hold=true)` it means that I already know the factorized form of the expression I want to factor. What if I don't know what the factorized form of the expression that I want to factor will look like?Thu, 17 Aug 2017 00:09:24 +0200https://ask.sagemath.org/question/38492/how-to-get-sage-to-keep-the-same-form-as-an-expression-from-sympy/?comment=38548#post-id-38548