Skip to content

Commit 868399e

Browse files
authored
Merge pull request #205 from corywalker/corywalker
Support some forms of compound inequalities and clean up the logic.
2 parents 44f8c58 + 9d5b8d3 commit 868399e

File tree

7 files changed

+93
-145
lines changed

7 files changed

+93
-145
lines changed

.github/workflows/go.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ jobs:
1515
runs-on: ubuntu-latest
1616
strategy:
1717
matrix:
18-
go-version: [ '1.21.x', '1.20.x' ]
18+
# Only test latest and minimum versions to save test compute.
19+
go-version: [ '1.23.x', '1.20.x' ]
1920

2021
steps:
2122
- uses: actions/checkout@v3

expreduce/builtin_comparison.go

+36-125
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,38 @@ func getCompSign(e expreduceapi.Ex) int {
8888
return -2
8989
}
9090

91+
func int64InSlice(target int64, list []int64) bool {
92+
for _, v := range list {
93+
if v == target {
94+
return true
95+
}
96+
}
97+
return false
98+
}
99+
100+
func comparisonFn(expr expreduceapi.ExpressionInterface, es expreduceapi.EvalStateInterface, expectedOrders []int64) expreduceapi.Ex {
101+
var lastN expreduceapi.Ex = nil
102+
for i := 1; i < len(expr.GetParts()); i++ {
103+
currN := es.Eval(
104+
atoms.NewExpression(
105+
[]expreduceapi.Ex{
106+
atoms.NewSymbol("System`N"),
107+
expr.GetParts()[i],
108+
},
109+
),
110+
)
111+
if !atoms.NumberQ(currN) {
112+
return expr
113+
}
114+
if lastN != nil && !int64InSlice(atoms.ExOrder(lastN, currN), expectedOrders) {
115+
return atoms.NewSymbol("System`False")
116+
}
117+
118+
lastN = currN
119+
}
120+
return atoms.NewSymbol("System`True")
121+
}
122+
91123
func getComparisonDefinitions() (defs []Definition) {
92124
defs = append(defs, Definition{
93125
Name: "Equal",
@@ -248,36 +280,7 @@ func getComparisonDefinitions() (defs []Definition) {
248280
)
249281
},
250282
legacyEvalFn: func(this expreduceapi.ExpressionInterface, es expreduceapi.EvalStateInterface) expreduceapi.Ex {
251-
if len(this.GetParts()) != 3 {
252-
return this
253-
}
254-
255-
a := es.Eval(
256-
atoms.NewExpression(
257-
[]expreduceapi.Ex{
258-
atoms.NewSymbol("System`N"),
259-
this.GetParts()[1],
260-
},
261-
),
262-
)
263-
b := es.Eval(
264-
atoms.NewExpression(
265-
[]expreduceapi.Ex{
266-
atoms.NewSymbol("System`N"),
267-
this.GetParts()[2],
268-
},
269-
),
270-
)
271-
272-
if !atoms.NumberQ(a) || !atoms.NumberQ(b) {
273-
return this
274-
}
275-
276-
// Less
277-
if atoms.ExOrder(a, b) == 1 {
278-
return atoms.NewSymbol("System`True")
279-
}
280-
return atoms.NewSymbol("System`False")
283+
return comparisonFn(this, es, []int64{1})
281284
},
282285
})
283286
defs = append(defs, Definition{
@@ -294,35 +297,7 @@ func getComparisonDefinitions() (defs []Definition) {
294297
)
295298
},
296299
legacyEvalFn: func(this expreduceapi.ExpressionInterface, es expreduceapi.EvalStateInterface) expreduceapi.Ex {
297-
if len(this.GetParts()) != 3 {
298-
return this
299-
}
300-
301-
a := es.Eval(
302-
atoms.NewExpression(
303-
[]expreduceapi.Ex{
304-
atoms.NewSymbol("System`N"),
305-
this.GetParts()[1],
306-
},
307-
),
308-
)
309-
b := es.Eval(
310-
atoms.NewExpression(
311-
[]expreduceapi.Ex{
312-
atoms.NewSymbol("System`N"),
313-
this.GetParts()[2],
314-
},
315-
),
316-
)
317-
318-
if !atoms.NumberQ(a) || !atoms.NumberQ(b) {
319-
return this
320-
}
321-
// Greater
322-
if atoms.ExOrder(a, b) == -1 {
323-
return atoms.NewSymbol("System`True")
324-
}
325-
return atoms.NewSymbol("System`False")
300+
return comparisonFn(this, es, []int64{-1})
326301
},
327302
})
328303
defs = append(defs, Definition{
@@ -339,39 +314,7 @@ func getComparisonDefinitions() (defs []Definition) {
339314
)
340315
},
341316
legacyEvalFn: func(this expreduceapi.ExpressionInterface, es expreduceapi.EvalStateInterface) expreduceapi.Ex {
342-
if len(this.GetParts()) != 3 {
343-
return this
344-
}
345-
346-
a := es.Eval(
347-
atoms.NewExpression(
348-
[]expreduceapi.Ex{
349-
atoms.NewSymbol("System`N"),
350-
this.GetParts()[1],
351-
},
352-
),
353-
)
354-
b := es.Eval(
355-
atoms.NewExpression(
356-
[]expreduceapi.Ex{
357-
atoms.NewSymbol("System`N"),
358-
this.GetParts()[2],
359-
},
360-
),
361-
)
362-
363-
if !atoms.NumberQ(a) || !atoms.NumberQ(b) {
364-
return this
365-
}
366-
// Less
367-
if atoms.ExOrder(a, b) == 1 {
368-
return atoms.NewSymbol("System`True")
369-
}
370-
// Equal
371-
if atoms.ExOrder(a, b) == 0 {
372-
return atoms.NewSymbol("System`True")
373-
}
374-
return atoms.NewSymbol("System`False")
317+
return comparisonFn(this, es, []int64{1, 0})
375318
},
376319
})
377320
defs = append(defs, Definition{
@@ -388,39 +331,7 @@ func getComparisonDefinitions() (defs []Definition) {
388331
)
389332
},
390333
legacyEvalFn: func(this expreduceapi.ExpressionInterface, es expreduceapi.EvalStateInterface) expreduceapi.Ex {
391-
if len(this.GetParts()) != 3 {
392-
return this
393-
}
394-
395-
a := es.Eval(
396-
atoms.NewExpression(
397-
[]expreduceapi.Ex{
398-
atoms.NewSymbol("System`N"),
399-
this.GetParts()[1],
400-
},
401-
),
402-
)
403-
b := es.Eval(
404-
atoms.NewExpression(
405-
[]expreduceapi.Ex{
406-
atoms.NewSymbol("System`N"),
407-
this.GetParts()[2],
408-
},
409-
),
410-
)
411-
412-
if !atoms.NumberQ(a) || !atoms.NumberQ(b) {
413-
return this
414-
}
415-
// Greater
416-
if atoms.ExOrder(a, b) == -1 {
417-
return atoms.NewSymbol("System`True")
418-
}
419-
// Equal
420-
if atoms.ExOrder(a, b) == 0 {
421-
return atoms.NewSymbol("System`True")
422-
}
423-
return atoms.NewSymbol("System`False")
334+
return comparisonFn(this, es, []int64{-1, 0})
424335
},
425336
})
426337
defs = append(defs, Definition{

expreduce/resources.go

+23-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

expreduce/resources/comparison.m

+20-5
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,14 @@
195195
ESameTest[True, 1 < 2],
196196
ESameTest[True, 3 < 5.5],
197197
ESameTest[False, 5.5 < 3],
198-
ESameTest[False, 3 < 3]
199-
]
198+
ESameTest[False, 3 < 3],
199+
ESameTest[True, 1 < 2 < 3],
200+
], ETests[
201+
ESameTest[True, Less[2]],
202+
ESameTest[False, 1<-1<a],
203+
], EKnownFailures[
204+
ESameTest[False, 1<a<-1],
205+
],
200206
};
201207

