Ask Your Question

How to get sage to keep the same form as an expression from sympy?

asked 2017-08-08 18:31:19 -0500

ensaba gravatar image

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

This gives:


But when I convert it back to Sage:


The result reverts to expr1.

How can I get the call to _sage_() not do any rewrites on the sympy expression?

edit retag flag offensive close merge delete



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)
dan_fulea gravatar imagedan_fulea ( 2017-08-09 17:14:46 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2017-08-11 12:23:36 -0500

mforets gravatar image

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!

edit flag offensive delete link 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?

ensaba gravatar imageensaba ( 2017-08-16 17:09:24 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2017-08-08 18:31:19 -0500

Seen: 52 times

Last updated: Aug 11