org.apfloat
Class Apcomplex

java.lang.Object
  |
  +--java.lang.Number
        |
        +--org.apfloat.Apcomplex
All Implemented Interfaces:
java.io.Serializable
Direct Known Subclasses:
Apfloat

public class Apcomplex
extends java.lang.Number
implements java.io.Serializable

Arbitrary precision complex number class. An apcomplex consists of a real and imaginary part of type Apfloat.

Note that although the Apcomplex class extends Number, the methods inherited from Number return the value of the real part of the complex number. Thus they are more meaningful for the Apfloat class and its subclasses.

See Also:
Apfloat, Serialized Form

Field Summary
static long DEFAULT
          Default precision.
static Apcomplex I
          Imaginary unit.
static long INFINITE
          Infinite precision or scale.
static Apint ONE
          Constant for one.
static Apint ZERO
          Constant for zero.
 
Constructor Summary
protected Apcomplex()
          Default constructor.
  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.
  Apcomplex(java.io.PushbackReader in)
          Reads an apcomplex from a reader.
  Apcomplex(java.lang.String value)
          Constructs an apcomplex from a string.
 
Method Summary
 Apcomplex add(Apcomplex z)
          Adds two apcomplex numbers.
 byte byteValue()
          Returns the value of the this number as a byte.
 Apcomplex conj()
          Returns the complex conjugate of this apcomplex.
 Apcomplex divide(Apcomplex z)
          Divides two apcomplex numbers.
 double doubleValue()
          Returns the value of the this number as a double.
 long equalDigits(Apcomplex z)
          Computes number of equal digits.
 boolean equals(java.lang.Object obj)
          Compares this object to the specified object.
 float floatValue()
          Returns the value of the this number as a float.
 int hashCode()
          Returns a hash code for this apcomplex.
 Apfloat imag()
          Returns the imaginary part of this apcomplex.
 int intValue()
          Returns the value of the this number as an int.
 long longValue()
          Returns the value of the this number as a long.
 Apcomplex multiply(Apcomplex z)
          Multiplies two apcomplex numbers.
 long precision()
          Returns the precision of this apcomplex.
 int radix()
          Radix of this apcomplex.
 Apfloat real()
          Returns the real part of this apcomplex.
 long scale()
          Returns the scale of this apcomplex.
 short shortValue()
          Returns the value of the this number as a short.
 Apcomplex subtract(Apcomplex z)
          Subtracts two apcomplex numbers.
 java.lang.String toString()
          Returns a string representation of this apcomplex.
 java.lang.String toString(boolean pretty)
          Returns a string representation of this apcomplex.
 void writeTo(java.io.Writer out)
          Write a string representation of this apcomplex to a Writer.
 void writeTo(java.io.Writer out, boolean pretty)
          Write a string representation of this apcomplex to a Writer.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

ZERO

public static final Apint ZERO
Constant for zero. It is safe to use ZERO in 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 use ONE in 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 use I in 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
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,
                 ApfloatRuntimeException
Constructs an apcomplex from a string.

The input must be of one of the formats

realPart
"(" [whitespace] realPart [whitespace] ")"
"(" [whitespace] realPart [whitespace] "," [whitespace] imaginaryPart [whitespace] ")"

Throws:
java.lang.NumberFormatException - If the number is invalid.
ApfloatRuntimeException

Apcomplex

public Apcomplex(java.io.PushbackReader in)
          throws java.io.IOException,
                 java.lang.NumberFormatException,
                 ApfloatRuntimeException
Reads an apcomplex from a reader. The constructor stops reading at the first character it doesn't understand. The reader must thus be a PushbackReader so 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 is x + i y.
ApfloatRuntimeException

precision

public long precision()
               throws ApfloatRuntimeException
Returns the precision of this apcomplex.

Returns:
The precision of this apcomplex in number of digits of the radix in which it's presented.
ApfloatRuntimeException

scale

public long scale()
           throws ApfloatRuntimeException
Returns 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 apfloat in number of digits of the radix in which it's presented.
ApfloatRuntimeException
See Also:
Apfloat.scale()

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.
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.
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.
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 a double. Only takes the real part of this number.

Specified by:
doubleValue in class java.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 a float. Only takes the real part of this number.

Specified by:
floatValue in class java.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 a byte. Only takes the real part of this number.

Overrides:
byteValue in class java.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 a short. Only takes the real part of this number.

Overrides:
shortValue in class java.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 an int. Only takes the real part of this number.

Specified by:
intValue in class java.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 a long. Only takes the real part of this number.

Specified by:
longValue in class java.lang.Number
Returns:
The numeric value represented by this object after conversion to type long.
See Also:
Apfloat.longValue()

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.
ApfloatRuntimeException

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:
equals in class java.lang.Object
Parameters:
obj - The object to compare with.
Returns:
true if the objects are equal; false otherwise.

hashCode

public int hashCode()
Returns a hash code for this apcomplex.

Overrides:
hashCode in class java.lang.Object
Returns:
The hash code value for this object.

toString

public java.lang.String toString()
Returns a string representation of this apcomplex.

Overrides:
toString in class java.lang.Object
Returns:
A string representing this object.

toString

public java.lang.String toString(boolean pretty)
                          throws ApfloatRuntimeException
Returns a string representation of this apcomplex.

Parameters:
pretty - true to use a fixed-point notation, false to use an exponential notation.
Returns:
A string representing this object.
ApfloatRuntimeException

writeTo

public void writeTo(java.io.Writer out)
             throws java.io.IOException,
                    ApfloatRuntimeException
Write a string representation of this apcomplex to a Writer.

Parameters:
out - The output Writer.
java.io.IOException
ApfloatRuntimeException

writeTo

public void writeTo(java.io.Writer out,
                    boolean pretty)
             throws java.io.IOException,
                    ApfloatRuntimeException
Write a string representation of this apcomplex to a Writer.

Parameters:
out - The output Writer.
pretty - true to use a fixed-point notation, false to use an exponential notation.
Throws:
java.io.IOException - In case of I/O error writing to the stream.
ApfloatRuntimeException