Package linalg.complex_number
Class CNumber
java.lang.Object
linalg.complex_number.CNumber
This class allows for the creation of complex and real numbers and provides
methods for computation with those numbers.
-
Field Summary
Modifier and TypeFieldDescriptionstatic 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))^21023static 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
-
Method Summary
Modifier and TypeMethodDescriptionstatic CNumber
Computes absolute value of a number.static CNumber
Performs complex addition of two numbers.static CNumber
Performs complex addition of two or more numbers.static CNumber
Computes the additive inverse of a Number.static CNumber[]
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
compareToReal(CNumber b)
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)
boolean
Checks for equivalence between two complex numbersstatic CNumber
exp(double x)
static CNumber
float
Note: This method may result in loss of accuracyfloat
Note: This method may result in loss of accuracydouble
double
getReal()
int
Note: This method may result in loss of accuracyint
intValue()
Note: This method may result in loss of accuracystatic CNumber
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
isInt()
Checks if a give number is a real integer.boolean
isReal()
Checks if given number has no imaginary part.static int
long
Note: This method may result in loss of accuracylong
Note: This method may result in loss of accuracydouble
mag()
Computes the magnitude of a complex number (The numbers distance from the origin in the complex plane).static CNumber
Computes value with greatest magnitude amongst all passed values.static CNumber
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
Performs complex exponentiation of two numbers.static CNumber
Performs complex exponentiation of two numbers.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.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
random()
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
Rounds number to specified number of decimal places.static CNumber
The complex signum function.static CNumber
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.toString()
-
Field Details
-
re
public double rere is real portion of complex number. im is imaginary portion of complex number. -
im
public double imre is real portion of complex number. im is imaginary portion of complex number. -
ONE
The number 1. -
NEGATIVE_ONE
The number -1. -
ZERO
The number 0. -
IMAGINARY_UNIT
The imaginary unit Defined as i^2=-1 => i=(-1)^(1/2). -
PI
The double value closer than any other to the mathmatical constant pi = 3.14159... -
E
The double value closer than any other to Euler's Constant e = 2.71828... -
MAX_VALUE
A real number holding the largest positive finite value of type double, (2-2^(-52))^21023 -
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
-
CNumber
-
-
Method Details
-
randn
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. Ifcomplex
true, a random real and imaginary component are chosen and a complex number is returned.
-
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
Generates a random real number.- Returns:
- random real number
-
random
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
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 numbermax
- - maximum value for random numbermagnitude_flag
- - optional flag to indicate if the Number should be real or complex.- Returns:
- random real or complex number between min and max.
-
conjugate
Computes and returns complex conjugate of a complex number. For example the complex conjugate ofa+bi
isa-bi
.- Parameters:
a
- - complex number- Returns:
- complex conjugate of
a
-
add
Performs complex addition of two numbers.- Parameters:
a
- - complex numberb
- - complex number- Returns:
- sum of a and b. e.g. a+b
-
add
Performs complex addition of two or more numbers.- Parameters:
a
- - complex numberb
- - complex numbervalues
- - Optional list of additional values to sum- Returns:
- Sum of complex numbers.
-
subtract
Performs complex subtraction of two numbers.- Parameters:
a
- - complex numberb
- - complex number- Returns:
- difference of a and b. e.g. a-b
-
multiply
Performs complex multiplication of two numbers.- Parameters:
a
- - complex numberb
- - complex number- Returns:
- product of a and b. e.g. a*b
-
divide
Performs complex division of two numbers.- Parameters:
a
- - complex numberb
- - complex number- Returns:
- Quotient of a and b. e.g. a/b
-
abs
Computes absolute value of a number. Note: this will Always result in a non-negative real number.
This method is the same asmag()
- Parameters:
value
- - value to take absolute value of.- Returns:
- The absolute value of the number.
-
inv
Computes the multiplicative inverse of a Number. For example, given a number n, the inverse is 1/n. This is equivalent toNumber.divide(Number.ONE, value);
- Parameters:
value
- - Number to take inverse of- Returns:
- - Inverse of value
-
addInv
Computes the additive inverse of a Number. For example, given a number n, the inverse is -n. This is equivalent toNumber.subtract(Number.ZERO, value);
- Parameters:
value
- - Number to take inverse of- Returns:
- - Inverse of value
-
pow
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 expressionexponenent
- - exponent of exponential expression- Returns:
- Result of the exponential expression
base^exponenent
-
pow
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 expressionexponent
- - exponent of exponential expression- Returns:
- Result of the exponential expression
base^exp
-
exp
- Parameters:
x
- - exponent- Returns:
- e^x
-
exp
- Parameters:
x
- - exponent- Returns:
- e^x
-
sqrt
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, seeallSqrt(CNumber value)
- Parameters:
value
- - value to take square root of.- Returns:
- Returns the square root of value.
-
allSqrt
Computes all possible square roots of a number. If you would only like the principle square root then seesqrt(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
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
Checks for equivalence between two complex numbers -
equals
public boolean equals(double b)- Parameters:
b
- - Number to check equivalence to- Returns:
- True if the two numbers are equivalent. Otherwise, returns false.
-
round
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
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 ofb
, then this method will return a negative number.
- If the magnitude of this number is greater than that ofb
, then this method will return a positive number.
-
compareToReal
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 ofb
, then this method will return a negative number.
- If the real value of this number is greator than that ofb
, 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 ofb
, then this method will return a negative number.
- If the real value of this number is greater than that ofb
, 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 asabs(CNumber)
- Returns:
- magnitude of complex number
-
max
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
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
- Parameters:
a
- - a number- Returns:
- The length of the string representation of the number
-
toString
-