0
$\begingroup$

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

$\endgroup$
5
  • $\begingroup$ Piecewise[], OddQ[], and EvenQ[] should prove useful. $\endgroup$ Jun 23, 2015 at 4:18
  • $\begingroup$ Thanks. I did try that but I don't know what to write (or how to write $x^{n}$) on the left hand side. $\endgroup$
    – Jack
    Jun 23, 2015 at 4:23
  • $\begingroup$ 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 *)] $\endgroup$ Jun 23, 2015 at 4:25
  • $\begingroup$ Thanks. I will try that and let you know. $\endgroup$
    – Jack
    Jun 23, 2015 at 4:26
  • $\begingroup$ If you really mean that x=+/-1 then why not (-1)^n? $\endgroup$
    – bill s
    Jun 23, 2015 at 6:29

2 Answers 2

4
$\begingroup$

You can use Boole like this:

xn[n_Integer?Positive] := x^Boole[OddQ[n]]
$\endgroup$
1
  • 2
    $\begingroup$ Iverson saves the day once more. :) $\endgroup$ Jun 23, 2015 at 7:01
1
$\begingroup$
Clear[f]
f[n_ /; EvenQ[n] && n > 0, x_] := 1
f[n_ /; OddQ[n] && n > 0, x_] := x
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.