mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-11-26 00:13:49 +00:00
Upgraded AngouriMath to v2.8.0 (#1235)
This commit is contained in:
1 parent
fae3ceb840
commit
506c06fbfc
24 files changed
+974
-105
No files matched your search
@@ -13,14 +13,17 @@ namespace AIStudio.Tools.Mathematics;
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// AngouriMath computes an exact power or factorial in full, and an expansion term by term, without
|
||||
/// checking its cancellation token meanwhile: 9^9^9, (10^9)!, and the expansion of (x + y)^500 run
|
||||
/// on long past any budget, see AngouriMathBehaviorTests. The guards estimate the size of such a
|
||||
/// checking its cancellation token meanwhile: (10^9)! and the expansion of (x + y)^500 run on long
|
||||
/// past any budget, and so did 9^9^9 up to version 2.6.0; since version 2.7.0, AngouriMath leaves a
|
||||
/// power of more than sixteen million bits written instead. The guards estimate the size of such a
|
||||
/// result from the expression alone, before anything is computed, and refuse it when it would be
|
||||
/// larger than any result the tools return anyway.<br/><br/>
|
||||
/// The estimate works from the inside out, so the inner power of 9^9^9^9 is known before the outer
|
||||
/// one is judged. Only numbers of known value count: a power of a variable is never computed as a
|
||||
/// number. Call the guards inside a computation of MathComputationService, since they evaluate the
|
||||
/// exponents and the arguments of factorials, which have passed the guards before.
|
||||
/// exponents and the arguments of factorials, which have passed the guards before.<br/><br/>
|
||||
/// The same holds for the special functions polygamma and gamma(a, z), which AngouriMath computes for as
|
||||
/// long as their arguments ask, see MathLimits. CheckExactSize refuses them beyond their limits as well.
|
||||
/// </remarks>
|
||||
public static class MathGuards
|
||||
{
|
||||
@@ -28,11 +31,27 @@ public static class MathGuards
|
||||
|
||||
/// <summary>
|
||||
/// Refuses an expression whose exact evaluation would produce a number of more than
|
||||
/// MathLimits.MAX_EXACT_DIGITS digits.
|
||||
/// MathLimits.MAX_EXACT_DIGITS digits, or which takes a special function beyond its limits.
|
||||
/// </summary>
|
||||
/// <param name="expression">The expression to be evaluated or simplified.</param>
|
||||
/// <exception cref="MathComputationException">A part of the expression would grow too large.</exception>
|
||||
public static void CheckExactSize(Entity expression) => EstimateDigits(expression);
|
||||
/// <exception cref="MathComputationException">A part of the expression would grow too large or take too long.</exception>
|
||||
public static void CheckExactSize(Entity expression) => EstimateDigits(expression, false);
|
||||
|
||||
/// <summary>
|
||||
/// Refuses an expression whose variables have values, as at the sample points of NumericCheck, when one of its
|
||||
/// powers or factorials would have more than MathLimits.MAX_EXACT_DIGITS digits, or when it takes a special
|
||||
/// function beyond its limits.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// A value of a variable may turn a part which CheckExactSize let through into one which runs on long past
|
||||
/// the budget: x = 0.61 makes (10^6 * x)! the factorial of 610,000, and x^100000 a fraction of hundreds of
|
||||
/// thousands of digits; in polygamma(1000 * x, 2), x becomes the order. Unlike CheckExactSize, it takes the
|
||||
/// largest of the parts of a sum, a product, or a quotient instead of adding up their digits: the checks
|
||||
/// evaluate long sums, such as the expansion of (x + 1)^200, whose terms each have few digits.
|
||||
/// </remarks>
|
||||
/// <param name="expression">The expression with the values of its variables in their place.</param>
|
||||
/// <exception cref="MathComputationException">A part of the expression would grow too large or take too long.</exception>
|
||||
public static void CheckValueAtPoint(Entity expression) => EstimateDigits(expression, true);
|
||||
|
||||
/// <summary>
|
||||
/// Refuses an expression whose expansion would take a power of a sum beyond
|
||||
@@ -62,14 +81,19 @@ public static class MathGuards
|
||||
/// An upper bound for sums, products, and quotients: the digits of numerator and denominator
|
||||
/// add up. A number which is not exact, such as a decimal of the current precision, needs none.
|
||||
/// </remarks>
|
||||
private static double EstimateDigits(Entity node)
|
||||
/// <param name="node">The node to be estimated.</param>
|
||||
/// <param name="largestPart">Whether a sum, a product, or a quotient counts the digits of its largest part only, see CheckValueAtPoint.</param>
|
||||
private static double EstimateDigits(Entity node, bool largestPart)
|
||||
{
|
||||
var digits = node switch
|
||||
{
|
||||
Entity.Number.Rational rational => DigitsOf(rational.ERational.Numerator) + DigitsOf(rational.ERational.Denominator),
|
||||
Entity.Powf power => EstimatePower(power),
|
||||
Entity.Factorialf factorial => EstimateFactorial(factorial),
|
||||
_ => node.DirectChildren.Sum(EstimateDigits),
|
||||
Entity.Powf power => EstimatePower(power, largestPart),
|
||||
Entity.Factorialf factorial => EstimateFactorial(factorial, largestPart),
|
||||
Entity.Polygammaf polygamma => EstimatePolygamma(polygamma, largestPart),
|
||||
Entity.IncompleteGammaf gamma => EstimateIncompleteGamma(gamma, largestPart),
|
||||
_ when largestPart => node.DirectChildren.Select(child => EstimateDigits(child, true)).DefaultIfEmpty(0).Max(),
|
||||
_ => node.DirectChildren.Sum(child => EstimateDigits(child, false)),
|
||||
};
|
||||
|
||||
if (digits > MathLimits.MAX_EXACT_DIGITS)
|
||||
@@ -78,10 +102,10 @@ public static class MathGuards
|
||||
return digits;
|
||||
}
|
||||
|
||||
private static double EstimatePower(Entity.Powf power)
|
||||
private static double EstimatePower(Entity.Powf power, bool largestPart)
|
||||
{
|
||||
var baseDigits = EstimateDigits(power.Base);
|
||||
var exponentDigits = EstimateDigits(power.Exponent);
|
||||
var baseDigits = EstimateDigits(power.Base, largestPart);
|
||||
var exponentDigits = EstimateDigits(power.Exponent, largestPart);
|
||||
if (MathVariables.HasAny(power.Base))
|
||||
return baseDigits + exponentDigits;
|
||||
|
||||
@@ -104,9 +128,9 @@ public static class MathGuards
|
||||
return magnitude * Math.Max(baseDigits, 0);
|
||||
}
|
||||
|
||||
private static double EstimateFactorial(Entity.Factorialf factorial)
|
||||
private static double EstimateFactorial(Entity.Factorialf factorial, bool largestPart)
|
||||
{
|
||||
var argumentDigits = EstimateDigits(factorial.Argument);
|
||||
var argumentDigits = EstimateDigits(factorial.Argument, largestPart);
|
||||
var argument = ValueOf(factorial.Argument);
|
||||
if (argument is null)
|
||||
return argumentDigits;
|
||||
@@ -119,6 +143,43 @@ public static class MathGuards
|
||||
return n * Math.Log10(n / Math.E) + Math.Log10(2 * Math.PI * n) / 2 + 1;
|
||||
}
|
||||
|
||||
private static double EstimatePolygamma(Entity.Polygammaf polygamma, bool largestPart)
|
||||
{
|
||||
var digits = EstimateDigits(polygamma.Order, largestPart) + EstimateDigits(polygamma.Argument, largestPart);
|
||||
var order = ValueOf(polygamma.Order);
|
||||
if (order > MathLimits.MAX_POLYGAMMA_ORDER)
|
||||
throw new MathComputationException(string.Create(CultureInfo.InvariantCulture, $"'{Show(polygamma)}' has the order {order.Value:0.###}. The tool cannot stop the computation of polygamma midway, and its time grows with the order, so the order is limited to {MathLimits.MAX_POLYGAMMA_ORDER}."));
|
||||
|
||||
return digits;
|
||||
}
|
||||
|
||||
private static double EstimateIncompleteGamma(Entity.IncompleteGammaf gamma, bool largestPart)
|
||||
{
|
||||
var digits = EstimateDigits(gamma.Parameter, largestPart) + EstimateDigits(gamma.LowerLimit, largestPart);
|
||||
var argument = NumberOf(gamma.LowerLimit);
|
||||
if (argument is not null && AbsoluteValueOf(argument) > MathLimits.MAX_INCOMPLETE_GAMMA_ARGUMENT)
|
||||
throw new MathComputationException(string.Create(CultureInfo.InvariantCulture, $"'{Show(gamma)}' takes gamma(a, z) at a z of about {AbsoluteValueOf(argument):0.###} in absolute value. The tool cannot stop this computation midway, and its time grows with z, so the absolute value of z is limited to {MathLimits.MAX_INCOMPLETE_GAMMA_ARGUMENT:N0}."));
|
||||
|
||||
var parameter = NumberOf(gamma.Parameter);
|
||||
if (parameter is not null && !IsAllowedIncompleteGammaParameter(parameter))
|
||||
throw new MathComputationException(string.Create(CultureInfo.InvariantCulture, $"'{Show(gamma)}' takes gamma(a, z) for a = {Show(parameter)}. The tool cannot stop this computation midway, and its time grows with z, the more so when a is negative or not real, so a is limited to the real numbers from {MathLimits.MIN_INCOMPLETE_GAMMA_PARAMETER} to {MathLimits.MAX_INCOMPLETE_GAMMA_PARAMETER}."));
|
||||
|
||||
return digits;
|
||||
}
|
||||
|
||||
private static bool IsAllowedIncompleteGammaParameter(Entity.Number.Complex parameter)
|
||||
{
|
||||
var real = parameter.RealPart.EDecimal.ToDouble();
|
||||
return parameter.ImaginaryPart.EDecimal.IsZero && real is >= MathLimits.MIN_INCOMPLETE_GAMMA_PARAMETER and <= MathLimits.MAX_INCOMPLETE_GAMMA_PARAMETER;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The value of a part without variables, as a number, or null when it has none.
|
||||
/// </summary>
|
||||
private static Entity.Number.Complex? NumberOf(Entity part) => MathVariables.HasAny(part) ? null : part.Evaled as Entity.Number.Complex;
|
||||
|
||||
private static double AbsoluteValueOf(Entity.Number.Complex number) => double.Hypot(number.RealPart.EDecimal.ToDouble(), number.ImaginaryPart.EDecimal.ToDouble());
|
||||
|
||||
/// <summary>
|
||||
/// The value of a part without variables, as a double, or null when it has none.
|
||||
/// </summary>
|
||||
|
||||
Reference in new issue
Block a user