Conversation
rikardn
force-pushed
the
gamma
branch
4 times, most recently
from
February 27, 2026 14:08
b84aec7 to
61ddb76
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR defers calculations of the gamma function from the automatic simplification to an explicit function call. It ties in to the discussion in #2056, but instead of arguing why this is a good idea in general I am here trying to optimize the use of the gamma function in some real applications.
Apologies for the long text, but since this has been a point of discussion before, I would like to make sure that the reasoning behind this is clear to everyone.
Before the PR
Before this PR the gamma function had automatic simplifications for:
What this PR does
calculatefunction to calculate gamma for these cases (and possible future cases)calculate(or if we prefer some other name) would be like adoitfunction that performs "deferred" calculations. This is then distinct fromevalthat evaluates expressions onto some floating point number andrefinethat leverage assumptions to simplify expressions.Benchmarks
The gamma function can be very slow to calculate. I did some benchmarking of the current situation using symengine.py on my laptop:
Integers
It looks like at least O(n) in the number of digits for integers.
Rationals
This looks like O(n) for number of digits in the numerator.
RealDouble
Without auto simplification
Couldn't get good measurements, but should be somewhere around 20ns - 100ns.
Why?
There are arguments against automatic simplification in general (see #2056). I will give arguments for this particular case.
Result might not be needed
Always calculating the gamma for integers takes time and space to store the often large result and the result might not be needed. Some examples:
Piecewise(x > 0, gamma(8977887798), 0). If doing subs with x=-1 the calculation of the gamma was useless.There might be faster ways
If we have the expression 1001*gamma(1001) instead of calculating gamma(1001) and do the multiplication we could leverage that$\Gamma(z + 1) = z\Gamma(z)$ and instead calculate gamma(1002) removing the need for the extra multiplication.
If we have the expression gamma(1001)*gamma(-1000) we could use that$\Gamma(z) \Gamma(1 - z) = \frac{\pi}{\sin{\pi z}}$ directly.
If we have gamma in an inequality for example
Ge(gamma(2392), 2)we could potentially evaluate the inequality faster without having to calculate the gamma function.All these cases might arise from
subssituations.We might want higher precision
If we have gamma(2.35) it will be automatically calculated as
RealDoublemaking it impossible to evaluate with a higher precision later.Smaller memory footprint
For gamma(237) we would instead of directly calculating and storing the resulting integer we will now instead keep gamma(237) which has a slightly more complex expression tree, but takes less memory since the resulting integer would be much larger (460 decimal digits).
No drawbacks?
One drawback could be if the only thing you want is to calculate gamma for some number now you would need to do
calculate(gamma(2393)).Is this a slippery slope?
I the discussion of #2056 we could see pros and cons of automatic simplification in general. I think this PR still fits under the current system, since as I have argued, there are few benefits of automatic simplification in this particular case, but this must be taken on a case by case basis. I also think this way of thinking can be extended to more cases in
functions.cpp, but not into more core operators likemul,addetc.