1)set break resume
2)expose UnittestCount UnittestAux Unittest
3
4testsuite "ncalg"
5
6testcase "fractions"
7
8ALPHABET := ['x, 'y, 'z];
9OVL := OrderedVariableList(ALPHABET)
10F := Fraction(Integer)
11OFM := FreeMonoid(OVL)
12FDA := FDALG(OVL, F)
13
14x := 'x::OFM::FDA;
15y := 'y::OFM::FDA;
16z := 'z::OFM::FDA;
17
18testEquals("(x^-1 + y^-1)^-1 - y*(x + y)^-1*x", "0")
19testEquals("x - (x^-1 + (y^-1 - x)^-1)^-1 - x*y*x", "0")
20
21f1 := x + y
22f2 := x + 3*y
23f3 := x - y
24
25testEquals("((f1 + f2) + f3) - (f1 + (f2 + f3))", "0")
26testEquals("((f1 + f2^-1) + f3) - (f1 + (f2^-1 + f3))", "0")
27testEquals("(f1*f2)*f3 - f1*(f2*f3)", "0")
28testEquals("(f1*f2^-2)*f3 - f1*(f2^-2*f3)", "0")
29testEquals("(f1 + f2)*f3 - f1*f3 - f2*f3", "0")
30testEquals("(f1 + f2^-1)*f3 - f1*f3 - f2^-1*f3", "0")
31
32testcaseNoClear "admissible linear systems"
33
34als ==> admissibleLinearSystem
35
36-- preparing some admissible linear systems representing zero
37f_0 := 0$FDA;
38f_1 : FDA := qnew(2);
39f_2 : FDA := qnew(3);
40for k in 2 .. 3 repeat _
41  qsetelt!(pencil(f_1), k, k, 1, 1$F)
42for k in 2 .. 4 repeat _
43  qsetelt!(pencil(f_2), k, k, 1, 1$F)
44f_3 := copy(f_2);
45addRows!(f_3, 2, 3, 2);
46f_4 : FDA := x*y*z;
47f_4(4) := 0;
48
49-- preparing some admissible linear systems
50f_5 : FDA := x*y*z;
51f_6 := copy(f_5);
52f_6 := addRows!(f_6, 3, 4, 2);
53f_7 := insertRowsColumns(f_5, [1], [2]);
54f_8 : FDA := x*z*y;
55
56-- preparing some big admissible linear systems
57f_11 : FDA := x*y*x*y*x*y*x*y + z
58f_12 : FDA := x*y*x*y*x*y*x*y + x
59f_13 := invertMIN(f_11)
60f_14 := invertMIN(f_12)
61
62-- prepare some tiny admissible linear systems
63f_15 : FDA := z^-1;
64f_16 := insertRowsColumns(f_15, [0,1], [0,1]);
65qsetelt!(pencil(f_16), 2, 3, 1, -2);
66qsetelt!(pencil(f_16), 2, 4, 1, -1/2);
67f_16(1) := 1;
68f_16(2) := 0;
69f_16(3) := -2;
70
71-- check minimization
72als(minimize(f_1))
73als(minimize(f_2))
74als(minimize(f_3))
75als(minimize(f_4))
76als(minimize(f_16))
77
78-- simple tests (zero)
79testTrue("zero?(f_0)")
80testTrue("zero?(f_1)")
81testTrue("zero?(f_2)")
82testTrue("zero?(f_3)")
83testTrue("zero?(f_4)")
84
85-- simple tests (true)
86testEquals("f_0", "f_0")
87testEquals("f_1", "f_0")
88testEquals("f_0", "f_2")
89testEquals("f_1", "f_4")
90testEquals("f_5", "f_6")
91testEquals("f_5", "f_7")
92testEquals("f_7", "f_6")
93
94-- simple tests (false)
95testTrue("not(f_5 = f_8)")
96testTrue("not(f_8 = f_6)")
97testTrue("not(f_8 = f_7)")
98
99-- tests which depend on linear word problem (false)
100testTrue("not(f_13 = f_14)")
101
102testcaseNoClear "factoring"
103
104XDP ==> XDPOLY(OVL, F)
105XPF ==> XPolynomialFactor(OVL, F)
106lS ==> List(Symbol)
107
108x := 'x::OFM::XDP;
109y := 'y::OFM::XDP;
110z := 'z::OFM::XDP;
111
112XDP_to_FDA(p) ==
113    lt := listOfTerms(p)
114    res : FDA := 0
115    for t in lt repeat
116        res := res + (t.c)::FDA*(t.k)::FDA
117    res
118
119e1 := ['mult1, 'mult3]$lS
120e1p3 := ['mult1, 'prop3]$lS
121e1o3 := ['mult1, 'ord3]$lS
122e2 := ['mult1, 'mult2, 'mult3]$lS
123p2 := ['mult1, 'prop2, mult3]$lS
124p23 := ['mult1, 'prop2, 'prop3]$lS
125p3 := ['mult1, 'mult2, 'prop3]$lS
126e3 := ['mult3]$lS
127
128check_factor_prop(nl1, nl) ==
129    nl2 : List(FDA) := factor(nl1)
130    for fi2 in nl2 for fi in nl repeat
131        if not(scalar?(fi2/fi)) then return false
132    true
133
134check_factor_prop3(x1, lx) ==
135    lx2 := factor(x1)$XPF
136    for fi2 in lx2 for fi1 in lx repeat
137        lc2 := leadingCoefficient(fi2)
138        lc1 := leadingCoefficient(fi1)
139        if not(lc2*fi1 = lc1*fi2) then
140            print fi1::OutputForm
141            print fi2::OutputForm
142            return false
143    true
144
145check_factor_ord3(x1, lx) ==
146    lx2 := factor(x1)$XPF
147    for fi2 in lx2 repeat
148        res := false
149        for fi1 in lx for k in 1.. repeat
150            lc2 := leadingCoefficient(fi2)
151            lc1 := leadingCoefficient(fi1)
152            not(lc2*fi1 = lc1*fi2) => "iterate"
153            res := true
154            break
155        not res =>
156            print fi2::OutputForm
157            print lx::OutputForm
158            return res
159        lx := delete(lx, k)
160    true
161
162test_factor(l: List XDP, ol : lS): Void ==
163    free lx
164    free l1
165    free nl
166    free nl1
167    lx := l
168    nl := [XDP_to_FDA(p) for p in l]
169    l1 := reduce(*, l)
170    nl1 := reduce(*, nl)
171    if member?('mult1, ol) then
172        testEquals("factor nl1", "nl")
173    if member?('mult2, ol) then
174        nl1 := XDP_to_FDA(l1)
175        testEquals("factor nl1", "nl")
176    if member?('prop2, ol) then
177        nl1 := XDP_to_FDA(l1)
178        testTrue("check_factor_prop(nl1, nl)")
179    if member?('mult3, ol) then
180        testEquals("factor(l1)$XPF", "lx")
181    if member?('prop3, ol) then
182        testTrue("check_factor_prop3(l1, lx)")
183    if member?('ord3, ol) then
184        testTrue("check_factor_ord3(l1, lx)")
185
186a2 := x*y - y*x
187a3 := x*y*z - x*z*y + z*x*y - z*y*x + y*z*x - y*x*z
188
189test_factor([x, y], e2)
190test_factor([x, 1+y*x], e2)
191test_factor([1 - x*y, 1 - y*x], p3)
192test_factor([1 - x*y], e2)
193test_factor([x*y-y*x], e2)
194test_factor([x*y+y*x], e2)
195test_factor([1 - x*y*z*y*x*z], e2)
196test_factor([x - 3, x - 3], p2)
197test_factor([2*x - 3, 2*x - 3], p23)
198test_factor([x*x-2], e2)
199test_factor([x+y, x-y], e2)
200test_factor([x, 1 + y*x, 1+z*y, x*y*z-z*x*y], e2)
201test_factor([1-x*y, 2+y*x, 3-y*z, 2-z*y, 1-x*z], e1p3)
202test_factor([3*x*y+5*y*x, 3*x*y+5*y*x], p23)
203test_factor([3*x*y+5*y*x, 3*x*y+5*y*x, 3*x*y+5*y*x], p23)
204test_factor([2 + x, (1/2)*(1 + y), -6 + 2*z], p3)
205test_factor([1-x*y, 2+y*x, 3-y*z, 2-z*y, x], p23)
206test_factor([(1/12)*(x^2 + 5), 12*(x^2 + x + 7)], p23)
207test_factor([-(29/3)*(3*x^2 - x - 7), -(3/29)*(2*x^2 +3*x - 4)], e1p3)
208test_factor([(1/5)*(x^2 + 5), 5*(x^3 + x + 7)], e1p3)
209test_factor([x^3 + 5, x^3 + x + 7], e1)
210test_factor([(1/7)*(x^3 + x + 7), 7*a3, a2], e1p3)
211test_factor([a2^2 - 2, a2^2 - 3], e1o3)
212test_factor([a3, 1 + y], e2)
213test_factor([x^4 + 5, x^4 + x + 7], e3)
214test_factor([a2^2 - 5, a2^2 - 3, a2^2 - 2], e3)
215test_factor([(x^2+y)^4 + 5, (x^2+y)^4 + (x^2+y) + 7], e3)
216
217statistics()
218