3
3
4
4
5
5
import itertools
6
+ from typing import Optional , Union
6
7
7
8
import sympy
8
9
26
27
)
27
28
from mathics .core .numbers import dps
28
29
30
+ def cmp (a , b ) -> int :
31
+ "Returns 0 if a == b, -1 if a < b and 1 if a > b"
32
+ return (a > b ) - (a < b )
29
33
30
34
class SameQ (BinaryOperator ):
31
35
"""
@@ -208,7 +212,7 @@ def numerify_args(items, evaluation):
208
212
class _EqualityOperator (_InequalityOperator ):
209
213
"Compares all pairs e.g. a == b == c compares a == b, b == c, and a == c."
210
214
211
- def do_compare (self , l1 , l2 ):
215
+ def do_compare (self , l1 , l2 ) -> Union [ bool , None ] :
212
216
if l1 .same (l2 ):
213
217
return True
214
218
elif l1 == SymbolTrue and l2 == SymbolFalse :
@@ -254,7 +258,13 @@ def apply(self, items, evaluation):
254
258
args = self .numerify_args (items , evaluation )
255
259
wanted = operators [self .get_name ()]
256
260
for x , y in itertools .combinations (args , 2 ):
257
- c = do_cmp (x , y )
261
+ if isinstance (x , String ) or isinstance (y , String ):
262
+ if not (isinstance (x , String ) and isinstance (y , String )):
263
+ c = 1
264
+ else :
265
+ c = cmp (x .get_string_value (), y .get_string_value ())
266
+ else :
267
+ c = do_cmp (x , y )
258
268
if c is None :
259
269
return
260
270
elif c not in wanted :
@@ -345,7 +355,7 @@ def apply(self, items, evaluation):
345
355
return Expression ("And" , * groups )
346
356
347
357
348
- def do_cmp (x1 , x2 ):
358
+ def do_cmp (x1 , x2 ) -> Optional [ int ] :
349
359
350
360
# don't attempt to compare complex numbers
351
361
for x in (x1 , x2 ):
@@ -399,8 +409,8 @@ class Equal(_EqualityOperator, SympyComparison):
399
409
"""
400
410
<dl>
401
411
<dt>'Equal[$x$, $y$]'
402
- <dt>'$x$ == $y$'
403
- <dd>yields 'True' if $x$ and $y$ are known to be equal, or
412
+ <dt>'$x$ == $y$'
413
+ <dd>yields 'True' if $x$ and $y$ are known to be equal, or
404
414
'False' if $x$ and $y$ are known to be unequal.
405
415
<dt>'$lhs$ == $rhs$'
406
416
<dd>represents the equation $lhs$ = $rhs$.
@@ -413,6 +423,17 @@ class Equal(_EqualityOperator, SympyComparison):
413
423
>> 1==1.
414
424
= True
415
425
426
+ Strings are allowed:
427
+ Equal["11", "11"]
428
+ = True
429
+
430
+ Equal["121", "11"]
431
+ = False
432
+
433
+ Comparision to mismatched types is False:
434
+ Equal[11, "11"]
435
+ = False
436
+
416
437
Lists are compared based on their elements:
417
438
>> {{1}, {2}} == {{1}, {2}}
418
439
= True
@@ -500,6 +521,17 @@ class Unequal(_EqualityOperator, SympyComparison):
500
521
>> 1 != 1.
501
522
= False
502
523
524
+ Strings are allowed:
525
+ Unequal["11", "11"]
526
+ = False
527
+
528
+ Equal["121", "11"]
529
+ = True
530
+
531
+ Comparision to mismatched types is True:
532
+ Equal[11, "11"]
533
+ = True
534
+
503
535
Lists are compared based on their elements:
504
536
>> {1} != {2}
505
537
= True
0 commit comments