001 /* 002 * $Id: QuatGenPolynomialTest.java 1888 2008-07-12 13:37:34Z kredel $ 003 */ 004 005 package edu.jas.poly; 006 007 import junit.framework.Test; 008 import junit.framework.TestCase; 009 import junit.framework.TestSuite; 010 import edu.jas.arith.BigQuaternion; 011 012 013 /** 014 * BigQuaternion coefficients GenPolynomial tests with JUnit. 015 * @author Heinz Kredel. 016 */ 017 018 public class QuatGenPolynomialTest extends TestCase { 019 020 /** 021 * main 022 */ 023 public static void main (String[] args) { 024 junit.textui.TestRunner.run( suite() ); 025 } 026 027 /** 028 * Constructs a <CODE>QuatGenPolynomialTest</CODE> object. 029 * @param name String. 030 */ 031 public QuatGenPolynomialTest(String name) { 032 super(name); 033 } 034 035 /** 036 */ 037 public static Test suite() { 038 TestSuite suite= new TestSuite(QuatGenPolynomialTest.class); 039 return suite; 040 } 041 042 //private final static int bitlen = 100; 043 044 GenPolynomialRing<BigQuaternion> fac; 045 046 GenPolynomial<BigQuaternion> a; 047 GenPolynomial<BigQuaternion> b; 048 GenPolynomial<BigQuaternion> c; 049 GenPolynomial<BigQuaternion> d; 050 GenPolynomial<BigQuaternion> e; 051 052 int rl = 7; 053 int kl = 10; 054 int ll = 10; 055 int el = 5; 056 float q = 0.5f; 057 058 protected void setUp() { 059 a = b = c = d = e = null; 060 fac = new GenPolynomialRing<BigQuaternion>(new BigQuaternion(1),rl); 061 } 062 063 protected void tearDown() { 064 a = b = c = d = e = null; 065 fac = null; 066 } 067 068 069 /** 070 * Test constructor and toString. 071 * 072 */ 073 public void testConstruction() { 074 c = fac.getONE(); 075 assertTrue("length( c ) = 1", c.length() == 1); 076 assertTrue("isZERO( c )", !c.isZERO() ); 077 assertTrue("isONE( c )", c.isONE() ); 078 079 d = fac.getZERO(); 080 assertTrue("length( d ) = 0", d.length() == 0); 081 assertTrue("isZERO( d )", d.isZERO() ); 082 assertTrue("isONE( d )", !d.isONE() ); 083 } 084 085 086 /** 087 * Test random polynomial. 088 * 089 */ 090 public void testRandom() { 091 for (int i = 0; i < 7; i++) { 092 a = fac.random(ll); 093 // fac.random(rl+i, kl*(i+1), ll+2*i, el+i, q ); 094 assertTrue("length( a"+i+" ) <> 0", a.length() >= 0); 095 assertTrue(" not isZERO( a"+i+" )", !a.isZERO() ); 096 assertTrue(" not isONE( a"+i+" )", !a.isONE() ); 097 } 098 } 099 100 101 /** 102 * Test addition. 103 * 104 */ 105 public void testAddition() { 106 107 a = fac.random(ll); 108 b = fac.random(ll); 109 110 c = a.sum(b); 111 d = c.subtract(b); 112 assertEquals("a+b-b = a",a,d); 113 114 c = fac.random(ll); 115 116 ExpVector u = ExpVector.EVRAND(rl,el,q); 117 BigQuaternion x = BigQuaternion.QRAND(kl); 118 119 b = new GenPolynomial<BigQuaternion>(fac,x, u); 120 c = a.sum(b); 121 d = a.sum(x,u); 122 assertEquals("a+p(x,u) = a+(x,u)",c,d); 123 124 c = a.subtract(b); 125 d = a.subtract(x,u); 126 assertEquals("a-p(x,u) = a-(x,u)",c,d); 127 128 a = new GenPolynomial<BigQuaternion>(fac); 129 b = new GenPolynomial<BigQuaternion>(fac,x, u); 130 c = b.sum(a); 131 d = a.sum(x,u); 132 assertEquals("a+p(x,u) = a+(x,u)",c,d); 133 134 c = a.subtract(b); 135 d = a.subtract(x,u); 136 assertEquals("a-p(x,u) = a-(x,u)",c,d); 137 } 138 139 140 /** 141 * Test object multiplication. 142 * 143 */ 144 145 public void testMultiplication() { 146 147 a = fac.random(ll); 148 assertTrue("not isZERO( a )", !a.isZERO() ); 149 150 b = fac.random(ll); 151 assertTrue("not isZERO( b )", !b.isZERO() ); 152 153 c = b.multiply(a); 154 d = a.multiply(b); 155 assertTrue("not isZERO( c )", !c.isZERO() ); 156 assertTrue("not isZERO( d )", !d.isZERO() ); 157 158 //System.out.println("a = " + a); 159 //System.out.println("b = " + b); 160 e = d.subtract(c); 161 assertTrue("!isZERO( a*b-b*a ) " + e, !e.isZERO() ); 162 163 assertTrue("a*b = b*a", !c.equals(d) ); 164 //assertEquals("a*b = b*a",c,d); 165 166 c = fac.random(ll); 167 //System.out.println("c = " + c); 168 d = a.multiply( b.multiply(c) ); 169 e = (a.multiply(b)).multiply(c); 170 171 //System.out.println("d = " + d); 172 //System.out.println("e = " + e); 173 174 //System.out.println("d-e = " + d.subtract(c) ); 175 176 assertEquals("a(bc) = (ab)c",d,e); 177 assertTrue("a(bc) = (ab)c", d.equals(e) ); 178 179 BigQuaternion x = a.leadingBaseCoefficient().inverse(); 180 c = a.monic(); 181 d = a.multiply(x); 182 assertEquals("a.monic() = a(1/ldcf(a))",c,d); 183 184 BigQuaternion y = b.leadingBaseCoefficient().inverse(); 185 c = b.monic(); 186 d = b.multiply(y); 187 assertEquals("b.monic() = b(1/ldcf(b))",c,d); 188 189 e = new GenPolynomial<BigQuaternion>(fac,y); 190 d = b.multiply(e); 191 assertEquals("b.monic() = b(1/ldcf(b))",c,d); 192 193 //d = e.multiply(b); 194 //assertEquals("b.monic() = (1/ldcf(b) (0))*b",c,d); 195 } 196 197 }