202208
Greater::usage = "`a > b` returns True if `a` is greater than `b`.";
@@ -207,7 +213,10 @@
207213
ESameTest[False, 1 > 2],
208214
ESameTest[False, 3 > 5.5],
209215
ESameTest[True, 5.5 > 3],
210-
ESameTest[False, 3 > 3]
216+
ESameTest[False, 3 > 3],
217+
ESameTest[True, 3 > 2 > 1],
218+
], ETests[
219+
ESameTest[True, Greater[2]],
211220
]
212221
};
213222

@@ -223,7 +232,10 @@
223232
ESameTest[True, 1 <= 2],
224233
ESameTest[True, 3 <= 5.5],
225234
ESameTest[False, 5.5 <= 3],
226-
ESameTest[True, 3 <= 3]
235+
ESameTest[True, 3 <= 3],
236+
ESameTest[True, 1 <= 2 <= 3],
237+
], ETests[
238+
ESameTest[True, LessEqual[2]],
227239
]
228240
};
229241

@@ -235,7 +247,10 @@
235247
ESameTest[False, 1 >= 2],
236248
ESameTest[False, 3 >= 5.5],
237249
ESameTest[True, 5.5 >= 3],
238-
ESameTest[True, 3 >= 3]
250+
ESameTest[True, 3 >= 3],
251+
ESameTest[True, 3 >= 2 >= 2 >= 1],
252+
], ETests[
253+
ESameTest[True, GreaterEqual[2]],
239254
]
240255
};
241256

