Docsity

Matrix Java Class (utils), Esercizi di Programmazione Orientata agli Oggetti

Toggle sidebar
bg1
package it.unibo.util;
import java.util.Arrays;
public class Matrix {
public static int getRows(double[][] mat) {
return mat.length;
}
public static int getColumns(double[][] mat) {
if(mat.length > 0) {
return mat[0].length;
}
else {
return 0;
}
}
public static boolean isRegular(double[][] mat) {
final int columns = getColumns(mat);
for(int i = 1; i < getRows(mat); i++) {
if(mat[i].length != columns) {
return false;
}
}
return true;
}
public static boolean isSquare(double[][] mat) {
return getRows(mat) == getColumns(mat);
}
public static boolean isDiagonal(double[][] mat) {
for(int i = 0; i < getRows(mat); i++) {
for(int j = 0; j < getRows(mat); j++) {
if(i != j && mat[i][j] != 0) {
return false;
}
}
}
return true;
}
public static boolean isIdentity(double[][] mat) {
for(int i = 0; i < getRows(mat); i++) {
for(int j = 0; j < getRows(mat); j++) {
if((i != j && mat[i][j] != 0) || (i == j && mat[i][j] != 1)) {
pf3
pf4
pf5

Anteprima parziale del testo

Scarica Matrix Java Class (utils) e più Esercizi in PDF di Programmazione Orientata agli Oggetti solo su Docsity!

package it.unibo.util; import java.util.Arrays; public class Matrix { public static int getRows(double[][] mat) { return mat.length; } public static int getColumns(double[][] mat) { if(mat.length > 0) { return mat[0].length; } else { return 0; } } public static boolean isRegular(double[][] mat) { final int columns = getColumns(mat); for(int i = 1; i < getRows(mat); i++) { if(mat[i].length != columns) { return false; } } return true; } public static boolean isSquare(double[][] mat) { return getRows(mat) == getColumns(mat); } public static boolean isDiagonal(double[][] mat) { for(int i = 0; i < getRows(mat); i++) { for(int j = 0; j < getRows(mat); j++) { if(i != j && mat[i][j] != 0) { return false; } } } return true; } public static boolean isIdentity(double[][] mat) { for(int i = 0; i < getRows(mat); i++) { for(int j = 0; j < getRows(mat); j++) { if((i != j && mat[i][j] != 0) || (i == j && mat[i][j] != 1)) {

return false; } } } return true; } public static boolean isSimmetric(double[][] mat) { for(int i = 0; i < getRows(mat); i++) { for(int j = 0; j < getRows(mat); j++) { if(mat[i][j] != mat[j][i]) { return false; } } } return true; } public static int size(double[][] mat) { if(!isRegular(mat)) { throw new IllegalArgumentException("An irregular matrix was passed!"); } else { return getRows(mat) * getColumns(mat); } } public static int rank(double[][] mat) { if(size(mat) < 1) { throw new IllegalArgumentException("Invalid Matrix!"); } else { return 1; } } public static String toString(double[][] mat) { StringBuilder result = new StringBuilder(""); for(int i = 0; i < getRows(mat); i++) { for(int j = 0; j < getColumns(mat); j++) { result.append(mat[i][j] + " "); } result.append("\n"); } return result.toString(); }

else { double[][] result = new double[getRows(mat1)][getColumns(mat2)]; int r = 0; for(int k = 0; k < getColumns(mat2); k++) { r = 0; for(int i = 0; i < getRows(mat1); i++) { for(int j = 0; j < getColumns(mat1); j++) { result[r][k] += mat1[i][j] * mat2[j][k]; } r++; } } return result; } } private static double[][] subMatrix(double[][] mat, int row, int column){ if(size(mat) < 1) { throw new IllegalArgumentException("Matrix must have length >= 1!"); } else { double[][] result = new double[getRows(mat) - 1][getColumns(mat) - 1]; int k = 0, m = 0; for(int i = 0; i < getRows(mat); i++) { if(i != row) { m = 0; for(int j = 0; j < getColumns(mat); j++) { if(j != column) { result[k][m] = mat[i][j]; m++; } } k++; } } return result; } } public static double[][] scalarMultiply(double[][] mat, double k){ if(size(mat) < 1) { throw new IllegalArgumentException("Matrix must have length >= 1!"); } else { double[][] result = new double[getRows(mat)][getColumns(mat)];

for(int i = 0; i < getRows(mat); i++) { for(int j = 0; j < getColumns(mat); j++) { result[i][j] = mat[i][j] * k; } } return result; } } public static double determinant(double[][] mat) { if((getRows(mat) != getColumns(mat)) || size(mat) < 1) { throw new IllegalArgumentException("Matrix must be squared!"); } else { if(size(mat) == 1) { return mat[0][0]; } else if(size(mat) == 4) { return mat[0][0] * mat[1][1] - mat[0][1] * mat[1][0]; } else { int i, j = getColumns(mat) / 2; double result = 0; for(i = 0; i < getRows(mat); i++) { result += Math.pow(-1, i + j) * mat[i][j] * determinant(subMatrix(mat, i, j)); } return result; } } } public static double[][] transpose(double[][] mat) { double[][] result = new double[getRows(mat)][getColumns(mat)]; if(!isSquare(mat)) { throw new IllegalArgumentException("Matrix must be square!"); } else { for(int i = 0; i < getRows(mat); i++) { for(int j = 0; j < getRows(mat); j++) { result[i][j] = mat[j][i]; } } return result; } }

result[rowIndex][j] = k; } return result; }/ private static double[][] addRows(double[][] mat, int sourceRow, int targetRow, double k){ double[][] result = copyOf(mat); for(int j = 0; j < getColumns(mat); j++) { result[targetRow][j] += k * mat[sourceRow][j]; } return result; } public static double trace(double[][] mat) { double result = 0; if(!isSquare(mat)) { throw new IllegalArgumentException("Matrix must be square!"); } else { for(int i = 0; i < getRows(mat); i++) { result += mat[i][i]; } return result; } } }