001: // Copyright (c) 1997 Per M.A. Bothner.
002: // This is free software; for terms and warranty disclaimer see ./COPYING.
003:
004: package gnu.math;
005:
006: import java.io.*;
007:
008: /** A Quantity represented as the product of a plain double and a Unit.
009: * @author Per Bothner
010: */
011:
012: public class DQuantity extends Quantity implements Externalizable {
013: double factor;
014: Unit unt;
015:
016: public final Unit unit() {
017: return unt;
018: }
019:
020: public final Complex number() {
021: return new DFloNum(factor);
022: }
023:
024: public final RealNum re() {
025: return new DFloNum(factor);
026: }
027:
028: public final double doubleValue() {
029: return factor * unt.factor;
030: }
031:
032: public DQuantity(double factor, Unit unit) {
033: this .factor = factor;
034: this .unt = unit;
035: }
036:
037: public boolean isExact() {
038: return false;
039: }
040:
041: public boolean isZero() {
042: return factor == 0.0;
043: }
044:
045: public static DQuantity add(DQuantity x, DQuantity y, double k) {
046: if (x.dimensions() != y.dimensions())
047: throw new ArithmeticException("units mis-match");
048: double unit_ratio = y.unit().factor / x.unit().factor;
049: return new DQuantity(x.factor + k * unit_ratio * y.factor, x
050: .unit());
051: }
052:
053: public static DQuantity times(DQuantity x, DQuantity y) {
054: double factor = x.factor * y.factor;
055: Unit unit = Unit.times(x.unit(), y.unit());
056: return new DQuantity(factor, unit);
057: }
058:
059: public static DQuantity divide(DQuantity x, DQuantity y) {
060: double factor = x.factor / y.factor;
061: Unit unit = Unit.divide(x.unit(), y.unit());
062: return new DQuantity(factor, unit);
063: }
064:
065: public Numeric add(Object y, int k) {
066: if (y instanceof DQuantity)
067: return add(this , (DQuantity) y, (double) k);
068: if (dimensions() == Dimensions.Empty && y instanceof RealNum)
069: return new DQuantity(factor + k
070: * ((RealNum) y).doubleValue(), unit());
071: if (!(y instanceof Numeric))
072: throw new IllegalArgumentException();
073: return ((Numeric) y).addReversed(this , k);
074: }
075:
076: public Numeric addReversed(Numeric x, int k) {
077: if (dimensions() == Dimensions.Empty && x instanceof RealNum)
078: return new DFloNum(((RealNum) x).doubleValue() + k * factor);
079: throw new IllegalArgumentException();
080: }
081:
082: public Numeric mul(Object y) {
083: if (y instanceof DQuantity)
084: return times(this , (DQuantity) y);
085: if (y instanceof RealNum)
086: return new DQuantity(factor * ((RealNum) y).doubleValue(),
087: unit());
088: if (!(y instanceof Numeric))
089: throw new IllegalArgumentException();
090: return ((Numeric) y).mulReversed(this );
091: }
092:
093: public Numeric mulReversed(Numeric x) {
094: if (x instanceof RealNum)
095: return new DQuantity(((RealNum) x).doubleValue() * factor,
096: unit());
097: throw new IllegalArgumentException();
098: }
099:
100: public Numeric div(Object y) {
101: if (y instanceof DQuantity) {
102: DQuantity qy = (DQuantity) y;
103: if (dimensions() == qy.dimensions())
104: return new DFloNum((factor * unit().doubleValue())
105: / (qy.factor * qy.unit().factor));
106: return divide(this , qy);
107: }
108: if (y instanceof RealNum)
109: return new DQuantity(factor / ((RealNum) y).doubleValue(),
110: unit());
111: if (!(y instanceof Numeric))
112: throw new IllegalArgumentException();
113: return ((Numeric) y).divReversed(this );
114: }
115:
116: public Numeric divReversed(Numeric x) {
117: if (x instanceof RealNum)
118: return new DQuantity(((RealNum) x).doubleValue() / factor,
119: Unit.divide(Unit.Empty, unit()));
120: throw new IllegalArgumentException();
121: }
122:
123: /**
124: * @serialData Write the value (using writeDouble) followed
125: * by the Unit (using writeObject).
126: */
127:
128: public void writeExternal(ObjectOutput out) throws IOException {
129: out.writeDouble(factor);
130: out.writeObject(unt);
131: }
132:
133: public void readExternal(ObjectInput in) throws IOException,
134: ClassNotFoundException {
135: factor = in.readDouble();
136: unt = (Unit) in.readObject();
137: }
138: }
|