From 07632aad9d3c7143e687bbb9ff11937e4d65492a Mon Sep 17 00:00:00 2001 From: Utkarsh1520 <72489678+Utkarsh1520@users.noreply.github.com> Date: Fri, 16 Oct 2020 21:00:00 +0530 Subject: [PATCH] Multiplication Of 3D Matrice. --- 3D.java | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 3D.java diff --git a/3D.java b/3D.java new file mode 100644 index 000000000..7344fbb45 --- /dev/null +++ b/3D.java @@ -0,0 +1,49 @@ +public static void main(String[] args) { + Scanner getIt = new Scanner(System.in); + System.out.println("Input 1 integer n: "); + int n = getIt.nextInt(); + if (n > 0){ + final double M[][][] = new double[n][n][n]; + for (int i = 0; i < n; i++) + for (int j = 0; j < n; j++) + for (int k = 0; k < n; k++) + M[i][j][k] = 3.0; + System.out.println("Input 1 integer p: "); + int p = getIt.nextInt(); + if(p > 0){ + for(int q = 1; q <= p; q++){ + for (int i = 0; i < n; i++){ + for (int j = 0; j < n; j++){ + for (int k = 0; k < n; k++){ + System.out.printf("%f ", Math.pow(matrixMult(M[i], M[i])[j][k], q)); + } + System.out.println("\n"); + } + + } + System.out.println("xxxxxxxxxxxxxxxxxxxxx"); + } + } + else{ + System.out.println("Woops, you entered a negative p."); + } + } + else{ + System.out.println("Woops, you entered a negative n."); + } +} + +public static double[][] matrixMult(double a[][], double b[][]) { + int aRows = a.length; + int aCols = a[0].length; + int bCols = b[0].length; + double[][] result = new double[aRows][bCols]; + for(int i = 0; i < aRows; i++){ + for(int j = 0; j < bCols; j++){ + for(int k = 0; k < aCols; k++){ + result[i][j] += a[i][k] * b[k][j]; + } + } + } + return result; +}