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