Search code examples
javaalgorithmalgebra

Adjacent Neighbor Summation


If I am given the numbers [1,2,1] and I want to find the sum of each number with it's adjacent neighbors (in a ring) repeated a number of times. I can use the following formula:

base case: [x=1, y=2, z=1]
repeat 7 times.
staring with index 0 or (variable x):
round 1 index 0: [(x+y+z), y, z] == [4, 2, 1]
round 2 index 1: [(x+y+z), (x+2y+2z), z] == [4, 7, 1]
round 3 index 2: [(x+y+z), (x+2y+2z), (2x+3y+4z)] == [4, 7, 12]
round 4 index 0: [(4x+6y+7z), (x+2y+2z), (2x+3y+4z)] == [23, 7, 12]
round 5 index 1: [(4x+6y+7z), (7x+11y+13z), (2x+3y+4z)] == [23, 42, 12]
round 6 index 2: [(4x+6y+7z), (7x+11y+13z), (13x+20y+24z)] == [23, 42, 77]
round 7 index 0: [(24x+37y+24z), (7x+11y+13z), (13x+20y+24z)] == [142, 42, 77]

Since the sequence can be repeated millions of times, I am wondering how I can calculate the i-th round without calculating rounds 0 through i. Any advice would be appreciated.


Solution

  • This question should be migrated to math.stackexchange.com, but:

    If we call X[n] the column vector (x[n],y[n],z[n]) were the "n", the "time" index means a full round, we get the relation X(n+1) = A X(n) where A is the matrix

           1  1  1
      A =  1  2  2
           2  3  4
    

    and hence X(n) = A^n X(0)