001 /* 002 * $Id: FactorAlgebraicPrimTest.java 3650 2011-05-28 18:32:35Z kredel $ 003 */ 004 005 package edu.jas.application; 006 007 008 import java.util.SortedMap; 009 010 import junit.framework.Test; 011 import junit.framework.TestCase; 012 import junit.framework.TestSuite; 013 014 import org.apache.log4j.BasicConfigurator; 015 016 import edu.jas.arith.BigRational; 017 import edu.jas.kern.ComputerThreads; 018 import edu.jas.poly.AlgebraicNumber; 019 import edu.jas.poly.AlgebraicNumberRing; 020 import edu.jas.poly.GenPolynomial; 021 import edu.jas.poly.GenPolynomialRing; 022 import edu.jas.poly.TermOrder; 023 024 025 /** 026 * Factor algebraic tests with JUnit. 027 * @author Heinz Kredel. 028 */ 029 030 public class FactorAlgebraicPrimTest extends TestCase { 031 032 033 /** 034 * main. 035 */ 036 public static void main(String[] args) { 037 //BasicConfigurator.configure(); 038 junit.textui.TestRunner.run(suite()); 039 } 040 041 042 /** 043 * Constructor. 044 * @param name String. 045 */ 046 public FactorAlgebraicPrimTest(String name) { 047 super(name); 048 } 049 050 051 /** 052 */ 053 public static Test suite() { 054 TestSuite suite = new TestSuite(FactorAlgebraicPrimTest.class); 055 return suite; 056 } 057 058 059 int rl = 3; 060 061 062 int kl = 5; 063 064 065 int ll = 5; 066 067 068 int el = 3; 069 070 071 float q = 0.3f; 072 073 074 @Override 075 protected void setUp() { 076 } 077 078 079 @Override 080 protected void tearDown() { 081 ComputerThreads.terminate(); 082 } 083 084 085 /** 086 * Test dummy for Junit. 087 * 088 */ 089 public void testDummy() { 090 } 091 092 093 /** 094 * Test algebraic factorization. 095 * 096 */ 097 public void testAlgebraicFactorization() { 098 099 TermOrder to = new TermOrder(TermOrder.INVLEX); 100 BigRational cfac = new BigRational(1); 101 String[] alpha = new String[] { "alpha" }; 102 String[] vars = new String[] { "z" }; 103 GenPolynomialRing<BigRational> pfac = new GenPolynomialRing<BigRational>(cfac, 1, to, alpha); 104 GenPolynomial<BigRational> agen = pfac.univariate(0, 2); 105 agen = agen.sum(pfac.getONE()); // x^2 + 1 106 AlgebraicNumberRing<BigRational> afac = new AlgebraicNumberRing<BigRational>(agen, true); 107 GenPolynomialRing<AlgebraicNumber<BigRational>> apfac = new GenPolynomialRing<AlgebraicNumber<BigRational>>( 108 afac, 1, to, vars); // univariate 109 110 //System.out.println("agen = " + agen); 111 //System.out.println("afac = " + afac); 112 //System.out.println("apfac = " + apfac); 113 114 FactorAlgebraicPrim<BigRational> fac = new FactorAlgebraicPrim<BigRational>(afac); 115 116 for (int i = 1; i < 7; i++) { 117 int facs = 0; 118 GenPolynomial<AlgebraicNumber<BigRational>> a; 119 GenPolynomial<AlgebraicNumber<BigRational>> c = apfac.random(2, ll + i, el + i, q); 120 //a = a.monic(); 121 GenPolynomial<AlgebraicNumber<BigRational>> b = apfac.random(2, ll + i, el + i, q); 122 if (b.degree() == 0) { 123 b = b.multiply(apfac.univariate(0)); 124 } 125 //b = b.monic(); 126 //if ( false && ! a.leadingBaseCoefficient().isONE() ) { 127 //continue; 128 //ExpVector e = a.leadingExpVector(); 129 //a.doPutToMap(e,cfac.getONE()); 130 //} 131 if (c.degree() > 0) { 132 facs++; 133 } 134 if (b.degree() > 0) { 135 facs++; 136 } 137 //a = apfac.univariate(0,2).sum( apfac.getONE() ); // x^2 + 1 138 //a = a.multiply(a); 139 //a = a.multiply( apfac.univariate(0,2).subtract( apfac.getONE() ) ); // x^2 - 1 140 //a = apfac.univariate(0,3).subtract( apfac.getONE() ); // x^3 - 1 141 //a = apfac.univariate(0,3).sum( apfac.getONE() ); // x^3 + 1 142 a = c.multiply(b); 143 //a = a.monic(); 144 //System.out.println("\na = " + a); 145 //System.out.println("b = " + b.monic()); 146 //System.out.println("c = " + c.monic()); 147 148 long sp = System.currentTimeMillis(); 149 SortedMap<GenPolynomial<AlgebraicNumber<BigRational>>, Long> sm = fac.baseFactors(a); 150 sp = System.currentTimeMillis() - sp; 151 //System.out.println("\na = " + a); 152 //System.out.println("sm = " + sm); 153 if (sm.size() >= facs) { 154 assertTrue("#facs < " + facs, sm.size() >= facs); 155 } else { 156 System.out.println("sm.size() < facs = " + facs); 157 } 158 boolean t = fac.isFactorization(a, sm); 159 //System.out.println("t = " + t); 160 assertTrue("prod(factor(a)) = a", t); 161 162 long sr = System.currentTimeMillis(); 163 sm = new edu.jas.ufd.FactorAlgebraic<BigRational>(afac).baseFactors(a); 164 sr = System.currentTimeMillis() - sr; 165 //System.out.println("\na = " + a); 166 //System.out.println("sm = " + sm); 167 if (sm.size() >= facs) { 168 assertTrue("#facs < " + facs, sm.size() >= facs); 169 } else { 170 System.out.println("sm.size() < facs = " + facs); 171 } 172 t = fac.isFactorization(a, sm); 173 //System.out.println("t = " + t); 174 assertTrue("prod(factor(a)) = a", t); 175 //System.out.println("time: sp = " + sp + ", sr = " + sr + " milliseconds"); 176 } 177 } 178 179 }