Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Help with Fast Matrix Exponentiation

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

    	import java.io.*;
    	import java.util.Scanner;
    	public class Matrix {
    	  static int[][] mult(int[][] X, int[][] Y) {
    	  int Z[][] = new int[2][2];
    	  Z[0][0] = X[0][0]*Y[0][0] + X[0][1]*Y[1][0];
    	  Z[0][1] = X[0][0]*Y[0][1] + X[0][1]*Y[1][1];
    	  Z[1][0] = X[1][0]*Y[0][0] + X[1][1]*Y[0][1];
    	  Z[1][1] = X[1][0]*Y[1][0] + X[1][1]*Y[1][1];
    	  return Z;
    	  }
    	  public static void main(String argv[]) {
    	    // create a scanner for interactive input
    	    Scanner scin = new Scanner(System.in);
    	    int matrix[][] = new int[2][2];
    	    int B[][] = new int[2][2];
    	    int A[][] = new int[2][2];
    	    int k,n;
    	    // Ask for the matrix
    	    System.out.print("[ M[0][0] M[0][1] ] =  ");
    	    matrix[0][0] = scin.nextInt(); matrix[0][1] = scin.nextInt();
    	    System.out.print("[ M[1][0] M[1][1] ] =  ");
    	    matrix[1][0] = scin.nextInt(); matrix[1][1] = 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[0][0]=1;C[0][1]=2;
    	    		C[1][0]=3;C[1][1]=4;
    	      while(m<n)
    	      {
    	    	  if(m==n)  
    	    	  {
    	    		  break;
    	    	  }
    	    	  m++;
     
    	    	  A[0][0] = (C[0][0]*matrix[0][0])+(C[1][0]*matrix[0][1]); A[0][1] = (C[0][0]*matrix[0][1])+(C[0][1]*matrix[1][1]);
    	    	  A[1][0] = (C[1][0]*matrix[0][0])+(C[1][1]*matrix[1][0]); A[1][1] = (C[1][0]*matrix[0][1])+(C[1][1]*matrix[1][1]);
     
    	    	  C[0][0]=A[0][0];C[0][1]=A[0][1];
    	    	  C[1][0]=A[1][0];C[1][1]=A[1][1];
     
     
     
    	      }
    	    }
    	    while(k > 0) 
    	    {
    	      B= mult(B,B);
    	      if ((k%1) == 1) A = mult(A,B);
    	      k = k/2;
    	}
    	System.out.println( A[0][0] + "  " + A[0][1]);
    	  System.out.println( A[1][0] + "  " + A[1][1]);
    	}
    	}


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.
    Last edited by Norm; September 18th, 2012 at 01:06 PM.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Aug 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

Similar Threads

  1. Sending data too fast
    By polyfrag in forum Java Networking
    Replies: 1
    Last Post: August 10th, 2012, 04:28 PM
  2. need HELP FAST PLEASE HELP ME
    By britben95 in forum Member Introductions
    Replies: 1
    Last Post: November 14th, 2011, 08:05 PM
  3. need help fast
    By coke32 in forum Loops & Control Statements
    Replies: 6
    Last Post: October 31st, 2011, 11:04 PM
  4. need help fast!!
    By nwollis in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 27th, 2010, 05:12 PM
  5. Code stopping, need help fast.
    By aussiemcgr in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 11th, 2010, 09:00 AM