commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Haswell, Joe" <>
Subject RE: [math] ComplexFormat parse for inverted complex number
Date Mon, 11 Oct 2010 22:06:11 GMT
Would something like

String[] components = sZ.split("\\s*i\\s*\\+\\s*");
Complex number = new Complex(Double.parseDouble(components[0]), Double.parseDouble(components[1]));

Work for you?  Is the Im(c)i + Re(c) widely used enough to warrant the addition of support
for it?
Joe Haswell | HP Software

-----Original Message-----
From: Luc Maisonobe [] 
Sent: Monday, October 11, 2010 3:24 PM
To: Commons Users List
Subject: Re: [math] ComplexFormat parse for inverted complex number

Le 11/10/2010 22:38, Grzegorz Szpetkowski a écrit :
> I have a question how can I (easily) parse String like that:
> String sZ = "20i + 5.4";
> On page I found example:
> ComplexFormat cf = new ComplexFormat();
> Complex c = cf.parse("1.110 + 2.222i");
> It's ok, but if I have inverted form like "20i + 5.4" then there is
> exception: "org.apache.commons.math.MathRuntimeException$9:
> unparseable complex number: "20i + 5.4".

I don't think it's possible for now.
One way to change that would be to add a flag "imaginaryFirst" in
ComplexFormat and change the parsing logic in the public Complex
parse(String source, ParsePosition pos) method according to this flag.

Are your numbers always in inverted order or do you have both orders ?
In the first case an appropriate semantic for the flag would be to
always use the order specified (in both parse and format). In the second
case, the semantic would rather be to accept both inverted numbers and
classical numbers on parse, but to always put real part first on format.

What do you think ?


> Pseudo-algorithm for proposed solution:
> 1) Parse first number
> 2) IF first number has imaginary character THEN remember that second
> number should be real number (without imaginary character)
> 3) Parse second number
> 4) IF second number has imaginary character and first had it too (look
> at 2)), THEN throw exception
> Thanks ! Greg
> ---------------------------------------------------------------------
> To unsubscribe, e-mail:
> For additional commands, e-mail:

To unsubscribe, e-mail:
For additional commands, e-mail:

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message