Skip to content

Commit

Permalink
Factor out functional|list|manip|matrix|numbertheory|patt|power|random
Browse files Browse the repository at this point in the history
  • Loading branch information
corywalker committed Jul 22, 2017
1 parent eb53197 commit 27403c2
Show file tree
Hide file tree
Showing 13 changed files with 852 additions and 806 deletions.
2 changes: 2 additions & 0 deletions example/factorout.go
Expand Up @@ -91,6 +91,8 @@ func main() {
b.WriteString(fmt.Sprintf("%v\n", tests.String()))
}
b.WriteString(fmt.Sprintf("\n"))
} else {
//fmt.Printf("<<<%v has no ::usage so skipping!>>>\n", def.Name)
}
}
fmt.Printf("%s\n", strings.TrimSpace(strings.Replace(b.String(), "\t", " ", -1)))
Expand Down
9 changes: 9 additions & 0 deletions expreduce/builtin.go
Expand Up @@ -56,6 +56,15 @@ func ToTestInstructions(tc *Expression) []TestInstruction {
st.Parts[1].(*String).Val, st.Parts[2].(*String).Val})
continue
}
if st, isSt := HeadAssertion(tiEx, "System`EExampleOnlyInstruction"); isSt {
if len(st.Parts) != 3 {
log.Fatalf("Invalid test case: %v\n", tiEx)
continue
}
instructions = append(instructions, &ExampleOnlyInstruction{
st.Parts[1].(*String).Val, st.Parts[2].(*String).Val})
continue
}
if st, isSt := HeadAssertion(tiEx, "System`EComment"); isSt {
if len(st.Parts) != 2 {
log.Fatalf("Invalid test case: %v\n", tiEx)
Expand Down
73 changes: 0 additions & 73 deletions expreduce/builtin_functional.go
Expand Up @@ -5,37 +5,12 @@ import "math/big"
func getFunctionalDefinitions() (defs []Definition) {
defs = append(defs, Definition{
Name: "Function",
Usage: "`Function[inner]` defines a pure function where `inner` is evaluated with `Slot` parameters.\n\n" +
"`Function[x, inner]` defines a pure function where `inner` is evaluated a single parameter `x`.",
Attributes: []string{"HoldAll"},
SimpleExamples: []TestInstruction{
&SameTest{"1 + x", "Function[1 + #][x]"},
&SameTest{"1 + x + 2y", "Function[1 + # + 2#2][x, y]"},
&SameTest{"a^2", "Function[x, x^2][a]"},
&SameTest{"a^2", "Function[x, x^2][a, b]"},
&SameTest{"x^2", "Function[x, x^2][x]"},
&SameTest{"4", "Function[x, x^2][-2]"},
},
})
defs = append(defs, Definition{
Name: "Slot",
Usage: "`#` serves as a pure function's first parameter.\n\n" +
"`#n` serves as a pure function's `n`'th parameter.",
Attributes: []string{"NHoldAll"},
SimpleExamples: []TestInstruction{
&SameTest{"1 + x", "Function[1 + #][x]"},
&SameTest{"1 + x + 2y", "Function[1 + # + 2#2][x, y]"},
&SameTest{"True", "# === Slot[1]"},
&SameTest{"True", "#2 === Slot[2]"},
&SameTest{"2a + 4b", "(4 # + (2 # &)[a] &)[b]"},
},
Tests: []TestInstruction{
&SameTest{"foo[test, k]", "(foo[test, #] &) &[j][k]"},
},
})
defs = append(defs, Definition{
Name: "Apply",
Usage: "`Apply[f, e]` (`f@@e`) replaces the head of expression `e` with `f`.",
legacyEvalFn: func(this *Expression, es *EvalState) Ex {
if len(this.Parts) != 3 {
return this
Expand All @@ -50,29 +25,9 @@ func getFunctionalDefinitions() (defs []Definition) {
}
return this.Parts[2]
},
SimpleExamples: []TestInstruction{
&SameTest{"bar[syma, symb]", "Apply[bar, foo[syma, symb]]"},
&SameTest{"bar[syma, symb]", "bar@@foo[syma, symb]"},
&SameTest{"{syma, symb}", "List@@(syma + symb)"},
&TestComment{"`Apply` is useful in performing aggregations on `List`s:"},
&SameTest{"12", "Times @@ {2, 6}"},
&SameTest{"a b", "Times @@ {a, b}"},
},
FurtherExamples: []TestInstruction{
&TestComment{"`Apply` has no effect on atoms:"},
&SameTest{"1", "foo @@ 1"},
&SameTest{"bar", "foo @@ bar"},
},
Tests: []TestInstruction{
&SameTest{"foo[a,b,c]", "Apply[foo, {a,b,c}]"},
&SameTest{"foo[bar, buzz]", "Apply[foo, {bar, buzz}]"},
&SameTest{"foo[bar, buzz]", "foo @@ {bar, buzz}"},
&SameTest{"foo[1, 2]", "foo @@ {1, 2}"},
},
})
defs = append(defs, Definition{
Name: "Map",
Usage: "`Map[f, expr]` returns a new expression with the same head as `expr`, but with `f` mapped to each of the arguments.",
legacyEvalFn: func(this *Expression, es *EvalState) Ex {
if len(this.Parts) != 3 {
return this
Expand All @@ -91,22 +46,9 @@ func getFunctionalDefinitions() (defs []Definition) {
}
return this.Parts[2]
},
SimpleExamples: []TestInstruction{
&SameTest{"{foo[a], foo[b], foo[c]}", "Map[foo, {a, b, c}]"},
&SameTest{"{foo[a], foo[b], foo[c]}", "foo /@ {a, b, c}"},
&SameTest{"{2, 4, 9}", "Times /@ {2, 4, 9}"},
&SameTest{"{foo[{a, b}], foo[c]}", "Map[foo, {{a, b}, c}]"},
&SameTest{"Map[foo]", "Map[foo]"},
&SameTest{"foo", "Map[foo, foo]"},
&SameTest{"Map[foo, foo, foo]", "Map[foo, foo, foo]"},
&TestComment{"Pure functions are useful with `Map`:"},
&SameTest{"{4,16}", "Function[x, x^2] /@ {2,4}"},
&SameTest{"{4,16}", "Function[#^2] /@ {2,4}"},
},
})
defs = append(defs, Definition{
Name: "Array",
Usage: "`Array[f, n]` creates a list of `f[i]`, with `i` = 1 to `n`.",
legacyEvalFn: func(this *Expression, es *EvalState) Ex {
if len(this.Parts) != 3 {
return this
Expand All @@ -126,24 +68,9 @@ func getFunctionalDefinitions() (defs []Definition) {
}
return this.Parts[2]
},
SimpleExamples: []TestInstruction{
&SameTest{"{f[1], f[2], f[3]}", "Array[f, 3]"},
&SameTest{"Null", "mytest[x_] := 5"},
&SameTest{"{5, 5, 5}", "Array[mytest, 3]"},
&SameTest{"{(a + b)[1], (a + b)[2], (a + b)[3]}", "Array[a + b, 3]"},
&SameTest{"Array[a, a]", "Array[a, a]"},
},
})
defs = append(defs, Definition{
Name: "Identity",
Usage: "`Identity[expr_]` returns `expr`.",
SimpleExamples: []TestInstruction{
&SameTest{"5", "Identity[5]"},
&SameTest{"a", "Identity[Identity[a]]"},
},
Rules: []Rule{
{"Identity[expr_]", "expr"},
},
})
return
}
93 changes: 0 additions & 93 deletions expreduce/builtin_matrix.go
Expand Up @@ -59,25 +59,10 @@ func calcIJ(i, j, innerDim int64, a, b *Expression) Ex {
func GetMatrixDefinitions() (defs []Definition) {
defs = append(defs, Definition{
Name: "Inverse",
Usage: "`Inverse[mat]` finds the inverse of the square matrix `mat`.",
Details: "The row-reduce method has not been added yet, but the shortcuts to finding the inverses of matrices up to 3x3 have been added.",
Rules: []Rule{
{"Inverse[{{a_}}]", "{{1/a}}"},
{"Inverse[{{a_, b_}, {c_, d_}}]", "{{d/(-b c + a d), -(b/(-b c + a d))}, {-(c/(-b c + a d)), a/(-b c + a d)}}"},
{"Inverse[{{a_, b_, c_}, {d_, e_, f_}, {h_, i_, j_}}]", "{{(-f i + e j)/(-c e h + b f h + c d i - a f i - b d j + a e j), (c i - b j)/(-c e h + b f h + c d i - a f i - b d j + a e j), (-c e + b f)/(-c e h + b f h + c d i - a f i - b d j + a e j)}, {(f h - d j)/(-c e h + b f h + c d i - a f i - b d j + a e j), (-c h + a j)/(-c e h + b f h + c d i - a f i - b d j + a e j), (c d - a f)/(-c e h + b f h + c d i - a f i - b d j + a e j)}, {(-e h + d i)/(-c e h + b f h + c d i - a f i - b d j + a e j), (b h - a i)/(-c e h + b f h + c d i - a f i - b d j + a e j), (-b d + a e)/(-c e h + b f h + c d i - a f i - b d j + a e j)}}"},
},
SimpleExamples: []TestInstruction{
&SameTest{"{{-2, 1}, {3/2, -(1/2)}}", "Inverse[{{1, 2}, {3, 4}}]"},
&SameTest{"{{2/5, -(1/5), 0}, {-(1/15), 19/45, -(1/9)}, {-(1/15), -(11/45), 2/9}}", "Inverse[{{3, 2, 1}, {1, 4, 2}, {2, 5, 7}}]"},
},
FurtherExamples: []TestInstruction{
&TestComment{"Symbolic elements are handled correctly:"},
&SameTest{"Inverse[{{b}}]", "{{1/b}}"},
},
})
defs = append(defs, Definition{
Name: "Dimensions",
Usage: "`Dimensions[expr]` finds the dimensions of `expr`.",
legacyEvalFn: func(this *Expression, es *EvalState) Ex {
if len(this.Parts) != 2 {
return this
Expand All @@ -88,68 +73,17 @@ func GetMatrixDefinitions() (defs []Definition) {
}
return intSliceToList(dimensions(expr, 0, &es.CASLogger))
},
SimpleExamples: []TestInstruction{
&SameTest{"{2, 2}", "Dimensions[{{1, 3}, {1, 2}}]"},
&SameTest{"{2}", "Dimensions[{{{1, 2}, {3, 2}}, {{1, 2}}}]"},
},
FurtherExamples: []TestInstruction{
&SameTest{"{}", "Dimensions[foo]"},
&TestComment{"`Dimensions` works with any head, not just `List`:"},
&SameTest{"{0}", "Dimensions[foo[]]"},
},
Tests: []TestInstruction{
&SameTest{"{2}", "Dimensions[{1, 2}]"},
&SameTest{"{0}", "Dimensions[{}]"},
&SameTest{"{1}", "Dimensions[foo[{1}]]"},
&SameTest{"{1, 1}", "Dimensions[{{1}}]"},
&SameTest{"{2, 1}", "Dimensions[{{1}, {1}}]"},
&SameTest{"{2}", "Dimensions[{{1}, {1, 2}}]"},
&SameTest{"{2, 2}", "Dimensions[{{1, 3}, {1, 2}}]"},
&SameTest{"{2, 2, 1}", "Dimensions[{{{1}, {3}}, {{1}, {2}}}]"},
&SameTest{"{2, 2, 2}", "Dimensions[{{{1, 2}, {3, 2}}, {{1, 2}, {2, 2}}}]"},
&SameTest{"{2}", "Dimensions[{{{1, 2}, {3, 2}}, {{1, 2}}}]"},
&SameTest{"{2, 2, 2}", "Dimensions[{{{1, 2}, {3, 2}}, {{1, 2}, {foo, 2}}}]"},
&SameTest{"{2, 2}", "Dimensions[{{{1, 2}, {3, 2}}, {{1, 2}, foo[foo, 2]}}]"},
&SameTest{"{1, 2, 0}", "Dimensions[{{{}, {}}}]"},
&SameTest{"{1, 2}", "Dimensions[{{{}, foo[x]}}]"},
},
})
defs = append(defs, Definition{
Name: "VectorQ",
Usage: "`VectorQ[expr]` returns True if `expr` is a vector, False otherwise.",
legacyEvalFn: singleParamQEval(vectorQ),
SimpleExamples: []TestInstruction{
&SameTest{"True", "VectorQ[{1, 2, c}]"},
&SameTest{"True", "VectorQ[{1, 2, foo[a]}]"},
&SameTest{"False", "VectorQ[foo[1, 2, 3]]"},
&SameTest{"False", "VectorQ[{1, 2, 3, {}}]"},
&SameTest{"True", "VectorQ[{f[a], f[b]}]"},
&SameTest{"True", "VectorQ[{a, c}]"},
&SameTest{"True", "VectorQ[{}]"},
},
})
defs = append(defs, Definition{
Name: "MatrixQ",
Usage: "`MatrixQ[expr]` returns True if `expr` is a 2D matrix, False otherwise.",
legacyEvalFn: singleParamQLogEval(matrixQ),
SimpleExamples: []TestInstruction{
&SameTest{"False", "MatrixQ[{}]"},
&SameTest{"True", "MatrixQ[{{}}]"},
&SameTest{"True", "MatrixQ[{{a}}]"},
&SameTest{"False", "MatrixQ[{{{}}}]"},
&SameTest{"False", "MatrixQ[{{{a}}}]"},
&SameTest{"True", "MatrixQ[{{a}, {b}}]"},
&SameTest{"True", "MatrixQ[{{a, b}, {c, d}}]"},
&SameTest{"False", "MatrixQ[{{a, b, e}, {c, d}}]"},
&SameTest{"True", "MatrixQ[{{a, b, e}, {c, d, f}}]"},
&SameTest{"False", "MatrixQ[{{{a}, {b}}, {{c}, {d}}}]"},
&SameTest{"True", "MatrixQ[{{a, b, e}}]"},
},
})
defs = append(defs, Definition{
Name: "Dot",
Usage: "`a.b` computes the product of `a` and `b` for vectors and matrices.",
Attributes: []string{"Flat", "OneIdentity"},
legacyEvalFn: func(this *Expression, es *EvalState) Ex {
if len(this.Parts) == 2 {
return this.Parts[1]
Expand Down Expand Up @@ -204,29 +138,9 @@ func GetMatrixDefinitions() (defs []Definition) {
}
return this
},
SimpleExamples: []TestInstruction{
&SameTest{"a c + b d", "{a, b}.{c, d}"},
&SameTest{"{a, b}.{c, d, e}", "{a, b}.{c, d, e}"},
&SameTest{"Dot[1, {c, d, e}]", "Dot[1, {c, d, e}]"},
&SameTest{"0", "Dot[{}, {}]"},
&SameTest{"{{a, b}, {c, d}}.{e, f, g}", "{{a, b}, {c, d}}.{e, f, g}"},
&SameTest{"{a, b}", "Dot[{a, b}]"},
&SameTest{"a", "Dot[a]"},
&SameTest{"1", "Dot[1]"},

// Matrix multiplication
&SameTest{"{{a e + b g, a f + b h}, {c e + d g, c f + d h}}", "{{a, b}, {c, d}}.{{e, f}, {g, h}}"},
&SameTest{"{{a e + b f}, {c e + d f}}", "{{a, b}, {c, d}}.{{e}, {f}}"},
&SameTest{"{{a, b}, {c, d}}.{{e, f}}", "{{a, b}, {c, d}}.{{e, f}}"},
},
KnownFailures: []TestInstruction{
&SameTest{"{a e + b f, c e + d f}", "{{a, b}, {c, d}}.{e, f}"},
&SameTest{"{a e + c f, b e + d f}", "{e, f}.{{a, b}, {c, d}}"},
},
})
defs = append(defs, Definition{
Name: "Transpose",
Usage: "`Transpose[mat]` transposes the first two levels of `mat`",
legacyEvalFn: func(this *Expression, es *EvalState) Ex {
if len(this.Parts) != 2 {
return this
Expand All @@ -250,13 +164,6 @@ func GetMatrixDefinitions() (defs []Definition) {
}
return toReturn
},
SimpleExamples: []TestInstruction{
&SameTest{"Transpose[{a, b}]", "Transpose[{a, b}]"},
&SameTest{"{{a, b}}", "Transpose[{{a}, {b}}]"},
&SameTest{"{{a}, {b}}", "Transpose[{{a, b}}]"},
&SameTest{"{{{a}}, {{b}}}", "Transpose[{{{a}, {b}}}]"},
&SameTest{"Transpose[a]", "Transpose[a]"},
},
})
return
}

0 comments on commit 27403c2

Please sign in to comment.