Skip to content

Commit

Permalink
Merge pull request #202 from corywalker/corywalker
Browse files Browse the repository at this point in the history
Corywalker
  • Loading branch information
corywalker committed Sep 7, 2023
2 parents 262adcd + e7c6025 commit 190864c
Show file tree
Hide file tree
Showing 54 changed files with 2,172 additions and 461 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -128,6 +128,8 @@ go test ./...
# Or to test some important parts with helpful information printed:
go test -v github.com/corywalker/expreduce/expreduce -count=1
# To exit early, press Ctrl-\
# To quickly iterate on one module:
go generate ./expreduce/builtin.go && go test -v github.com/corywalker/expreduce/expreduce -count=1 -testmodules=combinatorics -run=TestIncludedModules
```

The use of `go generate` might require the download of additional dependencies, for example `go install github.com/go-bindata/go-bindata/...@latest`.
31 changes: 26 additions & 5 deletions expreduce.go
Expand Up @@ -19,12 +19,27 @@ import (
)

var debug = flag.Bool("debug", false, "Debug mode. No initial definitions.")
var rawterm = flag.Bool("rawterm", false, "Do not use readline. Useful for pexpect integration.")

var rawterm = flag.Bool(
"rawterm",
false,
"Do not use readline. Useful for pexpect integration.",
)
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
var netprofile = flag.Bool("netprofile", false, "Enable live profiling at http://localhost:8080/debug/pprof/")

var netprofile = flag.Bool(
"netprofile",
false,
"Enable live profiling at http://localhost:8080/debug/pprof/",
)
var scriptfile = flag.String("script", "", "script `file` to read from")
var initfile = flag.String("initfile", "", "A script to run on initialization.")
var preloadRubi = flag.Bool("preloadrubi", false, "Preload the Rubi definitions for integral support on startup.")

var preloadRubi = flag.Bool(
"preloadrubi",
false,
"Preload the Rubi definitions for integral support on startup.",
)

func main() {
flag.Parse()
Expand All @@ -48,7 +63,9 @@ func main() {

es := expreduce.NewEvalState()
if *preloadRubi {
fmt.Println("Pre-loading Rubi snapshot for integral support. Disable with -preloadrubi=false.")
fmt.Println(
"Pre-loading Rubi snapshot for integral support. Disable with -preloadrubi=false.",
)
es.Eval(atoms.E(atoms.S("LoadRubiBundledSnapshot")))
fmt.Println("Done loading Rubi snapshot.")
fmt.Print("\n")
Expand Down Expand Up @@ -136,7 +153,11 @@ func interactiveSession(es *expreduce.EvalState) {
}
}

func printFormattedOutput(es *expreduce.EvalState, res expreduceapi.Ex, promptNum int) {
func printFormattedOutput(
es *expreduce.EvalState,
res expreduceapi.Ex,
promptNum int,
) {
isNull := false
asSym, isSym := res.(*atoms.Symbol)
if isSym {
Expand Down
13 changes: 11 additions & 2 deletions expreduce/atoms/calculator.go
Expand Up @@ -11,7 +11,13 @@ const (
FoldFnMul
)

func typedRealPart(fn foldFn, i *Integer, r *Rational, f *Flt, c *Complex) expreduceapi.Ex {
func typedRealPart(
fn foldFn,
i *Integer,
r *Rational,
f *Flt,
c *Complex,
) expreduceapi.Ex {
if c != nil {
toReturn := c
if f != nil {
Expand Down Expand Up @@ -72,7 +78,10 @@ func typedRealPart(fn foldFn, i *Integer, r *Rational, f *Flt, c *Complex) expre
return nil
}

func ComputeNumericPart(fn foldFn, e expreduceapi.ExpressionInterface) (expreduceapi.Ex, int) {
func ComputeNumericPart(
fn foldFn,
e expreduceapi.ExpressionInterface,
) (expreduceapi.Ex, int) {
var foldedInt *Integer
var foldedRat *Rational
var foldedFlt *Flt
Expand Down
8 changes: 6 additions & 2 deletions expreduce/atoms/ex_complex.go
Expand Up @@ -42,7 +42,8 @@ func (cmplx *Complex) IsEqual(other expreduceapi.Ex) string {
if !otherIsComplex {
return "EQUAL_FALSE"
}
if (cmplx.Re.IsEqual(otherConv.Re) != "EQUAL_TRUE") || (cmplx.Im.IsEqual(otherConv.Im) != "EQUAL_TRUE") {
if (cmplx.Re.IsEqual(otherConv.Re) != "EQUAL_TRUE") ||
(cmplx.Im.IsEqual(otherConv.Im) != "EQUAL_TRUE") {
return "EQUAL_FALSE"
}
return "EQUAL_TRUE"
Expand Down Expand Up @@ -127,7 +128,10 @@ func (cmplx *Complex) mulC(c *Complex) {
// Perhaps create "Calculator" utility??
// TODO(corywalker) Remove the definition that cmplx implements in code.
a, _ := ComputeNumericPart(FoldFnMul, E(S("Dummy"), cmplx.Re, c.Re))
b, _ := ComputeNumericPart(FoldFnMul, E(S("Dummy"), NewInt(-1), cmplx.Im, c.Im))
b, _ := ComputeNumericPart(
FoldFnMul,
E(S("Dummy"), NewInt(-1), cmplx.Im, c.Im),
)
cc, _ := ComputeNumericPart(FoldFnMul, E(S("Dummy"), cmplx.Re, c.Im))
d, _ := ComputeNumericPart(FoldFnMul, E(S("Dummy"), cmplx.Im, c.Re))
e, _ := ComputeNumericPart(FoldFnAdd, E(S("Dummy"), a, b))
Expand Down
28 changes: 19 additions & 9 deletions expreduce/atoms/ex_expression.go
Expand Up @@ -32,7 +32,11 @@ func HeadAssertion(ex expreduceapi.Ex, head string) (*Expression, bool) {
return nil, false
}

func HeadExAssertion(ex expreduceapi.Ex, head expreduceapi.Ex, cl expreduceapi.LoggingInterface) (*Expression, bool) {
func HeadExAssertion(
ex expreduceapi.Ex,
head expreduceapi.Ex,
cl expreduceapi.LoggingInterface,
) (*Expression, bool) {
expr, isExpr := ex.(*Expression)
if isExpr {
if IsSameQ(head, expr.GetParts()[0]) {
Expand All @@ -42,7 +46,10 @@ func HeadExAssertion(ex expreduceapi.Ex, head expreduceapi.Ex, cl expreduceapi.L
return nil, false
}

func OperatorAssertion(ex expreduceapi.Ex, opHead string) (*Expression, *Expression, bool) {
func OperatorAssertion(
ex expreduceapi.Ex,
opHead string,
) (*Expression, *Expression, bool) {
expr, isExpr := ex.(*Expression)
if isExpr {
headExpr, headIsExpr := expr.GetParts()[0].(*Expression)
Expand Down Expand Up @@ -86,7 +93,9 @@ func (thisExpr *Expression) PropagateConditionals() (*Expression, bool) {
return thisExpr, false
}

func (thisExpr *Expression) StringForm(params expreduceapi.ToStringParams) string {
func (thisExpr *Expression) StringForm(
params expreduceapi.ToStringParams,
) string {
headAsSym, isHeadSym := thisExpr.GetParts()[0].(*Symbol)
fullForm := false
if isHeadSym && !fullForm && params.Esi != nil {
Expand All @@ -101,12 +110,13 @@ func (thisExpr *Expression) StringForm(params expreduceapi.ToStringParams) strin
}
}

if len(thisExpr.GetParts()) == 2 && isHeadSym && (headAsSym.Name == "System`InputForm" ||
headAsSym.Name == "System`FullForm" ||
headAsSym.Name == "System`TraditionalForm" ||
headAsSym.Name == "System`TeXForm" ||
headAsSym.Name == "System`StandardForm" ||
headAsSym.Name == "System`OutputForm") {
if len(thisExpr.GetParts()) == 2 && isHeadSym &&
(headAsSym.Name == "System`InputForm" ||
headAsSym.Name == "System`FullForm" ||
headAsSym.Name == "System`TraditionalForm" ||
headAsSym.Name == "System`TeXForm" ||
headAsSym.Name == "System`StandardForm" ||
headAsSym.Name == "System`OutputForm") {
mutatedParams := params
mutatedParams.Form = headAsSym.Name[7:]
return thisExpr.GetParts()[1].StringForm(mutatedParams)
Expand Down
13 changes: 10 additions & 3 deletions expreduce/atoms/ex_rational.go
Expand Up @@ -15,9 +15,15 @@ type Rational struct {
needsEval bool
}

func (thisRational *Rational) StringForm(params expreduceapi.ToStringParams) string {
func (thisRational *Rational) StringForm(
params expreduceapi.ToStringParams,
) string {
if params.Form == "FullForm" {
return fmt.Sprintf("Rational[%d, %d]", thisRational.Num, thisRational.Den)
return fmt.Sprintf(
"Rational[%d, %d]",
thisRational.Num,
thisRational.Den,
)
}
if params.Form == "TeXForm" {
return fmt.Sprintf("\\frac{%d}{%d}", thisRational.Num, thisRational.Den)
Expand All @@ -34,7 +40,8 @@ func (thisRational *Rational) IsEqual(other expreduceapi.Ex) string {
return "EQUAL_FALSE"
}
// Assume rational already simplified
if (thisRational.Num.Cmp(otherConv.Num) != 0) || (thisRational.Den.Cmp(otherConv.Den) != 0) {
if (thisRational.Num.Cmp(otherConv.Num) != 0) ||
(thisRational.Den.Cmp(otherConv.Den) != 0) {
return "EQUAL_FALSE"
}
return "EQUAL_TRUE"
Expand Down
8 changes: 6 additions & 2 deletions expreduce/atoms/ex_symbol.go
Expand Up @@ -89,7 +89,9 @@ func (sym *Symbol) Copy() expreduceapi.Ex {
return sym
}

func (sym *Symbol) Attrs(dm expreduceapi.DefinitionMap) expreduceapi.Attributes {
func (sym *Symbol) Attrs(
dm expreduceapi.DefinitionMap,
) expreduceapi.Attributes {
def, isDef := dm.Get(sym.Name)
if !isDef {
return expreduceapi.Attributes{}
Expand Down Expand Up @@ -233,7 +235,9 @@ func AttrsToStrings(sym *expreduceapi.Attributes) []string {
return strings
}

func AttrsToSymList(sym *expreduceapi.Attributes) expreduceapi.ExpressionInterface {
func AttrsToSymList(
sym *expreduceapi.Attributes,
) expreduceapi.ExpressionInterface {
toReturn := E(S("List"))
for _, s := range AttrsToStrings(sym) {
toReturn.AppendEx(S(s))
Expand Down
4 changes: 3 additions & 1 deletion expreduce/atoms/string_utils.go
Expand Up @@ -4,7 +4,9 @@ import "github.com/corywalker/expreduce/pkg/expreduceapi"

type fakeEvalState struct{}

func (fes fakeEvalState) GetStringFn(headStr string) (expreduceapi.ToStringFnType, bool) {
func (fes fakeEvalState) GetStringFn(
headStr string,
) (expreduceapi.ToStringFnType, bool) {
return nil, false
}

Expand Down
23 changes: 18 additions & 5 deletions expreduce/builtin.go
Expand Up @@ -106,7 +106,9 @@ func toTestInstructions(tc expreduceapi.ExpressionInterface) []TestInstruction {
return instructions
}

func (def *Definition) annotateWithDynamicTests(es expreduceapi.EvalStateInterface) {
func (def *Definition) annotateWithDynamicTests(
es expreduceapi.EvalStateInterface,
) {
tests, testsDef := es.GetSymDef("Tests`" + def.Name)
if !testsDef {
return
Expand Down Expand Up @@ -150,7 +152,9 @@ func (def *Definition) annotateWithDynamicTests(es expreduceapi.EvalStateInterfa
}
}

func (def *Definition) annotateWithDynamicUsage(es expreduceapi.EvalStateInterface) {
func (def *Definition) annotateWithDynamicUsage(
es expreduceapi.EvalStateInterface,
) {
if len(def.Usage) > 0 {
return
}
Expand Down Expand Up @@ -187,13 +191,19 @@ type NamedDefSet struct {
// GetAllDefinitions returns a list of all builtin functions with metadata. The
// function returns a list organized by category.
func GetAllDefinitions() (defs []NamedDefSet) {
defs = append(defs, NamedDefSet{"combinatorics", getCombinatoricsDefinitions()})
defs = append(
defs,
NamedDefSet{"combinatorics", getCombinatoricsDefinitions()},
)
defs = append(defs, NamedDefSet{"calculus", getCalculusDefinitions()})
defs = append(defs, NamedDefSet{"comparison", getComparisonDefinitions()})
defs = append(defs, NamedDefSet{"atoms", getAtomsDefinitions()})
defs = append(defs, NamedDefSet{"functional", getFunctionalDefinitions()})
defs = append(defs, NamedDefSet{"expression", getExpressionDefinitions()})
defs = append(defs, NamedDefSet{"equationdata", getEquationDataDefinitions()})
defs = append(
defs,
NamedDefSet{"equationdata", getEquationDataDefinitions()},
)
defs = append(defs, NamedDefSet{"solve", getSolveDefinitions()})
defs = append(defs, NamedDefSet{"flowcontrol", getFlowControlDefinitions()})
defs = append(defs, NamedDefSet{"list", getListDefinitions()})
Expand All @@ -212,7 +222,10 @@ func GetAllDefinitions() (defs []NamedDefSet) {
defs = append(defs, NamedDefSet{"pattern", getPatternDefinitions()})
defs = append(defs, NamedDefSet{"boolean", getBooleanDefinitions()})
defs = append(defs, NamedDefSet{"simplify", getSimplifyDefinitions()})
defs = append(defs, NamedDefSet{"numbertheory", getNumberTheoryDefinitions()})
defs = append(
defs,
NamedDefSet{"numbertheory", getNumberTheoryDefinitions()},
)
defs = append(defs, NamedDefSet{"stats", getStatsDefinitions()})
defs = append(defs, NamedDefSet{"manip", getManipDefinitions()})
defs = append(defs, NamedDefSet{"rubi", getRubiDefinitions()})
Expand Down

0 comments on commit 190864c

Please sign in to comment.