Created
May 29, 2017 09:01
-
-
Save PoslavskySV/621cb4ee7d0452fe3119522464a673d0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import edu.jas.arith.BigInteger; | |
import edu.jas.arith.ModInteger; | |
import edu.jas.arith.ModIntegerRing; | |
import edu.jas.poly.GenPolynomial; | |
import edu.jas.poly.GenPolynomialRing; | |
import edu.jas.ufd.FactorAbstract; | |
import edu.jas.ufd.FactorFactory; | |
import edu.jas.ufd.GCDFactory; | |
import edu.jas.ufd.GreatestCommonDivisorAbstract; | |
import java.util.Random; | |
import java.util.SortedMap; | |
public class MainJAS { | |
public static void main(String[] args) throws Exception { | |
String[] vars; | |
vars = new String[]{"x", "y", "z"}; | |
testGCDInZ(vars, 25, 125, 100); | |
vars = new String[]{"x"}; | |
testFactorizationInZ(vars, 70, 70, 3, 100); | |
vars = new String[]{"x"}; | |
testFactorizationInZp(vars, 17, 250, 250, 3, 100); | |
vars = new String[]{"x", "y"}; | |
testFactorizationInZ(vars, 4, 10, 4, 100); | |
} | |
/** | |
* Generate random multivariate polynomial over {@code vars} | |
* | |
* @param rnd random source | |
* @param factorSize number of terms in the poly | |
* @param factorMaxDegree maximal degree of the poly | |
* @param vars the variables | |
*/ | |
static String randomPoly(Random rnd, int factorSize, int factorMaxDegree, String... vars) { | |
StringBuilder sb = new StringBuilder(); | |
for (int i = 0; i < factorSize; i++) { | |
if (i != 0) | |
sb.append((rnd.nextBoolean() && rnd.nextBoolean()) ? "-" : "+"); | |
int n = rnd.nextInt(100); | |
sb.append(n); | |
for (String var : vars) | |
sb.append("*").append(var).append("^").append(rnd.nextInt(factorMaxDegree)); | |
} | |
return sb.toString(); | |
} | |
static void testFactorizationInZ(String[] vars, | |
int factorsSize, int factorsDegree, int nFactors, int nIterations) { | |
GenPolynomialRing<BigInteger> factory = new GenPolynomialRing<BigInteger>(BigInteger.ONE, vars); | |
Random rnd = new Random(123);//fix seed | |
FactorAbstract<BigInteger> jasFactor = FactorFactory.getImplementation(BigInteger.ONE); | |
for (int i = 0; i < nIterations; i++) { | |
System.out.println("Iteration: " + i); | |
GenPolynomial<BigInteger> poly = factory.getONE(); | |
for (int j = 0; j < nFactors; j++) { | |
String fString = randomPoly(rnd, factorsSize, factorsDegree, vars); | |
GenPolynomial<BigInteger> factor = factory.parse(fString); | |
if (factor.isConstant()) { | |
--j; | |
continue; | |
} | |
poly = poly.multiply(factor); | |
} | |
System.out.println("Input degree: " + poly.degree()); | |
// run JAS | |
System.out.println("Now running JAS..."); | |
System.out.println("Input: " + poly); | |
long start = System.currentTimeMillis(); | |
SortedMap<GenPolynomial<BigInteger>, Long> map = jasFactor.factors(poly); | |
System.out.println("JAS #factors: " + map.size()); | |
System.out.println("JAS time: " + (System.currentTimeMillis() - start) + "ms"); | |
assert map.size() >= nFactors; | |
System.out.println(); | |
} | |
} | |
static void testFactorizationInZp(String[] vars, | |
long modulus, | |
int factorsSize, int factorsDegree, int nFactors, int nIterations) { | |
ModIntegerRing ring = new ModIntegerRing(modulus); | |
GenPolynomialRing<ModInteger> factory = new GenPolynomialRing<ModInteger>(ring, vars); | |
Random rnd = new Random(123);//fix seed | |
FactorAbstract<ModInteger> jasFactor = FactorFactory.getImplementation(ring); | |
for (int i = 0; i < nIterations; i++) { | |
System.out.println("Iteration: " + i); | |
GenPolynomial<ModInteger> poly = factory.getONE(); | |
for (int j = 0; j < nFactors; j++) { | |
String fString = randomPoly(rnd, factorsSize, factorsDegree, vars); | |
GenPolynomial<ModInteger> factor = factory.parse(fString); | |
if (factor.isConstant()) { | |
--j; | |
continue; | |
} | |
poly = poly.multiply(factor); | |
} | |
System.out.println("Input degree: " + poly.degree()); | |
// run JAS | |
System.out.println("Now running JAS..."); | |
System.out.println("Input: " + poly); | |
long start = System.currentTimeMillis(); | |
SortedMap<GenPolynomial<ModInteger>, Long> map = jasFactor.factors(poly); | |
System.out.println("JAS #factors: " + map.size()); | |
System.out.println("Time: " + (System.currentTimeMillis() - start) + "ms"); | |
assert map.size() >= nFactors; | |
System.out.println(); | |
} | |
} | |
static void testGCDInZ(String[] vars, | |
int factorsSize, int factorsDegree, int nIterations) { | |
GenPolynomialRing<BigInteger> factory = new GenPolynomialRing<BigInteger>(BigInteger.ONE, vars); | |
Random rnd = new Random(123);//fix seed | |
for (int i = 0; i < nIterations; i++) { | |
System.out.println("Iteration: " + i); | |
String sPoly = randomPoly(rnd, factorsSize, factorsDegree, vars); | |
GenPolynomial<BigInteger> a = factory.parse(sPoly); | |
sPoly = randomPoly(rnd, factorsSize, factorsDegree, vars); | |
GenPolynomial<BigInteger> b = factory.parse(sPoly); | |
sPoly = randomPoly(rnd, factorsSize, factorsDegree, vars); | |
GenPolynomial<BigInteger> gcd = factory.parse(sPoly); | |
a = a.multiply(gcd); | |
b = b.multiply(gcd); | |
GreatestCommonDivisorAbstract<BigInteger> gcdFactory = GCDFactory.getImplementation(BigInteger.ONE); | |
System.out.println("Now running JAS..."); | |
System.out.println("Input a: " + a); | |
System.out.println("Input b: " + b); | |
System.out.println("Input gcd: " + gcd); | |
long start = System.currentTimeMillis(); | |
GenPolynomial<BigInteger> actual = gcdFactory.gcd(a, b); | |
System.out.println("JAS gcd: " + actual); | |
System.out.println("Time: " + (System.currentTimeMillis() - start) + "ms"); | |
assert actual.degree() >= gcd.degree(); | |
System.out.println(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment