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