Skip to content

Commit

Permalink
Move Expand function into pure Expreduce.
Browse files Browse the repository at this point in the history
  • Loading branch information
corywalker committed Jul 4, 2017
1 parent 9cfb93f commit bf03b5d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
11 changes: 0 additions & 11 deletions expreduce/builtin_power.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,17 +243,6 @@ func GetPowerDefinitions() (defs []Definition) {
defs = append(defs, Definition{
Name: "Expand",
Usage: "`Expand[expr]` attempts to expand `expr`.",
Rules: []Rule{
{"possibleExponents[n_Integer,m_Integer]", "Flatten[Permutations/@((PadRight[#,m])&/@IntegerPartitions[n,m]),1]"},
{"genVars[addends_List,exponents_List]", "Product[addends[[ExpandUnique`i]]^exponents[[ExpandUnique`i]],{ExpandUnique`i,1,Length[addends]}]"},
{"genExpand[addends_List,exponents_List]", "Sum[(Multinomial@@exponents[[ExpandUnique`i]])*genVars[addends,exponents[[ExpandUnique`i]]],{ExpandUnique`i,1,Length[exponents]}]"},
{"Expand[s_Plus^n_Integer]", "genExpand[List@@s,possibleExponents[n,Length[s]]]"},
{"Expand[s_Plus^n_Integer*rest___]", "Expand[Expand[Plus[s]^n]*rest]"},
{"Expand[a1s_Plus*a2s_Plus*rest___]", "Expand[Sum[ExpandUnique`a1*ExpandUnique`a2,{ExpandUnique`a1,List@@a1s},{ExpandUnique`a2,List@@a2s}]*rest]"},
{"Expand[addends_Plus/den_]", "Sum[Expand[ExpandUnique`a/den],{ExpandUnique`a,List@@addends}]"},
{"Expand[c_?AtomQ*addends_Plus]", "Sum[Expand[c*ExpandUnique`a],{ExpandUnique`a,List@@addends}]"},
{"Expand[a_]", "a"},
},
SimpleExamples: []TestInstruction{
&SameTest{"a^3 + 3 a^2 * b + 3 a b^2 + b^3 + 3 a^2 * c + 6 a b c + 3 b^2 * c + 3 a c^2 + 3 b c^2 + c^3", "Expand[(a + b + c)^3]"},
&SameTest{"a c + b c + a d + b d + a e + b e", "(a + b) * (c + d + e) // Expand"},
Expand Down
15 changes: 15 additions & 0 deletions expreduce/resources/power.er
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
possibleExponents[n_Integer, m_Integer] :=
Flatten[Permutations /@ ((PadRight[#, m]) & /@
IntegerPartitions[n, m]), 1];
genVars[addends_List, exponents_List] :=
Product[addends[[ExpandUnique`i]]^
exponents[[ExpandUnique`i]], {ExpandUnique`i, 1, Length[addends]}];
genExpand[addends_List, exponents_List] :=
Sum[(Multinomial @@ exponents[[ExpandUnique`i]])*
genVars[addends, exponents[[ExpandUnique`i]]], {ExpandUnique`i, 1,
Length[exponents]}];
Expand[a_] := a //. {
s_Plus^n_Integer :>
genExpand[List @@ s, possibleExponents[n, Length[s]]],
c_*s_Plus :> ((c*#) &) /@ s
};

0 comments on commit bf03b5d

Please sign in to comment.