Skip to content

Commit

Permalink
Improve stringification of negatives.
Browse files Browse the repository at this point in the history
  • Loading branch information
corywalker committed Nov 26, 2018
1 parent da73d3a commit a9c7f19
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 17 deletions.
9 changes: 0 additions & 9 deletions expreduce/atoms/ex_integer.go
Expand Up @@ -5,7 +5,6 @@ import (
"hash/fnv"
"math/big"

"github.com/corywalker/expreduce/expreduce/parser/parens"
"github.com/corywalker/expreduce/pkg/expreduceapi"
)

Expand All @@ -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)
}

Expand Down
57 changes: 51 additions & 6 deletions 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"
)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
},
Expand Down
4 changes: 2 additions & 2 deletions expreduce/resources.go
Git LFS file not shown
3 changes: 3 additions & 0 deletions expreduce/resources/arithmetic.m
Expand Up @@ -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)],
Expand Down

0 comments on commit a9c7f19

Please sign in to comment.