Class CNumber

java.lang.Object
linalg.complex_number.CNumber

public class CNumber extends Object
This class allows for the creation of complex and real numbers and provides methods for computation with those numbers.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static CNumber
    The double value closer than any other to Euler's Constant e = 2.71828...
    double
    re is real portion of complex number.
    static CNumber
    The imaginary unit Defined as i^2=-1 => i=(-1)^(1/2).
    static CNumber
    A real number holding the largest positive finite value of type double, (2-2^(-52))^21023
    static CNumber
    A real number holding the smallest possible double value, 2^(-1074)
    static CNumber
    The number -1.
    static CNumber
    The number 1.
    static CNumber
    The double value closer than any other to the mathmatical constant pi = 3.14159...
    double
    re is real portion of complex number.
    static CNumber
    The number 0.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
    CNumber​(double real)
     
    CNumber​(double real, double imaginary)
     
    CNumber​(String num)
     
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static CNumber
    abs​(CNumber value)
    Computes absolute value of a number.
    static CNumber
    add​(CNumber a, CNumber b)
    Performs complex addition of two numbers.
    static CNumber
    add​(CNumber a, CNumber b, CNumber... values)
    Performs complex addition of two or more numbers.
    static CNumber
    addInv​(CNumber value)
    Computes the additive inverse of a Number.
    static CNumber[]
    allSqrt​(CNumber value)
    Computes all possible square roots of a number.
    int
    Compares the size of two complex numbers (magnitudes).
    int
    compareToReal​(double b)
    Compares the real value of two numbers.
    int
    Compares the real value of two numbers.
    static CNumber
    Computes and returns complex conjugate of a complex number.
    static CNumber
    Performs complex division of two numbers.
    double
     
    double
     
    boolean
    equals​(double b)
    Checks for equivalence between two complex numbers

    Also see equals(Object)
    boolean
    equals​(Object b)
    Checks for equivalence between two complex numbers
    static CNumber
    exp​(double x)
     
    static CNumber
    exp​(CNumber x)
     
    float
    Note: This method may result in loss of accuracy
    float
    Note: This method may result in loss of accuracy
    double
     
    double
     
    int
    Note: This method may result in loss of accuracy
    int
    Note: This method may result in loss of accuracy
    static CNumber
    inv​(CNumber value)
    Computes the multiplicative inverse of a Number.
    boolean
    Checks if given number has non-zero imaginary part.
    boolean
    Checks if given number has no real part.
    boolean
    Checks if a give number is a real integer.
    boolean
    Checks if given number has no imaginary part.
    static int
     
    long
    Note: This method may result in loss of accuracy
    long
    Note: This method may result in loss of accuracy
    double
    mag()
    Computes the magnitude of a complex number (The numbers distance from the origin in the complex plane).
    static CNumber
    max​(CNumber... values)
    Computes value with greatest magnitude amongst all passed values.
    static CNumber
    min​(CNumber... values)
    Computes value with smallest magnitude amongst all passed values.
    static CNumber
    Performs complex multiplication of two numbers.
    boolean
    nearZero​(double tol)
    Checks if a number is near zero.
    static CNumber
    pow​(CNumber base, int exponenent)
    Performs complex exponentiation of two numbers.
    static CNumber
    pow​(CNumber base, CNumber exponent)
    Performs complex exponentiation of two numbers.
    static CNumber
    Generates a random real a where a is a normaly distributed psedorandom number with a mean of zero and standard deviation of one.
    static CNumber
    randn​(boolean complex)
    Generates a random real or complex number a, or a+bi where a and b are normaly distributed psedorandom numbers with a mean of zero and standard deviation of one.
    static CNumber
    Generates a random real number.
    static CNumber
    random​(double mag)
    Generates a random complex number with given magnitude.
    static CNumber
    random​(double min, double max, boolean... magnitude_flag)
    Generates a random number between min and max.
    static CNumber
    round​(CNumber n, int decimals)
    Rounds number to specified number of decimal places.
    static CNumber
    sign​(CNumber value)
    The complex signum function.
    static CNumber
    sqrt​(CNumber value)
    Computes the square root of a number.

    Note: This method only returns the principle square root (the positive square root or square root with positive real part).
    static CNumber
    Performs complex subtraction of two numbers.
     

    Methods inherited from class java.lang.Object

    getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • re

      public double re
      re is real portion of complex number. im is imaginary portion of complex number.
    • im

      public double im
      re is real portion of complex number. im is imaginary portion of complex number.
    • ONE

      public static final CNumber ONE
      The number 1.
    • NEGATIVE_ONE

      public static final CNumber NEGATIVE_ONE
      The number -1.
    • ZERO

      public static final CNumber ZERO
      The number 0.
    • IMAGINARY_UNIT

      public static final CNumber IMAGINARY_UNIT
      The imaginary unit Defined as i^2=-1 => i=(-1)^(1/2).
    • PI

      public static final CNumber PI
      The double value closer than any other to the mathmatical constant pi = 3.14159...
    • E

      public static final CNumber E
      The double value closer than any other to Euler's Constant e = 2.71828...
    • MAX_VALUE

      public static final CNumber MAX_VALUE
      A real number holding the largest positive finite value of type double, (2-2^(-52))^21023
    • MIN_VALUE

      public static final CNumber MIN_VALUE
      A real number holding the smallest possible double value, 2^(-1074)
  • Constructor Details

    • CNumber

      public CNumber()
    • CNumber

      public CNumber(double real)
    • CNumber

      public CNumber(double real, double imaginary)
    • CNumber

      public CNumber(String num)
    • CNumber

      public CNumber(CNumber n)
  • Method Details

    • randn

      public static CNumber randn(boolean complex)
      Generates a random real or complex number a, or a+bi where a and b are normaly distributed psedorandom numbers with a mean of zero and standard deviation of one.
      Parameters:
      complex - - selects if a real or complex number should be selected..
      Returns:
      If complex false, a random real number is returned. If complex true, a random real and imaginary component are chosen and a complex number is returned.
    • randn

      public static CNumber randn()
      Generates a random real a where a is a normaly distributed psedorandom number with a mean of zero and standard deviation of one.
      Returns:
      A random real number from a standard normal distribution.
    • random

      public static CNumber random()
      Generates a random real number.
      Returns:
      random real number
    • random

      public static CNumber random(double mag)
      Generates a random complex number with given magnitude. Note: the magnitude must be a non-negative real number.
      Parameters:
      mag - - magnitude
      Returns:
      random complex number with specified magnitude.
    • random

      public static CNumber random(double min, double max, boolean... magnitude_flag)
      Generates a random number between min and max. If magnitude_flag is passed a true, then a random complex number with magnitude between min and max (where min and max are non-negative values) is generated. If magnitude_flag is passed a false, then a random real value between min and max is generated. If no magnitude_flag is passed, then it is treated as false.
      Parameters:
      min - - minimum value for random number
      max - - maximum value for random number
      magnitude_flag - - optional flag to indicate if the Number should be real or complex.
      Returns:
      random real or complex number between min and max.
    • conjugate

      public static CNumber conjugate(CNumber a)
      Computes and returns complex conjugate of a complex number. For example the complex conjugate of a+bi is a-bi.
      Parameters:
      a - - complex number
      Returns:
      complex conjugate of a
    • add

      public static CNumber add(CNumber a, CNumber b)
      Performs complex addition of two numbers.
      Parameters:
      a - - complex number
      b - - complex number
      Returns:
      sum of a and b. e.g. a+b
    • add

      public static CNumber add(CNumber a, CNumber b, CNumber... values)
      Performs complex addition of two or more numbers.
      Parameters:
      a - - complex number
      b - - complex number
      values - - Optional list of additional values to sum
      Returns:
      Sum of complex numbers.
    • subtract

      public static CNumber subtract(CNumber a, CNumber b)
      Performs complex subtraction of two numbers.
      Parameters:
      a - - complex number
      b - - complex number
      Returns:
      difference of a and b. e.g. a-b
    • multiply

      public static CNumber multiply(CNumber a, CNumber b)
      Performs complex multiplication of two numbers.
      Parameters:
      a - - complex number
      b - - complex number
      Returns:
      product of a and b. e.g. a*b
    • divide

      public static CNumber divide(CNumber a, CNumber b)
      Performs complex division of two numbers.
      Parameters:
      a - - complex number
      b - - complex number
      Returns:
      Quotient of a and b. e.g. a/b
    • abs

      public static CNumber abs(CNumber value)
      Computes absolute value of a number. Note: this will Always result in a non-negative real number.
      This method is the same as mag()
      Parameters:
      value - - value to take absolute value of.
      Returns:
      The absolute value of the number.
    • inv

      public static CNumber inv(CNumber value)
      Computes the multiplicative inverse of a Number. For example, given a number n, the inverse is 1/n. This is equivalent to Number.divide(Number.ONE, value);
      Parameters:
      value - - Number to take inverse of
      Returns:
      - Inverse of value
    • addInv

      public static CNumber addInv(CNumber value)
      Computes the additive inverse of a Number. For example, given a number n, the inverse is -n. This is equivalent to Number.subtract(Number.ZERO, value);
      Parameters:
      value - - Number to take inverse of
      Returns:
      - Inverse of value
    • pow

      public static CNumber pow(CNumber base, int exponenent)
      Performs complex exponentiation of two numbers. Note: base can be any complex number. TODO: Extend this using to complex numbers using Talyor series expansion of e^x. (May need a new method exponenent())
      Parameters:
      base - - base of exponential expression
      exponenent - - exponent of exponential expression
      Returns:
      Result of the exponential expression base^exponenent
    • pow

      public static CNumber pow(CNumber base, CNumber exponent)
      Performs complex exponentiation of two numbers. Note: The exponent, must be a real integer. TODO: Extend this using to complex numbers using Talyor series expansion of e^x. (May need a new method exp())
      Parameters:
      base - - base of exponential expression
      exponent - - exponent of exponential expression
      Returns:
      Result of the exponential expression base^exp
    • exp

      public static CNumber exp(double x)
      Parameters:
      x - - exponent
      Returns:
      e^x
    • exp

      public static CNumber exp(CNumber x)
      Parameters:
      x - - exponent
      Returns:
      e^x
    • sqrt

      public static CNumber sqrt(CNumber value)
      Computes the square root of a number.

      Note: This method only returns the principle square root (the positive square root or square root with positive real part).
      If you wish to see all square roots, see allSqrt(CNumber value)
      Parameters:
      value - - value to take square root of.
      Returns:
      Returns the square root of value.
    • allSqrt

      public static CNumber[] allSqrt(CNumber value)
      Computes all possible square roots of a number. If you would only like the principle square root then see sqrt(CNumber value)
      Parameters:
      value - - value to take square root of.
      Returns:
      Returns all square roots of value (both positive and negative) in an array.
    • sign

      public static CNumber sign(CNumber value)
      The complex signum function.
      Parameters:
      value - - Value to evaluet in sign function.
      Returns:
      If the number is zero then this function returns zero. Otherwise, returns the number divided by its magnitude.
    • equals

      public boolean equals(Object b)
      Checks for equivalence between two complex numbers
      Overrides:
      equals in class Object
      Parameters:
      b - - Number to check equivalence to
      Returns:
      True if the two numbers are equivalent. Otherwise, returns false.
    • equals

      public boolean equals(double b)
      Checks for equivalence between two complex numbers

      Also see equals(Object)
      Parameters:
      b - - Number to check equivalence to
      Returns:
      True if the two numbers are equivalent. Otherwise, returns false.
    • round

      public static CNumber round(CNumber n, int decimals)
      Rounds number to specified number of decimal places. If the number is complex, both the real and imaginary parts will be rounded.
      Parameters:
      n - - Number to round.
      decimals - - Number of decimals to round to.
      Returns:
      The number n rounded to the specified number of decimals.
    • nearZero

      public boolean nearZero(double tol)
      Checks if a number is near zero.
      Parameters:
      tol - - tolerance of how close to zero is considered "near".
      Returns:
      Returns true if magnitude of number is less than or equal to tol of zero. Otherwise, returns false.
    • compareTo

      public int compareTo(CNumber b)
      Compares the size of two complex numbers (magnitudes).
      Parameters:
      b - - Number to compare
      Returns:
      - If the magnitude of this number is equal to that of b, then this method will return 0.
      - If the magnitude of this number is less than that of b, then this method will return a negative number.
      - If the magnitude of this number is greater than that of b, then this method will return a positive number.
    • compareToReal

      public int compareToReal(CNumber b)
      Compares the real value of two numbers.
      Parameters:
      b - - Number to compare
      Returns:
      - If the real value of this number is equal to that of b, then this method will return 0.
      - If the real value of this number is less than that of b, then this method will return a negative number.
      - If the real value of this number is greator than that of b, then this method will return a positive number.
    • compareToReal

      public int compareToReal(double b)
      Compares the real value of two numbers.
      Parameters:
      b - - Number to compare
      Returns:
      - If the real value of this number is equal to that of b, then this method will return 0.
      - If the real value of this number is less than that of b, then this method will return a negative number.
      - If the real value of this number is greater than that of b, then this method will return a positive number.
    • mag

      public double mag()
      Computes the magnitude of a complex number (The numbers distance from the origin in the complex plane).
      This method is the same as abs(CNumber)
      Returns:
      magnitude of complex number
    • max

      public static CNumber max(CNumber... values)
      Computes value with greatest magnitude amongst all passed values.
      Parameters:
      values - - a set of values of which to compute the maximum. Each item may be passed as its own variable or as an array. For example, given the CNumbers a, b, c and an array of CNumbers vals, this method may be called in either of the following ways.
      - CNumber.max(a, b, c)
      - CNumber.max(vals)
      Returns:
      Returns value with maximum magnitude amongst values. If two values tie for largest, the first encountered will be returned.
    • min

      public static CNumber min(CNumber... values)
      Computes value with smallest magnitude amongst all passed values.
      Parameters:
      values - - a set of values of which to compute the minimum. Each item may be passed as its own variable or as an array. For example, given the CNumbers a, b, c and an array of CNumbers vals, this method may be called in either of the following ways.
      - CNumber.max(a, b, c)
      - CNumber.max(vals)
      Returns:
      Returns value with minimum magnitude amounts values. If two values tie for smallest, the first encountered will be returned.
    • getReal

      public double getReal()
      Returns:
      Real part of number
    • getImaginary

      public double getImaginary()
      Returns:
      Complex part of number
    • isReal

      public boolean isReal()
      Checks if given number has no imaginary part.
      Returns:
      true if given Number has no imaginary part. Otherwise, returns false.
    • isComplex

      public boolean isComplex()
      Checks if given number has non-zero imaginary part.
      Returns:
      true if given Number has non-zero imaginary part. Otherwise, returns false.
    • isImaginary

      public boolean isImaginary()
      Checks if given number has no real part.
      Returns:
      true if given Number has no real part and non-zero imaginary part. Otherwise, returns false.
    • isInt

      public boolean isInt()
      Checks if a give number is a real integer.
      Returns:
      true if number is a real integer. Otherwise, returns false.
    • doubleValue

      public double doubleValue()
      Returns:
      real part of given Number as double
    • floatValue

      public float floatValue()
      Note: This method may result in loss of accuracy
      Returns:
      real part of given Number as float
    • intValue

      public int intValue()
      Note: This method may result in loss of accuracy
      Returns:
      real part of given Number as int
    • longValue

      public long longValue()
      Note: This method may result in loss of accuracy
      Returns:
      real part of given Number as double
    • doubleImaginaryValue

      public double doubleImaginaryValue()
      Returns:
      imaginary part of given Number as double
    • floatImaginaryValue

      public float floatImaginaryValue()
      Note: This method may result in loss of accuracy
      Returns:
      imaginary part of given Number as float
    • intImaginaryValue

      public int intImaginaryValue()
      Note: This method may result in loss of accuracy
      Returns:
      imaginary part of given Number as int
    • longImaginaryValue

      public long longImaginaryValue()
      Note: This method may result in loss of accuracy
      Returns:
      imaginary part of given Number as long
    • length

      public static int length(CNumber a)
      Parameters:
      a - - a number
      Returns:
      The length of the string representation of the number
    • toString

      public String toString()
      Overrides:
      toString in class Object