Skip to content

Commit ebe952b

Browse files
authored
Merge pull request #1404 from mathics/add-test-algebra-unit-test
Move more tests
2 parents 399d561 + e50033c commit ebe952b

2 files changed

Lines changed: 294 additions & 61 deletions

File tree

mathics/builtin/numbers/algebra.py

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -548,67 +548,6 @@ class FactorTermsList(Builtin):
548548
= {-3, -1 + a - 2 a x - a y + 2 x + y - 2 x y + 2 a x y}
549549
>> FactorTermsList[f, x]
550550
= {-3, 1 - a - y + a y, -1 + 2 x}
551-
#> FactorTermsList[f, y]
552-
= {-3, 1 - a - 2 x + 2 a x, -1 + y}
553-
>> FactorTermsList[f, {x, y}]
554-
= {-3, -1 + a, -1 + y, -1 + 2 x}
555-
#> FactorTermsList[f, {y, x}]
556-
= {-3, -1 + a, -1 + 2 x, -1 + y}
557-
#> FactorTermsList[f, {x, y, z}]
558-
= {-3, -1 + a, 1, -1 + y, -1 + 2 x}
559-
#> FactorTermsList[f, {x, y, z, t}]
560-
= {-3, -1 + a, 1, 1, -1 + y, -1 + 2 x}
561-
#> FactorTermsList[f, 3/5]
562-
= {-3, -1 + a - 2 a x - a y + 2 x + y - 2 x y + 2 a x y}
563-
#> FactorTermsList[f, {x, 3, y}]
564-
= {-3, -1 + a, -1 + y, -1 + 2 x}
565-
566-
#> FactorTermsList[f/c]
567-
= {-3, -1 / c + a / c - 2 a x / c - a y / c + 2 x / c + y / c - 2 x y / c + 2 a x y / c}
568-
#> FactorTermsList[f/c, x] == FactorTermsList[f/c, {x, y}]
569-
= True
570-
571-
#> g = Sin[x]*Cos[y]*(1 - 2 a)
572-
= Cos[y] (1 - 2 a) Sin[x]
573-
#> FactorTermsList[g]
574-
= {-1, 2 a Cos[y] Sin[x] - Cos[y] Sin[x]}
575-
#> FactorTermsList[g, x]
576-
= {-1, 2 a Cos[y] Sin[x] - Cos[y] Sin[x]}
577-
#> FactorTermsList[g, x] == FactorTermsList[g, y] == FactorTermsList[g, {x, y}]
578-
= True
579-
580-
#> v = 3 * y * (1 - b) a^x
581-
= 3 y (1 - b) a ^ x
582-
#> FactorTermsList[v]
583-
= {-3, -y a ^ x + b y a ^ x}
584-
#> FactorTermsList[v, x]
585-
= {-3, -y a ^ x + b y a ^ x}
586-
#> FactorTermsList[v, y]
587-
= {-3, b a ^ x - a ^ x, y}
588-
589-
#> FactorTermsList[7]
590-
= {7, 1}
591-
#> FactorTermsList[0]
592-
= {1, 0}
593-
#> FactorTermsList[-3]
594-
= {-3, 1}
595-
#> FactorTermsList[7, {y, x}]
596-
= {7, 1}
597-
#> FactorTermsList[7, x]
598-
= {7, 1}
599-
#> FactorTermsList[7 - I, x]
600-
= {7 - I, 1}
601-
#> FactorTermsList[(x - 1) (1 + a), {c, d}]
602-
= {1, -1 - a + x + a x}
603-
#> FactorTermsList[(x - 1) (1 + a), {c, x}]
604-
= {1, 1 + a, -1 + x, 1}
605-
#> FactorTermsList[(x - 1) (1 + a), {}] == FactorTermsList[(x - 1) (1 + a)]
606-
= True
607-
608-
#> FactorTermsList[x]
609-
= {1, x}
610-
#> FactorTermsList["x"]
611-
= {1, x}
612551
"""
613552

614553
rules = {

test/test_algebra.py

Lines changed: 294 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,294 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Unit tests from builtins ... algebra.py
4+
"""
5+
from .helper import check_evaluation
6+
7+
8+
def test_coefficient():
9+
for str_expr, str_expected in (
10+
# Form 1: Coefficent[expr, form]
11+
(
12+
"Coefficient[(x + 2)/(y - 3) + (x + 3)/(y - 2), z, 0]",
13+
"(2 + x) / (-3 + y) + (3 + x) / (-2 + y)",
14+
),
15+
(
16+
"Coefficient[y (x - 2)/((y^2 - 9)) + (x + 5)/(y + 2), x]",
17+
"y / (-9 + y ^ 2) + 1 / (2 + y)",
18+
),
19+
20+
# MMA returns better one: (-2 + x) / (-9 + y ^ 2)"
21+
(
22+
"Coefficient[y (x - 2)/((y^2 - 9)) + (x + 5)/(y + 2), y]",
23+
"x / (-9 + y ^ 2) - 2 / (-9 + y ^ 2)",
24+
),
25+
(
26+
"Coefficient[y (x - 2)/((y - 3)(y + 3)) + (x + 5)/(y + 2), x]",
27+
"y / (-9 + y ^ 2) + 1 / (2 + y)",
28+
),
29+
30+
# MMA returns better one: (-2 + x) / ((-3 + y) (3 + y))
31+
(
32+
"Coefficient[y (x - 2)/((y - 3)(y + 3)) + (x + 5)/(y + 2), y]",
33+
"x / (-9 + y ^ 2) - 2 / (-9 + y ^ 2)",
34+
),
35+
36+
(
37+
"Coefficient[x^3 - 2 x/y + 3 x z, y]",
38+
"0",
39+
),
40+
41+
# Form 2: Coefficent[expr, form, n]
42+
(
43+
"Coefficient[x^2 + axy^2 - bSin[c], c]",
44+
"0",
45+
),
46+
47+
):
48+
check_evaluation(str_expr, str_expected)
49+
50+
def test_coefficient_list ():
51+
for str_expr, str_expected in (
52+
# Form 1: Coefficent[expr, form]
53+
(
54+
"CoefficientList[x^2 + a x y^2 - b Sin[c], y]",
55+
"{-b Sin[c] + x ^ 2, 0, a x}",
56+
),
57+
(
58+
"CoefficientList[0, x]",
59+
"{}",
60+
),
61+
(
62+
"CoefficientList[1, x]",
63+
"{1}",
64+
),
65+
(
66+
"CoefficientList[x + 1, {}]",
67+
"1 + x",
68+
),
69+
70+
# Form 2 CoefficientList[poly, {var1, var2, ...}]
71+
(
72+
"CoefficientList[a x^2 + b y^3 + c x + d y + 5, {x}]",
73+
"{5 + b y ^ 3 + d y, c, a}",
74+
),
75+
(
76+
"CoefficientList[a x^2 + b y^3 + c x + d y + 5, {}]",
77+
"5 + a x ^ 2 + b y ^ 3 + c x + d y",
78+
),
79+
(
80+
"CoefficientList[a x^2 + b y^3 + c x + d y + 5, {x, y + 1}]",
81+
"{{5 + b y ^ 3 + d y}, {c}, {a}}",
82+
),
83+
(
84+
"CoefficientList[a x^2 + b y^3 + c x + d y + 5, {x + 1, y}]",
85+
"{{5 + a x ^ 2 + c x, d, 0, b}}",
86+
),
87+
(
88+
"CoefficientList[a x^2 + b y^3 + c x + d y + 5, {x + 1, y + 1}]",
89+
"{{5 + a x ^ 2 + b y ^ 3 + c x + d y}}",
90+
),
91+
(
92+
"CoefficientList[y (x - 2)/((z - 3) (z + 3)) + (x + 5)/(z + 2), {x, y}]",
93+
"{{5 / (2 + z), -2 / (-9 + z ^ 2)}, {1 / (2 + z), 1 / (-9 + z ^ 2)}}",
94+
),
95+
(
96+
"CoefficientList[0, {x, y}]",
97+
"{}",
98+
),
99+
(
100+
"CoefficientList[1, {x, y}]",
101+
"{{1}}",
102+
),
103+
):
104+
check_evaluation(str_expr, str_expected)
105+
106+
107+
def test_exponent ():
108+
for str_expr, str_expected in (
109+
(
110+
"Exponent[(x^2 + 1)^3 - 1, x, List]",
111+
"{2, 4, 6}",
112+
),
113+
(
114+
"Exponent[5 x^2 - 3 x + 7, x, List]",
115+
"{0, 1, 2}",
116+
),
117+
(
118+
"Exponent[(x + 1) + (x + 1)^2, x, List]",
119+
"{0, 1, 2}",
120+
),
121+
(
122+
"Exponent[(x + 3 y - 2 z)^3 * (5 y + z), {x, y}, List]",
123+
"{{0, 1, 2, 3}, {0, 1, 2, 3, 4}}",
124+
),
125+
(
126+
'Exponent[(x + 3 y - 2 z)^3*(5 y + z), {"x", "y"}, List]',
127+
"{{0}, {0}}",
128+
),
129+
(
130+
"Exponent[(x + 3 y - 2 z)^3*(5 y + z), {}]",
131+
"{}",
132+
),
133+
(
134+
"Exponent[x^a + b y^3 + c x + 2 y^e + 5, {x, y}, List]",
135+
"{{0, 1, a}, {0, 3, e}}",
136+
),
137+
(
138+
"Exponent[x^2 / y^3, {x, y}]",
139+
"{2, -3}",
140+
),
141+
(
142+
"Exponent[(x + 2)/(y - 3) + (x + 3)/(y - 2), {x, y, z}, List]",
143+
"{{0, 1}, {0}, {0}}",
144+
),
145+
(
146+
"Exponent[x + 6 x^3 y^2 - 3/((x^2) (y^2)), {x, y}, List]",
147+
"{{-2, 1, 3}, {-2, 0, 2}}",
148+
),
149+
(
150+
"Exponent[x^5 Sin[x^2] + x * x^3 Cos[x], x, List]",
151+
"{4, 5}",
152+
),
153+
(
154+
"Exponent[x^5 Sin[x^2] + y Cos[y^2] + Log[x^3] + 6 y^4, {x, y}, List]",
155+
"{{0, 5}, {0, 1, 4}}",
156+
),
157+
):
158+
check_evaluation(str_expr, str_expected)
159+
160+
def test_factor_terms_list ():
161+
for str_expr, str_expected in (
162+
(
163+
"f = 3 (-1 + 2 x) (-1 + y) (1 - a); FactorTermsList[f, y]",
164+
"{-3, 1 - a - 2 x + 2 a x, -1 + y}",
165+
),
166+
(
167+
"FactorTermsList[f, {x, y}]",
168+
"{-3, -1 + a, -1 + y, -1 + 2 x}",
169+
),
170+
(
171+
"FactorTermsList[f, {y, x}]",
172+
"{-3, -1 + a, -1 + 2 x, -1 + y}",
173+
),
174+
(
175+
"FactorTermsList[f, {x, y, z}]",
176+
"{-3, -1 + a, 1, -1 + y, -1 + 2 x}",
177+
),
178+
(
179+
"FactorTermsList[f, {x, y, z, t}]",
180+
"{-3, -1 + a, 1, 1, -1 + y, -1 + 2 x}",
181+
),
182+
(
183+
"FactorTermsList[f, 3/5]",
184+
"{-3, -1 + a - 2 a x - a y + 2 x + y - 2 x y + 2 a x y}",
185+
),
186+
(
187+
"FactorTermsList[f, {x, 3, y}]",
188+
"{-3, -1 + a, -1 + y, -1 + 2 x}",
189+
),
190+
(
191+
"FactorTermsList[f/c]",
192+
"{-3, -1 / c + a / c - 2 a x / c - a y / c + 2 x / c + y / c - 2 x y / c + 2 a x y / c}",
193+
),
194+
(
195+
"FactorTermsList[f/c, x] == FactorTermsList[f/c, {x, y}]",
196+
"True",
197+
),
198+
(
199+
"g = Sin[x]*Cos[y]*(1 - 2 a)",
200+
"Cos[y] (1 - 2 a) Sin[x]",
201+
),
202+
(
203+
"FactorTermsList[g]",
204+
"{-1, 2 a Cos[y] Sin[x] - Cos[y] Sin[x]}",
205+
),
206+
(
207+
"FactorTermsList[g, x]",
208+
"{-1, 2 a Cos[y] Sin[x] - Cos[y] Sin[x]}",
209+
),
210+
(
211+
"FactorTermsList[g, x] == FactorTermsList[g, y] == FactorTermsList[g, {x, y}]",
212+
"True",
213+
),
214+
215+
(
216+
"v = 3 * y * (1 - b) a^x",
217+
"3 y (1 - b) a ^ x",
218+
),
219+
(
220+
"FactorTermsList[v]",
221+
"{-3, -y a ^ x + b y a ^ x}",
222+
),
223+
(
224+
"FactorTermsList[v, x]",
225+
"{-3, -y a ^ x + b y a ^ x}",
226+
),
227+
(
228+
"FactorTermsList[v, y]",
229+
"{-3, b a ^ x - a ^ x, y}",
230+
),
231+
232+
(
233+
"FactorTermsList[7]",
234+
"{7, 1}",
235+
),
236+
(
237+
"FactorTermsList[0]",
238+
"{1, 0}",
239+
),
240+
(
241+
"FactorTermsList[-3]",
242+
"{-3, 1}",
243+
),
244+
(
245+
"FactorTermsList[7, {y, x}]",
246+
"{7, 1}",
247+
),
248+
(
249+
"FactorTermsList[7, x]",
250+
"{7, 1}",
251+
),
252+
(
253+
"FactorTermsList[7 - I, x]",
254+
"{7 - I, 1}",
255+
),
256+
(
257+
"FactorTermsList[(x - 1) (1 + a), {c, d}]",
258+
"{1, -1 - a + x + a x}",
259+
),
260+
(
261+
"FactorTermsList[(x - 1) (1 + a), {c, x}]",
262+
"{1, 1 + a, -1 + x, 1}",
263+
),
264+
(
265+
"FactorTermsList[(x - 1) (1 + a), {}] == FactorTermsList[(x - 1) (1 + a)]",
266+
"True",
267+
),
268+
269+
(
270+
"FactorTermsList[x]",
271+
"{1, x}",
272+
),
273+
(
274+
'FactorTermsList["x"]',
275+
"{1, x}",
276+
),
277+
278+
):
279+
check_evaluation(str_expr, str_expected)
280+
281+
def test_simplify ():
282+
for str_expr, str_expected in (
283+
(
284+
"Simplify[a*x^2+b*x^2]",
285+
"x ^ 2 (a + b)",
286+
),
287+
288+
# triggers TypeError in sympy.simplify
289+
(
290+
"Clear[f]; x f[{y}] // Simplify",
291+
"x f[{y}]",
292+
),
293+
):
294+
check_evaluation(str_expr, str_expected)

0 commit comments

Comments
 (0)