-
-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathtest_numericq.py
174 lines (167 loc) · 4.34 KB
/
test_numericq.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# -*- coding: utf-8 -*-
import pytest
from .helper import check_evaluation, session
@pytest.mark.parametrize(
(
"str_expr",
"str_expected",
),
[ # Numbers are numeric:
("2", "True"),
("3+2 I", "True"),
("2/9", "True"),
# but Strings, not
(""" "hi!" """, "False"),
# Infinity is not numeric:
("Infinity", "False"),
(
"Pi",
"True",
),
(
"E",
"True",
),
(
"I",
"True",
),
# A detached symbol is not numeric
(
"a",
"False",
),
# If a symbol is assigned to a numeric downvalue,
# NumericQ evaluates first its argument
(
"b=2;b",
"True",
),
(
"$MachinePrecision",
"True",
),
# Also, an undefined symbol can be tagged as Numeric:
(
"NumericQ[a]=True; a",
"True",
),
# and also untagged as Numeric
# (notice that as in WMA, `Protect` do not prevent the assignment):
("NumericQ[Pi]=False; Pi", False),
# General builtin symbols are not numeric.
(
"Print",
"False",
),
# Head of numeric functions are not numeric
(
"Sin",
"False",
),
],
)
def test_atomic_numericq(str_expr, str_expected):
check_evaluation(f"NumericQ[{str_expr}]", str_expected)
session.evaluate("ClearAll[a, b, Pi]")
session.evaluate("NumericQ[a]=False;NumericQ[b]=False; NumericQ[Pi]=True;")
@pytest.mark.parametrize(
(
"str_expr",
"str_expected",
),
[
# If an expression has a Head with attribute NumericFunction
# and all their arguments are numeric, returns True
(
"Sin[1]",
"True",
),
(
"a",
"False",
),
(
"Sin[a]",
"False",
),
(
"NumericQ[a]=True; Sin[a]",
"True",
),
(
"Attributes[F]=NumericFunction; F[1]",
"True",
),
(
"Attributes[F]=NumericFunction; F[Pi]",
"True",
),
(
"""Attributes[F]=NumericFunction; F["bla"]""",
"False",
),
(
"""F[1,l->2]""",
"False",
),
# NumericQ returns True for expressions that
# cannot be evaluated to a number:
("1/(Sin[1]^2+Cos[1]^2-1)", "True"),
("Simplify[1/(Sin[1]^2+Cos[1]^2-1)]", "False"),
],
)
def test_expression_numericq(str_expr, str_expected):
check_evaluation(f"NumericQ[{str_expr}]", str_expected)
session.evaluate("ClearAll[a, F]")
session.evaluate("NumericQ[a]=False;NumericQ[b]=False; NumericQ[Pi]=True;")
@pytest.mark.parametrize(
(
"str_expr",
"str_expected",
),
[
# We can set that a symbol `a` is Numeric or not
# by direct assign
(
"NumericQ[a]=True;NumericQ[a]",
"True",
),
(
"NumericQ[a]=False;NumericQ[a]",
"False",
),
# In WMA, Clear does not reset the value of NumericQ
# In Mathics, it does:
("NumericQ[a]=True; Clear[a]; NumericQ[a]", "True"),
("NumericQ[a]=True; ClearAll[a]; NumericQ[a]", "True"),
("NumericQ[a]=False", "False"),
# We can only set True or False, otherwise the assignment fails:
(
"NumericQ[a]=37",
"37",
),
(
"NumericQ[a]",
"False",
),
(
"NumericQ[a]:=($MachinePrecision>10);NumericQ[a]",
"False",
),
# Assignment to NumericQ[expr] for expr expressions or patterns
# does not show error messages, but does not have any effect:
(
"NumericQ[g[x]]=True;NumericQ[g[x]]",
"False",
),
(
"NumericQ[g[x_]]=True;NumericQ[g[y]]",
"False",
),
],
)
def test_assign_numericq(str_expr, str_expected):
check_evaluation(str_expr, str_expected)
session.evaluate("ClearAll[a, F, g]")
session.evaluate("NumericQ[a]=False;NumericQ[b]=False; NumericQ[Pi]=True;")