KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lsmp > djep > groupJep > groups > Zn


1 /* @author rich
2  * Created on 05-Mar-2004
3  *
4  * This code is covered by a Creative Commons
5  * Attribution, Non Commercial, Share Alike license
6  * <a HREF="http://creativecommons.org/licenses/by-nc-sa/1.0">License</a>
7  */

8 package org.lsmp.djep.groupJep.groups;
9 import org.lsmp.djep.groupJep.interfaces.*;
10 import java.math.*;
11
12 /**
13  * The group of integers mod n.
14  * For prime values of n this is a field, with some
15  * nice division tables. i.e. for Z5
16  * <pre>* | 1 2 3 4
17  * ------------
18  * 1 | 1 2 3 4
19  * 2 | 2 4 1 3
20  * 3 | 3 1 4 2
21  * 4 | 4 3 2 1
22  * </pre>
23  * <pre>/ | 1 2 3 4
24  * ------------
25  * 1 | 1 2 3 4
26  * 2 | 3 1 4 2
27  * 3 | 2 4 1 3
28  * 4 | 4 3 2 1
29  * </pre>
30  *
31  * @author Rich Morris
32  * Created on 05-Mar-2004
33  */

34 public class Zn extends Group implements FieldI,
35     OrderedSetI,HasModI,HasPowerI {
36     BigInteger modulus;
37     /**
38      * Operations on the reals (Implemented as BigInteger).
39      */

40     private Zn() {}
41     
42     public Zn(BigInteger modulus) {
43         this.modulus = modulus;
44     }
45
46     public Number JavaDoc getZERO() {
47         return BigInteger.ZERO;
48     }
49
50     public Number JavaDoc getONE() {
51         return BigInteger.ONE;
52     }
53
54     public Number JavaDoc getInverse(Number JavaDoc num) {
55         BigInteger a = (BigInteger) num;
56         return a.negate().mod(modulus);
57     }
58
59     public Number JavaDoc getMulInverse(Number JavaDoc num) {
60         BigInteger a = (BigInteger) num;
61         return a.modInverse(modulus);
62     }
63
64     public Number JavaDoc add(Number JavaDoc num1, Number JavaDoc num2) {
65         BigInteger a = (BigInteger) num1;
66         BigInteger b = (BigInteger) num2;
67         return a.add(b).mod(modulus);
68     }
69
70     public Number JavaDoc sub(Number JavaDoc num1, Number JavaDoc num2) {
71         BigInteger a = (BigInteger) num1;
72         BigInteger b = (BigInteger) num2;
73         return a.subtract(b).mod(modulus);
74     }
75
76     public Number JavaDoc mul(Number JavaDoc num1, Number JavaDoc num2) {
77         BigInteger a = (BigInteger) num1;
78         BigInteger b = (BigInteger) num2;
79         return a.multiply(b).mod(modulus);
80     }
81
82     public Number JavaDoc div(Number JavaDoc num1, Number JavaDoc num2) {
83         BigInteger a = (BigInteger) num1;
84         BigInteger b = (BigInteger) num2;
85         return a.multiply(b.modInverse(modulus)).mod(modulus);
86     }
87     
88     public Number JavaDoc mod(Number JavaDoc num1, Number JavaDoc num2) {
89         BigInteger a = (BigInteger) num1;
90         BigInteger b = (BigInteger) num2;
91         return a.mod(b).mod(modulus);
92     }
93     
94     public Number JavaDoc pow(Number JavaDoc num1, Number JavaDoc num2) {
95         BigInteger a = (BigInteger) num1;
96         BigInteger b = (BigInteger) num2;
97         return a.modPow(b,modulus);
98     }
99     public boolean equals(Number JavaDoc a,Number JavaDoc b) {
100         return ((Integer JavaDoc) a).compareTo((Integer JavaDoc) b) == 0;
101     }
102     
103     public int compare(Number JavaDoc a,Number JavaDoc b) {
104         return ((Integer JavaDoc) a).compareTo((Integer JavaDoc) b);
105     }
106
107
108     public Number JavaDoc valueOf(String JavaDoc str) {
109         BigInteger in = new BigInteger(str);
110         return in.mod(modulus);
111     }
112     
113     public String JavaDoc toString() { return "Integers mod "+this.modulus; }
114 }
115
Popular Tags