Ask Your Question
1

"showing" both input and output

asked 2014-09-06 22:41:45 +0200

mathochist gravatar image

updated 2023-01-09 23:59:34 +0200

tmonteil gravatar image

Is there a way to get Sage to show the "pretty" versions of both the input and the output of a calculation, instead of just the output? (sort of like the TI-89 and such)

For example, when I enter as input "integrate(x^2, x, 0, 3)" it correctly returns 9. But can I get it to return something like (the typeset version of) \int_0^3 x^2 dx = 9 ? It's obvious enough in this example what I'm asking for but some queries get complicated and seeing them as they would be printed helps make sure I'm asking Sage for what I mean to ask for.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2014-09-06 23:16:56 +0200

tmonteil gravatar image

updated 2014-09-07 01:35:15 +0200

Usually, for symbolic expressions, there is a hold parameter that prevents Sage to evaluate your input so that you can get its latex representation, for example:

sage: (x^2).add(x^2, hold=True)
x^2 + x^2
sage: latex(_)
x^{2} + x^{2}

sage: tan(pi/2, hold=True)
tan(1/2*pi)
sage: latex(_)
\tan\left(\frac{1}{2} \, \pi\right)
sage: tan(pi/2)
Infinity
sage: latex(_)
\infty

The problem here is that there is no hold parameter for integration:

sage: integral(x^2, x, 0, 3, hold=True)
TypeError: integrate() got an unexpected keyword argument 'hold'

The workaround could be to use definite_integral that supports this paramter:

sage: from sage.symbolic.integration.integral import definite_integral
sage: definite_integral(x^2, x, 0, 3, hold=True)
integrate(x^2, x, 0, 3)
sage: latex(_)
\int_{0}^{3} x^{2}\,{d x}

Once trac ticket 10035 and trac ticket 16941 (which i opened for the occasion, and needs rewiew) will be solved, you will be able to write something along the lines:

sage: def myshow(s):
....:    with hold=True:
....:        left = str(eval(s))
....:    right = str(eval(s))
....:    return left + '=' + right

sage: myshow('integral(x^2, x, 0, 3)')
\int_{0}^{3} x^{2}\,{d x} = 9
edit flag offensive delete link more

Comments

I see. So it looks like untill the 'hold' command works on all (or almost all) input, I won't quite get what I want. Thank you for the effort.

mathochist gravatar imagemathochist ( 2014-09-09 16:22:12 +0200 )edit

My computer says that with hold=True is an invalid syntax

Cyrille gravatar imageCyrille ( 2019-10-16 15:31:51 +0200 )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

Stats

Asked: 2014-09-06 22:41:45 +0200

Seen: 552 times

Last updated: Sep 07 '14