Multiplying two 3×3 matrices

 
class Demo{
	static double mulp(double[] row, double[] col){
		double res = 0;
		for (int i = 0; i<3; i++){
			double prod = row[i]*col[i];
			res += prod;
		}
		return res;
	}
	static  double[] row_matx(double[][] matx, int index){
		return matx[index];
	}
	static double[] col_matx(double[][] matx, int index) {
		double[] col = new double[3];
		for (int i = 0; i<3; i++){
			col[i] = matx[i][index];
		}
		return col;
	}
	static double[][] matx_mulp(double[][] matx1, double[][] matx2){
		double[][] matx3 = new double[3][3];
		for (int i =0; i<3; i++) {
			for (int j = 0; j<3; j++){
				matx3[i][j] = mulp(row_matx(matx1, i), col_matx(matx2, j));
			}
		}
		return matx3;
	}
	static void print_matx(double[][] matx){
		for (int i = 0; i<3; i++){
			for (int j = 0; j<3; j++) {
				System.out.print(String.valueOf(matx[i][j]) + "  ");
			}
			System.out.println();
		}
	}
	public static void main(String[] args){
		double[][] matx1 = {
			{1,2,3},
			{2,4,3},
			{2,6,3}};
		double[][] matx2 = {
			{1,2,3},
			{2,4,3},
			{2,6,3}};
		double[][] matx3 = matx_mulp(matx1, matx2);
		print_matx(matx3);
	}
}
 

Comments