KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > maths > groups > QuaternionGroup


1 package JSci.maths.groups;
2
3 /**
4 * The QuaternionGroup class represents the quaternion group.
5 * @jsci.planetmath QuaternionGroup
6 * @version 1.0
7 * @author Mark Hale
8 */

9 public final class QuaternionGroup extends FiniteGroup {
10         private static final QuaternionGroup _instance = new QuaternionGroup();
11
12         /**
13         * The identity element.
14         */

15         private final Member ONE;
16         private QuaternionGroup() {
17                 super(8);
18                 ONE = new Member(1);
19         }
20         /**
21         * Constructs the quaternion group.
22         * Singleton.
23         */

24         public static final QuaternionGroup getInstance() {
25                 return _instance;
26         }
27         public String JavaDoc toString() {
28                 return "Q";
29         }
30         /**
31         * Returns the elements of this group.
32         */

33         public Group.Member[] getElements() {
34                 return new Group.Member[] {new Member(+1), new Member(+2), new Member(+3), new Member(+4),
35                                                         new Member(-1), new Member(-2), new Member(-3), new Member(-4)};
36         }
37         /**
38         * Returns the identity element.
39         */

40         public Monoid.Member identity() {
41                 return ONE;
42         }
43         /**
44         * Returns true if the element is the identity element of this group.
45         * @param a a group element
46         */

47         public boolean isIdentity(Monoid.Member a) {
48                 return a.equals(ONE);
49         }
50         /**
51         * Returns true if one element is the inverse of the other.
52         * @param a a group element
53         * @param b a group element
54         */

55         public boolean isInverse(Group.Member a, Group.Member b) {
56                 return (a instanceof Member) && (b instanceof Member) && a.compose(b).equals(ONE);
57         }
58         /** Multiplication table */
59         private static final int multTable[][] = {{1,2,3,4}, {2,-1,4,-3}, {3,-4,-1,2}, {4,3,-2,-1}};
60         class Member implements Group.Member {
61                 /** 1 = '1', 2 = 'i', 3 = 'j', 4 = 'k' */
62                 private final int unit;
63
64                 public Member(int e) {
65                         if(e<-4 || e==0 || e>4)
66                                 throw new IllegalArgumentException JavaDoc();
67                         unit = e;
68                 }
69                 /**
70                 * Returns true if this member is equal to another.
71                 */

72                 public boolean equals(Object JavaDoc o) {
73                         return (o instanceof Member) && (unit == ((Member)o).unit);
74                 }
75         public int hashCode() {
76             return unit;
77         }
78         public Object JavaDoc getSet() {
79             return QuaternionGroup.this;
80         }
81                 /**
82                 * The group composition law.
83                 * @param g a group member
84                 */

85                 public Semigroup.Member compose(Semigroup.Member g) {
86                         int gunit = ((Member)g).unit;
87                         if(unit>0 && gunit>0)
88                                 return new Member(multTable[unit][gunit]);
89                         else if(unit>0 && gunit<0)
90                                 return new Member(-multTable[unit][-gunit]);
91                         else if(unit<0 && gunit>0)
92                                 return new Member(-multTable[-unit][gunit]);
93                         else
94                                 return new Member(multTable[-unit][-gunit]);
95                 }
96                 /**
97                 * Returns the inverse member.
98                 */

99                 public Group.Member inverse() {
100                         return new Member(-unit);
101                 }
102         }
103 }
104
105
Popular Tags