Skip to content

Commit d9ff463

Browse files
Answer a family with a whole-number parameter (#2205)
sin(x) = 1/2 in x and y was declined. Its families come from the periodic root pi/6 + 2 pi n_1, and the check that each family holds pinned n_1 at 1.37, where it does not, and asked Simplify, which knows nothing about n_1. The non-square solver now pins an n_1 a family brings at a whole number, and decides the residual as forall n_1 in ZZ : residual = 0. The quantifier establishes that n_1 is whole, and a new reader takes a whole number of periods out of a trigonometric function's argument under it: sin and cos lose a whole multiple of 2 pi, tan and cot one of pi, as Complex.sin_add_int_mul_two_pi and Complex.tan_add_int_mul_pi have it. The reader is asked only under a quantifier over whole numbers, and outside one nothing changes. sin(x) - 1/2 in x, y is [[pi/6 + 2 pi n_1, t_1], [pi - pi/6 + 2 pi n_1, t_1]], and forall n in ZZ : sin(x + 2 pi n) = sin(x) is True. Part of #212 Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura
1 parent 7633d18 commit d9ff463

7 files changed

Lines changed: 175 additions & 14 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,18 +401,34 @@ over the rationals with rational solutions or had fewer equations and was linear
401401
system is now eliminated one unknown at a time, and an unknown that nothing is left to constrain becomes a parameter,
402402
`t_1`, as in the linear case. Every row is substituted back into every equation and kept only where each simplifies to
403403
zero. Where one does not, or where a branch cannot be decided, the system is declined as `null`, which `Solve`
404-
documents as no solution found ([#212](https://github.com/asc-community/AngouriMath/issues/212)). Code that caught the
405-
exception around `Solve` to detect a non-square system no longer sees it.
404+
documents as no solution found ([#212](https://github.com/asc-community/AngouriMath/issues/212)). A family with an
405+
`n_1` from a periodic root, which stands for every whole number, is kept where it is zero for each, as
406+
`forall n_1 in ZZ` decides it. Code that caught the exception around `Solve` to detect a non-square system no longer
407+
sees it.
406408

407409
| Input | Was (2.6.0) | Now |
408410
|---|---|---|
409411
| `MathS.Equations("x^2 + y^2 - 1").Solve("x", "y")` | `WrongNumberOfArgumentsException` | `[[t_1, sqrt(-(t_1 ^ 2 + -1))], [t_1, -sqrt(-(t_1 ^ 2 + -1))]]` |
410412
| `MathS.Equations("a*x + y - 1").Solve("x", "y")` | `WrongNumberOfArgumentsException` | `[[t_1, -(a * t_1 + -1)]]` |
411413
| `MathS.Equations("sin(x)", "x").Solve("x")` | `WrongNumberOfArgumentsException` | `[0]` |
414+
| `MathS.Equations("sin(x) - 1/2").Solve("x", "y")` | `WrongNumberOfArgumentsException` | `[[pi / 6 + 2 * pi * n_1, t_1], [pi - pi / 6 + 2 * pi * n_1, t_1]]` |
412415
| `MathS.Equations("sin(x) - y", "cos(x) - y", "x - pi/4").Solve("x", "y")` | `WrongNumberOfArgumentsException` | `[[pi / 4, 1/2 * sqrt(2)]]` |
413416
| `MathS.Equations("sqrt(y) - x").Solve("x", "y")` | `WrongNumberOfArgumentsException` | `null`: `y = x^2` is no solution where `x` is negative |
414417
| `MathS.Equations("x + y - a", "x - y - b", "x - c").Solve("x", "y")` | `WrongNumberOfArgumentsException` | `null`: there are solutions only where `a + b = 2c` |
415418

419+
### A whole number of periods comes out of a trigonometric function under a quantifier
420+
421+
**Silent: a statement quantified over whole numbers is decided where it was left as written.** Where the quantifiers
422+
around a sine, a cosine, a secant or a cosecant establish that a term of its argument is a whole multiple of `2 pi`,
423+
or of `pi` for a tangent or a cotangent, the term is taken out, as `Complex.sin_add_int_mul_two_pi` and
424+
`Complex.tan_add_int_mul_pi` have it. Nothing changes outside a quantifier, and `sin(x + pi n)` keeps its `pi n`
425+
([#212](https://github.com/asc-community/AngouriMath/issues/212)).
426+
427+
| Input | Was (2.6.0) | Now |
428+
|---|---|---|
429+
| `"forall n in ZZ : sin(x + 2 pi n) = sin(x)".ToEntity().Simplify()` | `forall n in ZZ : sin(2 * n * pi + x) = sin(x)` | `True` |
430+
| `"forall n in ZZ : tan(pi/4 + pi n) = 1".ToEntity().Simplify()` | `forall n in ZZ : tan(pi * (1/4 + n)) = 1` | `True` |
431+
416432
### A system is not answered with a copy of one of its rows
417433

418434
**Silent: a repeated row goes.** Two branches of an elimination can reach one solution, and each was answered: two

‎Sources/.editorconfig‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,9 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
402402
[Tests/UnitTests/Core/FloorByFactsTest.cs]
403403
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
404404

405+
[Tests/UnitTests/Core/PeriodByFactsTest.cs]
406+
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
407+
405408
[AngouriMath/Core/Entity/Continuous/Number/ErrorFunctions.cs]
406409
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
407410

‎Sources/AngouriMath/Functions/Boolean/QuantifierFacts.cs‎

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ private sealed class Frame
7676
/// </summary>
7777
internal static (Entity? Verdict, Entity Body)? Decide(Quantifiers.Kind kind, Entity var, Entity over, Entity body, bool isExact)
7878
{
79-
if (var is not Variable x || over is not Set set || !body.Nodes.Any(Reads))
79+
if (var is not Variable x || over is not Set set
80+
|| !body.Nodes.Any(node => Reads(node) || set is SpecialSet whole && Quantifiers.IsIntegerSet(whole) && IsPeriodic(node)))
8081
return null;
8182
var renamed = Variable.CreateVariableUnchecked($"{x.Name}'{Interlocked.Increment(ref lastRenamed)}");
8283
var mark = ProofRecording.Mark();
@@ -450,6 +451,46 @@ internal static bool PrimeDividesByTheBinomialTheorem(Entity p, Entity d)
450451
return null;
451452
}
452453

454+
/// <summary>
455+
/// The function of <paramref name="argument"/> with each term of it that is a whole number of
456+
/// periods by the facts in scope taken out, or <see langword="null"/>: <c>sin(x + 2 pi n)</c> is
457+
/// <c>sin(x)</c> for a whole <c>n</c>, as <c>Complex.sin_add_int_mul_two_pi</c> has it. The period is
458+
/// <paramref name="halfTurns"/> times <c>pi</c>: two for the sine, the cosine, the secant and the
459+
/// cosecant, and one for the tangent and the cotangent, as <c>Complex.tan_add_int_mul_pi</c> has it.
460+
/// https://github.com/asc-community/AngouriMath/issues/212
461+
/// </summary>
462+
internal static Entity? WithoutWholePeriods(Entity argument, int halfTurns, System.Func<Entity, Entity> function, string lemma)
463+
{
464+
if (top is null || argument is Number || !MentionsARenamedName(argument))
465+
return null;
466+
// A term of the argument, or of its multiple of pi where it is gathered into one,
467+
// such as pi * (2 n + 1/6).
468+
var kept = new List<Entity>();
469+
Entity? periods = null;
470+
foreach (var term in Sumf.LinearChildren(argument))
471+
{
472+
if (!MentionsARenamedName(term) || MathS.UnsafeAndInternal.DivideByEntityStrict(term, MathS.pi) is not { } multiple)
473+
{
474+
kept.Add(term);
475+
continue;
476+
}
477+
var parts = Sumf.LinearChildren(multiple);
478+
foreach (var part in parts)
479+
if (MentionsARenamedName(part) && IsWholeNumber(Rational.Create(1, halfTurns) * part))
480+
periods = periods is null ? part * MathS.pi : periods + part * MathS.pi;
481+
else
482+
kept.Add(parts.Count == 1 ? term : part * MathS.pi);
483+
}
484+
if (periods is null)
485+
return null;
486+
var rest = kept.Count == 0 ? Integer.Zero : kept.Aggregate((left, right) => left + right);
487+
return Recorded(function(argument), function(rest), $"{periods} is a whole number of periods", lemma);
488+
}
489+
490+
/// <summary>A function <see cref="WithoutWholePeriods"/> takes whole periods out of, of anything but a number.</summary>
491+
private static bool IsPeriodic(Entity node)
492+
=> node is Sinf(not Number) or Cosf(not Number) or Tanf(not Number) or Cotanf(not Number) or Secantf(not Number) or Cosecantf(not Number);
493+
453494
/// <summary><paramref name="to"/>, with the step from <paramref name="from"/> recorded where a proof is.</summary>
454495
private static Entity Recorded(Entity from, Entity to, string rule, string lemma)
455496
{

‎Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/NonSquareSystemSolver.cs‎

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,16 @@ Variable ParameterFor(int index)
6666

6767
if (Eliminate(equations, variables, variables.Length, ParameterFor, ledger) is not { Count: > 0 } families)
6868
return null;
69+
var own = new HashSet<Variable>(equations.SelectMany(equation => equation.Vars));
6970
foreach (var family in families)
7071
foreach (var equation in equations)
7172
{
7273
var substituted = equation;
7374
for (var index = 0; index < variables.Length; index++)
7475
substituted = substituted.Substitute(variables[index], family[index]);
75-
if (!Vanishes(substituted))
76+
// An n_1 a family brings from a periodic root stands for every whole number.
77+
var whole = substituted.Vars.Where(name => !own.Contains(name) && name.Name.StartsWith("n_")).Distinct().ToList();
78+
if (!Vanishes(substituted, whole))
7679
return null;
7780
}
7881
return families;
@@ -183,7 +186,7 @@ Variable ParameterFor(int index)
183186
/// </summary>
184187
private static bool? Holds(Entity equation)
185188
{
186-
if (Vanishes(equation))
189+
if (Vanishes(equation, System.Array.Empty<Variable>()))
187190
return true;
188191
if (equation.Vars.Any() || equation.Evaled is not Complex value || value.IsNaN)
189192
return null;
@@ -201,30 +204,50 @@ Variable ParameterFor(int index)
201204
/// <c>0 provided not c = 0</c> for a <c>c</c> that is zero without being written
202205
/// <c>0</c>, such as <c>(a + 1)^2 - a^2 - 2a - 1</c>.
203206
/// </summary>
204-
private static bool Vanishes(Entity expression)
207+
/// <remarks>
208+
/// A symbol in <paramref name="whole"/> stands for every whole number, so it is pinned at
209+
/// one, and the expression is zero where it is zero for each: <c>sin(pi/6 + 2 pi n_1) - 1/2</c>
210+
/// is decided as <c>forall n_1 in ZZ : sin(pi/6 + 2 pi n_1) - 1/2 = 0</c>, which takes the
211+
/// whole periods out. https://github.com/asc-community/AngouriMath/issues/212
212+
/// </remarks>
213+
private static bool Vanishes(Entity expression, IReadOnlyList<Variable> whole)
205214
{
206215
if (expression.InnerSimplified.Evaled is Complex { IsZero: true })
207216
return true;
208217
// Away from zero at a point is not zero everywhere, which settles most without
209218
// simplifying.
210-
if (Pinned(expression).Evaled is Complex { IsNaN: false } atAPoint
219+
if (Pinned(expression, whole).Evaled is Complex { IsNaN: false } atAPoint
211220
&& ((Real)atAPoint.Abs()).EDecimal.ToDouble() > 1e-9)
212221
return false;
222+
if (whole.Count > 0)
223+
{
224+
Entity statement = expression.EqualTo(Integer.Zero);
225+
foreach (var name in whole)
226+
statement = MathS.ForAll(name, MathS.Sets.Z, statement);
227+
if (statement.Simplify() == Entity.Boolean.True)
228+
return true;
229+
}
213230
return expression.Simplify() switch
214231
{
215232
Providedf(var simplified, var condition) =>
216-
simplified.Evaled is Complex { IsZero: true } && Pinned(condition).Evaled is Entity.Boolean { Value: true },
233+
simplified.Evaled is Complex { IsZero: true } && Pinned(condition, whole).Evaled is Entity.Boolean { Value: true },
217234
var simplified => simplified.Evaled is Complex { IsZero: true },
218235
};
219236
}
220237

221-
/// <summary><paramref name="expression"/> with each of its symbols at a fixed value.</summary>
222-
private static Entity Pinned(Entity expression)
238+
/// <summary>
239+
/// <paramref name="expression"/> with each of its symbols at a fixed value, a whole one for
240+
/// a symbol in <paramref name="whole"/>.
241+
/// </summary>
242+
private static Entity Pinned(Entity expression, IReadOnlyList<Variable> whole)
223243
{
224244
var values = new[] { 1.37, 0.61, 2.23, 1.91, 0.83, 1.13 };
225-
var index = 0;
245+
var wholeValues = new[] { 2, -1, 3 };
246+
var (index, wholeIndex) = (0, 0);
226247
foreach (var symbol in expression.Vars.ToList())
227-
expression = expression.Substitute(symbol, values[index++ % values.Length]);
248+
expression = whole.Contains(symbol)
249+
? expression.Substitute(symbol, wholeValues[wholeIndex++ % wholeValues.Length])
250+
: expression.Substitute(symbol, values[index++ % values.Length]);
228251
return expression;
229252
}
230253
}

‎Sources/AngouriMath/Functions/Evaluation/Evaluation.Continuous/Evaluation.Continuous.Trigonometry.Classes.cs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ protected override Entity InnerSimplify(bool isExact) =>
4848
{
4949
Complex n when !isExact => Number.Sin(n),
5050
{ Evaled: Complex n } arg when isExact && TrigonometryTableValues.PullSin(n, out var res) => res,
51+
_ when Functions.Boolean.QuantifierFacts.WithoutWholePeriods(a, 2, static x => new Sinf(x), "Complex.sin_add_int_mul_two_pi") is { } reduced => reduced.InnerSimplified(isExact),
5152
_ => null
5253
},
5354
(@this, a) => ((Sinf)@this).New(a), isExact);
@@ -63,6 +64,7 @@ protected override Entity InnerSimplify(bool isExact) =>
6364
{
6465
Complex n when !isExact => Number.Cos(n),
6566
{ Evaled: Complex n } when isExact && TrigonometryTableValues.PullCos(n, out var res) => res,
67+
_ when Functions.Boolean.QuantifierFacts.WithoutWholePeriods(a, 2, static x => new Cosf(x), "Complex.cos_add_int_mul_two_pi") is { } reduced => reduced.InnerSimplified(isExact),
6668
_ => null
6769
},
6870
(@this, a) => ((Cosf)@this).New(a), isExact);
@@ -83,6 +85,7 @@ protected override Entity InnerSimplify(bool isExact) =>
8385
? (1 / res).InnerSimplified
8486
: null
8587
: Secant(n),
88+
_ when Functions.Boolean.QuantifierFacts.WithoutWholePeriods(a, 2, static x => new Secantf(x), "Complex.cos_add_int_mul_two_pi") is { } reduced => reduced.InnerSimplified(isExact),
8689
_ => null
8790
},
8891
(@this, a) => ((Secantf)@this).New(a), isExact);
@@ -103,6 +106,7 @@ protected override Entity InnerSimplify(bool isExact) =>
103106
? (1 / res).InnerSimplified
104107
: null
105108
: Cosecant(n),
109+
_ when Functions.Boolean.QuantifierFacts.WithoutWholePeriods(a, 2, static x => new Cosecantf(x), "Complex.sin_add_int_mul_two_pi") is { } reduced => reduced.InnerSimplified(isExact),
106110
_ => null
107111
},
108112
(@this, a) => ((Cosecantf)@this).New(a), isExact);
@@ -157,6 +161,7 @@ protected override Entity InnerSimplify(bool isExact) =>
157161
isExact
158162
? TrigonometryTableValues.PullTan(n, out var res) ? res : null
159163
: Number.Tan(n),
164+
_ when Functions.Boolean.QuantifierFacts.WithoutWholePeriods(a, 1, static x => new Tanf(x), "Complex.tan_add_int_mul_pi") is { } reduced => reduced.InnerSimplified(isExact),
160165
_ => null
161166
},
162167
(@this, a) => ((Tanf)@this).New(a), isExact);
@@ -175,6 +180,7 @@ protected override Entity InnerSimplify(bool isExact) =>
175180
isExact
176181
? TrigonometryTableValues.PullTan(n, out var res) ? (1 / res).InnerSimplified : null
177182
: Number.Cotan(n),
183+
_ when Functions.Boolean.QuantifierFacts.WithoutWholePeriods(a, 1, static x => new Cotanf(x), "Complex.tan_add_int_mul_pi") is { } reduced => reduced.InnerSimplified(isExact),
178184
_ => null
179185
},
180186
(@this, a) => ((Cotanf)@this).New(a), isExact);

