Skip to content

Instantly share code, notes, and snippets.

@isomorphisms
Created August 27, 2014 07:12
Show Gist options
  • Save isomorphisms/737779f08d78788a0c15 to your computer and use it in GitHub Desktop.
Save isomorphisms/737779f08d78788a0c15 to your computer and use it in GitHub Desktop.
Fibonacci series matrix version
#with expm
require(expm)
matrix( c(0,1,1,1), 2,2 ) -> fibonacci
c(1L,1L) -> start
start %*% (fibonacci %*% 1337) #1337'th term-pair in Fibonacci series
# [,1] [,2]
#[1,] 1.887894e+279 3.054677e+279
eigen(fibonacci) #think about THAT one!
#$values
#[1] 1.618034 -0.618034
#
#$vectors
# [,1] [,2]
#[1,] 0.5257311 -0.8506508
#[2,] 0.8506508 0.5257311
#without expm -- using metaprogramming instead
pm <- function(x) paste(x,collapse=" %*% ")
#build a string
pm( 'start', pm(rep( 'fibonacci', 12)), sep=' %*% ' ) -> i.wish.you.were.a.call #why is the second sep necessary?
# [1] "start %*% fibonacci %*% fibonacci %*% fibonacci %*% fibonacci %*% fibonacci %*% fibonacci %*% fibonacci %*% fibonacci %*% fibonacci %*% fibonacci %*% fibonacci %*% fibonacci"
eval(i.wish.you.were.a.call) #i wish you worked
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment