1 4 package gnu.math; 5 6 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 22 public RealNum re () { return number().re(); } 23 24 28 public RealNum im () { return number().im(); } 29 30 34 public final double reValue() { return doubleValue(); } 35 36 40 public final double imValue() { return doubleImagValue(); } 41 42 45 public double doubleValue () 46 { return unit().doubleValue() * re().doubleValue (); } 47 48 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 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 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 (); 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 ("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 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 (); 154 } 155 156 public static Quantity times (Quantity x, Quantity y) 157 { 158 Unit unit = Unit.times(x.unit(), y.unit()); 159 Numeric num = x.number().mul(y.number()); 161 return Quantity.make ((Complex) num, unit); 162 } 163 164 public Numeric mul (Object 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 (); 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 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 (); 197 } 198 199 public String toString (int radix) 200 { 201 String str = number ().toString (radix); 202 if (unit () == Unit.Empty) 203 return str; 204 return str + unit ().toString (); 205 } 206 } 207 | Popular Tags |