You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Gilles (JIRA)" <ji...@apache.org> on 2011/07/12 15:51:00 UTC

[jira] [Created] (MATH-614) "toString" method for "Complex"

"toString" method for "Complex"
-------------------------------

                 Key: MATH-614
                 URL: https://issues.apache.org/jira/browse/MATH-614
             Project: Commons Math
          Issue Type: Wish
            Reporter: Gilles
            Assignee: Gilles
            Priority: Trivial
             Fix For: 3.0


Class "Complex" (in package "complex") lacks a "toString()" method.
[Request from Arne Ploese on the "dev" ML.]

I also propose to slightly modify the "format" method in "ComplexFormat" so that the stringified version of "Complex(1, 1)" is "1 + i" instead of "1 + 1i".


--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

[jira] [Commented] (MATH-614) "toString" method for "Complex"

Posted by "Arne Plöse (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/MATH-614?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13068257#comment-13068257 ] 

Arne Plöse commented on MATH-614:
---------------------------------

What about this:
{code}
    @Override
    public String toString() {
        if (isNaN) {
            return String.format("%s + %si", Double.toString(Double.NaN), Double.toString(Double.NaN));
        } 
        
        if (imaginary == 0.0) {
            return Double.toString(real);
        } 
        
        if (imaginary > 0) {
            return String.format("%s + %si", Double.toString(real), Double.toString(imaginary));
        } 
        
        return String.format("%s - %si", Double.toString(real), Double.toString(Math.abs(imaginary)));
    }
{code}

testcase:
{code}
    @Test
    public void testToString() {
        Assert.assertEquals("NaN + NaNi", new Complex(nan, inf).toString());
        Assert.assertEquals("1.0", Complex.ONE.toString());
        Assert.assertEquals("0.0 + 1.0i", Complex.I.toString());
        Assert.assertEquals("0.0 - 1.0i", new Complex(0.0, - 1.0).toString());
        Assert.assertEquals("Infinity + Infinityi", new Complex(inf, inf).toString());
        Assert.assertEquals("Infinity - Infinityi", new Complex(inf, - inf).toString());
    }
{code}


> "toString" method for "Complex"
> -------------------------------
>
>                 Key: MATH-614
>                 URL: https://issues.apache.org/jira/browse/MATH-614
>             Project: Commons Math
>          Issue Type: Wish
>            Reporter: Gilles
>            Assignee: Gilles
>            Priority: Trivial
>             Fix For: 3.0
>
>         Attachments: math.patch
>
>
> Class "Complex" (in package "complex") lacks a "toString()" method.
> [Request from Arne Ploese on the "dev" ML.]
> I also propose to slightly modify the "format" method in "ComplexFormat" so that the stringified version of "Complex(1, 1)" is "1 + i" instead of "1 + 1i".

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

       

[jira] [Commented] (MATH-614) "toString" method for "Complex"

Posted by "Gilles (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/MATH-614?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13082323#comment-13082323 ] 

Gilles commented on MATH-614:
-----------------------------

We could wait a few more days. If the contenders do not bring the issue on the ML, I'll resolve it next week...


> "toString" method for "Complex"
> -------------------------------
>
>                 Key: MATH-614
>                 URL: https://issues.apache.org/jira/browse/MATH-614
>             Project: Commons Math
>          Issue Type: Wish
>            Reporter: Gilles
>            Assignee: Gilles
>            Priority: Trivial
>             Fix For: 3.0
>
>         Attachments: math.patch
>
>
> Class "Complex" (in package "complex") lacks a "toString()" method.
> [Request from Arne Ploese on the "dev" ML.]
> I also propose to slightly modify the "format" method in "ComplexFormat" so that the stringified version of "Complex(1, 1)" is "1 + i" instead of "1 + 1i".

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

Re: [jira] [Commented] (MATH-614) "toString" method for "Complex"

Posted by Luc Maisonobe <Lu...@free.fr>.
Le 13/07/2011 14:33, Gilles Sadowski a écrit :
> Hello.

Hi,

