Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
go-version: [ '1.21.x', '1.20.x' ]
# Only test latest and minimum versions to save test compute.
go-version: [ '1.23.x', '1.20.x' ]

steps:
- uses: actions/checkout@v3
Expand Down
161 changes: 36 additions & 125 deletions expreduce/builtin_comparison.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,38 @@ func getCompSign(e expreduceapi.Ex) int {
return -2
}

func int64InSlice(target int64, list []int64) bool {
for _, v := range list {
if v == target {
return true
}
}
return false
}

func comparisonFn(expr expreduceapi.ExpressionInterface, es expreduceapi.EvalStateInterface, expectedOrders []int64) expreduceapi.Ex {
var lastN expreduceapi.Ex = nil
for i := 1; i < len(expr.GetParts()); i++ {
currN := es.Eval(
atoms.NewExpression(
[]expreduceapi.Ex{
atoms.NewSymbol("System`N"),
expr.GetParts()[i],
},
),
)
if !atoms.NumberQ(currN) {
return expr
}
if lastN != nil && !int64InSlice(atoms.ExOrder(lastN, currN), expectedOrders) {
return atoms.NewSymbol("System`False")
}

lastN = currN
}
return atoms.NewSymbol("System`True")
}

func getComparisonDefinitions() (defs []Definition) {
defs = append(defs, Definition{
Name: "Equal",
Expand Down Expand Up @@ -248,36 +280,7 @@ func getComparisonDefinitions() (defs []Definition) {
)
},
legacyEvalFn: func(this expreduceapi.ExpressionInterface, es expreduceapi.EvalStateInterface) expreduceapi.Ex {
if len(this.GetParts()) != 3 {
return this
}

a := es.Eval(
atoms.NewExpression(
[]expreduceapi.Ex{
atoms.NewSymbol("System`N"),
this.GetParts()[1],
},
),
)
b := es.Eval(
atoms.NewExpression(
[]expreduceapi.Ex{
atoms.NewSymbol("System`N"),
this.GetParts()[2],
},
),
)

if !atoms.NumberQ(a) || !atoms.NumberQ(b) {
return this
}

// Less
if atoms.ExOrder(a, b) == 1 {
return atoms.NewSymbol("System`True")
}
return atoms.NewSymbol("System`False")
return comparisonFn(this, es, []int64{1})
},
})
defs = append(defs, Definition{
Expand All @@ -294,35 +297,7 @@ func getComparisonDefinitions() (defs []Definition) {
)
},
legacyEvalFn: func(this expreduceapi.ExpressionInterface, es expreduceapi.EvalStateInterface) expreduceapi.Ex {
if len(this.GetParts()) != 3 {
return this
}

a := es.Eval(
atoms.NewExpression(
[]expreduceapi.Ex{
atoms.NewSymbol("System`N"),
this.GetParts()[1],
},
),
)
b := es.Eval(
atoms.NewExpression(
[]expreduceapi.Ex{
atoms.NewSymbol("System`N"),
this.GetParts()[2],
},
),
)

if !atoms.NumberQ(a) || !atoms.NumberQ(b) {
return this
}
// Greater
if atoms.ExOrder(a, b) == -1 {
return atoms.NewSymbol("System`True")
}
return atoms.NewSymbol("System`False")
return comparisonFn(this, es, []int64{-1})
},
})
defs = append(defs, Definition{
Expand All @@ -339,39 +314,7 @@ func getComparisonDefinitions() (defs []Definition) {
)
},
legacyEvalFn: func(this expreduceapi.ExpressionInterface, es expreduceapi.EvalStateInterface) expreduceapi.Ex {
if len(this.GetParts()) != 3 {
return this
}

a := es.Eval(
atoms.NewExpression(
[]expreduceapi.Ex{
atoms.NewSymbol("System`N"),
this.GetParts()[1],
},
),
)
b := es.Eval(
atoms.NewExpression(
[]expreduceapi.Ex{
atoms.NewSymbol("System`N"),
this.GetParts()[2],
},
),
)

if !atoms.NumberQ(a) || !atoms.NumberQ(b) {
return this
}
// Less
if atoms.ExOrder(a, b) == 1 {
return atoms.NewSymbol("System`True")
}
// Equal
if atoms.ExOrder(a, b) == 0 {
return atoms.NewSymbol("System`True")
}
return atoms.NewSymbol("System`False")
return comparisonFn(this, es, []int64{1, 0})
},
})
defs = append(defs, Definition{
Expand All @@ -388,39 +331,7 @@ func getComparisonDefinitions() (defs []Definition) {
)
},
legacyEvalFn: func(this expreduceapi.ExpressionInterface, es expreduceapi.EvalStateInterface) expreduceapi.Ex {
if len(this.GetParts()) != 3 {
return this
}

a := es.Eval(
atoms.NewExpression(
[]expreduceapi.Ex{
atoms.NewSymbol("System`N"),
this.GetParts()[1],
},
),
)
b := es.Eval(
atoms.NewExpression(
[]expreduceapi.Ex{
atoms.NewSymbol("System`N"),
this.GetParts()[2],
},
),
)

if !atoms.NumberQ(a) || !atoms.NumberQ(b) {
return this
}
// Greater
if atoms.ExOrder(a, b) == -1 {
return atoms.NewSymbol("System`True")
}
// Equal
if atoms.ExOrder(a, b) == 0 {
return atoms.NewSymbol("System`True")
}
return atoms.NewSymbol("System`False")
return comparisonFn(this, es, []int64{-1, 0})
},
})
defs = append(defs, Definition{
Expand Down
31 changes: 23 additions & 8 deletions expreduce/resources.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 20 additions & 5 deletions expreduce/resources/comparison.m
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,14 @@
ESameTest[True, 1 < 2],
ESameTest[True, 3 < 5.5],
ESameTest[False, 5.5 < 3],
ESameTest[False, 3 < 3]
]
ESameTest[False, 3 < 3],
ESameTest[True, 1 < 2 < 3],
], ETests[
ESameTest[True, Less[2]],
ESameTest[False, 1<-1<a],
], EKnownFailures[
ESameTest[False, 1<a<-1],
],
};

