diff --git a/expreduce/atoms/ex_integer.go b/expreduce/atoms/ex_integer.go index dcf0323..dbf0196 100644 --- a/expreduce/atoms/ex_integer.go +++ b/expreduce/atoms/ex_integer.go @@ -5,7 +5,6 @@ import ( "hash/fnv" "math/big" - "github.com/corywalker/expreduce/expreduce/parser/parens" "github.com/corywalker/expreduce/pkg/expreduceapi" ) @@ -20,14 +19,6 @@ type Integer struct { }*/ func (thisInt *Integer) StringForm(params expreduceapi.ToStringParams) string { - if thisInt.Val.Cmp(big.NewInt(0)) < 0 { - if parens.NeedsParens("System`Times", params.PreviousHead) { - if params.Form == "TeXForm" { - return fmt.Sprintf("{(%d)}", thisInt.Val) - } - return fmt.Sprintf("(%d)", thisInt.Val) - } - } return fmt.Sprintf("%d", thisInt.Val) } diff --git a/expreduce/builtin_arithmetic.go b/expreduce/builtin_arithmetic.go index c64f420..493ee80 100644 --- a/expreduce/builtin_arithmetic.go +++ b/expreduce/builtin_arithmetic.go @@ -1,12 +1,14 @@ package expreduce import ( + "bytes" "fmt" "math/big" "strings" "github.com/corywalker/expreduce/expreduce/atoms" "github.com/corywalker/expreduce/expreduce/iterspec" + "github.com/corywalker/expreduce/expreduce/parser/parens" "github.com/corywalker/expreduce/pkg/expreduceapi" ) @@ -142,8 +144,46 @@ func getArithmeticDefinitions() (defs []Definition) { defs = append(defs, Definition{ Name: "Plus", Default: "0", - toString: func(this expreduceapi.ExpressionInterface, params expreduceapi.ToStringParams) (bool, string) { - return toStringInfix(this.GetParts()[1:], " + ", "System`Plus", params) + toString: func(this expreduceapi.ExpressionInterface, p expreduceapi.ToStringParams) (bool, string) { + thisHead := "System`Plus" + parts := this.GetParts()[1:] + if p.Form != "InputForm" && p.Form != "OutputForm" && p.Form != "TeXForm" { + return false, "" + } + if len(parts) < 2 { + return false, "" + } + addParens := parens.NeedsParens(thisHead, p.PreviousHead) + var buffer bytes.Buffer + if addParens { + if p.Form == "TeXForm" { + buffer.WriteString("{\\left(") + } else { + buffer.WriteString("(") + } + } + nextParams := p + nextParams.PreviousHead = thisHead + for i := 0; i < len(parts); i++ { + toWrite := parts[i].StringForm(nextParams) + if i != 0 { + if toWrite[0] == '-' { + buffer.WriteString(" - ") + toWrite = toWrite[1:] + } else { + buffer.WriteString(" + ") + } + } + buffer.WriteString(toWrite) + } + if addParens { + if p.Form == "TeXForm" { + buffer.WriteString("\\right)}") + } else { + buffer.WriteString(")") + } + } + return true, buffer.String() }, legacyEvalFn: func(this expreduceapi.ExpressionInterface, es expreduceapi.EvalStateInterface) expreduceapi.Ex { // Calls without argument receive identity values @@ -239,17 +279,22 @@ func getArithmeticDefinitions() (defs []Definition) { if !numOk || !denOk { return false, "" } + prefix := "" + if strings.HasPrefix(numStr, "-1"+delim) { + prefix = "-" + numStr = numStr[3:] + } if params.Form == "TeXForm" { - return true, fmt.Sprintf("\\frac{%v}{%v}", numStr, denStr) + return true, fmt.Sprintf("%v\\frac{%v}{%v}", prefix, numStr, denStr) } - return true, fmt.Sprintf("(%v)/(%v)", numStr, denStr) + return true, fmt.Sprintf("%v(%v)/(%v)", prefix, numStr, denStr) } ok, res := toStringInfix(num.GetParts()[1:], delim, "System`Times", params) if !ok { return false, "" } - if strings.HasPrefix(res, "(-1)"+delim) { - return true, "-" + res[5:] + if strings.HasPrefix(res, "-1"+delim) { + return true, "-" + res[3:] } return true, res }, diff --git a/expreduce/resources.go b/expreduce/resources.go index d4f4c02..5f38d06 100644 --- a/expreduce/resources.go +++ b/expreduce/resources.go @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:de8042936af3daea88a4f4af8ffb88dc239b1f617d391ee29ddb3ec8c6c3cbef -size 1340326 +oid sha256:c49043712e79f86317a58233aa619034ef3f1074c1e199339acd301d72b07957 +size 1340442 diff --git a/expreduce/resources/arithmetic.m b/expreduce/resources/arithmetic.m index f5f68ed..390c75f 100644 --- a/expreduce/resources/arithmetic.m +++ b/expreduce/resources/arithmetic.m @@ -213,6 +213,9 @@ ESameTest[I/(2 Sqrt[3] a^2), (0+1/6*I)*3^(1/2)*a^(-2)], (* Test wouldntBeLessThanNegOne. *) ESameTest[(1/3)*3^(-1/2), (1/3)*3^(-1/2)], + + ESameTest[Sqrt[2/\[Pi]], Sqrt[2]*Sqrt[1/Pi]], + ESameTest[Sqrt[3/(2 \[Pi])], Sqrt[3/2]*Sqrt[1/Pi]], ], EKnownFailures[ ESameTest[-2^(1/3), (-2)*2^(-2/3)], ESameTest[-2^(1+a), (-2)*2^(a)],