> How can one tell Mathematica to simplify Sin[n Pi]=0 or
> Cos[n Pi]=(-1)^n and similar kind of stuff.
That's a good question. Mathematica normally can't make assumptions like "n
is an integer)", and I'm not sure how to do this easily. Perhaps try the
following... say you want to simplify an expression "expr" that contains the
constructs you describe above. To get the simplifications you desire, simply
execute:
expr /. { Sin[n Pi] -> (0), Cos[n Pi] -> ( (-1)^n ) }
> Also, how does one tell it "A" & "B" are matrices so it doesn't
> commute them (AB.not equal.BA).
Mathematica won't commute things that can't be commuted. I think you might
specify that you want Matrix multiplication and not plain (component-wise)
multiplication.
A . B matrix multiply (ought not to commute)
A * B component multiply (may commute, because that's ok)
A + B addition (may commute...)
--
Rex A. Dieter rdi...@math.unl.edu (NeXT/MIME)
Research Associate Voice: (402)472-9747
Department of Mathematics and Statistics FAX: (402)472-8466
University of Nebraska - Lincoln http://www.math.unl.edu/~rdieter/
One can easily extend the rules in Mathematica. For example, if 'n' is
a free variable you want Mathematica to treat as an integer, you can
try:
(1) Unprotect[IntegerQ];
(2) IntegerQ[n] := True
(3) Protect[IntegerQ];
Notice that there is no underscore after the n in line (2). Unfortunately
this won't get Sin to evaluate Sin[n Pi]. You can work around that
problem as follows:
(4) Unprotect[Sin, Cos];
(5) Sin[x_ Pi] /; IntegerQ[x] := 0
(6) Cos[x_ Pi] /; IntegerQ[x] := (-1)^x
(7) Protect[Sin, Cos];
This will give you the result you need for Sin[n Pi] and Cos[n Pi]
but you'll be disappointed by Sin[2 n Pi] and Cos[2 n Pi]. But you
can work around this by defining integers to be closed under addition and
multiplication and additive inverses:
(8) Unprotect[IntegerQ];
(9) IntegerQ[-x_] := IntegerQ[x]
(10) IntegerQ[x_+y_] /; IntegerQ[x] && IntegerQ[y] := True
(11) IntegerQ[x_ y_] /; IntegerQ[x] && IntegerQ[y] := True
(12) Protect[IntegerQ];
(Note) The following would be INCORRECT:
(10') IntegerQ[x_+y_] := IntegerQ[x] && IntegerQ[y]
as can be seen from the example IntegerQ[1/2 + 1/2]
If you lose track of rule changes, say for IntegerQ, type
(13) ?? IntegerQ
In addition to displaying the usage information for IntegerQ, any
user-defined rules are displayed.
Eric
Below is a cleaned up version. Sorry for any inconvenience.
I have left out the output.
Bob Hanlon
_______________
(* Extend definitions of standard functions IntegerQ, EvenQ, OddQ,
Positive, Negative, NonNegative *)
Unprotect[ IntegerQ, EvenQ, OddQ, Positive, Negative, NonNegative ];
Clear[ IntegerQ, EvenQ, OddQ, Positive, Negative, NonNegative ];
IntegerQ[ m_?IntegerQ + n_?IntegerQ ] = True;
IntegerQ[ m_?IntegerQ * n_?IntegerQ ] = True;
IntegerQ[ m_?IntegerQ ^ n_Integer?NonNegative ] = True;
IntegerQ[ x_ ^ (-n_) ] := IntegerQ[ (1/x)^n ];
EvenQ[ x_?EvenQ + y_?EvenQ ] = True; (* 2m+2n = 2(m+n) *)
EvenQ[ x_?OddQ + y_?OddQ ] = True; (* (2m+1)+(2n+1) = 2(m+n+1) *)
EvenQ[ x_?EvenQ * y_?IntegerQ ] = True; (* 2m n = 2(m n) *)
EvenQ[ x_?EvenQ ^ n_Integer?Positive ] = True;
OddQ[ x_?OddQ + y_?EvenQ ] = True; (* (2m+1) + 2n = 2(m+n)+1 *)
OddQ[ x_?OddQ * y_?OddQ ] = True; (* (2m+1)(2n+1) = 2(2m*n+m+n)+1 *)
OddQ[ x_?OddQ ^ n_Integer?NonNegative ] = True;
NonNegative[ x_?NonNegative + y_?NonNegative ] = True;
NonNegative[ x_?NonNegative * y_?NonNegative ] = True;
NonNegative[ x_?NonNegative ^ y_Integer?Positive ] = True;
Positive[ x_?Positive + y_?Positive ] = True;
Positive[ x_?Positive * y_?Positive ] = True;
Positive[ x_?Negative * y_?Negative ] = True;
Positive[ x_?Positive ^ n_Integer ] = True;
Positive[ x_?Negative ^ n_?EvenQ ] = True;
Negative[ x_?Negative + y_?Negative ] = True;
Negative[ x_?Negative * y_?Positive ] = True;
Negative[ x_?Negative ^ n_?OddQ ] = True;
Protect[ IntegerQ, EvenQ, OddQ, Positive, Negative, NonNegative ];
(* Define setPos, setNeg, setNonNeg, setEven, setOdd *)
Clear[ setPos, setNeg, setNonNeg, setEven, setOdd ];
setPos::usage = "setPos[varSeq] sets Positive and NonNegative to
True, and Negative to False for symbols given in varSeq.";
setNeg::usage = "setNeg[varSeq] sets Negative to True, and Positive
and NonNegative to False for symbols given in varSeq.";
setNonNeg::usage = "setNonNeg[varSeq] sets NonNegative to True,
and Negative to False for symbols given in varSeq.";
setInteger::usage = "setInteger[varSeq] sets IntegerQ to True
for symbols given in varSeq.";
setEven::usage = "setEven[varSeq] sets IntegerQ and EvenQ to
True, and OddQ to False for symbols given in varSeq.";
setOdd::usage = "setOdd[varSeq] sets IntegerQ and OddQ to True,
and EvenQ to False for symbols given in varSeq.";
setPos[varSeq__Symbol] := Module[ { varList = {varSeq} },
(#/: Positive[#] = True )& /@ varList;
(#/: NonNegative[#] = True )& /@ varList;
(#/: Negative[#] = False)& /@ varList ];
setNeg[varSeq__Symbol] := Module[ { varList = {varSeq} },
(#/: Negative[#] = True )& /@ varList;
(#/: Positive[#] = False)& /@ varList;
(#/: NonNegative[#] = False)& /@ varList ];
setNonNeg[varSeq__Symbol] := Module[ { varList = {varSeq} },
(#/: NonNegative[#] = True )& /@ varList;
(#/: Negative[#] = False)& /@ varList ];
setInteger[varSeq__Symbol] := Module[ { varList = {varSeq} },
(#/: IntegerQ[#] = True )& /@ varList ];
setEven[varSeq__Symbol] := Module[ { varList = {varSeq} },
(#/: IntegerQ[#] = True )& /@ varList;
(#/: EvenQ[#] = True)& /@ varList;
(#/: OddQ[#] = False)& /@ varList ];
setOdd[varSeq__Symbol] := Module[ { varList = {varSeq} },
(#/: IntegerQ[#] = True )& /@ varList;
(#/: EvenQ[#] = False)& /@ varList;
(#/: OddQ[#] = True)& /@ varList ];
(* Extend definitions of standard trig functions *)
Unprotect[Sin, Csc, Cos, Sec, Tan, Cot];
Clear[evenMultPi2Q, oddMultPi2Q, Sin, Csc, Cos, Sec, Tan, Cot];
evenMultPi2Q::usage = "evenMultPi2Q[x] returns True if x is an
even integer multiple of Pi/2.";
oddMultPi2Q::usage = "oddMultPi2Q[x] returns True if x is an odd
integer multiple of Pi/2.";
evenMultPi2Q[x_] := EvenQ[2x/Pi]; oddMultPi2Q[x_] := OddQ[2x/Pi];
pwr1 = { (-1)^(n_?EvenQ + a_:0) :> (-1)^a,
(-1)^(n_?OddQ + a_:0) :> -(-1)^a,
(-1)^(1 + x_) :> -(-1)^x };
Sin[ y_?evenMultPi2Q ] := 0;
Sin[ y_?oddMultPi2Q ] := (-1)^Expand[(2y/Pi - 1)/2] //. pwr1;
Sin[ x_ + y_?evenMultPi2Q ] := (-1)^Expand[y/Pi] Sin[x] //. pwr1;
Sin[ x_ + y_?oddMultPi2Q ] :=
(-1)^Expand[(2y/Pi - 1)/2] Cos[x] //. pwr1;
Csc[ y_?evenMultPi2Q ] = ComplexInfinity;
Csc[ y_?oddMultPi2Q ] := (-1)^Expand[(2y/Pi - 1)/2] //. pwr1;
Csc[ x_ + y_?evenMultPi2Q ] := (-1)^Expand[y/Pi] Csc[x] //. pwr1;
Csc[ x_ + y_?oddMultPi2Q ] :=
(-1)^Expand[(2y/Pi - 1)/2] Sec[x] //. pwr1;
Cos[ y_?evenMultPi2Q ] := (-1)^Expand[y/Pi] //. pwr1;
Cos[ y_?oddMultPi2Q ] = 0;
Cos[ x_ + y_?evenMultPi2Q ] := (-1)^Expand[y/Pi] Cos[x] //. pwr1;
Cos[ x_ + y_?oddMultPi2Q ] :=
(-1)^Expand[(2y/Pi + 1)/2] Sin[x] //. pwr1;
Sec[ y_?evenMultPi2Q ] = (-1)^Expand[y/Pi] //. pwr1;
Sec[ y_?oddMultPi2Q ] := ComplexInfinity;
Sec[ x_ + y_?evenMultPi2Q ] := (-1)^Expand[y/Pi] Sec[x] //. pwr1;
Sec[ x_ + y_?oddMultPi2Q ] :=
(-1)^Expand[(2y/Pi + 1)/2] Csc[x] //. pwr1;
Tan[ y_?evenMultPi2Q ] = 0; Tan[ y_?oddMultPi2Q ] = ComplexInfinity;
Tan[ x_ + y_?evenMultPi2Q ] := Tan[x];
Tan[ x_ + y_?oddMultPi2Q ] := -Cot[x];
Cot[ y_?evenMultPi2Q ] = ComplexInfinity; Cot[ y_?oddMultPi2Q ] = 0;
Cot[ x_ + y_?evenMultPi2Q ] := Cot[x];
Cot[ x_ + y_?oddMultPi2Q ] := -Tan[x];
Protect[ Sin, Csc, Cos, Sec, Tan, Cot ];
funcs = {Sin, Cos, Tan, Csc, Sec, Cot};
setInteger[m, n]; setEven[keven]; setOdd[kodd];
TableForm[ Table[#[x + n Pi/2], {n, 0, 4}]& /@ funcs,
TableHeadings -> { None,
{x, x + Pi/2, x + Pi, x + 3Pi/2, x + 2k Pi} } ]
TableForm[ Partition[ Flatten[ { f[n Pi/2], f[keven Pi/2],
f[kodd Pi/2] } /. {f -> #}& /@ funcs ], 3],
TableSpacing -> {1, 5} ]
test = {comment, f[a b Pi/2 + x], f[a b Pi + x] } /.
{ {comment -> "integers:", a -> n, b -> m},
{comment -> "one even:", a -> 2n, b -> m},
{comment -> "both even:", a -> 2n, b -> 2m},
{comment -> "one odd:", a -> 2n+1, b -> m},
{comment -> "both odd:", a -> 2n+1, b -> 2m+1} };
TableForm[test /. f -> Sin, TableSpacing -> {2, 5} ] (* very slow *)
TableForm[test /. f -> Cos, TableSpacing -> {2, 5} ] (* very slow *)