You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Juan Barandiaran (Created) (JIRA)" <ji...@apache.org> on 2011/12/09 22:35:40 UTC

[jira] [Created] (MATH-722) [math] Complex Tanh for "big" numbers

[math] Complex Tanh for "big" numbers
-------------------------------------

                 Key: MATH-722
                 URL: https://issues.apache.org/jira/browse/MATH-722
             Project: Commons Math
          Issue Type: Bug
    Affects Versions: 2.2
         Environment: I'm working with Eclipse 3.6.2 on Windows XP, but the bug is Enviroment independent
            Reporter: Juan Barandiaran
            Priority: Minor


Hi,

In Complex.java the tanh is computed with the following formula:

tanh(a + bi) = sinh(2a)/(cosh(2a)+cos(2b)) + [sin(2b)/(cosh(2a)+cos(2b))]i

The problem that I'm finding is that as soon as "a" is a "big" number,
both sinh(2a) and cosh(2a) are infinity and then the method tanh returns in
the real part NaN (infinity/infinity) when it should return 1.0.

Wouldn't it be appropiate to add something as in the FastMath library??:

if (real>20.0){
      return createComplex(1.0, 0.0);
}
if (real<-20.0){
      return createComplex(-1.0, 0.0);
}


Best regards,

JBB


--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators: https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

[jira] [Commented] (MATH-722) [math] Complex Tanh for "big" numbers

Posted by "Sébastien Brisard (Commented JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/MATH-722?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13167068#comment-13167068 ] 

Sébastien Brisard commented on MATH-722:
----------------------------------------

Even when a is not large enough to cause overflows, I'm not sure the general expression used is quite accurate (because {{cosh(2 * a)}} and {{sinh(2 * a)}} are very close, but not equal). I've often come accross this issue, and found that reverting to exponential representation (keeping only the exponentials with negative argument) was much better behaved.

