Skip to content

Commit

Permalink
Factor out comb|comp|expr|flow
Browse files Browse the repository at this point in the history
  • Loading branch information
corywalker committed Jul 22, 2017
1 parent aa9d42f commit eb53197
Show file tree
Hide file tree
Showing 10 changed files with 503 additions and 469 deletions.
4 changes: 1 addition & 3 deletions example/factorout.go
Expand Up @@ -69,8 +69,6 @@ func main() {
tests.WriteString(fmt.Sprintf("EComment[\"%v\"]", tComment.Comment))
} else if tString, tIsString := t.(*expreduce.StringTest); tIsString {
tests.WriteString(fmt.Sprintf("EStringTest[\"%v\", \"%v\"]", tString.Out, tString.In))
} else if tDiff, tIsDiff := t.(*expreduce.DiffTest); tIsDiff {
tests.WriteString(fmt.Sprintf("EDiffTest[%v, %v]", tDiff.Out, tDiff.In))
} else if tExampleOnly, tIsExampleOnly := t.(*expreduce.ExampleOnlyInstruction); tIsExampleOnly {
tests.WriteString(fmt.Sprintf("EExampleOnlyInstruction[\"%v\", \"%v\"]", tExampleOnly.Out, tExampleOnly.In))
} else if _, tIsResetState := t.(*expreduce.ResetState); tIsResetState {
Expand All @@ -95,6 +93,6 @@ func main() {
b.WriteString(fmt.Sprintf("\n"))
}
}
fmt.Printf("%s\n", strings.Replace(b.String(), "\t", " ", -1))
fmt.Printf("%s\n", strings.TrimSpace(strings.Replace(b.String(), "\t", " ", -1)))
}
}
59 changes: 0 additions & 59 deletions expreduce/builtin_combinatorics.go
Expand Up @@ -89,8 +89,6 @@ func factorial(n *big.Int) (result *big.Int) {
func getCombinatoricsDefinitions() (defs []Definition) {
defs = append(defs, Definition{
Name: "IntegerPartitions",
Usage: "`IntegerPartitions[n]` lists the possible ways to partition `n` into smaller integers.\n\n" +
"`IntegerPartitions[n, k]` lists the possible ways to partition `n` into smaller integers, using up to `k` elements.",
legacyEvalFn: func(this *Expression, es *EvalState) Ex {
if len(this.Parts) != 2 && len(this.Parts) != 3 {
return this
Expand Down Expand Up @@ -132,29 +130,9 @@ func getCombinatoricsDefinitions() (defs []Definition) {

return exParts
},
SimpleExamples: []TestInstruction{
&TestComment{"Find the partitions of 4:"},
&SameTest{"{{4}, {3, 1}, {2, 2}, {2, 1, 1}, {1, 1, 1, 1}}", "IntegerPartitions[4]"},
&TestComment{"Find the partitions of 10, using a maximum of k = 2 integers:"},
&SameTest{"{{10}, {9, 1}, {8, 2}, {7, 3}, {6, 4}, {5, 5}}", "IntegerPartitions[10, 2]"},
},
FurtherExamples: []TestInstruction{
&TestComment{"The partitions of zero is a nested empty List:"},
&SameTest{"{{}}", "IntegerPartitions[0]"},
},
Tests: []TestInstruction{
&SameTest{"{{1}}", "IntegerPartitions[1]"},
&SameTest{"{}", "IntegerPartitions[-1]"},
&SameTest{"{}", "IntegerPartitions[-5]"},
&SameTest{"IntegerPartitions[.5]", "IntegerPartitions[.5]"},
// With k
&SameTest{"{{10}}", "IntegerPartitions[10, 1]"},
&SameTest{"{}", "IntegerPartitions[10, 0]"},
},
})
defs = append(defs, Definition{
Name: "Permutations",
Usage: "`Permutations[list]` lists the possible permutations for a given list.",
legacyEvalFn: func(this *Expression, es *EvalState) Ex {
if len(this.Parts) != 2 {
return this
Expand All @@ -178,31 +156,12 @@ func getCombinatoricsDefinitions() (defs []Definition) {

return exPerms
},
SimpleExamples: []TestInstruction{
&TestComment{"Find the permutations of `{1, 2, 3}`:"},
&SameTest{"{{1, 2, 3}, {1, 3, 2}, {2, 1, 3}, {2, 3, 1}, {3, 1, 2}, {3, 2, 1}}", "Permutations[Range[3]]"},
&TestComment{"`Permutations` ignores duplicates:"},
&SameTest{"{{1, 2, 2}, {2, 1, 2}, {2, 2, 1}}", "Permutations[{1, 2, 2}]"},
},
})
defs = append(defs, Definition{
Name: "Multinomial",
Usage: "`Multinomial[n1, n2, ...]` gives the multinomial coefficient for the given term.",
Attributes: []string{"Listable", "NumericFunction", "Orderless", "ReadProtected"},
Rules: []Rule{
{"Multinomial[seq___]", "Factorial[Apply[Plus, {seq}]] / Apply[Times, Map[Factorial, {seq}]]"},
},
SimpleExamples: []TestInstruction{
&TestComment{"Find the multinomial coefficient for the 1, 3, 1 term:"},
&SameTest{"20", "Multinomial[1, 3, 1]"},
&TestComment{"`Multinomial` handles symbolic arguments:"},
&SameTest{"Factorial[k+2] / Factorial[k]", "Multinomial[1,k,1]"},
},
})
defs = append(defs, Definition{
Name: "Factorial",
Usage: "`n!` returns the factorial of `n`.",
Attributes: []string{"Listable", "NumericFunction", "ReadProtected"},
legacyEvalFn: func(this *Expression, es *EvalState) Ex {
if len(this.Parts) != 2 {
return this
Expand All @@ -216,24 +175,6 @@ func getCombinatoricsDefinitions() (defs []Definition) {
}
return this
},
SimpleExamples: []TestInstruction{
&SameTest{"2432902008176640000", "20!"},
&SameTest{"120", "Factorial[5]"},
},
FurtherExamples: []TestInstruction{
&SameTest{"1", "Factorial[0]"},
&SameTest{"ComplexInfinity", "Factorial[-1]"},
},
Tests: []TestInstruction{
&SameTest{"1", "Factorial[1]"},
&SameTest{"1", "Factorial[0]"},
&SameTest{"1", "Factorial[-0]"},
&SameTest{"ComplexInfinity", "Factorial[-10]"},
&SameTest{"120", "Factorial[5]"},

&SameTest{"Indeterminate", "0 * Infinity"},
&SameTest{"Indeterminate", "0 * ComplexInfinity"},
},
})
return
}

0 comments on commit eb53197

Please sign in to comment.