Download | Plain Text | No Line Numbers


  1. /*
  2.  * Copyright (c) 2008, Manuel Mausz <manuel at mausz.at>
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions are met:
  7.  * * Redistributions of source code must retain the above copyright
  8.  * notice, this list of conditions and the following disclaimer.
  9.  * * Redistributions in binary form must reproduce the above copyright
  10.  * notice, this list of conditions and the following disclaimer in the
  11.  * documentation and/or other materials provided with the distribution.
  12.  * * Neither the name of the copyright holders nor the
  13.  * names of its contributors may be used to endorse or promote products
  14.  * derived from this software without specific prior written permission.
  15.  *
  16.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  19.  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  20.  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  21.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  22.  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  23.  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24.  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  25.  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  26.  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  27.  * DAMAGE.
  28.  */
  29.  
  30. /**
  31.  * Complex encapsulate a complex value
  32.  *
  33.  * @version 1.0
  34.  * @author Manuel Mausz (manuel at mausz.at)
  35.  * @author http://manuel.mausz.at/
  36.  */
  37. final public class Complex extends CalcValue
  38. {
  39. final private double real;
  40. final private double img;
  41.  
  42. /**
  43.   * Constructor
  44.   *
  45.   * @param real real part
  46.   * @param img imaginary part
  47.   */
  48. Complex(double real, double img)
  49. {
  50. addOps();
  51. this.real = real;
  52. this.img = img;
  53. }
  54.  
  55. /**
  56.   * Add supported operations
  57.   */
  58. private void addOps()
  59. {
  60. addOp(new CalcOperator("+"), "add");
  61. addOp(new CalcOperator("-"), "sub");
  62. addOp(new CalcOperator("*"), "mul");
  63. }
  64.  
  65. /**
  66.   * @return real part
  67.   */
  68. public double getReal()
  69. {
  70. return real;
  71. }
  72.  
  73. /**
  74.   * @return imaginary part
  75.   */
  76. public double getImg()
  77. {
  78. return img;
  79. }
  80.  
  81.  
  82. /**
  83.   * Converts instance to string
  84.   *
  85.   * @return string
  86.   */
  87. public String toString()
  88. {
  89. return real + " " + img;
  90. }
  91.  
  92. /**
  93.   * Addition
  94.   *
  95.   * @param val
  96.   * @return new Complex instance
  97.   */
  98. public Complex add(Complex val)
  99. {
  100. return new Complex(real + val.getReal(), img + val.getImg());
  101. }
  102.  
  103. /**
  104.   * Subtraction
  105.   *
  106.   * @param val
  107.   * @return new Complex instance
  108.   */
  109. public Complex sub(Complex val)
  110. {
  111. return new Complex(real - val.getReal(), img - val.getImg());
  112. }
  113.  
  114. /**
  115.   * Multiplication
  116.   *
  117.   * @param val
  118.   * @return new Complex instance
  119.   */
  120. public Complex mul(Complex val)
  121. {
  122. double realnew = real * val.getReal() - img * val.getImg();
  123. double imgnew = real * val.getImg() + img * val.getReal();
  124. return new Complex(realnew, imgnew);
  125. }
  126. }
  127.