/* SchematHornera.java w(x) = 1 + 2x + 3x^2 + 4x^3 w(x) = 1 + x(2 + 3x + 4x^2) w(x) = 1 + x(2 + x(3 + 4x)) w(x) = 1 + x(2 + x(3 + x4)) s0 = 4 s1 = 3 + x * s0 s2 = 2 + x * s1 s3 = 1 + x * s2 */ public class SchematHornera { public static void main(String[] args) { double[] a = {1,2,3,4}; // 1 + 2x + 3x^2 + 4x^3 double x = 0.5; double suma = a[a.length - 1]; for (int i = a.length - 2; i >= 0; i--) { suma = a[i] + x*suma; } System.out.println("w(" + x + ") = " + suma); } }
w(0.5) = 3.25 Press any key to continue...
Zadanie Dokonaj analizy programu SchematHornera.
a = {1.0,2.0,3.0,4.0} x = 0.5 suma = a[4 - 1] = a[3] = 4.0 i = 2 2 >= 0 suma = a[2] + 0.5*4.0 = 3.0 + 2.0 = 5.0 i = 1 1 >= 0 suma = a[1] + 0.5*5.0 = 2.0 + 2.5 = 4.5 i = 0 0 >= 0 suma = a[0] + 0.5*4.5 = 1.0 + 2.25 = 3.25 i = -1 -1 >= 0 false "w(0.5) = 3.25"
/* SumaWielomianow.java 1 + 2x + 3x^2 + 2 + 4x --------------- 3 + 6x + 3x^2 */ public class SumaWielomianow { public static void main(String[] args) { double[] w1 = {1, 2, 3}; // 1 + 2x + 3x^2 double[] w2 = {2, 4}; // 2 + 4x double[] suma = suma(w1, w2); // 3 + 6x + 3x^2 for (int i = 0; i < suma.length; i++) System.out.print(suma[i] + " "); System.out.println(); } static double[] suma(double[] w1, double[] w2) { int max = Math.max(w1.length, w2.length); double[] suma = new double[max]; for (int i = 0; i < max; i++) { double wsp1 = i < w1.length ? w1[i] : 0; double wsp2 = i < w2.length ? w2[i] : 0; suma[i] = wsp1 + wsp2; } return suma; } }
3.0 6.0 3.0 Press any key to continue...
Zadanie Dokonaj analizy programu SumaWielomianow.
w1 = {1.0, 2.0, 3.0} w2 = {2.0, 4.0} suma = suma(w1, w2) = {3.0, 6.0, 3.0} * suma(w1, w2) = {3.0, 6.0, 3.0} max = max(3,2) = 3 suma = {0.0, 0.0, 0.0} i = 0 0 < 3 wsp1 = (0 < 3 ? w1[0] : 0) = 1.0 wsp2 = (0 < 2 ? w2[0] : 0) = 2.0 suma[0] = 1.0 + 2.0 = 3.0 i = 1 1 < 3 wsp1 = (1 < 3 ? w1[1] : 0) = 2.0 wsp2 = (1 < 2 ? w2[1] : 0) = 4.0 suma[1] = 2.0 + 4.0 = 6.0 i = 2 2 < 3 wsp1 = (2 < 3 ? w1[2] : 0) = 3.0 wsp2 = (2 < 2 ? w2[2] : 0) = 0.0 suma[2] = 3.0 + 0.0 = 3.0 i = 3 3 < 3 false return suma = {3.0, 6.0, 3.0} i = 0 0 < 3 '3.0 ' i = 1 1 < 3 '6.0 ' i = 2 2 < 3 '3.0 ' i = 3 3 < 3 false ""
/* IloczynWielomianow.java i j (1 + 2x + 3x^2) * (2 + 4x) = 1*2 + 1*4x + 2*2x + 2*4x^2 + 3*2x^2 + 3*4x^3 = 2 + 8x + 14x^2 + 12x^3 */ public class IloczynWielomianow { public static void main(String[] args) { double[] w1 = {1, 2, 3}; // 1 + 2x + 3x^2 double[] w2 = {2, 4}; // 2 + 4x double[] iloczyn = iloczyn(w1, w2); // 2 + 8x + 14x^2 + 12x^3 for (int i = 0; i < iloczyn.length; i++) System.out.print(iloczyn[i] + " "); System.out.println(); } static double[] iloczyn(double[] w1, double[] w2) { int st = w1.length + w2.length - 1; double[] iloczyn = new double[st]; for (int i = 0; i < w1.length; i++) { for (int j = 0; j < w2.length; j++) { iloczyn[i+j] = iloczyn[i+j] + w1[i]*w2[j]; } } return iloczyn; } }
2.0 8.0 14.0 12.0 Press any key to continue...
Zadanie Dokonaj analizy programu IloczynWielomianow.
w1 = {1.0, 2.0, 3.0} w2 = {2.0, 4.0} iloczyn = iloczyn(w1, w2) = {2.0, 8.0, 14.0, 12.0} * iloczyn(w1, w2) = {2.0, 8.0, 14.0, 12.0} st = 3 + 2 - 1 = 4 iloczyn = {0.0, 0.0, 0.0, 0.0} i = 0 0 < 3 j = 0 0 < 2 iloczyn[0+0] = iloczyn[0] + w1[0]*w2[0] = 0.0 + 1.0*2.0 = 2.0 j = 1 1 < 2 iloczyn[0+1] = iloczyn[1] + w1[0]*w2[1] = 0.0 + 1.0*4.0 = 4.0 j = 2 2 < 2 false i = 1 1 < 3 j = 0 0 < 2 iloczyn[1+0] = iloczyn[1] + w1[1]*w2[0] = 4.0 + 2.0*2.0 = 8.0 j = 1 1 < 2 iloczyn[1+1] = iloczyn[2] + w1[1]*w2[1] = 0.0 + 2.0*4.0 = 8.0 j = 2 2 < 2 false i = 2 2 < 3 j = 0 0 < 2 iloczyn[2+0] = iloczyn[2] + w1[2]*w2[0] = 8.0 + 3.0*2.0 = 14.0 j = 1 1 < 2 iloczyn[2+1] = iloczyn[3] + w1[2]*w2[1] = 0.0 + 3.0*4.0 = 12.0 j = 2 2 < 2 false i = 3 3 < 3 false return iloczyn = {2.0, 8.0, 14.0, 12.0} i = 0 0 < 4 '2.0 ' i = 1 1 < 4 '8.0 ' i = 2 2 < 4 '14.0 ' i = 3 3 < 4 '12.0 ' i = 4 4 < 4 false ""
/* IlorazWielomianow.java 3x^2 + 0.5x + 1.5 {1.5, 0.5, 3.0} ---------------------- ---------------------- 12x^3 + 14x^2 + 8x + 2 : 4x + 4 {2.0, 8.0, 14.0, 12.0} : {4.0, 4.0} -(12x^3 + 12x^2) -(12.0, 12.0) ---------------------- ------------ 2x^2 + 8x + 2 {2.0, 8.0, 2.0, 0.0} -(2x^2 + 2x) -(2.0, 2.0) ------------- ----------- 6x + 2 {2.0, 6.0, 0.0, 0.0} -(6x + 6) -(6.0, 6.0) ------ ---------- - 4 {-4.0, 0.0, 0.0, 0.0} */ public class IlorazWielomianow { public static void main(String[] args) { double[] w1 = {2.0, 8.0, 14.0, 12.0}; // 2 + 8x + 14x^2 + 12x^3 double[] w2 = {4.0, 4.0}; // 4 + 4x double[][] wynik = iloraz(w1, w2); // 1.5 + 0.5x + 3x^2, -4 double[] iloraz = wynik[0]; double[] reszta = wynik[1]; System.out.print("iloraz: "); for (int j = 0; j < iloraz.length; j++) System.out.print(iloraz[j] + " "); System.out.println(); System.out.print("reszta: "); for (int j = 0; j < w2.length - 1; j++) System.out.print(reszta[j] + " "); System.out.println(); } static double[][] iloraz(double[] w1, double[] w2) { int st = w1.length - w2.length + 1; double[] iloraz = new double[st]; double[] reszta = w1.clone(); for (int k = iloraz.length - 1, i = w1.length - 1; k >= 0; k--, i--) { iloraz[k] = reszta[i] / w2[w2.length-1]; for (int j = w2.length - 1; j >= 0; j--) { reszta[k + j] = reszta[k + j] - iloraz[k]*w2[j]; } } return new double[][]{iloraz, reszta}; } }
iloraz: 1.5 0.5 3.0 reszta: -4.0 Press any key to continue...
Zadanie Dokonaj analizy programu IlorazWielomianow.
w1 = {2.0, 8.0, 14.0, 12.0} w2 = {4.0, 4.0} wynik = iloraz(w1, w2) = {{1.5, 0.5, 3.0}, {-4.0, 0.0, 0.0, 0.0}} * iloraz(w1, w2) = {{1.5, 0.5, 3.0}, {-4.0, 0.0, 0.0, 0.0}} st = 3 - 1 + 1 = 3 iloraz = {0.0, 0.0, 0.0} reszta = {2.0, 8.0, 14.0, 12.0} k = 2 i = 3 2 >= 0 iloraz[2] = reszta[3] / w2[1] = 12.0 / 4.0 = 3.0 // {0.0, 0.0, 3.0} j = 1 1 >= 0 reszta[2 + 1] = reszta[3] - iloraz[2]*w2[1] = 12.0 - 3.0*4.0 = 0.0 j = 0 // {2.0, 8.0, 14.0, 0.0} 0 >= 0 reszta[2 + 0] = reszta[2] - iloraz[2]*w2[0] = 14.0 - 3.0*4.0 = 2.0 j = -1 // {2.0, 8.0, 2.0, 0.0} -1 >= 0 false k = 1 i = 2 1 >= 0 iloraz[1] = reszta[2] / w2[1] = 2.0 / 4.0 = 0.5 // {0.0, 0.5, 3.0} j = 1 1 >= 0 reszta[1 + 1] = reszta[2] - iloraz[1]*w2[1] = 2.0 - 0.5*4.0 = 0.0 j = 0 // {2.0, 8.0, 0.0, 0.0} 0 >= 0 reszta[1 + 0] = reszta[1] - iloraz[1]*w2[0] = 8.0 - 0.5*4.0 = 6.0 j = -1 // {2.0, 6.0, 0.0, 0.0} -1 >= 0 false k = 0 i = 1 0 >= 0 iloraz[0] = reszta[1] / w2[1] = 6.0 / 4.0 = 1.5 // {1.5, 0.5, 3.0} j = 1 1 >= 0 reszta[0 + 1] = reszta[1] - iloraz[0]*w2[1] = 6.0 - 1.5*4.0 = 0.0 j = 0 // {2.0, 0.0, 0.0, 0.0} 0 >= 0 reszta[0 + 0] = reszta[0] - iloraz[0]*w2[0] = 2.0 - 1.5*4.0 = -4.0 j = -1 // {-4.0, 0.0, 0.0, 0.0} -1 >= 0 false return {{1.5, 0.5, 3.0}, {-4.0, 0.0, 0.0, 0.0}} iloraz = wynik[0] = {1.5, 0.5, 3.0} reszta = wynik[1] = {-4.0, 0.0, 0.0, 0.0} "iloraz: 1.5 0.5 3.0" "reszta: -4.0"