Ask Your Question

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

asked 2016-10-15 12:33:36 -0600

Jason021 gravatar image

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


edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2016-10-17 14:31:59 -0600

mforets gravatar image

updated 2016-10-17 14:36:07 -0600

This is an idea: with

latex_formula = latex(g); latex_formula

you get x \ {\mapsto}\ \sin\left(x\right)^{5}.


new_latex_formula = LatexExpr(latex_formula.replace('\\sin\\left(x\\right)^{5}', '\\sin^{5}\\left(x\\right)'))

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.

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: 2016-10-15 12:33:36 -0600

Seen: 128 times

Last updated: Oct 17 '16