
Dodawanie macierzy (Java)
Aby dodać dwie macierze dodajemy do siebie ich odpowiadające sobie elementy i tworzymy trzecią macierz. Obie macierze muszą być tego samego stopnia.
+
=
Do wykonania dodawania możemy użyć metod:
add(Matrix) (Przykład w klasie Matrix011) oraz
add2(Matrix) (Przykład w klasie Matrix012) z klasy Matrix.
Matrix011
double[] array1 = {1, 2, 3, 4, 5, 6, 7, 8, 9};
Matrix matrix1 = new Matrix(array1, 3);
ArrayUtil.print(array1);
double[] array2 = {9, 8, 7, 6, 5, 4, 3, 2, 1};
Matrix matrix2 = new Matrix(array2, 3);
ArrayUtil.print(array2);
try {
matrix1.add(matrix2);
} catch (MatrixException e) {
e.printStackTrace();
}
matrix1.printToConsole();
Po uruchomieniu klasy na konsoli zobaczymy:
[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0] [9.0, 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0] 10.0 10.0 10.0 10.0 10.0 10.0 10.0 10.0 10.0
Matrix012
double[] array1 = {1, 2, 3, 4, 5, 6, 7, 8, 9};
Matrix matrix1 = new Matrix(array1, 3);
ArrayUtil.print(array1);
double[] array2 = {9, 8, 7, 6, 5, 4, 3, 2, 1};
Matrix matrix2 = new Matrix(array2, 3);
ArrayUtil.print(array2);
Matrix matrix3 = null;
try {
matrix3 = matrix1.add2(matrix2);
} catch (MatrixException e) {
e.printStackTrace();
}
matrix3.printToConsole();
Po uruchomieniu klasy na konsoli zobaczymy:
[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0] [9.0, 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0] 10.0 10.0 10.0 10.0 10.0 10.0 10.0 10.0 10.0
Pliki do ściągnięcia
Moduł matrices – aktualny stan projektu = 006;
