Ti posto un po' del codice a cui avevo cominciato a lavorare a tempo perso
codice:
package math.matrix;
import math.matrix.exceptions.*;
/**
*
* @author Andrea
*/
public class Matrix {
private double[][] matrix;
/** Creates a new instance of Matrix */
public Matrix(double[][] matrix) {
this.matrix = matrix;
}
public Matrix(Matrix m) {
this.matrix = m.matrix;
}
public Matrix (int dim) {
matrix = new double[dim][dim];
for (int i=0; i < dim; i++) {
for (int j = 0; j < dim; j++) {
matrix[i][j] = 0;
}
}
}
public Matrix (int rows, int cols, int elem) {
matrix = new double[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
matrix[i][j] = elem;
}
}
}
public Matrix suppressRow(int row) throws MatrixRowCountException {
if (row >= matrix.length) {
throw new MatrixRowCountException("Row index out of range");
}
else {
double[][] m = new double[matrix.length -1][matrix[0].length];
int k = 0;
for (int i = 0; i < m.length && k < matrix.length; i++) {
if (row != k) {
for (int j = 0; j < matrix[0].length; j++) {
m[i][j] = matrix[k][j];
}
k++;
}
else {
k++;
i--;
}
}
return new Matrix(m);
}
}
public Matrix suppressColumn(int col) throws MatrixColumnCountException {
if (col >= matrix[0].length) {
throw new MatrixColumnCountException("Row index out of range");
}
else {
double[][] m = new double[matrix.length][matrix[0].length - 1];
int k = 0;
for (int j = 0; j < m[0].length && k < matrix[0].length; j++) {
if (col != k) {
for (int i = 0; i < matrix.length; i++) {
m[i][j] = matrix[i][k];
}
k++;
}
else {
k++;
j--;
}
}
return new Matrix(m);
}
}
public Matrix computeMinor (int row, int col) throws MatrixRowCountException, MatrixColumnCountException {
return this.suppressColumn(col).suppressRow(row);
}
public String toString() {
StringBuffer buf = new StringBuffer();
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
buf.append(matrix[i][j]+"\t");
}
buf.append("\n");
}
return buf.toString();
}
public static void main (String[] args) {
Matrix myMatrix = new Matrix(new double[][] {
{1,2,3,5,6},
{4,5,6,7,8},
{7,8,9,10,11},
{10,11,12,13,14},
{13,14,15,16,17,18}
});
System.out.println(myMatrix);
try {
Matrix myMatrix3 = myMatrix.computeMinor(1,2);
System.out.println("\n\n\nNew Matrix\n"+myMatrix3);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
Lascia stare le exception personalizzate e l'eventuale impacchettamento e imports... la sostanza sta nei metodi per sopprimere riga e colonna. L'altro vien da sè.