public static double determinant(double m[][], int dim) {
		double subMatrix[][];
		double result;

		if (dim == 1)
			result = m[0][0];
		else if (dim == 2)
			result = m[0][0] * m[1][1] - m[1][0] * m[0][1];

		else {
			result = 0;
			for (int p = 0; p < dim; p++) {
				subMatrix = new double[dim - 1][];
				for (int r = 0; r < (dim - 1); r++)
					subMatrix[r] = new double[dim - 1];
				for (int i = 1; i < dim; i++) {
					int count = 0;
					for (int j = 0; j < dim; j++) {
						if (j == p)
							continue;
						subMatrix[i - 1][count] = m[i][j];
						count++;
					}
				}
				result += Math.pow(-1.0, 1.0 + p + 1.0) * m[0][p]
						* determinant(subMatrix, dim - 1);
			}
		}
		return result;
	}