Skip to content
Merged
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
3 changes: 3 additions & 0 deletions Sources/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -1093,3 +1093,6 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed

[AngouriMath/Functions/Simplification/ProductsOfMatrices.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/Convenience/MathS.Equality.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
69 changes: 69 additions & 0 deletions Sources/AngouriMath/Convenience/MathS.Equality.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//
// 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 AngouriMath.Core;
using AngouriMath.Core.Budgets;
using AngouriMath.Core.Transformations;

namespace AngouriMath
{
partial class MathS
{
/// <summary>
/// Whether <paramref name="left"/> and <paramref name="right"/> are equal: proved by the
/// rewrite rules over one e-graph, refuted at a point, or neither within
/// <paramref name="budget"/>.
/// </summary>
/// <param name="left">One side.</param>
/// <param name="right">The other.</param>
/// <param name="budget">
/// What the question may spend. <see cref="WorkBudget.Steps"/> counts work -- a point
/// tried, a rule's match attempt, a node the graph grows by -- so the same question under
/// the same steps gets the same answer on any machine; a <see cref="WorkBudget.Time"/>
/// makes it depend on the machine, and <see cref="EqualityAnswer.Budget"/> says when it
/// did. <see cref="Settings.Budget"/>, where set, overrides this.
/// </param>
/// <param name="weakest">
/// The weakest soundness a proof may rest on. <see cref="Soundness.Sound"/>, the default,
/// admits only rules that hold with no side condition -- reordering the terms of a sum or
/// the factors of a product among them -- and a proof that needs more is
/// <see cref="EqualityVerdict.Unknown"/>. <see cref="Soundness.SoundUnderAssumptions"/>
/// admits the conditional rules and the canonical-form pass as well, and names those it
/// used in <see cref="EqualityAnswer.Assumptions"/>.
/// </param>
/// <param name="domain">
/// <see cref="Domain.Complex"/>, the default, or <see cref="Domain.Real"/>: where the
/// variables range, and so where a witness may be looked for. A name is a number here, so
/// <c>a * b</c> and <c>b * a</c> are equal; a question with a matrix or a matrix operation
/// anywhere in it has no product reordered, since a name in it may stand for a matrix.
/// </param>
/// <returns>
/// <see cref="EqualityVerdict.Equal"/> only with a proof;
/// <see cref="EqualityVerdict.NotEqual"/> only with a point at which both sides are
/// defined and enclosures of their values are disjoint;
/// <see cref="EqualityVerdict.Unknown"/> otherwise -- which is not "unequal".
/// </returns>
/// <remarks>
/// <para>
/// Not <c>(left - right).Simplify() == 0</c>. A simplification picks one representative
/// per input by cost, and two equal inputs can settle on different ones; a graph holding
/// both merges their classes whether or not an extraction would agree. And a
/// simplification has no third answer: a difference that does not simplify to zero is
/// read as "unequal" by whoever compares it.
/// </para>
/// <para>
/// The rules are the safe ceiling's, which do not enlarge; zero-equivalence is
/// undecidable over this language, so <see cref="EqualityVerdict.Unknown"/> is an
/// answer this gives often and honestly.
/// <a href="https://github.com/asc-community/AngouriMath/issues/1251">#1251</a>
/// </para>
/// </remarks>
public static EqualityAnswer AreEqual(Entity left, Entity right, WorkBudget budget,
Soundness weakest = Soundness.Sound, Domain domain = Domain.Complex)
=> EqualityOracle.Ask(left, right, budget, weakest, domain);
}
}
12 changes: 11 additions & 1 deletion Sources/AngouriMath/Core/Operations/Catalogue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using AngouriMath.Core.Budgets;
using AngouriMath.Core.Transformations;
using static AngouriMath.Entity;
using static AngouriMath.Entity.Number;
Expand Down Expand Up @@ -157,6 +158,15 @@ internal static class Catalogue
: MathS.Series.Taylor(e, degree + 1, (variable, around)),
TheVariable, new Parameter("degree", "The highest power kept."), new Parameter("around", "The point, or none for zero."));

/// <summary><see cref="MathS.AreEqual(Entity, Entity, WorkBudget, Soundness, Domain)"/>.</summary>
public static Query<Func<Entity, Entity, WorkBudget, Soundness, Domain, EqualityAnswer>> AreEqual { get; } = new(
"are_equal", "Decides whether two expressions are equal: proved by rewrite rules, refuted at a point where both are defined, or neither within the budget.",
static (left, right, budget, weakest, domain) => MathS.AreEqual(left, right, budget, weakest, domain),
new Parameter("other", "The expression on the right."),
new Parameter("budget", "What the question may spend, in steps for an answer that does not depend on the machine."),
new Parameter("weakest", "The weakest soundness a proof may rest on: sound, or sound under assumptions."),
new Parameter("domain", "Where the variables range: the complex or the real numbers."));

