diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index 9d3e6e7b8..c50ccd7ea 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -574,6 +574,31 @@ one point, or else `Simplify` proving it zero. A singular matrix answers as `[[1 | `"[[sin(x)^2, 1], [1 - cos(x)^2, 1]]^(-1)".Simplify()` | `NaN` | `1 / [[sin(x) ^ 2, 1], [sin(x) ^ 2, 1]]` | | `"[[e^x, e^(2x)], [2, e^x]]^(-1)".Simplify()` | its inverse | unchanged | +### The rank of a symbolic matrix whose rows are dependent is not full + +**Silent: wrong answers become right ones.** The elimination behind `Rank`, `RowEchelonForm` and +`ReducedRowEchelonForm` divides by its pivots as quotients in lowest terms, which carried the condition that the pivot +is not 0: the factor `-2 b / b` was `-2 provided not b = 0`. An entry made from such a factor was read as 0 by nothing, +was taken for a pivot, and the rank came out full. The elimination is over the rational functions in the symbols, where +a pivot is not 0, and the condition is dropped from the entries it makes; an entry that is a canonical `0 provided` its +denominator is not 0, like `b / a - b / a`, is 0. An entry past the canonical quotient's bound on size, which a 4x4 +reaches, or one that is not a rational function, is decided by its values at three sets of values for the symbols +where rigorous enclosures settle it, and simplified only where they do not. Measured on 2.10.0 and on this branch, +against sympy's `Matrix.rank(simplify=True)` and `Matrix.rref` +([#2757](https://github.com/asc-community/AngouriMath/issues/2757)). + +| | Was (2.10.0) | Now | +|---|---|---| +| `Rank` of `[[b, d - 1], [-2 b, -2 d + 2]]` | 2 | 1 | +| `Rank` of `[[1, 0, 1], [1, b, d], [-3, -2 b, -2 d - 1]]` | 3 | 2 | +| `Rank` of `[[x, c, x], [x, b, d], [-3 x, -c - 2 b, -2 d - x]]` | 3 | 2 | +| `ReducedRowEchelonForm` of the same | the identity | `[[1, 0, (b * x - c * d) / (b * x - c * x)], [0, 1, (d - x) / (b - c)], [0, 0, 0]]` | +| `Rank` of `[[a + 1, a b, a + 1], [a + 1, b, a b], [-3 a - 3, -a b - 2 b, -2 a b - a - 1]]` | 3 | 2 | +| `Rank` of `[[b / a - b / a, 1], [0, 1]]` | 2 | 1 | +| `Rank` of `[[sqrt(b), sqrt(a) + 1, 3 a], [3 sqrt(a) + 3, 3 a sin(x), -3 (-a b + t^2)], [sqrt(a) + 1, a sin(x), a b - t^2]]` | 3 | 2 | +| `Rank` of the 4x4 in `APivotsConditionDoesNotHideAZeroRowTest`, whose rows are dependent | 4 | 3 | +| `Rank` of `[[a, b], [c, d]]` | 2 | unchanged | + ### Ei, Ci, Chi and li have limits at their logarithmic singularity Unevaluated limits become values. `Ei(u)`, `Ci(u)` and `Chi(u)` are `eulergamma` plus the logarithm of `u` plus what diff --git a/Sources/AngouriMath/Core/Entity/Omni/Entity.Matrix.cs b/Sources/AngouriMath/Core/Entity/Omni/Entity.Matrix.cs index ddaa0d741..e47083941 100644 --- a/Sources/AngouriMath/Core/Entity/Omni/Entity.Matrix.cs +++ b/Sources/AngouriMath/Core/Entity/Omni/Entity.Matrix.cs @@ -715,31 +715,127 @@ private static (Entity[,] Entries, int Pivots) Eliminate(Matrix matrix, bool red /// /// An entry the elimination made, normalised: a number as it is, a rational function over the rationals as /// its canonical quotient, which keeps the entries from growing with each row operation, and anything else - /// as its inner simplification. + /// as its inner simplification; in each case without a condition on where it has a value. /// + /// + /// The elimination is over the field of rational functions in the symbols, where a pivot is not 0 and + /// dividing by it is exact. The canonical quotient keeps the points where an expression has no value, so + /// the factor -2 b / b is -2 provided not b = 0, and the row operation made from it was an + /// entry no step could read: neither the canonical quotient, which takes no condition, nor + /// Simplify, which left -2 d + 2 - (-2) (d - 1) provided not b = 0 as it was. It was taken + /// for a pivot, the rank of [[b, d - 1], [-2 b, -2 d + 2]] was 2, and the reduced form of + /// [[x, c, x], [x, b, d], [-3 x, -c - 2 b, -2 d - x]] was the identity. The condition is that a + /// pivot is not 0, which is what the forms and the rank already say they assume. + /// https://github.com/asc-community/AngouriMath/issues/2757 + /// private static Entity Normalised(Entity entry) { - var inner = entry.InnerSimplified; + var inner = Unconditioned(entry.InnerSimplified); if (inner is Number) return inner; - return Functions.RationalFunction.TryCanonicalize(inner, out var canonical, wholeCoefficients: true) ? canonical : inner; + return Functions.RationalFunction.TryCanonicalize(inner, out var canonical, wholeCoefficients: true) + ? Unconditioned(canonical) + : inner; + } + + /// without the conditions on where it has a value. + private static Entity Unconditioned(Entity entry) + { + while (entry is Providedf(var expression, _)) + entry = expression; + return entry; } /// /// Whether an entry is 0: a number exactly, a rational function over the rationals by its canonical quotient, - /// and anything else once simplified, a condition on the zero set aside, since an entry that is 0 wherever - /// it has a value is no pivot to divide by. + /// anything else by its values at sampled points where they decide it, and otherwise once simplified, a + /// condition on the zero set aside, since an entry that is 0 wherever it has a value is no pivot to divide by. /// + /// + /// The canonical quotient declines past its bound on size, and the entries of a 4x4 elimination pass it: + /// Simplify did not find the 0 in a quotient of a thousand nodes, and the rank of a singular 4x4 of + /// polynomials was 4. https://github.com/asc-community/AngouriMath/issues/2757 + /// private static bool IsZeroEntry(Entity entry) { + entry = Unconditioned(entry); if (entry is Number.Complex number) return number.IsZero; + // The canonical 0 of a quotient is 0 provided its denominator is not: b / a - b / a. if (Functions.RationalFunction.TryCanonicalize(entry, out var canonical)) - return canonical is Number.Complex { IsZero: true }; - var simplified = entry.Simplify(); - while (simplified is Providedf(var expression, _)) - simplified = expression; - return simplified is Number.Complex { IsZero: true }; + return Unconditioned(canonical) is Number.Complex { IsZero: true }; + if (IsZeroAtSampledPoints(entry) is { } byValue) + return byValue; + return Unconditioned(entry.Simplify()) is Number.Complex { IsZero: true }; + } + + /// + /// Whether is 0 at three sets of values for its symbols, distinct, off any short + /// decimal, and one of them with negative values: where an enclosure of its value + /// at one of them excludes 0, where at two or more it is within 1e-30 of 0 + /// and at none away from it, and where the evaluation cannot place it. + /// + /// + /// The enclosures are rigorous, so a value away from 0 is a nonzero entry. A rational function that is not + /// 0 vanishes on a set of measure zero, and three points like these landing on it to thirty digits is not + /// a thing that happens by accident; for the elimination's purposes, which is the rank for general values + /// of the symbols, an entry 0 at all of them is 0. + /// + private static bool? IsZeroAtSampledPoints(Entity entry) + { + var symbols = entry.Vars.Distinct().ToList(); + var zeros = 0; + for (var set = 0; set < SampledValues.Length; set++) + { + var pinned = entry; + for (var i = 0; i < symbols.Count; i++) + { + var value = PeterO.Numbers.EDecimal.FromString(SampledValues[set][i % SampledValues[set].Length]) + .Add(PeterO.Numbers.EDecimal.FromInt32(i / SampledValues[set].Length)); + pinned = pinned.Substitute(symbols[i], Number.Real.Create(set == 2 && i % 2 == 1 ? value.Negate() : value)); + } + switch (IsZeroAtAPoint(pinned)) + { + case false: + return false; + case true: + zeros++; + break; + } + if (symbols.Count == 0) + return zeros > 0 ? true : null; + } + return zeros >= 2 ? true : null; + } + + [Core.ConstantField] + private static readonly string[][] SampledValues = + { + new[] { "1.3719264803", "0.6182947351", "2.2360917423", "1.9137402968", "0.8329471056", "1.1307846219" }, + new[] { "0.7243918605", "1.5821036479", "1.0476293158", "2.6913584027", "1.2650719384", "0.9384152706" }, + new[] { "1.8462075193", "0.5397162804", "1.6928403517", "0.9215847360", "2.1473960582", "1.4087625391" }, + }; + + /// + /// Whether a number with no symbols is 0: where an enclosure of it excludes 0, + /// where one at forty or eighty digits is within 1e-30 of it, otherwise + /// . + /// + private static bool? IsZeroAtAPoint(Entity value) + { + if (Numerics.IntervalEvaluation.Of(value) is { IsFinite: true } inDoubles + && (!inDoubles.Re.ContainsZero || !inDoubles.Im.ContainsZero)) + return false; + foreach (var digits in new[] { 40, 80 }) + if (Numerics.PreciseEvaluation.Of(value, digits) is { IsFinite: true } inDecimals) + { + if (!inDecimals.Re.ContainsZero || !inDecimals.Im.ContainsZero) + return false; + if (PeterO.Numbers.EDecimal.Max(inDecimals.Re.Magnitude, inDecimals.Im.Magnitude) + .CompareTo(PeterO.Numbers.EDecimal.FromString("1e-30")) <= 0) + return true; + } + return null; } /// diff --git a/Sources/Tests/UnitTests/Algebra/APivotsConditionDoesNotHideAZeroRowTest.cs b/Sources/Tests/UnitTests/Algebra/APivotsConditionDoesNotHideAZeroRowTest.cs new file mode 100644 index 000000000..54494c9e2 --- /dev/null +++ b/Sources/Tests/UnitTests/Algebra/APivotsConditionDoesNotHideAZeroRowTest.cs @@ -0,0 +1,60 @@ +// +// 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.Extensions; +using Xunit; +using static AngouriMath.Entity; + +namespace AngouriMath.Tests.Algebra +{ + /// + /// The elimination's factor -2 b / b was the canonical quotient -2 provided not b = 0, and the entry + /// made from it, -2 d + 2 - (-2) (d - 1) provided not b = 0, was 0 without anything reading it as 0: it + /// was taken for a pivot, and the rank of [[b, d - 1], [-2 b, -2 d + 2]] was 2. Every matrix here has + /// rows that are dependent for every value of the symbols; the ranks and forms are sympy's + /// (Matrix.rank(simplify=True), Matrix.rref). + /// https://github.com/asc-community/AngouriMath/issues/2757 + /// + [Trait("Area", "Algebra")] + public sealed class APivotsConditionDoesNotHideAZeroRowTest + { + [Theory] + [InlineData("[[b, d - 1], [-2 b, -2 d + 2]]", 1)] + [InlineData("[[b, d + 1], [-2 b, -2 d - 2]]", 1)] + [InlineData("[[1, 0, 1], [1, b, d], [-3, -2 b, -2 d - 1]]", 2)] + [InlineData("[[x, c, x], [x, b, d], [-3 x, -c - 2 b, -2 d - x]]", 2)] + [InlineData("[[a + 1, a b, a + 1], [a + 1, b, a b], [-3 a - 3, -a b - 2 b, -2 a b - a - 1]]", 2)] + [InlineData("[[a, b, a b], [a b, a + 1, t^2], [2 a b + 2 a, 2 a + 2 b + 2, 2 a b + 2 t^2]]", 2)] + [InlineData("[[sin(x), d - 1], [-2 sin(x), -2 d + 2]]", 1)] + // Dependent rows whose elimination makes entries past the canonical quotient's bound on size. + [InlineData("[[-2*a*b + c + 3*x, -2*b*d + x^2, t^2 + t/2, 0], [a*(a*b/2 + a*x + t) - c, a*(a/2 + 3*d + 3*t) - 1, -2*a^2*b + a^2*x - t^2 - 2*x^2, a^2*x/2 + 2*a*b + a*t + 2], [c, 1, t^2 + 2*x^2, -2], [a*b/2 + a*x + t, a/2 + 3*d + 3*t, -2*a*b + a*x, (a*x + 4*b + 2*t)/2]]", 3)] + [InlineData("[[sqrt(b), sqrt(a) + 1, 3*a], [3*sqrt(a) + 3, 3*a*sin(x), -3*(-a*b + t^2)], [sqrt(a) + 1, a*sin(x), a*b - t^2]]", 2)] + // An entry that is 0 as a canonical quotient is 0 provided its denominator is not. + [InlineData("[[b / a - b / a, 1], [0, 1]]", 1)] + // Independent: the rank stays full. + [InlineData("[[a, a], [1, 2]]", 2)] + [InlineData("[[a b, b], [1, 1]]", 2)] + [InlineData("[[x, 1, 0, a], [1, x^2, 1, 0], [b, 1, x, 1], [a, 0, 1, sqrt(x) + b]]", 4)] + public void HasItsRank(string matrix, int rank) + => Assert.Equal(rank, ((Matrix)matrix.ToEntity()).Rank); + + [Fact] + public void TheReducedFormIsSympys() + { + var reduced = ((Matrix)"[[x, c, x], [x, b, d], [-3 x, -c - 2 b, -2 d - x]]".ToEntity()).ReducedRowEchelonForm; + var expected = (Matrix)"[[1, 0, (b x - c d) / (x (b - c))], [0, 1, (d - x) / (b - c)], [0, 0, 0]]".ToEntity(); + for (var row = 0; row < 3; row++) + for (var column = 0; column < 3; column++) + { + var difference = (reduced[row, column] - expected[row, column]).Simplify(); + while (difference is Providedf(var inner, _)) + difference = inner; + Assert.Equal(Number.Integer.Zero, difference); + } + } + } +}