DQuantity.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Copyright (c) 1997 Per M.A. Bothner.
  2. // This is free software; for terms and warranty disclaimer see ./COPYING.
  3. package gnu.math;
  4. import java.io.*;
  5. /** A Quantity represented as the product of a plain double and a Unit.
  6. * @author Per Bothner
  7. */
  8. public class DQuantity extends Quantity implements Externalizable
  9. {
  10. double factor;
  11. Unit unt;
  12. public final Unit unit() { return unt; }
  13. public final Complex number() { return new DFloNum(factor); }
  14. public final RealNum re() { return new DFloNum (factor); }
  15. public final double doubleValue() { return factor * unt.factor; }
  16. public DQuantity (double factor, Unit unit)
  17. {
  18. this.factor = factor;
  19. this.unt = unit;
  20. }
  21. public boolean isExact () { return false; }
  22. public boolean isZero () { return factor == 0.0; }
  23. public static DQuantity add (DQuantity x, DQuantity y, double k)
  24. {
  25. if (x.dimensions() != y.dimensions())
  26. throw new ArithmeticException ("units mis-match");
  27. double unit_ratio = y.unit().factor / x.unit().factor;
  28. return new DQuantity (x.factor + k * unit_ratio * y.factor, x.unit());
  29. }
  30. public static DQuantity times (DQuantity x, DQuantity y)
  31. {
  32. double factor = x.factor * y.factor;
  33. Unit unit = Unit.times(x.unit(), y.unit());
  34. return new DQuantity (factor, unit);
  35. }
  36. public static DQuantity divide (DQuantity x, DQuantity y)
  37. {
  38. double factor = x.factor / y.factor;
  39. Unit unit = Unit.divide(x.unit(), y.unit());
  40. return new DQuantity (factor, unit);
  41. }
  42. public Numeric add (Object y, int k)
  43. {
  44. if (y instanceof DQuantity)
  45. return add (this, (DQuantity) y, (double) k);
  46. if (dimensions() == Dimensions.Empty && y instanceof RealNum)
  47. return new DQuantity (factor + k * ((RealNum)y).doubleValue (), unit());
  48. if (!(y instanceof Numeric))
  49. throw new IllegalArgumentException ();
  50. return ((Numeric)y).addReversed(this, k);
  51. }
  52. public Numeric addReversed (Numeric x, int k)
  53. {
  54. if (dimensions() == Dimensions.Empty && x instanceof RealNum)
  55. return new DFloNum (((RealNum)x).doubleValue () + k * factor);
  56. throw new IllegalArgumentException ();
  57. }
  58. public Numeric mul (Object y)
  59. {
  60. if (y instanceof DQuantity)
  61. return times(this, (DQuantity) y);
  62. if (y instanceof RealNum)
  63. return new DQuantity (factor * ((RealNum)y).doubleValue (), unit());
  64. if (!(y instanceof Numeric))
  65. throw new IllegalArgumentException ();
  66. return ((Numeric)y).mulReversed(this);
  67. }
  68. public Numeric mulReversed (Numeric x)
  69. {
  70. if (x instanceof RealNum)
  71. return new DQuantity (((RealNum)x).doubleValue () * factor, unit());
  72. throw new IllegalArgumentException ();
  73. }
  74. public Numeric div (Object y)
  75. {
  76. if (y instanceof DQuantity)
  77. {
  78. DQuantity qy = (DQuantity) y;
  79. if (dimensions() == qy.dimensions())
  80. return new DFloNum ((factor * unit().doubleValue())
  81. / (qy.factor * qy.unit().factor));
  82. return divide(this, qy);
  83. }
  84. if (y instanceof RealNum)
  85. return new DQuantity (factor / ((RealNum)y).doubleValue (), unit());
  86. if (!(y instanceof Numeric))
  87. throw new IllegalArgumentException ();
  88. return ((Numeric)y).divReversed(this);
  89. }
  90. public Numeric divReversed (Numeric x)
  91. {
  92. if (x instanceof RealNum)
  93. return new DQuantity (((RealNum)x).doubleValue () / factor,
  94. Unit.divide(Unit.Empty, unit()));
  95. throw new IllegalArgumentException ();
  96. }
  97. /**
  98. * @serialData Write the value (using writeDouble) followed
  99. * by the Unit (using writeObject).
  100. */
  101. public void writeExternal(ObjectOutput out) throws IOException
  102. {
  103. out.writeDouble(factor);
  104. out.writeObject(unt);
  105. }
  106. public void readExternal(ObjectInput in)
  107. throws IOException, ClassNotFoundException
  108. {
  109. factor = in.readDouble();
  110. unt = (Unit) in.readObject();
  111. }
  112. }