Newsgroups: comp.lang.apl
Path: watmath!watserv2.uwaterloo.ca!mach1!torn!utnut!cs.utexas.edu!sdd.hp.com!nigel.msen.com!spool.mu.edu!howland.reston.ans.net!noc.near.net!uunet!decwrl!csus.edu!sfsuvax1.sfsu.edu!vpcsc4
From: vpcsc4@sfsuvax1.sfsu.edu (Emmett McLean)
Subject: Nth power of 2 by 2
Message-ID: <1993Apr14.083011.28647@csus.edu>
Sender: news@csus.edu
Organization: San Francisco State University
References: <930414042235_70530.1226_CHV75-1@CompuServe.COM> <1993Apr14.072908.7593@alf.uib.no>
Date: Wed, 14 Apr 1993 08:30:11 GMT
Lines: 62


NB. Lewis Robinson points out in the April-May `93 APLBUG Newsletter 
NB. that there is an article by Willams in the December 1992 issue
NB. of Mathematics Magazine called "The nth Power of a 2 x 2 Matrix".
NB. Williams's formula for the solution, in ASCII mathematical notation is
NB. as follows:
NB. 
NB. Let a = eigenvalue derived by adding the square root in
NB.         the eigenvalue derivation
NB. 
NB.     b = eigenvalue derived by adding the square root in
NB.         the eigenvalue derivation
NB. 
NB.     G = matrix to take the power of
NB. 
NB.     I = identity matrix
NB.  
NB.     n = power
NB. 
NB.    Then
NB. 
NB.    G^n = (-/a-b)*((a^n)*(G-(b*I)) - (b^n)*(G-(a*I)))

NB. Lewis then codes the solution in APL.

NB. Here is a J solution. I couldn't beat J's power function.

   <' Emmett'

   sumInsert =. +/
   transpose =. |:
   sqrt   =. %:
   square =. *:
   determinant =. -/ .*
   right =. ]
   left =.  [
   identity =. =/~@i.@#
   reverse =. |.
   minusInsert =. -/

   a_plus_d =. sumInsert@((<0 1)&transpose)
   discriminate =. sqrt@(square@a_plus_d - 4&*@determinant)
   eigenvalues =. (%&2)@(a_plus_d + (, -)@discriminate) 
   coefficients =.  ((right ^"0 left) % minusInsert@]) eigenvalues@]
   matrices =. right -"2 reverse@(identity *"2 0 right)@eigenvalues
   nthPower =. -/@(coefficients * matrices@right)
   
   N =. 2 2 $  1 1 1 0
   NB. this value is N generates Fibonacci's numbers
   
   11 nthPower N
144 89
 89 55
   N&(+/ .*) ^: (10) N
144 89
 89 55

   (20) test '(71) nthPower N'
0.0385 1408
   (20) test 'N&(+/ .*) ^: (70) N'
0.031 1492

