Complex.java 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. public abstract class Complex extends Quaternion
  5. {
  6. @Override public final RealNum jm() { return IntNum.zero(); }
  7. @Override public final RealNum km() { return IntNum.zero(); }
  8. @Override public final Complex complexPart() { return this; }
  9. @Override public Quaternion vectorPart() {
  10. return Complex.make(IntNum.zero(), im());
  11. }
  12. @Override public Quaternion unitVector() {
  13. int imSign = im().sign();
  14. switch (imSign) {
  15. case 1:
  16. return Complex.imOne();
  17. case 0:
  18. return IntNum.zero();
  19. case -1:
  20. return Complex.imMinusOne();
  21. case -2: default:
  22. return Complex.make(0, Double.NaN);
  23. }
  24. }
  25. @Override public Quaternion unitQuaternion() {
  26. if (im().isZero())
  27. return re().unitQuaternion();
  28. if (re().isZero())
  29. return Complex.make(IntNum.zero(), (RealNum)im().unitQuaternion());
  30. return DComplex.unitQuaternion(doubleRealValue(), doubleImagValue());
  31. }
  32. @Override public Quaternion conjugate() {
  33. return Complex.make(re(), im().rneg());
  34. }
  35. public boolean isExact ()
  36. {
  37. // Should we return false if unit() != unit.Empty ?
  38. return re().isExact() && im().isExact();
  39. }
  40. /** Check if value is finite, infinite, or NaN.
  41. * @return 1 if finite; 0 if infinite; -1 if NaN.
  42. */
  43. public int classifyFinite() {
  44. int r = re().classifyFinite();
  45. if (r < 0)
  46. return r;
  47. int i = im().classifyFinite();
  48. return r < i ? r : i;
  49. }
  50. public Complex toExact ()
  51. {
  52. RealNum re = re();
  53. RealNum im = im();
  54. RatNum xre = re.toExact();
  55. RatNum xim = im.toExact();
  56. if (xre == re && xim == im)
  57. return this;
  58. else
  59. return new CComplex(xre, xim);
  60. }
  61. public Complex toInexact ()
  62. {
  63. if(isExact())
  64. return this;
  65. return new DComplex(re().doubleValue(), im().doubleValue());
  66. }
  67. private static CComplex imOne;
  68. private static CComplex imMinusOne;
  69. public static CComplex imOne()
  70. {
  71. if (imOne == null)
  72. imOne = new CComplex (IntNum.zero(), IntNum.one());
  73. return imOne;
  74. }
  75. public static CComplex imMinusOne()
  76. {
  77. if (imMinusOne == null)
  78. imMinusOne = new CComplex (IntNum.zero(), IntNum.minusOne());
  79. return imMinusOne;
  80. }
  81. public static Complex make (RealNum re, RealNum im)
  82. {
  83. if (im.isZero() && im.isExact())
  84. return re;
  85. if (! re.isExact() && ! im.isExact())
  86. return new DComplex(re.doubleValue(), im.doubleValue());
  87. return new CComplex (re, im);
  88. }
  89. public static Complex make (double re, double im)
  90. {
  91. if (im == 0.0)
  92. return new DFloNum(re);
  93. return new DComplex(re, im);
  94. }
  95. public static Complex polar (double r, double t)
  96. {
  97. if (t == 0.0)
  98. return new DFloNum(r);
  99. return new DComplex(r * Math.cos(t), r * Math.sin(t));
  100. }
  101. public static Complex polar (RealNum r, RealNum t)
  102. {
  103. if (t.isZero() && t.isExact())
  104. return r;
  105. return polar(r.doubleValue(), t.doubleValue());
  106. }
  107. public static Complex power (Complex x, Complex y)
  108. {
  109. if (y instanceof IntNum)
  110. return (Complex) x.power((IntNum) y);
  111. double y_re = y.doubleRealValue();
  112. double y_im = y.doubleImagValue();
  113. if (x.isZero() && x.isExact() && y.isExact()) {
  114. if (y_re > 0)
  115. return IntNum.zero();
  116. else if (y_re == 0 && y_im == 0)
  117. return IntNum.one();
  118. }
  119. double x_re = x.doubleRealValue();
  120. double x_im = x.doubleImagValue();
  121. if (x_im == 0.0 && y_im == 0
  122. && (x_re >= 0 || Double.isInfinite(x_re) || Double.isNaN(x_re)))
  123. return new DFloNum (Math.pow (x_re, y_re));
  124. return DComplex.power (x_re, x_im, y_re, y_im);
  125. }
  126. public Numeric abs ()
  127. {
  128. /* #ifdef JAVA5 */
  129. return new DFloNum(Math.hypot(doubleRealValue(), doubleImagValue()));
  130. /* #else */
  131. // return new DFloNum(DComplex.hypot(doubleRealValue(), doubleImagValue()));
  132. /* #endif */
  133. }
  134. public RealNum angle()
  135. {
  136. return new DFloNum(Math.atan2(doubleImagValue(), doubleRealValue()));
  137. }
  138. @Override public final RealNum colatitude() { return IntNum.zero(); }
  139. @Override public final RealNum longitude() { return IntNum.zero(); }
  140. public static boolean equals (Complex x, Complex y)
  141. {
  142. return x.re().equals(y.re())
  143. && x.im().equals(y.im());
  144. }
  145. public boolean equals (Object obj)
  146. {
  147. if (obj == null || ! (obj instanceof Complex))
  148. return false;
  149. return Complex.equals (this, (Complex) obj);
  150. }
  151. public static int compare (Complex x, Complex y)
  152. {
  153. int code = x.im().compare(y.im());
  154. if (code != 0)
  155. return code;
  156. return x.re().compare(y.re());
  157. }
  158. public int compare (Object obj)
  159. {
  160. if (! (obj instanceof Complex))
  161. return ((Numeric) obj).compareReversed(this);
  162. return compare(this, (Complex) obj);
  163. }
  164. public boolean isZero ()
  165. {
  166. return re().isZero () && im().isZero();
  167. }
  168. // public abstract Complex neg ();
  169. /*
  170. Unit unit () { return Unit.Empty; }
  171. Dimesions dims() { return unit().dims; }
  172. */
  173. public String toString (int radix)
  174. {
  175. // Note: The r4rs read syntax does not allow unsigned pure
  176. // imaginary numbers, i.e. you must use +5i, not 5i.
  177. // Although our reader allows the sign to be dropped, we always
  178. // print it so that the number may be read by any r4rs system.
  179. if (im().isZero ())
  180. return re().toString(radix);
  181. String imString = im().toString(radix) + "i";
  182. char ch0 = imString.charAt(0);
  183. if (ch0 != '-' && ch0 != '+')
  184. imString = "+" + imString;
  185. if (re().isZero())
  186. return imString;
  187. return re().toString(radix) + imString;
  188. }
  189. public static Complex neg (Complex x)
  190. {
  191. return Complex.make (x.re().rneg(), x.im().rneg());
  192. }
  193. public Numeric neg () { return neg (this); }
  194. public static Complex add (Complex x, Complex y, int k)
  195. {
  196. return Complex.make (RealNum.add(x.re(), y.re(), k),
  197. RealNum.add(x.im(), y.im(), k));
  198. }
  199. public Numeric add (Object y, int k)
  200. {
  201. if (y instanceof Complex)
  202. return add (this, (Complex) y, k);
  203. return ((Numeric)y).addReversed(this, k);
  204. }
  205. public Numeric addReversed (Numeric x, int k)
  206. {
  207. if (x instanceof Complex)
  208. return add ((Complex)x, this, k);
  209. throw new IllegalArgumentException ();
  210. }
  211. public static Complex times (Complex x, Complex y)
  212. {
  213. RealNum x_re = x.re();
  214. RealNum x_im = x.im();
  215. RealNum y_re = y.re();
  216. RealNum y_im = y.im();
  217. return Complex.make (RealNum.add (RealNum.times(x_re, y_re),
  218. RealNum.times(x_im, y_im), -1),
  219. RealNum.add (RealNum.times(x_re, y_im),
  220. RealNum.times(x_im, y_re), 1));
  221. }
  222. public Numeric mul (Object y)
  223. {
  224. if (y instanceof Complex)
  225. return times(this, (Complex) y);
  226. return ((Numeric)y).mulReversed(this);
  227. }
  228. public Numeric mulReversed (Numeric x)
  229. {
  230. if (x instanceof Complex)
  231. return times((Complex)x, this);
  232. throw new IllegalArgumentException ();
  233. }
  234. public static Complex divide (Complex x, Complex y)
  235. {
  236. if (! x.isExact () || ! y.isExact ())
  237. return DComplex.div (x.doubleRealValue(), x.doubleImagValue(),
  238. y.doubleRealValue(), y.doubleImagValue());
  239. RealNum x_re = x.re();
  240. RealNum x_im = x.im();
  241. RealNum y_re = y.re();
  242. RealNum y_im = y.im();
  243. RealNum q = RealNum.add (RealNum.times(y_re, y_re),
  244. RealNum.times(y_im, y_im), 1);
  245. RealNum n = RealNum.add(RealNum.times(x_re, y_re),
  246. RealNum.times(x_im, y_im), 1);
  247. RealNum d = RealNum.add(RealNum.times(x_im, y_re),
  248. RealNum.times(x_re, y_im), -1);
  249. return Complex.make(RealNum.divide(n, q), RealNum.divide(d, q));
  250. }
  251. public Numeric div (Object y)
  252. {
  253. if (y instanceof Complex)
  254. return divide(this, (Complex) y);
  255. return ((Numeric)y).divReversed(this);
  256. }
  257. public Numeric divReversed (Numeric x)
  258. {
  259. if (x instanceof Complex)
  260. return divide((Complex)x, this);
  261. throw new IllegalArgumentException ();
  262. }
  263. public Complex exp ()
  264. {
  265. return polar (Math.exp(doubleRealValue()), doubleImagValue());
  266. }
  267. public Complex log ()
  268. {
  269. return DComplex.log(doubleRealValue(), doubleImagValue());
  270. }
  271. public Complex sqrt ()
  272. {
  273. return DComplex.sqrt(doubleRealValue(), doubleImagValue());
  274. }
  275. @Override public Complex sin() {
  276. return DComplex.sin(doubleRealValue(), doubleImagValue());
  277. }
  278. @Override public Complex cos() {
  279. return DComplex.cos(doubleRealValue(), doubleImagValue());
  280. }
  281. @Override public Complex tan() {
  282. return DComplex.tan(doubleRealValue(), doubleImagValue());
  283. }
  284. }