001/*
002 * $Id$
003 */
004
005package edu.jas.fd;
006
007
008import java.util.ArrayList;
009import java.util.List;
010import java.util.Arrays;
011
012import edu.jas.arith.BigRational;
013import edu.jas.arith.BigQuaternion;
014import edu.jas.arith.BigQuaternionRing;
015import edu.jas.gb.SolvableGroebnerBaseAbstract;
016import edu.jas.gb.SolvableGroebnerBaseSeq;
017import edu.jas.kern.ComputerThreads;
018import edu.jas.poly.GenPolynomial;
019import edu.jas.poly.GenSolvablePolynomial;
020import edu.jas.poly.GenSolvablePolynomialRing;
021import edu.jas.poly.PolyUtil;
022import edu.jas.poly.PolynomialList;
023import edu.jas.poly.RecSolvablePolynomial;
024import edu.jas.poly.RecSolvablePolynomialRing;
025import edu.jas.poly.RelationGenerator;
026import edu.jas.poly.TermOrder;
027import edu.jas.poly.TermOrderByName;
028import edu.jas.poly.WeylRelationsIterated;
029
030import junit.framework.Test;
031import junit.framework.TestCase;
032import junit.framework.TestSuite;
033
034
035/**
036 * GCD Simple PRS algorithm tests with JUnit. <b>Note:</b> not in sync with
037 * implementation.
038 * @author Heinz Kredel
039 */
040
041public class GCDSimpleQuatTest extends TestCase {
042
043
044    /**
045     * main.
046     */
047    public static void main(String[] args) {
048        junit.textui.TestRunner.run(suite());
049        ComputerThreads.terminate();
050    }
051
052
053    /**
054     * Constructs a <CODE>GCDSimpleQuatTest</CODE> object.
055     * @param name String.
056     */
057    public GCDSimpleQuatTest(String name) {
058        super(name);
059    }
060
061
062    /**
063     */
064    public static Test suite() {
065        TestSuite suite = new TestSuite(GCDSimpleQuatTest.class);
066        return suite;
067    }
068
069
070    GreatestCommonDivisorAbstract<BigQuaternion> fd;
071
072
073    TermOrder to = TermOrderByName.INVLEX;
074
075
076    GenSolvablePolynomialRing<BigQuaternion> qfac;
077
078
079    //GenSolvablePolynomialRing<GenPolynomial<BigQuaternion>> rfac;
080    RecSolvablePolynomialRing<BigQuaternion> rfac;
081
082
083    GenSolvablePolynomial<BigQuaternion> a, b, a0, b0, c, d, e, f;
084
085
086    GenSolvablePolynomial<GenPolynomial<BigQuaternion>> ar, br, cr, dr, er, ar0, br0;
087
088
089    int rl = 4;
090
091
092    int kl = 2;
093
094
095    int ll = 2;
096
097
098    int el = 3;
099
100
101    float q = 0.25f;
102
103
104    @Override
105    protected void setUp() {
106        a = b = c = d = e = null;
107        ar = br = cr = dr = er = null;
108        String[] vars = new String[] { "a", "b", "c", "d" };
109        BigQuaternionRing cf = new BigQuaternionRing();
110        fd = new GreatestCommonDivisorSimple<BigQuaternion>(cf);
111        qfac = new GenSolvablePolynomialRing<BigQuaternion>(cf, to, vars);
112        RelationGenerator<BigQuaternion> wl = new WeylRelationsIterated<BigQuaternion>();
113        qfac.addRelations(wl);
114        rfac = (RecSolvablePolynomialRing<BigQuaternion>) qfac.recursive(1);
115        //System.out.println("qfac = " + qfac);
116    }
117
118
119    @Override
120    protected void tearDown() {
121        a = b = c = d = e = null;
122        ar = br = cr = dr = er = null;
123        fd = null;
124        qfac = null;
125        rfac = null;
126    }
127
128
129    /**
130     * Test quaternion base gcd simple.
131     */
132    public void testQuatBaseGcdSimple() {
133        String[] uvars = new String[] { "x" };
134        BigQuaternionRing cf = new BigQuaternionRing();
135        //GenSolvablePolynomialRing<BigQuaternion> qfac;
136        qfac = new GenSolvablePolynomialRing<BigQuaternion>(cf, 1, to, uvars);
137        //System.out.println("qfac = " + qfac.toScript());
138        //GenSolvablePolynomial<BigQuaternion> a, b, c, d, e;
139        //GreatestCommonDivisorAbstract<BigQuaternion> fd;
140        //fd = new GreatestCommonDivisorSimple<BigQuaternion>(cf);
141        for (int i = 0; i < 3; i++) {
142            //System.out.println();
143            a = qfac.random(kl + (i), ll + 2 * i, el + 2, q);
144            a = (GenSolvablePolynomial<BigQuaternion>) a.sum(qfac.univariate(0).power(2));
145            b = qfac.random(kl + (i + 1), ll + i, el + 2, q);
146            b = (GenSolvablePolynomial<BigQuaternion>) b.sum(qfac.univariate(0));
147            c = qfac.random(kl + (i + 1), ll + 1, el + 1, q);
148            c = c.multiply(qfac.univariate(0));
149            if (a.isZERO()||b.isZERO()||c.isZERO()) {
150                // skip for this turn
151                continue;
152            }
153            a = a.monic();
154            b = b.monic();
155            c = c.monic();
156            //a = fd.basePrimitivePart(a);
157            //b = fd.basePrimitivePart(b);
158            //c = (GenSolvablePolynomial<BigQuaternion>) fd.basePrimitivePart(c).abs();
159            //System.out.println("a  = " + a);
160            //System.out.println("b  = " + b);
161            //System.out.println("c  = " + c);
162
163            //a = a.multiply(c);
164            //b = b.multiply(c);
165            a = c.multiply(a);
166            b = c.multiply(b);
167            //System.out.println("a  = " + a);
168            //System.out.println("b  = " + b);
169
170            d = fd.leftBaseGcd(a, b);
171            //System.out.println("d  = " + d);
172            //System.out.println("c  = " + c);
173
174            e = (GenSolvablePolynomial<BigQuaternion>) FDUtil.<BigQuaternion> leftBaseSparsePseudoRemainder(a, d);
175            //System.out.println("e = " + e);
176            assertTrue("gcd(ca,cb) | ca " + e, e.isZERO());
177
178            e = (GenSolvablePolynomial<BigQuaternion>) FDUtil.<BigQuaternion> leftBaseSparsePseudoRemainder(b, d);
179            //System.out.println("e = " + e);
180            assertTrue("gcd(ca,cb) | cb " + e, e.isZERO());
181
182            // todo
183            //e = (GenSolvablePolynomial<BigQuaternion>) FDUtil.<BigQuaternion> leftBaseSparsePseudoRemainder(d, c);
184            //System.out.println("e = " + e);
185            //assertTrue("c | gcd(ca,cb) " + e, e.isZERO());
186        }
187    }
188
189}