In the present case, maybe the following representation of the real part
{{(1 - exp(-4 * a)) / (1 + 2 * exp(-2 * a) * cos(2 * b) + exp(-4 * a))}}
would avoid overflows (for {{a > 0}}, while remaining fairly accurate (otherwise, the threshold needs to be tuned).

Similar expressions can be derived for {{a < 0}}.

What do you think?
Sébastien
                
> [math] Complex Tanh for "big" numbers
> -------------------------------------
>
>                 Key: MATH-722
>                 URL: https://issues.apache.org/jira/browse/MATH-722
>             Project: Commons Math
>          Issue Type: Bug
>    Affects Versions: 2.2
>         Environment: I'm working with Eclipse 3.6.2 on Windows XP, but the bug is Enviroment independent
>            Reporter: Juan Barandiaran
>            Priority: Minor
>              Labels: patch
>   Original Estimate: 0.25h
>  Remaining Estimate: 0.25h
>
> Hi,
> In Complex.java the tanh is computed with the following formula:
> tanh(a + bi) = sinh(2a)/(cosh(2a)+cos(2b)) + [sin(2b)/(cosh(2a)+cos(2b))]i
> The problem that I'm finding is that as soon as "a" is a "big" number,
> both sinh(2a) and cosh(2a) are infinity and then the method tanh returns in
> the real part NaN (infinity/infinity) when it should return 1.0.
> Wouldn't it be appropiate to add something as in the FastMath library??:
> if (real>20.0){
>       return createComplex(1.0, 0.0);
> }
> if (real<-20.0){
>       return createComplex(-1.0, 0.0);
> }
> Best regards,
> JBB

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators: https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira

       

[jira] [Updated] (MATH-722) [math] Complex Tanh for "big" numbers

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

Gilles updated MATH-722:
------------------------

    Fix Version/s: 3.0
    
> [math] Complex Tanh for "big" numbers
> -------------------------------------
>
>                 Key: MATH-722
>                 URL: https://issues.apache.org/jira/browse/MATH-722
>             Project: Commons Math
>          Issue Type: Bug
>    Affects Versions: 2.2
>         Environment: I'm working with Eclipse 3.6.2 on Windows XP, but the bug is Enviroment independent
>            Reporter: Juan Barandiaran
>            Assignee: Sébastien Brisard
>            Priority: Minor
>              Labels: patch
>             Fix For: 3.0
>
>   Original Estimate: 0.25h
>  Remaining Estimate: 0.25h
>
> Hi,
> In Complex.java the tanh is computed with the following formula:
> tanh(a + bi) = sinh(2a)/(cosh(2a)+cos(2b)) + [sin(2b)/(cosh(2a)+cos(2b))]i
> The problem that I'm finding is that as soon as "a" is a "big" number,
> both sinh(2a) and cosh(2a) are infinity and then the method tanh returns in
> the real part NaN (infinity/infinity) when it should return 1.0.
> Wouldn't it be appropiate to add something as in the FastMath library??:
> if (real>20.0){
>       return createComplex(1.0, 0.0);
> }
> if (real<-20.0){
>       return createComplex(-1.0, 0.0);
> }
> Best regards,
> JBB

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators: https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira

       

[jira] [Commented] (MATH-722) [math] Complex Tanh for "big" numbers

Posted by "Sébastien Brisard (Commented JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/MATH-722?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13194419#comment-13194419 ] 

Sébastien Brisard commented on MATH-722:
----------------------------------------

I've run some tests comparing the two formulas: I didn't notice any major difference. Therefore I suggest to stick with the existing formula.
Meanwhile, the fix proposed by Juan must be implemented. It can be proved mathematically that for any value of {{b}}, the value of {{tanh(a + i * b)}} is within one ulp of 1.0 for {{abs(a) > 18.9}}, so the proposed threshold (20.0) is fairly safe, and consistent with other parts of CM. Note that the same fix must be implemented for {{tan}} also.
                
> [math] Complex Tanh for "big" numbers
> -------------------------------------
>
>                 Key: MATH-722
>                 URL: https://issues.apache.org/jira/browse/MATH-722
>             Project: Commons Math
>          Issue Type: Bug
>    Affects Versions: 2.2
>         Environment: I'm working with Eclipse 3.6.2 on Windows XP, but the bug is Enviroment independent
>            Reporter: Juan Barandiaran
>            Assignee: Sébastien Brisard
>            Priority: Minor
>              Labels: patch
>             Fix For: 3.0
>
>   Original Estimate: 0.25h
>  Remaining Estimate: 0.25h
>
> Hi,
> In Complex.java the tanh is computed with the following formula:
> tanh(a + bi) = sinh(2a)/(cosh(2a)+cos(2b)) + [sin(2b)/(cosh(2a)+cos(2b))]i
> The problem that I'm finding is that as soon as "a" is a "big" number,
> both sinh(2a) and cosh(2a) are infinity and then the method tanh returns in
> the real part NaN (infinity/infinity) when it should return 1.0.
> Wouldn't it be appropiate to add something as in the FastMath library??:
> if (real>20.0){
>       return createComplex(1.0, 0.0);
> }
> if (real<-20.0){
>       return createComplex(-1.0, 0.0);
> }
> Best regards,
> JBB

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators: https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira

       

[jira] [Commented] (MATH-722) [math] Complex Tanh for "big" numbers

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

Luc Maisonobe commented on MATH-722:
------------------------------------

Very good idea Sébastien. Using stable formulas according to argument size is surely a good thing. We can also use FastMath.expm1() both in numerator and denominator, it will improve accuracy when a is close to 0.
                
> [math] Complex Tanh for "big" numbers
> -------------------------------------
>
>                 Key: MATH-722
>                 URL: https://issues.apache.org/jira/browse/MATH-722
>             Project: Commons Math
>          Issue Type: Bug
>    Affects Versions: 2.2
>         Environment: I'm working with Eclipse 3.6.2 on Windows XP, but the bug is Enviroment independent
>            Reporter: Juan Barandiaran
>            Priority: Minor
>              Labels: patch
>   Original Estimate: 0.25h
>  Remaining Estimate: 0.25h
>
> Hi,
> In Complex.java the tanh is computed with the following formula:
> tanh(a + bi) = sinh(2a)/(cosh(2a)+cos(2b)) + [sin(2b)/(cosh(2a)+cos(2b))]i
> The problem that I'm finding is that as soon as "a" is a "big" number,
> both sinh(2a) and cosh(2a) are infinity and then the method tanh returns in
> the real part NaN (infinity/infinity) when it should return 1.0.
> Wouldn't it be appropiate to add something as in the FastMath library??:
> if (real>20.0){
>       return createComplex(1.0, 0.0);
> }
> if (real<-20.0){
>       return createComplex(-1.0, 0.0);
> }
> Best regards,
> JBB

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators: https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira

       

[jira] [Resolved] (MATH-722) [math] Complex Tanh for "big" numbers

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

Sébastien Brisard resolved MATH-722.
------------------------------------

    Resolution: Fixed

Changes proposed by Juan committed in {{r1236548}}.
                
> [math] Complex Tanh for "big" numbers
> -------------------------------------
>
>                 Key: MATH-722
>                 URL: https://issues.apache.org/jira/browse/MATH-722
>             Project: Commons Math
>          Issue Type: Bug
>    Affects Versions: 2.2
>         Environment: I'm working with Eclipse 3.6.2 on Windows XP, but the bug is Enviroment independent
>            Reporter: Juan Barandiaran
>            Assignee: Sébastien Brisard
>            Priority: Minor
>              Labels: patch
>             Fix For: 3.0
>
>   Original Estimate: 0.25h
>  Remaining Estimate: 0.25h
>
> Hi,
> In Complex.java the tanh is computed with the following formula:
> tanh(a + bi) = sinh(2a)/(cosh(2a)+cos(2b)) + [sin(2b)/(cosh(2a)+cos(2b))]i
> The problem that I'm finding is that as soon as "a" is a "big" number,
> both sinh(2a) and cosh(2a) are infinity and then the method tanh returns in
> the real part NaN (infinity/infinity) when it should return 1.0.
> Wouldn't it be appropiate to add something as in the FastMath library??:
> if (real>20.0){
>       return createComplex(1.0, 0.0);
> }
> if (real<-20.0){
>       return createComplex(-1.0, 0.0);
> }
> Best regards,
> JBB

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators: https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira