diff --git a/02-Wielomiany/out/production/Main.class b/02-Wielomiany/out/production/Main.class index 7e1d69c..d27faa0 100644 Binary files a/02-Wielomiany/out/production/Main.class and b/02-Wielomiany/out/production/Main.class differ diff --git a/02-Wielomiany/out/production/PolynomialTask.class b/02-Wielomiany/out/production/PolynomialTask.class index fd65ed8..991fbda 100644 Binary files a/02-Wielomiany/out/production/PolynomialTask.class and b/02-Wielomiany/out/production/PolynomialTask.class differ diff --git a/02-Wielomiany/src/META-INF/MANIFEST.MF b/02-Wielomiany/src/META-INF/MANIFEST.MF new file mode 100644 index 0000000..5ee19cb --- /dev/null +++ b/02-Wielomiany/src/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: Main + diff --git a/02-Wielomiany/src/Main.java b/02-Wielomiany/src/Main.java index 8a36bd9..fe64543 100644 --- a/02-Wielomiany/src/Main.java +++ b/02-Wielomiany/src/Main.java @@ -4,8 +4,7 @@ import java.util.List; public class Main { - public static void main(String[] args) throws DivisionErrorException, MultiplierNotFoundException { - //ex input in run console : 2 "1 1 1 0 1" "0 1 1" + public static void main(String[] args){ int n = Integer.parseInt(args[0]); List firstPolynomial = new ArrayList<>(); args[1] = args[1].substring(1, args[1].length()-1); @@ -14,6 +13,11 @@ public class Main { List secondPolynomial = new ArrayList<>(); Arrays.asList(args[2].split(",\\s*" )).forEach(factor -> secondPolynomial.add(Integer.valueOf(factor))); PolynomialTask polynomialTask = new PolynomialTask(n, firstPolynomial, secondPolynomial); - polynomialTask.printAllValuesToStandardOutput(); + System.out.print("["); + System.out.print(polynomialTask.printMultipliedPoly() + + ", " + polynomialTask.printSubtractedPoly() + + ", " + polynomialTask.printGcd()); + System.out.print("]"); + } } diff --git a/02-Wielomiany/src/PolynomialTask.java b/02-Wielomiany/src/PolynomialTask.java index 1370891..8fd7fb3 100644 --- a/02-Wielomiany/src/PolynomialTask.java +++ b/02-Wielomiany/src/PolynomialTask.java @@ -1,134 +1,150 @@ -import java.util.ArrayList; import java.util.Arrays; -import java.util.Collections; import java.util.List; -import java.util.stream.Collectors; public class PolynomialTask { private int n; - private List firstPolynomial; - private List secondPolynomial; + private int[] firstPolynomialAsArray; + private int[] secondPolynomialAsArray; public PolynomialTask(int n, List firstPoly, List secondPoly) { this.n = n; - this.firstPolynomial = firstPoly; - this.secondPolynomial = secondPoly; + this.firstPolynomialAsArray = parseListToArray(firstPoly); + this.secondPolynomialAsArray = parseListToArray(secondPoly); } - public Integer[] multiplyPolynomials(List firstPolynomial, List secondPolynomial) { - int[] multiplied = new int[firstPolynomial.size() + secondPolynomial.size() - 1]; - int sizeOfFirstPoly = firstPolynomial.size(); - int sizeOfSecondPoly = secondPolynomial.size(); + private int[] parseListToArray(List polynomial) { + int[] result = new int[polynomial.size()]; + for (int i = 0; i < polynomial.size(); i++) { + result[i] = polynomial.get(i); + } + return result; + } + + private int[] multiplyPolynomials(int[] firstPolynomial, int[] secondPolynomial) { + int[] multiplied = new int[firstPolynomial.length + secondPolynomial.length - 1]; + int sizeOfFirstPoly = firstPolynomial.length; + int sizeOfSecondPoly = secondPolynomial.length; for (int i = 0; i < sizeOfFirstPoly; i++) { for (int j = 0; j < sizeOfSecondPoly; j++) - multiplied[i + j] = (multiplied[i + j] + (firstPolynomial.get(i) * secondPolynomial.get(j))) % n; + multiplied[i + j] = (multiplied[i + j] + (firstPolynomial[i] * secondPolynomial[j])) % n; } - return Arrays.stream(multiplied).boxed().toArray(Integer[]::new); + return multiplied; } - public int[] moduloDividePolynomialsReturnQuotient(List firstPoly, List secondPoly) throws MultiplierNotFoundException, DivisionErrorException { - int[] quotient = new int[firstPoly.size() + secondPoly.size()]; - int firstPolyDegree = firstPoly.size() - 1; - int secondPolyDegree = secondPoly.size() - 1; - List polynomialAfterSubtract = firstPoly; - - - while (firstPolyDegree >= secondPolyDegree) { - polynomialAfterSubtract = calcQuotient(polynomialAfterSubtract, secondPoly, quotient); - firstPolyDegree = polynomialAfterSubtract.size() - 1; + private int[] polyDiv(int[] polyOne, int[] polyTwo) { + if (polyOne.length < polyTwo.length) { + return null; } - - int[] remainder = new int[polynomialAfterSubtract.size()]; - for (int i = 0; i < polynomialAfterSubtract.size(); i++) { - remainder[i] = polynomialAfterSubtract.get(i); + int firstPolyDeg = polyOne.length - 1; + int secondPolyDeg = polyTwo.length - 1; + int[] tempArr = new int[polyOne.length]; + int[] result; + fillTemporaryArray(polyTwo, tempArr); + int tempMultiplier; + int shift = 0; + while (firstPolyDeg >= secondPolyDeg) { + parseNegativeElement(polyOne, firstPolyDeg); + tempMultiplier = findMultiplier(polyOne[firstPolyDeg], polyTwo[secondPolyDeg]); + tempArr = shiftValuesInArray(tempArr, shift); + tempArr = multiplyPolyByNumber(tempArr, tempMultiplier); + tempArr = moduloArray(tempArr); + polyOne = subtractTwoPolynomials(polyOne, tempArr); + firstPolyDeg--; + shift++; } - return remainder; + result = Arrays.copyOf(polyOne, shift - 1); + return result; } - private List calcQuotient(List firstPoly, List secondPoly, int[] quotient) throws MultiplierNotFoundException, DivisionErrorException { - int firstPolyDegree = firstPoly.size() - 1; - int secondPolyDegree = secondPoly.size() - 1; - if (firstPolyDegree < secondPolyDegree) { - throw new DivisionErrorException(); + 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]; } - int quotientCoefficient; - if ((((float) firstPoly.get(firstPolyDegree) / (float) secondPoly.get(secondPolyDegree))) == Math.round(firstPoly.get(firstPolyDegree) / secondPoly.get(secondPolyDegree))) { - quotientCoefficient = firstPoly.get(firstPolyDegree) / secondPoly.get(secondPolyDegree); - } else { - quotientCoefficient = invElem(firstPoly.get(firstPolyDegree), secondPoly.get(secondPolyDegree)); - } - quotient[firstPolyDegree - secondPolyDegree] += quotientCoefficient; - List newPoly = generatePolyFromIndexAndValue(firstPolyDegree - secondPolyDegree, quotientCoefficient); - Integer[] multipliedPolynomials = multiplyPolynomials(newPoly, secondPoly); - List polynomialAfterFirstDivide = new ArrayList<>(Arrays.asList(multipliedPolynomials)); - - return removeUnnecessaryZeros(subtractTwoPolynomials(firstPoly, polynomialAfterFirstDivide)); } - private List removeUnnecessaryZeros(List polynomialAfterSubtract) { - int amountOfZeros = 0; - for (int i = polynomialAfterSubtract.size() - 1; i >= 0; i--) { - if (polynomialAfterSubtract.get(i) == 0) { - amountOfZeros++; - } else { - break; - } + private int[] subtractTwoPolynomials(int[] polyOne, int[] polyTwo) { + int[] result = new int[polyOne.length]; + for (int i = 0; i < polyOne.length; i++) { + result[i] = polyOne[i] - polyTwo[i]; } - - polynomialAfterSubtract = polynomialAfterSubtract.subList(0, polynomialAfterSubtract.size() - amountOfZeros); - return polynomialAfterSubtract; + return result; } - private List subtractTwoPolynomials(List firstPoly, List secondPoly) { - List subtractedPolynomial = new ArrayList<>(Collections.nCopies(firstPoly.size() + secondPoly.size(), 0)); - for (int index = firstPoly.size(); index >= 0; index--) { - if (index < secondPoly.size()) { - subtractedPolynomial.set(index, firstPoly.get(index) - secondPoly.get(index)); - parseNegativeElement(subtractedPolynomial, index); - } + private int[] moduloArray(int[] array) { + for (int i = 0; i < array.length; i++) { + array[i] = array[i] % n; } - return subtractedPolynomial; + return array; } - private void parseNegativeElement(List subtractedPolynomial, int index) { - while (subtractedPolynomial.get(index) < 0) - subtractedPolynomial.set(index, (subtractedPolynomial.get(index) * subtractedPolynomial.get(index)) % n); - } - - private List generatePolyFromIndexAndValue(int size, int quotientCoefficient) { - List poly = new ArrayList<>(Collections.nCopies(size + 1, 0)); - poly.set(size, quotientCoefficient); + private int[] multiplyPolyByNumber(int[] poly, int multiplier) { + for (int i = 0; i < poly.length; i++) { + poly[i] = poly[i] * multiplier; + } return poly; } - private int invElem(int a, int b) throws MultiplierNotFoundException { + 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 void parseNegativeElement(int[] polyOne, int firstPolyDeg) { + while (polyOne[firstPolyDeg] < 0) { + polyOne[firstPolyDeg] = n + polyOne[firstPolyDeg]; + } + } + + private int findMultiplier(int a, int b) { for (int i = 0; i < n; i++) { if (a == (b * i) % n) { return i; } } - - throw new MultiplierNotFoundException(); + return -1; } - public void printAllValuesToStandardOutput() throws DivisionErrorException, MultiplierNotFoundException { - List> values = new ArrayList<>(); - values.add(Arrays.asList(multiplyPolynomials(firstPolynomial, secondPolynomial))); - values.add(Arrays.stream(moduloDividePolynomialsReturnQuotient(firstPolynomial, secondPolynomial)).boxed().collect(Collectors.toList())); - try { - values.add(gcd(firstPolynomial, secondPolynomial)); - } catch (MultiplierNotFoundException e) { + public String printMultipliedPoly() { + return Arrays.toString(multiplyPolynomials(firstPolynomialAsArray, secondPolynomialAsArray)); + } + + public String printSubtractedPoly() { + int[] result = polyDiv(firstPolynomialAsArray, secondPolynomialAsArray); + if (result == null) { + return "Division Error"; + } else if (result.length == 0) { + return "[0]"; + } else { + return Arrays.toString(polyDiv(firstPolynomialAsArray, secondPolynomialAsArray)); } - System.out.println(values); } - private List gcd(List polyOne, List polyTwo) throws MultiplierNotFoundException, DivisionErrorException { - if (polyTwo.isEmpty()) return polyOne; - List poly = Arrays.stream(moduloDividePolynomialsReturnQuotient(polyOne, polyTwo)).boxed().collect(Collectors.toList()); - return gcd(polyTwo, poly); + public String printGcd() { + int[] gcd = gcd(firstPolynomialAsArray, secondPolynomialAsArray); + if (gcd == null) { + return "Division Error"; + } else + return Arrays.toString(gcd); + } + private int[] gcd(int[] firstPoly, int[] secondPoly) { + if (secondPoly.length == 0) { + return firstPoly; + } + if (firstPoly.length >= secondPoly.length) { + return gcd(secondPoly, polyDiv(firstPoly, secondPoly)); + } else { + return gcd(secondPoly, polyDiv(secondPoly, firstPoly)); + } } }