Skip to content

Commit

Permalink
Add FactorTermsList.
Browse files Browse the repository at this point in the history
  • Loading branch information
corywalker committed Jul 9, 2017
1 parent 18ce7f5 commit 2587567
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
30 changes: 30 additions & 0 deletions expreduce/builtin_power.go
Expand Up @@ -388,5 +388,35 @@ func GetPowerDefinitions() (defs []Definition) {
&SameTest{"1-x/a", "PolynomialRemainder[1 + x^3, 1 + a*x^2, x]"},
},
})
defs = append(defs, Definition{
Name: "FactorTermsList",
Usage: "`FactorTermsList[expr]` factors out the constant term of `expr` and puts the factored result into a `List`.",
SimpleExamples: []TestInstruction{
&SameTest{"{2,Sin[8 k]}", "FactorTermsList[2*Sin[8*k]]"},
&SameTest{"{1/2,a+x}", "FactorTermsList[a/2 + x/2]"},
&SameTest{"{1,a+x}", "FactorTermsList[a + x]"},
},
Tests: []TestInstruction{
&SameTest{"{1,1}", "FactorTermsList[1]"},
&SameTest{"{5,1}", "FactorTermsList[5]"},
&SameTest{"{5.,1}", "FactorTermsList[5.]"},
&SameTest{"{1,a}", "FactorTermsList[a]"},
&SameTest{"{1/2,a}", "FactorTermsList[a/2]"},
&SameTest{"{-(3/2),x}", "FactorTermsList[(-3*x)/2]"},
&SameTest{"{2,a+x}", "FactorTermsList[2*a + 2*x]"},
&SameTest{"{1/2,a/(2 b+2 c)+x/(2 b+2 c)}", "FactorTermsList[(a/2 + x/2)/(2*b + 2*c)]"},
&SameTest{"{1,2+x^2}", "FactorTermsList[(8 + 4*x^2)/4]"},
&SameTest{"{-(1/2),2+3 x+x^2}", "FactorTermsList[(-4 - 6*x - 2*x^2)/4]"},
&SameTest{"{-(1/2),-2+3 x+x^2}", "FactorTermsList[(2 - 3*x - x^2)/2]"},
&SameTest{"{-(1/2),-2-3 x+x^2}", "FactorTermsList[(2 + 3*x - x^2)/2]"},
&SameTest{"{1/2,2+3 x+x^2}", "FactorTermsList[(2 + 3*x + x^2)/2]"},
&SameTest{"{1/2,-2-3 x+x^2}", "FactorTermsList[(-2 - 3*x + x^2)/2]"},
&SameTest{"{5,2+3 x+x^2}", "FactorTermsList[5*(1 + x)*(2 + x)]"},
&SameTest{"{40,1+3 x+3 x^2+x^3}", "FactorTermsList[5*(2 + 2*x)^3]"},
&SameTest{"{-6,1+x}", "FactorTermsList[(-12 - 12*x)/2]"},
&SameTest{"{2/3,3+x}", "FactorTermsList[(18 + 6*x)/9]"},
&SameTest{"{-(2800301/67344500),1-2 x+x^3}", "FactorTermsList[(-2800301/538756 + (2800301*x)/269378 - (2800301*x^3)/538756)/125]"},
},
})
return
}
4 changes: 3 additions & 1 deletion expreduce/resources/power.er
Expand Up @@ -83,9 +83,11 @@ FactorTermsList[expr_] := Module[{e = expr, toFactor, cTerms, c},
If[Head[toFactor] =!= Plus,
Return[ExpreduceConstantTerm[toFactor]]
];
cTerms = (ExpreduceConstantTerm /@ (List @@ toFactor) //
(* Parens are necessary due to precedence issue. *)
cTerms = ((ExpreduceConstantTerm /@ (List @@ toFactor)) //
Transpose)[[1]];
c = GCD @@ cTerms;
If[Last[cTerms] < 0, c = -c];
{c, toFactor/c // Expand}
];
Attributes[FactorTermsList] = {Protected};

0 comments on commit 2587567

Please sign in to comment.