Skip to content

Commit

Permalink
CharacteristicPolynomial - improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
axkr committed Jan 10, 2024
1 parent 381b437 commit 0911494
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1009,8 +1009,18 @@ public static IAST generateCharacteristicPolynomial(int dim, IAST matrix, IExpr
// new GenPolynomialRing<edu.jas.poly.Complex<BigRational>>(cf, vars);
// GenMatrix<edu.jas.poly.Complex<BigRational>> A;
// pf.charPolynomial(A);
final IExpr[] valuesForIdentityMatrix = {F.C0, variable};
return F.Det(F.Subtract(matrix, diagonalMatrix(valuesForIdentityMatrix, dim)));

return F.Det(subtractDiagonalValue(dim, matrix, variable));
}

private static IASTAppendable subtractDiagonalValue(int dimension, IAST matrix, IExpr value) {
IASTAppendable subtractMatrix = F.ListAlloc(dimension);
for (int i = 1; i < dimension + 1; i++) {
IASTMutable row = ((IAST) matrix.get(i)).copy();
row.set(i, row.get(i).subtract(value));
subtractMatrix.append(row);
}
return subtractMatrix;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,19 @@ public void testAntisymmetricMatrixQ() {
"True");
}


@Test
public void testCharacteristicPolynomial() {
check("CharacteristicPolynomial({{a, b}, {c, d}}, x)", //
"-b*c+a*d-a*x-d*x+x^2");
check("CharacteristicPolynomial({{1, 1, 1}, {1, 1/2, 1/3}, {1, 2, 3}},x)", //
"-1/3-7/3*x+9/2*x^2-x^3");
check("CharacteristicPolynomial(N({{1, 1, 1}, {1, 1/2, 1/3}, {1, 2, 3}}),x)", //
"-0.333333-2.33333*x+4.5*x^2-x^3");
check("CharacteristicPolynomial({{1, 2*I}, {3 + 4*I, 5}}, z)", //
"13-I*6-6*z+z^2");
}

@Test
public void testCholeskyDecomposition() {
check("matG=CholeskyDecomposition({{11.0,3.0},{3.0, 5.0}})", //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2787,18 +2787,6 @@ public void testCharacters() {
"{{\"t\",\"h\",\"i\"},{\"h\",\"i\",\"s\"},{\"i\",\"s\",\" \"},{\"s\",\" \",\"i\"},{\" \",\"i\",\"s\"},{\"i\",\"s\",\" \"},{\"s\",\" \",\"a\"},{\" \",\"a\",\" \"},{\"a\",\" \",\"s\"},{\" \",\"s\",\"t\"},{\"s\",\"t\",\"r\"},{\"t\",\"r\",\"i\"},{\"r\",\"i\",\"n\"},{\"i\",\"n\",\"g\"}}");
}

@Test
public void testCharacteristicPolynomial() {
check("CharacteristicPolynomial({{a, b}, {c, d}}, x)", //
"-b*c+a*d-a*x-d*x+x^2");
check("CharacteristicPolynomial({{1, 1, 1}, {1, 1/2, 1/3}, {1, 2, 3}},x)", //
"-1/3-7/3*x+9/2*x^2-x^3");
check("CharacteristicPolynomial(N({{1, 1, 1}, {1, 1/2, 1/3}, {1, 2, 3}}),x)", //
"-0.333333-2.33333*x+4.5*x^2-x^3");
check("CharacteristicPolynomial({{1, 2*I}, {3 + 4*I, 5}}, z)", //
"13-I*6-6*z+z^2");
}

@Test
public void testCheck() {
check("Check(0^(-42), failure)", //
Expand Down Expand Up @@ -26430,6 +26418,8 @@ public void testValueQ() {

@Test
public void testVariables() {
check("Variables(FactorInteger(1232))", //
"{}");
check("Variables({x+y,x,z})", //
"{x,y,z}");
check("Variables(x + f(x)+Pi*E)", //
Expand Down

0 comments on commit 0911494

Please sign in to comment.