001/*
002 * $Id: Examples.java 4036 2012-07-25 13:11:28Z kredel $
003 */
004
005package edu.jas.gbufd;
006
007
008import java.io.IOException;
009import java.io.Reader;
010import java.io.StringReader;
011import java.util.ArrayList;
012import java.util.List;
013
014import org.apache.log4j.BasicConfigurator;
015
016import edu.jas.arith.BigRational;
017import edu.jas.arith.ModInteger;
018import edu.jas.arith.ModIntegerRing;
019import edu.jas.gb.GroebnerBase;
020import edu.jas.poly.GenPolynomial;
021import edu.jas.poly.GenPolynomialRing;
022import edu.jas.poly.GenPolynomialTokenizer;
023import edu.jas.poly.PolynomialList;
024import edu.jas.poly.TermOrder;
025
026
027/**
028 * Examples for Groebner base usage.
029 * @author Christoph Zengler.
030 * @author Heinz Kredel.
031 */
032public class Examples {
033
034
035    /**
036     * main.
037     */
038    public static void main(String[] args) {
039        BasicConfigurator.configure();
040        //example1();
041        //example2();
042        //example3();
043        exampleGB();
044        //exampleGB1();
045        //exampleGBTrinks();
046    }
047
048
049    /**
050     * example1. Coefficients in Boolean residue class ring.
051     * 
052     */
053    public static void example1() {
054        // moved to edu.jas.application.Examples
055    }
056
057
058    /*
059     * example2. Coefficients in Boolean residue class ring with cuppling of
060     * variables.
061     * 
062     */
063    public static void example2() {
064        // moved to edu.jas.application.Examples
065    }
066
067
068    /**
069     * example3. Coefficients in Boolean ring and additional idempotent
070     * generators.
071     * 
072     */
073    public static void example3() {
074        String[] vars = { "v3", "v2", "v1" };
075
076        ModIntegerRing z2 = new ModIntegerRing(2);
077        GenPolynomialRing<ModInteger> z2p = new GenPolynomialRing<ModInteger>(z2, vars.length, new TermOrder(
078                TermOrder.INVLEX), vars);
079        List<GenPolynomial<ModInteger>> fieldPolynomials = new ArrayList<GenPolynomial<ModInteger>>();
080
081        //add v1^2 + v1, v2^2 + v2, v3^2 + v3 to fieldPolynomials
082        for (int i = 0; i < vars.length; i++) {
083            GenPolynomial<ModInteger> var = z2p.univariate(i);
084            fieldPolynomials.add(var.multiply(var).sum(var));
085        }
086
087
088        List<GenPolynomial<ModInteger>> polynomials = new ArrayList<GenPolynomial<ModInteger>>();
089
090        GenPolynomial<ModInteger> v1 = z2p.univariate(0);
091        GenPolynomial<ModInteger> v2 = z2p.univariate(1);
092        GenPolynomial<ModInteger> v3 = z2p.univariate(2);
093        GenPolynomial<ModInteger> notV1 = v1.sum(z2p.ONE);
094        GenPolynomial<ModInteger> notV2 = v2.sum(z2p.ONE);
095        GenPolynomial<ModInteger> notV3 = v3.sum(z2p.ONE);
096
097        //v1*v2
098        GenPolynomial<ModInteger> p1 = v1.multiply(v2);
099
100        //v1*v2 + v1 + v2 + 1
101        GenPolynomial<ModInteger> p2 = notV1.multiply(notV2);
102
103        //v1*v3 + v1 + v3 + 1
104        GenPolynomial<ModInteger> p3 = notV1.multiply(notV3);
105
106        polynomials.add(p1);
107        polynomials.add(p2);
108        polynomials.add(p3);
109
110        polynomials.addAll(fieldPolynomials);
111
112        //GroebnerBase<ModInteger> gb = new GroebnerBaseSeq<ModInteger>();
113        GroebnerBase<ModInteger> gb = GBFactory.getImplementation(z2);
114
115        List<GenPolynomial<ModInteger>> G = gb.GB(polynomials);
116
117        System.out.println(G);
118    }
119
120
121    /**
122     * Example GBase.
123     * 
124     */
125    @SuppressWarnings("unchecked")
126    static public void exampleGB1() {
127        BigRational coeff = new BigRational();
128        GroebnerBase<BigRational> gb = GBFactory.getImplementation(coeff);
129
130        String exam = "(x1,x2,y) G " + "( " + "( x1 + x2 - 10 ), ( 2 x1 - x2 + 4 ) " + ") ";
131        Reader source = new StringReader(exam);
132        GenPolynomialTokenizer parser = new GenPolynomialTokenizer(source);
133        PolynomialList<BigRational> F = null;
134
135        try {
136            F = (PolynomialList<BigRational>) parser.nextPolynomialSet();
137        } catch (ClassCastException e) {
138            e.printStackTrace();
139            return;
140        } catch (IOException e) {
141            e.printStackTrace();
142            return;
143        }
144        System.out.println("F = " + F);
145
146        List<GenPolynomial<BigRational>> G = gb.GB(F.list);
147
148        PolynomialList<BigRational> trinks = new PolynomialList<BigRational>(F.ring, G);
149        System.out.println("G = " + trinks);
150    }
151
152
153    /**
154     * Example GBase.
155     * 
156     */
157    @SuppressWarnings("unchecked")
158    static public void exampleGB() {
159        BigRational coeff = new BigRational();
160        GroebnerBase<BigRational> gb = GBFactory.getImplementation(coeff);
161
162        String exam = "(x,y) G " + "( " + "( y - ( x^2 - 1 ) ) " + ") ";
163        Reader source = new StringReader(exam);
164        GenPolynomialTokenizer parser = new GenPolynomialTokenizer(source);
165        PolynomialList<BigRational> F = null;
166
167        try {
168            F = (PolynomialList<BigRational>) parser.nextPolynomialSet();
169        } catch (ClassCastException e) {
170            e.printStackTrace();
171            return;
172        } catch (IOException e) {
173            e.printStackTrace();
174            return;
175        }
176        System.out.println("F = " + F);
177
178        List<GenPolynomial<BigRational>> G = gb.GB(F.list);
179
180        PolynomialList<BigRational> trinks = new PolynomialList<BigRational>(F.ring, G);
181        System.out.println("G = " + trinks);
182    }
183
184
185    /**
186     * Example Trinks GBase.
187     * 
188     */
189    @SuppressWarnings("unchecked")
190    static public void exampleGBTrinks() {
191        BigRational coeff = new BigRational();
192        GroebnerBase<BigRational> bb = GBFactory.getImplementation(coeff);
193
194        String exam = "(B,S,T,Z,P,W) L " + "( " + "( 45 P + 35 S - 165 B - 36 ), "
195                + "( 35 P + 40 Z + 25 T - 27 S ), " + "( 15 W + 25 S P + 30 Z - 18 T - 165 B**2 ), "
196                + "( - 9 W + 15 T P + 20 S Z ), " + "( P W + 2 T Z - 11 B**3 ), "
197                + "( 99 W - 11 B S + 3 B**2 ), " + "( B**2 + 33/50 B + 2673/10000 ) " + ") ";
198        Reader source = new StringReader(exam);
199        GenPolynomialTokenizer parser = new GenPolynomialTokenizer(source);
200        PolynomialList<BigRational> F = null;
201
202        try {
203            F = (PolynomialList<BigRational>) parser.nextPolynomialSet();
204        } catch (ClassCastException e) {
205            e.printStackTrace();
206            return;
207        } catch (IOException e) {
208            e.printStackTrace();
209            return;
210        }
211        System.out.println("F = " + F);
212
213        List<GenPolynomial<BigRational>> G = bb.GB(F.list);
214
215        PolynomialList<BigRational> trinks = new PolynomialList<BigRational>(F.ring, G);
216        System.out.println("G = " + trinks);
217    }
218
219}