# Converting sin(x)^n to sin^n(x)

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/347... 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.

edit retag close merge delete

Sort by ยป oldest newest most voted

This 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.

more