Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gamma of complex numbers incorrectly simplifies to factorial #13869

Open
sagetrac-JoalHeagney mannequin opened this issue Dec 27, 2012 · 17 comments
Open

Gamma of complex numbers incorrectly simplifies to factorial #13869

sagetrac-JoalHeagney mannequin opened this issue Dec 27, 2012 · 17 comments

Comments

@sagetrac-JoalHeagney
Copy link
Mannequin

sagetrac-JoalHeagney mannequin commented Dec 27, 2012

If I type the following into sage-5.5 built from source,

(gamma(-i-3/2)).simplify_full()

I get the following result:

factorial(-I - 5/2)

This:

n(factorial(-I - 5/2))

gives the obvious error:

TypeError: cannot evaluate symbolic expression numerically

while:

n(gamma(-I-5/2))

gives no problems.

-0.0417366258078936 + 0.0863691073697635*I

I've only noticed this happening in 5.5 - no such problems in 5.4 (that I remember).

CC: @benjaminfjones @sagetrac-dsm @eviatarbach @paulmasson

Component: symbolics

Keywords: gamma, simplify_full, simplify_factorial, maxima

Issue created by migration from https://trac.sagemath.org/ticket/13869

@sagetrac-JoalHeagney sagetrac-JoalHeagney mannequin added this to the sage-5.11 milestone Dec 27, 2012
@burcin
Copy link
Contributor

burcin commented Dec 27, 2012

comment:1

This seems to be our doing. In simplify_factorial() we have:

return self.parent()(self._maxima_().makefact().factcomb().minfactorial())

Here is what the Maxima manual says about makefact():

transforms occurrences of binomial,gamma, and beta functions in exp to factorials. 

There is also a corresponding makegamma() function, but this will change factorials to gamma as well.

@burcin burcin assigned burcin and unassigned aghitza Dec 27, 2012
@kcrisman
Copy link
Member

comment:2

In Sage 5.2:

sage: (gamma(-i-3/2)).simplify_full()
factorial(-I - 5/2)
sage: a = gamma(-i-3/2)
sage: a._maxima_().makefact()
(-%i-5/2)!

This seems to be our doing. In simplify_factorial() we have:

So I agree with Burcin that this comes from us.

The problem is that we do rely on this for some examples of the (mostly improved) simplification. E.g. from #6636

sage: var('k')
k
sage: f = binomial(n,k)*factorial(k)*factorial(n-k)
sage: f._maxima_()
k!*binomial(n,k)*(n-k)!
sage: f._maxima_().makefact()
n!

On the other hand, does Maxima really want these occurrences with non-integer inputs to be transformed thusly?

@sagetrac-JoalHeagney
Copy link
Mannequin Author

sagetrac-JoalHeagney mannequin commented Jan 9, 2013

comment:3

So, should we report upstream?

@kcrisman
Copy link
Member

kcrisman commented Jan 9, 2013

comment:4

Replying to @sagetrac-JoalHeagney:

So, should we report upstream?

I'm not a special functions expert, so maybe it would be nice if someone who had work interrupted reported.

@sagetrac-JoalHeagney
Copy link
Mannequin Author

sagetrac-JoalHeagney mannequin commented Jan 11, 2013

comment:5

Hmm, maybe not. From what I can see from the maxima documentation, it's kinda hinted that if you call makefact, it's assumed that you know what you're asking for.

Would it be possible to change the simplification code to use makegamma instead?

In the sage -maxima interface, I was able to do the following (these are all out of order cut-and-paste from my maxima session.)

(%i18) makegamma(binomial(n,k)*factorial(k)*factorial(n-k));
(%o18)                           gamma(n + 1)
(%i12) gamma_expand:true;
(%o12)                               true
(%i13) gamma(2+i);
(%o13)                        i (i + 1) gamma(i)
(%i16) makegamma(binomial(n,k)*factorial(k)*factorial(n-k));
(%o16)                            n gamma(n)

The benefits here, are that makegamma and gamma_expand functions seem to provide the same simplifications (if not more) that minfactorial and makefact do, but gamma functions can handle (by definition) more fields (RR, CC).

I don't know if we want gamma_expand to be true all the time, but it does seem to give another level of simplification, pulling out i(i+1) in my second example.

Then we could document the .makefact function so that users could convert sagemath (new default) gamma answers to factorials for themselves?

@sagetrac-JoalHeagney
Copy link
Mannequin Author

sagetrac-JoalHeagney mannequin commented Jan 11, 2013

comment:6

In the documentation of maxima http://maxima.sourceforge.net/docs/manual/en/maxima_15.html there are also a lot of other expand options we may want to investigate as well:

E.g. besselexpand, beta_expand, expintexpand, hypergeometric and expand_hypergeometric,

@sagetrac-JoalHeagney
Copy link
Mannequin Author

sagetrac-JoalHeagney mannequin commented Jan 11, 2013

comment:7

Another argument for the makegamma option is the following simplification from a maxima newsgroup:

(%i7) gamma_expand:true;
(%o7)                                true
(%i8) gamma(a+1)/gamma(a);
(%o8)                                  a

