Skip to content

Commit 2745d2d

Browse files
Rafael-SOWNetclaude
andcommitted
A system's answer with each parameter and the set it ranges over
EquationSystem.SolveWithParameters(params Variable[]) and MathS.SolveSystemWithParameters(Entity, params Variable[]) answer the rows Solve answers and, beside them, each parameter of the rows with its set, as ParametrizedSolutions(Matrix Rows, IReadOnlyList<(Variable Parameter, Set Domain)> Parameters). sin(x) = 1/2 in x, y is the two rows with (n_1, ZZ), (t_1, CC). An answer without parameters lists none; null where Solve is null, and where a parameter's set cannot be named. The set is said where the parameter is minted, by SolutionParameters.Mint: ZZ for the n of a period, a mod and a Lambert W branch, RR for the argument r of abs, (0, oo) for the r of signum, [0, 1) for the t of floor and ceil, (-1/2, 1/2) for round, and CC (RR over the reals) for an unknown nothing constrains. The non-square check reads which symbols are whole from there rather than from an "n_" prefix. A second equation's roots minted the same name as the first's where the two do not reach each other, so sin(x) = 1/2, sin(y) = 1/2 was (pi/6 + 2 pi n_1, pi/6 + 2 pi n_1) and left out (pi/6, 13 pi/6). Such a parameter is renamed where the rows are put together (n_2), one that reaches a later equation stays shared (tan(x) = 1, y = x), and while a system is solved one name stands for one set. Measured on v2.7.0 and here; the changed answers are in BREAKING-CHANGES.md. solve_system_with_parameters and solve_conjunction_with_parameters join the catalogue; am_solve_system reports 'parameters', each with its set; the terminal lists both entries in OneShot.notOffered. Part of #212. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 73b77bf commit 2745d2d

18 files changed