‎Sources/Tests/UnitTests/Algebra/SolveTest/NonSquareSystemTest.cs‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,13 @@ public sealed class NonSquareSystemTest
3030
/// Each row, substituted into each equation, is zero at several values of the parameters
3131
/// and symbols left in it, negative ones among them. That is checked numerically rather
3232
/// than by simplifying, which is how the solver checks it: <c>sqrt(t^2) - t</c> is zero at
33-
/// every positive <c>t</c> and at no negative one.
33+
/// every positive <c>t</c> and at no negative one. An <c>n_1</c>, which a periodic root
34+
/// brings and which stands for a whole number, is sampled at whole numbers.
3435
/// </summary>
3536
private static void HoldsAtSampledValues(string[] equations, string[] variables, Entity.Matrix solutions)
3637
{
3738
var samples = new[] { 1.37, -1.7, 2.3, -0.61 };
39+
var wholeSamples = new[] { 2, -1, 3, 0 };
3840
for (var row = 0; row < solutions.RowCount; row++)
3941
foreach (var text in equations)
4042
{
@@ -46,7 +48,9 @@ private static void HoldsAtSampledValues(string[] equations, string[] variables,
4648
{
4749
var pinned = residual;
4850
for (var i = 0; i < symbols.Count; i++)
49-
pinned = pinned.Substitute(symbols[i], samples[(shift + i) % samples.Length]);
51+
pinned = symbols[i].Name.StartsWith("n_") && !equations.Any(equation => equation.ToEntity().Vars.Contains(symbols[i]))
52+
? pinned.Substitute(symbols[i], wholeSamples[(shift + i) % wholeSamples.Length])
53+
: pinned.Substitute(symbols[i], samples[(shift + i) % samples.Length]);
5054
var value = Assert.IsAssignableFrom<Entity.Number.Complex>(pinned.Evaled);
5155
Assert.False(value.IsNaN, $"{residual} at {pinned}");
5256
Assert.True(((Entity.Number.Real)value.Abs()).EDecimal.ToDouble() < 1e-9, $"{residual} at {pinned} is {value}");
@@ -74,6 +78,25 @@ public void AShortSystemIsAFamily(string[] equations, string[] variables, int ro
7478
HoldsAtSampledValues(equations, variables, solutions);
7579
}
7680

81+
/// <summary>
82+
/// A family a periodic root brings a whole-number parameter to, <c>n_1</c>, which holds for
83+
/// every whole <c>n_1</c> and is answered: <c>sin(x) = 1/2</c> in <c>x</c> and <c>y</c> is
84+
/// the two families <c>(pi/6 + 2 pi n_1, t_1)</c> and <c>(pi - pi/6 + 2 pi n_1, t_1)</c>.
85+
/// </summary>
86+
[Theory]
87+
[InlineData(new[] { "sin(x) - 1/2" }, new[] { "x", "y" }, 2)]
88+
[InlineData(new[] { "cos(x) - 1" }, new[] { "x", "y" }, 1)]
89+
[InlineData(new[] { "tan(x) - 1" }, new[] { "x", "y" }, 1)]
90+
[InlineData(new[] { "sin(x) - 1/2", "y - x" }, new[] { "x", "y", "z" }, 2)]
91+
public void AFamilyWithAWholeNumberParameterIsAnswered(string[] equations, string[] variables, int rows)
92+
{
93+
var solutions = Solve(equations, variables);
94+
Assert.NotNull(solutions);
95+
Assert.Equal(rows, solutions!.RowCount);
96+
Assert.Contains(solutions.Vars, name => name.Name.StartsWith("n_"));
97+
HoldsAtSampledValues(equations, variables, solutions);
98+
}
99+
77100
/// <summary>
78101
/// More equations than unknowns, which the triangularisation does not take because they
79102
/// are not polynomial. The last drops <c>x = -1</c>, where <c>sin(-pi/2) - 1</c> is -2.

0 commit comments

Comments
 (0)