Package org.apache.commons.math3.complex
Class ComplexFormat
- java.lang.Object
-
- org.apache.commons.math3.complex.ComplexFormat
-
public class ComplexFormat extends java.lang.ObjectFormats a Complex number in cartesian format "Re(c) + Im(c)i". 'i' can be replaced with 'j' (or anything else), and the number format for both real and imaginary parts can be configured.
-
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.StringDEFAULT_IMAGINARY_CHARACTERThe default imaginary character.private java.lang.StringimaginaryCharacterThe notation used to signify the imaginary part of the complex number.private java.text.NumberFormatimaginaryFormatThe format used for the imaginary part.private java.text.NumberFormatrealFormatThe format used for the real part.
-
Constructor Summary
Constructors Constructor Description ComplexFormat()Create an instance with the default imaginary character, 'i', and the default number format for both real and imaginary parts.ComplexFormat(java.lang.String imaginaryCharacter)Create an instance with a custom imaginary character, and the default number format for both real and imaginary parts.ComplexFormat(java.lang.String imaginaryCharacter, java.text.NumberFormat format)Create an instance with a custom imaginary character, and a custom number format for both real and imaginary parts.ComplexFormat(java.lang.String imaginaryCharacter, java.text.NumberFormat realFormat, java.text.NumberFormat imaginaryFormat)Create an instance with a custom imaginary character, a custom number format for the real part, and a custom number format for the imaginary part.ComplexFormat(java.text.NumberFormat format)Create an instance with a custom number format for both real and imaginary parts.ComplexFormat(java.text.NumberFormat realFormat, java.text.NumberFormat imaginaryFormat)Create an instance with a custom number format for the real part and a custom number format for the imaginary part.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.Stringformat(java.lang.Double c)This method callsformat(Object,StringBuffer,FieldPosition).java.lang.StringBufferformat(java.lang.Object obj, java.lang.StringBuffer toAppendTo, java.text.FieldPosition pos)Formats a object to produce a string.java.lang.Stringformat(Complex c)This method callsformat(Object,StringBuffer,FieldPosition).java.lang.StringBufferformat(Complex complex, java.lang.StringBuffer toAppendTo, java.text.FieldPosition pos)Formats aComplexobject to produce a string.private java.lang.StringBufferformatImaginary(double absIm, java.lang.StringBuffer toAppendTo, java.text.FieldPosition pos)Format the absolute value of the imaginary part.static java.util.Locale[]getAvailableLocales()Get the set of locales for which complex formats are available.java.lang.StringgetImaginaryCharacter()Access the imaginaryCharacter.java.text.NumberFormatgetImaginaryFormat()Access the imaginaryFormat.static ComplexFormatgetInstance()Returns the default complex format for the current locale.static ComplexFormatgetInstance(java.lang.String imaginaryCharacter, java.util.Locale locale)Returns the default complex format for the given locale.static ComplexFormatgetInstance(java.util.Locale locale)Returns the default complex format for the given locale.java.text.NumberFormatgetRealFormat()Access the realFormat.Complexparse(java.lang.String source)Parses a string to produce aComplexobject.Complexparse(java.lang.String source, java.text.ParsePosition pos)Parses a string to produce aComplexobject.
-
-
-
Field Detail
-
DEFAULT_IMAGINARY_CHARACTER
private static final java.lang.String DEFAULT_IMAGINARY_CHARACTER
The default imaginary character.- See Also:
- Constant Field Values
-
imaginaryCharacter
private final java.lang.String imaginaryCharacter
The notation used to signify the imaginary part of the complex number.
-
imaginaryFormat
private final java.text.NumberFormat imaginaryFormat
The format used for the imaginary part.
-
realFormat
private final java.text.NumberFormat realFormat
The format used for the real part.
-
-
Constructor Detail
-
ComplexFormat
public ComplexFormat()
Create an instance with the default imaginary character, 'i', and the default number format for both real and imaginary parts.
-
ComplexFormat
public ComplexFormat(java.text.NumberFormat format) throws NullArgumentExceptionCreate an instance with a custom number format for both real and imaginary parts.- Parameters:
format- the custom format for both real and imaginary parts.- Throws:
NullArgumentException- ifrealFormatisnull.
-
ComplexFormat
public ComplexFormat(java.text.NumberFormat realFormat, java.text.NumberFormat imaginaryFormat) throws NullArgumentExceptionCreate an instance with a custom number format for the real part and a custom number format for the imaginary part.- Parameters:
realFormat- the custom format for the real part.imaginaryFormat- the custom format for the imaginary part.- Throws:
NullArgumentException- ifimaginaryFormatisnull.NullArgumentException- ifrealFormatisnull.
-
ComplexFormat
public ComplexFormat(java.lang.String imaginaryCharacter) throws NullArgumentException, NoDataExceptionCreate an instance with a custom imaginary character, and the default number format for both real and imaginary parts.- Parameters:
imaginaryCharacter- The custom imaginary character.- Throws:
NullArgumentException- ifimaginaryCharacterisnull.NoDataException- ifimaginaryCharacteris an empty string.
-
ComplexFormat
public ComplexFormat(java.lang.String imaginaryCharacter, java.text.NumberFormat format) throws NullArgumentException, NoDataExceptionCreate an instance with a custom imaginary character, and a custom number format for both real and imaginary parts.- Parameters:
imaginaryCharacter- The custom imaginary character.format- the custom format for both real and imaginary parts.- Throws:
NullArgumentException- ifimaginaryCharacterisnull.NoDataException- ifimaginaryCharacteris an empty string.NullArgumentException- ifformatisnull.
-
ComplexFormat
public ComplexFormat(java.lang.String imaginaryCharacter, java.text.NumberFormat realFormat, java.text.NumberFormat imaginaryFormat) throws NullArgumentException, NoDataExceptionCreate an instance with a custom imaginary character, a custom number format for the real part, and a custom number format for the imaginary part.- Parameters:
imaginaryCharacter- The custom imaginary character.realFormat- the custom format for the real part.imaginaryFormat- the custom format for the imaginary part.- Throws:
NullArgumentException- ifimaginaryCharacterisnull.NoDataException- ifimaginaryCharacteris an empty string.NullArgumentException- ifimaginaryFormatisnull.NullArgumentException- ifrealFormatisnull.
-
-
Method Detail
-
getAvailableLocales
public static java.util.Locale[] getAvailableLocales()
Get the set of locales for which complex formats are available.This is the same set as the
NumberFormatset.- Returns:
- available complex format locales.
-
format
public java.lang.String format(Complex c)
This method callsformat(Object,StringBuffer,FieldPosition).- Parameters:
c- Complex object to format.- Returns:
- A formatted number in the form "Re(c) + Im(c)i".
-
format
public java.lang.String format(java.lang.Double c)
This method callsformat(Object,StringBuffer,FieldPosition).- Parameters:
c- Double object to format.- Returns:
- A formatted number.
-
format
public java.lang.StringBuffer format(Complex complex, java.lang.StringBuffer toAppendTo, java.text.FieldPosition pos)
Formats aComplexobject to produce a string.- Parameters:
complex- the object to format.toAppendTo- where the text is to be appendedpos- On input: an alignment field, if desired. On output: the offsets of the alignment field- Returns:
- the value passed in as toAppendTo.
-
formatImaginary
private java.lang.StringBuffer formatImaginary(double absIm, java.lang.StringBuffer toAppendTo, java.text.FieldPosition pos)Format the absolute value of the imaginary part.- Parameters:
absIm- Absolute value of the imaginary part of a complex number.toAppendTo- where the text is to be appended.pos- On input: an alignment field, if desired. On output: the offsets of the alignment field.- Returns:
- the value passed in as toAppendTo.
-
format
public java.lang.StringBuffer format(java.lang.Object obj, java.lang.StringBuffer toAppendTo, java.text.FieldPosition pos) throws MathIllegalArgumentExceptionFormats a object to produce a string.objmust be either aComplexobject or aNumberobject. Any other type of object will result in anIllegalArgumentExceptionbeing thrown.- Parameters:
obj- the object to format.toAppendTo- where the text is to be appendedpos- On input: an alignment field, if desired. On output: the offsets of the alignment field- Returns:
- the value passed in as toAppendTo.
- Throws:
MathIllegalArgumentException- isobjis not a valid type.- See Also:
Format.format(java.lang.Object, java.lang.StringBuffer, java.text.FieldPosition)
-
getImaginaryCharacter
public java.lang.String getImaginaryCharacter()
Access the imaginaryCharacter.- Returns:
- the imaginaryCharacter.
-
getImaginaryFormat
public java.text.NumberFormat getImaginaryFormat()
Access the imaginaryFormat.- Returns:
- the imaginaryFormat.
-
getInstance
public static ComplexFormat getInstance()
Returns the default complex format for the current locale.- Returns:
- the default complex format.
-
getInstance
public static ComplexFormat getInstance(java.util.Locale locale)
Returns the default complex format for the given locale.- Parameters:
locale- the specific locale used by the format.- Returns:
- the complex format specific to the given locale.
-
getInstance
public static ComplexFormat getInstance(java.lang.String imaginaryCharacter, java.util.Locale locale) throws NullArgumentException, NoDataException
Returns the default complex format for the given locale.- Parameters:
locale- the specific locale used by the format.imaginaryCharacter- Imaginary character.- Returns:
- the complex format specific to the given locale.
- Throws:
NullArgumentException- ifimaginaryCharacterisnull.NoDataException- ifimaginaryCharacteris an empty string.
-
getRealFormat
public java.text.NumberFormat getRealFormat()
Access the realFormat.- Returns:
- the realFormat.
-
parse
public Complex parse(java.lang.String source) throws MathParseException
Parses a string to produce aComplexobject.- Parameters:
source- the string to parse.- Returns:
- the parsed
Complexobject. - Throws:
MathParseException- if the beginning of the specified string cannot be parsed.
-
-