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
51 changes: 51 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,32 @@ function could give different trees.
| `(1 / x + 1) ^ 40` | `…: no answer for …` | `…: a quotient to the power 40, above the bound of 32` |
| 17 variables | `…: no answer for …` | `…: 17 variables, above the bound of 16` |

### A finite set holds a member written another way

**Silent: wrong answers become right ones.** A finite set looked its members up by their hundred-digit evaluations,
and one number written two ways can evaluate to decimals that differ in the last digit: `sqrt(8)` ends `...283145` and
`2 sqrt(2)` ends `...283146`. A miss on the decimal was taken for "not a member", so a set rejected its own element,
kept it twice, and did not take it out. Members whose evaluations agree that far are now compared as numbers: the same
where their difference simplifies to 0, different where enclosures in twice the working digits are apart, and otherwise
undecided. An evaluated set has only the decimals, which decide nothing at that distance, so an operation on two such
sets stays written where it gave a wrong set. Measured on master `578680eed` and on this branch
([#2755](https://github.com/asc-community/AngouriMath/issues/2755)).

| | Was (`578680eed`) | Now |
|---|---|---|
| `TryContains`: `sqrt(8)` in `{ 2 sqrt(2) }` | decided, not a member | decided, a member |
| `TryContains`: `sqrt(8)` in `{ x, 2 sqrt(2) }` | undecided | decided, a member |
| `(sqrt(8) in { -2 sqrt(2), 2 sqrt(2) }).Simplify()` | `False` | `True` |
| `(cos(pi/5) in { (1 + sqrt(5))/4 }).Evaled` | `False` | `True` |
| `{ (1 + sqrt(2))^2, 3 + 2 sqrt(2) }.Simplify()` | `{ (1 + sqrt(2)) ^ 2, 3 + 2 * sqrt(2) }` | `{ 3 + 2 * sqrt(2) }` |
| `({ (1 + sqrt(2))^2 } \ { 3 + 2 sqrt(2) }).Simplify()` | `{ (1 + sqrt(2)) ^ 2 }` | `{ }` |
| `({ (1 + sqrt(2))^2 } /\ { 3 + 2 sqrt(2) }).Simplify()` | `{ }` | `{ (1 + sqrt(2)) ^ 2 }` |
| `({ sin(pi/3) } = { sqrt(3)/2 }).Simplify()` | `False` | `True` |
| `({ sqrt(8) } \ { 2 sqrt(2) }).Evaled` | `{ 2.8284...283145 }` | the difference of the two evaluated sets, unevaluated |

Two numbers whose evaluations agree in every digit still share a key, and `pi + 10^(-120) in { pi }` is still
`True`: that is [#1376](https://github.com/asc-community/AngouriMath/issues/1376).

### A square system is answered exactly whichever order its equations are in

**Not silent: a decimal answer is now exact, and a refused system has an answer.** The eliminator took the last
Expand All @@ -548,6 +574,31 @@ only in decimals, is answered as before ([#2607](https://github.com/asc-communit
| `2^x + 2^y - 8`, `x - y` | `null` | `[[2 + 2 * pi * i * n_1 / ln(2), …]]` |
| `x - y`, `ln(x) + ln(y) - 1` | `[[sqrt(e), sqrt(e)]]` | unchanged |

### A listed set less a listed set keeps a member that may equal one taken out

**Silent: wrong answers become right ones.** The difference of two listed sets took out each member of the right
side by its key, and kept every other member of the left as though it differed from all of them. A member with a
variable can equal a member taken out: `{ a, 1 } \ { 1 }` was `{ a }`, which holds `1` at `a = 1`. A member of the left
is now taken out where it is shown to be a member of the right, kept where it is shown not to be one, and kept under
the difference where that is not decided, with only the members of the right it may equal. A member set written
another way is the same member, in a difference and when the set is built. Measured on master `8b4f507c1`, and for
the member sets on [#2755](https://github.com/asc-community/AngouriMath/issues/2755)'s branch `76a2f4a3c`, against
this branch ([#2770](https://github.com/asc-community/AngouriMath/issues/2770),
[#2773](https://github.com/asc-community/AngouriMath/issues/2773)).

| | Was | Now |
|---|---|---|
| `({ a, 1 } \ { 1 }).Simplify()` | `{ a }` | `{ a } \ { 1 }` |
| `({ a, -1 } \ { a }).Simplify()` | `{ -1 }` | `{ -1 } \ { a }` |
| `({ 22/7, b, 2 } \ { 2 }).Simplify()` | `{ 22/7, b }` | `{ 22/7, b } \ { 2 }` |
| `({ a, b } \ { a }).Simplify()` | `{ b }` | `{ b } \ { a }` |
| `({ a, 1 } \ { 1, 2 }).Simplify()` | `{ a, 1 } \ { 1, 2 }` | `{ a } \ { 1, 2 }` |
| `({ 1, 2, 3 } \ { 2, a }).Simplify()` | `{ 1, 2, 3 } \ { 2, a }` | `{ 1, 3 } \ { a }` |
| `(x \/ { 1 } = { y, 1 }).Solve("x")` | `{ { y }, { y, 1 } }`, without `{ }` at `y = 1` | `{ x : x \/ { 1 } = { y, 1 } }` |
| `({ { (1 + sqrt(2))^2 }, 1 } \ { { 3 + 2 sqrt(2) } }).InnerSimplified` | `{ { (1 + sqrt(2)) ^ 2 }, 1 }` | `{ 1 }` |
| `({ { sqrt(8) }, 1 } \ { { 2 sqrt(2) } }).Evaled` | `{ { 2.8284...283145 }, 1 }` | the difference of the two evaluated sets, unevaluated |
| `{ { sqrt(8) }, { 2 * sqrt(2) } }`, parsed | two members | `{ { sqrt(8) } }` |

### A family of roots through n = 0 is kept, and its undefined member left out

**Silent: missing real roots come back.** Solving `f(g(x)) = c` for a periodic `f` gives a family `c n` with
Expand Down
3 changes: 3 additions & 0 deletions Sources/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,9 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
[AngouriMath/Core/Entity/Omni/Sets/Cardinality.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/Core/Entity/Omni/Sets/FiniteSet.SameNumber.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/Core/Sets/SubsetTest.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
129 changes: 120 additions & 9 deletions Sources/AngouriMath/Core/Entity/Omni/Entity.Omni.Classes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private static Dictionary<Entity, Entity> BuildDictionaryFromElements(IEnumerabl
else
dict[elem.Evaled] = dict.TryGetValue(elem.Evaled, out var value) ? Simplificator.PickSimplest(elem, value) : elem;
}
return dict;
return noCheck ? dict : WithRespelledSetsMerged(WithRespelledMembersMerged(dict));
}

/// <summary>
Expand Down Expand Up @@ -124,15 +124,102 @@ IEnumerator IEnumerable.GetEnumerator()
internal static FiniteSet Unite(FiniteSet A, FiniteSet B)
=> new FiniteSet(A.Elements.Concat(B.Elements));

// It could be written with one chain request, but readability > one line
/// <summary>
/// <paramref name="A"/> without the members of <paramref name="B"/>, where each
/// member of <paramref name="A"/> is decided to be one of <paramref name="B"/>'s or not.
/// </summary>
internal static bool TryFullSubtract(FiniteSet A, FiniteSet B, [NotNullWhen(true)] out FiniteSet? resultSet)
{
var constantSets = A.Vars.Count == 0 && B.Vars.Count == 0;
var dict = BuildDictionaryFromElements(A.Elements, noCheck: true);
foreach (var el in B)
if (!dict.Remove(el.Evaled) && !constantSets) { resultSet = null; return false; }
resultSet = new FiniteSet(dict.Values, noCheck: true); // we didn't add anything
return true;
var left = WithoutMembersOf(A, B, out var decided);
resultSet = decided ? left : null;
return decided;
}

/// <summary>
/// <paramref name="A"/> without each member that is decided to be a member of
/// <paramref name="B"/>; <paramref name="decided"/> says whether every member it
/// keeps is decided not to be one.
/// </summary>
/// <remarks>
/// A member is taken out by membership, as <see cref="TryContains"/> decides it, by
/// value for a number or a set written another way, and is kept only where it is
/// shown not to be in <paramref name="B"/>. Each member used to be kept unless its key
/// was one of <paramref name="B"/>'s, as though a member with a variable differed
/// from every member taken out: <c>{ a, 1 } \ { 1 }</c> was <c>{ a }</c>, which is
/// <c>{ }</c> at <c>a = 1</c>. Such a member is kept undecided, and the difference
/// stays written for it: <c>{ a } \ { 1 }</c>.
/// https://github.com/asc-community/AngouriMath/issues/2770
/// </remarks>
internal static FiniteSet WithoutMembersOf(FiniteSet A, FiniteSet B, out bool decided)
{
decided = true;
List<Entity>? kept = null;
var removedAny = false;
foreach (var member in A.elements.Values)
{
if (!B.TryContains(member, out var contains))
decided = false;
if (contains)
removedAny = true;
else
(kept ??= new()).Add(member);
}
return removedAny ? new FiniteSet((IEnumerable<Entity>?)kept ?? Array.Empty<Entity>(), noCheck: true) : A;
}

/// <summary>
/// <paramref name="members"/> with the members that are the same set written two
/// ways made one, the simplest of the spellings at the place of the first: each
/// holds the other's members, as <c>{ sqrt(8) }</c> and <c>{ 2 sqrt(2) }</c> do.
/// Member sets whose inclusion is not decided are both kept.
/// https://github.com/asc-community/AngouriMath/issues/2773
/// </summary>
/// <remarks>
/// A member set is keyed by its evaluation, a set of decimals rounded one by one,
/// so the two spellings above were two keys and the set kept both, where
/// <see cref="WithRespelledMembersMerged"/> merges numbers.
/// </remarks>
private static Dictionary<Entity, Entity> WithRespelledSetsMerged(Dictionary<Entity, Entity> members)
{
if (members.Count < 2)
return members;
List<int>? sets = null;
var index = 0;
foreach (var member in members.Values)
{
if (member is FiniteSet { Vars.Count: 0 })
(sets ??= new()).Add(index);
index++;
}
if (sets is null || sets.Count < 2)
return members;
var entries = members.ToArray();
bool[]? merged = null;
for (var i = 0; i < sets.Count; i++)
{
if (merged is not null && merged[sets[i]])
continue;
for (var j = i + 1; j < sets.Count; j++)
{
if (merged is not null && merged[sets[j]])
continue;
var first = (FiniteSet)entries[sets[i]].Value;
var second = (FiniteSet)entries[sets[j]].Value;
if (first.Count != second.Count
|| !first.TryIsSubsetOf(second, out var inside) || !inside
|| !second.TryIsSubsetOf(first, out inside) || !inside)
continue;
(merged ??= new bool[entries.Length])[sets[j]] = true;
entries[sets[i]] = new(entries[sets[i]].Key, Simplificator.PickSimplest(first, second));
}
}
if (merged is null)
return members;
var result = new Dictionary<Entity, Entity>(members.Count);
for (var k = 0; k < entries.Length; k++)
if (!merged[k])
result[entries[k].Value.Evaled] = entries[k].Value;
return result;
}

/// <inheritdoc/>
Expand All @@ -142,6 +229,28 @@ public override bool TryContains(Entity entity, out bool contains)
contains = elements.ContainsKey(entity.Evaled);
// x in { x, 2 } and 2 in { x, 2 } are both unambiguously true
if (contains) return true;
// The keys are evaluations rounded to the working digits, and sqrt(8) in
// { 2 sqrt(2) } misses its key in the last digit, so the members near it are
// compared as numbers, and a set as an element with the sets among the members.
// One shown the same makes it a member whatever the others are:
// sqrt(8) in { x, 2 sqrt(2) }. One not shown either way leaves it undecided.
// https://github.com/asc-community/AngouriMath/issues/2755
if (entity.Vars.Count == 0)
{
var byValue = entity.Evaled switch
{
FiniteSet listed => MemberWithTheMembersOf(elements, entity as FiniteSet ?? listed),
Complex value => MemberWithTheValueOf(elements, entity, value, out _),
_ => false,
};
if (byValue is true)
{
contains = true;
return true;
}
if (byValue is null)
return false;
}
// however, 1 in { x, 2 }, y in { x, 2 } are both unknown
// meanwhile, 2 in { 1, 3 } is unambiguously false
return entity.Vars.Count == 0 && elements.Keys.All(el => el.Vars.Count == 0);
Expand All @@ -164,8 +273,10 @@ public bool Equals(FiniteSet? other)
return false;
if (other.Count != Count)
return false;
// Equality of entities compares the members' evaluations and proves nothing:
// it is not the membership, which can simplify to decide.
foreach (var pair in elements)
if (!other.TryContains(pair.Key, out var contains) || !contains)
if (!other.elements.ContainsKey(pair.Key))
return false;
return true;
}
Expand Down
Loading
Loading