001/*
002 * $Id$
003 */
004
005package edu.jas.application;
006
007
008import edu.jas.arith.BigRational;
009import edu.jas.poly.AlgebraicNumber;
010import edu.jas.poly.AlgebraicNumberRing;
011import edu.jas.poly.GenPolynomial;
012import edu.jas.poly.GenPolynomialRing;
013
014import junit.framework.Test;
015import junit.framework.TestCase;
016import junit.framework.TestSuite;
017
018
019/**
020 * PolyUtilApp tests with JUnit.
021 * @author Heinz Kredel
022 */
023
024public class PolyUtilAppTest extends TestCase {
025
026
027    /**
028     * main.
029     */
030    public static void main(String[] args) {
031
032        junit.textui.TestRunner.run(suite());
033    }
034
035
036    /**
037     * Constructs a <CODE>PolyUtilAppTest</CODE> object.
038     * @param name String.
039     */
040    public PolyUtilAppTest(String name) {
041        super(name);
042    }
043
044
045    /**
046     */
047    public static Test suite() {
048        TestSuite suite = new TestSuite(PolyUtilAppTest.class);
049        return suite;
050    }
051
052
053    //TermOrder to = new TermOrder(TermOrder.INVLEX);
054
055
056    //GenPolynomialRing<BigRational> dfac;
057
058
059    //GenPolynomialRing<BigRational> cfac;
060
061
062    //GenPolynomialRing<GenPolynomial<BigRational>> rfac;
063
064
065    //BigRational ai, bi, ci, di, ei;
066
067
068    //GenPolynomial<BigRational> a, b, c, d, e;
069
070
071    //GenPolynomial<GenPolynomial<BigRational>> ar, br, cr, dr, er;
072
073
074    //int rl = 5;
075
076
077    int kl = 5;
078
079
080    //int ll = 5;
081
082
083    //int el = 5;
084
085
086    //float q = 0.6f;
087
088
089    @Override
090    protected void setUp() {
091        //a = b = c = d = e = null;
092        //ai = bi = ci = di = ei = null;
093        //ar = br = cr = dr = er = null;
094        // dfac = new GenPolynomialRing<BigRational>(new BigRational(1), rl, to);
095        // cfac = null; //new GenPolynomialRing<BigRational>(new BigRational(1),rl-1,to);
096        // rfac = null; //new GenPolynomialRing<GenPolynomial<BigRational>>(cfac,1,to);
097    }
098
099
100    @Override
101    protected void tearDown() {
102        //a = b = c = d = e = null;
103        //ai = bi = ci = di = ei = null;
104        //ar = br = cr = dr = er = null;
105        // dfac = null;
106        // cfac = null;
107        // rfac = null;
108    }
109
110
111    /**
112     * Test primitive element.
113     * 
114     */
115    public void testPrimitiveElement() {
116        String[] va = new String[] { "alpha" };
117        String[] vb = new String[] { "beta" };
118        GenPolynomialRing<BigRational> aufac, bufac;
119
120        // x^3 - 2
121        aufac = new GenPolynomialRing<BigRational>(new BigRational(1), 1, va);
122        GenPolynomial<BigRational> m;
123        m = aufac.univariate(0, 3);
124        m = m.subtract(aufac.fromInteger(2));
125        //System.out.println("m = " + m);
126
127        // x^2 - 3
128        bufac = new GenPolynomialRing<BigRational>(new BigRational(1), 1, vb);
129        GenPolynomial<BigRational> n;
130        n = bufac.univariate(0, 2);
131        n = n.subtract(bufac.fromInteger(3));
132        //System.out.println("n = " + n);
133
134        AlgebraicNumberRing<BigRational> afac = new AlgebraicNumberRing<BigRational>(m);
135        //System.out.println("afac = " + afac);
136
137        AlgebraicNumberRing<BigRational> bfac = new AlgebraicNumberRing<BigRational>(n);
138        //System.out.println("bfac = " + bfac);
139
140        PrimitiveElement<BigRational> pe;
141        pe = PolyUtilApp.<BigRational> primitiveElement(afac, bfac);
142        //System.out.println("pe = " + pe);
143        AlgebraicNumberRing<BigRational> cfac = pe.primitiveElem;
144
145        AlgebraicNumber<BigRational> a = afac.getGenerator();
146        AlgebraicNumber<BigRational> b = bfac.getGenerator();
147
148        // convert to primitive element field
149        AlgebraicNumber<BigRational> as = PolyUtilApp.<BigRational> convertToPrimitiveElem(cfac, pe.A, a);
150        AlgebraicNumber<BigRational> bs = PolyUtilApp.<BigRational> convertToPrimitiveElem(cfac, pe.B, b);
151
152        // test alpha+(t)beta == gamma
153        AlgebraicNumber<BigRational> cs = as.sum(bs);
154        //System.out.println("cs  = " + cs);
155        assertEquals("alpha+beta == gamma", cs, cfac.getGenerator());
156    }
157
158
159    /**
160     * Test primitive element of extension tower.
161     * 
162     */
163    public void testPrimitiveElementTower() {
164        String[] va = new String[] { "alpha" };
165        String[] vb = new String[] { "beta" };
166        GenPolynomialRing<BigRational> ufac;
167        ufac = new GenPolynomialRing<BigRational>(new BigRational(1), 1, va);
168
169        // x^3 - 2
170        GenPolynomial<BigRational> m;
171        m = ufac.univariate(0, 3);
172        m = m.subtract(ufac.fromInteger(2));
173        //System.out.println("m = " + m);
174
175        AlgebraicNumberRing<BigRational> afac;
176        afac = new AlgebraicNumberRing<BigRational>(m);
177        //System.out.println("afac = " + afac);
178
179        GenPolynomialRing<AlgebraicNumber<BigRational>> aufac;
180        aufac = new GenPolynomialRing<AlgebraicNumber<BigRational>>(afac, 1, vb);
181
182        // y^2 - x
183        GenPolynomial<AlgebraicNumber<BigRational>> n;
184        n = aufac.univariate(0, 2);
185        n = n.subtract(aufac.getONE().multiply(afac.getGenerator()));
186        //System.out.println("n = " + n);
187
188        AlgebraicNumberRing<AlgebraicNumber<BigRational>> bfac;
189        bfac = new AlgebraicNumberRing<AlgebraicNumber<BigRational>>(n);
190        //System.out.println("bfac = " + bfac);
191
192        PrimitiveElement<BigRational> pe;
193        pe = PolyUtilApp.<BigRational> primitiveElement(bfac);
194        //System.out.println("pe = " + pe);
195        AlgebraicNumberRing<BigRational> cfac = pe.primitiveElem;
196
197        AlgebraicNumber<BigRational> a = afac.getGenerator();
198        AlgebraicNumber<AlgebraicNumber<BigRational>> b = bfac.getGenerator();
199
200        // convert to primitive element ring
201        AlgebraicNumber<BigRational> as = PolyUtilApp.<BigRational> convertToPrimitiveElem(cfac, pe.A, a);
202        AlgebraicNumber<BigRational> bs = PolyUtilApp.<BigRational> convertToPrimitiveElem(cfac, pe.A, pe.B,
203                        b);
204
205        // test alpha+(t)beta == gamma
206        AlgebraicNumber<BigRational> cs = as.sum(bs);
207        //System.out.println("cs  = " + cs);
208        assertEquals("alpha+beta == gamma", cs, cfac.getGenerator());
209
210        // test for polynomials, too simple
211        String[] vx = new String[] { "x" };
212        GenPolynomialRing<AlgebraicNumber<BigRational>> rafac = new GenPolynomialRing<AlgebraicNumber<BigRational>>(
213                        afac, 1, vx);
214        GenPolynomialRing<AlgebraicNumber<AlgebraicNumber<BigRational>>> rbfac = new GenPolynomialRing<AlgebraicNumber<AlgebraicNumber<BigRational>>>(
215                        bfac, 1, vx);
216        GenPolynomial<AlgebraicNumber<BigRational>> ap = rafac.getONE().multiply(a);
217        GenPolynomial<AlgebraicNumber<AlgebraicNumber<BigRational>>> bp = rbfac.getONE().multiply(b);
218        //System.out.println("ap = " + ap);
219        //System.out.println("bp = " + bp);
220
221        GenPolynomial<AlgebraicNumber<BigRational>> asp = PolyUtilApp
222                        .<BigRational> convertToPrimitiveElem(cfac, pe.A, ap);
223        GenPolynomial<AlgebraicNumber<BigRational>> bsp = PolyUtilApp
224                        .<BigRational> convertToPrimitiveElem(cfac, pe.A, pe.B, bp);
225        //System.out.println("asp = " + asp);
226        //System.out.println("bsp = " + bsp);
227
228        // test alpha+(t)beta == gamma
229        GenPolynomial<AlgebraicNumber<BigRational>> csp = asp.sum(bsp);
230        //System.out.println("csp = " + csp);
231        assertEquals("alpha+beta == gamma", csp.leadingBaseCoefficient(), cfac.getGenerator());
232
233        // polynomials, trivial
234        GenPolynomialRing<AlgebraicNumber<BigRational>> gfac = csp.ring;
235        assertEquals("gfac == afac", gfac, asp.ring);
236        assertEquals("gfac == bfac", gfac, bsp.ring);
237        //no assertEquals("gfac == rafac", gfac, rafac);
238
239        asp = gfac.random(kl);
240        bsp = gfac.random(kl);
241        //System.out.println("asp = " + asp);
242        //System.out.println("bsp = " + bsp);
243        csp = asp.multiply(bsp);
244        //System.out.println("csp = " + csp);
245        assertEquals("a*b == b*a", csp, bsp.multiply(asp));
246    }
247
248}