diff --git a/Sources/AngouriMath/Core/Entity/Omni/Entity.Matrix.cs b/Sources/AngouriMath/Core/Entity/Omni/Entity.Matrix.cs index 31025fcf2..9b1fc065b 100644 --- a/Sources/AngouriMath/Core/Entity/Omni/Entity.Matrix.cs +++ b/Sources/AngouriMath/Core/Entity/Omni/Entity.Matrix.cs @@ -311,6 +311,71 @@ public Entity AsScalar() ); private LazyPropertyA determinant; + /// + /// The characteristic polynomial of this square matrix, det(x I - A) in the + /// variable , written as a polynomial in it with each coefficient + /// simplified: for [[a, b], [c, d]] it is x^2 - (a + d) x + a d - b c. + /// where the matrix is not square, or where + /// already occurs in it, since then the polynomial would not be one in . + /// + /// + /// + /// The monic one: its leading coefficient is 1, the next is minus the trace and + /// the constant term is (-1)^n det(A). det(A - x I), the other convention, + /// is (-1)^n times it; the monic one is the one the Cayley–Hamilton theorem and + /// the minimal polynomial are stated with. + /// + /// + /// It is the of x I - A, which divides by nothing: its + /// entries are polynomials in x and the entries of A, and Bareiss' + /// elimination and Laplace expansion both leave a polynomial. Gaussian elimination would + /// leave its pivots, which are polynomials in x, as divisions, and the + /// characteristic polynomial -- defined everywhere -- would have no value where one of + /// them vanished. #381 + /// + /// + /// + /// + /// Console.WriteLine(MathS.Matrix(new Entity[,] { { 1, 2 }, { 3, 4 } }).CharacteristicPolynomial("x")); + /// + /// Prints + /// + /// x ^ 2 - 5 * x - 2 + /// + /// + public Entity? CharacteristicPolynomial(Variable x) + { + if (!IsSquare || ContainsNode(x)) + return null; + var shifted = MathS.Matrix(RowCount, ColumnCount, (row, column) => (row == column ? (Entity)x : 0) - this[row, column]); + if (shifted.Determinant is not { } shiftedDeterminant) + return null; + // Read off power by power, so that each coefficient is simplified on its own + // rather than the whole left as the determinant's expansion wrote it. + var byPower = Functions.Algebra.AnalyticalSolving.PolynomialSolver + .GatherMonomialInformation( + Sumf.LinearChildren(shiftedDeterminant.Expand()), x); + if (byPower is null) + return shiftedDeterminant; + Entity? polynomial = null; + foreach (var power in byPower.Keys.OrderByDescending(static power => power)) + { + var coefficient = byPower[power].Simplify(); + if (coefficient == 0) + continue; + Entity monomial = power.IsZero ? 1 : power.Equals(PeterO.Numbers.EInteger.One) ? x : MathS.Pow(x, Number.Integer.Create(power)); + // A term whose coefficient reads more simply negated is subtracted, as it is + // written by hand: x^2 - 5 x, and x^2 - (a + d) x rather than + (-a - d) x. + var negated = (-coefficient).Simplify(); + var subtracted = polynomial is not null + && (coefficient is Number.Real { EDecimal.IsNegative: true } || negated.Complexity < coefficient.Complexity); + var magnitude = subtracted ? negated : coefficient; + var term = magnitude == 1 ? monomial : power.IsZero ? magnitude : magnitude * monomial; + polynomial = polynomial is null ? term : subtracted ? polynomial - term : polynomial + term; + } + return polynomial ?? 0; + } + /// Returns an inverse matrix if it exists public Matrix? Inverse => inverse.GetValue(static @this => { diff --git a/Sources/Tests/UnitTests/Algebra/CharacteristicPolynomialTest.cs b/Sources/Tests/UnitTests/Algebra/CharacteristicPolynomialTest.cs new file mode 100644 index 000000000..c3ad14def --- /dev/null +++ b/Sources/Tests/UnitTests/Algebra/CharacteristicPolynomialTest.cs @@ -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; +using AngouriMath.Extensions; +using Xunit; +using static AngouriMath.Entity.Number; + +namespace AngouriMath.Tests.Algebra +{ + /// + /// The characteristic polynomial of a square matrix, det(x I - A). + /// https://github.com/asc-community/AngouriMath/issues/381 + /// + [Trait("Area", "Algebra")] + public sealed class CharacteristicPolynomialTest + { + private static Entity Of(Entity[,] entries) => MathS.Matrix(entries).CharacteristicPolynomial("x")!; + + /// The textbook one: x^2 - (a + d) x + a d - b c, the trace and the determinant. + [Fact] + public void TwoByTwoIsTheTraceAndTheDeterminant() + => Assert.Equal(Integer.Zero, + (Of(new Entity[,] { { "a", "b" }, { "c", "d" } }) - "x^2 - (a + d) * x + a * d - b * c".ToEntity()).Simplify()); + + [Theory] + [InlineData("x ^ 2 - 5 * x - 2")] + public void ItIsWrittenAsAPolynomialInTheVariable(string written) + { + Assert.Equal(written, Of(new Entity[,] { { 1, 2 }, { 3, 4 } }).Stringize()); + Assert.Equal("x ^ 2 - (a + d) * x + d * a - b * c", Of(new Entity[,] { { "a", "b" }, { "c", "d" } }).Stringize()); + } + + /// + /// A value everywhere, at the diagonal entries included: there Gaussian elimination's + /// pivots, x - 1 and the like, vanish, and a polynomial computed through them had + /// no value. + /// + [Theory] + [InlineData(1)] + [InlineData(4)] + public void ItHasAValueAtEveryPoint(int at) + => Assert.Equal(Integer.Create(-6), Of(new Entity[,] { { 1, 2 }, { 3, 4 } }).Substitute("x", at).Evaled); + + /// + /// A triangular matrix's eigenvalues are its diagonal entries, so they are the roots, and + /// the constant term is (-1)^n det(A): here -(2 · 3 · -1), which is 6. + /// + [Fact] + public void ATriangularMatrixHasItsDiagonalForRoots() + { + var polynomial = Of(new Entity[,] { { 2, 5, 7 }, { 0, 3, 1 }, { 0, 0, -1 } }); + foreach (var root in new[] { 2, 3, -1 }) + Assert.Equal(Integer.Zero, polynomial.Substitute("x", root).Evaled); + Assert.Equal(Integer.Create(6), polynomial.Substitute("x", 0).Evaled); + } + + [Fact] + public void OnlyForASquareMatrixWithoutTheVariable() + { + Assert.Null(MathS.Matrix(new Entity[,] { { 1, 2, 3 }, { 4, 5, 6 } }).CharacteristicPolynomial("x")); + Assert.Null(MathS.Matrix(new Entity[,] { { "x", 1 }, { 0, 1 } }).CharacteristicPolynomial("x")); + } + } +} diff --git a/Sources/Tests/UnitTests/Common/PublicApi.txt b/Sources/Tests/UnitTests/Common/PublicApi.txt index d7302cae4..0586b67c7 100644 --- a/Sources/Tests/UnitTests/Common/PublicApi.txt +++ b/Sources/Tests/UnitTests/Common/PublicApi.txt @@ -1410,6 +1410,7 @@ AngouriMath.Entity+Matrix+EntityTensorWrapperOperations.ToString(AngouriMath.Ent AngouriMath.Entity+Matrix.$() : AngouriMath.Entity+Matrix AngouriMath.Entity+Matrix.Adjugate { } : AngouriMath.Entity+Matrix AngouriMath.Entity+Matrix.AsScalar() : AngouriMath.Entity +AngouriMath.Entity+Matrix.CharacteristicPolynomial(AngouriMath.Entity+Variable) : AngouriMath.Entity AngouriMath.Entity+Matrix.Codomain { } : AngouriMath.Core.Domain AngouriMath.Entity+Matrix.ColumnCount { } : System.Int32 AngouriMath.Entity+Matrix.Determinant { } : AngouriMath.Entity