>
>>>
>>> Gilles commented on MATH-614:
>>> -----------------------------
>>>
>>> Above code added in revision 1145932.
>>> Is there a case against that implementation?
>>
>> I'm not sure I understand well.
>> Shouldn't the toString method be based on some default configuration
>> of ComplexFormat and most importantly be such that it can be parsed
>> back using ComplexFormat ?
>
> In principle, no. I've read somewhere that, as a rule, users should *not* be
> encouraged to consider the output of "toString" as a standard representation
> of the instance. It's purpose is to provide easy printing/logging but it's
> not a substitute for serialization.
>
>> I fear that the current implementation can be written but not read.
>> ComplexFormat takes care to be able to do both.
>
> At first, I also thought that it should use the default "ComplexFormat". But
> even it could, there is a disadvantage, if we want the output of "toString"
> to be a faithful representation of the two real numbers that compose the
> complex number: E.g.
> ---CUT---
>    double a = 1.12345678910111213;
>    double b = 1.98765432123456778;
>
>    Complex c = new Complex(a, b);
>
>    log.debug("a={}", a);
>    log.debug("b={}", b);
>    log.debug("c={}", c);
> ---CUT---
>
> The 3 log statements will indirectly use "toString" but in the third, the
> "a" and "b" components will not be output in the same way as in the 2
> preceding statements because of the use of "ComplexFormat". This could be
> fairly confusing...

OK, it's fine with me.

Thanks for the explanation.

best regards,
Luc

>
>
> Regards,
> Gilles


Re: [jira] [Commented] (MATH-614) "toString" method for "Complex"

Posted by Gilles Sadowski <gi...@harfang.homelinux.org>.
Hello.

> >
> >Gilles commented on MATH-614:
> >-----------------------------
> >
> >Above code added in revision 1145932.
> >Is there a case against that implementation?
> 
> I'm not sure I understand well.
> Shouldn't the toString method be based on some default configuration
> of ComplexFormat and most importantly be such that it can be parsed
> back using ComplexFormat ?

In principle, no. I've read somewhere that, as a rule, users should *not* be
encouraged to consider the output of "toString" as a standard representation
of the instance. It's purpose is to provide easy printing/logging but it's
not a substitute for serialization.

> I fear that the current implementation can be written but not read.
> ComplexFormat takes care to be able to do both.

At first, I also thought that it should use the default "ComplexFormat". But
even it could, there is a disadvantage, if we want the output of "toString"
to be a faithful representation of the two real numbers that compose the
complex number: E.g.
---CUT---
  double a = 1.12345678910111213;
  double b = 1.98765432123456778;

  Complex c = new Complex(a, b);

  log.debug("a={}", a);
  log.debug("b={}", b);
  log.debug("c={}", c);
---CUT---

The 3 log statements will indirectly use "toString" but in the third, the
"a" and "b" components will not be output in the same way as in the 2
preceding statements because of the use of "ComplexFormat". This could be
fairly confusing...


Regards,
Gilles

Re: [jira] [Commented] (MATH-614) "toString" method for "Complex"

Posted by Luc Maisonobe <Lu...@free.fr>.
Hi Gilles,

