Ask Your Question

Best way to use mathics inside sagemath. Latex issues

asked 2022-05-31 12:23:01 +0100

Nasser gravatar image

updated 2022-05-31 12:36:37 +0100

I just started to learn how to use mathics from sagemath. I am not sure if I am doing it correctly. I just need to use mathics for integration.

Sometimes mathics return result from integration which I need to obtain its Latex. but it seems sagemath command does not know what to do in some cases and return result which does not compile. Here is an example

│ SageMath version 9.6, Release Date: 2022-05-15                     │
│ Using Python 3.10.4. Type "help()" for help.                       │
sage: from sage.interfaces.mathics import mathics
sage: res=mathics('Integrate[Sin[x]/(3 + Cos[x])^2,x]')

sage: res
ConditionalExpression[3 + Cos[x], {3 + Cos[x] != 0}]

sage: latex(res)

Is there a better way to handle such cases? Am I doing something wrong here?

Here is another example

sage: res=mathics('Integrate[x^n*Log[a*x],x]')

sage: res
Piecewise[{{(-1 + n Log[a x] + Log[a x]) x ^ (1 + n) / (1 + 2 n + n ^ 2), n != -1}}, Log[a x] ^ 2 / 2]

sage: latex(res)
}-1\right\}\right\},\frac{\text{Log}\left[a x\right]^2}{2}\right]

Can I add a code to check on result before calling latex on it? This is all done in a script which runs over 10's of thousands of integrals. So need a way to automate this checking in code. Not by looking at output and deciding what to do.

I found the following workaround which I am now using

sage: res=mathics('Integrate[x^n*Log[a*x],x]')
sage: the_latex=mathics('TeXForm['+str(res)+']')

sage: the_latex
\text{Piecewise}\left[\left\{\left\{\frac{\left(-1+n \text{Log}\left[a x\right]+\text{Log}\left[a x\right]\right) x^{1+n}}{1+2 n+n^2},n\text{!=}-1\right\}\right\},\frac{\text{Log}\left[a x\right]^2}{2}\right]

I thought sagemath latex() command will know how to do this directly. Will sage latex() fully support mathics output conversion in the future?

edit retag flag offensive close merge delete


First example, Mathics errs :

sage: mathematica.Integrate(sin(x)/(3+cos(x))^2, x).sage()
1/(cos(x) + 3)
sage: integrate(sin(x)/(3+cos(x))^2, x)
1/(cos(x) + 3)
sage: integrate(sin(x)/(3+cos(x))^2, x, algorithm="maxima")
1/(cos(x) + 3)
sage: integrate(sin(x)/(3+cos(x))^2, x, algorithm="sympy")
1/(cos(x) + 3)
sage: integrate(sin(x)/(3+cos(x))^2, x, algorithm="giac")
1/(cos(x) + 3)
sage: integrate(sin(x)/(3+cos(x))^2, x, algorithm="fricas")
1/(cos(x) + 3)
sage: integrate(sin(x)/(3+cos(x))^2, x, algorithm="mathematica_free")
1/(cos(x) + 3)
sage: mathematica.Integrate(sin(x)/(3+cos(x))^2, x).sage()
1/(cos(x) + 3)

but :

sage: mathics.Integrate(sin(x)/(3+cos(x))^2, x)
ConditionalExpression[3 + Cos[x], {3 + Cos[x] != 0}]
Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2022-05-31 14:57:34 +0100 )edit

1 Answer

Sort by » oldest newest most voted

answered 2022-05-31 15:21:11 +0100

Emmanuel Charpentier gravatar image

You may convert your mathics to a Sage expression and latex that. Using your second example :

sage: latex(mathics.Integrate(x^p*log(a*x), x)._sage_())
\begin{cases}{\frac{{\left(p \log\left(a x\right) + \log\left(a x\right) - 1\right)} x^{p + 1}}{p^{2} + 2 \, p + 1}} & {p \neq \left(-1\right)}\\{\frac{1}{2} \, \log\left(a x\right)^{2}} & {1}\end{cases}

which's Mathjax can't translate...


edit flag offensive delete link more


Your method worked on the second example but on the first example it gives strange result: Compare sage: var('x p a') sage: latex(mathics.Integrate(sin(x)/(3+cos(x))^2, x)._sage_()) which gives this result \text{\texttt{System ConditionalExpression[System Plus[3,{ }System Cos[Global x]],{ }System List[System Unequal[System Plus[3,{ }System Cos[Global x]],{ }0]]]}} But using TeXForm gives sage: mathics('TeXForm[Integrate[Sin[x]/(3+Cos[x])^2, x]]') gives \text{ConditionalExpression}\left[3+\text{Cos}\left[x\right],\left\{3+\text{Cos}\left[x\right]\text{!=}0\right\}\right] So I think for now, will use TeXForm for everything when using mathics in sagemath as it seems to produce better latex for all examples.

Nasser gravatar imageNasser ( 2022-05-31 23:24:05 +0100 )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: 2022-05-31 12:23:01 +0100

Seen: 231 times

Last updated: May 31 '22