|
| 1 | +// |
| 2 | +// Copyright (c) 2019-2026 Angouri. |
| 3 | +// AngouriMath is licensed under MIT. |
| 4 | +// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md. |
| 5 | +// Website: https://am.angouri.org. |
| 6 | +// |
| 7 | + |
| 8 | +using System; |
| 9 | +using System.Linq; |
| 10 | +using System.Threading; |
| 11 | +using System.Threading.Tasks; |
| 12 | +using AngouriMath.Extensions; |
| 13 | +using Xunit; |
| 14 | + |
| 15 | +namespace AngouriMath.Tests.Common |
| 16 | +{ |
| 17 | + /// <summary> |
| 18 | + /// <c>Simplify</c> returns on quotients over a symbolic denominator. Bringing two of their |
| 19 | + /// fractions over a common denominator re-simplifies the cross products, and reading those as |
| 20 | + /// polynomials expands squares of sums of thirty terms and more, whose exponent splits were |
| 21 | + /// enumerated in time exponential in the number of terms. The integrator met them after a |
| 22 | + /// substitution. <a href="https://github.com/asc-community/AngouriMath/issues/233">#233</a> |
| 23 | + /// </summary> |
| 24 | + [Trait("Area", "Common")] |
| 25 | + public sealed class SimplifyOverASymbolicCommonDenominatorTest |
| 26 | + { |
| 27 | + [Fact] |
| 28 | + public void TheSquareOfALongSumExpands() |
| 29 | + { |
| 30 | + var names = Enumerable.Range(0, 30).Select(i => "s" + (char)('a' + i / 26) + (char)('a' + i % 26)).ToArray(); |
| 31 | + var sum = string.Join(" + ", names); |
| 32 | + var expanded = Run(() => $"({sum})^2".ToEntity().Expand()); |
| 33 | + // Thirty squares and 435 products of two, none of which combine. |
| 34 | + Assert.Equal(465, Entity.Sumf.LinearChildren(expanded).Count); |
| 35 | + var at = expanded; |
| 36 | + for (var i = 0; i < names.Length; i++) |
| 37 | + at = at.Substitute(names[i], i + 1); |
| 38 | + Assert.Equal((Entity)(465 * 465), at.Evaled); |
| 39 | + } |
| 40 | + |
| 41 | + // Each returns as it was written: the search finds nothing shorter. Under a second or |
| 42 | + // three each on a desktop, where the first did not return in ten minutes and the |
| 43 | + // third took forty-seven seconds. |
| 44 | + [Theory] |
| 45 | + [InlineData("u^4/((c*u + d*(2 - u))^2*(2 - u))")] |
| 46 | + [InlineData("u^3/((c*u + d)^2*(2 - u))")] |
| 47 | + [InlineData("((A + B*(1 - 2*c/d))*sin(h)*(1 - u^2) + B*(-1/2 + c/d - c^2/(2*d^2))*sin(h))/(c + d*(2*(1 - u^2) - 1))^2/(-sin(h))")] |
| 48 | + public void ReturnsWithTheValueItWasGiven(string written) |
| 49 | + { |
| 50 | + var expression = written.ToEntity(); |
| 51 | + var simplified = Run(() => expression.Simplify(1)); |
| 52 | + var compared = 0; |
| 53 | + foreach (var shift in new[] { 0.0, 0.37, 1.13 }) |
| 54 | + { |
| 55 | + Entity Pinned(Entity e) => e |
| 56 | + .Substitute("A", 1.3 + shift).Substitute("B", -0.7 + shift).Substitute("c", 0.4 + shift) |
| 57 | + .Substitute("d", 1.9 - shift).Substitute("h", 0.6 + shift).Substitute("u", 0.3 + shift); |
| 58 | + var want = Pinned(expression).EvalNumerical(); |
| 59 | + var got = Pinned(simplified).EvalNumerical(); |
| 60 | + if (want.IsNaN) |
| 61 | + continue; |
| 62 | + compared++; |
| 63 | + Assert.True(Math.Abs((double)(got - want).RealPart) + Math.Abs((double)(got - want).ImaginaryPart) |
| 64 | + < 1e-9 * Math.Max(1, Math.Abs((double)want.RealPart) + Math.Abs((double)want.ImaginaryPart)), |
| 65 | + $"{written} simplified to {simplified}, which is {got} where the expression is {want}"); |
| 66 | + } |
| 67 | + Assert.Equal(3, compared); |
| 68 | + } |
| 69 | + |
| 70 | + /// <summary> |
| 71 | + /// On a thread of its own, so that a test host whose pool is busy cannot hold it back, |
| 72 | + /// and with a wall far above the run, so that the bound is reached only by a computation |
| 73 | + /// that does not finish. |
| 74 | + /// </summary> |
| 75 | + private static Entity Run(Func<Entity> work) |
| 76 | + { |
| 77 | + var task = Task.Factory.StartNew(work, CancellationToken.None, |
| 78 | + TaskCreationOptions.LongRunning, TaskScheduler.Default); |
| 79 | + Assert.True(task.Wait(TimeSpan.FromMinutes(2)), "did not return in two minutes"); |
| 80 | + return task.Result; |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments