1
0
forked from kalmar/DALGLI0

Added Polynomial class and basic math operations

This commit is contained in:
Hubert Tylkowski 2018-06-29 12:30:53 +02:00
parent 0cbe06592f
commit cc5a883982
6 changed files with 202 additions and 1 deletions

View File

@ -3,6 +3,6 @@ package com.tylkowski.ilorazy;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
System.out.println(args[0] + " " + args[1]); QuotientRingTask quotientRingTask = new QuotientRingTask(new Polynomial(args[1], Integer.parseInt(args[0])));
} }
} }

View File

@ -0,0 +1,189 @@
package com.tylkowski.ilorazy;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class Polynomial {
private int[] polynomial;
private int modulo;
public Polynomial(String polyString, int modulo) {
this.polynomial = parsePolynomialString(polyString);
this.modulo = modulo;
}
public Polynomial(int[] polynomial, int modulo) {
this.polynomial = polynomial;
this.modulo = modulo;
}
public Polynomial(List<Integer> polynomial, int modulo) {
this.polynomial = polynomial.stream().mapToInt(i -> i).toArray();
this.modulo = modulo;
}
public int getModulo() {
return modulo;
}
int getSize() {
return polynomial.length;
}
private int[] parsePolynomialString(String poly) {
poly = poly.substring(1, poly.length() - 1);
return Arrays.stream(poly.split(",\\s*")).map(String::trim).mapToInt(Integer::parseInt).toArray();
}
public List<Integer> asList() {
return Arrays.stream(polynomial).boxed().collect(Collectors.toList());
}
public int[] asArray(List<Integer> list) {
return list.stream().mapToInt(i -> i).toArray();
}
public Polynomial add(Polynomial polyTwo) {
int size = Math.max(polynomial.length, polyTwo.getSize());
int[] result = new int[size];
for (int i = 0; i < size; i++) {
int res = 0;
if (i >= polynomial.length) {
res = polyTwo.valueAt(i);
}
if (i >= polyTwo.getSize()) {
res = polynomial[i];
}
if (i < polynomial.length && i< polyTwo.getSize()) {
res = polynomial[i] + polyTwo.valueAt(i);
}
result[i] = res;
}
return new Polynomial(result, modulo);
}
public Polynomial subtract(Polynomial polyTwo) {
int[] result = new int[polynomial.length];
for (int i = 0; i < polynomial.length; i++) {
result[i] = polynomial[i] - polyTwo.valueAt(i);
}
return new Polynomial(result, modulo);
}
public Polynomial multiply(Polynomial multiplyPolynomial) {
int[] multiplied = new int[polynomial.length + multiplyPolynomial.getSize() - 1];
int sizeOfFirstPoly = polynomial.length;
int sizeOfSecondPoly = multiplyPolynomial.getSize();
for (int i = 0; i < sizeOfFirstPoly; i++) {
for (int j = 0; j < sizeOfSecondPoly; j++)
multiplied[i + j] = (multiplied[i + j] + (polynomial[i] * multiplyPolynomial.valueAt(j)));
}
return new Polynomial(multiplied, modulo);
}
public Polynomial divide(Polynomial polyTwo) {
int[] poly = polynomial;
if (poly.length < polyTwo.getSize()) {
return null;
}
int firstPolyDeg = poly.length - 1;
int secondPolyDeg = polyTwo.getSize() - 1;
int[] tempArr = new int[poly.length];
int[] result;
fillTemporaryArray(polyTwo.getPolynomialArray(), tempArr);
int tempMultiplier;
int shift = 0;
while (firstPolyDeg >= secondPolyDeg) {
parseNegativeElement(poly, firstPolyDeg);
tempMultiplier = findMultiplier(poly[firstPolyDeg], polyTwo.valueAt(secondPolyDeg));
tempArr = shiftValuesInArray(tempArr, shift);
tempArr = multiplyPolynomialByNumber(tempArr, tempMultiplier);
tempArr = moduloArray(tempArr);
poly = subtract(new Polynomial(tempArr, modulo)).getPolynomialArray();
firstPolyDeg--;
shift++;
}
result = Arrays.copyOf(poly, shift - 1);
return new Polynomial(result, modulo);
}
public int valueAt(int index) {
return polynomial[index];
}
public void moduloPoly() {
for (int i = 0; i < polynomial.length; i++) {
polynomial[i] = polynomial[i] % modulo;
}
}
public int[] moduloArray(int[] poly) {
for (int i = 0; i < poly.length; i++) {
poly[i] = poly[i] % modulo;
}
return poly;
}
private Polynomial multiplyPolynomialByNumber(int multiplier) {
for (int i = 0; i < polynomial.length; i++) {
polynomial[i] = polynomial[i] * multiplier;
}
return new Polynomial(polynomial, modulo);
}
private int[] multiplyPolynomialByNumber(int[] poly, int multiplier) {
for (int i = 0; i < poly.length; i++) {
poly[i] = poly[i] * multiplier;
}
return poly;
}
public int[] getPolynomialArray() {
return polynomial;
}
private int[] shiftValuesInArray(int[] array, int amount) {
if (amount == 0) {
return array;
} else {
int[] res = new int[array.length];
System.arraycopy(array, amount, res, 0, array.length - amount);
for (int j = array.length - amount + 1; j < res.length; j++) {
res[j] = 0;
}
return res;
}
}
private int findMultiplier(int a, int b) {
for (int i = 0; i < modulo; i++) {
if (a == (b * i) % modulo) {
return i;
}
}
return -1;
}
private void fillTemporaryArray(int[] polyTwo, int[] tempArr) {
for (int i = 0; i < polyTwo.length; i++) {
tempArr[tempArr.length - 1 - i] = polyTwo[polyTwo.length - 1 - i];
}
}
private void parseNegativeElement(int[] polyOne, int polyDeg) {
while (polyOne[polyDeg] < 0) {
polyOne[polyDeg] = modulo + polyOne[polyDeg];
}
}
@Override
public String toString() {
return "Polynomial{" +
"polynomial=" + Arrays.toString(polynomial) +
", modulo=" + modulo +
'}';
}
}

View File

@ -0,0 +1,12 @@
package com.tylkowski.ilorazy;
public class QuotientRingTask {
private Polynomial polynomial;
public QuotientRingTask(Polynomial poly) {
}
}