Logo Search packages:      
Sourcecode: cdk version File versions  Download package

Complex.java

/* Complex.java
 * 
 * Copyright (C) 1997-2007  Stephan Michels <stephan@vern.chem.tu-berlin.de>
 * 
 * Contact: cdk-devel@lists.sourceforge.net
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1
 * of the License, or (at your option) any later version.
 * All we ask is that proper credit is given for our work, which includes
 * - but is not limited to - adding the above copyright notice to the beginning
 * of your source code files, and to any copyright notice that you may distribute
 * with programs based on this work.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *  */

package org.openscience.cdk.math;

/**
 * This class handles complex values.
 * 
 * @cdk.module qm
 */ 
00033 public class Complex
{
  /** The real part of this value */
00036   public double real = 0;
  /** The imaginary part of this value */
00038   public double imag = 0;
  
  /**
   * Creates a complex number
   *
   * @param real the real part
   * @param imag the imaginary part
   */
00046   public Complex(double real, double imag)
  {
    this.real = real;
    this.imag = imag;
  }
  
  /**
   * Creates a copy of a complex object
   */
00055   public Complex(Complex c)
  {
    real = c.real;
    imag = c.imag;
  }
  
  /**
   * Sets the real part of this complex value
   */
00064   public void setRealPart(double real)
  {
    this.real = real;
  }
  
  /**
   * Gets the real part of this complex value
   */
00072   public double getRealPart()
  {
    return real;
  }
  
  /**
   * Sets the imaginary part of this value
   */
00080   public void setImaginaryPart(double imag)
  {
    this.imag = imag;
  }
  
  /**
   * Gets the imaginary part of this value
   */
00088   public double getImaginaryPart()
  {
    return imag;
  }
  
  /**
   * Add a complex value
   */
00096   public void add(Complex c)
  {
    real += c.real;
    imag += c.imag;
  }
  
  /**
   * Subtracs a complex value
   */
00105   public void sub(Complex c)
  {
    real -= c.real;
    imag -= c.imag;
  }
  
  /**
   * Multiply this value with a complex value
   */
00114   public void mul(Complex c)
  {
    double newreal = real*c.real-imag*c.imag;
    double newimag = real*c.imag+imag*c.real;
    real = newreal;
    imag = newimag;
  }
  
  /**
   * Div this value by a complex value
   */
00125   public void div(Complex c)
  {
    double modulus = c.real*c.real+c.imag*c.imag;
    double newreal = imag*c.imag+real*c.real;
    double newimag = imag*c.real-real*c.imag;
    real = newreal/modulus;
    imag = newimag/modulus;
  }
  
  /**
   * Create a string of the content of this class
   */
00137   public String toString()
  {
    return "("+real+"+i*"+imag+")";
  }
}

Generated by  Doxygen 1.6.0   Back to index