KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* @author rich
2  * Created on 16-May-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
10 import org.nfunk.jep.JEP;
11 import org.lsmp.djep.groupJep.interfaces.*;
12 /**
13  * Possibly the quatonians, compleatly untested.
14  *
15  * @author Rich Morris
16  * Created on 16-May-2004
17  */

18 public class Quartonians extends Group implements RingI {
19
20     public static class Quartonian extends Number JavaDoc {
21         double x,y,z,w;
22         public Quartonian(double x,double y,double z,double w){
23             this.w = x; this.y = y; this.z=z; this.w=w;
24         }
25         public double doubleValue() {return x;}
26         public float floatValue() {return (float) x;}
27         public int intValue() {return (int) x;}
28         public long longValue() {return (long) x;}
29         // TODO pretty print so 0 + 0 i + 0 j + 1 k printed as k
30
public String JavaDoc toString() {return ""+x+"+"+y+" i +"+z+" j +"+w+" k";
31     // StringBuffer sb = new StringBuffer();
32
// if(x!=0.0) sb.append(x);
33

34         }
35     }
36     private Quartonian ZERO = new Quartonian(0,0,0,0);
37     private Quartonian ONE = new Quartonian(1,0,0,0);
38     private Quartonian I = new Quartonian(0,1,0,0);
39     private Quartonian J = new Quartonian(0,0,1,0);
40     private Quartonian K = new Quartonian(0,0,0,1);
41
42     public Number JavaDoc getZERO() {return ZERO;}
43     public Number JavaDoc getONE() {return ONE; }
44
45     public Number JavaDoc getInverse(Number JavaDoc num) {
46         Quartonian q = (Quartonian) num;
47         return new Quartonian(-q.x,-q.y,-q.z,-q.w);
48     }
49
50     public Number JavaDoc add(Number JavaDoc a, Number JavaDoc b) {
51         Quartonian p = (Quartonian) a;
52         Quartonian q = (Quartonian) b;
53         return new Quartonian(p.x+q.x,p.y+q.y,p.z+q.z,p.w+q.w);
54     }
55
56     public Number JavaDoc sub(Number JavaDoc a, Number JavaDoc b) {
57         Quartonian p = (Quartonian) a;
58         Quartonian q = (Quartonian) b;
59         return new Quartonian(p.x-q.x,p.y-q.y,p.z-q.z,p.w-q.w);
60     }
61
62
63     public Number JavaDoc mul(Number JavaDoc a, Number JavaDoc b) {
64         Quartonian p = (Quartonian) a;
65         Quartonian q = (Quartonian) b;
66         return new Quartonian(
67             p.x*q.x - p.y*q.y - p.z*q.z - p.w*q.w,
68             p.x*q.y - p.y*q.x + p.z*q.w - p.w*q.z,
69             p.x*q.z - p.y*q.w + p.z*q.x + p.w*q.y,
70             p.x*q.w - p.y*q.z - p.z*q.y + p.w*q.x
71             );
72     }
73
74     public boolean equals(Number JavaDoc a, Number JavaDoc b) {
75         Quartonian p = (Quartonian) a;
76         Quartonian q = (Quartonian) b;
77         return (p.x==q.x)&&(p.y==q.y)&&(p.z==q.z)&&(p.w==q.w);
78     }
79
80     public Number JavaDoc valueOf(String JavaDoc s) {
81         return new Quartonian(Double.parseDouble(s),0,0,0);
82     }
83
84     public void addStandardConstants(JEP j) {
85         super.addStandardConstants(j);
86         j.addConstant("i",I);
87         j.addConstant("j",J);
88         j.addConstant("k",K);
89     }
90     public String JavaDoc toString() {return "quaternions";}
91
92 }
93
Popular Tags