// ---------------------------------------------------------------- queries on a number

/// <summary><see cref="MathS.ToBaseN(Real, int)"/>.</summary>
Expand Down Expand Up @@ -244,7 +254,7 @@ internal static class Catalogue
public static IReadOnlyList<Operation> All { get; } =
[
Simplify, Normalize, Expand, Factorize, Differentiate, Integrate, Limit, Substitute,
Solve, SolveSystem, Evaluate, EvaluateNumerically, Variables, Alternatives, Latex, Sympy, TruthTable, SatisfyingAssignments, Series,
Solve, SolveSystem, Evaluate, EvaluateNumerically, Variables, Alternatives, Latex, Sympy, TruthTable, SatisfyingAssignments, Series, AreEqual,
ToBase, FromBase,
Determinant, Inverse, Transpose, Rank, ReducedRowEchelonForm, Trace, TensorProduct,
PrimeFactorization, Totient, GreatestCommonDivisor, DivisorCount, IsPrime,
Expand Down
96 changes: 96 additions & 0 deletions Sources/AngouriMath/Core/Transformations/EqualityAnswer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
//
// 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;
using System.Collections.Generic;
using System.Linq;
using AngouriMath.Core.Budgets;

namespace AngouriMath.Core.Transformations
{
/// <summary>What <see cref="MathS.AreEqual"/> could establish about two expressions.</summary>
public enum EqualityVerdict
{
/// <summary>
/// Neither shown: no proof within the budget and no point at which the two differ. Says
/// nothing about whether they are equal.
/// </summary>
Unknown,

/// <summary>Proved equal by rewrite rules; see <see cref="EqualityAnswer.Justification"/>.</summary>
Equal,

/// <summary>
/// Shown to differ at <see cref="EqualityAnswer.Witness"/>, where both are defined and
/// enclosures of their values are disjoint. Never a failure to prove equality.
/// </summary>
NotEqual,
}

/// <summary>
/// The answer of <see cref="MathS.AreEqual"/>: a verdict, what it rests on, and what it cost.
/// </summary>
/// <remarks>
/// <b>What <see cref="EqualityVerdict.Equal"/> claims.</b> At every assignment of the free
/// variables in the domain asked at which both sides have a value, the values agree, provided
/// that each rewrite the proof used holds as its <see cref="Soundness"/> declares. A
/// <see cref="Soundness.Sound"/> justification rests on nothing else. A
/// <see cref="Soundness.SoundUnderAssumptions"/> one rests in addition on the side
/// conditions of the steps named in <see cref="Assumptions"/>, which are not data the library
/// can check: a caller who needs an unconditional answer asks for
/// <see cref="Soundness.Sound"/> and gets <see cref="EqualityVerdict.Unknown"/> instead.
/// </remarks>
public sealed record EqualityAnswer
{
internal EqualityAnswer(EqualityVerdict verdict, Soundness? justification,
IReadOnlyList<string> assumptions, IReadOnlyDictionary<Entity.Variable, Entity>? witness,
BudgetOutcome budget)
{
Verdict = verdict;
Justification = justification;
Assumptions = assumptions;
Witness = witness;
Budget = budget;
}

/// <summary>Equal, not equal, or not shown either way.</summary>
public EqualityVerdict Verdict { get; }

/// <summary>
/// For <see cref="EqualityVerdict.Equal"/>, the weakest soundness among the steps the
/// proof may rest on. <see langword="null"/> for the other verdicts.
/// </summary>
public Soundness? Justification { get; }

/// <summary>
/// The conditional steps — rules declaring <see cref="Soundness.SoundUnderAssumptions"/>,
/// and the canonical-form pass — that joined two classes while the proof was sought: a
/// superset of those it needs. Empty for a <see cref="Soundness.Sound"/> proof.
/// </summary>
public IReadOnlyList<string> Assumptions { get; }

/// <summary>
/// For <see cref="EqualityVerdict.NotEqual"/>, the point at which the two sides differ:
/// a value for each free variable. Empty where neither side has one.
/// </summary>
public IReadOnlyDictionary<Entity.Variable, Entity>? Witness { get; }

/// <summary>What the question spent, and which ceiling, if any, stopped it.</summary>
public BudgetOutcome Budget { get; }

/// <inheritdoc/>
public override string ToString()
=> Verdict switch
{
EqualityVerdict.Equal when Assumptions.Count == 0 => $"Equal ({Justification})",
EqualityVerdict.Equal => $"Equal ({Justification}: {string.Join(", ", Assumptions)})",
EqualityVerdict.NotEqual => "NotEqual at "
+ (Witness!.Count == 0 ? "the constants" : string.Join(", ", Witness.Select(p => $"{p.Key} = {p.Value}"))),
_ => $"Unknown ({Budget})",
};
}
}
Loading
Loading