Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,23 @@ sum, `arcsin(a)` or `arccos(1/3)`, is written as before
| `cos(x) = -1` | `union({ pi / 2 - -pi / 2 + 2 * pi * n, -(pi / 2 - -pi / 2) + 2 * pi * n }, n in ZZ)` | `union({ pi + 2 * pi * n }, n in ZZ)` |
| `2cos(x)^2 + cos(x) - 1 = 0` | four members, among them `pi / 2 - -pi / 2` and `-(pi / 2 - -pi / 2)` | `union({ pi + 2 * pi * n, pi / 3 + 2 * pi * n, -pi / 3 + 2 * pi * n }, n in ZZ)` |

### A root of a numeric search is found at the size of the equation's numbers

**Not silent: an incomplete answer has more roots.** The numeric search starts from a grid from `-10` to `10`, and
its tolerances are made for roots of about unit size. `10^20 arcsin(x / 10^20) = x pi / 3` has the roots of
`arcsin(t) = t pi / 3` times `10^20`, and it was answered with `0` alone. An equation is now searched once more for each
decimal size of its whole and rational numbers, at least two orders from 1 and at most six sizes: in `x = 10^e t`, with
its values divided by their size there. The roots found are put back. An equation whose numbers are all near 1 is
searched as before, and where a search at another size cannot be run, the answer at unit size stands
([#2643](https://github.com/asc-community/AngouriMath/issues/2643)).

| `.Solve("x")` | Was (2.10.0) | Now |
|---|---|---|
| `10^20 arcsin(x / 10^20) = x pi / 3` | `{ 0 }` | `{ 0, -50000000000000000000, 50000000000000000000 }` |
| `10^20 arctan(x / 10^20) = x / 2` | `{ 0 }` | `{ 0, -2.3311…E+20, 2.3311…E+20 }` |
| `10^(-12) arcsin(x / 10^(-12)) = x pi / 3` | `{ 0 }` | `{ 0, -5E-13, 5E-13 }` |
| `arcsin(x) = x pi / 3` | `{ -0.4999…, 0, 0.4999… }` | unchanged |

### A definite integral of a function of abs is split where its argument changes sign

**An answer where there was none.** A definite integral whose integrand holds `abs(u)` was left unevaluated unless the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,107 @@ internal static HashSet<Complex> SolveNt(Entity expr, Entity.Variable v, MathS.S
=> Search(expr, v, settings, forTheSolver: true);

private static HashSet<Complex>? Search(Entity expr, Entity.Variable v, MathS.Settings.NewtonSetting settings, bool forTheSolver)
{
if (SearchAtOneScale(expr, v, settings, forTheSolver) is not { } found)
return null;
// The grid, the steps the iteration settles to and the residual a root is kept by are all in
// absolute terms, made for roots and values of about unit size. An equation whose numbers are
// powers of ten far from 1 can have its roots, and its values, at that size instead:
// 10^20 arcsin(x / 10^20) = x pi / 3 has the roots of arcsin(t) = t pi / 3 times 10^20, which the
// grid from -10 to 10 does not reach, and was answered with 0 alone. So it is searched once more
// for each such size of its numbers, in x = 10^e t and divided by the size its values have there,
// and the roots found are put back. A root found at more than one size is kept once.
// https://github.com/asc-community/AngouriMath/issues/2643
foreach (var exponent in ExponentsOfItsNumbers(expr))
{
var size = Rational.Create(EInteger.FromInt32(10).Pow(Math.Abs(exponent)));
Entity scaleOfX = exponent > 0 ? size : 1 / size;
var inT = expr.Substitute(v, scaleOfX * v);
if (SizeOfItsValues(inT, v) is { } valueSize)
inT = (inT / valueSize).InnerSimplified;
// A search at another size that cannot be run, or whose points are not isolated, adds nothing; the
// answer at unit size stands as it was.
if (SearchAtOneScale(inT, v, settings, forTheSolver) is not { } atThisScale)
continue;
foreach (var t in atThisScale)
{
var root = Complex.Create(t.RealPart.EDecimal.ScaleByPowerOfTen(exponent), t.ImaginaryPart.EDecimal.ScaleByPowerOfTen(exponent));
if (!found.Any(known => AreOneRootAtAnySize(known, root)) && HoldsAsWritten(expr, v, root))
found.Add(root);
}
}
return found;
}

/// <summary>
/// Whether a root found at another size holds in <paramref name="expr"/> itself: defined there, and leaving a
/// residual that is no evidence against it beside the terms the equation adds up to.
/// </summary>
/// <remarks>
/// A search at another size is of the equation divided by the size of its values at a few points, and a point
/// where the equation is small beside that size passes the search's absolute floor without being a root.
/// <c>(1 + 4 10^30 x)^(1/3) + sqrt(-1 - 10^30 x) = -4</c>, which has no roots, is about <c>10^30</c> in size away
/// from 0 and <c>5 + i</c> at 0, and 0 came back from the search at <c>10^30</c>. It was then dropped as no root
/// of the equation, and the search, which had found nothing at unit size, answered <c>{ }</c>, where an empty search
/// leaves an equation unsolved. So a search at another size adds only roots that hold as the equation is
/// written, and never makes the answer of the search at unit size empty where it was not.
/// https://github.com/asc-community/AngouriMath/issues/2643
/// </remarks>
private static bool HoldsAsWritten(Entity expr, Entity.Variable v, Complex root)
{
var substituted = expr.Substitute(v, root);
return !AnalyticalSolving.StatementSolver.IsUndefinedExactly(substituted)
&& substituted.Evaled is Complex { IsFinite: true }
&& !AnalyticalSolving.StatementSolver.IsEvidenceAgainst(substituted);
}

/// <summary>
/// The decimal exponents, at least two in size, of the whole and rational numbers in
/// <paramref name="expr"/>, the largest first and at most <see cref="MostOtherScales"/> of them.
/// </summary>
private static List<int> ExponentsOfItsNumbers(Entity expr)
=> expr.InnerSimplified.Nodes.OfType<Rational>()
.Where(number => !number.ERational.IsZero)
.Select(number => (int)Math.Round(Math.Log10(Math.Abs(number.ERational.ToDouble()))))
.Where(exponent => Math.Abs(exponent) >= 2 && Math.Abs(exponent) < 300)
.Distinct()
.OrderByDescending(exponent => Math.Abs(exponent)).ThenByDescending(exponent => exponent)
.Take(MostOtherScales)
.ToList();

/// <summary>How many sizes other than 1 an equation is searched at.</summary>
private const int MostOtherScales = 6;

/// <summary>
/// The power of ten nearest the largest value <paramref name="expr"/> takes at a few points of
/// about unit size, or <see langword="null"/> where it takes none there.
/// </summary>
private static Entity? SizeOfItsValues(Entity expr, Entity.Variable v)
{
var largest = 0.0;
foreach (var point in new[] { "0.37", "-0.83", "1.29", "-2.11", "0.61i" })
if (expr.Substitute(v, point.ToEntity()).Evaled is Complex { IsFinite: true, IsNaN: false } value)
largest = Math.Max(largest, value.Abs().EDecimal.ToDouble());
if (largest == 0 || double.IsInfinity(largest) || double.IsNaN(largest))
return null;
var exponent = (int)Math.Round(Math.Log10(largest));
var power = Rational.Create(EInteger.FromInt32(10).Pow(Math.Abs(exponent)));
return exponent >= 0 ? power : 1 / power;
}

/// <summary>
/// Whether two roots found at different sizes are one, to a part in 10^9 of the larger, or both 0: relative
/// at every size, where <see cref="AreTheSameRoot"/> is for roots of one search and absolute below 1.
/// </summary>
private static bool AreOneRootAtAnySize(Complex a, Complex b)
{
var difference = ((Complex)(a - b)).Abs().EDecimal;
var size = EDecimal.Max(a.Abs().EDecimal, b.Abs().EDecimal);
return difference.IsZero || difference.CompareTo(size.Multiply(EDecimal.Create(1, -9))) <= 0;
}

/// <summary>The search at the size the settings give, which is unit size by default.</summary>
private static HashSet<Complex>? SearchAtOneScale(Entity expr, Entity.Variable v, MathS.Settings.NewtonSetting settings, bool forTheSolver)
{
// Perform one iteration of searching for a root with Newton-Raphson method
static Complex NewtonIter(FastExpression f, FastExpression df, NumericsComplex value, int precision)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// Copyright (c) 2019-2026 Angouri.
// AngouriMath is licensed under MIT.
// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.
// Website: https://am.angouri.org.
//

using System.Linq;
using AngouriMath;
using AngouriMath.Extensions;
using Xunit;
using static AngouriMath.Entity;
using static AngouriMath.Entity.Set;

namespace AngouriMath.Tests.Algebra.SolveTest
{
/// <summary>
/// The numeric search starts from a grid from -10 to 10, and its tolerances are made for roots of about unit size.
/// <c>10^k arcsin(x / 10^k) = x pi / 3</c> has the roots of <c>arcsin(t) = t pi / 3</c> times <c>10^k</c>, and was
/// answered with 0 alone at k = 20. It is searched at the sizes of its numbers as well now.
/// https://github.com/asc-community/AngouriMath/issues/2643
/// </summary>
[Trait("Area", "Algebra")]
public sealed class TheNumericSearchFollowsTheSizeOfTheNumbersTest
{
[Theory]
[InlineData("arcsin(x / 10^({0})) * 10^({0}) = x pi / 3", 0.5)]
[InlineData("arctan(x / 10^({0})) * 10^({0}) = x / 2", 2.331122370414422)]
[InlineData("tanh(x / 10^({0})) * 10^({0}) = x / 2", 1.915008048154537)]
public void TheRealRootsScaleWithTheEquation(string written, double positiveRoot)
{
foreach (var k in new[] { -30, -20, -12, -6, -3, 3, 6, 12, 20, 30 })
{
var equation = string.Format(written, k);
var roots = Assert.IsType<FiniteSet>(equation.ToEntity().Solve("x"))
.Select(root => (Number.Complex)root.Evaled)
.Where(root => root.ImaginaryPart.IsZero)
.Select(root => root.RealPart.EDecimal.ScaleByPowerOfTen(-k).ToDouble())
.ToList();
foreach (var expected in new[] { -positiveRoot, 0, positiveRoot })
Assert.True(roots.Any(root => System.Math.Abs(root - expected) <= 1e-9 * System.Math.Max(1, System.Math.Abs(expected))),
$"{equation}: {expected} * 10^{k} is not among [{string.Join(", ", roots)}] (each over 10^{k})");
}
}
}
}
Loading