Lines changed: 633 additions & 27 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,30 @@ unchanged ([#2450](https://github.com/asc-community/AngouriMath/issues/2450)).
333333

334334
---
335335

336+
### Two parameters of a system's answer that are independent have two names
337+
338+
**Silent: a part of the solutions becomes all of them.** Solving a system, each equation's roots bring their own
339+
parameters, and a second equation minted the same name as the first where the two do not reach each other. So
340+
`sin(x) = 1/2` and `sin(y) = 1/2` came back as `(pi/6 + 2 pi n_1, pi/6 + 2 pi n_1)` and its three siblings, which says
341+
`x - y` is a whole number of periods and leaves out `(pi/6, 13 pi/6)`. Each independent parameter now has a name of its
342+
own, and one that reaches a later equation is still shared, as in `tan(x) = 1`, `y = x`. A name also stands for one set
343+
throughout an answer, so the `t` of a `floor` and the `t` of a free unknown are never the same name
344+
([#212](https://github.com/asc-community/AngouriMath/issues/212)).
345+
346+
| Input | Was (2.7.0) | Now |
347+
|---|---|---|
348+
| `MathS.Equations("sin(x) - 1/2", "sin(y) - 1/2").Solve("x", "y")` | `[[pi / 6 + 2 * pi * n_1, pi / 6 + 2 * pi * n_1], …]`, four rows | `[[pi / 6 + 2 * pi * n_1, pi / 6 + 2 * pi * n_2], …]`, four rows |
349+
| `MathS.Equations("sin(x) - 1/2", "cos(y)").Solve("x", "y")` | `[[pi / 6 + 2 * pi * n_1, pi / 2 + 2 * pi * n_1], …]` | `[[pi / 6 + 2 * pi * n_1, pi / 2 + 2 * pi * n_2], …]` |
350+
| `MathS.Equations("tan(x) - 1", "tan(y) - 1", "tan(z) - 1").Solve("x", "y", "z")` | `[[pi / 4 + pi * n_1, pi / 4 + pi * n_1, pi / 4 + pi * n_1]]` | `[[pi / 4 + pi * n_1, pi / 4 + pi * n_2, pi / 4 + pi * n_3]]` |
351+
| `MathS.Equations("floor(x) - 1", "floor(y) - 2").Solve("x", "y")` | `[[1 + t_1 provided …, 2 + t_1 provided …]]` | `[[1 + t_1 provided …, 2 + t_2 provided …]]` |
352+
| `MathS.Equations("abs(x) - 1", "abs(y) - 2").Solve("x", "y")` | `[[e ^ (i * r_1) provided r_1 in RR, 2 * e ^ (i * r_1) provided r_1 in RR]]` | `[[e ^ (i * r_1) provided r_1 in RR, 2 * e ^ (i * r_2) provided r_2 in RR]]` |
353+
| `MathS.Equations("tan(x) - 1", "y - x").Solve("x", "y")` | `[[pi / 4 + pi * n_1, pi / 4 + pi * n_1]]` | unchanged |
354+
355+
`EquationSystem.SolveWithParameters` and `MathS.SolveSystemWithParameters` are new, and say what each parameter is:
356+
`(n_1, ZZ)`, `(n_2, ZZ)` for the first row of the table.
357+
358+
---
359+
336360
## 2.7.0 — since 2.6.0
337361

338362
**Read these first: names that read differently.** Each of these was read as something else on 2.6.0. Code that used

‎Sources/.editorconfig‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,3 +1129,9 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
11291129

11301130
[Tests/UnitTests/Convenience/ShadersTest.cs]
11311131
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
1132+
1133+
[AngouriMath/Functions/Continuous/Solvers/EquationSolver/SolutionParameters.cs]
1134+
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
1135+
1136+
[Tests/UnitTests/Algebra/SolveTest/SystemParametersTest.cs]
1137+
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

‎Sources/AngouriMath/Convenience/MathS.cs‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,44 @@ public static Integer GreatestCommonDivisor(Integer a, Integer b)
356356
public static Matrix? SolveSystem(Entity system, params Variable[] variables)
357357
=> EquationSystem.EquationsOf(system, out _) is { } equations ? new EquationSystem(equations).Solve(variables) : null;
358358

359+
/// <summary>
360+
/// Solves a system of equations written as one statement, as
361+
/// <see cref="SolveSystem(Entity, Variable[])"/> does, and says what each parameter in the
362+
/// answer is and which set it ranges over, as
363+
/// <see cref="EquationSystem.SolveWithParameters(Variable[])"/> does for a system written
364+
/// as a list.
365+
/// </summary>
366+
/// <param name="system">
367+
/// Equations joined by <c>and</c>, as <see cref="SolveSystem(Entity, Variable[])"/> takes them.
368+
/// </param>
369+
/// <param name="variables">The unknowns, in the order of the answer's columns.</param>
370+
/// <returns>
371+
/// The rows <see cref="SolveSystem(Entity, Variable[])"/> answers and each parameter in
372+
/// them with its set: <c>(t_1, CC)</c> for an unknown nothing constrains, <c>(n_1, ZZ)</c>
373+
/// for a whole number of periods. <see langword="null"/> where
374+
/// <see cref="SolveSystem(Entity, Variable[])"/> is, and where a parameter is one whose
375+
/// set cannot be named.
376+
/// </returns>
377+
/// <remarks>https://github.com/asc-community/AngouriMath/issues/212</remarks>
378+
/// <exception cref="NotSufficientlySupportedException">
379+
/// As <see cref="SolveSystem(Entity, Variable[])"/>.
380+
/// </exception>
381+
/// <example>
382+
/// <code>
383+
/// var solved = MathS.SolveSystemWithParameters("2x - 4y = 12", "x", "y");
384+
/// Console.WriteLine(solved!.Rows);
385+
/// foreach (var (parameter, domain) in solved.Parameters)
386+
/// Console.WriteLine($"{parameter} in {domain}");
387+
/// </code>
388+
/// Prints
389+
/// <code>
390+
/// [[6 + 2 * t_1, t_1]]
391+
/// t_1 in CC
392+
/// </code>
393+
/// </example>
394+
public static ParametrizedSolutions? SolveSystemWithParameters(Entity system, params Variable[] variables)
395+
=> EquationSystem.EquationsOf(system, out _) is { } equations ? new EquationSystem(equations).SolveWithParameters(variables) : null;
396+
359397
/// <summary>
360398
/// Solves a first-order linear ordinary differential equation
361399
/// <a href="https://en.wikipedia.org/wiki/Linear_differential_equation"/> by its

‎Sources/AngouriMath/Core/EquationSystem.cs‎

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,85 @@ private static IEnumerable<Entity> AsExpressionsEqualToZero(Entity equation)
144144
/// </exception>
145145
public Matrix? Solve(params Variable[] vars) => EquationSolver.SolveSystem(equations, vars);
146146

147+
/// <summary>
148+
/// Solves the system as <see cref="Solve(Variable[])"/> does, and says what each parameter
149+
/// in the answer is and which set it ranges over.
150+
/// </summary>
151+
/// <param name="vars">The unknowns, as <see cref="Solve(Variable[])"/> takes them.</param>
152+
/// <returns>
153+
/// The rows <see cref="Solve(Variable[])"/> answers, in
154+
/// <see cref="ParametrizedSolutions.Rows"/>, and in
155+
/// <see cref="ParametrizedSolutions.Parameters"/> each symbol of theirs that is neither an
156+
/// unknown nor a symbol of the equations, in the order it first appears, with the set it
157+
/// ranges over: <c>t_1</c> over <c>CC</c> for an unknown nothing constrains (<c>RR</c>
158+
/// where <see cref="MathS.Settings.Codomain"/> is <see cref="Domain.Real"/>), <c>n_1</c>
159+
/// over <c>ZZ</c> for a whole number of periods or a branch of the Lambert W function,
160+
/// <c>r_1</c> over <c>RR</c> for the argument of a number of given modulus, and the
161+
/// <c>t_1</c> of <c>floor(x) = 1</c> over <c>[0, 1)</c>. The list is empty where the
162+
/// answer has no parameter. <see langword="null"/> where <see cref="Solve(Variable[])"/>
163+
/// is, and where a parameter is one this cannot name the set of.
164+
/// </returns>
165+
/// <remarks>
166+
/// <para>
167+
/// Each row is a family of solutions, one for each value of the parameters it mentions,
168+
/// taken from their sets; a row need not mention all of them. A name stands for one set
169+
/// throughout the answer, and within a row two parameters are two names: in
170+
/// <c>sin(x) = 1/2</c>, <c>sin(y) = 1/2</c> the rows read
171+
/// <c>(pi/6 + 2 pi n_1, pi/6 + 2 pi n_2)</c>. Rows are separate families, so a
172+
/// name in two rows is chosen independently in each.
173+
/// </para>
174+
/// <para>
175+
/// A row may say where it holds, as <c>1 + t_1 provided t_1 in RR and t_1 &gt;= 0 and
176+
/// t_1 &lt; 1</c> does; the set of its parameter is said here either way.
177+
/// </para>
178+
/// https://github.com/asc-community/AngouriMath/issues/212
179+
/// </remarks>
180+
/// <exception cref="Exceptions.NotSufficientlySupportedException">
181+
/// As <see cref="Solve(Variable[])"/>.
182+
/// </exception>
183+
/// <example>
184+
/// <code>
185+
/// var solved = MathS.Equations("sin(x) - 1/2").SolveWithParameters("x", "y");
186+
/// Console.WriteLine(solved!.Rows);
187+
/// foreach (var (parameter, domain) in solved.Parameters)
188+
/// Console.WriteLine($"{parameter} in {domain}");
189+
/// </code>
190+
/// Prints
191+
/// <code>
192+
/// [[pi / 6 + 2 * pi * n_1, t_1], [pi - pi / 6 + 2 * pi * n_1, t_1]]
193+
/// n_1 in ZZ
194+
/// t_1 in CC
195+
/// </code>
196+
/// </example>
197+
public ParametrizedSolutions? SolveWithParameters(params Variable[] vars)
198+
{
199+
var scope = AngouriMath.Functions.Algebra.AnalyticalSolving.SolutionParameters.Open(out var opened);
200+
try
201+
{
202+
if (Solve(vars) is not { } rows)
203+
return null;
204+
var known = new HashSet<Variable>(equations.SelectMany(equation => equation.Vars));
205+
known.UnionWith(vars);
206+
var parameters = new List<(Variable Parameter, Set Domain)>();
207+
for (var row = 0; row < rows.RowCount; row++)
208+
for (var column = 0; column < rows.ColumnCount; column++)
209+
foreach (var symbol in rows[row, column].Vars)
210+
{
211+
if (!known.Add(symbol))
212+
continue;
213+
if (scope.SetOf(symbol) is not { } domain)
214+
return null;
215+
parameters.Add((symbol, domain));
216+
}
217+
return new ParametrizedSolutions(rows, parameters);
218+
}
219+
finally
220+
{
221+
if (opened)
222+
scope.Close();
223+
}
224+
}
225+
147226
/// <returns>
148227
/// Latexized version of the system. It adds
149228
/// zero equality to all the provided expressions
@@ -166,4 +245,32 @@ public string Latexize()
166245
/// <inheritdoc/>
167246
public override string ToString() => string.Join("\n", equations.Select(c => c.Stringize() + " = 0"));
168247
}
248+
249+
/// <summary>
250+
/// The solutions of a system as families: the rows <see cref="EquationSystem.Solve"/>
251+
/// answers, and the parameters they are written in, each with the set it ranges over.
252+
/// See <see cref="EquationSystem.SolveWithParameters"/>.
253+
/// https://github.com/asc-community/AngouriMath/issues/212
254+
/// </summary>
255+
/// <param name="Rows">
256+
/// A row for each family of solutions and a column for each unknown, as
257+
/// <see cref="EquationSystem.Solve"/> answers them.
258+
/// </param>
259+
/// <param name="Parameters">
260+
/// Each parameter of <paramref name="Rows"/> and the set it ranges over, in the order the
261+
/// parameters first appear: <c>(n_1, ZZ)</c>, <c>(t_1, CC)</c>. Empty where no row has one.
262+
/// </param>
263+
public sealed record ParametrizedSolutions(Matrix Rows, IReadOnlyList<(Variable Parameter, Set Domain)> Parameters)
264+
{
265+
/// <summary>
266+
/// Whether the rows are equal and the parameters are the same, in the same order, with
267+
/// the same sets: compared element by element, where a record compares a list by reference.
268+
/// </summary>
269+
public bool Equals(ParametrizedSolutions? other)
270+
=> other is not null && Rows == other.Rows && Parameters.SequenceEqual(other.Parameters);
271+
272+
/// <inheritdoc/>
273+
public override int GetHashCode()
274+
=> Parameters.Aggregate(Rows.GetHashCode(), (hash, pair) => unchecked(hash * 31 + pair.GetHashCode()));
275+
}
169276
}

‎Sources/AngouriMath/Core/Operations/Catalogue.cs‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,18 @@ internal static class Catalogue
108108
static (system, variables) => MathS.SolveSystem(system, [.. variables]),
109109
new Parameter("variables", "The variables to solve for, in the order of the answer's columns."));
110110

111+
/// <summary><see cref="EquationSystem.SolveWithParameters(Variable[])"/>.</summary>
112+
public static Query<Func<IEnumerable<Entity>, IReadOnlyList<Variable>, ParametrizedSolutions?>> SolveSystemWithParameters { get; } = new(
113+
"solve_system_with_parameters", "Solves equations jointly for several variables, as solve_system does, and names each parameter of the answer with the set it ranges over.",
114+
static (equations, variables) => MathS.Equations(equations).SolveWithParameters([.. variables]),
115+
new Parameter("variables", "The variables to solve for, in the order of the answer's columns."));
116+
117+
/// <summary><see cref="MathS.SolveSystemWithParameters(Entity, Variable[])"/>.</summary>
118+
public static Query<Func<Entity, IReadOnlyList<Variable>, ParametrizedSolutions?>> SolveConjunctionWithParameters { get; } = new(
119+
"solve_conjunction_with_parameters", "Solves a conjunction of equations jointly for several variables, as solve_conjunction does, and names each parameter of the answer with the set it ranges over.",
120+
static (system, variables) => MathS.SolveSystemWithParameters(system, [.. variables]),
121+
new Parameter("variables", "The variables to solve for, in the order of the answer's columns."));
122+
111123
/// <summary><see cref="Entity.Evaled"/>.</summary>
112124
public static Query<Func<Entity, Entity>> Evaluate { get; } = new(
113125
"evaluate", "Evaluates as far as it goes: to a number, a boolean or a set where it can, and as an expression where it cannot.",
@@ -275,7 +287,7 @@ internal static class Catalogue
275287
public static IReadOnlyList<Operation> All { get; } =
276288
[
277289
Simplify, Normalize, Expand, Factorize, Differentiate, Integrate, Limit, Substitute,
278-
Solve, SolveSystem, SolveConjunction, Evaluate, EvaluateNumerically, Variables, Alternatives, Latex, Sympy, Hlsl, Glsl, TruthTable, SatisfyingAssignments, Series, AreEqual,
290+
Solve, SolveSystem, SolveConjunction, SolveSystemWithParameters, SolveConjunctionWithParameters, Evaluate, EvaluateNumerically, Variables, Alternatives, Latex, Sympy, Hlsl, Glsl, TruthTable, SatisfyingAssignments, Series, AreEqual,
279291
ToBase, FromBase,
280292
Determinant, Inverse, Transpose, Rank, ReducedRowEchelonForm, Trace, TensorProduct,
281293
PrimeFactorization, Totient, GreatestCommonDivisor, DivisorCount, IsPrime,

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ internal static Set Solve(Entity equation, Variable x)
121121
// unbounded, so the same `Solve` finished or did not depending on which internal
122122
// path accepted it. https://github.com/asc-community/AngouriMath/issues/896
123123
var ledger = BudgetLedger.For("SolveSystem", SystemSolveBudget);
124+
// One name stands for one set in the answer, which the scope keeps while it is open.
125+
var parameters = SolutionParameters.Open(out var opened);
124126
try
125127
{
126128
var variables = new Variable[vars.Length];
@@ -190,6 +192,8 @@ internal static Set Solve(Entity equation, Variable x)
190192
}
191193
finally
192194
{
195+
if (opened)
196+
parameters.Close();
193197
ledger.Report();
194198
}
195199
}
@@ -447,7 +451,7 @@ private static Entity AtASamplePoint(Entity residual, HashSet<Variable> own)
447451
// https://github.com/asc-community/AngouriMath/issues/550. A free parameter says
448452
// what the answer is in the same way the trigonometric solvers say it with n_1.
449453
if (solutions == MathS.Sets.C)
450-
return new() { new List<Entity> { Variable.CreateUnique(nameSource, "t") } };
454+
return new() { new List<Entity> { SolutionParameters.Mint(nameSource + equations[0], "t", MathS.Sets.C) } };
451455
// An equation left unsolved has roots that were not found, and no rows here would
452456
// say it has none: the rows of the other branches would read as every solution.
453457
// y^2 - 2, x^2 + (P - Q y) x - 3, with (1 + sqrt(2))^70 = P + Q sqrt(2), declines
@@ -480,14 +484,15 @@ private static Entity AtASamplePoint(Entity residual, HashSet<Variable> own)
480484

481485
foreach (var sol in sols)
482486
{
483-
if (InSolveSystem(rest.Select(eq => eq.Substitute(var, sol)).ToList(), remainingVars, nameSource, ledger) is not { } rows)
487+
var below = rest.Select(eq => eq.Substitute(var, sol)).ToList();
488+
if (InSolveSystem(below, remainingVars, nameSource, ledger) is not { } rows)
484489
return null;
485490
foreach (var j in rows)
486491
{
487492
replacements.Clear();
488493
for (int varid = 0; varid < remainingVars.Length; varid++)
489494
replacements.Add(remainingVars[varid], j[varid]);
490-
j.Add(sol.Substitute(replacements).InnerSimplified);
495+
j.Add(SolutionParameters.Apart(sol, j, below, nameSource).Substitute(replacements).InnerSimplified);
491496
result.Add(j);
492497
}
493498
}

0 commit comments

Comments
 (0)