KickJava   Java API By Example, From Geeks To Geeks.

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


1 package JSci.maths.groups;
2
3 /**
4 * The DihedralGroup class represents the <i>n</i>th dihedral group.
5 * Elements are represented by rotations and reflections.
6 * @jsci.planetmath DihedralGroup
7 * @version 1.4
8 * @author Mark Hale
9 */

10 public final class DihedralGroup extends FiniteGroup {
11         /**
12         * The number of rotations/reflections.
13         */

14         private final int n;
15         /**
16         * The identity element.
17         */

18         private final Member ONE;
19         /**
20         * Constructs a dihedral group.
21         * @param n the number of rotations/reflections
22         */

23         public DihedralGroup(int n) {
24                 super(2*n);
25                 this.n = n;
26                 ONE = new Member(0, false);
27         }
28         /**
29         * Returns true if this group is isomorphic to another.
30         */

31         public boolean equals(Object JavaDoc o) {
32                 return (o instanceof DihedralGroup) && (((DihedralGroup)o).order == order);
33         }
34     public int hashCode() {
35         return order;
36     }
37         public String JavaDoc toString() {
38                 return "D_"+n;
39         }
40         /**
41         * Returns the elements of this group.
42         */

43         public Group.Member[] getElements() {
44                 Group.Member elements[] = new Group.Member[order];
45                 for(int i=0; i<n; i++) {
46                         elements[i] = new Member(i, false);
47                         elements[i+n] = new Member(i, true);
48                 }
49                 return elements;
50         }
51         /**
52         * Returns the identity element.
53         */

54         public Monoid.Member identity() {
55                 return ONE;
56         }
57         /**
58         * Returns true if the element is the identity element of this group.
59         * @param a a group element
60         */

61         public boolean isIdentity(Monoid.Member a) {
62                 return a.equals(ONE);
63         }
64         /**
65         * Returns true if one element is the inverse of the other.
66         * @param a a group element
67         * @param b a group element
68         */

69         public boolean isInverse(Group.Member a, Group.Member b) {
70                 return (a instanceof Member) && (b instanceof Member) && a.compose(b).equals(ONE);
71         }
72         class Member implements Group.Member {
73                 /** 0 <= rotate < n */
74                 private final int rotate;
75                 private final boolean reflect;
76
77                 public Member(int theta, boolean flip) {
78                         rotate = (theta<0) ? theta%n+n : theta%n;
79                         reflect = flip;
80                 }
81                 /**
82                 * Returns true if this member is equal to another.
83                 */

84                 public boolean equals(Object JavaDoc o) {
85                         return (o instanceof Member) && (rotate == ((Member)o).rotate) && (reflect == ((Member)o).reflect);
86                 }
87         public int hashCode() {
88             return 2*rotate + (reflect ? 1 : 0);
89         }
90         public Object JavaDoc getSet() {
91             return DihedralGroup.this;
92         }
93                 /**
94                 * The group composition law.
95                 * @param g a group member
96                 */

97                 public Semigroup.Member compose(Semigroup.Member g) {
98                         if(reflect)
99                                 return new Member(rotate-((Member)g).rotate, !((Member)g).reflect);
100                         else
101                                 return new Member(rotate+((Member)g).rotate, ((Member)g).reflect);
102                 }
103                 /**
104                 * Returns the inverse member.
105                 */

106                 public Group.Member inverse() {
107                         return new Member(-rotate, !reflect);
108                 }
109         }
110 }
111
112
Popular Tags