commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ted Dunning <ted.dunn...@gmail.com>
Subject Re: [math] ComplexFormat parse for inverted complex number
Date Mon, 11 Oct 2010 22:44:14 GMT
Two variants on this, what about splitting into chunks of the form
[\-+]?\d+i? instead?  Each chunk could be parsed separately
and combined by adding.

Secondly, whether or not the inverted form or redundant forms like 0+4i or
5+3+2i are common, is there really any penalty to making the parse be
slightly more general?  Put another way, is there any conceivable benefit
from restricting the format?

This is similar in some ways to the way that Java throws a format exception
for Integer.parseInt("   43").  Yes, spaces are not digits, but no, there
isn't really any benefit from failing to parse in these obvious cases.

On Mon, Oct 11, 2010 at 3:06 PM, Haswell, Joe <josiah.d.haswell@hp.com>wrote:

> 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 [mailto:Luc.Maisonobe@free.fr]
> 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 http://commons.apache.org/math/userguide/complex.html 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 ?
>
> Luc
>
> >
> > 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: user-unsubscribe@commons.apache.org
> > For additional commands, e-mail: user-help@commons.apache.org
> >
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message