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 5 of 5

Thread: Fibonacci with matrixes and recursive powers

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Fibonacci with matrixes and recursive powers

    m[][]=AlgoritmiMatrici.identita(2);-->it generates the 2x2 identity matrix
    AlgoritmiMatrici.prodotto(m,m);-->is the result of the multiplication between m and m

    This code should calculate the umteenth number of Fabionacci series, but it alwyas returns 1.

    Can anyone help me?

    public static int matrice1(int n)
      {
      	int m[][]=AlgoritmiMatrici.identita(2);
     
      	potenzamatrice(m, n-1);
     
      	return m[0][0];
      }
      public static void potenzamatrice(int[][] m, int n)
      {
        	if(n>1)
    	{
    		potenzamatrice(m, n/2);
    		m=AlgoritmiMatrici.prodotto(m,m);
    	}	
    	if(n%2==1)
    	{
    		int a[][]={{1,1},{1,0}};
    		m=AlgoritmiMatrici.prodotto(m,a);
    	}
     }


  2. #2
    Member goldest's Avatar
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    63
    Thanks
    1
    Thanked 12 Times in 10 Posts

    Default Re: Fibonacci with matrixes and recursive powers

    Hey Enrico,

    Can you please post what your prodotto method does? Seems like your multi dimensional integer array is getting filled after the call to prodotto method i.e AlgoritmiMatrici.prodotto(m,m);.

    From the code, seems like what you are returning is the 1st element of your 1st array [return m[0][0];]. So may be there is a possibility that your prodotto method is populating the values in such a way that it always happens to be ''1" at the first element.

    I hope you are getting me.

    Goldest
    Java Is A Funny Language... Really!

    Sun: Java Coding Conventions

    Click on THANKS if you like the solution provided.
    Click on Star if you are really impressed with the solution.

  3. #3
    Junior Member
    Join Date
    Dec 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Fibonacci with matrixes and recursive powers

    Here you are.
    It returns a matrix which is the multiplication between two matrixes

    public static int [][] prodotto(int t1[][],int t2[][]) {
    	//it is possible only if t1[0].length = t2.length
    	int n = t1.length;
    	int m = t2[0].length;
    	int prod[][] = new int [n][m];
    	for (int i=0 ; i<n; i++)
    		for (int k=0; k<m ; k++){
    			int s=0;			
    			for (int r=0;r<t1[0].length;r++)
    				s=s+t1[i][r]*t2[r][k];
    			prod[i][k]=s;
     
    		}//end for
     
    	 return prod;
    	}//fine prodotto
    Last edited by Enrico123; December 27th, 2010 at 02:52 PM.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Fibonacci with matrixes and recursive powers

    The array passed to one of the functions is being recreated and the caller expects the change to take effect on the passed object, which does not happen in java. Take the following as an example:
    	public static void main(String[] args){
    		int[] m = new int[10];
    		for ( int i = 0; i < 10; i++ ){
    			m[i] = -1;
    		}
     
    		test(m);
    		System.out.println(m[0]);
    	}
     
    	public static void test(int[] m){
    		int v[] = new int[10];
    		for ( int i = 0; i < m.length; i++ ){
    			v[i] = 1;
    		}
    		m = v;
    	}
    Will print -1

  5. #5
    Junior Member
    Join Date
    Dec 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Fibonacci with matrixes and recursive powers

    Ok.
    I tried to return the value of the matrix after the product, but the result is always the same...

Similar Threads

  1. [SOLVED] StackOverflowError with recursive method
    By samfin in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 2nd, 2010, 04:05 PM
  2. Program to compute powers
    By Shyamz1 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 25th, 2010, 04:51 PM
  3. Fibonacci Spiral Help?
    By cmh0114 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 12th, 2010, 09:21 PM
  4. Replies: 1
    Last Post: October 19th, 2009, 11:53 PM
  5. [SOLVED] Problem in generating Fibonacci sequence in java
    By big_c in forum Algorithms & Recursion
    Replies: 2
    Last Post: April 24th, 2009, 08:52 AM