Class Apcomplex
- java.lang.Object
-
- java.lang.Number
-
- org.apfloat.Apcomplex
-
- All Implemented Interfaces:
java.io.Serializable,java.util.Formattable
- Direct Known Subclasses:
Apfloat
public class Apcomplex extends java.lang.Number implements java.util.Formattable, java.io.SerializableArbitrary precision complex number class. An apcomplex consists of a real and imaginary part of typeApfloat.Note that although the Apcomplex class extends
Number, the methods inherited fromNumberreturn the value of the real part of the complex number. Thus they are more meaningful for theApfloatclass and its subclasses.- Version:
- 1.13.0
- See Also:
Apfloat,ApcomplexMath, Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static longDEFAULTDefault precision.(package private) static intEXTRA_PRECISIONExtra precision that is added in various apfloat internal operations to avoid round-off errors.static ApcomplexIImaginary unit.private Apfloatimagstatic longINFINITEInfinite precision or scale.static ApintONEConstant for one.(package private) static Apint[]ONESprivate Apfloatrealstatic java.util.Comparator<Apcomplex>REAL_ABS_IMAG_ORDERComparator for ordering by real part, and in case of a tie, by the absolute value of the imaginary part.static java.util.Comparator<Apcomplex>REAL_IMAG_ORDERComparator for ordering by real part, and in case of a tie, by imaginary part.private static longserialVersionUIDstatic ApintZEROConstant for zero.(package private) static Apint[]ZEROS
-
Constructor Summary
Constructors Modifier Constructor Description protectedApcomplex()Default constructor.Apcomplex(java.io.PushbackReader in)Reads an apcomplex from a reader.Apcomplex(java.lang.String value)Constructs an apcomplex from a string.Apcomplex(Apfloat real)Construct a real apcomplex whose imaginary part is zero.Apcomplex(Apfloat real, Apfloat imag)Construct an apcomplex with the specified real and imaginary part.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Apcomplexadd(Apcomplex z)Adds two apcomplex numbers.bytebyteValue()Returns the value of the this number as abyte.bytebyteValueExact()Returns the value of the this number as abyte, checking for lost information.Apcomplexconj()Returns the complex conjugate of this apcomplex.Apcomplexdivide(Apcomplex z)Divides two apcomplex numbers.doubledoubleValue()Returns the value of the this number as adouble.longequalDigits(Apcomplex z)Computes number of equal digits.booleanequals(java.lang.Object obj)Compares this object to the specified object.floatfloatValue()Returns the value of the this number as afloat.voidformatTo(java.util.Formatter formatter, int flags, int width, int precision)Formats the object using the provided formatter.inthashCode()Returns a hash code for this apcomplex.Apfloatimag()Returns the imaginary part of this apcomplex.intintValue()Returns the value of the this number as anint.intintValueExact()Returns the value of the this number as anint, checking for lost information.booleanisInteger()Returns if this number has an integer value.booleanisZero()Returns if this number is zero.longlongValue()Returns the value of the this number as along.longlongValueExact()Returns the value of the this number as along, checking for lost information.Apcomplexmultiply(Apcomplex z)Multiplies two apcomplex numbers.Apcomplexnegate()Negative value.longprecision()Returns the precision of this apcomplex.Apcomplexprecision(long precision)Returns an apcomplex with the same value as this apcomplex accurate to the specified precision.intradix()Radix of this apcomplex.Apfloatreal()Returns the real part of this apcomplex.longscale()Returns the scale of this apcomplex.shortshortValue()Returns the value of the this number as ashort.shortshortValueExact()Returns the value of the this number as ashort, checking for lost information.longsize()Returns the size of this apcomplex.Apcomplexsubtract(Apcomplex z)Subtracts two apcomplex numbers.booleantest(Apcomplex z)Tests two apcomplex numbers for equality.ApcomplextoRadix(int radix)Convert this apcomplex to the specified radix.java.lang.StringtoString()Returns a string representation of this apcomplex.java.lang.StringtoString(boolean pretty)Returns a string representation of this apcomplex.voidwriteTo(java.io.Writer out)Write a string representation of this apcomplex to aWriter.voidwriteTo(java.io.Writer out, boolean pretty)Write a string representation of this apcomplex to aWriter.
-
-
-
Field Detail
-
ZERO
public static final Apint ZERO
Constant for zero. It is safe to useZEROin all addition, subtraction, multiplication, division and comparison operations regardless of the radix used.
-
ONE
public static final Apint ONE
Constant for one. Note that this number is created using the initial default radix. It is safe to useONEin all multiplication, division and equality comparison operations regardless of the radix used. However, in subtraction and addition it only works with numbers in the same radix.
-
I
public static final Apcomplex I
Imaginary unit. That is,Apcomplex(ZERO, ONE). It is safe to useIin all multiplication, division and equality comparison operations regardless of the radix used. In addition and subtraction it only works with numbers in the same radix.
-
INFINITE
public static final long INFINITE
Infinite precision or scale. Can be used as the precision argument when constructing apfloats.- See Also:
- Constant Field Values
-
DEFAULT
public static final long DEFAULT
Default precision. Can be used as an argument when constructing apfloats.- See Also:
- Constant Field Values
-
REAL_IMAG_ORDER
public static final java.util.Comparator<Apcomplex> REAL_IMAG_ORDER
Comparator for ordering by real part, and in case of a tie, by imaginary part.
-
REAL_ABS_IMAG_ORDER
public static final java.util.Comparator<Apcomplex> REAL_ABS_IMAG_ORDER
Comparator for ordering by real part, and in case of a tie, by the absolute value of the imaginary part. If a tie persists, order by imaginary part.
-
EXTRA_PRECISION
static final int EXTRA_PRECISION
Extra precision that is added in various apfloat internal operations to avoid round-off errors.- See Also:
- Constant Field Values
-
ZEROS
static final Apint[] ZEROS
-
ONES
static final Apint[] ONES
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
real
private Apfloat real
-
imag
private Apfloat imag
-
-
Constructor Detail
-
Apcomplex
protected Apcomplex()
Default constructor. To be used only by subclasses that overload all needed methods.
-
Apcomplex
public Apcomplex(Apfloat real)
Construct a real apcomplex whose imaginary part is zero.- Parameters:
real- The real part of the number.
-
Apcomplex
public Apcomplex(Apfloat real, Apfloat imag) throws java.lang.IllegalArgumentException
Construct an apcomplex with the specified real and imaginary part.- Parameters:
real- The real part of the number.imag- The imaginary part of the number.- Throws:
java.lang.IllegalArgumentException- If the real part and imaginary part are not zero but have different radixes.
-
Apcomplex
public Apcomplex(java.lang.String value) throws java.lang.NumberFormatException, ApfloatRuntimeExceptionConstructs an apcomplex from a string.The input must be of one of the formats
realPart
"(" [whitespace] realPart [whitespace] ")"
"(" [whitespace] realPart [whitespace] "," [whitespace] imaginaryPart [whitespace] ")"- Parameters:
value- The input string.- Throws:
java.lang.NumberFormatException- If the number is invalid.ApfloatRuntimeException
-
Apcomplex
public Apcomplex(java.io.PushbackReader in) throws java.io.IOException, java.lang.NumberFormatException, ApfloatRuntimeExceptionReads an apcomplex from a reader. The constructor stops reading at the first character it doesn't understand. The reader must thus be aPushbackReaderso that the invalid character can be returned back to the stream.The input must be of one of the formats
realPart
"(" [whitespace] realPart [whitespace] ")"
"(" [whitespace] realPart [whitespace] "," [whitespace] imaginaryPart [whitespace] ")"- Parameters:
in- The input stream.- Throws:
java.io.IOException- In case of I/O error reading from the stream.java.lang.NumberFormatException- If the number is invalid.ApfloatRuntimeException
-
-
Method Detail
-
radix
public int radix()
Radix of this apcomplex.- Returns:
- Radix of this apcomplex.
-
real
public Apfloat real()
Returns the real part of this apcomplex.- Returns:
- The real part of this apcomplex.
-
imag
public Apfloat imag()
Returns the imaginary part of this apcomplex.- Returns:
- The imaginary part of this apcomplex.
-
conj
public Apcomplex conj() throws ApfloatRuntimeException
Returns the complex conjugate of this apcomplex.- Returns:
x - i y, where this apcomplex isx + i y.- Throws:
ApfloatRuntimeException
-
precision
public long precision() throws ApfloatRuntimeExceptionReturns the precision of this apcomplex.- Returns:
- The precision of this apcomplex in number of digits of the radix in which it's presented.
- Throws:
ApfloatRuntimeException
-
precision
public Apcomplex precision(long precision) throws java.lang.IllegalArgumentException, ApfloatRuntimeException
Returns an apcomplex with the same value as this apcomplex accurate to the specified precision.If the requested precision less than this number's current precision, the functionality is quite obvious: the precision is simply truncated, and e.g. comparison and equality checking will work as expected. Some rounding errors in e.g. addition and subtraction may still occur, as "invisible" trailing digits can remain in the number.
If the requested precision more than this number's current precision, the functionality is quite undefined: the digits up to this number's current precision are guaranteed to be the same, but the "new" digits are undefined: they may be zero, or they may be digits that have been previously discarded with a call to precision() with a smaller number of digits, or they may be something else, or any combination of these.
These limitations allow various performance optimizations to be made.
- Parameters:
precision- Precision of the new apcomplex.- Returns:
- An apcomplex with the specified precision and same value as this apcomplex.
- Throws:
java.lang.IllegalArgumentException- Ifprecisionis <= 0.ApfloatRuntimeException- Since:
- 1.2
-
scale
public long scale() throws ApfloatRuntimeExceptionReturns the scale of this apcomplex. The scale is the maximum of the scale of the real part and imaginary part.Zero has a scale of
-INFINITE.- Returns:
- The exponent of this apcomplex in number of digits of the radix in which it's presented.
- Throws:
ApfloatRuntimeException- See Also:
Apfloat.scale()
-
size
public long size() throws ApfloatRuntimeExceptionReturns the size of this apcomplex. The size is the maximum of the size of the real part and imaginary part.Zero has a size of
0.- Returns:
- The number of digits in this number, from the most significant digit to the least significant nonzero digit, in the radix in which it's presented.
- Throws:
ApfloatRuntimeException- Since:
- 1.6
- See Also:
Apfloat.size()
-
isZero
public boolean isZero() throws ApfloatRuntimeExceptionReturns if this number is zero.- Returns:
- If this number is zero.
- Throws:
ApfloatRuntimeException- Since:
- 1.13.0
-
isInteger
public boolean isInteger() throws ApfloatRuntimeExceptionReturns if this number has an integer value. Note that this does not necessarily mean that this object is an instance ofApint. Neither does it mean that the precision is infinite.A complex number needs to have a zero imaginary part to be an integer.
- Returns:
- If this number's value is an integer.
- Throws:
ApfloatRuntimeException- Since:
- 1.9.0
-
negate
public Apcomplex negate() throws ApfloatRuntimeException
Negative value.- Returns:
-this.- Throws:
ApfloatRuntimeException- Since:
- 1.1
-
add
public Apcomplex add(Apcomplex z) throws ApfloatRuntimeException
Adds two apcomplex numbers.- Parameters:
z- The number to be added to this number.- Returns:
this + z.- Throws:
ApfloatRuntimeException
-
subtract
public Apcomplex subtract(Apcomplex z) throws ApfloatRuntimeException
Subtracts two apcomplex numbers.- Parameters:
z- The number to be subtracted from this number.- Returns:
this - z.- Throws:
ApfloatRuntimeException
-
multiply
public Apcomplex multiply(Apcomplex z) throws ApfloatRuntimeException
Multiplies two apcomplex numbers.- Parameters:
z- The number to be multiplied by this number.- Returns:
this * z.- Throws:
ApfloatRuntimeException
-
divide
public Apcomplex divide(Apcomplex z) throws java.lang.ArithmeticException, ApfloatRuntimeException
Divides two apcomplex numbers.- Parameters:
z- The number by which this number is to be divided.- Returns:
this / z.- Throws:
java.lang.ArithmeticException- In case the divisor is zero.ApfloatRuntimeException
-
doubleValue
public double doubleValue()
Returns the value of the this number as adouble. Only takes the real part of this number.- Specified by:
doubleValuein classjava.lang.Number- Returns:
- The numeric value represented by this object after conversion to type
double. - See Also:
Apfloat.doubleValue()
-
floatValue
public float floatValue()
Returns the value of the this number as afloat. Only takes the real part of this number.- Specified by:
floatValuein classjava.lang.Number- Returns:
- The numeric value represented by this object after conversion to type
float. - See Also:
Apfloat.floatValue()
-
byteValue
public byte byteValue()
Returns the value of the this number as abyte. Only takes the real part of this number.- Overrides:
byteValuein classjava.lang.Number- Returns:
- The numeric value represented by this object after conversion to type
byte. - See Also:
Apfloat.byteValue()
-
shortValue
public short shortValue()
Returns the value of the this number as ashort. Only takes the real part of this number.- Overrides:
shortValuein classjava.lang.Number- Returns:
- The numeric value represented by this object after conversion to type
short. - See Also:
Apfloat.shortValue()
-
intValue
public int intValue()
Returns the value of the this number as anint. Only takes the real part of this number.- Specified by:
intValuein classjava.lang.Number- Returns:
- The numeric value represented by this object after conversion to type
int. - See Also:
Apfloat.intValue()
-
longValue
public long longValue()
Returns the value of the this number as along. Only takes the real part of this number.- Specified by:
longValuein classjava.lang.Number- Returns:
- The numeric value represented by this object after conversion to type
long. - See Also:
Apfloat.longValue()
-
byteValueExact
public byte byteValueExact() throws java.lang.ArithmeticExceptionReturns the value of the this number as abyte, checking for lost information. If the value of this number is out of the range of thebytetype, then anArithmeticExceptionis thrown.- Returns:
- The numeric value represented by this object after conversion to type
byte. - Throws:
java.lang.ArithmeticException- If the value ofthiswill not exactly fit in abyteor has a nonzero fractional part.- Since:
- 1.9.0
-
shortValueExact
public short shortValueExact() throws java.lang.ArithmeticExceptionReturns the value of the this number as ashort, checking for lost information. If the value of this number is out of the range of theshorttype, then anArithmeticExceptionis thrown.- Returns:
- The numeric value represented by this object after conversion to type
shortor has a nonzero fractional part. - Throws:
java.lang.ArithmeticException- If the value ofthiswill not exactly fit in ashort.- Since:
- 1.9.0
-
intValueExact
public int intValueExact() throws java.lang.ArithmeticExceptionReturns the value of the this number as anint, checking for lost information. If the value of this number is out of the range of theinttype, then anArithmeticExceptionis thrown.- Returns:
- The numeric value represented by this object after conversion to type
int. - Throws:
java.lang.ArithmeticException- If the value ofthiswill not exactly fit in anintor has a nonzero fractional part.- Since:
- 1.9.0
-
longValueExact
public long longValueExact() throws java.lang.ArithmeticExceptionReturns the value of the this number as along, checking for lost information. If the value of this number is out of the range of thelongtype, then anArithmeticExceptionis thrown.- Returns:
- The numeric value represented by this object after conversion to type
long. - Throws:
java.lang.ArithmeticException- If the value ofthiswill not exactly fit in alongor has a nonzero fractional part.- Since:
- 1.9.0
-
equalDigits
public long equalDigits(Apcomplex z) throws ApfloatRuntimeException
Computes number of equal digits.Compares the digits of the numbers starting from the most significant digits. The exponent and sign are taken into consideration, so if either one doesn't match, the numbers are considered to have zero equal digits.
For example, the numbers (12345, 123) and (123456, 12) have zero matching digits, and the numbers (12345, 12) and (12355, 13) have three matching digits.
- Parameters:
z- Number to compare with.- Returns:
- Number of matching digits in the radix in which the numbers are presented.
- Throws:
ApfloatRuntimeException
-
toRadix
public Apcomplex toRadix(int radix) throws java.lang.NumberFormatException, ApfloatRuntimeException
Convert this apcomplex to the specified radix.- Parameters:
radix- The radix.- Returns:
- This number in the specified radix.
- Throws:
java.lang.NumberFormatException- If the radix is invalid.ApfloatRuntimeException- Since:
- 1.2
-
equals
public boolean equals(java.lang.Object obj)
Compares this object to the specified object.Note: two apfloats are considered equal if they have an identical mantissa, but different precision.
- Overrides:
equalsin classjava.lang.Object- Parameters:
obj- The object to compare with.- Returns:
trueif the objects are equal;falseotherwise.
-
test
public boolean test(Apcomplex z) throws ApfloatRuntimeException
Tests two apcomplex numbers for equality. Returnsfalseif the numbers are definitely known to be not equal. Iftrueis returned, equality is unknown and should be verified by callingequals(Object). This method is usually significantly faster than callingequals(Object).- Parameters:
z- The number to test against.- Returns:
falseif the numbers are definitely not equal,trueif unknown.- Throws:
ApfloatRuntimeException- Since:
- 1.10.0
-
hashCode
public int hashCode()
Returns a hash code for this apcomplex.- Overrides:
hashCodein classjava.lang.Object- Returns:
- The hash code value for this object.
-
toString
public java.lang.String toString()
Returns a string representation of this apcomplex.- Overrides:
toStringin classjava.lang.Object- Returns:
- A string representing this object.
-
toString
public java.lang.String toString(boolean pretty) throws ApfloatRuntimeExceptionReturns a string representation of this apcomplex.- Parameters:
pretty-trueto use a fixed-point notation,falseto use an exponential notation.- Returns:
- A string representing this object.
- Throws:
ApfloatRuntimeException
-
writeTo
public void writeTo(java.io.Writer out) throws java.io.IOException, ApfloatRuntimeExceptionWrite a string representation of this apcomplex to aWriter.- Parameters:
out- The outputWriter.- Throws:
java.io.IOException- In case of I/O error writing to the stream.ApfloatRuntimeException
-
writeTo
public void writeTo(java.io.Writer out, boolean pretty) throws java.io.IOException, ApfloatRuntimeExceptionWrite a string representation of this apcomplex to aWriter.- Parameters:
out- The outputWriter.pretty-trueto use a fixed-point notation,falseto use an exponential notation.- Throws:
java.io.IOException- In case of I/O error writing to the stream.ApfloatRuntimeException
-
formatTo
public void formatTo(java.util.Formatter formatter, int flags, int width, int precision)Formats the object using the provided formatter.- Specified by:
formatToin interfacejava.util.Formattable- Parameters:
formatter- The formatter.flags- The flags to modify the output format.width- The minimum number of characters to be written to the output, or-1for no minimum.precision- The maximum number of characters to be written to the output, or-1for no maximum.- Since:
- 1.3
- See Also:
Apfloat.formatTo(Formatter,int,int,int)
-
-