001/*
002 * $Id$
003 */
004
005package edu.jas.ufd;
006
007
008import java.util.ArrayList;
009import java.util.List;
010import java.util.SortedMap;
011
012import edu.jas.arith.BigInteger;
013import edu.jas.arith.BigRational;
014import edu.jas.kern.ComputerThreads;
015import edu.jas.poly.GenPolynomial;
016import edu.jas.poly.GenPolynomialRing;
017import edu.jas.vector.GenMatrix;
018import edu.jas.vector.GenMatrixRing;
019import edu.jas.vector.LinAlg;
020
021import junit.framework.Test;
022import junit.framework.TestCase;
023import junit.framework.TestSuite;
024
025
026/**
027 * GenMatrix tests with JUnit
028 * @author Heinz Kredel
029 * @see edu.jas.vector.GenMatrixTest
030 */
031
032public class GenMatrixFFTest extends TestCase {
033
034
035    /**
036     * main.
037     */
038    public static void main(String[] args) {
039        junit.textui.TestRunner.run(suite());
040    }
041
042
043    /**
044     * Constructs a <CODE>GenMatrixFFTest</CODE> object.
045     * @param name String.
046     */
047    public GenMatrixFFTest(String name) {
048        super(name);
049    }
050
051
052    /**
053     */
054    public static Test suite() {
055        TestSuite suite = new TestSuite(GenMatrixFFTest.class);
056        return suite;
057    }
058
059
060    int rl = 5;
061
062
063    int kl = 10;
064
065
066    int ll = 10;
067
068
069    float q = 0.5f;
070
071
072    int rows = 3 + 20;
073
074
075    int cols = 3 + 20;
076
077
078    @Override
079    protected void setUp() {
080    }
081
082
083    @Override
084    protected void tearDown() {
085    }
086
087
088    /**
089     * Test fraction free GE.
090     */
091    public void testFractionfreeGE() {
092        BigInteger cfac = new BigInteger(1);
093        int n = 4;
094        int m = 5;
095        GenMatrixRing<BigInteger> mfac = new GenMatrixRing<BigInteger>(cfac, n, m);//rows, cols);
096        //GenVectorModul<BigInteger> vfac = new GenVectorModul<BigInteger>(cfac, m);//rows);
097
098        GenMatrix<BigInteger> A, Ap;
099        //A = mfac.random(kl, 0.7f);
100        A = mfac.parse("[ [3,4,-2,1,-2], [1,-1,2,2,7], [4,-3,4,-3,2], [-1,1,6,-1,1] ]");
101        //System.out.println("A = " + A);
102        if (A.isZERO()) {
103            return;
104        }
105        assertTrue(" not isZERO( A )", !A.isZERO());
106        Ap = A.copy();
107
108        LinAlg<BigInteger> lu = new LinAlg<BigInteger>();
109        //BasicLinAlg<BigInteger> blas = new BasicLinAlg<BigInteger>();
110
111        List<Integer> P = lu.fractionfreeGaussElimination(Ap);
112        //System.out.println("P = " + P);
113        if (P.size() == 0) {
114            //System.out.println("undecomposable");
115            return;
116        }
117        assertTrue("#P != 0: ", P.size() > 0);
118        //System.out.println("A = " + A);
119
120        n = 10;
121        m = n;
122        mfac = new GenMatrixRing<BigInteger>(cfac, n, m);//rows, cols);
123
124        A = mfac.random(5, 0.67f);
125        //System.out.println("A = " + A);
126        if (A.isZERO()) {
127            return;
128        }
129        assertTrue(" not isZERO( A )", !A.isZERO());
130        Ap = A.copy();
131
132        P = lu.fractionfreeGaussElimination(A);
133        //System.out.println("P = " + P);
134        if (P.size() == 0) {
135            //System.out.println("undecomposable");
136            return;
137        }
138        assertTrue("#P != 0: ", P.size() > 0);
139        //System.out.println("A = " + A);
140    }
141
142
143    /**
144     * Test fraction free GE polynomial.
145     */
146    public void testFractionfreeGEpoly() {
147        BigRational cfac = new BigRational(1);
148        int n = 4;
149        int m = n;
150        String[] vars = new String[] { "a1", "a2", "a3", "a4" };
151        GenPolynomialRing<BigRational> pfac = new GenPolynomialRing<BigRational>(cfac, vars);
152        GenMatrixRing<GenPolynomial<BigRational>> mfac = new GenMatrixRing<GenPolynomial<BigRational>>(pfac,
153                        n, m);
154
155        GenMatrix<GenPolynomial<BigRational>> A, Ap;
156        A = mfac.random(4, 0.5f);
157        //A = mfac.parse("[ [3,4,-2,1,-2], [1,-1,2,2,7], [4,-3,4,-3,2], [-1,1,6,-1,1] ]");
158        //System.out.println("A = " + A);
159        if (A.isZERO()) {
160            return;
161        }
162        assertTrue(" not isZERO( A )", !A.isZERO());
163        Ap = A.copy();
164
165        LinAlg<GenPolynomial<BigRational>> lu = new LinAlg<GenPolynomial<BigRational>>();
166
167        List<Integer> P = lu.fractionfreeGaussElimination(Ap);
168        //System.out.println("P = " + P);
169        if (P.size() == 0) {
170            //System.out.println("undecomposable");
171            return;
172        }
173        assertTrue("#P != 0: ", P.size() > 0);
174        //System.out.println("A = " + A);
175
176
177        vars = new String[] { "a11", "a12", "a13", "a14", "a21", "a22", "a23", "a24", "a31", "a32", "a33",
178                "a34", "a41", "a42", "a43", "a44" };
179        pfac = new GenPolynomialRing<BigRational>(cfac, vars);
180        mfac = new GenMatrixRing<GenPolynomial<BigRational>>(pfac, n, m);
181        A = mfac.parse("[ [a11, a12, a13, a14], [a21, a22, a23, a24], [a31, a32, a33, a34], [a41, a42, a43, a44]] ");
182        //System.out.println("A = " + A);
183        if (A.isZERO()) {
184            return;
185        }
186        assertTrue(" not isZERO( A )", !A.isZERO());
187        Ap = A.copy();
188
189        P = lu.fractionfreeGaussElimination(A);
190        //System.out.println("P = " + P);
191        if (P.size() == 0) {
192            //System.out.println("undecomposable");
193            return;
194        }
195        assertTrue("#P != 0: ", P.size() > 0);
196        //System.out.println("A = " + A);
197
198        FactorAbstract<BigRational> ufd = FactorFactory.getImplementation(cfac);
199        int i = 0;
200        for (ArrayList<GenPolynomial<BigRational>> row : A.matrix) {
201            int j = 0;
202            for (GenPolynomial<BigRational> elem : row) {
203                if (elem.isZERO()) {
204                    j++;
205                    continue;
206                }
207                SortedMap<GenPolynomial<BigRational>, Long> pf = ufd.factors(elem);
208                //System.out.println("A(" + i + "," + j + ") = " + elem);
209                //System.out.println("factors = " + pf);
210                assertTrue("#factors == 1:", pf.size() == 1);
211                j++;
212            }
213            i++;
214        }
215        ComputerThreads.terminate();
216    }
217
218}