001/* 002 * $Id: SGCDFactoryTest.java 5688 2017-01-03 08:45:09Z kredel $ 003 */ 004 005package edu.jas.fd; 006 007 008import org.apache.log4j.BasicConfigurator; 009 010import edu.jas.arith.BigComplex; 011import edu.jas.arith.BigInteger; 012import edu.jas.arith.BigRational; 013import edu.jas.arith.ModInteger; 014import edu.jas.arith.ModIntegerRing; 015import edu.jas.poly.AlgebraicNumber; 016import edu.jas.poly.AlgebraicNumberRing; 017import edu.jas.poly.GenPolynomial; 018import edu.jas.poly.GenPolynomialRing; 019import edu.jas.poly.GenSolvablePolynomial; 020import edu.jas.poly.GenSolvablePolynomialRing; 021import edu.jas.poly.TermOrder; 022import edu.jas.poly.TermOrderByName; 023 024// import java.util.Map; 025 026import junit.framework.Test; 027import junit.framework.TestCase; 028import junit.framework.TestSuite; 029 030 031/** 032 * Solvable GreatestCommonDivisor factory tests with JUnit. 033 * @author Heinz Kredel 034 */ 035 036public class SGCDFactoryTest extends TestCase { 037 038 039 /** 040 * main. 041 */ 042 public static void main(String[] args) { 043 BasicConfigurator.configure(); 044 junit.textui.TestRunner.run(suite()); 045 } 046 047 048 /** 049 * Constructs a <CODE>SGCDFactoryTest</CODE> object. 050 * @param name String. 051 */ 052 public SGCDFactoryTest(String name) { 053 super(name); 054 } 055 056 057 /** 058 */ 059 public static Test suite() { 060 TestSuite suite = new TestSuite(SGCDFactoryTest.class); 061 return suite; 062 } 063 064 065 TermOrder to = TermOrderByName.INVLEX; 066 067 068 GenSolvablePolynomialRing<BigInteger> dfac; 069 070 071 GenSolvablePolynomialRing<BigInteger> cfac; 072 073 074 GenSolvablePolynomialRing<GenPolynomial<BigInteger>> rfac; 075 076 077 BigInteger ai, bi, ci, di, ei; 078 079 080 GenSolvablePolynomial<BigInteger> a, b, c, d, e; 081 082 083 GenSolvablePolynomial<GenPolynomial<BigInteger>> ar, br, cr, dr, er; 084 085 086 int rl = 5; 087 088 089 int kl = 4; 090 091 092 int ll = 5; 093 094 095 int el = 3; 096 097 098 float q = 0.3f; 099 100 101 @Override 102 protected void setUp() { 103 a = b = c = d = e = null; 104 ai = bi = ci = di = ei = null; 105 ar = br = cr = dr = er = null; 106 dfac = new GenSolvablePolynomialRing<BigInteger>(new BigInteger(1), rl, to); 107 cfac = new GenSolvablePolynomialRing<BigInteger>(new BigInteger(1), rl - 1, to); 108 rfac = new GenSolvablePolynomialRing<GenPolynomial<BigInteger>>(cfac, 1, to); 109 } 110 111 112 @Override 113 protected void tearDown() { 114 a = b = c = d = e = null; 115 ai = bi = ci = di = ei = null; 116 ar = br = cr = dr = er = null; 117 dfac = null; 118 cfac = null; 119 rfac = null; 120 } 121 122 123 /** 124 * Test get BigInteger implementation. 125 */ 126 public void testBigInteger() { 127 BigInteger bi = new BigInteger(); 128 GreatestCommonDivisor<BigInteger> ufd; 129 130 ufd = SGCDFactory.getImplementation(bi); 131 //System.out.println("ufd = " + ufd); 132 assertTrue("ufd = Primitive " + ufd, ufd instanceof GreatestCommonDivisorPrimitive); 133 } 134 135 136 /** 137 * Test get ModInteger implementation. 138 */ 139 public void testModInteger() { 140 ModIntegerRing mi = new ModIntegerRing(19, true); 141 GreatestCommonDivisor<ModInteger> ufd; 142 143 ufd = SGCDFactory.getImplementation(mi); 144 //System.out.println("ufd = " + ufd); 145 assertTrue("ufd = Simple " + ufd, ufd instanceof GreatestCommonDivisorSimple); 146 147 mi = new ModIntegerRing(30); 148 ufd = SGCDFactory.getImplementation(mi); 149 //System.out.println("ufd = " + ufd); 150 assertTrue("ufd = Primitive " + ufd, ufd instanceof GreatestCommonDivisorPrimitive); 151 } 152 153 154 /** 155 * Test get BigRational implementation. 156 */ 157 public void testBigRational() { 158 BigRational b = new BigRational(); 159 GreatestCommonDivisor<BigRational> ufd; 160 161 ufd = SGCDFactory.getImplementation(b); 162 //System.out.println("ufd = " + ufd); 163 assertTrue("ufd = Primitive " + ufd, ufd instanceof GreatestCommonDivisorPrimitive); 164 } 165 166 167 /** 168 * Test get BigComplex implementation. 169 */ 170 public void testBigComplex() { 171 BigComplex b = new BigComplex(); 172 GreatestCommonDivisor<BigComplex> ufd; 173 174 ufd = SGCDFactory.<BigComplex> getImplementation(b); 175 //System.out.println("ufd = " + ufd); 176 assertTrue("ufd = Simple " + ufd, ufd instanceof GreatestCommonDivisorSimple); 177 } 178 179 180 /** 181 * Test get AlgebraicNumber<BigRational> implementation. 182 * 183 */ 184 public void testAlgebraicNumberBigRational() { 185 BigRational b = new BigRational(); 186 GenPolynomialRing<BigRational> fac; 187 fac = new GenPolynomialRing<BigRational>(b, 1); 188 GenPolynomial<BigRational> mo = fac.random(kl, ll, el, q); 189 while (mo.isZERO() || mo.isONE() || mo.isConstant()) { 190 mo = fac.random(kl, ll, el, q); 191 } 192 193 AlgebraicNumberRing<BigRational> afac; 194 afac = new AlgebraicNumberRing<BigRational>(mo); 195 196 GreatestCommonDivisor<AlgebraicNumber<BigRational>> ufd; 197 198 ufd = SGCDFactory.<AlgebraicNumber<BigRational>> getImplementation(afac); 199 //System.out.println("ufd1 = " + ufd); 200 assertTrue("ufd = Primitive " + ufd, ufd instanceof GreatestCommonDivisorPrimitive); 201 202 203 mo = fac.univariate(0).subtract(fac.getONE()); 204 afac = new AlgebraicNumberRing<BigRational>(mo, true); 205 206 ufd = SGCDFactory.<AlgebraicNumber<BigRational>> getImplementation(afac); 207 //System.out.println("ufd1 = " + ufd); 208 assertTrue("ufd = Simple " + ufd, ufd instanceof GreatestCommonDivisorSimple); 209 } 210 211 212 /** 213 * Test get AlgebraicNumber<ModInteger> implementation. 214 */ 215 public void testAlgebraicNumberModInteger() { 216 ModIntegerRing b = new ModIntegerRing(19, true); 217 GenPolynomialRing<ModInteger> fac; 218 fac = new GenPolynomialRing<ModInteger>(b, 1); 219 GenPolynomial<ModInteger> mo = fac.random(kl, ll, el, q); 220 while (mo.isZERO() || mo.isONE() || mo.isConstant()) { 221 mo = fac.random(kl, ll, el, q); 222 } 223 224 AlgebraicNumberRing<ModInteger> afac; 225 afac = new AlgebraicNumberRing<ModInteger>(mo); 226 227 AlgebraicNumber<ModInteger> a = afac.getONE(); 228 assertTrue("a == 1 " + a, a.isONE()); 229 GreatestCommonDivisor<AlgebraicNumber<ModInteger>> ufd; 230 231 ufd = SGCDFactory.<AlgebraicNumber<ModInteger>> getImplementation(afac); 232 //System.out.println("ufd2 = " + ufd); 233 assertTrue("ufd = Primitive " + ufd, ufd instanceof GreatestCommonDivisorPrimitive); 234 235 mo = fac.univariate(0).subtract(fac.getONE()); 236 afac = new AlgebraicNumberRing<ModInteger>(mo, true); 237 ufd = SGCDFactory.<AlgebraicNumber<ModInteger>> getImplementation(afac); 238 //System.out.println("ufd2 = " + ufd); 239 assertTrue("ufd = Simple " + ufd, ufd instanceof GreatestCommonDivisorSimple); 240 } 241 242}