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
23 changes: 23 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,29 @@ decided, such as `sqrt(a)`, is kept intersected with `RR`. Measured on master (2
| `"x^3 - 2 <= 0".Solve("x")` | `{ (-1/2 + i * 1/2 * sqrt(3)) * 2 ^ (1/3), (-1/2 + i * -1/2 * sqrt(3)) * 2 ^ (1/3) } \/ (-oo; 2 ^ (1/3)]` | `(-oo; 2 ^ (1/3)]` |
| `"x^2 <= a".Solve("x")` | `{ sqrt(a), -sqrt(a) } \/ { x : -(-4) * a >= 0 and x in (-1/2 * abs(2 * sqrt(a)); abs(2 * sqrt(a)) / 2) }` | `{ x : -(-4) * a >= 0 and x in (-1/2 * abs(2 * sqrt(a)); abs(2 * sqrt(a)) / 2) } \/ { sqrt(a), -sqrt(a) } /\ RR` |

### A value tan, cotan, tanh, sec, cosec or a power never takes has no roots, and a far-out near-root is not one

**Silent: numbers that were not roots are gone.** `tan(w)` is `i` only where `e^(2 i w) = 0`, which is nowhere, and
likewise `tan` and `cotan` never take `-i`, `tanh` and `coth` never take `-1`, `sec` and `cosec` never take 0, and a
power of a number that is not 0 never takes 0. An equation through one of these at such a value has no roots whatever
is inside; it went on past the analytical answer to Newton's method, which answered points where the equation only
tends to hold, `x - i cos(arcsin(x))` being `1 / (2x)` far out. Such an equation is now `{ }`, and Newton's method
keeps a point only where the Newton step from it is shorter than a thousandth of its size (at least 1). Measured on
master (a3f3b9ed1) and on this branch; every one of 90 changed rows in a sweep of 1408 is such an equation
([#2763](https://github.com/asc-community/AngouriMath/issues/2763)).

| `.Solve("x")` | Was (master) | Now |
|---|---|---|
| `tan(arcsin(x)) = i` | `{ -24258808.638504 + 87745223.784967i, 24258808.638504 + 87745223.784967i }` | `{ }` |
| `cotan(arccos(x)) = i` | three points of size about 10^8 | `{ }` |
| `tan(x^2 + 1) = i` | forty points from `-8.04 - 8.00i` to `10.05 + 8.02i` | `{ }` |
| `tan(arcsec(x)) = i` | dozens of points `5.9E-10i` to `5.1E-9i` | `{ }` |
| `tan(sinh(x)) = i` | `union({ ln(-204.20823846791 + 40.00362744916i) + 2 * pi * i * n, … }, n in ZZ)` | `{ }` |
| `tanh(cosh(x)) = -1` | `{ -8 - 10i, -8 - 4i, …, 10 + 10i }`, twenty points of the search's grid | `{ }` |
| `tan(ln(x)) = i` | `{ x : tan(ln(x)) = i }` | `{ }` |
| `e^(artanh(x)) = 0` | `{ -1 }`, where `artanh` has no value | `{ }` |
| `x - i cos(arcsin(x)) = 0` | `{ -24258808.638504 + 87745223.784967i, 24258808.638504 + 87745223.784967i }` | `{ x : x - i * cos(arcsin(x)) = 0 }` |

### An odd root of a negative number is real with downcasting off as well

**Silent, with `MathS.Settings.DowncastingEnabled` off only.** An odd root of a negative number is the real one:
Expand Down
6 changes: 6 additions & 0 deletions Sources/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -1109,9 +1109,15 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
[Tests/UnitTests/Common/StatementAsANumberCompilationTest.cs]
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n

[AngouriMath/Functions/Continuous/Solvers/EquationSolver/OmittedValueSolver.cs]
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n

[AngouriMath/Functions/Continuous/Solvers/EquationSolver/LambertWSolver.cs]
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n

[Tests/UnitTests/Algebra/SolveTest/AValueAFunctionNeverTakesHasNoRootsTest.cs]
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n

[Tests/UnitTests/Algebra/SolveTest/LambertWSolveTest.cs]
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,12 @@ internal static Set Solve(Entity expr, Variable x, bool compensateSolving = fals
return new ConditionalSet(x, residual.Equalizes(0));
}

// tan(arcsin(x)) = i has no root, since tan(w) = i for no w; the spellings tried for it
// went on to Newton's method and answered points far out where it nearly holds.
// https://github.com/asc-community/AngouriMath/issues/2763
if (!compensateSolving && OmittedValueSolver.Solve(expr, x) is { } neverTaken)
return neverTaken;

// Over the reals an equation in moduli of linear functions is solved by cases, between
// the kinks: |x - 2| = |x - 3| is x = 5/2, Sullivan and Mackey's Prob 1.5.16.
// https://github.com/asc-community/AngouriMath/issues/1573
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
//
// 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.Collections.Generic;
using System.Linq;
using static AngouriMath.Entity;
using static AngouriMath.Entity.Number;

namespace AngouriMath.Functions.Algebra.AnalyticalSolving
{
/// <summary>
/// An equation in <c>x</c> only through one function <c>F(u)</c>, every root of which, as an
/// equation in <c>F(u)</c>, is a value <c>F</c> never takes: it has no roots.
/// <c>tan(arcsin(x)) = i</c> is <c>{ }</c>. https://github.com/asc-community/AngouriMath/issues/2763
/// </summary>
/// <remarks>
/// <para>
/// The values each function omits at every finite argument <c>w</c>:
/// <c>tan(w) = i</c> is <c>e^(2 i w) = 0</c> and <c>tan(w) = -i</c> is <c>e^(-2 i w) = 0</c>, and
/// <c>cotan</c> the same, so both omit <c>i</c> and <c>-i</c>; <c>sec</c> and <c>cosec</c>, one over
/// a finite number, omit 0; and <c>b^w = e^(w ln(b))</c> omits 0 for a number <c>b</c> that is
/// not 0.
/// </para>
/// <para>
/// The equation <c>tan(t) = i</c> is already answered <c>{ }</c>, and the replacement of the inner
/// subtree finds that. But the spellings tried after it, <c>x / cos(arcsin(x)) = i</c> among them,
/// went on to Newton's method, which answered points of size 10^8 where
/// <c>x - i cos(arcsin(x))</c> is <c>1 / (2x)</c>, under its tolerance but not 0. Answered here,
/// before any spelling, the equation is never searched.
/// </para>
/// </remarks>
internal static class OmittedValueSolver
{
/// <summary>
/// <see cref="Set.Empty"/> where <paramref name="expr"/> <c>= 0</c> is such an equation,
/// otherwise <see langword="null"/>.
/// </summary>
internal static Set? Solve(Entity expr, Variable x)
{
// Only numbers besides x, so that only numbers are left besides t: a tan(x^2) = i a holds
// everywhere at a = 0. Asked first, as it is the cheapest: most equations with a parameter
// end here, before any node is looked at.
foreach (var variable in expr.Vars)
if (variable != x)
return null;
int? occurrences = null;
foreach (var node in expr.Nodes)
{
if (OmittedValues(node, x) is not { } omitted
|| expr.CountNode(node) * node.CountNode(x) != (occurrences ??= expr.CountNode(x)))
continue;
var t = Variable.CreateTemp(expr.Vars);
var inT = expr.Substitute(node, t);
// A polynomial all of whose roots are omitted values vanishes at one of them; most do not,
// and are let go before their roots are looked for.
if (!IsPolynomialIn(inT, t)
|| !omitted.Any(value => inT.Substitute(t, value).Evaled is Complex { IsZero: true })
|| PolynomialSolver.SolveAsPolynomial(inT, t, out var isIdentity) is not { } roots
|| isIdentity)
return null;
var all = roots.Select(root => root.InnerSimplified).ToList();
return all.Count > 0 && all.All(root => omitted.Any(value => IsExactly(root, value)))
? Set.Empty : null;
}
return null;
}

/// <summary>
/// Whether <paramref name="expr"/> is written as a polynomial in <paramref name="t"/>: sums,
/// differences and products of it, its whole powers, and quotients by what does not contain it.
/// A quotient by <c>t</c> could have its roots cancelled or added, and a polynomial's roots are
/// all of its roots. <c>tanh(u) = -1</c> is <c>(t - 1) / (t + 1) = -1</c> in <c>t = e^(2u)</c>, and is
/// left to the solvers after this.
/// </summary>
private static bool IsPolynomialIn(Entity expr, Variable t)
=> !expr.ContainsNode(t) || expr switch
{
Variable when expr == t => true,
Sumf(var a, var b) => IsPolynomialIn(a, t) && IsPolynomialIn(b, t),
Minusf(var a, var b) => IsPolynomialIn(a, t) && IsPolynomialIn(b, t),
Mulf(var a, var b) => IsPolynomialIn(a, t) && IsPolynomialIn(b, t),
Divf(var a, var b) => !b.ContainsNode(t) && IsPolynomialIn(a, t),
Powf(var a, Integer { EInteger: var n }) => n.Sign > 0 && IsPolynomialIn(a, t),
_ => false,
};

/// <summary>
/// The values <paramref name="node"/>'s function never takes, where the node has
/// <paramref name="x"/> in its argument; otherwise <see langword="null"/>.
/// </summary>
private static Complex[]? OmittedValues(Entity node, Variable x)
=> node switch
{
Tanf(var argument) when argument.ContainsNode(x) => new[] { Complex.ImaginaryOne, Complex.MinusImaginaryOne },
Cotanf(var argument) when argument.ContainsNode(x) => new[] { Complex.ImaginaryOne, Complex.MinusImaginaryOne },
Secantf(var argument) when argument.ContainsNode(x) => new Complex[] { Integer.Zero },
Cosecantf(var argument) when argument.ContainsNode(x) => new Complex[] { Integer.Zero },
Powf(var @base, var power) when power.ContainsNode(x) && IsNonZeroConstant(@base) => new Complex[] { Integer.Zero },
_ => null,
};

/// <summary>
/// A base known not to be 0 without evaluating it: an exact number that is not 0, or
/// <c>e</c> or <c>pi</c>.
/// </summary>
private static bool IsNonZeroConstant(Entity @base)
=> @base == MathS.e || @base == MathS.pi
|| @base is Complex number && IsExact(number) && !number.IsZero;

private static bool IsExact(Complex number)
=> number.RealPart is Rational && number.ImaginaryPart is Rational;

/// <summary>Whether <paramref name="root"/> is the number <paramref name="value"/>, compared exactly.</summary>
private static bool IsExactly(Entity root, Complex value)
=> root is Complex number && IsExact(number)
&& ((Rational)number.RealPart).ERational.CompareTo(((Rational)value.RealPart).ERational) == 0
&& ((Rational)number.ImaginaryPart).ERational.CompareTo(((Rational)value.ImaginaryPart).ERational) == 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,11 @@ void IterateFrom(NumericsComplex start)
// https://github.com/asc-community/AngouriMath/issues/1420
if (forTheSolver && !AreIsolated(distinct, f))
return null;
return new HashSet<Complex>(distinct.Select(root => Refined(function, derivative, v, root)));
var refined = new HashSet<Complex>();
foreach (var root in distinct)
if (Refined(function, derivative, v, root) is { } kept)
refined.Add(kept);
return refined;
}

/// <summary>
Expand All @@ -250,7 +254,9 @@ void IterateFrom(NumericsComplex start)

/// <summary>
/// <paramref name="root"/>, found in <c>double</c>, carried to the working precision by Newton's
/// steps in the library's own evaluation, and given to the digits the error left is known to.
/// steps in the library's own evaluation, and given to the digits the error left is known to; or
/// <see langword="null"/> where the first of those steps is longer than a thousandth of the root's
/// size, at least 1, so that no root is near it.
/// </summary>
/// <remarks>
/// <para>
Expand All @@ -272,7 +278,7 @@ void IterateFrom(NumericsComplex start)
/// <see cref="DoubleDigits"/> a <c>double</c> is sure of.
/// </para>
/// </remarks>
private static Complex Refined(Entity function, Entity derivative, Entity.Variable v, Complex root)
private static Complex? Refined(Entity function, Entity derivative, Entity.Variable v, Complex root)
{
var context = MathS.Settings.DecimalPrecisionContext.Value;
if (context.Precision.IsZero || !context.Precision.CanFitInInt32())
Expand All @@ -293,6 +299,17 @@ private static Complex Refined(Entity function, Entity derivative, Entity.Variab
{
if (Step(current, System.Math.Min(digits, working)) is not var (next, step))
return RoundedTo(root, DoubleDigits);
// A root is where the Newton step from it is short, and not only where the value is small: a
// function that tends to 0 far out is under any tolerance there, and the step from such a point
// is as long as the point. x - i cos(arcsin(x)) is 1 / (2x) for large x, and points of size 10^8
// were answered as roots of it; in double it is 0 there outright. At a root found in double the
// first step is the error left, about 10^-16 of its size, and at a root of order m about the m-th
// root of that over m, inside a thousandth up to m = 6. A first step of 0 can be the value read as 0
// below the downcasting floor at these digits, as 1 / (2x) is at 10^31, and is taken again at all
// of them. https://github.com/asc-community/AngouriMath/issues/2763
if (count == 0 && (step.IsZero && working < digits && Step(current, digits) is var (_, again) ? again : step) is var first
&& first.Abs().EDecimal.CompareTo(EDecimal.Max(EDecimal.One, root.Abs().EDecimal).Multiply(EDecimal.Create(1, -3))) > 0)
return null;
current = next;
last = step;
if (working >= digits && step.Abs().EDecimal.CompareTo(current.Abs().EDecimal.Multiply(enough)) <= 0)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//
// 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.Extensions;
using Xunit;

namespace AngouriMath.Tests.Algebra
{
/// <summary>
/// <c>tan(w) = i</c> is <c>e^(2 i w) = 0</c>, so <c>tan</c> never takes <c>i</c> or <c>-i</c>, nor
/// does <c>cotan</c>; <c>sec</c> and <c>cosec</c> never take 0, nor does a power of a number that
/// is not 0. An equation in x through one of them alone, at such a value, has no roots, whatever
/// is inside. <c>tan(arcsin(x)) = i</c> was answered with two points of size 10^8, found by
/// Newton's method on <c>x - i cos(arcsin(x))</c>, which is <c>1 / (2x)</c> there.
/// https://github.com/asc-community/AngouriMath/issues/2763
/// </summary>
[Trait("Area", "Algebra")]
public sealed class AValueAFunctionNeverTakesHasNoRootsTest
{
[Theory]
[InlineData("tan(arcsin(x)) = i")]
[InlineData("tan(arcsin(x)) = -i")]
[InlineData("cotan(arccos(x)) = i")]
[InlineData("2 tan(arcsin(x)) = 2i")]
[InlineData("tan(arcsin(x))^2 = -1")]
[InlineData("tan(x^2 + 1) = i")]
[InlineData("tan(sqrt(x)) = -i")]
[InlineData("sec(x^2) = 0")]
[InlineData("2^(x^2 + x) = 0")]
public void HasNoRoots(string equation)
=> Assert.True(Assert.IsType<Entity.Set.FiniteSet>(equation.ToEntity().Solve("x")).IsSetEmpty);

/// <summary>
/// A root of a value only one of whose roots is omitted is still solved for the other:
/// <c>(tan(x) - i)(tan(x) - 1) = 0</c> is <c>tan(x) = 1</c>, <c>x = pi/4 + pi n</c>.
/// </summary>
[Fact]
public void AnotherValueIsStillSolved()
{
var roots = "(tan(x) - i)(tan(x) - 1) = 0".ToEntity().Solve("x");
Assert.False(roots is Entity.Set.FiniteSet { IsSetEmpty: true });
Assert.Contains("pi / 4", roots.ToString());
}

/// <summary>
/// Newton's method keeps a point only where the step from it is short. <c>x - i cos(arcsin(x))</c>
/// has no root, since <c>x = i sqrt(1 - x^2)</c> squares to <c>0 = -1</c>, and is <c>1 / (2x)</c>
/// far out, under the search's tolerance but with a step as long as the point; and in double it
/// is 0 there outright. Left unsolved, which is honest; the points are not roots.
/// </summary>
[Fact]
public void APointWhereTheFunctionOnlyTendsToZeroIsNotARoot()
{
var roots = "x - i cos(arcsin(x)) = 0".ToEntity().Solve("x");
Assert.False(roots is Entity.Set.FiniteSet { IsSetEmpty: false });
}

/// <summary>The search still answers a genuine root: <c>cos(x) = x</c> at 0.7390851332151607.</summary>
[Fact]
public void AGenuineRootIsStillFound()
{
var roots = Assert.IsType<Entity.Set.FiniteSet>("cos(x) = x".ToEntity().Solve("x"));
Assert.Contains(roots, root => root.EvalNumerical() is var value
&& System.Math.Abs(value.RealPart.EDecimal.ToDouble() - 0.7390851332151607) < 1e-12
&& value.ImaginaryPart.IsZero);
}
}
}
Loading