Le 13/07/2011 11:57, Gilles (JIRA) a écrit :
>
>      [ https://issues.apache.org/jira/browse/MATH-614?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13064460#comment-13064460 ]
>
> Gilles commented on MATH-614:
> -----------------------------
>
> Above code added in revision 1145932.
> Is there a case against that implementation?

I'm not sure I understand well.
Shouldn't the toString method be based on some default configuration of 
ComplexFormat and most importantly be such that it can be parsed back 
using ComplexFormat ?

I fear that the current implementation can be written but not read. 
ComplexFormat takes care to be able to do both.

Luc

>
>
>> "toString" method for "Complex"
>> -------------------------------
>>
>>                  Key: MATH-614
>>                  URL: https://issues.apache.org/jira/browse/MATH-614
>>              Project: Commons Math
>>           Issue Type: Wish
>>             Reporter: Gilles
>>             Assignee: Gilles
>>             Priority: Trivial
>>              Fix For: 3.0
>>
>>          Attachments: math.patch
>>
>>
>> Class "Complex" (in package "complex") lacks a "toString()" method.
>> [Request from Arne Ploese on the "dev" ML.]
>> I also propose to slightly modify the "format" method in "ComplexFormat" so that the stringified version of "Complex(1, 1)" is "1 + i" instead of "1 + 1i".
>
> --
> This message is automatically generated by JIRA.
> For more information on JIRA, see: http://www.atlassian.com/software/jira
>
>


[jira] [Commented] (MATH-614) "toString" method for "Complex"

Posted by "Gilles (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/MATH-614?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13064460#comment-13064460 ] 

Gilles commented on MATH-614:
-----------------------------

Above code added in revision 1145932.
Is there a case against that implementation?


> "toString" method for "Complex"
> -------------------------------
>
>                 Key: MATH-614
>                 URL: https://issues.apache.org/jira/browse/MATH-614
>             Project: Commons Math
>          Issue Type: Wish
>            Reporter: Gilles
>            Assignee: Gilles
>            Priority: Trivial
>             Fix For: 3.0
>
>         Attachments: math.patch
>
>
> Class "Complex" (in package "complex") lacks a "toString()" method.
> [Request from Arne Ploese on the "dev" ML.]
> I also propose to slightly modify the "format" method in "ComplexFormat" so that the stringified version of "Complex(1, 1)" is "1 + i" instead of "1 + 1i".

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

[jira] [Resolved] (MATH-614) "toString" method for "Complex"

Posted by "Gilles (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/MATH-614?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Gilles resolved MATH-614.
-------------------------

    Resolution: Fixed

> "toString" method for "Complex"
> -------------------------------
>
>                 Key: MATH-614
>                 URL: https://issues.apache.org/jira/browse/MATH-614
>             Project: Commons Math
>          Issue Type: Wish
>            Reporter: Gilles
>            Assignee: Gilles
>            Priority: Trivial
>             Fix For: 3.0
>
>         Attachments: math.patch
>
>
> Class "Complex" (in package "complex") lacks a "toString()" method.
> [Request from Arne Ploese on the "dev" ML.]
> I also propose to slightly modify the "format" method in "ComplexFormat" so that the stringified version of "Complex(1, 1)" is "1 + i" instead of "1 + 1i".

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

[jira] [Commented] (MATH-614) "toString" method for "Complex"

Posted by "Gilles (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/MATH-614?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13064875#comment-13064875 ] 

Gilles commented on MATH-614:
-----------------------------

{quote}
I still feel there's a case to be made [...]
{quote}

Which one?

> "toString" method for "Complex"
> -------------------------------
>
>                 Key: MATH-614
>                 URL: https://issues.apache.org/jira/browse/MATH-614
>             Project: Commons Math
>          Issue Type: Wish
>            Reporter: Gilles
>            Assignee: Gilles
>            Priority: Trivial
>             Fix For: 3.0
>
>         Attachments: math.patch
>
>
> Class "Complex" (in package "complex") lacks a "toString()" method.
> [Request from Arne Ploese on the "dev" ML.]
> I also propose to slightly modify the "format" method in "ComplexFormat" so that the stringified version of "Complex(1, 1)" is "1 + i" instead of "1 + 1i".

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

[jira] [Commented] (MATH-614) "toString" method for "Complex"

Posted by "Hasan Diwan (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/MATH-614?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13064698#comment-13064698 ] 

Hasan Diwan commented on MATH-614:
----------------------------------

I still feel there's a case to be made for having toString() return the generally-accepted, unambiguous display of a complex number (in this case).

> "toString" method for "Complex"
> -------------------------------
>
>                 Key: MATH-614
>                 URL: https://issues.apache.org/jira/browse/MATH-614
>             Project: Commons Math
>          Issue Type: Wish
>            Reporter: Gilles
>            Assignee: Gilles
>            Priority: Trivial
>             Fix For: 3.0
>
>         Attachments: math.patch
>
>
> Class "Complex" (in package "complex") lacks a "toString()" method.
> [Request from Arne Ploese on the "dev" ML.]
> I also propose to slightly modify the "format" method in "ComplexFormat" so that the stringified version of "Complex(1, 1)" is "1 + i" instead of "1 + 1i".

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

[jira] [Commented] (MATH-614) "toString" method for "Complex"

Posted by "Hasan Diwan (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/MATH-614?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13064881#comment-13064881 ] 

Hasan Diwan commented on MATH-614:
----------------------------------

The generally-accepted, unambiguous display of a complex number is [real] + [imaginary]i -- see wikipedia or any mathematics textbook for true examples. And that is what I believe toString() should return, as my patch makes it do.

> "toString" method for "Complex"
> -------------------------------
>
>                 Key: MATH-614
>                 URL: https://issues.apache.org/jira/browse/MATH-614
>             Project: Commons Math
>          Issue Type: Wish
>            Reporter: Gilles
>            Assignee: Gilles
>            Priority: Trivial
>             Fix For: 3.0
>
>         Attachments: math.patch
>
>
> Class "Complex" (in package "complex") lacks a "toString()" method.
> [Request from Arne Ploese on the "dev" ML.]
> I also propose to slightly modify the "format" method in "ComplexFormat" so that the stringified version of "Complex(1, 1)" is "1 + i" instead of "1 + 1i".

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

[jira] [Commented] (MATH-614) "toString" method for "Complex"

Posted by "Gilles (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/MATH-614?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13065195#comment-13065195 ] 

Gilles commented on MATH-614:
-----------------------------

My output of "toString" is no more ambiguous than yours, as far as it reflect the internal state of the Complex class instance. And IMO, it is more legible in cases like
* {{a + 1i}} vs {{(a, 1)}}
* {{a + 0i}} vs {{(a, 0)}}
* {{0 + bi}} vs {{(0, b)}}
* {{0 + 0i}} vs {{(0, 0)}}
* {{a + -bi}} vs {{(a, -b)}}

However, if a majority of people favour your alternative, I'll change it.

Also, please note that you can have full control over the format by using the "ComplexFormat" class which can, for example, output a nice (and generally-accepted)
{noformat}
 a - bi
{noformat}
instead of
{noformat}
 a + -bi
{noformat}


> "toString" method for "Complex"
> -------------------------------
>
>                 Key: MATH-614
>                 URL: https://issues.apache.org/jira/browse/MATH-614
>             Project: Commons Math
>          Issue Type: Wish
>            Reporter: Gilles
>            Assignee: Gilles
>            Priority: Trivial
>             Fix For: 3.0
>
>         Attachments: math.patch
>
>
> Class "Complex" (in package "complex") lacks a "toString()" method.
> [Request from Arne Ploese on the "dev" ML.]
> I also propose to slightly modify the "format" method in "ComplexFormat" so that the stringified version of "Complex(1, 1)" is "1 + i" instead of "1 + 1i".

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

[jira] [Commented] (MATH-614) "toString" method for "Complex"

Posted by "Gilles (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/MATH-614?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13064143#comment-13064143 ] 

Gilles commented on MATH-614:
-----------------------------

Maybe the "toString" method should only be expected to expose the actual components of the complex number instance, without consideration for "nice" formatting (e.g. printing an "i"): the simplest would thus be:
{code}
public String toString() {
  return "(" + real + ", " + imaginary + ")";
}
{code}
Hence we don't need special rules for NaN and infinities.


> "toString" method for "Complex"
> -------------------------------
>
>                 Key: MATH-614
>                 URL: https://issues.apache.org/jira/browse/MATH-614
>             Project: Commons Math
>          Issue Type: Wish
>            Reporter: Gilles
>            Assignee: Gilles
>            Priority: Trivial
>             Fix For: 3.0
>
>         Attachments: math.patch
>
>
> Class "Complex" (in package "complex") lacks a "toString()" method.
> [Request from Arne Ploese on the "dev" ML.]
> I also propose to slightly modify the "format" method in "ComplexFormat" so that the stringified version of "Complex(1, 1)" is "1 + i" instead of "1 + 1i".

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

[jira] [Commented] (MATH-614) "toString" method for "Complex"

Posted by "Phil Steitz (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/MATH-614?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13082151#comment-13082151 ] 

Phil Steitz commented on MATH-614:
----------------------------------

I think the current impl is fine and this issue can be resolved as FIXED.

> "toString" method for "Complex"
> -------------------------------
>
>                 Key: MATH-614
>                 URL: https://issues.apache.org/jira/browse/MATH-614
>             Project: Commons Math
>          Issue Type: Wish
>            Reporter: Gilles
>            Assignee: Gilles
>            Priority: Trivial
>             Fix For: 3.0
>
>         Attachments: math.patch
>
>
> Class "Complex" (in package "complex") lacks a "toString()" method.
> [Request from Arne Ploese on the "dev" ML.]
> I also propose to slightly modify the "format" method in "ComplexFormat" so that the stringified version of "Complex(1, 1)" is "1 + i" instead of "1 + 1i".

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

[jira] [Updated] (MATH-614) "toString" method for "Complex"

Posted by "Hasan Diwan (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/MATH-614?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Hasan Diwan updated MATH-614:
-----------------------------

    Attachment: math.patch

Adds a toString() method to class Complex and an associated test

> "toString" method for "Complex"
> -------------------------------
>
>                 Key: MATH-614
>                 URL: https://issues.apache.org/jira/browse/MATH-614
>             Project: Commons Math
>          Issue Type: Wish
>            Reporter: Gilles
>            Assignee: Gilles
>            Priority: Trivial
>             Fix For: 3.0
>
>         Attachments: math.patch
>
>
> Class "Complex" (in package "complex") lacks a "toString()" method.
> [Request from Arne Ploese on the "dev" ML.]
> I also propose to slightly modify the "format" method in "ComplexFormat" so that the stringified version of "Complex(1, 1)" is "1 + i" instead of "1 + 1i".

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira