# How to define $x^n$ where $x=\pm 1$ and $n\in\mathbb{Z}^{+}$?

I do not know how I can define $x^{n}$ in Mathematica, where $x=\pm 1$ and $n=1,2,3,\ldots$.

For even values of $n$ we simply have that $x^{n}=1$. While for odd values of $n$ we have that $x^{n}=x$. That is, $${x^n} = \left\{ {\begin{array}{*{20}{l}} 1&,&{{\rm{if }}\;n\;{\rm{ is\; even}}}\\ x&,&{{\rm{if }}\;n\;{\rm{ is\; odd}}} \end{array}} \right.$$

Thank you, Jack

• Piecewise[], OddQ[], and EvenQ[] should prove useful. Jun 23, 2015 at 4:18
• Thanks. I did try that but I don't know what to write (or how to write $x^{n}$) on the left hand side.
– Jack
Jun 23, 2015 at 4:23
• Does it really have to be a superscript (which is used for exponentation by default)? You can do something like xn[x : (-1 | 1), n_Integer?Positive] := Piecewise[(* stuff *)] Jun 23, 2015 at 4:25
• Thanks. I will try that and let you know.
– Jack
Jun 23, 2015 at 4:26
• If you really mean that x=+/-1 then why not (-1)^n? Jun 23, 2015 at 6:29

You can use Boole like this:
xn[n_Integer?Positive] := x^Boole[OddQ[n]]

Clear[f]