Class Complex

java.lang.Object
org.opensourcephysics.numerics.Complex

public class Complex extends Object
Class description
  • Constructor Summary

    Constructors
    Constructor
    Description
    Default constructor.
    Complex(double re_in)
    Constructor from a single double value.
    Complex(double re_in, double im_in)
    Initialize the real and imaginary components to the values given by the parameters.
    Complex(Number re_in)
    Construct from a Number.
    Copy constructor
  • Method Summary

    Modifier and Type
    Method
    Description
    double
    abs()
    Returns the absolute value of the complex number.
    double
    Returns the square of the absolute value (re*re+im*im).
     
     
    add(double d)
    Returns the sum of a complex number and a double
    Returns the sum of two complex numbers
    double
    arg()
    Returns the argument of this complex number (Math.atan2(re,im))
     
     
     
     
    static Complex
    cart(double re, double im)
    Returns a Complex from real and imaginary parts.
    Convert polar to Cartesian
    Conjugats the complex number
    cos()
    Returns the cosine of this complex number.
     
    div(double d)
    Devide the complex number by a double value.
    Returns the result of dividing this complex number by the parameter.
    boolean
    equals(Complex b, double tolerance)
    Compares this object with the Complex number given as parameter
    exp()
    Returns e to the power of the complex number
    double
    im()
    Returns the imaginary component of this object
     
    boolean
    Returns true if either the real or imaginary component of this Complex is an infinite value.
    boolean
    Returns true if either the real or imaginary component of this Complex is a Not-a-Number (NaN) value.
    log()
    Returns the logarithm of this complex number.
    double
    mag()
    Returns the magnitude of the complex number
    mul(double b)
    Multiplies the complex number with a double value.
    Multiplies the complex number with another complex value.
    neg()
    Returns the negative value of this complex number.
    static Complex
    Convert text representation to a Complex.
    Convert Cartesian to polar
    static Complex
    pow(double base, Complex exponent)
    Returns the base raised to the power of the Complex exponent.
    static Complex
    pow(Complex base, double exponent)
     
    static Complex
    pow(Complex base, Complex exponent)
    Returns the Complex base raised to the power of the Complex exponent.
    power(double exponent)
    Returns the value of this complex number raised to the power of a real component (in double precision).
    power(Complex exponent)
    Returns the value of this complex number raised to the power of a complex exponent
    double
    re()
    Returns the real component of this object
    void
    set(double re_in, double im_in)
    Sets the real and imaginary values of the object.
    void
    Copies the values from the parameter object to this object
    void
    setIm(double im_in)
    Sets the imaginary component of the object
    void
    setRe(double re_in)
    Sets the real component of the object
    sin()
    Returns the sine of this complex number.
     
    Calculates the square root of this object.
    subtract(double d)
    Subtracts the double d from the complex number Returns the subtraction of complex z from a complex number
    Returns the subtraction of complex z from a complex number
    tan()
    Returns the tangent of this complex number.
     
    Returns the value of this complex number as a string in the format:

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • Complex

      public Complex()
      Default constructor.
    • Complex

      public Complex(double re_in)
      Constructor from a single double value. The complex number is initialized with the real component equal to the parameter, and the imaginary component equal to zero.
      Parameters:
      re_in -
    • Complex

      public Complex(Number re_in)
      Construct from a Number. This constructor uses the doubleValue() method of the parameter to initialize the real component of the complex number. The imaginary component is initialized to zero.
      Parameters:
      re_in -
    • Complex

      public Complex(Complex z)
      Copy constructor
      Parameters:
      z -
    • Complex

      public Complex(double re_in, double im_in)
      Initialize the real and imaginary components to the values given by the parameters.
      Parameters:
      re_in -
      im_in -
  • Method Details

    • polar

      public Complex polar()
      Convert Cartesian to polar
    • cartesian

      public Complex cartesian()
      Convert polar to Cartesian
    • re

      public double re()
      Returns the real component of this object
    • im

      public double im()
      Returns the imaginary component of this object
    • set

      public void set(Complex z)
      Copies the values from the parameter object to this object
    • set

      public void set(double re_in, double im_in)
      Sets the real and imaginary values of the object.
    • setRe

      public void setRe(double re_in)
      Sets the real component of the object
    • setIm

      public void setIm(double im_in)
      Sets the imaginary component of the object
    • equals

      public boolean equals(Complex b, double tolerance)
      Compares this object with the Complex number given as parameter
      b
      . The
      tolerance
      parameter is the radius within which the
      b
      number must lie for the two complex numbers to be considered equal.
      Returns:
      true
      if the complex number are considered equal,
      false
      otherwise.
    • toString

      public String toString()
      Returns the value of this complex number as a string in the format:
      (real, imaginary)
      .
      Overrides:
      toString in class Object
    • abs

      public double abs()
      Returns the absolute value of the complex number.
    • abs2

      public double abs2()
      Returns the square of the absolute value (re*re+im*im).
    • mag

      public double mag()
      Returns the magnitude of the complex number
    • arg

      public double arg()
      Returns the argument of this complex number (Math.atan2(re,im))
    • parseComplex

      public static Complex parseComplex(String s)
      Convert text representation to a Complex. input format (real_double,imaginary_double)
    • add

      public Complex add(Complex z)
      Returns the sum of two complex numbers
    • add

      public Complex add(double d)
      Returns the sum of a complex number and a double
    • subtract

      public Complex subtract(Complex z)
      Returns the subtraction of complex z from a complex number
    • subtract

      public Complex subtract(double d)
      Subtracts the double d from the complex number Returns the subtraction of complex z from a complex number
    • neg

      public Complex neg()
      Returns the negative value of this complex number.
    • mul

      public Complex mul(double b)
      Multiplies the complex number with a double value.
      Returns:
      The result of the multiplication
    • mul

      public Complex mul(Complex b)
      Multiplies the complex number with another complex value.
      Returns:
      The result of the multiplication
    • div

      public Complex div(Complex b)
      Returns the result of dividing this complex number by the parameter.
    • div

      public Complex div(double d)
      Devide the complex number by a double value.
      Returns:
      The result of the division
    • invert

      public Complex invert()
    • conjugate

      public Complex conjugate()
      Conjugats the complex number
    • power

      public Complex power(double exponent)
      Returns the value of this complex number raised to the power of a real component (in double precision).

      This method considers special cases where a simpler algorithm would return "ugly" results.
      For example when the expression (-1e40)^0.5 is evaluated without considering the special case, the argument of the base is the double number closest to pi. When sin and cos are used for the final evaluation of the result, the slight difference of the argument from pi causes a non-zero value for the real component of the result. Because the value of the base is so high, the error is magnified.Although the error is normal for floating point calculations, the consideration of commonly occurring special cases improves the accuracy and esthetics of the results.

      If you know a more elegant way to solve this problem, please let me know at nathanfunk@hotmail.com .

    • cart

      public static Complex cart(double re, double im)
      Returns a Complex from real and imaginary parts.
    • power

      public Complex power(Complex exponent)
      Returns the value of this complex number raised to the power of a complex exponent
    • pow

      public static Complex pow(Complex base, double exponent)
    • pow

      public static Complex pow(double base, Complex exponent)
      Returns the base raised to the power of the Complex exponent.
    • pow

      public static Complex pow(Complex base, Complex exponent)
      Returns the Complex base raised to the power of the Complex exponent.
    • exp

      public Complex exp()
      Returns e to the power of the complex number
    • log

      public Complex log()
      Returns the logarithm of this complex number.
    • sqrt

      public Complex sqrt()
      Calculates the square root of this object.
    • sin

      public Complex sin()
      Returns the sine of this complex number.
    • cos

      public Complex cos()
      Returns the cosine of this complex number.
    • tan

      public Complex tan()
      Returns the tangent of this complex number.
    • asin

      public Complex asin()
    • acos

      public Complex acos()
    • atan

      public Complex atan()
    • sinh

      public Complex sinh()
    • cosh

      public Complex cosh()
    • tanh

      public Complex tanh()
    • asinh

      public Complex asinh()
    • acosh

      public Complex acosh()
    • atanh

      public Complex atanh()
    • isInfinite

      public boolean isInfinite()
      Returns true if either the real or imaginary component of this Complex is an infinite value.

      Returns:
      true if either component of the Complex object is infinite; false, otherwise.

    • isNaN

      public boolean isNaN()
      Returns true if either the real or imaginary component of this Complex is a Not-a-Number (NaN) value.

      Returns:
      true if either component of the Complex object is NaN; false, otherwise.