KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > math > Quantity


1 // Copyright (c) 1997 Per M.A. Bothner.
2
// This is free software; for terms and warranty disclaimer see ./COPYING.
3

4 package gnu.math;
5
6 /** A quantity with a unit.
7  * This generalizes the DSSSL quantity type (to more than lengths).
8  * @author Per Bothner
9  */

10
11 public abstract class Quantity extends Numeric
12 {
13   public Unit unit() { return Unit.Empty; }
14   public Dimensions dimensions() { return unit().dimensions(); }
15
16   public abstract Complex number ();
17
18   /** The value of the real component, as a RealNum.
19    * The unit() is not factored in, so you actually need to multiply
20    * by unit() to get the true real component.
21    */

22   public RealNum re () { return number().re(); }
23
24   /** The value of the imaginary component, as a RealNum.
25    * The unit() is not factored in, so you actually need to multiply
26    * by unit() to get the true imaginary component.
27    */

28   public RealNum im () { return number().im(); }
29
30   /** The value of the real component, as a double.
31    * This is relative to the unit().dims - i.e. unit().doubleValue()
32    * is factored in.
33    * A final alias for the virtual doubleValue. */

34   public final double reValue() { return doubleValue(); }
35
36   /** The value of the imaginary component, as a double.
37    * This is relative to the unit().dims - i.e. unit().doubleValue()
38    * is factored in.
39    * A final alias for the virtual doubleImagValue. */

40   public final double imValue() { return doubleImagValue(); }
41
42   /** The value of the real component, as a double.
43    * This is relative to the unit().dims - i.e. unit()/doubleValue()
44    * is factored in. */

45   public double doubleValue ()
46   { return unit().doubleValue() * re().doubleValue (); }
47
48   /** The value of the imaginary component, as a double.
49    * This is relative to the unit().dims - i.e. unit()/doubleValue()
50    * is factored in. */

51   public double doubleImagValue() {return unit().doubleValue() * im().doubleValue ();}
52
53   public static Quantity make (Complex x, Unit u)
54   {
55     if (u == Unit.Empty)
56       return x;
57     if (x instanceof DFloNum)
58       return new DQuantity (x.doubleValue(), u);
59     return new CQuantity (x, u);
60   }
61
62   public static Quantity make (RealNum re, RealNum im, Unit unit)
63   {
64     if (unit == Unit.Empty)
65       return Complex.make (re, im);
66     if (im.isZero () && (!re.isExact() || !im.isExact()))
67       return new DQuantity(re.doubleValue(), unit);
68     return new CQuantity (re, im, unit);
69   }
70
71   public static Quantity make (double re, double im, Unit unit)
72   {
73     if (unit == Unit.Empty)
74       return Complex.make (re, im);
75     if (im == 0.0)
76       return new DQuantity(re, unit);
77     return new CQuantity (new DFloNum(re), new DFloNum(im), unit);
78   }
79
80   /*
81   public static Quantity make (Quantity x, Quantity y)
82   {
83     double x_factor = x.unit().doubleValue();
84     if (x.unit() != y.unit())
85       {
86     if (x.dimensions() != y.dimensions())
87       throw new ArithmeticException ("units mis-match");
88     double re = x.doubleValue() / x_factor;
89     double im = y.doubleValue() / x_factor;
90     return Complex.make (re, im, x.unit());
91       }
92     if (! x.isExact() || ! y.isExact())
93       {
94     return Complex.make (x.doubleValue () / x_factor,
95                  y.doubleValue () / x_factor, x.unit());
96       }
97     return Complex.make (x.re(), y.re(), x.unit());
98   }
99   */

100
101   public Numeric neg () { return make ((Complex) number().neg(), unit()); }
102   public Numeric abs () { return make ((Complex) number().abs(), unit()); }
103
104   public static int compare (Quantity x, Quantity y)
105   {
106     if (x.unit() == y.unit())
107       return Complex.compare(x.number(), y.number());
108     if (x.dimensions() != y.dimensions() || x.imValue() != y.imValue())
109       return -3;
110     return DFloNum.compare(x.reValue(), y.reValue());
111   }
112
113   public int compare (Object JavaDoc obj)
114   {
115     if (! (obj instanceof Quantity))
116       return ((Numeric) obj).compareReversed(this);
117     return compare(this, (Quantity) obj);
118   }
119
120   public int compareReversed (Numeric x)
121   {
122     if (x instanceof Quantity)
123       return compare((Quantity)x, this);
124     throw new IllegalArgumentException JavaDoc ();
125   }
126
127   public static Quantity add (Quantity x, Quantity y, int k)
128   {
129     if (x.unit() == y.unit())
130       return make (Complex.add (x.number(), y.number(), k), x.unit());
131     else if (x.dimensions() != y.dimensions())
132       throw new ArithmeticException JavaDoc ("units mis-match");
133     else
134       {
135     double x_factor = x.unit().doubleValue();
136     double re = (x.reValue() + k * y.reValue()) / x_factor;
137     double im = (x.imValue() + k * y.imValue()) / x_factor;
138     return Quantity.make (re, im, x.unit());
139       }
140   }
141
142   public Numeric add (Object JavaDoc y, int k)
143   {
144     if (y instanceof Quantity)
145       return add (this, (Quantity) y, k);
146     return ((Numeric)y).addReversed (this, k);
147   }
148
149   public Numeric addReversed (Numeric x, int k)
150   {
151     if (x instanceof Quantity)
152       return add ((Quantity)x, this, k);
153     throw new IllegalArgumentException JavaDoc ();
154   }
155
156   public static Quantity times (Quantity x, Quantity y)
157   {
158     Unit unit = Unit.times(x.unit(), y.unit());
159     // return Quantity.make (Complex.times(x.number(), y.number()), unit);
160
Numeric num = x.number().mul(y.number());
161     return Quantity.make ((Complex) num, unit);
162   }
163
164   public Numeric mul (Object JavaDoc y)
165   {
166     if (y instanceof Quantity)
167       return times(this, (Quantity) y);
168     return ((Numeric)y).mulReversed (this);
169   }
170
171   public Numeric mulReversed (Numeric x)
172   {
173     if (x instanceof Quantity)
174       return times((Quantity) x, this);
175     throw new IllegalArgumentException JavaDoc ();
176   }
177
178   public static Quantity divide (Quantity x, Quantity y)
179   {
180     Unit unit = Unit.divide (x.unit(), y.unit());
181     Numeric num = x.number().div(y.number());
182     return Quantity.make ((Complex) num, unit);
183   }
184
185   public Numeric div (Object JavaDoc y)
186   {
187     if (y instanceof Quantity)
188       return divide(this, (Quantity) y);
189     return ((Numeric)y).divReversed (this);
190   }
191
192   public Numeric divReversed (Numeric x)
193   {
194     if (x instanceof Quantity)
195       return divide((Quantity)x, this);
196     throw new IllegalArgumentException JavaDoc ();
197   }
198
199   public String JavaDoc toString (int radix)
200   {
201     String JavaDoc str = number ().toString (radix);
202     if (unit () == Unit.Empty)
203       return str;
204     return str + unit ().toString ();
205   }
206 }
207
Popular Tags