001
002/*
003 * $Id: LocalTest.java 3789 2011-10-01 18:54:43Z kredel $
004 */
005
006package edu.jas.application;
007
008import java.util.ArrayList;
009import java.util.List;
010
011import junit.framework.Test;
012import junit.framework.TestCase;
013import junit.framework.TestSuite;
014
015//import org.apache.log4j.BasicConfigurator;
016//import org.apache.log4j.Logger;
017
018//import edu.jas.structure.RingElem;
019import edu.jas.arith.BigRational;
020import edu.jas.kern.ComputerThreads;
021import edu.jas.poly.GenPolynomial;
022import edu.jas.poly.GenPolynomialRing;
023
024
025/**
026 * Local tests with JUnit. 
027 * @author Heinz Kredel.
028 */
029
030public class LocalTest extends TestCase {
031
032/**
033 * main.
034 */
035   public static void main (String[] args) {
036       //BasicConfigurator.configure();
037       junit.textui.TestRunner.run( suite() );
038   }
039
040/**
041 * Constructs a <CODE>LocalTest</CODE> object.
042 * @param name String.
043 */
044   public LocalTest(String name) {
045          super(name);
046   }
047
048/**
049 * suite.
050 */ 
051 public static Test suite() {
052     TestSuite suite= new TestSuite(LocalTest.class);
053     return suite;
054   }
055
056   //private final static int bitlen = 100;
057
058   Ideal<BigRational> id;
059   LocalRing<BigRational> fac;
060   GenPolynomialRing<BigRational> mfac;
061   List<GenPolynomial<BigRational>> F;
062
063   Local< BigRational > a;
064   Local< BigRational > b;
065   Local< BigRational > c;
066   Local< BigRational > d;
067   Local< BigRational > e;
068
069   int rl = 3; 
070   int kl = 3;
071   int ll = 5;
072   int el = 2;
073   float q = 0.3f;
074   int il = 2; 
075
076   protected void setUp() {
077       a = b = c = d = e = null;
078       BigRational cfac = new BigRational(1);
079       String[] vars = new String[] { "x", "y", "z" };
080       mfac = new GenPolynomialRing<BigRational>( cfac, rl, vars );
081       id = null;
082       while ( id == null || id.isONE() ) {
083           F = new ArrayList<GenPolynomial<BigRational>>( il );
084           for ( int i = 0; i < rl; i++ ) {
085               //GenPolynomial<BigRational> mo = mfac.random(kl,ll,el,q);
086               GenPolynomial<BigRational> mo = mfac.univariate(i);
087               mo = mo.sum( mfac.fromInteger( cfac.random(7).denominator() ) );
088               while ( mo.isConstant() ) {
089                    mo = mfac.random(kl,ll,el,q);
090               }
091               F.add( mo );
092           }
093           id = new Ideal<BigRational>(mfac,F);
094           id = id.GB();
095       }
096       //System.out.println("id = " + id);
097       fac = new LocalRing<BigRational>( id );
098       //System.out.println("fac = " + fac);
099       F = null;
100   }
101
102   protected void tearDown() {
103       a = b = c = d = e = null;
104       fac = null;
105       id = null;
106       mfac = null;
107       ComputerThreads.terminate();
108   }
109
110
111/**
112 * Test constructor and toString.
113 * 
114 */
115 public void testConstruction() {
116     c = fac.getONE();
117     //System.out.println("c = " + c);
118     //System.out.println("c.num = " + c.num);
119     assertTrue("length( c ) = 1", c.num.length() == 1);
120     assertTrue("isZERO( c )", !c.isZERO() );
121     assertTrue("isONE( c )", c.isONE() );
122
123     d = fac.getZERO();
124     //System.out.println("d = " + d);
125     //System.out.println("d.num = " + d.num);
126     assertTrue("length( d ) = 0", d.num.length() == 0);
127     assertTrue("isZERO( d )", d.isZERO() );
128     assertTrue("isONE( d )", !d.isONE() );
129 }
130
131
132/**
133 * Test random polynomial.
134 * 
135 */
136 public void testRandom() {
137     //System.out.println("fac = " + fac);
138     for (int i = 0; i < 4; i++) {
139         //a = fac.random(ll+i);
140         a = fac.random(kl*(i+1), ll+i, el, q );
141         //System.out.println("a = " + a);
142         assertTrue("length( a"+i+" ) <> 0", a.num.length() >= 0);
143         if ( a.isZERO() || a.isONE() ) {
144             continue;
145         }
146         assertTrue(" not isZERO( a"+i+" )", !a.isZERO() );
147         assertTrue(" not isONE( a"+i+" )", !a.isONE() );
148     }
149 }
150
151
152/**
153 * Test addition.
154 * Not jet working because of monic GBs.
155 */
156 public void testAddition() {
157     //System.out.println("fac = " + fac);
158
159     a = fac.random(kl,ll,el,q);
160     b = fac.random(kl,ll,el,q);
161     //System.out.println("a = " + a);
162     //System.out.println("b = " + b);
163
164     c = a.sum(b);
165     d = c.subtract(b);
166     //System.out.println("c = " + c);
167     //System.out.println("d = " + d);
168     assertEquals("a+b-b = a",a,d);
169
170     c = a.sum(b);
171     d = b.sum(a);
172     assertEquals("a+b = b+a",c,d);
173
174
175     c = fac.random(kl,ll,el,q);
176     d = c.sum( a.sum(b) );
177     e = c.sum( a ).sum(b);
178     assertEquals("c+(a+b) = (c+a)+b",d,e);
179
180     c = a.sum( fac.getZERO() );
181     d = a.subtract( fac.getZERO() );
182     assertEquals("a+0 = a-0",c,d);
183
184     c = fac.getZERO().sum( a );
185     d = fac.getZERO().subtract( a.negate() );
186     assertEquals("0+a = 0+(-a)",c,d);
187 }
188
189
190/**
191 * Test object multiplication.
192 * Not jet working because of monic GBs
193 */
194
195 public void testMultiplication() {
196     //System.out.println("fac = " + fac);
197
198     a = fac.random(kl,ll,el,q);
199     b = fac.random(kl,ll,el,q);
200
201     if ( a.isZERO() || b.isZERO() ) {
202         return;
203     }
204     //System.out.println("a = " + a);
205     //System.out.println("b = " + b);
206     assertTrue("not isZERO( a )", !a.isZERO() );
207     assertTrue("not isZERO( b )", !b.isZERO() );
208
209     c = b.multiply(a);
210     d = a.multiply(b);
211     //System.out.println("c = " + c);
212     //System.out.println("d = " + d);
213     assertTrue("not isZERO( c )", !c.isZERO() );
214     assertTrue("not isZERO( d )", !d.isZERO() );
215
216     e = d.subtract(c);
217     assertTrue("isZERO( a*b-b*a ) " + e, e.isZERO() );
218
219     assertTrue("a*b = b*a", c.equals(d) );
220     assertEquals("a*b = b*a",c,d);
221
222     c = fac.random(kl,ll,el,q);
223     d = a.multiply( b.multiply(c) );
224     e = (a.multiply(b)).multiply(c);
225     //System.out.println("c = " + c);
226     //System.out.println("d = " + d);
227     //System.out.println("e = " + e);
228
229     assertEquals("a(bc) = (ab)c",d,e);
230     assertTrue("a(bc) = (ab)c", d.equals(e) );
231
232     c = a.multiply( fac.getONE() );
233     d = fac.getONE().multiply( a );
234     assertEquals("a*1 = 1*a",c,d);
235
236     if ( a.isUnit() ) {
237        c = a.inverse();
238        d = c.multiply(a);
239        //System.out.println("a = " + a);
240        //System.out.println("c = " + c);
241        //System.out.println("d = " + d);
242        assertTrue("a*1/a = 1",d.isONE()); 
243        d = c.inverse();
244        //System.out.println("d = " + d);
245        assertTrue("1/(1/a) = a",d.equals(a)); 
246     }
247 }
248
249}