Ask Your Question

How can I print equations just like latex?

asked 2015-02-13 01:34:10 -0600

Nownuri gravatar image

updated 2015-02-13 02:11:34 -0600

Now I'm testing several equations and solutions with Sage. After testing the solutions, I'd present it to the group members. But I met a problem to print the equations.

For example, when I write the code.

sage: z(x)=6/(5*pi*50*(x^2+25))*exp((-x+sqrt(x^2+25))/50)
sage: view (z)

Then the sage doesn't show the equation that is exactly same with the code, but it just shows simplified form. Could anyone let me know how to show the original equation?

And it would be also appreciated, if you tell me how to modify the simplifying mechanism. I'd love to write equations that don't contain any large denominators with many exponential terms.

thank you.

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2015-02-17 00:45:29 -0600

Thorsten gravatar image

There is the hold option, which might can help:


which evaluates to $x \mapsto x^2x$ For an easier typing one could use Infix opertors:

 def hold_mult(a,b):
     return a.mul(b,hold=true)
 h = infix_operator('multiply')(hold_mult)

and then use x^2 *h* x.

However in your case if you need just the initial equation to display it might be the easiest just to print it as a string. If you need this functionality more often, an (extendet version of a) function like this could be helpful:

def paranthese_match(prefix,str):
   matches = []
   while (str.find(prefix) != -1):
       p = -1
       for li in [str.find(prefix)+len(prefix)..len(str)-1]:
           if str[li] == '(':
             p -=1
           if str[li] == ')':
             p += 1
           if p==0:
       str = str[li:]
   return matches
def print_expression(str):
   str = str.replace('*',' \cdot ')
   str = str.replace('pi',' \pi ')
   for sr in paranthese_match('sqrt(',str):
       str = str.replace('sqrt('+sr+")",'\sqrt{'+sr+'}')
   for sr in paranthese_match('exp(',str):
       str = str.replace('exp('+sr+")",'e^{'+sr+'}')

Then print_expresion("6/(5*pi*h*50*h*(x^2+25))*h*exp((-x+sqrt(x^2+25))/50)") leads to $6/(5 \cdot \pi \cdot h \cdot 50 \cdot h \cdot (x^2+25)) \cdot h \cdot e^{(-x+\sqrt{x^2+25})/50}$

edit flag offensive delete link more

answered 2015-02-17 01:02:37 -0600

updated 2015-02-17 15:32:24 -0600

I was looking for the same thing and found these comments on the issue.

This works with a _few_ functions, but not all.

This is probably the best answer on why it does this simplification (and why there's no way to stop it)

As far as the simplification mechanisms, you can look at this page. There are a number of them spread throughout that highlight different means:

If you're looking to just reproduce the same text as your input, unfortunately I have found no better way than writing it out in LaTeX as a initial condition step so Sage doesn't mangle the form. This is a quick latex renderer that I modified from a this post on ask.sagemath

def latexrender(s):
        pic = text("$" + s.replace('$','\$') + "$",(0,0),axes=False,figsize=1,color='black',fontsize=20)
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: 2015-02-13 01:34:10 -0600

Seen: 816 times

Last updated: Feb 17 '15