001 /*
002 * $Id: Examples.java 3432 2010-12-24 14:28:19Z kredel $
003 */
004
005 package edu.jas.gbufd;
006
007
008 import java.io.IOException;
009 import java.io.Reader;
010 import java.io.StringReader;
011 import java.util.ArrayList;
012 import java.util.List;
013
014 import org.apache.log4j.BasicConfigurator;
015
016 import edu.jas.arith.BigRational;
017 import edu.jas.arith.ModInteger;
018 import edu.jas.arith.ModIntegerRing;
019 import edu.jas.gb.GroebnerBase;
020 import edu.jas.poly.GenPolynomial;
021 import edu.jas.poly.GenPolynomialRing;
022 import edu.jas.poly.GenPolynomialTokenizer;
023 import edu.jas.poly.PolynomialList;
024 import edu.jas.poly.TermOrder;
025
026
027 /**
028 * Examples for Groebner base usage.
029 * @author Christoph Zengler.
030 * @author Heinz Kredel.
031 */
032 public 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 } catch (IOException e) {
140 e.printStackTrace();
141 }
142 System.out.println("F = " + F);
143
144 List<GenPolynomial<BigRational>> G = gb.GB(F.list);
145
146 PolynomialList<BigRational> trinks = new PolynomialList<BigRational>(F.ring, G);
147 System.out.println("G = " + trinks);
148 }
149
150
151 /**
152 * Example GBase.
153 *
154 */
155 @SuppressWarnings("unchecked")
156 static public void exampleGB() {
157 BigRational coeff = new BigRational();
158 GroebnerBase<BigRational> gb = GBFactory.getImplementation(coeff);
159
160 String exam = "(x,y) G " + "( " + "( y - ( x^2 - 1 ) ) " + ") ";
161 Reader source = new StringReader(exam);
162 GenPolynomialTokenizer parser = new GenPolynomialTokenizer(source);
163 PolynomialList<BigRational> F = null;
164
165 try {
166 F = (PolynomialList<BigRational>) parser.nextPolynomialSet();
167 } catch (ClassCastException e) {
168 e.printStackTrace();
169 } catch (IOException e) {
170 e.printStackTrace();
171 }
172 System.out.println("F = " + F);
173
174 List<GenPolynomial<BigRational>> G = gb.GB(F.list);
175
176 PolynomialList<BigRational> trinks = new PolynomialList<BigRational>(F.ring, G);
177 System.out.println("G = " + trinks);
178 }
179
180
181 /**
182 * Example Trinks GBase.
183 *
184 */
185 @SuppressWarnings("unchecked")
186 static public void exampleGBTrinks() {
187 BigRational coeff = new BigRational();
188 GroebnerBase<BigRational> bb = GBFactory.getImplementation(coeff);
189
190 String exam = "(B,S,T,Z,P,W) L " + "( " + "( 45 P + 35 S - 165 B - 36 ), "
191 + "( 35 P + 40 Z + 25 T - 27 S ), " + "( 15 W + 25 S P + 30 Z - 18 T - 165 B**2 ), "
192 + "( - 9 W + 15 T P + 20 S Z ), " + "( P W + 2 T Z - 11 B**3 ), "
193 + "( 99 W - 11 B S + 3 B**2 ), " + "( B**2 + 33/50 B + 2673/10000 ) " + ") ";
194 Reader source = new StringReader(exam);
195 GenPolynomialTokenizer parser = new GenPolynomialTokenizer(source);
196 PolynomialList<BigRational> F = null;
197
198 try {
199 F = (PolynomialList<BigRational>) parser.nextPolynomialSet();
200 } catch (ClassCastException e) {
201 e.printStackTrace();
202 } catch (IOException e) {
203 e.printStackTrace();
204 }
205 System.out.println("F = " + F);
206
207 List<GenPolynomial<BigRational>> G = bb.GB(F.list);
208
209 PolynomialList<BigRational> trinks = new PolynomialList<BigRational>(F.ring, G);
210 System.out.println("G = " + trinks);
211 }
212
213 }