@kcrisman
Copy link
Member

comment:8

Would it be possible to change the simplification code to use makegamma instead?

I don't see any reason why not. If we start using Maxima code more like it's meant to be, that is a good thing, as long as we can keep as many simplifications as possible working.

I don't know if we want gamma_expand to be true all the time, but it does seem to give another level of simplification, pulling out i(i+1) in my second example.

The standard thing we do is make another simplify_blah function and then add that to simplify_full. Heck, why not add those other expand things too eventually... we might want to wait until the stuff for symbolic hypergeometric and bessels is in first for those, though :-)

@sagetrac-JoalHeagney
Copy link
Mannequin Author

sagetrac-JoalHeagney mannequin commented Jan 16, 2013

Changed keywords from gamma, simplify_full to gamma, simplify_full, simplify_factorial, maxima

@sagetrac-JoalHeagney
Copy link
Mannequin Author

sagetrac-JoalHeagney mannequin commented Jan 16, 2013

comment:9

Hah hah hah.

You won't believe this, but I went to the maxima-bugs newsgroup and found out that maxima has the following behaviour:

(%i2) float((%i + 3)!);   
(%o2)              4.980156681183563 %i + 1.5494982830181063

So, the issue seems to be:

  • maxima treats x! and gamma(x+1) identically in regards to dealing with complex numbers (which I think is mathematically appalling).

  • so if there's any difference between sagemath and maxima regarding (3 + i)!, it's sagemath's doing.

@benjaminfjones
Copy link
Contributor

comment:10

Replying to @sagetrac-JoalHeagney:

  • maxima treats x! and gamma(x+1) identically in regards to dealing with complex numbers (which I think is mathematically appalling).

  • so if there's any difference between sagemath and maxima regarding (3 + i)!, it's sagemath's doing.

Why is that appalling? \Gamma is a natural extension of factorial to the complex plane that agrees with factorial everywhere that factorial is defined.

+1 to exposing more simplification options from Maxima.

@sagetrac-JoalHeagney
Copy link
Mannequin Author

sagetrac-JoalHeagney mannequin commented Jan 17, 2013

comment:11

I don't argue with using gamma to extend factorial - just find it worrying that maxima treats factorial as equivalent to gamma. Much the same way sage doesn't treat ZZ as identically equivalent to RR, despite the fact that every element of ZZ appears in RR.

I'm finishing moving house for the next few days (2000km by car), and won't have reliable Internet for about a month after, so I am unable to code anything up, but I would much prefer that if we/maxima are going to treat factorial = gamma, to replace simplify_factorial with simplify_gamma instead?

Out of curiosity, does anyone know how we would do the maxima gamma_!expand:true; function call from sage/python?

@jdemeyer jdemeyer modified the milestones: sage-5.11, sage-5.12 Aug 13, 2013
@sagetrac-vbraun-spam sagetrac-vbraun-spam mannequin modified the milestones: sage-6.1, sage-6.2 Jan 30, 2014
@sagetrac-vbraun-spam sagetrac-vbraun-spam mannequin modified the milestones: sage-6.2, sage-6.3 May 6, 2014
@sagetrac-vbraun-spam sagetrac-vbraun-spam mannequin modified the milestones: sage-6.3, sage-6.4 Aug 10, 2014
@sagetrac-JoalHeagney
Copy link
Mannequin Author

sagetrac-JoalHeagney mannequin commented Nov 3, 2014

comment:17

Okay, after a long time, I think I can say that this ticket could be modified/closed.

Using the following I can get the behaviour I want from sage:

maxima.eval("gamma_expand:true")

and similar to set the different options for maxima expansion and:

var('n,k')
maxima.makegamma(binomial(n,k)*factorial(k)*factorial(n-k)).sage()

maxima.makegamma((gamma(i-3/2)).simplify_full()).sage()

to force expressions back to gammas rather than factorials.

Messy, but effective.

@kcrisman
Copy link
Member

kcrisman commented Nov 3, 2014

comment:18

Well, I'll go with modify, not close, because we would both want to document this workaround as well as still decide what to do. I mean, if makegamma really does do the same thing as the other one, maybe we could replace it... but I wonder whether it would take things in factorial form and then make them gammas, perhaps also controverting expectations. In reality, simplification isn't always equivalence... Anyway, I haven't looked at this for quite some time so maybe my comments make no sense anyway!

@kcrisman
Copy link
Member

comment:19

Note also that gamma_expand does alter some other stuff, like incomplete gammas, that come up in various integrals.

Maybe making that simplify_gamma function is the best idea after all. Maybe.

@rwst
Copy link
Contributor

rwst commented Dec 25, 2014

comment:20

The issue with N(factorial(...)) is #17489.

@rwst
Copy link
Contributor

rwst commented Feb 5, 2015

comment:21

Replying to @kcrisman:

Note also that gamma_expand does alter some other stuff, like incomplete gammas, that come up in various integrals.

Maybe making that simplify_gamma function is the best idea after all. Maybe.

I have a proposal at #17737.

@mkoeppe mkoeppe removed this from the sage-6.4 milestone Dec 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants