-
Notifications
You must be signed in to change notification settings - Fork 2
/
integer.m
72 lines (31 loc) · 1.34 KB
/
integer.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
(* Copyright E.M.Clarke and Xudong Zhao, Jan 22, 1991 *)
(* INTEGERS *)
(* Rules for deciding if the type of a term is integer. *)
integer[a_?NumberQ] := (Head[a] === Integer);
integer[x_ y_] := True /; TrueQ[integer[x]] && TrueQ[integer[y]];
integer[x_ + y_] := integer[x] /; integer[y];
integer[a_ ^ m_] := True /;
TrueQ[integer[a]] && TrueQ[integer[m]] && WeakSimplify[0 <= m];
(* odd integers *)
Odd[x_ y_] := and[Odd[x], Odd[y]] /; integer[x] && integer[y];
Odd[x_ ^ y_] := and[Odd[x], integer[y], WeakSimplify[0 <= y]];
Odd[x_Integer] := (Mod[x , 2] != 0);
(* even integers *)
Even[x_ y_] := or[Even[x], Even[y]] /; integer[x] && integer[y];
Even[x_ ^ y_] := and[Even[x], integer[y], WeakSimplify[0 < y]];
Even[x_Integer] := (Mod[x , 2] == 0);
(* rules for the powers of " -1 " *)
Unprotect[Power];
(-1)^(x_?Even y_.) := 1 /; !NumberQ[x] && integer[y];
(-1)^(x_?Odd y_.) := (-1)^y /; !NumberQ[x] && integer[y];
(-1)^(x_ + n_Integer) := (-1)^n (-1)^x;
0^n_ := 0 /; WeakSimplify[n>0];
(a_^b_)^c_ := a^(b c);
(a_ b_)^c_ := a^c b^c;
n_Integer ^ (a_ + m_Integer) := n^m n^a;
x_ ^ ((a_Integer) (c_ + d_)) := x ^ (a c + a d);
Protect[Power];
(* Divisible[a, b] means a is divisible by b *)
Divisible[a_, b_] := integer[a / b];
(* gcd[a, b] gives the greatest common divisor of 'a' and 'b' *)
gcd[a_Integer, b_Integer] := GCD[a, b];