# Help with Fast Matrix Exponentiation

• September 18th, 2012, 12:09 PM
asundar
Help with Fast Matrix Exponentiation
hey everyone,

I feel like I am close to the answer, but I'm just missing something little. What I'm supposed to do, basically, is come up with a program that allows me to increase matrices exponentially. My problem lies when I want to do anything higher than a power of two. For example, when I raise it to the power of 3, it does it by 4 instead. If I do it by 4, it does it by 6. Can anyone help me? I feel like it's something small that I am overlooking, but I am not able to figure it out.

Code :

``` import java.io.*; import java.util.Scanner; public class Matrix { static int[][] mult(int[][] X, int[][] Y) { int Z[][] = new int; Z = X*Y + X*Y; Z = X*Y + X*Y; Z = X*Y + X*Y; Z = X*Y + X*Y; return Z; } public static void main(String argv[]) { // create a scanner for interactive input Scanner scin = new Scanner(System.in); int matrix[][] = new int; int B[][] = new int; int A[][] = new int; int k,n; // Ask for the matrix System.out.print("[ M M ] = "); matrix = scin.nextInt(); matrix = scin.nextInt(); System.out.print("[ M M ] = "); matrix = scin.nextInt(); matrix = scin.nextInt(); System.out.print("n = "); n = scin.nextInt(); k = n; B = matrix; if ((k==1)) { A = matrix; } else { int m=1; int C[][]=matrix; C=1;C=2; C=3;C=4; while(m<n) { if(m==n) { break; } m++;   A = (C*matrix)+(C*matrix); A = (C*matrix)+(C*matrix); A = (C*matrix)+(C*matrix); A = (C*matrix)+(C*matrix);   C=A;C=A; C=A;C=A;       } } while(k > 0) { B= mult(B,B); if ((k%1) == 1) A = mult(A,B); k = k/2; } System.out.println( A + " " + A); System.out.println( A + " " + A); } }```
• September 18th, 2012, 01:01 PM
Norm
Re: Help with Fast Matrix Exponentiation
Can you post the program's output, explain what is wrong with it and show what the output should be?

Also please post pseudo code (or a detailed description) for the algorithm that the code is supposed to be following.

NOTE: This code has a poor coding style: putting multiple statements on the same line.
• September 18th, 2012, 02:41 PM
asundar
Re: Help with Fast Matrix Exponentiation
Ignore this, I found out what had happened from someone in my class about two hours later. Sorry for prematurely posting.