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
24 changes: 24 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,30 @@ unchanged ([#2450](https://github.com/asc-community/AngouriMath/issues/2450)).

---

### Two parameters of a system's answer that are independent have two names

**Silent: a part of the solutions becomes all of them.** Solving a system, each equation's roots bring their own
parameters, and a second equation minted the same name as the first where the two do not reach each other. So
`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
`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
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
throughout an answer, so the `t` of a `floor` and the `t` of a free unknown are never the same name
([#212](https://github.com/asc-community/AngouriMath/issues/212)).

| Input | Was (2.7.0) | Now |
|---|---|---|
| `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 |
| `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], …]` |
| `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]]` |
| `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 …]]` |
| `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]]` |
| `MathS.Equations("tan(x) - 1", "y - x").Solve("x", "y")` | `[[pi / 4 + pi * n_1, pi / 4 + pi * n_1]]` | unchanged |

`EquationSystem.SolveWithParameters` and `MathS.SolveSystemWithParameters` are new, and say what each parameter is:
`(n_1, ZZ)`, `(n_2, ZZ)` for the first row of the table.

---

## 2.7.0 — since 2.6.0

**Read these first: names that read differently.** Each of these was read as something else on 2.6.0. Code that used
Expand Down
6 changes: 6 additions & 0 deletions Sources/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
[Tests/UnitTests/Common/LogarithmOfAPerfectPowerTest.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/SolutionParameters.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/SystemParametersTest.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/PredicateEntailment.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
38 changes: 38 additions & 0 deletions Sources/AngouriMath/Convenience/MathS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,44 @@ public static Integer GreatestCommonDivisor(Integer a, Integer b)
public static Matrix? SolveSystem(Entity system, params Variable[] variables)
=> EquationSystem.EquationsOf(system, out _) is { } equations ? new EquationSystem(equations).Solve(variables) : null;

/// <summary>
/// Solves a system of equations written as one statement, as
/// <see cref="SolveSystem(Entity, Variable[])"/> does, and says what each parameter in the
/// answer is and which set it ranges over, as
/// <see cref="EquationSystem.SolveWithParameters(Variable[])"/> does for a system written
/// as a list.
/// </summary>
/// <param name="system">
/// Equations joined by <c>and</c>, as <see cref="SolveSystem(Entity, Variable[])"/> takes them.
/// </param>
/// <param name="variables">The unknowns, in the order of the answer's columns.</param>
/// <returns>
/// The rows <see cref="SolveSystem(Entity, Variable[])"/> answers and each parameter in
/// them with its set: <c>(t_1, CC)</c> for an unknown nothing constrains, <c>(n_1, ZZ)</c>
/// for a whole number of periods. <see langword="null"/> where
/// <see cref="SolveSystem(Entity, Variable[])"/> is, and where a parameter is one whose
/// set cannot be named.
/// </returns>
/// <remarks>https://github.com/asc-community/AngouriMath/issues/212</remarks>
/// <exception cref="NotSufficientlySupportedException">
/// As <see cref="SolveSystem(Entity, Variable[])"/>.
/// </exception>
/// <example>
/// <code>
/// var solved = MathS.SolveSystemWithParameters("2x - 4y = 12", "x", "y");
/// Console.WriteLine(solved!.Rows);
/// foreach (var (parameter, domain) in solved.Parameters)
/// Console.WriteLine($"{parameter} in {domain}");
/// </code>
/// Prints
/// <code>
/// [[6 + 2 * t_1, t_1]]
/// t_1 in CC
/// </code>
/// </example>
public static ParametrizedSolutions? SolveSystemWithParameters(Entity system, params Variable[] variables)
=> EquationSystem.EquationsOf(system, out _) is { } equations ? new EquationSystem(equations).SolveWithParameters(variables) : null;

/// <summary>
/// Solves a first-order linear ordinary differential equation
/// <a href="https://en.wikipedia.org/wiki/Linear_differential_equation"/> by its
Expand Down
107 changes: 107 additions & 0 deletions Sources/AngouriMath/Core/EquationSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,85 @@ private static IEnumerable<Entity> AsExpressionsEqualToZero(Entity equation)
/// </exception>
public Matrix? Solve(params Variable[] vars) => EquationSolver.SolveSystem(equations, vars);

/// <summary>
/// Solves the system as <see cref="Solve(Variable[])"/> does, and says what each parameter
/// in the answer is and which set it ranges over.
/// </summary>
/// <param name="vars">The unknowns, as <see cref="Solve(Variable[])"/> takes them.</param>
/// <returns>
/// The rows <see cref="Solve(Variable[])"/> answers, in
/// <see cref="ParametrizedSolutions.Rows"/>, and in
/// <see cref="ParametrizedSolutions.Parameters"/> each symbol of theirs that is neither an
/// unknown nor a symbol of the equations, in the order it first appears, with the set it
/// ranges over: <c>t_1</c> over <c>CC</c> for an unknown nothing constrains (<c>RR</c>
/// where <see cref="MathS.Settings.Codomain"/> is <see cref="Domain.Real"/>), <c>n_1</c>
/// over <c>ZZ</c> for a whole number of periods or a branch of the Lambert W function,
/// <c>r_1</c> over <c>RR</c> for the argument of a number of given modulus, and the
/// <c>t_1</c> of <c>floor(x) = 1</c> over <c>[0, 1)</c>. The list is empty where the
/// answer has no parameter. <see langword="null"/> where <see cref="Solve(Variable[])"/>
/// is, and where a parameter is one this cannot name the set of.
/// </returns>
/// <remarks>
/// <para>
/// Each row is a family of solutions, one for each value of the parameters it mentions,
/// taken from their sets; a row need not mention all of them. A name stands for one set
/// throughout the answer, and within a row two parameters are two names: in
/// <c>sin(x) = 1/2</c>, <c>sin(y) = 1/2</c> the rows read
/// <c>(pi/6 + 2 pi n_1, pi/6 + 2 pi n_2)</c>. Rows are separate families, so a
/// name in two rows is chosen independently in each.
/// </para>
/// <para>
/// A row may say where it holds, as <c>1 + t_1 provided t_1 in RR and t_1 &gt;= 0 and
/// t_1 &lt; 1</c> does; the set of its parameter is said here either way.
/// </para>
/// https://github.com/asc-community/AngouriMath/issues/212
/// </remarks>
/// <exception cref="Exceptions.NotSufficientlySupportedException">
/// As <see cref="Solve(Variable[])"/>.
/// </exception>
/// <example>
/// <code>
/// var solved = MathS.Equations("sin(x) - 1/2").SolveWithParameters("x", "y");
/// Console.WriteLine(solved!.Rows);
/// foreach (var (parameter, domain) in solved.Parameters)
/// Console.WriteLine($"{parameter} in {domain}");
/// </code>
/// Prints
/// <code>
/// [[pi / 6 + 2 * pi * n_1, t_1], [pi - pi / 6 + 2 * pi * n_1, t_1]]
/// n_1 in ZZ
/// t_1 in CC
/// </code>
/// </example>
public ParametrizedSolutions? SolveWithParameters(params Variable[] vars)
{
var scope = AngouriMath.Functions.Algebra.AnalyticalSolving.SolutionParameters.Open(out var opened);
try
{
if (Solve(vars) is not { } rows)
return null;
var known = new HashSet<Variable>(equations.SelectMany(equation => equation.Vars));
known.UnionWith(vars);
var parameters = new List<(Variable Parameter, Set Domain)>();
for (var row = 0; row < rows.RowCount; row++)
for (var column = 0; column < rows.ColumnCount; column++)
foreach (var symbol in rows[row, column].Vars)
{
if (!known.Add(symbol))
continue;
if (scope.SetOf(symbol) is not { } domain)
return null;
parameters.Add((symbol, domain));
}
return new ParametrizedSolutions(rows, parameters);
}
finally
{
if (opened)
scope.Close();
}
}

/// <returns>
/// Latexized version of the system. It adds
/// zero equality to all the provided expressions
Expand All @@ -166,4 +245,32 @@ public string Latexize()
/// <inheritdoc/>
public override string ToString() => string.Join("\n", equations.Select(c => c.Stringize() + " = 0"));
}

/// <summary>
/// The solutions of a system as families: the rows <see cref="EquationSystem.Solve"/>
/// answers, and the parameters they are written in, each with the set it ranges over.
/// See <see cref="EquationSystem.SolveWithParameters"/>.
/// https://github.com/asc-community/AngouriMath/issues/212
/// </summary>
/// <param name="Rows">
/// A row for each family of solutions and a column for each unknown, as
/// <see cref="EquationSystem.Solve"/> answers them.
/// </param>
/// <param name="Parameters">
/// Each parameter of <paramref name="Rows"/> and the set it ranges over, in the order the
/// parameters first appear: <c>(n_1, ZZ)</c>, <c>(t_1, CC)</c>. Empty where no row has one.
/// </param>
public sealed record ParametrizedSolutions(Matrix Rows, IReadOnlyList<(Variable Parameter, Set Domain)> Parameters)
{
/// <summary>
/// Whether the rows are equal and the parameters are the same, in the same order, with
/// the same sets: compared element by element, where a record compares a list by reference.
/// </summary>
public bool Equals(ParametrizedSolutions? other)
=> other is not null && Rows == other.Rows && Parameters.SequenceEqual(other.Parameters);

/// <inheritdoc/>
public override int GetHashCode()
=> Parameters.Aggregate(Rows.GetHashCode(), (hash, pair) => unchecked(hash * 31 + pair.GetHashCode()));
}
}
14 changes: 13 additions & 1 deletion Sources/AngouriMath/Core/Operations/Catalogue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,18 @@ internal static class Catalogue
static (system, variables) => MathS.SolveSystem(system, [.. variables]),
new Parameter("variables", "The variables to solve for, in the order of the answer's columns."));

/// <summary><see cref="EquationSystem.SolveWithParameters(Variable[])"/>.</summary>
public static Query<Func<IEnumerable<Entity>, IReadOnlyList<Variable>, ParametrizedSolutions?>> SolveSystemWithParameters { get; } = new(
"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.",
static (equations, variables) => MathS.Equations(equations).SolveWithParameters([.. variables]),
new Parameter("variables", "The variables to solve for, in the order of the answer's columns."));

/// <summary><see cref="MathS.SolveSystemWithParameters(Entity, Variable[])"/>.</summary>
public static Query<Func<Entity, IReadOnlyList<Variable>, ParametrizedSolutions?>> SolveConjunctionWithParameters { get; } = new(
"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.",
static (system, variables) => MathS.SolveSystemWithParameters(system, [.. variables]),
new Parameter("variables", "The variables to solve for, in the order of the answer's columns."));

/// <summary><see cref="Entity.Evaled"/>.</summary>
public static Query<Func<Entity, Entity>> Evaluate { get; } = new(
"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.",
Expand Down Expand Up @@ -275,7 +287,7 @@ internal static class Catalogue
public static IReadOnlyList<Operation> All { get; } =
[
Simplify, Normalize, Expand, Factorize, Differentiate, Integrate, Limit, Substitute,
Solve, SolveSystem, SolveConjunction, Evaluate, EvaluateNumerically, Variables, Alternatives, Latex, Sympy, Hlsl, Glsl, TruthTable, SatisfyingAssignments, Series, AreEqual,
Solve, SolveSystem, SolveConjunction, SolveSystemWithParameters, SolveConjunctionWithParameters, Evaluate, EvaluateNumerically, Variables, Alternatives, Latex, Sympy, Hlsl, Glsl, TruthTable, SatisfyingAssignments, Series, AreEqual,
ToBase, FromBase,
Determinant, Inverse, Transpose, Rank, ReducedRowEchelonForm, Trace, TensorProduct,
PrimeFactorization, Totient, GreatestCommonDivisor, DivisorCount, IsPrime,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ internal static Set Solve(Entity equation, Variable x)
// An allowance: a set MathS.Settings.Budget replaces SystemSolveBudget, which is
// what the exceptions below mean by "raise MathS.Settings.Budget to allow more".
var ledger = BudgetLedger.For("SolveSystem", SystemSolveBudget, BudgetRole.Allowance);
// One name stands for one set in the answer, which the scope keeps while it is open.
var parameters = SolutionParameters.Open(out var opened);
try
{
var variables = new Variable[vars.Length];
Expand Down Expand Up @@ -192,6 +194,8 @@ internal static Set Solve(Entity equation, Variable x)
}
finally
{
if (opened)
parameters.Close();
ledger.Report();
}
}
Expand Down Expand Up @@ -449,7 +453,7 @@ private static Entity AtASamplePoint(Entity residual, HashSet<Variable> own)
// https://github.com/asc-community/AngouriMath/issues/550. A free parameter says
// what the answer is in the same way the trigonometric solvers say it with n_1.
if (solutions == MathS.Sets.C)
return new() { new List<Entity> { Variable.CreateUnique(nameSource, "t") } };
return new() { new List<Entity> { SolutionParameters.Mint(nameSource + equations[0], "t", MathS.Sets.C) } };
// An equation left unsolved has roots that were not found, and no rows here would
// say it has none: the rows of the other branches would read as every solution.
// y^2 - 2, x^2 + (P - Q y) x - 3, with (1 + sqrt(2))^70 = P + Q sqrt(2), declines
Expand Down Expand Up @@ -482,14 +486,15 @@ private static Entity AtASamplePoint(Entity residual, HashSet<Variable> own)

foreach (var sol in sols)
{
if (InSolveSystem(rest.Select(eq => eq.Substitute(var, sol)).ToList(), remainingVars, nameSource, ledger) is not { } rows)
var below = rest.Select(eq => eq.Substitute(var, sol)).ToList();
if (InSolveSystem(below, remainingVars, nameSource, ledger) is not { } rows)
return null;
foreach (var j in rows)
{
replacements.Clear();
for (int varid = 0; varid < remainingVars.Length; varid++)
replacements.Add(remainingVars[varid], j[varid]);
j.Add(sol.Substitute(replacements).InnerSimplified);
j.Add(SolutionParameters.Apart(sol, j, below, nameSource).Substitute(replacements).InnerSimplified);
result.Add(j);
}
}
Expand Down
Loading
Loading