I need to solve a 3x3 matrix, x,y,z of a funtion (I only have a 2x2 matrix determinant solved)
I have figured out how to solve the determinant with this code and I have created another attached program that displays any size matrix
 I thought it might help.
public class matrix1 {
        public int determinant(int[][] arr) {
                int result = 0;
                if (arr.length == 1) {
                   result = arr[0][0];
                   return result;
                }
                if (arr.length == 2) {
                   result = arr[0][0] * arr[1][1] - arr[0][1] * arr[1][0];
                   return result;
                }
                for (int i = 0; i < arr[0].length; i++) {
                        int temp[][] = new int[arr.length - 1][arr[0].length - 1];

                        for (int c = 1; c < arr.length; c++) {
                        for (int a = 0; a < arr[0].length; a++) {

                        if (a < i) {
                             temp[c - 1][a] = arr[c][a];
                              } else if (a > i) {
                           temp[c - 1][a - 1] = arr[c][a];
                  }
                 }
               }
               result += arr[0][i] * Math.pow(-1, (int) i) * determinant(temp);
                }
               return result;
        }
}
public class matrix1app {

		 public static void main(String[] args) {
             int array[][] = { { 2, 3 }, { 6, 4 } };
             matrix1 d = new matrix1();
             int result = d.determinant(array);
             System.out.println(result);
     

	}

}
Member Avatar for ztini

I think you are confused about what a 2x2 and a 3x3 matrix is in Java.

int[5] = { 0, 1, 2, 3, 4 };

int[5][5] = {
{ 0, 1, 2, 3, 4 }
{ 0, 1, 2, 3, 4 }
{ 0, 1, 2, 3, 4 }
{ 0, 1, 2, 3, 4 }
{ 0, 1, 2, 3, 4 } };

int[5][5][5] = imagine the data represented as a cube.

int[5][5][5][5] = linear line of 5 cubes

etc.

commented: Helped me understand and clarify my problem +0

So if I did arr[3][3] it would become a 3x3 matrix

Member Avatar for ztini

Correct

Oksy so I took what you told me an created a new program that tells me the x,y,z values of a matrix using whats called cramers rule, but for some reason it won't solve the matrix for me.

public class CramersRule {

	private int[][] x1,y1,z1;
	int[] d;
	int d1,d2,d3,d4,d5,d6;
	int determinant;
	
	public CramersRule(){
	int x;
	int	y=3;
	int	z=3;

		
		d = new int[3];
		int[][] matrix = new int[y][z];
		 x1 = new int [y][z];
		 y1 = new int [y][z];
		z1 = new int [y][z];
		
		matrix[0][0] = 2;
		matrix[0][1] = 1;
		matrix[0][2] = 1;
		d[0] = 3;
		matrix[1][0] = 1;
		matrix[1][1] = -1;
		matrix[1][2] = -1;
		d[1] = 0;
		matrix[2][0] = 1;
		matrix[2][1] = 2;
		matrix[2][2] = 1;
		d[2]=0;
		

		
		for(int i=1; i<= matrix.length;i++)
			for(int j=1; j<= matrix.length;j++){
				x1[i][j] = matrix[i][j];
				y1[i][j] = matrix[i][j];
				z1[i][j] = matrix[i][j];

			}
		for(int i = 0; i <matrix.length;i++)
			x1[i][0] = d[i];
		for(int i = 0; i <matrix.length;i++)
			x1[i][0] = d[i];
		for(int i = 0; i <matrix.length;i++)
			x1[i][0] = d[i];
		x = find(x1)/find(matrix);
		y = find(y1)/find(matrix);
		z = find(z1)/find(matrix);
		
		
	}
	
	
	public int find(int[][] matrix){
		d1 = matrix[0][0] * matrix[0][1] * matrix[0][2];
		d2 = matrix[0][1] * matrix[0][2] * matrix[0][0];
		d3 = matrix[0][2] * matrix[0][0] * matrix[0][1];
		d4 = matrix[0][0] * matrix[0][2] * matrix[0][1];
		d5 = matrix[0][1] * matrix[0][0] * matrix[0][2];
		d6 = matrix[0][2] * matrix[0][1] * matrix[0][0];
		determinant = d1+d2+d3-d4-d5-d6;
return determinant;



	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	public String toString(){
		return determinant;
	}

}
Member Avatar for ztini

The issue you are having is that you don't switch the constants with the original determinant. The logic looks like this:

D = [n n n		C = [x1
	     n n n	             x2
	     n n n]		     x3]
	     
	Dx = [x1 n n
	      x2 n n
	      x3 n n]
	
	Dy = [n x1 n
	      n x2 n
	      n x3 n]
	
	Dz = [n n x1
	      n n x2
	      n n x3]

Then you take each cell in the first row and multiply it by the sum of the product diagonals of the 2x2 grid that does not intersect the cell's row/column.

Here is an example:

D = [4 -1 1
	     2  2 3
	     5 -2 6]
	     
	int i =  4 * ((2 *  6) + (3 * -2));
	int j = -1 * ((2 *  6) + (3 *  5));
	int k =  1 * ((2 * -2) + (2 *  5));
	
	int d = i - j + k;

So basically, this is what you want:

public class CramersRule {

	private static int[] C;
	private static int[][] D;
	
	public static int[] calculate(int[][] determinant, int[] constants) {
	
		CramersRule.D = determinant;
		CramersRule.C = constants;
		int[] solution = new int[3];
		int d  = switchColumn();
		
		if (d == 0) return null;  // no solution
		
		switchConstants(0); 
		solution[0] = switchColumn() / d;  // dx
		
		switchConstants(0);
		switchConstants(1);
		solution[1] = switchColumn() / d;  // dy

		switchConstants(1);
		switchConstants(2);
		solution[2] = switchColumn() / d;  // dz
		
		return solution;
	}
	
	private static void switchConstants(int column) {
		for (int i = 0; i < D.length; i++) {
			int temp = D[i][column];
			D[i][column] = C[i];
			C[i] = temp;
		}
	}

	private static int switchColumn() {
		return
			D[0][0] * (D[1][1] * D[2][2] - D[1][2] * D[2][1]) -  
			D[0][1] * (D[1][0] * D[2][2] - D[1][2] * D[2][0]) +
			D[0][2] * (D[1][0] * D[2][1] - D[1][1] * D[2][0]);
	}

	public static void print(int[] solution) {
		System.out.println();
		System.out.println("Solution: " + 
				"(" + solution[0] + ", " +
				solution[1] + ", " + solution[2] + ")");
	}
	
	public static void main(String[] args) {
		System.out.println("Solving: ");
		System.out.println("4x - y + z = -5");
		System.out.println("2x + 2y + 3z = 10");
		System.out.println("5x – 2y + 6z = 1");
		
		int[][] determinant = {{4, -1, 1},
							   {2,  2, 3},
							   {5, -2, 6}};
			
		int[] constants = {-5, 10, 1};
		
		print(CramersRule.calculate(determinant, constants));
	}
}

Console:

Solving: 
4x - y + z = -5
2x + 2y + 3z = 10
5x – 2y + 6z = 1

Solution: (-1, 3, 2)

Thank you very much for clearing that up and helping me understand. The switch statement made it much easier than the way I was doing it. I would of never thought of using it. Thanks again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.