Ask Your Question
2

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

asked 2016-10-15 19:33:36 +0100

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: 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 flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2016-10-17 21:31:59 +0100

mforets gravatar image

updated 2016-10-17 21:36:07 +0100

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.

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

Stats

Asked: 2016-10-15 19:33:36 +0100

Seen: 720 times

Last updated: Oct 17 '16