# 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?

edit retag close merge delete

1

Is 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)

( 2017-08-10 00:14:46 +0100 )edit

Sort by » oldest newest most voted

in 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!

more

If 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?

( 2017-08-17 00:09:24 +0100 )edit