Ticket #2627 (closed defect: fixed)

Opened 5 years ago

Last modified 5 years ago

[with patch, positive review] Integer(abs(gamma(n+1))) is not always equal to factorial(n) for n a positive integer

Reported by: ddrake Owned by: cwitty
Priority: major Milestone: sage-2.11
Component: misc Keywords: gamma function, factorial
Cc: Work issues:
Report Upstream: Reviewers:
Authors: Merged in:
Dependencies: Stopgaps:

Description

Integer(abs(gamma(n+1))) - factorial(n) should be zero for all positive integers, but on 2.10.3, I get:

sage: [ Integer(abs(gamma(n+1))) - factorial(n) for n in range(20, 30) ]

[0,
 0,
 0,
 1572864,
 -29360128,
 71303168,
 14738784256,
 -220528115712,
 11417398804480,
 -55923527647232]

I'm guessing this is due to some numerical noise. There should be some type-checking done in the gamma function.

I would also like to see, for instance, gamma(1/2) return sqrt(pi) instead of a floating point number, although I think the above issue is more pressing and easier to deal with.

Attachments

2627-exact-gamma.patch Download (8.8 KB) - added by robertwb 5 years ago.
trac_2627_doctest-fix.patch Download (672 bytes) - added by mabshoff 5 years ago.

Change History

comment:1 Changed 5 years ago by mhansen

The right fix would probably be to add a .gamma() method to the integers, and then also make the generic gamma use interval arithmetic.

Changed 5 years ago by robertwb

comment:2 Changed 5 years ago by robertwb

  • Summary changed from Integer(abs(gamma(n+1))) is not always equal to factorial(n) for n a positive integer to [with patch] Integer(abs(gamma(n+1))) is not always equal to factorial(n) for n a positive integer
sage: sage: [ Integer(abs(gamma(n+1))) - factorial(n) for n in range(20, 30) ]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
sage: gamma(1/2)
sqrt(pi)
sage: gamma(-101/2)
-2251799813685248*sqrt(pi)/275264606114823679801052037785492781962370429385126144787167211167753726318359375

comment:3 Changed 5 years ago by ddrake

  • Summary changed from [with patch] Integer(abs(gamma(n+1))) is not always equal to factorial(n) for n a positive integer to [with patch, positive functionality review] Integer(abs(gamma(n+1))) is not always equal to factorial(n) for n a positive integer

The patch applies cleanly and works as advertised. I tested with integers as large as 500000 and had no troubles. The half-integer and multifactorial stuff works too. Positive review for functionality; I'm not familiar enough with the Pyrex code to review that, although it looks pretty straightforward.

The doctests also pass for me.

comment:4 Changed 5 years ago by mabshoff

  • Summary changed from [with patch, positive functionality review] Integer(abs(gamma(n+1))) is not always equal to factorial(n) for n a positive integer to [with patch, positive review] Integer(abs(gamma(n+1))) is not always equal to factorial(n) for n a positive integer

I looked at the Cython code. Positive review.

Cheers,

Michael

comment:5 Changed 5 years ago by mabshoff

  • Status changed from new to closed
  • Resolution set to fixed

Merged in Sage 2.11.alpha2

comment:6 Changed 5 years ago by mabshoff

The is one doctest failure:

sage -t  devel/sage-main/sage/functions/transcendental.py
**********************************************************************
File "transcendental.py", line 102:
    sage: gamma(6)
Expected:
    120.000000000000
Got:
    120
**********************************************************************

Trivial fix coming up.

Cheers,

Michael

Changed 5 years ago by mabshoff

comment:7 Changed 5 years ago by mabshoff

Merged trac_2627_doctest-fix.patch in Sage 2.11.alpha2

Note: See TracTickets for help on using tickets.