1 package JSci.maths.groups; 2 3 9 public final class QuaternionGroup extends FiniteGroup { 10 private static final QuaternionGroup _instance = new QuaternionGroup(); 11 12 15 private final Member ONE; 16 private QuaternionGroup() { 17 super(8); 18 ONE = new Member(1); 19 } 20 24 public static final QuaternionGroup getInstance() { 25 return _instance; 26 } 27 public String toString() { 28 return "Q"; 29 } 30 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 40 public Monoid.Member identity() { 41 return ONE; 42 } 43 47 public boolean isIdentity(Monoid.Member a) { 48 return a.equals(ONE); 49 } 50 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 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 62 private final int unit; 63 64 public Member(int e) { 65 if(e<-4 || e==0 || e>4) 66 throw new IllegalArgumentException (); 67 unit = e; 68 } 69 72 public boolean equals(Object o) { 73 return (o instanceof Member) && (unit == ((Member)o).unit); 74 } 75 public int hashCode() { 76 return unit; 77 } 78 public Object getSet() { 79 return QuaternionGroup.this; 80 } 81 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 99 public Group.Member inverse() { 100 return new Member(-unit); 101 } 102 } 103 } 104 105 | Popular Tags |