Ask Your Question

Typesetting Simple Student Worksheet Question & need to defer computation

asked 2014-10-26 22:02:29 -0500

userX gravatar image

updated 2014-10-27 00:23:31 -0500

I have


the command print latex(eq1) simplifies the equation to $$\frac{2}{5} x + 4 = -\frac{15}{7} x + \frac{18}{7}$$

is there something like latex(eq1.hold()) that would produce


edit: I must apologize in advanced as my code may seem infantile but I am very new to this and trying to learn. That said, I was able to get something going but it seems to me there must be a million better ways to do this. Can you point me in the right direction to generalize this process and improve this code. Keep in mind the point of this is to write a student worksheet with problem sets and solutions. can be seen here link text

ri = lambda x,y:  floor(round(random()*(y-x)+x)); #random integer from x to y
ls = [ri(-5,-1),ri(1,3),ri(2,15),ri(2,15)];
rs = [ri(2,10),ri(-3,-1),ri(-6,-2),ri(2,15)];

def texLinearA(t): return "$\\frac{"+str(latex(t[0]))+"\\left("+str(latex(t[1]))+"+"+str(latex(t[2]))+"x\\right)}{"+str(latex(t[3]))+"}$";

def sageLinearA(t): x=var("x"); return  t[0]*(t[1]+t[2]*x)/t[3];

eq1 = texLinearA(ls)+"="+texLinearA(rs);
eq2 = sageLinearA(ls)==sageLinearA(rs);
eq3 = eq2*lcm(ls[3],rs[3]);

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2014-10-27 07:37:02 -0500

vdelecroix gravatar image


The nearest I am able to do is

sage:  sage: x.add(10,hold=True).mul(2,hold=True).mul(1/5,hold=True)
1/5*(2*(x + 10))

The thing is that there is nothing like division with symoblic expression (but you can take inverses and use multiplication). It can be seen on

sage: ((x-1)/(x+1)).operands()
[1/(x + 1), x - 1]
sage: ((x-1)/(x+1)).operator()
<built-in function mul>

And there is a problem with the argument hold since it sometimes reorder the terms (i.e. symbolic expressions always assume that + and * are commutative)

sage: var('y')
sage: x.mul(y,hold=True).operands()
[x, y]
sage: y.mul(x,hold=True).operands()
[x, y]

So an expression such as (x+3) / 5 does not exist in Sage as it automatically put 1/5 as the second term

sage: (x+3).mul(1/5, hold=True).operands()
[x + 3, 1/5]
sage: SR(1/5).mul(x+3, hold=True).operands()
[x + 3, 1/5]

Good luck.


edit flag offensive delete link more

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: 2014-10-26 22:02:29 -0500

Seen: 171 times

Last updated: Oct 27 '14