Zadanie 1 i 2 #38
BIN
zad2/Main.class
Normal file
BIN
zad2/Main.class
Normal file
Binary file not shown.
34
zad2/Main.java
Normal file
34
zad2/Main.java
Normal file
@ -0,0 +1,34 @@
|
||||
import java.util.Arrays;
|
||||
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
if (args.length == 0) {
|
||||
System.out.println("Proszę podać parametry np. java Main \"6\" \"[1,2,3,0,1]\" \"[2,4,0,0,2]\"");
|
||||
return;
|
||||
}
|
||||
int modulo = Integer.parseInt(args[0]);
|
||||
int[] polyF = stringToArray(args[1]);
|
||||
int[] polyG = stringToArray(args[2]);
|
||||
String multiple = Arrays.toString(PolynomialOperations.multiple(polyF, polyG, modulo));
|
||||
int[] divisionArray = PolynomialOperations.divide(polyF, polyG, modulo);
|
||||
String division;
|
||||
if (divisionArray.length == 0)
|
||||
division = "[0]";
|
||||
else
|
||||
division = Arrays.toString(divisionArray);
|
||||
String nwd = Arrays.toString(PolynomialOperations.NWD(polyF, polyG, modulo));
|
||||
if (nwd.equals("null"))
|
||||
nwd = "DivisionError";
|
||||
|
||||
|
||||
String outputString = "[" + multiple + ", " + division + ", " + nwd + "]";
|
||||
System.out.println(outputString);
|
||||
}
|
||||
|
||||
private static int[] stringToArray(String str) {
|
||||
return Arrays.stream(str.substring(1, str.length() - 1).split(","))
|
||||
.map(String::trim).mapToInt(Integer::parseInt).toArray();
|
||||
}
|
||||
}
|
BIN
zad2/PolynomialOperations.class
Normal file
BIN
zad2/PolynomialOperations.class
Normal file
Binary file not shown.
88
zad2/PolynomialOperations.java
Normal file
88
zad2/PolynomialOperations.java
Normal file
@ -0,0 +1,88 @@
|
||||
import java.util.Arrays;
|
||||
|
||||
public class PolynomialOperations {
|
||||
|
||||
public static int[] multiple(int[] f, int[] g, int n) {
|
||||
int[] result = new int[f.length + g.length - 1];
|
||||
for (int i = 0; i < f.length; i++)
|
||||
for (int j = 0; j < g.length; j++)
|
||||
result[i + j] += f[i] * g[j];
|
||||
|
||||
for (int i = 0; i < result.length; i++)
|
||||
result[i] = result[i] % n;
|
||||
return result;
|
||||
}
|
||||
|
||||
private static int findFactor(int a, int b, int mod) {
|
||||
for (int i = 0; i < mod; i++)
|
||||
if (a == (b * i) % mod)
|
||||
return i;
|
||||
return Integer.MIN_VALUE;
|
||||
}
|
||||
|
||||
private static int[] shr(int[] p, int shift) {
|
||||
if (shift <= 0)
|
||||
return p;
|
||||
int[] shiftedPoly = new int[p.length];
|
||||
for (int i = 0; i < p.length - shift; i++)
|
||||
shiftedPoly[i] = p[i + shift];
|
||||
return shiftedPoly;
|
||||
}
|
||||
|
||||
private static void polyMultiply(int[] p, int n) {
|
||||
Arrays.stream(p).forEach(value -> value = value * n);
|
||||
}
|
||||
|
||||
private static int[] polySubtract(int[] p, int[] s) {
|
||||
int result[] = Arrays.copyOf(p, p.length);
|
||||
for (int i = 0; i < p.length; ++i) {
|
||||
result[i] = p[i] - s[i];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static int[] createNewPoly(int[] p, int size) {
|
||||
int[] tmpTab = new int[size];
|
||||
for (int j = 0; j < p.length; j++) {
|
||||
tmpTab[tmpTab.length - 1 - j] = p[p.length - 1 - j];
|
||||
}
|
||||
return tmpTab;
|
||||
}
|
||||
|
||||
private static void applyModuloToTab(int p[], int mod) {
|
||||
Arrays.stream(p).forEach(value -> value = value % mod);
|
||||
}
|
||||
|
||||
public static int[] divide(int[] n, int[] d, int mod) {
|
||||
if (n.length < d.length) {
|
||||
throw new IllegalArgumentException("Numerator and denominator vectors must have the same size");
|
||||
}
|
||||
int nd = n.length - 1;
|
||||
int dd = d.length - 1;
|
||||
int index = 0;
|
||||
int[] tmpTab = createNewPoly(d, n.length);
|
||||
while (nd >= dd) {
|
||||
int factor = findFactor(n[nd], d[dd], mod);
|
||||
tmpTab = shr(tmpTab, index);
|
||||
polyMultiply(tmpTab, factor);
|
||||
|
||||
applyModuloToTab(tmpTab, mod);
|
||||
tmpTab = polySubtract(n, tmpTab);
|
||||
nd--;
|
||||
index++;
|
||||
}
|
||||
return Arrays.copyOf(n, index - 1);
|
||||
}
|
||||
|
||||
public static int[] NWD(int[] a, int[] b, int mod) {
|
||||
if (a.length <= 0 || b.length <= 0)
|
||||
return null;
|
||||
|
||||
while (b.length != 0) {
|
||||
int[] c = divide(a, b, mod);
|
||||
a = b;
|
||||
b = c;
|
||||
}
|
||||
return a;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user