expreduce/resources/trig.m

+3-3
Original file line numberDiff line numberDiff line change
@@ -89,18 +89,18 @@
8989

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

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

9898
trigToExpInner[n_Integer] := n;
9999
trigToExpInner[sym_Symbol] := sym;
100100
trigToExpInner[Cos[inner_]] := E^(-I inner//Expand)/2+E^(I inner//Expand)/2;
101101
trigToExpInner[Sin[inner_]] := 1/2 I E^(-I inner//Expand)-1/2 I E^(I inner//Expand);
102102
trigToExpInner[Tan[inner_]] := (I (E^(-I inner)-E^(I inner)))/(E^(-I inner)+E^(I inner));
103-
trigToExpInner[a_] := (Print["Unsupported call to TrigToExp", a];a);
103+
trigToExpInner[a_] := (Print["Unsupported call to TrigToExp[", a, "]"];a);
104104
TrigToExp[exp_] := Map[trigToExpInner, exp, {0, Infinity}]//Expand;
105105
Attributes[TrigToExp] = {Listable, Protected};
106106
Tests`TrigToExp = {

go.mod

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ require (
77
github.com/kavehmz/prime v1.0.0
88
github.com/op/go-logging v0.0.0-20160315200505-970db520ece7
99
github.com/orcaman/concurrent-map v1.0.0
10-
github.com/stretchr/testify v1.8.4
10+
github.com/stretchr/testify v1.9.0
1111
github.com/wcharczuk/go-chart v2.0.1+incompatible
1212
gopkg.in/readline.v1 v1.0.0-20160726135117-62c6fe619375
1313
modernc.org/wl v1.0.0
@@ -20,8 +20,8 @@ require (
2020
github.com/go-bindata/go-bindata v3.1.2+incompatible // indirect
2121
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
2222
github.com/pmezard/go-difflib v1.0.0 // indirect
23-
golang.org/x/image v0.11.0 // indirect
23+
golang.org/x/image v0.22.0 // indirect
2424
gopkg.in/yaml.v3 v3.0.1 // indirect
25-
modernc.org/golex v1.0.5 // indirect
25+
modernc.org/golex v1.1.0 // indirect
2626
modernc.org/strutil v1.2.0 // indirect
2727
)

0 commit comments

Comments
 (0)