Applying Eigenvalues to the Fibonacci Problem
http://scottsievert.github.io/blog/2015/01/31/the-mysterious-eigenvalue/
The Fibonacci problem is a well known mathematical problem that models population growth and was conceived in the 1200s. Leonardo
of Pisa aka Fibonacci decided to use a recursive equation: xn=xn−1+xn−2 with
the seed values x0=0 and x1=1.
Implementing this recursive function is straightforward:1
1 |
|
Since the Fibonacci sequence was conceived to model population growth, it would seem that there should be a simple equation that grows almost exponentially. Plus, this recursive calling is expensive both in time and memory.2.
The cost of this function doesn’t seem worthwhile. To see the surprising formula that we end up with, we need to define our Fibonacci problem in a matrix language.3
Calling each of those matrices and vectors variables and recognizing the fact that xn−1 follows
the same formula as xn allows
us to write
where we have used An to
mean n matrix
multiplications. The corresponding implementation looks something like this:
1 |
|
While this isn’t recursive, there’s still an n−1 unnecessary
matrix multiplications. These are expensive time-wise and it seems like there should be a simple formula involving n.
As populations grow exponentially, we would expect this formula to involve scalars raised to the nth
power. A simple equation like this could be implemented many times faster than the recursive implementation!
The trick to do this rests on the mysterious and intimidating eigenvalues and eigenvectors. These are just a nice way to view the same data but they have a lot
of mystery behind them. Most simply, for a matrix A they
obey the equation
for different eigenvalues λ and
eigenvectors x.
Through the way matrix multiplication is defined, we can represent all of these cases. This rests on the fact that the left multiplied diagonal matrix Λjust
scales each xi by λi.
The column-wise definition of matrix multiplication makes it clear that this is represents every case where the equation above occurs.
Or compacting the vectors xi into
a matrix called X and
the diagonal matrix of λi’s
into Λ,
we find that
Because the Fibonacci eigenvector matrix is invertible,4
And then because a matrix and it’s inverse cancel
Λn is
a simple computation because Λ is
a diagonal matrix: every element is just raised to the nth
power. That means the expensive matrix multiplication only happens twice now. This is a powerful speed boost and we can calculate the result by substituting for An
For this Fibonacci matrix, we find that Λ=diag(1+5√2,1−5√2)=diag(λ1,λ2).
We could define our Fibonacci function to carry out this matrix multiplication, but these matrices are simple: Λ is
diagonal and x0=[1;0].
So, carrying out this fairly simple computation gives
We would not expect this equation to give an integer. It involves the power of two irrational numbers, a division by another irrational number and even the golden ratio phi ϕ≈1.618!
However, it gives exactly the Fibonacci numbers – you can check yourself!
This means we can define our function rather simply:
1 |
|
As one would expect, this implementation is fast. We see speedups of roughly 1000 for n=25,
milliseconds vs microseconds. This is almost typical when mathematics are applied to a seemingly straightforward problem. There are often large benefits by making the implementation slightly more cryptic!
I’ve found that mathematics5 becomes
fascinating, especially in higher level college courses, and can often yield surprising results. I mean, look at this blog post. We went from a expensive recursive equation to a simple and fast equation that only involves scalars. This derivation is one I
enjoy and I especially enjoy the simplicity of the final result. This is part of the reason why I’m going to grad school for highly mathematical signal processing. Real world benefits + neat
theory = <3.
Yes, in some languages some compilers are smart enough to get rid of recursion for some functions.↩
I’m assuming you have taken a course that deals with matrices.↩
This happens when a matrix is diagonalizable.↩
Not math. Courses beyond calculus deserve a different name.↩
Posted by Scott
Sievert Jan 31st, 2015 math
Applying Eigenvalues to the Fibonacci Problem的更多相关文章
- [Algorithm] Fibonacci problem by using Dynamic programming
vThere are three ways to solve Fibonacci problem Recursion Memoize Bottom-up 'First Recursion approa ...
- Codeforces 1264F - Beautiful Fibonacci Problem(猜结论+找性质)
Codeforces 题面传送门 & 洛谷题面传送门 一道名副其实(beautiful)的结论题. 首先看到这道设问方式我们可以很自然地想到套用斐波那契数列的恒等式,注意到这里涉及到 \(F_ ...
- hdu 1568 Fibonacci 数学公式
Fibonacci Problem Description 2007年到来了.经过2006年一年的修炼,数学神童zouyu终于把0到的Fibonacci数列(f[0]=0,f[1]=1;f[i] = ...
- HDU - 1588 Gauss Fibonacci (矩阵高速幂+二分求等比数列和)
Description Without expecting, Angel replied quickly.She says: "I'v heard that you'r a very cle ...
- HDU 1588 Gauss Fibonacci(矩阵快速幂)
Gauss Fibonacci Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU:Gauss Fibonacci(矩阵快速幂+二分)
http://acm.hdu.edu.cn/showproblem.php?pid=1588 Problem Description Without expecting, Angel replied ...
- HDU 4099 Revenge of Fibonacci Trie+高精度
Revenge of Fibonacci Problem Description The well-known Fibonacci sequence is defined as following: ...
- 跨平台的CStdString类,实现了CString的接口
在实际工作中,std的string功能相对于MFC的CString来说,实在是相形见绌. CStdString类实现了CString的功能,支持跨平台. // ==================== ...
- Minimum Depth of Binary Tree 解答
Question Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along ...
随机推荐
- google project tango 学习笔记
google io 2015上 project tango 的演示视频
- 使用POI getCell 获取空的单元格之后在使用的时候报 NullPointerException
解决办法,在得到cell之后先判断cell是否为空,然后再进行逻辑处理. 得到的cell建议使用去除策略(如左对齐,居中等)的cell,不然有可能受到策略影响而导致结果异常. org.apache.p ...
- IOS开发之——意见反馈UITextView的使用+不能输入字符输入
@interface DMFeedbackViewController ()<UITextViewDelegate,UIAlertViewDelegate>@property (nonat ...
- iOS使用AVFoundation实现二维码扫描(ios7以上)——转载
关于二维码扫描有不少优秀第三方库: ZBar SDK 里面有详细的文档,相应介绍也非常多,如:http://rdcworld-iphone.blogspot.in/2013/03/how-to-use ...
- 【MPI学习6】MPI并行程序设计模式:具有不连续数据发送的MPI程序设计
基于都志辉老师<MPI并行程序设计模式>第14章内容. 前面接触到的MPI发送的数据类型都是连续型的数据.非连续类型的数据,MPI也可以发送,但是需要预先处理,大概有两类方法: (1)用户 ...
- [bzoj 1911][Apio 2010]特别行动队(斜率优化DP)
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1911 分析: 首先可以的到裸的方程f[i]=max{f[j]+a*(Si-Sj)^2+b*(S ...
- redis学习笔记——(2)
4.Redis中的string类型 String类型是最简单的类型,一个Key对应一个Value,String类型是二进制安全的.Redis的String可以包含任何数据,比如jpg图片或者序列化的对 ...
- Linq之扩展方法
目录 写在前面 系列文章 扩展方法 总结 写在前面 上篇文章介绍了隐式类型,自动属性,初始化器,匿名类的相关概念,及通过反编译的方式查看了编译器帮我们做了那些事.本篇文章将介绍扩展方法的知识点,及如何 ...
- AngularJS开发指南12:AngularJS的模板,CSS,数据绑定详解
模板 AngularJS模板是一种声明式的规则.它包含了模型和控制器的信息,最后会被渲染成用户在浏览器中看到的视图.它是静态的DOM,包含HTML,CSS和AngularJS指定的元素和属性.Angu ...
- AngularJs-指令和指令之间的交互(动感超人)
前言: 上节我们学习到了指令和控制器之间的交互,通过给指令添加动作,调用了控制器中的方法.本节我们学习指令和指令之间是如何交互的,我们通过一个小游戏来和大家一起学习,听大漠老师说这是国外的人写的dem ...