-
-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathtest_evaluators.py
119 lines (105 loc) · 4.56 KB
/
test_evaluators.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# -*- coding: utf-8 -*-
import pytest
from mathics.eval.nevaluator import eval_N, eval_NValues
from mathics.eval.numerify import numerify as eval_numerify
from .helper import session
evaluation = session.evaluation
def numerify(expr, evaluation):
return eval_numerify(expr, evaluation)
def test_sameQ():
a = session.evaluate("1.")
b = session.evaluate("1.`30")
c = session.evaluate("1.`10")
assert a.sameQ(b)
assert a.sameQ(c)
assert b.sameQ(c)
assert b.sameQ(a)
assert c.sameQ(a)
assert c.sameQ(b)
@pytest.mark.parametrize(
"str_expr, prec, str_expected",
[
("1", "$MachinePrecision", "1.000000000"),
("Sqrt[2]", "$MachinePrecision", "1.41421356237310"),
("2/9", "$MachinePrecision", "2.000000`5*9.0000000000`5^(-1.`)"),
("Pi", "$MachinePrecision", "3.141592653589793`15"),
("F[1.2, 2/9]", "$MachinePrecision", "F[1.2, .2222222222222222]"),
("F[1.2, 2/9]", "$MachinePrecision", "F[1.2, .2222222222222222`15]"),
# It is necessary to have 17 digits to get SameQ against N[2/9,5]
# ("F[1.2`3, 2/9]", "5", "F[1.20`3, .22222232818603515`15]"),
("F[1.2`3, 2/9]", "5", "F[1.20`3, .222222`5]"),
("F[1.2`3, 2/9]", "5", "F[1.20`3, .222222]"),
("a=1.2`3;F[a, 2/9]", "5", "F[1.20`3, .222222]"),
# In WMA, N[b]=1.2`3 should assign a NValue rule to N[b, $MachinePrecision]
("N[b]=1.2`3;F[b, 2/9]", "5", "F[b, .222222]"),
("N[b,_]=1.2`3;F[b, 2/9]", "5", "F[1.20`3, .222222]"),
],
)
def test_eval_N(str_expr, prec, str_expected):
expr_in = session.evaluate(f"Hold[{str_expr}]").elements[0]
prec = session.evaluate(prec)
expr_expected = session.evaluate(str_expected)
result = eval_N(expr_in, evaluation, prec=prec)
assert expr_expected.sameQ(result)
@pytest.mark.parametrize(
"str_expr, prec, str_expected, setup",
[
("1", "$MachinePrecision", "1.000000000", None),
# eval_NValues does not call `evaluate` over the input expression. So
# 2/9 is not evaluated to a Rational number, but kept as a division.
("2/9", "$MachinePrecision", "2.000000`5*9.0000000000`5^(-1.`)", None),
# eval_NValues does not call `evaluate` at the end neither. So
# Sqrt[2]->Sqrt[2.0`]
("Sqrt[2]", "$MachinePrecision", "Sqrt[2.0`]", None),
("Pi", "$MachinePrecision", "3.141592653589793`15", None),
(
"F[1.2, 2/9]",
"$MachinePrecision",
"F[1.2, 2.000000`5*9.0000000000`5^(-1.`)]",
None,
),
# Here 2/9 -> 2./9.
("F[1.2`3, 2/9]", "5", "F[1.20`3, 2.`5*9.`5^(-1.`)]", None),
# Here, since the input expression is not evaluated, `a` remains unevaluated.
("F[a, 2/9]", "5", "F[a, 2.000000`5*9.0000000000`5^(-1.`)]", "a=1.2`3;"),
("F[b, 2/9]", "5", "F[1.20`3, 2.*9.^(-1.`)]", "N[b,_]=1.2`3"),
],
)
def test_eval_NValues(str_expr, prec, str_expected, setup):
if setup:
session.evaluate(setup)
expr_in = session.evaluate(f"Hold[{str_expr}]").elements[0]
prec = session.evaluate(prec)
expr_expected = session.evaluate(f"Hold[{str_expected}]").elements[0]
result = eval_NValues(expr_in, prec, evaluation)
session.evaluate("ClearAll[a,b,c]")
assert expr_expected.sameQ(result)
@pytest.mark.parametrize(
"str_expr, str_expected, setup",
[
("1", "1", None),
("{1, 1.}", "{1, 1.}", None),
("{1.000123`6, 1.0001`4, 2/9}", "{1.000123`6, 1.0001`4, .22222`4}", None),
("F[1.000123`6, 1.0001`4, 2/9]", "F[1.000123`6, 1.0001`4, .22222`4]", None),
# eval_NValues does not call `evaluate` over the input expression. So
# 2/9 is not evaluated to a Rational number, but kept as a division.
("2/9", "2 * 9 ^ (-1)", None),
("Sqrt[2]", "Sqrt[2]", None),
("Pi", "Pi", None),
("F[1.3, 2/9]", "F[1.3, 0.2222222222222222]", None),
# Here 2/9 -> .22222232818603515`15
("F[1.2`3, 2/9]", "F[1.20`3, .2222`3]", None),
# Here, since the input expression is not evaluated, `a` remains unevaluated.
("F[a, 2/9]", "F[a, 2 *9 ^ (-1)]", "a=1.2`3;"),
# Here b is not evaluated
("F[b, 2/9]", "F[b, 2 *9 ^ (-1)]", "N[b,_]=1.2`3"),
],
)
def test_numerify(str_expr, str_expected, setup):
if setup:
session.evaluate(setup)
expr_in = session.evaluate(f"Hold[{str_expr}]").elements[0]
expr_expected = session.evaluate(f"Hold[{str_expected}]").elements[0]
result = numerify(expr_in, evaluation)
session.evaluate("ClearAll[a,b,c]")
assert expr_expected.sameQ(result)