ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 17 Oct 2016 14:31:59 -0500Converting sin(x)^n to sin^n(x)https://ask.sagemath.org/question/35153/converting-sinxn-to-sinnx/Hello all!
I am trying to figure out how to change the sage output when it has certain trig functions to a power. For example, if I do the following;
f(x) = sin(x)
g(x) = f(x)^5
I get out
g(x) = sin(x)^5
This can be a bit ambiguous, as someone not paying attention might read that as sin(x^5). Instead I would like to make it output;
g(x) = sin^5(x)
I have tried using wildcards as was suggested to me in another post here: https://ask.sagemath.org/question/34767/replacing-mathematical-functions-of-expressions-with-different-mathematical-functions-of-the-same-expression/ But it doesn't seem to like that (probably because it thinks I'm interrupting a math call function and it's getting syntax errors, which I expected when I tried it originally).
Is there any nice way of reformatting the output? I suspect wildcards would probably be capable if I were more adept at them? I would like it specifically for the basic trig functions. I don't mind if I need to write a "simplify" function to run on an expression to convert it (even if it converts everything to a string), but I can't seem to get anything to work correctly.
Thanks.Sat, 15 Oct 2016 12:33:36 -0500https://ask.sagemath.org/question/35153/converting-sinxn-to-sinnx/Answer by mforets for <p>Hello all!</p>
<p>I am trying to figure out how to change the sage output when it has certain trig functions to a power. For example, if I do the following;</p>
<pre><code>f(x) = sin(x)
g(x) = f(x)^5
</code></pre>
<p>I get out </p>
<pre><code>g(x) = sin(x)^5
</code></pre>
<p>This can be a bit ambiguous, as someone not paying attention might read that as sin(x^5). Instead I would like to make it output;</p>
<pre><code>g(x) = sin^5(x)
</code></pre>
<p>I have tried using wildcards as was suggested to me in another post here: <a href="https://ask.sagemath.org/question/34767/replacing-mathematical-functions-of-expressions-with-different-mathematical-functions-of-the-same-expression/">https://ask.sagemath.org/question/347...</a> But it doesn't seem to like that (probably because it thinks I'm interrupting a math call function and it's getting syntax errors, which I expected when I tried it originally).</p>
<p>Is there any nice way of reformatting the output? I suspect wildcards would probably be capable if I were more adept at them? I would like it specifically for the basic trig functions. I don't mind if I need to write a "simplify" function to run on an expression to convert it (even if it converts everything to a string), but I can't seem to get anything to work correctly.</p>
<p>Thanks.</p>
https://ask.sagemath.org/question/35153/converting-sinxn-to-sinnx/?answer=35171#post-id-35171This is an idea: with
latex_formula = latex(g); latex_formula
you get ```x \ {\mapsto}\ \sin\left(x\right)^{5}```.
Doing
new_latex_formula = LatexExpr(latex_formula.replace('\\sin\\left(x\\right)^{5}', '\\sin^{5}\\left(x\\right)'))
pretty_print(new_latex_formula)
returns the display you want. Further string manipulations should do the job in general. It may be useful to note that ```dir(sage.functions.trig)``` returns a list of strings with all trig functions (and other things).
Also relevant: [Sage, LaTeX and Friends](http://doc.sagemath.org/html/en/tutorial/latex.html).Mon, 17 Oct 2016 14:31:59 -0500https://ask.sagemath.org/question/35153/converting-sinxn-to-sinnx/?answer=35171#post-id-35171