Greater::usage = "`a > b` returns True if `a` is greater than `b`.";
Expand All @@ -207,7 +213,10 @@
ESameTest[False, 1 > 2],
ESameTest[False, 3 > 5.5],
ESameTest[True, 5.5 > 3],
ESameTest[False, 3 > 3]
ESameTest[False, 3 > 3],
ESameTest[True, 3 > 2 > 1],
], ETests[
ESameTest[True, Greater[2]],
]
};

Expand All @@ -223,7 +232,10 @@
ESameTest[True, 1 <= 2],
ESameTest[True, 3 <= 5.5],
ESameTest[False, 5.5 <= 3],
ESameTest[True, 3 <= 3]
ESameTest[True, 3 <= 3],
ESameTest[True, 1 <= 2 <= 3],
], ETests[
ESameTest[True, LessEqual[2]],
]
};

Expand All @@ -235,7 +247,10 @@
ESameTest[False, 1 >= 2],
ESameTest[False, 3 >= 5.5],
ESameTest[True, 5.5 >= 3],
ESameTest[True, 3 >= 3]
ESameTest[True, 3 >= 3],
ESameTest[True, 3 >= 2 >= 2 >= 1],
], ETests[
ESameTest[True, GreaterEqual[2]],
]
};

Expand Down
6 changes: 3 additions & 3 deletions expreduce/resources/trig.m
Original file line number Diff line number Diff line change
Expand Up @@ -89,18 +89,18 @@

TrigExpand[Cos[2*a_]] := Cos[a]^2-Sin[a]^2;
TrigExpand[Cos[a_]] := Cos[a];
TrigExpand[a_] := (Print["Unsupported call to TrigExpand", a];a);
TrigExpand[a_] := (Print["Unsupported call to TrigExpand[", a, "]"];a);
Attributes[TrigExpand] = {Protected};

TrigReduce[a_] := (Print["Unsupported call to TrigReduce", a];a);
TrigReduce[a_] := (Print["Unsupported call to TrigReduce[", a, "]"];a);
Attributes[TrigReduce] = {Protected};

trigToExpInner[n_Integer] := n;
trigToExpInner[sym_Symbol] := sym;
trigToExpInner[Cos[inner_]] := E^(-I inner//Expand)/2+E^(I inner//Expand)/2;
trigToExpInner[Sin[inner_]] := 1/2 I E^(-I inner//Expand)-1/2 I E^(I inner//Expand);
trigToExpInner[Tan[inner_]] := (I (E^(-I inner)-E^(I inner)))/(E^(-I inner)+E^(I inner));
trigToExpInner[a_] := (Print["Unsupported call to TrigToExp", a];a);
trigToExpInner[a_] := (Print["Unsupported call to TrigToExp[", a, "]"];a);
TrigToExp[exp_] := Map[trigToExpInner, exp, {0, Infinity}]//Expand;
Attributes[TrigToExp] = {Listable, Protected};
Tests`TrigToExp = {
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/kavehmz/prime v1.0.0
github.com/op/go-logging v0.0.0-20160315200505-970db520ece7
github.com/orcaman/concurrent-map v1.0.0
github.com/stretchr/testify v1.8.4
github.com/stretchr/testify v1.9.0
github.com/wcharczuk/go-chart v2.0.1+incompatible
gopkg.in/readline.v1 v1.0.0-20160726135117-62c6fe619375
modernc.org/wl v1.0.0
Expand All @@ -20,8 +20,8 @@ require (
github.com/go-bindata/go-bindata v3.1.2+incompatible // indirect
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/image v0.11.0 // indirect
golang.org/x/image v0.22.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
modernc.org/golex v1.0.5 // indirect
modernc.org/golex v1.1.0 // indirect
modernc.org/strutil v1.2.0 // indirect
)
Loading