Skip to content

Commit

Permalink
Utility for simplifying polynomials.
Browse files Browse the repository at this point in the history
  • Loading branch information
corywalker committed Jul 11, 2017
1 parent 23ae3a0 commit 0976649
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
5 changes: 5 additions & 0 deletions expreduce/builtin_power.go
Expand Up @@ -272,5 +272,10 @@ func GetPowerDefinitions() (defs []Definition) {
Name: "PolynomialGCD",
})
defs = append(defs, Definition{Name: "SquareFreeQ"})
defs = append(defs, Definition{
Name: "PSimplify",
OmitDocumentation: true,
ExpreduceSpecific: true,
})
return
}
6 changes: 6 additions & 0 deletions expreduce/cas_test.go
Expand Up @@ -12,13 +12,16 @@ import (

var testmodules = flag.String("testmodules", "",
"A regexp of modules to test, otherwise test all modules.")
var testsyms = flag.String("testsyms", "",
"A regexp of symbols to test, otherwise test all symbols.")
var verbosetest = flag.Bool("verbosetest", false,
"Print every test case that runs.")
var deftimings = flag.Bool("deftimings", false,
"Show the time consuption aggregations for each definition.")

func TestIncludedModules(t *testing.T) {
var testModEx = regexp.MustCompile(*testmodules)
var testSymEx = regexp.MustCompile(*testsyms)
defSets := GetAllDefinitions()
numTests := 0
lhsDefTimeCounter := TimeCounter{}
Expand All @@ -32,6 +35,9 @@ func TestIncludedModules(t *testing.T) {
}
fmt.Printf("Testing module %s\n", defSet.Name)
for _, def := range defSet.Defs {
if !testSymEx.MatchString(def.Name) {
continue
}
es := NewEvalState()
def.AnnotateWithDynamic(es)
td := TestDesc{
Expand Down
25 changes: 24 additions & 1 deletion expreduce/resources/power.m
Expand Up @@ -36,6 +36,8 @@
PolynomialQ[p_.*v_^Optional[exp_Integer], v_] :=
If[FreeQ[p, v] && Positive[exp], True, False];
PolynomialQ[p_, v_] := If[FreeQ[p, v], True, False];
(*Seemingly undocumented version with no variable specification:*)
PolynomialQ[p_] := PolynomialQ[p, Variables[p]];
Attributes[PolynomialQ] = {Protected};
Tests`PolynomialQ = {
ETests[
Expand Down Expand Up @@ -84,7 +86,8 @@
ESameTest[True, PolynomialQ[x^y, 1]]
], EKnownFailures[
ESameTest[True, PolynomialQ[2*x^2-3x+2, 2]],
ESameTest[True, PolynomialQ[2*x^2-3x, 2]]
ESameTest[True, PolynomialQ[2*x^2-3x, 2]],
ESameTest[False, PolynomialQ[x/y]]
]
};

Expand Down Expand Up @@ -385,3 +388,23 @@
ESameTest[False, SquareFreeQ[(2 x + 3)^2]]
]
};

PSimplify[expr_] := expr;
PSimplify[p_?PolynomialQ/q_?PolynomialQ] :=
If[Length[Variables[p]] === 1 && Variables[p] === Variables[q],
PolynomialQuotient[p, q, Variables[p][[1]]], p/q];
Tests`PSimplify = {
ESimpleExamples[
ESameTest[-1 + x^2, PSimplify[(1 - 2*x^2 + x^4)/(-1 + x^2)]],
ESameTest[4*x, PSimplify[(-4*x + 4*x^3)/(-1 + x^2)]],
ESameTest[-1 - x + x^3 + x^4, PSimplify[(1 - x^2 - x^3 + x^5)/(-1 + x)]],
ESameTest[2*x + 5*x^2 + 5*x^3, PSimplify[(-2*x - 3*x^2 + 5*x^4)/(-1 + x)]],
ESameTest[-6 + 11*x - 6*x^2 + x^3, PSimplify[(18 - 39*x + 29*x^2 - 9*x^3 + x^4)/(-3 + x)]],
ESameTest[13 - 15*x + 4*x^2, PSimplify[(-39 + 58*x - 27*x^2 + 4*x^3)/(-3 + x)]],
ESameTest[-3 - x + 3*x^2 + x^3, PSimplify[(-9 - 6*x + 8*x^2 + 6*x^3 + x^4)/(3 + x)]],
ESameTest[-2 + 6*x + 4*x^2, PSimplify[(-6 + 16*x + 18*x^2 + 4*x^3)/(3 + x)]]
], EKnownDangerous[
ESameTest[12 + 4*x - 15*x^2 - 5*x^3 + 3*x^4 + x^5, PSimplify[(-108 - 108*x + 207*x^2 + 239*x^3 - 81*x^4 - 153*x^5 - 27*x^6 + 21*x^7 + 9*x^8 + x^9)/(-9 - 6*x + 8*x^2 + 6*x^3 + x^4)]],
ESameTest[12 - 54*x - 33*x^2 + 18*x^3 + 9*x^4, PSimplify[(-108 + 414*x + 717*x^2 - 324*x^3 - 765*x^4 - 162*x^5 + 147*x^6 + 72*x^7 + 9*x^8)/(-9 - 6*x + 8*x^2 + 6*x^3 + x^4)]]
],
};

0 comments on commit 0976649

Please sign in to comment.