Board logo

標題: 用maxima學數值分析-特徵值和特徵向量 [打印本頁]

作者: bugmens    時間: 2016-7-27 14:27     標題: 用maxima學數值分析-特徵值和特徵向量

高中曾學過用特徵方程式來求特徵值和特徵向量,但在實務上反而用power法、逆power法或QR法。
我會採用"線代啟示錄"網站的內容,並搭配maxima程式來介紹各種求特徵值和特徵向量的方法。
https://ccjou.wordpress.com
作者: bugmens    時間: 2016-7-27 14:29

估計特徵值範圍的\(Gershgorin\)圓
https://ccjou.wordpress.com/2010 ... 的-gershgorin-圓/

\(A=\left[ \matrix{-4&0&1 \cr -1&3&1 \cr 1&2&5} \right]\),\( \matrix{圓心(-4,0),半徑|\;0|\;+|\;1|\;=1 \cr 圓心(3,0),半徑|\;-1|\;+|\;1|\;=2 \cr 圓心(5,0),半徑|\;1|\;+|\;2|\;=3} \)
矩陣\(A\)和轉置矩陣\(A^T\)有相同的特徵值
\(A^T=\left[ \matrix{-4&-1&1 \cr 0&3&2 \cr 1&1&5} \right]\),\( \matrix{圓心(-4,0),半徑|\;-1|\;+|\;1|\;=2 \cr 圓心(3,0),半徑|\;0|\;+|\;2|\;=2 \cr 圓心(5,0),半徑|\;1|\;+|\;1|\;=2} \)
矩陣\(A\)的特徵值為-4.144、2.390、5.754,都在\(Gershgorin\)圓內。


要先載入implicit_plot才能使用implicit_plot指令
(%i1) load(implicit_plot);
(%o1) C:\maxima-5.38.0\share\maxima\5.38.0_dirty\share\contrib\implicit_plot.lisp

矩陣A
(%i2)
A:matrix([-4,0,1],
              [-1,3,1],
              [1,2,5]);

(%o2) \(\left[ \matrix{-4&0&1 \cr -1&3&1 \cr 1&2&5} \right]\)

GershgorinCircle副程式
(%i3)
GershgorinCircle(A):=block
([circles:[],centers:"",xrange:[],yrange:[]],
for i:1 thru length(A) do
   (r:sum(if j#i then abs(A[i,j]) else 0,j,1,length(A[1])),
    circles:append(circles,[(x-A[i,i])^2+y^2=r^2]),
    centers:concat(centers,"set label ",i," at ",A[i,i],",0 point;"),
    xrange:append(xrange,[A[i,i]-r,A[i,i]+r]),
    yrange:append(yrange,[r,-r]),
    print("圓心(",A[i,i],"0),半徑",r)
  ),
  implicit_plot(circles,
                     [x,lmin(xrange)-1,lmax(xrange)+1],
                     [y,lmin(yrange)-1,lmax(yrange)+1],
                     same_xy,
                     [gnuplot_preamble,centers])
)$


計算矩陣A的Gershgorin Circle
(%i4) GershgorinCircle(A);
圓心\((-4,0)\),半徑1
圓心\((3,0)\),半徑2
圓心\((5,0)\),半徑3
(%o4) done


計算矩陣A^t的Gershgorin Circle
(%i5) GershgorinCircle(transpose(A));
圓心\((-4,0)\),半徑2
圓心\((3,0)\),半徑2
圓心\((5,0)\),半徑2
(%o5) done


[ 本帖最後由 bugmens 於 2016-7-28 09:37 AM 編輯 ]
作者: bugmens    時間: 2016-7-31 17:39

\(Power\)迭代法
https://ccjou.wordpress.com/2010/11/02/power-遞迴法/

\(Google PageRank\)就是用\(Power\)法來計算各網頁的\(PageRank\)值
https://math.pro/db/viewthread.php?tid=2299&page=1#pid13825

矩陣\(A=\left[ \matrix{-4&0&1 \cr -1&3&1 \cr 1&2&5} \right]\)絕對值的特徵值由大到小排列\(|\;5.7542|\;>|\;-4.1444|\;>|\;2.3902|\;\)
---------------------------
虛擬碼
1.給定一初始向量\(u_0\)
2.對於\(k=0,1,2,\ldots\),直至\(\lambda(k)\)收斂止,計算
3.\(\displaystyle u_{k+1}=\frac{Au_k}{\Vert\; Au_k \Vert\;}\)
4.\(\lambda(k+1)=u_{k+1}^T A u_{k+1}\)
----------------------------
計算\( A=\left[ \matrix{-4&0&1 \cr -1&3&1 \cr 1&2&5} \right] \)最大的絕對值特徵值和特徵向量
[計算過程]
設定初始向量\(u_0=\left[\matrix{1 \cr 1 \cr 1}\right]\)

第1輪:
\(Au_0=\left[ \matrix{-4&0&1 \cr -1&3&1 \cr 1&2&5}\right] \left[ \matrix{1 \cr 1 \cr 1} \right]=\left[ \matrix{-3 \cr 3 \cr 8} \right] \)
特徵向量\(\displaystyle u_1=\frac{Au_0}{\Vert\;Au_0\Vert\;}=\frac{\left[ \matrix{-3\cr 3 \cr 8}\right]}{\sqrt{(-3)^2+3^2+8^2}}=\left[ \matrix{-0.3313 \cr 0.3313 \cr 0.8835} \right] \)
特徵值\( \lambda_1=u_1^T A u_1=\left[ \matrix{-0.3313 & 0.3313 & 0.8835} \right] \left[ \matrix{-4&0&1 \cr -1&3&1 \cr 1&2&5} \right] \left[ \matrix{-0.3313 \cr 0.3313 \cr 0.8835} \right]=4.1951 \)

第2輪:
\(Au_1=\left[ \matrix{-4&0&1 \cr -1&3&1 \cr 1&2&5} \right] \left[ \matrix{-0.3313 \cr 0.3313 \cr 0.8835} \right]=\left[ \matrix{2.2087 \cr 2.2087 \cr 4.7488} \right]\)
特徵向量\(\displaystyle u_2=\frac{Au_1}{\Vert\;Au_1\Vert\;}=\frac{\left[ \matrix{2.2087 \cr 2.2087 \cr 4.7488}\right]}{\sqrt{2.2087^2+2.2087^2+4.7488^2}}=\left[ \matrix{0.3886 \cr 0.3886 \cr 0.8355} \right] \)
特徵值\(\lambda_2=u_2^T A u_2=\left[\matrix{0.3886&0.3886&0.8355}\right] \left[ \matrix{-4&0&1 \cr -1&3&1 \cr 1&2&5} \right] \left[\matrix{0.3886 \cr 0.3886 \cr 0.8355}\right]=4.8112 \)

第3輪:
\(Au_2=\left[ \matrix{-4&0&1 \cr -1&3&1 \cr 1&2&5} \right] \left[ \matrix{0.3886 \cr 0.3886 \cr 0.8355} \right]=\left[ \matrix{-0.7189 \cr 1.6127 \cr 5.3433} \right]\)
特徵向量\( \displaystyle u_3=\frac{Au_2}{\Vert\;Au_2\Vert\;}=\frac{\left[ \matrix{-0.7189 \cr 1.6127 \cr 5.3433}\right]}{\sqrt{(-0.7189)^2+1.6127^2+5.3433^2}}=\left[ \matrix{-0.1278 \cr 0.2866 \cr 0.9495} \right] \)
特徵值\(\lambda_3=u_3^T A u_3=\left[\matrix{-0.1278&0.2866&0.9495}\right] \left[ \matrix{-4&0&1 \cr -1&3&1 \cr 1&2&5} \right] \left[\matrix{-0.1278 \cr 0.2866 \cr 0.9495}\right]=5.2992\)

反覆計算直到最大絕對值特徵值\(\lambda=5.7542\)收斂為止


設定為數值運算
(%i1) numer:true;
(%o1) true

Power迭代法副程式
(%i2)
Power(A,epsilon):=block
([lambda1:0,lambda2:0,
   u:genmatrix(lambda([i,j],1),length(A),1)
],
do
  (norm:sqrt((A.u).(A.u)),
   u: (A.u)/norm,
   lambda1:transpose(u).A.u,
    print("特徵值",lambda1,",特徵向量",u),
    if abs(lambda1-lambda2)<epsilon then
      (return([lambda1,u])
      ),
    lambda2:lambda1
   )
)$


矩陣A
(%i3)
A:matrix([-4,0,1],
              [-1,3,1],
              [1,2,5]);

(%o3) \( \left[ \matrix{-4&0&1 \cr -1&3&1 \cr 1&2&5}\right] \)

計算矩陣A的最大絕對值特徵值和特徵向量,誤差10^-5
(%i4) Power(A,10^-5);
特徵值4.195121951219512,特徵向量\( \left[ \matrix{-0.3312945782245396 \cr 0.3312945782245396 \cr 0.8834522085987723} \right] \)
特徵值4.811249528123821,特徵向量\( \left[ \matrix{0.3885876702894004 \cr 0.3885876702894004 \cr 0.835463491122211} \right] \)
特徵值5.299190539203416,特徵向量\( \left[ \matrix{-0.127751129016673 \cr 0.2865768569833476 \cr 0.9495016345833805} \right] \)
特徵值5.481375464225344,特徵向量\( \left[ \matrix{0.2548162255461269 \cr 0.3379477601214591 \cr 0.9060132463862292} \right] \)
特徵值5.630841932875022,特徵向量\( \left[ \matrix{-0.01983355591413924 \cr 0.2915954709929835 \cr 0.9563360870301721} \right] \)
特徵值5.675246645900037,特徵向量\( \left[ \matrix{0.1801013767454748 \cr 0.3218782372414542 \cr 0.9294933536527943} \right] \)
特徵值5.723067597718835,特徵向量\( \left[ \matrix{0.03644148428537539 \cr 0.2989084211151836 \cr 0.9535857454940834} \right] \)
特徵值5.730714847076354,特徵向量\( \left[ \matrix{0.1403550033047277 \cr 0.3151515480071531 \cr 0.9386053349710008} \right] \)
特徵值5.747206056797989,特徵向量\( \left[ \matrix{0.06562511526259228 \cr 0.3033809467502935 \cr 0.9506068300804844} \right] \)
特徵值5.7468154332089,特徵向量\( \left[ \matrix{0.1195466125233259 \cr 0.3118719274730653 \cr 0.9425733437183723} \right] \)
特徵值5.753083430150924,特徵向量\( \left[ \matrix{0.08074385491236928 \cr 0.3057786038135667 \cr 0.9486726913660559} \right] \)
特徵值5.751683756373387,特徵向量\( \left[ \matrix{0.1087140210130338 \cr 0.310187221869381 \cr 0.9444390658079183} \right] \)
特徵值5.754312277364633,特徵向量\( \left[ \matrix{0.08857834565554373 \cr 0.3070250780781634 \cr 0.9475703024641626} \right] \)
特徵值5.75326102960935,特徵向量\( \left[ \matrix{0.1030864250836731 \cr 0.3093107842420867 \cr 0.9453570900538133} \right] \)
特徵值5.754455685847601,特徵向量\( \left[ \matrix{0.09263978964425018 \cr 0.3076692887809196 \cr 0.9469727969248706} \right] \)
特徵值5.753822420970558,特徵向量\( \left[ \matrix{0.1001652563476691 \cr 0.308854253924219 \cr 0.9458202637148985} \right] \)
特徵值5.754396491271145,特徵向量\( \left[ \matrix{0.09474585840858986 \cr 0.3080022900102694 \cr 0.9466561211246931} \right] \)
特徵值5.754044259194526,特徵向量\( \left[ \matrix{0.098649468141091 \cr 0.308616716056323 \cr 0.9460570833760988} \right] \)
特徵值5.754329734340657,特徵向量\( \left[ \matrix{0.09583814464185311 \cr 0.3081746223192885 \cr 0.9464900697788494} \right] \)
特徵值5.754140620372628,特徵向量\( \left[ \matrix{0.09786305388692572 \cr 0.3084932665854396 \cr 0.9461790143283508} \right] \)
特徵值5.754285386809937,特徵向量\( \left[ \matrix{0.09640469653336128 \cr 0.3082638919733257 \cr 0.946403459097529} \right] \)
特徵值5.754185562769438,特徵向量\( \left[ \matrix{0.09745508102955758 \cr 0.3084291606593783 \cr 0.9462420198006817} \right] \)
特徵值5.754259764059531,特徵向量\( \left[ \matrix{0.09669857231455173 \cr 0.3083101629526002 \cr 0.9463584043756721} \right] \)
特徵值5.75420751750356,特徵向量\( \left[ \matrix{0.09724344241992158 \cr 0.3083958871361926 \cr 0.9462746375676587} \right] \)
特徵值5.754245767596055,特徵向量\( \left[ \matrix{0.09685101227284877 \cr 0.3083341551256965 \cr 0.9463349989325384} \right] \)
特徵值5.754218540433258,特徵向量\( \left[ \matrix{0.09713365556656234 \cr 0.308378621525002 \cr 0.9462915400354248} \right] \)
特徵值5.754238317320978,特徵向量\( \left[ \matrix{0.09693008733225696 \cr 0.308346597897462 \cr 0.9463228485748517} \right] \)
特徵值5.754224159959818,特徵向量\( \left[ \matrix{0.0970767045344243 \cr 0.3083696637309211 \cr 0.9463003032479778} \right] \)
特徵值5.754234401503306,特徵向量\( \left[ \matrix{0.09697110615386674 \cr 0.3083530516383455 \cr 0.946316543296489} \right] \)
特徵值5.754227048509759,特徵向量\( \left[ \matrix{0.0970471617982491 \cr 0.3083650165891291 \cr 0.9463048477794515} \right] \)
(%o4) \([\; 5.754227048509759, \left[ \matrix{0.0970471617982491 \cr 0.3083650165891291 \cr 0.9463048477794515} \right] ]\; \)

[ 本帖最後由 bugmens 於 2016-8-4 07:07 AM 編輯 ]
作者: bugmens    時間: 2016-8-4 07:08

\(Inverse Power\)迭代法

原本\(Power\)法求矩陣的最大絕對值特徵值,若將原來的矩陣改成反矩陣\(A^{-1}\),可以求該矩陣的最小絕對值特徵值
矩陣\(A=\left[ \matrix{-4&0&1 \cr -1&3&1 \cr 1&2&5} \right]\)絕對值的特徵值由大到小排列\(|\;5.7542|\;>|\;-4.1444|\;>|\;2.3902|\;\)
---------------------------
虛擬碼
1.給定一初始向量\(u_0\)
2.對於\(k=0,1,2,\ldots\),直至\(\lambda(k)\)收斂止,計算
3.\(\displaystyle u_{k+1}=\frac{A^{-1}u_k}{\Vert\; A^{-1}u_k \Vert\;}\)
4.\(\displaystyle \lambda(k+1)=u_{k+1}^T A u_{k+1}\)
---------------------------
計算\(A=\left[ \matrix{-4&0&1 \cr -1&3&1 \cr 1&2&5} \right]\)最小的絕對值特徵值和特徵向量
[計算過程]
設定初始向量\( u_0=\left[ \matrix{1 \cr 1 \cr 1} \right] \)
反方陣\( A^{-1}=\left[ \matrix{\displaystyle -\frac{13}{57}&-\frac{2}{57}&\frac{1}{19}\cr -\frac{2}{19}&\frac{7}{19}&-\frac{1}{19}\cr \frac{5}{57}&-\frac{8}{57}&\frac{4}{19}} \right]=\left[ \matrix{-0.2281&-0.0351&0.0526 \cr -0.1053&0.3684&-0.0526 \cr 0.0877&-0.1404&0.2105} \right]\)

第1輪:
\(A^{-1}u_0=\left[ \matrix{-0.2281&-0.0351&0.0526 \cr -0.1053&0.3684&-0.0526 \cr 0.0877&-0.1404&0.2105} \right] \left[ \matrix{1 \cr 1 \cr 1}\right]=\left[ \matrix{-0.2105 \cr 0.2105 \cr 0.1579} \right]\)
特徵向量\( \displaystyle u_1=\frac{A^{-1}u_0}{\Vert\;A^{-1}u_0\Vert\;}=\frac{\left[ \matrix{-0.2105 \cr 0.2105 \cr 0.1579} \right]}{\sqrt{(-0.2105)^2+0.2105^2+0.1579^2}}=\left[ \matrix{-0.6247 \cr 0.6247 \cr 0.4685} \right] \)
特徵值\( \displaystyle \lambda_1=u_1^T A u_1=[\; \matrix{-0.6247 & 0.6247 & 0.4685} ]\; \left[ \matrix{-4&0&1 \cr -1&3&1 \cr 1&2&5} \right] \left[ \matrix{-0.6247 \cr 0.6247 \cr 0.4685} \right]=1.3902 \)

第2輪:
\( A^{-1}u_1=\left[ \matrix{-0.2281&-0.0351&0.0526 \cr -0.1053&0.3684&-0.0526 \cr 0.0877&-0.1404&0.2105} \right] \left[ \matrix{-0.6247 \cr 0.6247 \cr 0.4685} \right]=\left[ \matrix{0.1452 \cr 0.2712 \cr -0.0438} \right] \)
特徵向量\(\displaystyle u_2=\frac{A^{-1}u_1}{\Vert\; A^{-1}u_1 \Vert\;}=\frac{\left[ \matrix{0.1451 \cr 0.2712 \cr -0.0438} \right]}{\sqrt{0.1452^2+0.2712^2+(-0.0438)^2}}=\left[ \matrix{0.4673 \cr 0.8728 \cr -0.1411} \right]\)
特徵值\( \displaystyle \lambda_2=u_2^T A u_2=[\; \matrix{0.4673 & 0.8728 & -0.1411} ]\; \left[ \matrix{-4&0&1 \cr -1&3&1 \cr 1&2&5} \right] \left[ \matrix{0.4673 \cr 0.8728 \cr -0.1411} \right]=0.6025 \)

第3輪:
\( A^{-1}u_2=\left[ \matrix{-0.2281&-0.0351&0.0526 \cr -0.1053&0.3684&-0.0526 \cr 0.0877&-0.1404&0.2105} \right]\left[ \matrix{0.4673 \cr 0.8728 \cr -0.1411} \right]=\left[ \matrix{-0.1446 \cr 0.2798 \cr -0.1112} \right] \)
特徵向量\(\displaystyle u_3=\frac{A^{-1}u_2}{\Vert\; A^{-1}u_2 \Vert\;}=\frac{\left[ \matrix{-0.1446 \cr 0.2798 \cr -0.1112} \right]}{\sqrt{(-0.1446)^2+0.2798^2+(-0.1112)^2}}=\left[ \matrix{-0.4330 \cr 0.8377 \cr -0.3329} \right] \)
特徵值\( \displaystyle \lambda_3=u_3^T A u_3=\left[ \matrix{-0.4330 & 0.8377 & -0.3329} \right] \left[ \matrix{-4&0&1 \cr -1&3&1 \cr 1&2&5} \right] \left[ \matrix{-0.4330 \cr 0.8377 \cr -0.3329} \right]=1.7238 \)

反覆計算直到最小絕對值特徵值\(\lambda=2.3902\)收斂為止


設定為數值運算
(%i1) numer:true;
(%o1) true

InversePower迭代法副程式
(%i2)
InversePower(A,epsilon):=block
([lambda1:0,lambda2:0,
   u:genmatrix(lambda([i,j],1),length(A),1),
   invA:invert(A)
],
do
  (norm:sqrt((invA.u).(invA.u)),
   u: (invA.u)/norm,
   lambda1:transpose(u).A.u,
    print("特徵值",lambda1,",特徵向量",u),
    if abs(lambda1-lambda2)<epsilon then
      (return([lambda1,u])
      ),
    lambda2:lambda1
   )
)$


矩陣A
(%i3)
A:matrix([-4,0,1],
              [-1,3,1],
              [1,2,5]);

(%o3) \( \left[ \matrix{-4&0&1 \cr -1&3&1 \cr 1&2&5} \right] \)

計算矩陣A的最小絕對值特徵值和特徵向量,誤差10^-5
(%i4) InversePower(A,10^-5);
特徵值1.390243902439024,特徵向量\( \left[ \matrix{-0.6246950475544242 \cr 0.6246950475544242 \cr 0.4685212856658182} \right] \)
特徵值0.6025182651950873,特徵向量\( \left[ \matrix{0.4672551492229502 \cr 0.8727973542089068 \cr -0.1410581582559849} \right] \)
特徵值1.723810898162325,特徵向量\( \left[ \matrix{-0.4329600692855059 \cr 0.8376735458154866 \cr -0.3329393473970894} \right] \)
特徵值1.898404099984411,特徵向量\( \left[ \matrix{0.1183560105491442 \cr 0.8488240781640499 \cr -0.5152567700630842} \right] \)
特徵值2.353621222544481,特徵向量\( \left[ \matrix{-0.2088230074124524 \cr 0.8148882500740622 \cr -0.5406940830695776} \right] \)
特徵值2.290289620832044,特徵向量\( \left[ \matrix{-0.02198003227500158 \cr 0.8178754811107383 \cr -0.5749752825809717} \right] \)
特徵值2.404381834227433,特徵向量\( \left[ \matrix{-0.1304823266239602 \cr 0.8076149441908717 \cr -0.5750934396760006} \right] \)
特徵值2.366004441947428,特徵向量\( \left[ \matrix{-0.06838457207541901 \cr 0.8096852683579864 \cr -0.5828664654156365} \right] \)
特徵值2.398226522212817,特徵向量\( \left[ \matrix{-0.1043686777721781 \cr 0.8067637967691548 \cr -0.5815834895547717} \right] \)
特徵值2.383319198106618,特徵向量\( \left[ \matrix{-0.0837054173929339 \cr 0.8077016380546078 \cr -0.5836192825661075} \right] \)
特徵值2.393280270604373,特徵向量\( \left[ \matrix{-0.09565793262222155 \cr 0.8068381181023898 \cr -0.5829766814405501} \right] \)
特徵值2.388045766621258,特徵向量\( \left[ \matrix{-0.08878099624356632 \cr 0.8071987795210824 \cr -0.5835649621470388} \right] \)
特徵值2.391257973753078,特徵向量\( \left[ \matrix{-0.09275383808600854 \cr 0.8069324494868957 \cr -0.5833153070899075} \right] \)
特徵值2.389479539742537,特徵向量\( \left[ \matrix{-0.09046555403219637 \cr 0.8070614399676388 \cr -0.5834962002027171} \right] \)
特徵值2.390533916277717,特徵向量\( \left[ \matrix{-0.09178648747560744 \cr 0.8069766901476567 \cr -0.5834071153793234} \right] \)
特徵值2.389937075673304,特徵向量\( \left[ \matrix{-0.09102519898238785 \cr 0.8070212218484542 \cr -0.5834647895429893} \right] \)
特徵值2.390285726718177,特徵向量\( \left[ \matrix{-0.09146446943282166 \cr 0.8069937196208362 \cr -0.5834341328066945} \right] \)
特徵值2.390086420379639,特徵向量\( \left[ \matrix{-0.0912112245795963 \cr 0.8070088200522667 \cr -0.5834528917303767} \right] \)
特徵值2.390202073842328,特徵向量\( \left[ \matrix{-0.09135731484464309 \cr 0.8069997936976503 \cr -0.5834425198734888} \right] \)
特徵值2.390135660125875,特徵向量\( \left[ \matrix{-0.09127307748649945 \cr 0.8070048669335477 \cr -0.5834486867512093} \right] \)
特徵值2.390174078419733,特徵向量\( \left[ \matrix{-0.0913216657128404 \cr 0.8070018858881337 \cr -0.5834452069769944} \right] \)
特徵值2.390151969029395,特徵向量\( \left[ \matrix{-0.09129364662248557 \cr 0.8070035821341262 \cr -0.5834472456949792} \right] \)
特徵值2.390164739323511,特徵向量\( \left[ \matrix{-0.09130980700744958 \cr 0.8070025943037633 \cr -0.5834460831398715} \right] \)
特徵值2.390157382341876,特徵向量\( \left[ \matrix{-0.09130048744691514 \cr 0.8070031600260253 \cr -0.5834467590962906} \right] \)
(%o4) \([2.390157382341876,\left[ \matrix{-0.09130048744691514 \cr 0.8070031600260253 \cr -0.5834467590962906} \right] ]\)

[ 本帖最後由 bugmens 於 2016-8-5 05:26 PM 編輯 ]
作者: bugmens    時間: 2016-8-6 08:46

\(Shift Inverse Power\)迭代法

要求其他的特徵值,將原來的矩陣改成\( (A-\alpha I)^{-1} \),可以求離\(\alpha\)最接近的特徵值。

---------------------------
虛擬碼
1.給定一初始向量\(u_0\),偏移值\(\alpha\)
2.對於\(k=0,1,2,\ldots\),直至\(\lambda(k)\)收斂止,計算
3.\(\displaystyle u_{k+1}=\frac{(A-\alpha I)^{-1}u_k}{\Vert\; (A-\alpha I)^{-1}u_k \Vert\;}\)
4.\(\displaystyle \lambda(k+1)=u_{k+1}^T A u_{k+1}\)
---------------------------
給定\(\alpha\)值,計算\(A=\left[ \matrix{-4&0&1 \cr -1&3&1 \cr 1&2&5} \right]\)離\(\alpha\)最接近的特徵值和特徵向量
[計算過程]
設定初始向量\( u_0=\left[ \matrix{1 \cr 1 \cr 1} \right] \)
設定偏移值\(\alpha=-4\)(由\(Gershgorin Circle\)定理可知有個特徵值在圓心\((-4,0)\)半徑1的圓內)
反方陣\((A-\alpha I)^{-1}=\left[ \matrix{0&0&1 \cr -1&7&1 \cr 1&2&9} \right]^{-1}=\left[ \matrix{\displaystyle -\frac{61}{9}&-\frac{2}{9}&\frac{7}{9} \cr -\frac{10}{9}&\frac{1}{9}&\frac{1}{9} \cr 1&0&0} \right]=\left[ \matrix{-6.7778&-0.2222&0.7778 \cr -1.1111&0.1111&0.1111 \cr 1&0&0} \right]\)

第1輪:
\((A-\alpha I)^{-1}u_0=\left[ \matrix{-6.7778&-0.2222&0.7778 \cr -1.1111&0.1111&0.1111 \cr 1&0&0} \right] \left[ \matrix{1 \cr 1 \cr 1} \right]=\left[ \matrix{-6.2222 \cr -0.8889 \cr 1} \right] \)
特徵向量\(\displaystyle u_1=\frac{(A-\alpha I)^{-1}u_0}{\Vert\; (A-\alpha I)^{-1}u_0 \Vert\;}=\frac{\left[ \matrix{-6.2222 \cr -0.8889 \cr 1} \right]}{\sqrt{(-6.2222)^2+(-0.8889)^2+1^2}}=\left[ \matrix{-0.9777 \cr -0.1397 \cr 0.1571} \right]\)
特徵值\( \lambda_1=u_1^T A u_1=[\; \matrix{-0.9777 & -0.1397 & 0.1571} ]\; \left[ \matrix{-4&0&1 \cr -1&3&1 \cr 1&2&5} \right] \left[ \matrix{-0.9777 \cr -0.1397 \cr 0.1571} \right]=-4.1509 \)

第2輪:
\((A-\alpha I)^{-1}u_1=\left[ \matrix{-6.7778&-0.2222&0.7778 \cr -1.1111&0.1111&0.1111 \cr 1&0&0} \right] \left[ \matrix{-0.9777 \cr -0.1397 \cr 0.1571} \right]=\left[ \matrix{6.7796 \cr 1.0882 \cr -0.9777} \right]\)
特徵向量\(\displaystyle u_2=\frac{(A-\alpha I)^{-1}u_1}{\Vert\; (A-\alpha I)^{-1}u_1 \Vert\;}=\frac{\left[ \matrix{6.7796 \cr 1.0882 \cr -0.9777} \right]}{\sqrt{6.7796^2+1.0882^2+(-0.9777)^2}}=\left[ \matrix{0.9775 \cr 0.1569 \cr -0.1410} \right]\)
特徵值\(\lambda_2=u_2^T A u_2=\left[ \matrix{0.9775 & 0.1569 & -0.1410} \right] \left[ \matrix{-4&0&1 \cr -1&3&1 \cr 1&2&5} \right] \left[ \matrix{0.9775 \cr 0.1569 \cr -0.1410} \right]=-4.1441 \)

第3輪:
\((A-\alpha I)^{-1}u_2=\left[ \matrix{-6.7778&-0.2222&0.7778 \cr -1.1111&0.1111&0.1111 \cr 1&0&0} \right] \left[ \matrix{0.9775 \cr 0.1569 \cr -0.1410} \right]=\left[ \matrix{-6.7698 \cr -1.0843 \cr 0.9775} \right]\)
特徵向量\(\displaystyle u_3=\frac{\left[ \matrix{-6.7698 \cr -1.0843 \cr 0.9775} \right]}{\sqrt{(-6.7698)^2+(-1.0843)^2+0.9775^2}}=\left[ \matrix{-0.9775 \cr -0.1566 \cr 0.1411} \right]\)
特徵值\(\lambda_3=u_3^T A u_3=\left[ \matrix{-0.9775 & -0.1566 & 0.1411} \right] \left[ \matrix{-4&0&1 \cr -1&3&1 \cr 1&2&5} \right] \left[ \matrix{-0.9775 \cr -0.1566 \cr 0.1411} \right]=-4.1444 \)

反覆計算直到特徵值\(\lambda=-4.1444\)收斂為止


設定為數值運算
(%i1) numer:true;
(%o1) true

ShiftInversePower迭代法副程式
(%i2)
ShiftInversePower(A,alpha,epsilon):=block
([lambda1:0,lambda2:0,
  u:genmatrix(lambda([i,j],1),length(A),1),
  ShiftInvA:invert(A-alpha*ident(length(A)))
],
do
  (norm:sqrt((ShiftInvA.u).(ShiftInvA.u)),
   u: (ShiftInvA.u)/norm,
   lambda1:transpose(u).A.u,
   print("特徵值",lambda1,",特徵向量",u),
   if abs(lambda1-lambda2)<epsilon then
     (return([lambda1,u])
     ),
     lambda2:lambda1
   )
)$


矩陣A
(%i3)
A:matrix([-4,0,1],
              [-1,3,1],
              [1,2,5]);

(%o3) \( \left[ \matrix{-4&0&1 \cr -1&3&1 \cr 1&2&5}\right] \)

A[1,1]=-4,計算矩陣A離-4最近的特徵值,誤差10^-5
(%i4) ShiftInversePower(A,A[1,1],10^-5);
特徵值-4.150868637610485,特徵向量\( \left[ \matrix{-0.9776533929054586 \cr -0.1396647704150655 \cr 0.1571228667169487} \right] \)
特徵值-4.14414348452357,特徵向量\( \left[ \matrix{0.9775024211666079 \cr 0.1569038221100049 \cr -0.1409617225373306} \right] \)
特徵值-4.144395503309593,特徵向量\( \left[ \matrix{-0.9775285462559674 \cr -0.1565741860704146 \cr 0.1411469642290686} \right] \)
特徵值-4.144390102997756,特徵向量\( \left[ \matrix{0.9775277105485285 \cr 0.1565807707392749 \cr -0.1411454474804167} \right] \)
(%o4) \( [\;-4.144390102997756,\left[ \matrix{0.9775277105485285 \cr 0.1565807707392749 \cr -0.1411454474804167} \right] ]\; \)

A[2,2]=3,計算矩陣A離3最近的特徵值,誤差10^-5
(%i5) ShiftInversePower(A,A[2,2],10^-5);
特徵值3.4,特徵向量\(\left[ \matrix{0.0 \cr -0.447213595499958 \cr 0.8944271909999159} \right] \)
特徵值2.289558665231432,特徵向量\(\left[ \matrix{-0.06561787149247868 \cr 0.885841265148462 \cr -0.4593251004473507} \right] \)
特徵值2.426983132689005,特徵向量\(\left[ \matrix{0.0923650023965832 \cr -0.788287520453598 \cr 0.6083350157843926} \right] \)
特徵值2.382512315819198,特徵向量\(\left[ \matrix{-0.090698027506321 \cr 0.8111159042179215 \cr -0.5778103994661282} \right] \)
特徵值2.391869258878447,特徵向量\(\left[ \matrix{0.09140680193936326 \cr -0.8060912929597601 \cr 0.5846893397127062} \right] \)
特徵值2.389781526971133,特徵向量\(\left[ \matrix{-0.09127848203849248 \cr 0.8072048325603888 \cr -0.5831711558435508} \right] \)
特徵值2.390243815947408,特徵向量\(\left[ \matrix{0.09130930087416761 \cr -0.8069582576595488 \cr 0.5835074823589975} \right] \)
特徵值2.390141525165331,特徵向量\(\left[ \matrix{-0.09130268093133849 \cr 0.8070128481625035 \cr -0.583433015311436} \right] \)
特徵值2.390164183499367,特徵向量\(\left[ \matrix{0.09130416404587217 \cr -0.8070007600281967 \cr 0.5834495033349479} \right] \)
特徵值2.390159167449219,特徵向量\(\left[ \matrix{-0.09130383713123971 \cr 0.807003436501031 \cr -0.5834458525010168} \right] \)
(%o5) \( [\;2.390159167449219,\left[ \matrix{-0.09130383713123971 \cr 0.807003436501031 \cr -0.5834458525010168} \right] ]\; \)

A[3,3]=5,計算矩陣A離5最近的特徵值,誤差10^-5
(%i6) ShiftInversePower(A,A[3,3],10^-5);
特徵值5.607038123167155,特徵向量\( \left[ \matrix{0.05415303610738823 \cr 0.2166121444295529 \cr 0.9747546499329882} \right] \)
特徵值5.780069595037571,特徵向量\( \left[ \matrix{0.09942088604805867 \cr 0.3337701174470541 \cr 0.9373969255959819} \right] \)
特徵值5.745800818656059,特徵向量\( \left[ \matrix{0.09715673137405657 \cr 0.3011858672595754 \cr 0.9486029954157886} \right] \)
特徵值5.756608639911317,特徵向量\( \left[ \matrix{0.09690361111870185 \cr 0.3104257059630139 \cr 0.9456455843652629} \right] \)
特徵值5.753536916223192,特徵向量\( \left[ \matrix{0.09705322894473802 \cr 0.3077642101489163 \cr 0.9464997948773225} \right] \)
特徵值5.754430086933437,特徵向量\( \left[ \matrix{0.09700387900903709 \cr 0.3085321381841197 \cr 0.9462548109070491} \right] \)
特徵值5.754172318128385,特徵向量\( \left[ \matrix{0.09701866284758788 \cr 0.3083102704326569 \cr 0.946325607919921} \right] \)
特徵值5.754246849656881,特徵向量\( \left[ \matrix{0.09701434887646511 \cr 0.3083743806347206 \cr 0.9463051608652613} \right] \)
特徵值5.754225312899597,特徵向量\( \left[ \matrix{0.09701559914502492 \cr 0.3083558535212965 \cr 0.9463110699562194} \right] \)
特徵值5.754231537172271,特徵向量\( \left[ \matrix{0.0970152375416282 \cr 0.3083612077029521 \cr 0.9463093623486551} \right] \)
(%o6) \( [\;5.754231537172271, \left[ \matrix{0.0970152375416282 \cr 0.3083612077029521 \cr 0.9463093623486551} \right] ]\; \)

[ 本帖最後由 bugmens 於 2016-8-11 04:22 PM 編輯 ]
作者: bugmens    時間: 2016-8-11 17:34

\(ShiftInversePower\)迭代法+\(Rayleigh\) \(quotient\)

https://en.wikipedia.org/wiki/Rayleigh_quotient_iteration
前一篇\((A-\alpha I)^{-1}\)的\(\alpha\)值是固定的,但\(Rayleigh\) \(quotient\)改成\( \displaystyle \alpha=\frac{u^T A u}{u^T u}\)來加快收斂速度。


將特徵值問題的\(Au=\lambda u\),(\(\lambda\)為特徵值,\(u\)為特徵向量)
代入\( \displaystyle \alpha=\frac{u^T A u}{u^T u}=\frac{\lambda u^T u}{u^T u}=\lambda \)
也就是將\((A-\alpha I)^{-1}\)替換成\((A-\lambda I)^{-1}\)就可以加快收斂速度
---------------------------
虛擬碼
1.給定一初始向量\(u_0\),初始特徵值\(\lambda_0\)
2.對於\(k=0,1,2,\ldots\),直至\(\lambda(k)\)收斂止,計算
3.\(\displaystyle u_{k+1}=\frac{(A-\lambda_k I)^{-1}u_k}{\Vert\; (A-\lambda_k I)^{-1}u_k \Vert\;}\)
4.\(\lambda(k+1)=u_{k+1}^T A u_{k+1}\)
---------------------------
給定\(\lambda_0\)值,計算\(A=\left[ \matrix{-4&0&1 \cr -1&3&1 \cr 1&2&5} \right]\)離\(\lambda_0\)最接近的特徵值和特徵向量
[計算過程]
設定初始向量\(u_0=\left[ \matrix{1 \cr 1 \cr 1} \right]\)
設定初始特徵值\(\lambda_0=−4\)(由\(GershgorinCircle\)定理可知有個特徵值在圓心\((−4,0)\)半徑1的圓內)

第1輪:
\( A-\lambda_0 I=\left[ \matrix{0&0&1 \cr -1&7&1 \cr 1&2&9} \right] \)
\( (A-\lambda_0 I)^{-1}u_0=\left[ \matrix{-6.7778&-0.2222&0.7778 \cr -1.1111&0.1111&0.1111 \cr 1&0&0} \right] \left[ \matrix{1 \cr 1 \cr 1} \right] = \left[ \matrix{-6.2222 \cr -0.8889 \cr 1} \right]\)
特徵向量\( \displaystyle u_1=\frac{(A-\lambda_0 I)^{-1}u_0}{\Vert\; (A-\lambda_0 I)^{-1}u_0 \Vert\;}=\frac{\left[ \matrix{-6.2222 \cr -0.8889 \cr 1} \right]}{\sqrt{(-6.2222)^2+(-0.8889)^2+1^2}}=\left[ \matrix{-0.9777 \cr -0.1397 \cr 0.1571} \right]\)
特徵值\( \lambda_1=u_1^T A u_1=\left[ \matrix{-0.9777&-0.1397&0.1571} \right] \left[ \matrix{-4&0&1 \cr -1&3&1 \cr 1&2&5} \right] \left[ \matrix{-0.9777 \cr -0.1397 \cr 0.1571} \right]=-4.1507 \)

第2輪:
\(A-\lambda_1 I=\left[ \matrix{0.1509&0&1 \cr -1&7.1509&1 \cr 1&2&9.1509} \right] \)
\( (A-\lambda_1 I)^{-1}u_1=\left[ \matrix{151.1354&4.7649&-17.0367 \cr 24.1841&0.9067&-2.7419 \cr -21.8016&-0.7189&2.5703} \right] \left[ \matrix{-0.9777 \cr -0.1397 \cr 0.1571} \right]=\left[ \matrix{-151.1004 \cr -24.2011 \cr 21.8187} \right] \)
特徵向量\( \displaystyle u_2=\frac{(A-\lambda_1 I)^{-1}u_1}{\Vert\; (A-\lambda_1 I)^{-1}u_1 \Vert\;}=\frac{\left[ \matrix{-151.1004 \cr -24.2011 \cr 21.8187} \right]}{\sqrt{(-151.1004)^2+(-24.2011)^2+21.8187^2}}=\left[ \matrix{-0.9775 \cr -0.1566 \cr 0.1412} \right] \)
特徵值\( \lambda_2=u_1^T A u_1=\left[ \matrix{-0.9775 & -0.1566 & 0.1412} \right] \left[ \matrix{-4&0&1 \cr -1&3&1 \cr 1&2&5} \right] \left[ \matrix{-0.9775 \cr -0.1566 \cr 0.1412} \right]=-4.1444 \)

第3輪:
\(A-\lambda_2 I=\left[ \matrix{0.1444&0&1 \cr -1&7.1444&1 \cr 1&2&9.1444} \right] \)
\( (A-\lambda_2 I)^{-1}u_2=\left[ \matrix{5.7856 \cdot 10^{10} & 1.8271 \cdot 10^9 & -6.5267 \cdot 10^9 \cr 9.2674 \cdot 10^9 & 2.9296 \cdot 10^8 & -1.0455 \cdot 10^9 \cr -8.3538 \cdot 10^9 & -2.6381 \cdot 10^8 & 9.7239 \cdot 10^8} \right] \left[ \matrix{-0.9776 \cr -0.1566 \cr 0.1411} \right]=\left[ \matrix{-5.7762 \cdot 10^{10} \cr -9.2525 \cdot 10^9 \cr 8.3404 \cdot 10^9}\right] \)
特徵向量\( \displaystyle u_3=\frac{(A-\lambda_2 I)^{-1}u_2}{\Vert\; (A-\lambda_2 I)^{-1}u_2 \Vert\;}=\frac{\left[ \matrix{-5.7763 \cdot 10^{10} \cr -9.2525 \cdot 10^9 \cr 8.3404 \cdot 10^9} \right]}{\sqrt{(-5.7763 \cdot 10^{10})^2+(-9.2525 \cdot 10^9)^2+(8.3404 \cdot 10^9)^2}}=\left[ \matrix{-0.9775 \cr -0.1566 \cr 0.1411} \right] \)
特徵值\( \lambda_3=u_2^T A u_2=\left[ \matrix{-0.9775 & -0.1566 & 0.1411} \right] \left[ \matrix{-4&0&1 \cr -1&3&1 \cr 1&2&5} \right] \left[ \matrix{-0.9775 \cr -0.1566 \cr 0.1411} \right]=-4.1444 \)

反覆計算直到特徵值\(\lambda=-4.1444\)收斂為止


設定為數值運算
(%i1) numer:true;
(%o1) true

RayleighQuotientPower迭代法副程式
(%i2)
RayleighQuotientPower(A,lambda1,epsilon):=block
([lambda2:0,
  u:genmatrix(lambda([i,j],1),length(A),1)
],
do
  (y:invert(A-lambda1*ident(length(A))).u,
   norm:sqrt(y.y),
   u:y/norm,
   lambda1:transpose(u).A.u,
   print("特徵值",lambda1,",特徵向量",u),
   if abs(lambda1-lambda2)<epsilon then
     (return([lambda1,u])
     ),
   lambda2:lambda1
  )
)$


矩陣A
(%i3)
A:matrix([-4,0,1],
              [-1,3,1],
              [1,2,5]);

(%o3) \( \left[ \matrix{-4&0&1 \cr -1&3&1 \cr 1&2&2}\right] \)

A[1,1]=-4,計算矩陣A離-4最近的特徵值,誤差10^-5
(%i4) RayleighQuotientPower(A,A[1,1],10^-5);
特徵值-4.150868637610485,特徵向量\( \left[ \matrix{-0.9776533929054586 \cr -0.1396647704150655 \cr 0.1571228667169487} \right] \)
特徵值-4.144400989358965,特徵向量\( \left[ \matrix{-0.9775288279606554 \cr -0.1565664321230263 \cr 0.141153614328975} \right] \)
特徵值-4.144390218552043,特徵向量\( \left[ \matrix{-0.9775277325400289 \cr -0.1565806375486704 \cr 0.1411454429303385} \right] \)
特徵值-4.144390218535118,特徵向量\( \left[ \matrix{-0.9775277325374503 \cr -0.1565806375693802 \cr 0.1411454429252224} \right] \)
(%o4) \( [\;-4.144390218535118,\left[ \matrix{-0.9775277325374503 \cr -0.1565806375693802 \cr 0.1411454429252224} \right] ]\; \)

A[2,2]=3,計算矩陣A離3最近的特徵值,誤差10^-5
(%i5) RayleighQuotientPower(A,A[2,2],10^-5);
特徵值3.4,特徵向量\( \left[ \matrix{0.0 \cr -0.447213595499958 \cr 0.8944271909999159} \right] \)
特徵值2.34480387797816,特徵向量\( \left[ \matrix{-0.04418147155945099 \cr 0.9440107756536034 \cr -0.3269428895399374} \right] \)
特徵值2.386261415958715,特徵向量\( \left[ \matrix{-0.09118695120399674 \cr 0.8091554911793659 \cr -0.5804759521499571} \right] \)
特徵值2.390155311149527,特徵向量\( \left[ \matrix{-0.09130350144505062 \cr 0.8070054621306113 \cr -0.5834431032373528} \right] \)
特徵值2.390160076833697,特徵向量\( \left[ \matrix{-0.09130389650666068 \cr 0.8070029513063456 \cr -0.5834465143143361} \right] \)
(%o5) \( [\; 2.390160076833697,\left[ \matrix{-0.09130389650666068 \cr 0.8070029513063456 \cr -0.5834465143143361} \right] ]\; \)

A[3,3]=5,計算矩陣A離5最近的特徵值,誤差10^-5
(%i6) RayleighQuotientPower(A,A[3,3],10^-5);
特徵值5.607038123167155,特徵向量\( \left[ \matrix{0.05415303610738823 \cr 0.2166121444295529 \cr 0.9747546499329882} \right] \)
特徵值5.75863661416414,特徵向量\( \left[ \matrix{0.09749930430177767 \cr 0.3123865901727191 \cr 0.9449383598631875} \right] \)
特徵值5.754236049858843,特徵向量\( \left[ \matrix{-0.09701530629188788 \cr -0.308365169743175 \cr -0.9463080642340285} \right] \)
特徵值5.754230141705235,特徵向量\( \left[ \matrix{0.09701531863069972 \cr 0.3083600072972683 \cr 0.9463097451947816} \right] \)
(%o6) \( [\; 5.754230141705235,\left[ \matrix{0.09701531863069972 \cr 0.3083600072972683 \cr 0.9463097451947816} \right] ]\; \)



比較兩種方法所需的計算次數,發現採用\(Rayleigh\) \(quotient\)計算次數少於\(Shift\) \(Invers\) \(Power\)。

 \(\lambda=-4.1444\)\(\lambda=2.3902\)\(\lambda=5.7542\)
Shift Inverse Power
迭代法
4次10次10次
Rayleigh quotient
迭代法
4次5次4次

作者: bugmens    時間: 2016-9-15 10:54

power法一次只能求一個特徵值,Rutishauser在1958年提出\(LR\)演算法,可以求出全部的特徵值。
\(LR\)演算法是基於\(LU\)分解,也就是將\(A\)分解成\(L\)和\(U\)兩個矩陣,其中\(L\)為下三角矩陣,\(U\)為上三角矩陣,再將\(U\)乘上\(L\)形成下一個矩陣\(A\),如此反覆計算最後的矩陣\(A\)的對角線就是全部的特徵值。
關於\(LU\)分解請見https://ccjou.wordpress.com/2010/09/01/lu-分解/
---------------------------
虛擬碼
1.對於\(k=0,1,2,\ldots\),直到\( \lambda(1),\lambda(2),\ldots,\lambda(n) \)收斂為止,計算
2.\(A_k=L_k U_k\),計算\(A_k\)的\(LU\)分解。
3.\(A_{k+1}=U_k L_k\),將\(L_k\)和\(U_k\)對調後相乘得到下一個矩陣\(A_{k+1}\)
4.特徵值\( \lambda(1),\lambda(2),\ldots,\lambda(n) \)在矩陣\(A_{k+1}\)的對角線上。
---------------------------
計算\(A=\left[ \matrix{-4&0&1 \cr -1&3&1 \cr 1&2&5} \right] \)全部的特徵值。
[計算過程]
第1輪:
計算矩陣\(A\)的\(LU\)分解
\( \left[ \matrix{-4&0&1 \cr -1&3&1 \cr 1&2&5} \right]=\left[ \matrix{1&0&0 \cr 0.25&1&0 \cr -0.25&0.6667&1} \right] \left[ \matrix{-4&0&1 \cr 0&3.0&0.75 \cr 0&0&4.75} \right] \)
將\(L\)和\(U\)對調後相乘得到下一個矩陣\(A\)
\( \left[ \matrix{-4&0&1 \cr 0&3.0&0.75 \cr 0&0&4.75} \right] \left[ \matrix{1&0&0 \cr 0.25&1&0 \cr -0.25&0.6667&1} \right]=\left[ \matrix{-4.25&0.6667&1 \cr 0.5625&3.5&0.75 \cr -1.1875&3.1667&4.75} \right] \)
特徵值\( \left[-4.25,3.5,4.75 \right] \)

第2輪:
計算矩陣\(A\)的\(LU\)分解
\( \left[ \matrix{-4.25&0.6667&1 \cr 0.5625&3.5&0.75 \cr -1.1875&3.1667&4.75} \right]=\left[ \matrix{1&0&0 \cr -0.1324&1&0 \cr 0.2794&0.8306&1} \right] \left[ \matrix{-4.25&0.6667&1 \cr 0&3.5882&0.8824 \cr 0&0&3.7377} \right] \)
將\(L\)和\(U\)對調後相乘得到下一個矩陣\(A\)
\( \left[ \matrix{-4.25&0.6667&1 \cr 0&3.5882&0.8824 \cr 0&0&3.7377} \right] \left[ \matrix{1&0&0 \cr -0.1324&1&0 \cr 0.2794&0.8306&1} \right]=\left[ \matrix{-4.0588&1.4973&1.0 \cr -0.2284&4.3211&0.8824 \cr 1.0444&3.1045&3.7377} \right] \)
特徵值\( \left[ -4.0588,4.3211,3.7377 \right] \)

第3輪:
計算矩陣\(A\)的\(LU\)分解
\( \left[ \matrix{-4.0588&1.4973&1.0 \cr -0.2284&4.3211&0.8824 \cr 1.0444&3.1045&3.7377} \right]=\left[ \matrix{1&0&0 \cr 0.0563&1&0 \cr -0.2573&0.8237&1} \right] \left[ \matrix{-4.0588&1.4973&1.0 \cr 0&4.2369&0.8261 \cr 0&0&3.3146} \right] \)
將\(L\)和\(U\)對調後相乘得到下一個矩陣\(A\)
\( \left[ \matrix{-4.0588&1.4973&1.0 \cr 0&4.2369&0.8261 \cr 0&0&3.3146} \right] \left[ \matrix{1&0&0 \cr 0.0563&1&0 \cr -0.2573&0.8237&1} \right]=\left[ \matrix{-4.2319&2.3209&1.0 \cr 0.0253&4.9173&0.8261 \cr -0.8529&2.7301&3.3145} \right] \)
特徵值\( \left[ -4.2319 , 4.9173 , 3.3146 \right] \)

反覆計算直到全部特徵值\( \left[ 5.7542,-4.1444,2.3902 \right] \)


設定為數值運算
(%i1) numer:true;
(%o1) true

不顯示小數轉換成分數的過程
例如rat: replaced -4.25 by -17/4 = -4.25

(%i2) ratprint:false;
(%o2) false

LR迭代法副程式
/*print(...);*/將兩邊的/*和*/刪除後可以顯示計算過程
但顯示太多計算過程maxima程式會當掉,只好註解起來不顯示

(%i3)
LR(A,epsilon):=block
([count:0,P,L,U,n:length(A),eig1,eig2],
eig2:makelist(0,n),
do
   (count:count+1,
    /*print("第",count,"輪"),*/
    [P,L,U]:get_lu_factors(lu_factor(A)),
    /*print("計算矩陣A的LU分解",A,"="),*/
    /*print(L,"*",U),*/
    A:U.L,
    /*print("將L和U對調後相乘得到下一個矩陣A"),*/
    /*print(U,"*",L,"=",A),*/
    eig1:create_list(A[i,i],i,1,n),
    print("特徵值",eig1),
    if lmax(abs(eig1-eig2))<epsilon then
      (return(eig1)),
    eig2:eig1
  )
)$


矩陣A
(%i4)
A:matrix([-4,0,1],
              [-1,3,1],
              [1,2,5]);

(%o4) \( \left[ \matrix{-4&0&1\cr -1&3&1\cr 1&2&5} \right] \)

計算矩陣A全部的特徵值,誤差10^-5
(%i5) LR(A,10^-5);
0 errors, 0 warnings
特徵值\( \left[-4.25,3.5,4.75\right] \)
特徵值\( \left[-4.058823529411764,4.321118611378978,3.737704918032787\right] \)
特徵值\( \left[-4.231884057971015,4.91729873861925,3.314585319351763\right] \)
特徵值\( \left[-4.044520547945205,5.313248850521489,2.731271697423716\right] \)
特徵值\( \left[-4.27519051651143,5.669125227250055,2.606065289261376\right] \)
特徵值\( \left[-3.97464844523668,5.528967112469076,2.445681332767604\right] \)
特徵值\( \left[-4.386336456049431,5.951388036215438,2.434948419833993\right] \)
特徵值\( \left[-3.82905992615734,5.432559904220157,2.396500021937184\right] \)
特徵值\( \left[-4.616971218176369,6.21703651117396,2.39993470700241\right] \)
特徵值\( \left[-3.556070568397674,5.165962621376724,2.39010794702095\right] \)
特徵值\( \left[-5.09592634683869,6.703414031205933,2.392512315632756\right] \)
特徵值\( \left[-3.07014305149453,4.680374938114763,2.389768113379766\right] \)
特徵值\( \left[-6.15763906410022,7.766852181380823,2.390786882719396\right] \)
特徵值\( \left[-2.263100148442174,3.873135110765942,2.389965037676232\right] \)
特徵值\( \left[-8.927750729051786,10.53740902683053,2.390341702221251\right] \)
特徵值\( \left[-1.061374420236201,2.671290372000583,2.390084048235619\right] \)
特徵值\( \left[-20.85888707850496,22.46867122537871,2.390215853126253\right] \)
特徵值\( \left[0.4665444106715815,1.143322725027585,2.390132864300833\right] \)
特徵值\( \left[52.72558253627871,-51.11576036528454,2.390177829005824\right] \)
特徵值\( \left[2.06213873325895,-0.4522894269100236,2.390150693651076\right] \)
特徵值\( \left[13.17442195234688,-11.56458779576555,2.390165843418669\right] \)
特徵值\( \left[3.419996923785756,-1.810153822408377,2.390156898622622\right] \)
特徵值\( \left[8.58287969592947,-6.97304166698185,2.39016197105238\right] \)
特徵值\( \left[4.388368293639632,-2.77852730349426,2.39015900985463\right] \)
特徵值\( \left[7.044154778522382,-5.434315481286824,2.390160702764444\right] \)
特徵值\( \left[4.995309976952969,-3.385469697199792,2.390159720246825\right] \)
特徵值\( \left[6.383873007186085,-4.774033291505344,2.390160284319262\right] \)
特徵值\( \left[5.34546797814534,-3.735627936085535,2.390159957940198\right] \)
特徵值\( \left[6.071147185395128,-4.46130733112236,2.390160145727236\right] \)
特徵值\( \left[5.537890814471758,-3.928050851714158,2.390160037242404\right] \)
特徵值\( \left[5.916131959742552,-4.306292059474105,2.390160099731557\right] \)
特徵值\( \left[5.640814092845454,-4.030974156506412,2.390160063660963\right] \)
特徵值\( \left[5.8375586037984,-4.227718688248872,2.390160084450476\right] \)
特徵值\( \left[5.695070878935839,-4.08523095139107,2.390160072455235\right] \)
特徵值\( \left[5.797281242393553,-4.187441321764426,2.390160079370877\right] \)
特徵值\( \left[5.723453549499145,-4.113613624880672,2.390160075381531\right] \)
特徵值\( \left[5.776515673768994,-4.166675751450871,2.390160077681882\right] \)
特徵值\( \left[5.738241274640624,-4.128401350995675,2.390160076355056\right] \)
特徵值\( \left[5.765777948950188,-4.15593802607038,2.390160077120198\right] \)
特徵值\( \left[5.745929679925635,-4.136089756604526,2.390160076678896\right] \)
特徵值\( \left[5.760217049729156,-4.150377126662543,2.390160076933392\right] \)
特徵值\( \left[5.749922650695337,-4.140082727481946,2.390160076786614\right] \)
特徵值\( \left[5.757334865818812,-4.14749494269007,2.390160076871262\right] \)
特徵值\( \left[5.751995220747789,-4.142155297570226,2.390160076822442\right] \)
特徵值\( \left[5.7558404324125,-4.146000509263092,2.390160076850597\right] \)
特徵值\( \left[5.753070680591324,-4.143230757425678,2.39016007683436\right] \)
特徵值\( \left[5.755065392906378,-4.145225469750097,2.390160076843724\right] \)
特徵值\( \left[5.753628652968832,-4.14378872980715,2.390160076838324\right] \)
特徵值\( \left[5.754663399407986,-4.144823476249418,2.390160076841438\right] \)
特徵值\( \left[5.753918118401036,-4.144078195240673,2.390160076839642\right] \)
特徵值\( \left[5.754454883559434,-4.144614960400106,2.390160076840678\right] \)
特徵值\( \left[5.75406828136153,-4.144228358201605,2.390160076840081\right] \)
特徵值\( \left[5.754346722233892,-4.144506799074311,2.390160076840425\right] \)
特徵值\( \left[5.754146178167928,-4.144306255008148,2.390160076840227\right] \)
特徵值\( \left[5.754290615935278,-4.144450692775613,2.390160076840341\right] \)
特徵值\( \left[5.754186586569208,-4.144346663409477,2.390160076840275\right] \)
特徵值\( \left[5.754261511799679,-4.144421588639986,2.390160076840314\right] \)
特徵值\( \left[5.754207548012657,-4.144367624852942,2.390160076840291\right] \)
特徵值\( \left[5.754246414489054,-4.144406491329351,2.390160076840304\right] \)
特徵值\( \left[5.75421842151387,-4.144378498354161,2.390160076840297\right] \)
特徵值\( \left[5.754238582980808,-4.144398659821102,2.390160076840301\right] \)
特徵值\( \left[5.75422406200547,-4.144384138845762,2.390160076840299\right] \)
特徵值\( \left[5.75423452049635,-4.144394597336643,2.3901600768403\right] \)
特徵值\( \left[5.75422698793699,-4.144387064777282,2.390160076840299\right] \)
(%o5) \( \left[5.75422698793699,-4.144387064777282,2.390160076840299\right] \)


參考資料
https://books.google.com.tw/book ... e&q&f=false

[ 本帖最後由 bugmens 於 2016-9-16 10:41 PM 編輯 ]
作者: bugmens    時間: 2016-9-17 11:23

https://ccjou.wordpress.com/2010/09/01/lu-分解/
這篇利用maxima來計算\(LU\)分解的計算過程,可以觀察到其實是高斯消去法,\(L\)矩陣紀錄消去法化簡\(A\)矩陣的過程,而\(U\)矩陣則儲存化簡的結果。

這個程式無法處理對角線元素為0的情況,詳細內容請見"\(PA=LU\)分解"。
https://ccjou.wordpress.com/2012/04/13/palu-分解/



LU分解副程式
(%i1)
LU(A):=block
([n:length(A),U,L],
print("初始值L=",L:ident(n),",U=",U:A),
print("------------------"),
for i:1 thru n do
   (for j:i+1 thru n do
      (L[j,i]:U[j,i]/U[i,i],
       print("設L[",j,",",i,"]=U[",j,",",i,"]/U[",i,",",i,"]=",L[j,i],",L=",L),
       print("原本U=",U,",第",j,"列-",L[j,i],"*第",i,"列,得到U=",U:rowop(U,j,i,L[j,i])),
       print("------------------")
      )
   ),
return([L,U])
)$


本文使用的範例
(%i2)
A:matrix([-4,0,1],
              [-1,3,1],
              [1,2,5]);

(%o2) \( \left[ \matrix{-4&0&1\cr -1&3&1\cr 1&2&5} \right] \)

執行LU分解
(%i3) [L,U]: LU(A);
初始值\(L=\left[ \matrix{1&0&0\cr 0&1&0\cr 0&0&1} \right]\),\(U=\left[ \matrix{-4&0&1\cr -1&3&1\cr 1&2&5} \right]\)
------------------
設\( \displaystyle L [\; 2,1 ]\;=U[\; 2,1 ]\; / U[\; 1,1 ]\;=\frac{1}{4} \),\(L=\left[ \matrix{\displaystyle 1&0&0\cr \frac{1}{4}&1&0\cr 0&0&1} \right]\)
0 errors, 0 warnings
原本\(U=\left[ \matrix{-4&0&1\cr -1&3&1\cr 1&2&5} \right]\),第2列\( \displaystyle -\frac{1}{4}* \)第1列,得到\(U=\left[ \matrix{\displaystyle -4&0&1\cr 0&3&\frac{3}{4} \cr 1&2&5} \right]\)
------------------
設\( \displaystyle L [\; 3,1 ]\;=U[\; 3,1 ]\; / U[\; 1,1 ]\;=-\frac{1}{4} \),\(L=\left[ \matrix{\displaystyle 1&0&0\cr \frac{1}{4}&1&0\cr -\frac{1}{4}&0&1} \right]\)
原本\(U=\left[ \matrix{\displaystyle -4&0&1\cr 0&3&\frac{3}{4}\cr 1&2&5} \right]\),第3列\( \displaystyle --\frac{1}{4}* \)第1列,得到\(U=\left[ \matrix{\displaystyle -4&0&1\cr 0&3&\frac{3}{4}\cr 0&2&\frac{21}{4}} \right]\)
------------------
設\( \displaystyle L [\; 3,2 ]\;=U[\; 3,2 ]\; / U[\; 2,2 ]\;=\frac{2}{3} \),\(L=\left[ \matrix{\displaystyle 1&0&0 \cr \frac{1}{4}&1&0\cr -\frac{1}{4}&\frac{2}{3}&1} \right]\)
原本\(U=\left[ \matrix{\displaystyle -4&0&1\cr 0&3&\frac{3}{4}\cr 0&2&\frac{21}{4}} \right]\),第3列\( \displaystyle -\frac{2}{3}* \)第2列,得到\(U=\left[ \matrix{\displaystyle -4&0&1\cr 0&3&\frac{3}{4}\cr 0&0&\frac{19}{4}} \right]\)
------------------
(%o3) \( [\; \left[ \matrix{\displaystyle 1&0&0 \cr \frac{1}{4}&1&0\cr -\frac{1}{4}&\frac{2}{3}&1} \right],\left[ \matrix{\displaystyle -4&0&1 \cr 0&3&\frac{3}{4} \cr 0&0&\frac{19}{4}} \right] ]\; \)

利用maxima內建指令得到相同的答案
(%i4) [P,L,U]:get_lu_factors(lu_factor(A));
(%o4) \( [\; \left[ \matrix{1&0&0\cr 0&1&0\cr 0&0&1} \right],\left[ \matrix{\displaystyle 1&0&0\cr \frac{1}{4}&1&0\cr -\frac{1}{4}&\frac{2}{3}&1} \right],\left[ \matrix{\displaystyle -4&0&1\cr 0&3&\frac{3}{4}\cr 0&0&\frac{19}{4}} \right] ]\; \)

線代啟示錄文章中的範例
(%i5)
A:matrix([3,-1,2],
              [6,-1,5],
              [-9,7,3]);

(%o5) \( \left[ \matrix{3&-1&2\cr 6&-1&5\cr -9&7&3} \right] \)

(%i6) [L,U]: LU(A);
初始值\(L=\left[ \matrix{1&0&0\cr 0&1&0\cr 0&0&1} \right]\),\(U=\left[ \matrix{3&-1&2\cr 6&-1&5\cr -9&7&3} \right]\)
------------------
設\( \displaystyle L [\; 2,1 ]\;=U[\; 2,1 ]\; / U[\; 1,1 ]\;=2 \),\(L=\left[ \matrix{1&0&0\cr 2&1&0\cr 0&0&1} \right]\)
原本\(U=\left[ \matrix{3&-1&2\cr 6&-1&5\cr -9&7&3} \right]\),第2列\(-2*\)第1列,得到\(U=\left[ \matrix{3&-1&2\cr 0&1&1\cr -9&7&3} \right]\)
------------------
設\( \displaystyle L [\; 3,1 ]\;=U[\; 3,1 ]\; / U[\; 1,1 ]\;=-3 \),\(L=\left[ \matrix{1&0&0\cr 2&1&0\cr -3&0&1} \right]\)
原本\(U=\left[ \matrix{3&-1&2\cr 0&1&1\cr -9&7&3} \right]\),第3列\(--3*\)第1列,得到\(U=\left[ \matrix{3&-1&2\cr 0&1&1\cr 0&4&9} \right]\)
------------------
設\( \displaystyle L [\; 3,2 ]\;=U[\; 3,2 ]\; / U[\; 2,2 ]\;=4 \),\(L=\left[ \matrix{1&0&0\cr 2&1&0\cr -3&4&1} \right]\)
原本\(U=\left[ \matrix{3&-1&2\cr 0&1&1\cr 0&4&9} \right]\),第3列\(-4*\)第2列,得到\(U=\left[ \matrix{3&-1&2\cr 0&1&1\cr 0&0&5} \right]\)
------------------
(%o6) \( [\; \left[ \matrix{1&0&0\cr 2&1&0\cr -3&4&1} \right],\left[ \matrix{3&-1&2\cr 0&1&1\cr 0&0&5} \right] ]\; \)

利用maxima內建指令得到相同的答案
(%i7) [P,L,U]:get_lu_factors(lu_factor(A));
(%o7) \( [\;\left[ \matrix{1&0&0\cr 0&1&0\cr 0&0&1} \right],\left[ \matrix{1&0&0\cr 2&1&0\cr -3&4&1} \right],\left[ \matrix{3&-1&2\cr 0&1&1\cr 0&0&5} \right] ]\; \)

[ 本帖最後由 bugmens 於 2016-9-17 11:24 AM 編輯 ]
作者: bugmens    時間: 2016-9-25 17:09

但不是每個矩陣都可以用\(LR\)法求得全部的特徵值,William Edward Stephenson提供一個特徵值無法收斂的例子。
http://krex.k-state.edu/dspace/b ... e=1&isAllowed=y

在第21頁底下,該矩陣為\( A_1=\left[ \matrix{1&-1&1\cr 4&6&-1 \cr 4&4&1} \right] \),特徵值\( \matrix{\lambda_1=5 \cr \lambda_2=2 \cr \lambda_3=1} \)
其中\(X=\left[ \matrix{0&-1&-1\cr 1&1&1 \cr 1&0&1} \right] \),\( Y=X^{-1}=\left[ \matrix{1&1&0 \cr 0&1&-1 \cr -1&-1&1} \right] \),\(D=\left[ \matrix{5&0&0 \cr 0&2&0 \cr 0&0&1} \right] \),符合\( YAX=D \)
因為矩陣\(X\)的leading principal minor為0,導致原本的\(LR\)法無法收斂。

原本的\(LR\)法
\( A_2=\left[ \matrix{1&-0.2&1 \cr 20&6&-5 \cr 4&0.8&1} \right] \),\( A_3=\left[ \matrix{1&-0.04&1 \cr 100&6&-25 \cr 4&0.16&1} \right] \),\( A_4=\left[ \matrix{1&-0.008&1\cr 500&6&-125 \cr 4&0.032&1} \right] \)
發現對角線都是\( \left[ 1,6,1 \right] \),無法收斂到正確的特徵值\( \left[ 5,2,1 \right] \),以下是利用maxima呈現\( A_2,A_3,A_4 \)的計算過程


(%i1)
A:matrix([1,-1,1],
              [4,6,-1],
              [4,4,1]);

(%o1) \( \left[ \matrix{1&-1&1\cr 4&6&-1 \cr 4&4&1}\right] \)

(%i2)
for i:2 thru 4 do
  ([P,L,U]:get_lu_factors(lu_factor(A)),
    print("計算矩陣A的LU分解"),
    print(A,"=",L,"*",U),
    A:U.L,
    print("將L和U對調後相乘得到下一個矩陣A"),
    print(U,"*",L,"=",A),
    eig:create_list(A[i,i],i,1,length(A)),
    print("特徵值",eig),
    print("------------------")
  );

0 errors, 0 warnings
計算矩陣\(A\)的\(LU\)分解
\( \left[ \matrix{1&-1&1\cr 4&6&-1 \cr 4&4&1} \right] =\left[ \matrix{\displaystyle 1&0&0\cr 4&1&0 \cr 4&\frac{4}{5}&1} \right]*\left[ \matrix{1&-1&1\cr 0&10&-5\cr 0&0&1}\right] \)
將\(L\)和\(U\)對調後相乘得到下一個矩陣\(A\)
\( \left[ \matrix{1&-1&1\cr 0&10&-5\cr 0&0&1} \right] *\left[ \matrix{\displaystyle 1&0&0\cr 4&1&0 \cr 4&\frac{4}{5}&1}\right]=\left[ \matrix{\displaystyle 1&-\frac{1}{5}&1\cr 20&6&-5 \cr 4&\frac{4}{5}&1}\right] \)
特徵值\( \left[ 1,6,1 \right] \)
------------------
計算矩陣A的LU分解
\( \left[ \matrix{\displaystyle 1&-\frac{1}{5}&1\cr 20&6&-5\cr 4&\frac{4}{5}&1}\right] =\left[\matrix{\displaystyle 1&0&0\cr 20&1&0 \cr 4&\frac{4}{25}&1}\right]*\left[ \matrix{\displaystyle 1&-\frac{1}{5}&1\cr 0&10&-25 \cr 0&0&1} \right] \)
將\(L\)和\(U\)對調後相乘得到下一個矩陣\(A\)
\( \left[ \matrix{\displaystyle 1&-\frac{1}{5}&1\cr 0&10&-25 \cr 0&0&1}\right]*\left[ \matrix{\displaystyle 1&0&0\cr 20&1&0 \cr 4&\frac{4}{25}&1}\right]=\left[ \matrix{\displaystyle 1&-\frac{1}{25}&1\cr 100&6&-25 \cr 4&\frac{4}{25}&1}\right] \)
特徵值\( \left[ 1,6,1 \right] \)
------------------
計算矩陣\(A\)的\(LU\)分解
\( \left[ \matrix{\displaystyle 1&-\frac{1}{25}&1 \cr 100&6&-25 \cr 4&\frac{4}{25}&1}\right]= \left[ \matrix{\displaystyle 1&0&0\cr 100&1&0 \cr 4&\frac{4}{125}&1}\right] * \left[ \matrix{\displaystyle 1&-\frac{1}{25}&1\cr 0&10&-125 \cr 0&0&1}\right]  \)
將\(L\)和\(U\)對調後相乘得到下一個矩陣\(A\)
\( \left[ \matrix{\displaystyle 1&-\frac{1}{25}&1\cr 0&10&-125 \cr 0&0&1}\right]*\left[ \matrix{\displaystyle 1&0&0\cr 100&1&0 \cr 4&\frac{4}{125}&1}\right]=\left[ \matrix{\displaystyle 1&-\frac{1}{125}&1\cr 500&6&-125 \cr 4&\frac{4}{125}&1}\right] \)
特徵值\( \left[ 1,6,1 \right] \)
------------------
(%o2) done



改進的方法稱為LR with interchanges,也就是將其中兩列交換後再利用\(LR\)法計算
\( \left[ \matrix{1&-1&1 \cr 4&6&-1 \cr 4&4&1}\right] \)第1列和第2列交換\( \left[ \matrix{4&6&-1\cr 1&-1&1 \cr 4&4&1}\right] \)第2列\( \displaystyle -\frac{1}{4} \)第1列\( \left[ \matrix{4&6&-1 \cr 0&-2.5&1.25 \cr 4&4&1}\right] \)
\( \left[ \matrix{4&6&-1 \cr 0&-2.5&1.25 \cr 4&4&1}\right] \)第3列\( - \)第1列\( \left[ \matrix{4&6&-1 \cr 0&-2.5&1.25 \cr 0&-2&2}\right] \)第3列\(-0.8\)第2列\( \left[ \matrix{4&6&-1\cr 0&-2.5&1.25 \cr 0&0&1}\right]=R \),就是\(LU\)分解的\(U\)矩陣

\( \left[ \matrix{4&6&-1\cr 0&-2.5&1.25 \cr 0&0&1}\right] \)第1行和第2行交換\( \left[ \matrix{6&4&-1\cr -2.5&0&1.25 \cr 0&0&1}\right] \)第1行\( \displaystyle +\frac{1}{4} \)第2行\( \left[ \matrix{7&4&-1\cr -2.5&0&1.25 \cr 0&0&1}\right] \)
\( \left[ \matrix{7&4&-1\cr -2.5&0&1.25 \cr 0&0&1}\right] \)第1行\(+\)第3行\( \left[ \matrix{6&4&-1\cr -1.25&0&1.25 \cr 1&0&1}\right] \)第2行\(+0.8\)第3行\( \left[ \matrix{6&3.2&-1\cr -1.25&1&1.25 \cr 1&0.8&1}\right]=A_2 \)
第一部份先將第1列和第2列交換後針對列做高斯消去法,將運算過程記錄下來於第二部分對行做相對應運算得到\(A_2\)。
接下來就不用再列交換和行交換,就用原本的\(LR\)法計算出
\(A_3=\left[ \matrix{5.167&3.040&-1.00\cr -0.174&1.833&1.042\cr 0.167&0.160&1.000} \right] \),\(A_4=\left[ \matrix{5.032&3.008&-1.00\cr -0.03&1.968&1.008\cr 0.032&0.032&1.000} \right]\),全部計算過程利用maxima呈現。



設定為數值運算
(%i1) numer:true;
(%o1) true

不顯示小數轉換成分數的過程
例如rat: replaced -4.25 by -17/4 = -4.25

(%i2) ratprint:false;
(%o2) false

僅顯示小數5位
(%i3) fpprintprec:5;
(%o3) 5

列交換的LR迭代法副程式
(%i4)
LRwithInterchanges(A,row1,row2,epsilon):=block
([count:0,P,L,U,n:length(A),eig1,eig2],
eig2:makelist(0,n),
do
   (count:count+1,
    print("第",count,"輪"),
    if count=1 then
      (print(A,"第",row1,"列和第",row2,"列交換",A:rowswap(A,row1,row2))),
    print("計算矩陣A的LU分解"),
    [P,L,U]:get_lu_factors(lu_factor(A)),
    print(A,"=",L,"*",U),
    if count=1 then
       (print("L=",L,"第",row1,"列和第",row2,"列交換",L:rowswap(L,row1,row2))),
     print("將L和U對調後相乘得到下一個矩陣A"),
     A:U.L,
     print(U,"*",L,"=",A),
     eig1:create_list(A[i,i],i,1,n),
     print("特徵值",eig1),
     print("------------------"),
     if lmax(abs(eig1-eig2))<epsilon then
        (return(eig1)),
     eig2:eig1
    )
)$


原本LR法特徵值無法收斂的矩陣A
(%i5)
A:matrix([1,-1,1],
              [4,6,-1],
              [4,4,1]);

(%o5) \( \left[ \matrix{1&-1&1 \cr 4&6&-1 \cr 4&4&1} \right] \)

指定第1列和第2列交換,計算矩陣A全部的特徵值,誤差10^-5
(%i6) LRwithInterchanges(A,1,2,10^-5);
第1輪
0 errors, 0 warnings
\( \left[ \matrix{1&-1&1 \cr 4&6&-1 \cr 4&4&1}\right] \)第1列和第2列交換\( \left[ \matrix{4&6&-1\cr 1&-1&1 \cr 4&4&1}\right] \)
計算矩陣\(A\)的\(LU\)分解
\( \left[ \matrix{4&6&-1\cr 1&-1&1 \cr 4&4&1}\right]=\left[ \matrix{1&0&0\cr 0.25&1&0 \cr 1.0&0.8&1}\right] * \left[ \matrix{4&6&-1\cr 0&-2.5&1.25 \cr 0&0&1.0}\right]\)
\( L=\left[ \matrix{1&0&0\cr 0.25&1&0 \cr 1.0&0.8&1}\right] \)第1列和第2列交換\( \left[ \matrix{0.25&1&0\cr 1&0&0 \cr 1.0&0.8&1}\right] \)
將\(L\)和\(U\)對調後相乘得到下一個矩陣\(A\)
\( \left[ \matrix{4&6&-1\cr 0&-2.5&1.25 \cr 0&0&1.0}\right] * \left[ \matrix{0.25&1&0\cr 1&0&0 \cr 1.0&0.8&1}\right]=\left[ \matrix{6.0&3.2&-1\cr -1.25&1.0&1.25 \cr 1.0&0.8&1.0}\right] \)
特徵值\( \left[6.0,1.0,1.0 \right] \)
------------------
第2輪
計算矩陣\(A\)的\(LU\)分解
\( \left[ \matrix{6.0&3.2&-1 \cr -1.25&1.0&1.25 \cr 1.0&0.8&1.0}\right]=\left[ \matrix{1&0&0\cr -0.20833&1&0 \cr 0.16667&0.16&1}\right] * \left[ \matrix{6.0&3.2&-1\cr 0&1.6667&1.0417 \cr 0&0&1.0}\right]\)
將\(L\)和\(U\)對調後相乘得到下一個矩陣\(A\)
\( \left[ \matrix{6.0&3.2&-1\cr 0&1.6667&1.0417 \cr 0&0&1.0}\right]*\left[ \matrix{1&0&0\cr -0.20833&1&0 \cr 0.16667&0.16&1}\right]=\left[ \matrix{5.1667&3.04&-1.0\cr -0.17361&1.8333&1.0417 \cr 0.16667&0.16&1.0}\right] \)
特徵值\( \left[ 5.1667,1.8333,1.0 \right] \)
------------------
第3輪
計算矩陣\(A\)的\(LU\)分解
\( \left[ \matrix{5.1667&3.04&-1.0\cr -0.17361&1.8333&1.0417 \cr 0.16667&0.16&1.0}\right]=\left[ \matrix{1&0&0\cr -0.033602&1&0 \cr 0.032258&0.032&1}\right]*\left[ \matrix{5.1667&3.04&-1.0\cr 0&1.9355&1.0081 \cr 0&0&1.0}\right] \)
將\(L\)和\(U\)對調後相乘得到下一個矩陣\(A\)
\( \left[ \matrix{5.1667&3.04&-1.0\cr 0&1.9355&1.0081 \cr 0&0&1.0}\right] *\left[ \matrix{1&0&0\cr -0.033602&1&0 \cr 0.032258&0.032&1}\right]=\left[ \matrix{5.0323&3.008&-1.0\cr -0.032518&1.9677&1.0081 \cr 0.032258&0.032&1.0}\right] \)
特徵值\( \left[5.0323,1.9677,1.0 \right] \)
------------------
第4輪
計算矩陣\(A\)的\(LU\)分解
\( \left[ \matrix{5.0323&3.008&-1.0\cr -0.032518&1.9677&1.0081 \cr 0.032258&0.032&1.0}\right]=\left[ \matrix{1&0&0\cr -0.006462&1&0 \cr 0.0064103&0.0064&1}\right] * \left[ \matrix{5.0323&3.008&-1.0 \cr 0&1.9872&1.0016 \cr 0&0&1.0}\right] \)
將\(L\)和\(U\)對調後相乘得到下一個矩陣\(A\)
\( \left[ \matrix{5.0323&3.008&-1.0\cr 0&1.9872&1.0016 \cr 0&0&1.0}\right]*\left[ \matrix{1&0&0\cr -0.006462&1&0 \cr 0.0064103&0.0064&1}\right]=\left[ \matrix{5.0064&3.0016&-1.0\cr -0.0064205&1.9936&1.0016 \cr 0.0064103&0.0064&1.0}\right] \)
特徵值\( \left[5.0064,1.9936,1.0 \right] \)
------------------
第5輪
計算矩陣\(A\)的\(LU\)分解
\( \left[ \matrix{5.0064&3.0016&-1.0\cr -0.0064205&1.9936&1.0016 \cr 0.0064103&0.0064&1.0}\right]=\left[ \matrix{1&0&0\cr -0.0012825&1&0 \cr 0.0012804&0.00128&1}\right]*\left[ \matrix{5.0064&3.0016&-1.0\cr 0&1.9974&1.0003 \cr 0&0&1.0}\right] \)
將\(L\)和\(U\)對調後相乘得到下一個矩陣\(A\)
\( \left[ \matrix{5.0064&3.0016&-1.0\cr 0&1.9974&1.0003 \cr 0&0&1.0}\right]*\left[ \matrix{1&0&0\cr -0.0012825&1&0 \cr 0.0012804&0.00128&1}\right]=\left[ \matrix{5.0013&3.0003&-1.0\cr -0.0012808&1.9987&1.0003 \cr 0.0012804&0.00128&1.0}\right] \)
特徵值\( \left[5.0013,1.9987,1.0 \right] \)
------------------
第6輪
計算矩陣\(A\)的\(LU\)分解
\( \left[ \matrix{5.0013&3.0003&-1.0\cr -0.0012808&1.9987&1.0003 \cr 0.0012804&0.00128&1.0}\right]=\left[ \matrix{1&0&0\cr -2.561 \cdot 10^{-4}&1&0 \cr 2.5602 \cdot 10^{-4}&2.56 \cdot 10^{-4}&1}\right]*\left[ \matrix{5.0013&3.0003&-1.0 \cr 0&1.9995&1.0001 \cr 0&0&1.0}\right] \)
將\(L\)和\(U\)對調後相乘得到下一個矩陣\(A\)
\( \left[ \matrix{5.0013&3.0003&-1.0\cr 0&1.9995&1.0001 \cr 0&0&1.0}\right]*\left[ \matrix{1&0&0\cr -2.561 \cdot 10^{-4}&1&0 \cr 2.5602 \cdot 10^{-4}&2.56 \cdot 10^{-4}&1}\right]=\left[ \matrix{5.0003&3.0001&-1.0 \cr -2.5603 \cdot 10^{-4}&1.9997&1.0001 \cr 2.5602 \cdot 10^{-4}&2.56 \cdot 10^{-4}&1.0}\right] \)
特徵值\( \left[5.0003,1.9997,1.0 \right] \)
------------------
第7輪
計算矩陣\(A\)的\(LU\)分解
\( \left[ \matrix{5.0003&3.0001&-1.0\cr -2.5603 \cdot 10^{-4}&1.9997&1.0001 \cr 2.5602 \cdot 10^{-4}&2.56 \cdot 10^{-4}&1.0}\right]=\left[ \matrix{1&0&0\cr -5.1204 \cdot 10^{-5}&1&0 \cr 5.1201 \cdot 10^{-5}&5.12 \cdot 10^{-5}&1}\right] * \left[ \matrix{5.0003&3.0001&-1.0\cr 0&1.9999&1.0 \cr 0&0&1.0}\right] \)
將\(L\)和\(U\)對調後相乘得到下一個矩陣\(A\)
\( \left[ \matrix{5.0003&3.0001&-1.0\cr 0&1.9999&1.0 \cr 0&0&1.0}\right]*\left[ \matrix{1&0&0\cr -5.1204 \cdot 10^{-5}&1&0 \cr 5.1201 \cdot 10^{-5}&5.12 \cdot 10^{-5}&1}\right]=\left[ \matrix{5.0001&3.0&-1.0\cr -5.1201 \cdot 10^{-5}&1.9999&1.0 \cr 5.1201 \cdot 10^{-5}&5.12 \cdot 10^{-5}&1.0}\right] \)
特徵值\( \left[5.0001,1.9999,1.0\right] \)
------------------
第8輪
計算矩陣\(A\)的\(LU\)分解
\( \left[ \matrix{5.0001&3.0&-1.0\cr -5.1201 \cdot 10^{-5}&1.9999&1.0 \cr 5.1201 \cdot 10^{-5}&5.12 \cdot 10^{-5}&1.0}\right]=\left[ \matrix{1&0&0\cr -1.024 \cdot 10^{-5}&1&0 \cr 1.024 \cdot 10^{-5}&1.024 \cdot 10^{-5}&1}\right]*\left[ \matrix{5.0001&3.0&-1.0\cr 0&2.0&1.0 \cr 0&0&1.0}\right] \)
將\(L\)和\(U\)對調後相乘得到下一個矩陣\(A\)
\( \left[ \matrix{5.0001&3.0&-1.0\cr 0&2.0&1.0 \cr 0&0&1.0}\right]*\left[ \matrix{1&0&0\cr -1.024 \cdot 10^{-5}&1&0 \cr 1.024 \cdot 10^{-5}&1.024 \cdot 10^{-5}&1}\right]=\left[ \matrix{5.0&3.0&-1.0\cr -1.024 \cdot 10^{-5}&2.0&1.0 \cr 1.024 \cdot 10^{-5}&1.024 \cdot 10^{-5}&1.0}\right] \)
特徵值\( \left[5.0,2.0,1.0 \right] \)
------------------
第9輪
計算矩陣\(A\)的\(LU\)分解
\( \left[ \matrix{5.0&3.0&-1.0\cr -1.024 \cdot 10^{-5}&2.0&1.0 \cr 1.024 \cdot 10^{-5}&1.024 \cdot 10^{-5}&1.0}\right]=\left[ \matrix{1&0&0 \cr -2.048 \cdot 10^{-6}&1&0 \cr 2.048 \cdot 10^{-6}&2.048 \cdot 10^{-6}&1}\right]*\left[ \matrix{5.0&3.0&-1.0 \cr 0&2.0&1.0 \cr 0&0&1.0}\right] \)
將\(L\)和\(U\)對調後相乘得到下一個矩陣\(A\)
\( \left[ \matrix{5.0&3.0&-1.0\cr 0&2.0&1.0 \cr 0&0&1.0}\right]*\left[ \matrix{1&0&0\cr -2.048 \cdot 10^{-6}&1&0 \cr 2.048 \cdot 10^{-6}&2.048 \cdot 10^{-6}&1}\right]=\left[ \matrix{5.0&3.0&-1.0\cr -2.048 \cdot 10^{-6}&2.0&1.0 \cr 2.048 \cdot 10^{-6}&2.048 \cdot 10^{-6}&1.0}\right] \)
特徵值\( \left[5.0,2.0,1.0 \right] \)
------------------
(%o6) \( \left[5.0,2.0,1.0 \right] \)

[ 本帖最後由 bugmens 於 2016-9-26 05:04 PM 編輯 ]
作者: bugmens    時間: 2016-9-27 09:24

1961年,英國計算機科學家弗朗西斯 (John G.F. Francis) 和俄國數學家卡布諾夫斯凱雅 (Vera Kublanovskaya) 獨立提出了 \(QR\)演算法。經過五十年的演化,\(QR\)演算法逐漸成為當今特徵值和特徵向量數值計算的骨幹,並被視為20世紀最重要的十個演算法之一。另一方面,由於 \(LR\)演算法的穩定性不如\(QR\)演算法,今天已鮮少被提及。

節錄自線代啟示錄
https://ccjou.wordpress.com/2013/12/10/qr-演算法-上/

弗朗西斯1961年的論文
http://comjnl.oxfordjournals.org/content/4/3/265.full.pdf+html
---------------------------
虛擬碼
1.對於\(k=0,1,2,\ldots\),直到\( \lambda(1),\lambda(2),\ldots,\lambda(n) \)收斂為止,計算
2.\(A_k=Q_k R_k\),計算\(A_k\)的\(QR\)分解。
3.\(A_{k+1}=R_k Q_k\),將\(Q_k\)和\(R_k\)對調後相乘得到下一個矩陣\(A_{k+1}\)
4.特徵值\( \lambda(1),\lambda(2),\ldots,\lambda(n) \)在矩陣\(A_{k+1}\)的對角線上。
---------------------------
計算\( A=\left[ \matrix{-4&0&1\cr -1&3&1\cr 1&2&5} \right] \)全部的特徵值。
[計算過程]
第1輪:
計算矩陣\(A\)的\(QR\)分解
\( \left[ \matrix{-4&0&1\cr -1&3&1\cr 1&2&5} \right]=\left[ \matrix{-0.9428&0.0618&0.3276\cr -0.2357&-0.8184&-0.5241\cr

0.2357&-0.5713&0.7861} \right]*\left[ \matrix{4.2426&-0.2357&2.2204 \cdot 10^{-16}\cr 0.0&-3.5978&-3.6133\cr 0.0&0.0&3.7342}

\right] \)
將\(Q\)和\(R\)對調後相乘得到下一個矩陣\(A\)
\( \left[ \matrix{4.2426&-0.2357&2.2204 \cdot 10^{-16}\cr 0.0&-3.5978&-3.6133\cr 0.0&0.0&3.7342} \right]*\left[ \matrix{-

0.9428&0.0618&0.3276\cr -0.2357&-0.8184&-0.5241\cr 0.2357&-0.5713&0.7861} \right]=\left[ \matrix{-3.9444&0.4549&1.5133\cr -

0.0036&5.0088&-0.9549\cr 0.8802&-2.1335&2.9356} \right] \)
特徵值\( \left[ -3.9444,5.0088,2.9356 \right] \)

第2輪:
計算矩陣\(A\)的\(QR\)分解
\( \left[ \matrix{-3.9444&0.4549&1.5133\cr -0.0036&5.0088&-0.9549\cr 0.8802&-2.1335&2.9356} \right]=\left[ \matrix{-

0.9760&0.0810&0.2022\cr -9.0056 \cdot 10^{-4}&-0.9298&0.3682\cr 0.2178&0.3592&0.9075} \right]*\left[ \matrix{4.0415&-0.9132&-

0.8367\cr 0.0&-5.3864&2.0648\cr 0.0&0.0&2.6184} \right] \)
將\(Q\)和\(R\)對調後相乘得到下一個矩陣\(A\)
\( \left[ \matrix{4.0415&-0.9132&-0.8367\cr 0.0&-5.3864&2.0648\cr 0.0&0.0&2.6184} \right]* \left[ \matrix{-

0.9760&0.0810&0.2022\cr -9.0056 \cdot 10^{-4}&-0.9298&0.3682\cr 0.2178&0.3592&0.9075} \right]= \left[ \matrix{-4.1259&0.8759&-

0.2785\cr 0.4545&5.7496&-0.1093\cr 0.5702&0.9404&2.3762} \right] \)
特徵值\( \left[ -4.1259,5.7496,2.3762 \right] \)

第3輪:
計算矩陣\(A\)的\(QR\)分解
\( \left[ \matrix{-4.1259&0.8759&-0.2785\cr 0.4545&5.7496&-0.1093\cr 0.5702&0.9404&2.3762} \right]= \left[ \matrix{-0.9847&-

0.1302&0.1155\cr 0.1085&-0.9781&-0.1775\cr 0.1361&-0.1622&0.9773} \right]* \left[ \matrix{4.1898&-0.1107&0.5858\cr 0.0&-

5.8904&-0.2423\cr 0.0&0.0&2.3096} \right] \)
將\(Q\)和\(R\)對調後相乘得到下一個矩陣\(A\)
\( \left[ \matrix{4.1898&-0.1107&0.5858\cr 0.0&-5.8904&-0.2423\cr 0.0&0.0&2.3096} \right]*\left[ \matrix{-0.9847&-

0.1302&0.1155\cr 0.1085&-0.9781&-0.1775\cr 0.1361&-0.1622&0.9773} \right]=\left[ \matrix{-4.0581&-0.5321&1.0763\cr -

0.6720&5.8009&0.8085\cr 0.3143&-0.3746&2.2572} \right] \)
特徵值\( \left[ -4.0581,5.8009,2.2572 \right] \)

反覆計算直到全部特徵值\( \left[ 5.7542,-4.1444,2.3902 \right] \)


----------------------------
這次程式碼需要安裝https://sourceforge.net/projects ... ows/5.38.1-Windows/
的maxima-clisp-sbcl-5.38.1.exe,若安裝maxima-sbcl-5.38.1-win64.exe在執行load(lapack);時出現以下的錯誤訊息。
While evaluating the form starting at line 13, column 0
  of #P"C:/Program Files (x86)/Maxima-sbcl-5.38.1/share/maxima/5.38.1/share/lapack/load-lapack.lisp":;
                                                                                                     ; compilation unit aborted
                                                                                                     ;   caught 1 fatal ERROR condition
;
; compilation unit aborted
;   caught 1 fatal ERROR condition
;
; compilation unit aborted
;   caught 1 fatal ERROR condition
loadfile: failed to load C:/Program Files (x86)/Maxima-sbcl-5.38.1/share/maxima/5.38.1/share/lapack/load-lapack.lisp
-- an error. To debug this try: debugmode(true);
----------------------------


要先載入lapack才能使用dgeqrf指令
(%i1) load(lapack);
;;  Loading file C:\Users\user\maxima\binary\5_38_1_5_gdf93b7b_dirty\clisp\2_49__2010_07_07___built_on_toshiba__192_168_43_3__\share\lapack\lapack-package.fas ...
;;  Loaded file C:\Users\user\maxima\binary\5_38_1_5_gdf93b7b_dirty\clisp\2_49__2010_07_07___built_on_toshiba__192_168_43_3__\share\lapack\lapack-package.fas
...有幾十行類似的訊息...
;;  Loading file C:\Users\user\maxima\binary\5_38_1_5_gdf93b7b_dirty\clisp\2_49__2010_07_07___built_on_toshiba__192_168_43_3__\share\lapack\dgemm.fas ...
;;  Loaded file C:\Users\user\maxima\binary\5_38_1_5_gdf93b7b_dirty\clisp\2_49__2010_07_07___built_on_toshiba__192_168_43_3__\share\lapack\dgemm.fas
0 errors, 0 warnings
(%o1) C:\maxima-5.38.1\share\maxima\5.38.1_5_gdf93b7b_dirty\share\lapack\lapack.mac

QR分解副程式
(%i2)
QR(A,epsilon):=block
([count:0,Q,R,n:length(A),eig1,eig2],
eig2:makelist(0,n),
do
   (count:count+1,
    /*print("第",count,"輪"),*/
    [Q,R]:dgeqrf(A),
    /*print("計算矩陣A的QR分解",A,"="),*/
    /*print(Q,"*",R),*/
    A:R.Q,
    /*print("將Q和R對調後相乘得到下一個矩陣A"),*/
    /*print(R,"*",Q,"=",A),*/
    eig1:create_list(A[i,i],i,1,n),
    print("特徵值",eig1),
    if lmax(abs(eig1-eig2))<epsilon then
      (return(eig1)),
    eig2:eig1
  )
)$


矩陣A
(%i3)
A:matrix([-4,0,1],
              [-1,3,1],
              [1,2,5]);

(%o3) \( \left[ \matrix{-4&0&1\cr -1&3&1 \cr 1&2&5} \right] \)

計算矩陣A全部的特徵值,誤差10^-5
(%i4) QR(A,10^-5);
特徵值\( \left[-3.944444444444443,5.008822126847878,2.935622317596567\right] \)
特徵值\( \left[-4.125850340136053,5.749602762836074,2.37624757729998\right] \)
特徵值\( \left[-4.05812826971517,5.800914022263142,2.257214247452029\right] \)
特徵值\( \left[-4.062424573627523,5.699337059058267,2.363087514569257\right] \)
特徵值\( \left[-3.9888058478382,5.63243637676476,2.356369471073442\right] \)
特徵值\( \left[-3.846431951098917,5.458191384059295,2.388240567039621\right] \)
特徵值\( \left[-3.626635311452812,5.244350090320102,2.382285221132707\right] \)
特徵值\( \left[-3.147542963553108,4.756572866807507,2.390970096745598\right] \)
特徵值\( \left[-2.447555281262403,4.059406511538823,2.388148769723577\right] \)
特徵值\( \left[-1.273077705628198,2.882396372616165,2.390681333012031\right] \)
特徵值\( \left[0.1887543282710361,1.421650419107125,2.389595252621835\right] \)
特徵值\( \left[1.792441558902001,-0.1828180203586464,2.39037646145664\right] \)
特徵值\( \left[3.236224515952411,-1.626214507426892,2.389989991474478\right] \)
特徵值\( \left[4.228710740781744,-2.618950141448976,2.390239400667228\right] \)
特徵值\( \left[4.9356478091601,-3.325754357659397,2.390106548499294\right] \)
特徵值\( \left[5.2817166444885,-3.671904365492856,2.390187721004352\right] \)
特徵值\( \left[5.52698054546436,-3.917123340678473,2.390142795214107\right] \)
特徵值\( \left[5.617321691451712,-4.007491179509199,2.39016948805748\right] \)
特徵值\( \left[5.695768989139763,-4.085923407784471,2.3901544186447\right] \)
特徵值\( \left[5.71433037843271,-4.104493622778735,2.390163244346017\right] \)
特徵值\( \left[5.740366480751086,-4.130524691091749,2.390158210340657\right] \)
特徵值\( \left[5.742069167509259,-4.13223030430456,2.390161136795295\right] \)
特徵值\( \left[5.75149968365114,-4.141659142342601,2.390159458691456\right] \)
特徵值\( \left[5.750232183663825,-4.140392614159591,2.39016043049576\right] \)
特徵值\( \left[5.754015952686897,-4.144175824385144,2.390159871698241\right] \)
特徵值\( \left[5.752779110658064,-4.142939305317683,2.390160194659612\right] \)
特徵值\( \left[5.754442586633282,-4.144602595320878,2.39016000868759\right] \)
特徵值\( \left[5.753645152532918,-4.143805268593751,2.390160116060828\right] \)
特徵值\( \left[5.754427402570066,-4.144587456755889,2.390160054185819\right] \)
特徵值\( \left[5.753971827867405,-4.144131917758443,2.390160089891033\right] \)
特徵值\( \left[5.754355892472823,-4.14451596178042,2.390160069307593\right] \)
特徵值\( \left[5.754108293244391,-4.14426837442645,2.390160081182056\right] \)
特徵值\( \left[5.754301676088646,-4.144461750423915,2.390160074335267\right] \)
特徵值\( \left[5.754170203599655,-4.144330281884226,2.390160078284571\right] \)
特徵值\( \left[5.754268945170628,-4.144429021177805,2.390160076007176\right] \)
特徵值\( \left[5.754199929325,-4.144360006645707,2.390160077320705\right] \)
特徵值\( \left[5.754250726832598,-4.144410803395806,2.390160076563208\right] \)
特徵值\( \left[5.754214706157051,-4.144374783157142,2.390160077000091\right] \)
特徵值\( \left[5.754240942756682,-4.14440101950482,2.390160076748139\right] \)
特徵值\( \left[5.754222198416984,-4.144382275310429,2.390160076893448\right] \)
特徵值\( \left[5.754235777643633,-4.144395854453276,2.390160076809646\right] \)
特徵值\( \left[5.754226038367267,-4.144386115225238,2.390160076857978\right] \)
(%o4) \( \left[5.754226038367267,-4.144386115225238,2.390160076857978 \right] \)
作者: bugmens    時間: 2016-10-1 00:44

1.求QR分解的方法--Gram-Schmidt

詳細方法請見線代啟示錄的"Gram-Schmidt 正交化與 QR 分解"
https://ccjou.wordpress.com/2010 ... %E5%88%86%E8%A7%A3/



利用Gram-Schmidt正交化的QR分解
(%i1)
QRbyGramSchmidt(A):=block
([columns:length(A[1]),rank:rank(A),x,y,normy,q,Q,R],
x:create_list(col(A,i),i,1,columns),
y:makelist(0,columns),
normy:makelist(0,columns),
print("1.正交化"),
for i:1 thru rank do
   (print("y",i,"=x",i,"=",y[ i ]:x[ i ]),
    for j:1 thru i-1 do
      (print("y",i,"=y",i,"-(y",j,"^T.x",i,")/(|y",j,"|^2)y",j),
       print("=",y[ i ],"-(",transpose(y[j]),".",col(A,i),")/(",y[j],".",y[j],")",y[j]),
       numerator:transpose(y[j]).col(A,i),
       denominator:y[j].y[j],
       print("=",y[ i ],"-",numerator,"/",denominator,y[j]),
       print("=",y[ i ]:y[ i ]-numerator/denominator*y[j])
      ),
    normy[ i ]:sqrt(y[ i ].y[ i ]),
    print("|y",i,"|=sqrt(",y[ i ],".",y[ i ],")=",normy[ i ]),
    print("------------------------")
   ),
   print("2.單位化得正交矩陣Q"),
   q:makelist(0,rank),
   for i:1 thru rank do
     (q[ i ]:ratsimp(y[ i ]/normy[ i ]),
      print("q",i,"=y",i,"/|y",i,"|=",y[ i ],"/",normy[ i ],"=",q[ i ])
     ),
    print("Q=",Q:transpose(lreduce(append,map(transpose,q)))),
    print("------------------------"),
    print("3.上三角矩陣R"),
    R:zeromatrix(rank,columns),
    for i:1 thru rank do
      (R[i,i]:normy[ i ],
       print("R[",i,",",i,"]=|y",i,"|=",R[i,i]),
       for j:i+1 thru columns do
         (R[i,j]:ratsimp(transpose(q[ i ]).x[j]),
          print("R[",i,",",j,"]=q",i,"^Tx",j=transpose(q[ i ]),".",x[j],"=",R[i,j])
         )
       ),
    print("R=",R),
    return([Q,R])
)$


本文使用的範例
(%i2)
A:matrix([-4,0,1],
              [-1,3,1],
              [1,2,5]);

(%o2) \( \left[ \matrix{-4&0&1\cr -1&3&1\cr 1&2&5} \right] \)

執行QR分解
(%i3) [Q,R]: QRbyGramSchmidt(A);
1.正交化
\(y1=x1=\left[ \matrix{-4 \cr -1 \cr 1} \right] \)
\( |\; y1 |\;=sqrt(\left[ \matrix{-4 \cr -1 \cr 1} \right]\cdot \left[ \matrix{-4 \cr -1 \cr 1} \right])=3 \sqrt{2} \)
------------------------
\( y2=x2=\left[ \matrix{0 \cr 3 \cr 2} \right] \)
\( y2=y2-(y1^T \cdot x2)/(|\; y1 |\;^2)y1 \)
\( =\left[ \matrix{0 \cr 3 \cr 2} \right]-(\left[ \matrix{-4&-1&1} \right]\cdot \left[ \matrix{0 \cr 3 \cr 2} \right])/(\left[ \matrix{-4 \cr -1 \cr 1} \right]\cdot \left[ \matrix{-4 \cr -1 \cr 1} \right])\left[ \matrix{-4 \cr -1 \cr 1} \right] \)
\( =\left[ \matrix{0 \cr 3 \cr 2}\right]- -1/18 \left[ \matrix{-4 \cr -1 \cr 1} \right] \)
\( =\left[ \matrix{\displaystyle -\frac{2}{9}\cr \frac{53}{18}\cr \frac{37}{18}} \right] \)
\( \displaystyle |\; y2 |\;=sqrt(\left[ \matrix{\displaystyle -\frac{2}{9}\cr \frac{53}{18}\cr \frac{37}{18}} \right]\cdot \left[ \matrix{\displaystyle -\frac{2}{9}\cr \frac{53}{18}\cr \frac{37}{18}} \right])=\frac{\sqrt{233}}{3 \sqrt{2}} \)
------------------------
\( y3=x3=\left[ \matrix{1 \cr 1 \cr 5} \right] \)
\( y3=y3-(y1^T \cdot x3)/(|\; y1 |\;^2)y1 \)
\( =\left[ \matrix{1 \cr 1 \cr 5} \right]-(\left[ \matrix{-4&-1&1} \right]\cdot \left[ \matrix{1 \cr 1 \cr 5} \right])/(\left[ \matrix{-4 \cr -1 \cr 1} \right]\cdot \left[ \matrix{-4 \cr -1 \cr 1} \right])\left[ \matrix{-4 \cr -1 \cr 1} \right] \)
\( =\left[ \matrix{1 \cr 1 \cr 5} \right]-0/18\left[ \matrix{-4 \cr -1 \cr 1} \right] \)
\( =\left[ \matrix{1 \cr 1 \cr 5} \right] \)
\( y3=y3-(y2^T \cdot x3)/(|\; y2 |\;^2)y2 \)
\( =\left[ \matrix{1 \cr 1 \cr 5} \right]-(\left[ \matrix{\displaystyle -\frac{2}{9}&\frac{53}{18}&\frac{37}{18}} \right]\cdot \left[ \matrix{1 \cr 1 \cr 5} \right])/(\left[ \matrix{\displaystyle -\frac{2}{9}\cr \frac{53}{18} \cr \frac{37}{18}} \right]\cdot \left[ \matrix{\displaystyle -\frac{2}{9}\cr \frac{53}{18} \cr \frac{37}{18}} \right])\left[ \matrix{\displaystyle -\frac{2}{9}\cr \frac{53}{18} \cr \frac{37}{18}} \right] \)
\( \displaystyle =\left[ \matrix{1 \cr 1 \cr 5} \right]-13/\frac{233}{18}\left[ \matrix{\displaystyle -\frac{2}{9}\cr \frac{53}{18} \cr \frac{37}{18}} \right] \)
\( =\left[ \matrix{\displaystyle \frac{285}{233}\cr -\frac{456}{233} \cr \frac{684}{233}} \right] \)
\( \displaystyle |\; y3 |\;=sqrt(\left[ \matrix{\displaystyle \frac{285}{233}\cr -\frac{456}{233} \cr \frac{684}{233}} \right]\cdot \left[ \matrix{\displaystyle \frac{285}{233}\cr -\frac{456}{233} \cr \frac{684}{233}} \right])=\frac{57}{\sqrt{233}} \)
------------------------
2.單位化得正交矩陣\(Q\)
\( q1=y1/|\; y1 |\;=\left[ \matrix{-4 \cr -1 \cr 1} \right]/3\sqrt{2}=\left[ \matrix{\displaystyle -\frac{2^{3/2}}{3}\cr -\frac{1}{3 \sqrt{2}}\cr \frac{1}{3\sqrt{2}}} \right] \)
\( \displaystyle q2=y2/|\; y2 |\;=\left[ \matrix{\displaystyle -\frac{2}{9}\cr \frac{53}{18} \cr \frac{37}{18}} \right] / \frac{\sqrt{233}}{3\sqrt{2}}=\left[ \matrix{\displaystyle -\frac{2^{3/2}}{3 \sqrt{233}} \cr \frac{53}{3 \sqrt{2} \sqrt{233}} \cr \frac{37}{3 \sqrt{2} \sqrt{233}}} \right] \)
\( \displaystyle q3=y3/|\; y3 |\;=\left[ \matrix{\displaystyle \frac{285}{233}\cr -\frac{456}{233} \cr \frac{684}{233}}\right]/ \frac{57}{\sqrt{233}}=\left[ \matrix{\displaystyle \frac{5}{\sqrt{233}}\cr -\frac{8}{\sqrt{233}}\cr \frac{12}{\sqrt{233}}} \right] \)
\( Q=\left[ \matrix{\displaystyle -\frac{2^{3/2}}{3}&-\frac{2^{3/2}}{3\sqrt{233}}&\frac{5}{\sqrt{233}}\cr -\frac{1}{3\sqrt{2}}&\frac{53}{3\sqrt{2}\sqrt{233}}&-\frac{8}{\sqrt{233}}\cr \frac{1}{3\sqrt{2}}&\frac{37}{3 \sqrt{2}\sqrt{233}}&\frac{12}{\sqrt{233}}} \right] \)
------------------------
3.上三角矩陣\(R\)
\( R\left[1,1 \right]=|\; y1 |\;=3 \sqrt{2} \)
\( \displaystyle R\left[1,2 \right]=q1^Tx2=\left[ \matrix{\displaystyle -\frac{2^{3/2}}{3}&-\frac{1}{3\sqrt{2}}&\frac{1}{3\sqrt{2}}} \right]\cdot \left[ \matrix{0 \cr 3 \cr 2} \right]=-\frac{1}{3\sqrt{2}} \)
\( R\left[1,3 \right]=q1^Tx3=\left[ \matrix{\displaystyle -\frac{2^{3/2}}{3}&-\frac{1}{3\sqrt{2}}&\frac{1}{3\sqrt{2}}} \right]\cdot \left[ \matrix{1 \cr 1 \cr 5} \right]=0\)
\( \displaystyle R\left[2,2 \right]=|\; y2 |\;=\frac{\sqrt{233}}{3\sqrt{2}} \)
\( \displaystyle R \left[ 2,3 \right]=q2^Tx3=\left[ \matrix{\displaystyle -\frac{2^{3/2}}{3 \sqrt{233}}&\frac{53}{3\sqrt{2}\sqrt{233}}&\frac{37}{3\sqrt{2}\sqrt{233}}} \right]\cdot \left[ \matrix{1 \cr 1 \cr 5} \right]=\frac{39\sqrt{2}}{\sqrt{233}} \)
\( \displaystyle R \left[ 3,3 \right]=|\; y3 |\;=\frac{57}{\sqrt{233}} \)
\( R=\left[ \matrix{\displaystyle 3\sqrt{2}&-\frac{1}{3\sqrt{2}}&0\cr 0&\frac{\sqrt{233}}{3\sqrt{2}}&\frac{39\sqrt{2}}{\sqrt{233}}\cr 0&0&\frac{57}{\sqrt{233}}} \right] \)
(%o3) \( [\; \left[ \matrix{\displaystyle -\frac{2^{3/2}}{3}&-\frac{2^{3/2}}{3\sqrt{233}}&\frac{5}{\sqrt{233}}\cr -\frac{1}{3\sqrt{2}}&\frac{53}{3\sqrt{2}\sqrt{233}}&-\frac{8}{\sqrt{233}}\cr \frac{1}{3\sqrt{2}}&\frac{37}{3 \sqrt{2}\sqrt{233}}&\frac{12}{\sqrt{233}}} \right],\left[ \matrix{\displaystyle 3\sqrt{2}&-\frac{1}{3\sqrt{2}}&0\cr 0&\frac{\sqrt{233}}{3\sqrt{2}}&\frac{39\sqrt{2}}{\sqrt{233}}\cr 0&0&\frac{57}{\sqrt{233}}} \right] ]\; \)

Q矩陣和R矩陣相乘得到矩陣A
(%i4) Q.R;
(%o4) \( \left[ \matrix{-4&0&1\cr -1&3&1 \cr 1&2&5} \right] \)

線代啟示錄文章中的範例
(%i5)
A:matrix([1,2,-1],
              [1,-1,2],
              [-1,1,1],
              [1,-1,2]);

(%o5) \( \left[ \matrix{1&2&-1\cr 1&-1&2\cr -1&1&1\cr 1&-1&2} \right] \)

執行QR分解
(%i6) [Q,R]: QRbyGramSchmidt(A);
1.正交化
\( y1=x1=\left[ \matrix{1\cr 1 \cr -1 \cr 1} \right] \)
\( |\; y1 |\;=sqrt(\left[ \matrix{1\cr 1\cr -1\cr 1} \right]\cdot \left[ \matrix{1\cr 1\cr -1\cr 1} \right])=2 \)
------------------------
\( y2=x2=\left[ \matrix{2\cr -1\cr 1\cr -1} \right] \)
\( y2=y2-(y1^T \cdot x2)/(|\; y1 |\;^2)y1 \)
\( =\left[ \matrix{2\cr -1\cr 1\cr -1} \right]-(\left[ \matrix{1&1&-1&1} \right]\cdot \left[ \matrix{2\cr -1\cr 1\cr -1} \right])/(\left[ \matrix{1\cr 1\cr -1\cr 1} \right]\cdot \left[ \matrix{1\cr 1\cr -1\cr 1} \right])\left[ \matrix{1\cr 1\cr -1\cr 1} \right] \)
\( =\left[ \matrix{2\cr -1\cr 1\cr -1} \right]- -1/4 \left[ \matrix{1 \cr 1\cr -1\cr 1} \right] \)
\( =\left[ \matrix{\displaystyle \frac{9}{4}\cr -\frac{3}{4}\cr \frac{3}{4}\cr -\frac{3}{4}} \right] \)
\( \displaystyle |\; y2 |\;=sqrt(\left[ \matrix{\displaystyle \frac{9}{4}\cr -\frac{3}{4}\cr \frac{3}{4}\cr -\frac{3}{4}} \right] \cdot \left[ \matrix{\displaystyle \frac{9}{4}\cr -\frac{3}{4}\cr \frac{3}{4}\cr -\frac{3}{4}} \right])=\frac{3^{3/2}}{2} \)
------------------------
\( y3=x3=\left[ \matrix{-1 \cr 2 \cr 1 \cr 2} \right] \)
\( y3=y3-(y1^T \cdot x3)/(|\; y1 |\;^2)y1 \)
\( =\left[ \matrix{-1 \cr 2 \cr 1 \cr 2} \right]-(\left[ \matrix{1&1&-1&1} \right]\cdot \left[ \matrix{-1 \cr 2 \cr 1 \cr 2} \right])/(\left[ \matrix{1 \cr 1 \cr -1 \cr 1} \right] \cdot \left[ \matrix{1 \cr 1 \cr -1 \cr 1} \right])\left[ \matrix{1 \cr 1 \cr -1 \cr 1} \right] \)
\( =\left[ \matrix{-1\cr 2 \cr 1 \cr 2} \right]-2/4 \left[ \matrix{1 \cr 1 \cr -1 \cr 1} \right] \)
\( =\left[ \matrix{\displaystyle -\frac{3}{2}\cr \frac{3}{2}\cr \frac{3}{2}\cr \frac{3}{2}} \right] \)
\( y3=y3-(y2^T \cdot x3)/(|\; y2 |\;^2)y2 \)
\( =\left[ \matrix{\displaystyle -\frac{3}{2}\cr \frac{3}{2}\cr \frac{3}{2}\cr \frac{3}{2}} \right]-(\left[ \matrix{\frac{9}{4}&-\frac{3}{4}&\frac{3}{4}&-\frac{3}{4}} \right]\cdot \left[ \matrix{-1\cr 2\cr 1\cr 2} \right])/(\left[ \matrix{\displaystyle \frac{9}{4}\cr -\frac{3}{4}\cr \frac{3}{4}\cr -\frac{3}{4}} \right] \cdot \left[ \matrix{\displaystyle \frac{9}{4}\cr -\frac{3}{4}\cr \frac{3}{4}\cr -\frac{3}{4}} \right])\left[ \matrix{\displaystyle \frac{9}{4}\cr -\frac{3}{4}\cr \frac{3}{4}\cr -\frac{3}{4}} \right] \)
\( \displaystyle =\left[ \matrix{\displaystyle -\frac{3}{2}\cr \frac{3}{2}\cr \frac{3}{2}\cr \frac{3}{2}} \right]--\frac{9}{2}/\frac{27}{4}\left[ \matrix{\displaystyle \frac{9}{4}\cr -\frac{3}{4}\cr \frac{3}{4}\cr -\frac{3}{4}} \right] \)
\( =\left[ \matrix{0\cr 1\cr 2\cr 1} \right] \)
\( |\; y3 |\;=sqrt(\left[ \matrix{0\cr 1\cr 2\cr 1} \right] \cdot \left[ \matrix{0\cr 1\cr 2\cr 1} \right])=\sqrt{6} \)
------------------------
2.單位化得正交矩陣\(Q\)
\( q1=y1/ |\; y1 |\;=\left[ \matrix{1 \cr 1 \cr -1 \cr 1} \right]/2=\left[ \matrix{\displaystyle \frac{1}{2}\cr \frac{1}{2}\cr -\frac{1}{2}\cr\frac{1}{2}} \right] \)
\( \displaystyle q2=y2/ |\; y2 |\;=\left[ \matrix{\displaystyle \frac{9}{4}\cr -\frac{3}{4}\cr\frac{3}{4}\cr -\frac{3}{4}} \right]/ \frac{3^{3/2}}{2}=\left[ \matrix{\displaystyle \frac{\sqrt{3}}{2}\cr -\frac{1}{2\sqrt{3}}\cr\frac{1}{2\sqrt{3}}\cr -\frac{1}{2\sqrt{3}}} \right] \)
\( q3=y3/ |\; y3 |\;=\left[ \matrix{0\cr 1\cr 2\cr 1} \right]/ \sqrt{6}=\left[ \matrix{\displaystyle 0\cr\frac{1}{\sqrt{6}}\cr\frac{2}{\sqrt{6}}\cr\frac{1}{\sqrt{6}}} \right] \)
\( Q=\left[ \matrix{\displaystyle \frac{1}{2}&\frac{\sqrt{3}}{2}&0\cr \frac{1}{2}&-\frac{1}{2\sqrt{3}}&\frac{1}{\sqrt{6}}\cr -\frac{1}{2}&\frac{1}{2\sqrt{3}}&\frac{2}{\sqrt{6}}\cr \frac{1}{2}&-\frac{1}{2\sqrt{3}}&\frac{1}{\sqrt{6}}} \right] \)
------------------------
3.上三角矩陣\(R\)
\( R[1,1]=|\; y1 |\;=2 \)
\( \displaystyle R[1,2]=q1^Tx2=\left[ \matrix{\displaystyle \frac{1}{2}&\frac{1}{2}&-\frac{1}{2}&\frac{1}{2}} \right]\cdot \left[ \matrix{2\cr -1\cr 1\cr -1} \right]=-\frac{1}{2} \)
\( R[1,3]=q1^Tx3=\left[ \matrix{\displaystyle \frac{1}{2}&\frac{1}{2}&-\frac{1}{2}&\frac{1}{2}} \right] \cdot \left[ \matrix{-1\cr 2\cr 1\cr 2} \right]=1 \)
\( \displaystyle R[2,2]=|\; y2 |\;=\frac{3^{3/2}}{2} \)
\( R[2,3]=q2^Tx3=\left[ \matrix{\displaystyle \frac{\sqrt{3}}{2}& -\frac{1}{2 \sqrt{3}}& \frac{1}{2 \sqrt{3}}& -\frac{1}{2\sqrt{3}}} \right] \cdot \left[ \matrix{-1\cr 2 \cr 1 \cr 2} \right]=-\sqrt{3} \)
\( R[3,3]=|\; y3 |\;=\sqrt{6} \)
\( R=\left[ \matrix{\displaystyle 2&-\frac{1}{2}&1\cr 0&\frac{3^{3/2}}{2}&-\sqrt{3} \cr 0&0&\sqrt{6}} \right] \)
(%o6) \( [\left[ \matrix{\displaystyle \frac{1}{2}&\frac{\sqrt{3}}{2}&0\cr \frac{1}{2}&-\frac{1}{2\sqrt{3}}&\frac{1}{\sqrt{6}}\cr -\frac{1}{2}&\frac{1}{2\sqrt{3}}&\frac{2}{\sqrt{6}}\cr \frac{1}{2}&-\frac{1}{2\sqrt{3}}&\frac{1}{\sqrt{6}}} \right],\left[ \matrix{\displaystyle 2&-\frac{1}{2}&1\cr 0&\frac{3^{3/2}}{2}&-\sqrt{3} \cr 0&0&\sqrt{6}} \right]] \)

Q矩陣和R矩陣相乘得到矩陣A
(%i7) Q.R;
(%o7) \( \left[ \matrix{1&2&-1\cr 1&-1&2\cr -1&1&1\cr 1&-1&2} \right] \)

[ 本帖最後由 bugmens 於 2016-10-29 03:37 PM 編輯 ]
作者: bugmens    時間: 2016-10-8 23:11

2.求QR分解的方法--Householder

詳細方法請見線代啟示錄的"Householder 變換於 QR 分解的應用"
https://ccjou.wordpress.com/2011 ... qr-分解的應用/



要先載入diag才能使用diag指令
(%i1) load(diag);
(%o1) C:\maxima-5.38.1\share\maxima\5.38.1_5_gdf93b7b_dirty\share\contrib\diag.mac

執行1/(√3-√2)類型的分母有理化
(%i2) algebraic : true;
(%o2) true

利用Householder的QR分解
(%i3)
QRbyHouseholder(A):=block
([columns:length(A[1]),rows:length(A),rank:rank(A),x,e1,v,Hhat,Ahat,I,H,Q,R,list],
print("1.初始值"),
Q:ident(rows),
R:A,
Ahat:A,
print("Q=",Q,",R=",R,",A^=",Ahat),
for i:1 thru rank do
  (print("2.計算H矩陣"),
   x:col(Ahat,1),
   e1:zeromatrix(rows+1-i,1),
   e1[1,1]:1,
   delta:ratsimp(sqrt(x.x)),
   v:x-delta*e1,
   print("A矩陣第1行x=",x,",第1元為1其餘為0的行向量e1=",e1,",x的長度δ=",delta),
   if sqrt(v.v)=0 then
     (print("x-δe1=",x,"-",delta,"*",e1,"=",v,"為0向量,結束")
     )
   else/*若x-δe1不為0向量*/
     (print("v=(x-δe1)/|x-δe1|=",x,"-",delta,"*",e1,"/sqrt(",v,".",v,")=",v,"/",sqrt(v.v),"=",v:ratsimp(v/sqrt(v.v))),
      I:ident(length(Ahat)),
      Hhat:ratsimp(I-2*v.transpose(v)),
      print("H^=I-2*v.v^T=",I,"-2*",v,".",transpose(v),"=",Hhat),
      if i=1 then/*第一次的H矩陣對角線不用補1,所以H=H^*/
        (H:Hhat,
         print("H矩陣就是H^=",H)
        )
      else/*第2次之後H矩陣對角線要補上1,讓H矩陣大小和A矩陣相同*/
        (H:diag([ident(i-1),Hhat]),
         print("H^對角線補上1形成H矩陣=",H)
        ),   
      print("3,計算Q矩陣和R矩陣"),
      print("Q=Q.H=",Q,".",H,"=",Q:ratsimp(Q.H)),
      print("R=H.R=",H,".",R,"=",R:ratsimp(H.R)),
      if i<rank then   
        (print("取R矩陣右下角得到下一個A^=",Ahat:ratsimp(submatrix(1,Hhat.Ahat,1)))),
      print("------------------------")
     )
  ),
  if rows#rank then/*刪除R矩陣0列和Q矩陣對應的行*/
    (list:makelist(i,i,rank(A)+1,rows),
     print("4-1.刪除Q矩陣的第",list,"行"),
     print(Q,"=>",Q:apply(submatrix,cons(Q,list))),
     print("4-2,刪除R矩陣的第",list,"列"),
     print(R,"=>",R:apply(submatrix,reverse(cons(R,list))))
    ),
  for i:1 thru rank do/*讓R矩陣對角線元素為正*/
    (if R[i,i]<0 then
       (print("R[",i,",",i,"]=",R[i,i],"<0"),
        print("R矩陣第",i,"列",R[ i ],",Q矩陣第",i,"行",col(Q,i),"乘上-1倍"),
        R[ i ]:-R[ i ],
        Q:columnop(Q,i,i,2)
       )
    ),
  return([Q,R])
)$


線代啟示錄文章中的範例
(%i4)
A:matrix([0,-15,14],
              [4,32,2],
              [3,-1,4]);

(%o4) \( \left[ \matrix{0&-15&14\cr 4&32&2\cr 3&-1&4} \right] \)

執行QR分解               
(%i5) [Q,R]: QRbyHouseholder(A);
1.初始值
\( Q=\left[ \matrix{1&0&0\cr 0&1&0 \cr 0&0&1} \right],R=\left[ \matrix{0&-15&14\cr 4&32&2 \cr 3&-1&4} \right],\hat{A}=\left[ \matrix{0&-15&14\cr 4&32&2 \cr 3&-1&4} \right] \)
2.計算\(H\)矩陣
\(A\)矩陣第1行\(x=\left[ \matrix{0\cr 4\cr 3}\right] \),第1元為1其餘為0的行向量\( e1=\left[ \matrix{1 \cr 0\cr 0} \right],x \)的長度\( \delta=5 \)
\( v=(x-\delta e1)/|\; x-\delta e1 |\;=\left[ \matrix{\displaystyle 0\cr 4\cr 3} \right]-5 *\left[ \matrix{1\cr 0\cr 0} \right]/sqrt(\left[ \matrix{-5\cr 4\cr 3} \right] \cdot \left[ \matrix{-5\cr 4\cr 3} \right])=\left[ \matrix{-5\cr 4\cr 3} \right]/5\sqrt{2}=\left[ \matrix{\displaystyle -\frac{1}{\sqrt{2}}\cr \frac{2^{3/2}}{5}\cr \frac{3}{5\sqrt{2}}} \right] \)
\( \hat{H}=I-2*v \cdot v^T=\left[ \matrix{1&0&0\cr 0&1&0 \cr 0&0&1} \right]-2*\left[ \matrix{\displaystyle -\frac{1}{\sqrt{2}}\cr \frac{2^{3/2}}{5}\cr \frac{3}{5\sqrt{2}}}\right] \cdot \left[ \matrix{\displaystyle -\frac{1}{\sqrt{2}}&\frac{2^{3/2}}{5}&\frac{3}{5\sqrt{2}}} \right]=\left[ \matrix{\displaystyle 0&\frac{4}{5}&\frac{3}{5}\cr \frac{4}{5}&\frac{9}{25}&-\frac{12}{25} \cr \frac{3}{5}&-\frac{12}{25}&\frac{16}{25}} \right] \)
\(H\)矩陣就是\( \hat{H}=\left[\matrix{\displaystyle 0&\frac{4}{5}&\frac{3}{5}\cr \frac{4}{5}&\frac{9}{25}&-\frac{12}{25} \cr \frac{3}{5}&-\frac{12}{25}&\frac{16}{25}} \right] \)
3.計算\(Q\)矩陣和\(R\)矩陣
\( Q=Q \cdot H=\left[ \matrix{1&0&0\cr 0&1&0 \cr 0&0&1} \right]\cdot \left[ \matrix{\displaystyle 0&\frac{4}{5}&\frac{3}{5}\cr \frac{4}{5}&\frac{9}{25}&-\frac{12}{25} \cr \frac{3}{5}&-\frac{12}{25}&\frac{16}{25}} \right]=\left[ \matrix{\displaystyle 0&\frac{4}{5}&\frac{3}{5}\cr \frac{4}{5}&\frac{9}{25}&-\frac{12}{25} \cr \frac{3}{5}&-\frac{12}{25}&\frac{16}{25}} \right] \)
\( R=H \cdot R=\left[ \matrix{\displaystyle 0&\frac{4}{5}&\frac{3}{5}\cr \frac{4}{5}&\frac{9}{25}&-\frac{12}{25} \cr \frac{3}{5}&-\frac{12}{25}&\frac{16}{25}} \right] \cdot \left[ \matrix{0&-15&14\cr 4&32&2 \cr 3&-1&4}\right]=\left[ \matrix{5&25&4\cr 0&0&10 \cr 0&-25&10} \right] \)
取\(R\)矩陣右下角得到下一個\( \hat{A}=\left[ \matrix{0&10\cr -25&10\cr } \right]\)
------------------------
2.計算\(H\)矩陣
\( A矩陣第1行x=\left[ \matrix{0 \cr -25} \right] \),第1元為1其餘為0的行向量\( e1=\left[ \matrix{1 \cr 0} \right],x \)的長度\( \delta=25 \)
\( v=(x-\delta e1)/|\; x-\delta e1 |\;=\left[ \matrix{0 \cr -25} \right]-25*\left[ \matrix{1 \cr 0} \right]/sqrt(\left[ \matrix{-25 \cr -25} \right] \cdot \left[ \matrix{-25 \cr -25} \right])=\left[ \matrix{-25 \cr -25} \right]/25 \sqrt{2}=\left[ \matrix{\displaystyle -\frac{1}{\sqrt{2}} \cr -\frac{1}{\sqrt{2}}} \right] \)
\( \hat{H}=I-2*v \cdot v^T=\left[ \matrix{1&0\cr 0&1} \right]-2*\left[ \matrix{\displaystyle -\frac{1}{\sqrt{2}}\cr -\frac{1}{\sqrt{2}}} \right] \cdot \left[ \matrix{\displaystyle -\frac{1}{\sqrt{2}} & -\frac{1}{\sqrt{2}}} \right]=\left[ \matrix{0&-1 \cr -1&0} \right] \)
\( \hat{H} \)對角線補上1形成\(H\)矩陣\( =\left[ \matrix{1&0&0\cr 0&0&-1 \cr 0&-1&0} \right] \)
3.計算\(Q\)矩陣和\(R\)矩陣
\( Q=Q \cdot H=\left[ \matrix{\displaystyle 0&\frac{4}{5}&\frac{3}{5}\cr \frac{4}{5}&\frac{9}{25}&-\frac{12}{25} \cr \frac{3}{5}&-\frac{12}{25}&\frac{16}{25}} \right]\cdot \left[ \matrix{1&0&0\cr 0&0&-1 \cr 0&-1&0} \right]=\left[ \matrix{\displaystyle 0&-\frac{3}{5}&-\frac{4}{5}\cr \frac{4}{5}&\frac{12}{25}&-\frac{9}{25} \cr \frac{3}{5}&-\frac{16}{25}&\frac{12}{25}} \right] \)
\( R=H \cdot R=\left[ \matrix{1&0&0\cr 0&0&-1 \cr 0&-1&0} \right]\cdot \left[ \matrix{5&25&4\cr 0&0&10 \cr 0&-25&10} \right]=\left[ \matrix{5&25&4\cr 0&25&-10 \cr 0&0&-10} \right] \)
取\(R\)矩陣右下角得到下一個\( \hat{A}=[-10] \)
------------------------
2.計算\(H\)矩陣
\(A\)矩陣第1行\(x=\left[ -10 \right]\),第1元為1其餘為0的行向量\(e1=\left[ 1 \right]\),\(x\)的長度\( \delta=10 \)
\( v=(x-\delta e1)/ |\; x-\delta e1 |\;=[-10]-10*[1]/sqrt([-20]\cdot [-20])=[-20]/20=[-1] \)
\( \hat{H}=I-2*v \cdot v^T=[1]-2*[-1]\cdot [-1]=[-1] \)
\( \hat{H} \)對角線補上1形成\(H\)矩陣\(=\left[ \matrix{1&0&0 \cr 0&1&0 \cr 0&0&-1} \right]\)
3.計算\(Q\)矩陣和\(R\)矩陣
\( Q=Q \cdot H=\left[ \matrix{\displaystyle 0&-\frac{3}{5}&-\frac{4}{5} \cr \frac{4}{5}&\frac{12}{25}&-\frac{9}{25} \cr\frac{3}{5}&-\frac{16}{25}&\frac{12}{25}} \right] \cdot \left[ \matrix{1&0&0\cr 0&1&0\cr 0&0&-1} \right]=\left[ \matrix{\displaystyle 0&-\frac{3}{5}&\frac{4}{5} \cr \frac{4}{5}&\frac{12}{25}&\frac{9}{25} \cr\frac{3}{5}&-\frac{16}{25}&-\frac{12}{25}} \right] \)
\( R=H \cdot R=\left[ \matrix{1&0&0\cr 0&1&0\cr 0&0&-1} \right] \cdot \left[ \matrix{5&25&4\cr 0&25&-10\cr 0&0&-10} \right]=\left[ \matrix{5&25&4\cr 0&25&-10\cr 0&0&10} \right] \)
(%o5) \( [\; \left[ \matrix{\displaystyle 0&-\frac{3}{5}&\frac{4}{5}\cr \frac{4}{5}&\frac{12}{25}&\frac{9}{25} \cr \frac{3}{5}&-\frac{16}{25}&-\frac{12}{25}} \right],\left[ \matrix{5&25&4\cr 0&25&-10 \cr 0&0&10} \right] ]\; \)

Q矩陣和R矩陣相乘得到矩陣A
(%i6) ratsimp(Q.R);
(%o6) \( \left[ \matrix{0&-15&14\cr 4&32&2 \cr 3&-1&4} \right] \)

和前一篇用GramSchmidt方法比較
(%i7)
A:matrix([1,2,-1],
              [1,-1,2],
              [-1,1,1],
              [1,-1,2]);

(%o7) \( \left[ \matrix{1&2&-1\cr 1&-1&2 \cr -1&1&1 \cr 1&-1&2} \right] \)

執行QR分解
(%i8) [Q,R]: QRbyHouseholder(A);
………
計算過程太繁雜不列出來,請自行執行程式。
………

(%o8) \( [\; \left[ \matrix{\displaystyle \frac{1}{2}&\frac{\sqrt{3}}{2}&0 \cr
\frac{1}{2}&-\frac{1}{2\sqrt{3}}&\frac{1}{\sqrt{6}}\cr
-\frac{1}{2}&\frac{1}{2\sqrt{3}}&\frac{\sqrt{6}}{3} \cr
\frac{1}{2}&-\frac{1}{2\sqrt{3}}&\frac{1}{\sqrt{6}} } \right],
\left[ \matrix{\displaystyle 2&-\frac{1}{2}&1\cr 0&\frac{3^{3/2}}{2}&-\sqrt{3}\cr 0&0&\sqrt{6}} \right] ]\; \)

Q矩陣和R矩陣相乘得到矩陣A
(%i9) ratsimp(Q.R);
(%o9) \( \left[ \matrix{1&2&-1\cr 1&-1&2\cr -1&1&1\cr 1&-1&2} \right] \)



註:若沒加algebraic : true,執行結果為
\( [ \left[ \matrix{\displaystyle
\frac{1}{2}&-\frac{3^{3/2}-3}{2\sqrt{3}-6}&0 \cr
\frac{1}{2}&\frac{\sqrt{3}-1}{2 \sqrt{3}-6}&-\frac{(313496\sqrt{3}-542991)\sqrt{6}-767444 \sqrt{3}+1329252}{(767444\sqrt{3}-1329252)\sqrt{6}-626992 \cdot 3^{3/2}+3257946} \cr
-\frac{1}{2}&-\frac{\sqrt{3}-1}{2\sqrt{3}-6}&-\frac{(22508\sqrt{3}-38985)\sqrt{6}-55100\sqrt{3}+95436}{(27550\sqrt{3}-47718)\sqrt{6}-22508 \cdot 3^{3/2}+116955} \cr
\frac{1}{2}&\frac{\sqrt{3}-1}{2\sqrt{3}-6}&-\frac{(22508\sqrt{3}-38985)\sqrt{6}-55100\sqrt{3}+95436}{(55100\sqrt{3}-95436)\sqrt{6}-45016 \cdot 3^{3/2}+233910}} \right], \)
\( \left[ \matrix{\displaystyle 2&-\frac{1}{2}&1 \cr 0&-\frac{3^{5/2}-9}{2\sqrt{3}-6}&\frac{3^{3/2}-3}{\sqrt{3}-3} \cr 0&0&-\frac{(22508 \cdot 3^{3/2}-116955)\sqrt{6}-55100 \cdot 3^{3/2}+286308}{(27550\sqrt{3}-47718)\sqrt{6}-22508 \cdot 3^{3/2}+116955}} \right] \)

[ 本帖最後由 bugmens 於 2016-11-12 08:43 AM 編輯 ]
作者: bugmens    時間: 2016-10-10 18:38

3.求QR分解的方法--GivensRotation

詳細方法請見線代啟示錄的"Givens 旋轉於 QR 分解的應用"
https://ccjou.wordpress.com/2010 ... qr-分解的應用/



利用Givens旋轉的QR分解
(%i1)
QRbyGivensRotation(A):=block
([columns:length(A[1]),rows:length(A),rank:rank(A),n,r,c,s,Qji,list,Q,R],
n:max(columns,rows),
print("1.初始值"),
Q:ident(rows),
R:copymatrix(A),
print("Q=",Q,",R=",R),
for i:1 thru min(columns,rows) do
    (for j:i+1 thru rows do
       (if R[j,i]#0 then/*若R[j,i]已經是0就不處理*/
          (print("2.以R[",i,",",i,"]=",R[i,i],"消掉R[",j,",",i,"]=",R[j,i],"為0的旋轉矩陣Q",j,i),
           print("r=sqrt(R[",i,",",i,"]^2+R[",j,",",i,"]^2)=",r:sqrt(R[i,i]^2+R[j,i]^2)),
           c:R[i,i]/r,
           s:-R[j,i]/r,
           print("cosφ=R[",i,",",i,"]/r=",R[i,i],"/",r,"=",c),
           print("sinφ=-R[",j,",",i,"]/r=-",R[j,i],"/",r,"=",s),
           Qji:ident(rows),
           Qji[i,i]:c,    Qji[i,j]:-s,
           Qji[j,i]:s,    Qji[j,j]:c,
           print("Q",j,i,"[",i,",",i,"]=cosφ=",c,",    Q",j,i,"[",i,",",j,"]=-sinφ=",-s),
           print("Q",j,i,"[",j,",",i,"]=sinφ=",s,",     Q",j,i,"[",j,",",j,"]=cosφ=",c),
           print("旋轉矩陣Q",j,i,"=",Qji),
           print("3.更新Q矩陣和R矩陣"),
           print("Q=Q.Q",j,i,"^T=",Q,".",transpose(Qji),"=",Q:ratsimp(Q.transpose(Qji))),
           print("R=Q",j,i,".R=",Qji,".",R,"=",R:ratsimp(Qji.R)),
           print("----------------------")
          )
       )
    ),
if rows#rank then
  (list:makelist(i,i,rank(A)+1,rows),
   print("4-1.刪除Q矩陣的第",list,"行"),
   print(Q,"=>",Q:apply(submatrix,cons(Q,list))),
   print("4-2.刪除R矩陣的第",list,"列"),
   print(R,"=>",R:apply(submatrix,reverse(cons(R,list))))
  ),
for i:1 thru rank do
  (if R[i,i]<0 then
    (print("R[",i,",",i,"]=",R[i,i],"<0"),
     print("R矩陣第",i,"列",R[ i ],",Q矩陣第",i,"行",col(Q,i),"乘上-1倍"),
     R[ i ]:-R[ i ],
     Q:columnop(Q,i,i,2)
    )
  ),
return(ratsimp([Q,R]))
)$


線代啟示錄文章中的範例
(%i2)
A:matrix([0,-15,14],
              [4,32,2],
              [3,-1,4]);

(%o2) \( \left[ \matrix{0&-15&14\cr 4&32&2\cr 3&-1&4} \right] \)

執行QR分解
(%i3) [Q,R]: QRbyGivensRotation(A);
1.初始值
\( Q=\left[ \matrix{1&0&0\cr 0&1&0\cr 0&0&1} \right],R=\left[ \matrix{0&-15&14\cr 4&32&2\cr 3&-1&4} \right] \)
2.以\(R[1,1]=0\)消掉\(R[2,1]=4\)為0的旋轉矩陣\(Q21\)
\(r=sqrt(R[1,1]^2+R[2,1]^2)=4\)
\(cosφ=R[1,1]/r=0/4=0\)
\(sinφ=-R[2,1]/r=-4/4=-1\)
\(Q21[1,1]=cosφ=0\),    \(Q21[1,2]=-sinφ=1\)
\(Q21[2,1]=sinφ=-1\),   \(Q21[2,2]=cosφ=0\)
旋轉矩陣\(Q21=\left[ \matrix{0&1&0\cr -1&0&0\cr 0&0&1} \right]\)
3.更新\(Q\)矩陣和\(R\)矩陣
\( Q=Q \cdot Q21^T=\left[ \matrix{1&0&0\cr 0&1&0\cr 0&0&1} \right] \cdot \left[ \matrix{0&-1&0\cr 1&0&0\cr 0&0&1} \right]=\left[ \matrix{0&-1&0\cr 1&0&0\cr 0&0&1} \right] \)
\( R=Q21 \cdot R=\left[ \matrix{0&1&0\cr -1&0&0\cr 0&0&1} \right] \cdot \left[ \matrix{0&-15&14\cr 4&32&2\cr 3&-1&4} \right]=\left[ \matrix{4&32&2\cr 0&15&-14\cr 3&-1&4} \right] \)
------------------------
2.以\(R[1,1]=4\)消掉\(R[3,1]=3\)為0的旋轉矩陣\(Q31\)
\(r=sqrt(R[1,1]^2+R[3,1]^2)=5\)
\( \displaystyle cosφ=R[1,1]/r=4/5=\frac{4}{5} \)
\( \displaystyle sinφ=-R[3,1]/r=-3/5=-\frac{3}{5} \)
\( \displaystyle Q31[1,1]=cosφ=\frac{4}{5}\),    \(\displaystyle Q31[1,3]=-sinφ=\frac{3}{5}\)
\( \displaystyle Q31[3,1]=sinφ=-\frac{3}{5}\),     \(\displaystyle Q31[3,3]=cosφ=\frac{4}{5} \)
旋轉矩陣\(Q31=\left[\matrix{\displaystyle \frac{4}{5}&0&\frac{3}{5}\cr 0&1&0\cr -\frac{3}{5}&0&\frac{4}{5}} \right]\)
3.更新\(Q\)矩陣和\(R\)矩陣
\( Q=Q \cdot Q31^T=\left[ \matrix{0&-1&0\cr 1&0&0\cr 0&0&1} \right] \cdot \left[ \matrix{\displaystyle \frac{4}{5}&0&-\frac{3}{5}\cr 0&1&0\cr \frac{3}{5}&0&\frac{4}{5}} \right]=\left[ \matrix{\displaystyle 0&-1&0\cr \frac{4}{5}&0&-\frac{3}{5}\cr \frac{3}{5}&0&\frac{4}{5}} \right] \)
\(R=Q31 \cdot R=\left[ \matrix{\displaystyle \frac{4}{5}&0&\frac{3}{5}\cr 0&1&0\cr -\frac{3}{5}&0&\frac{4}{5}} \right] \cdot \left[ \matrix{4&32&2\cr 0&15&-14\cr 3&-1&4} \right]=\left[ \matrix{5&25&4\cr 0&15&-14\cr 0&-20&2} \right] \)
------------------------
2.以\(R[2,2]=15\)消掉\(R[3,2]=-20\)為0的旋轉矩陣\(Q32\)
\(r=sqrt(R[2,2]^2+R[3,2]^2)=25\)
\( \displaystyle cosφ=R[2,2]/r=15/25=\frac{3}{5} \)
\( \displaystyle sinφ=-R[3,2]/r=--20/25=\frac{4}{5} \)
\( \displaystyle Q32[2,2]=cosφ=\frac{3}{5}\),    \( \displaystyle Q32[2,3]=-sinφ=-\frac{4}{5} \)
\( \displaystyle Q32[3,2]=sinφ=\frac{4}{5}\),    \( \displaystyle Q32[3,3]=cosφ=\frac{3}{5} \)
旋轉矩陣\( Q32=\left[ \matrix{\displaystyle 1&0&0\cr 0&\frac{3}{5}&-\frac{4}{5}\cr 0&\frac{4}{5}&\frac{3}{5}} \right] \)
3.更新\(Q\)矩陣和\(R\)矩陣
\(Q=Q \cdot Q32^T=\left[ \matrix{\displaystyle 0&-1&0\cr \frac{4}{5}&0&-\frac{3}{5}\cr \frac{3}{5}&0&\frac{4}{5}} \right]\cdot \left[ \matrix{\displaystyle 1&0&0\cr 0&\frac{3}{5}&\frac{4}{5}\cr 0&-\frac{4}{5}&\frac{3}{5}} \right]=\left[ \matrix{\displaystyle 0&-\frac{3}{5}&-\frac{4}{5}\cr \frac{4}{5}&\frac{12}{25}&-\frac{9}{25}\cr \frac{3}{5}&-\frac{16}{25}&\frac{12}{25}} \right] \)
\( R=Q32 \cdot R=\left[ \matrix{\displaystyle 1&0&0\cr 0&\frac{3}{5}&-\frac{4}{5}\cr 0&\frac{4}{5}&\frac{3}{5}} \right] \cdot \left[ \matrix{5&25&4\cr 0&15&-14\cr 0&-20&2} \right]=\left[ \matrix{5&25&4\cr 0&25&-10\cr 0&0&-10} \right] \)
------------------------
\( R[3,3]=-10<0 \)
\(R\)矩陣第3列\( [\matrix{0,0,-10}] \),\(Q\)矩陣第3行\( \left[ \matrix{\displaystyle -\frac{4}{5}\cr -\frac{9}{25}\cr \frac{12}{25}} \right] \)乘上\(-1\)倍
0 errors, 0 warnings
(%o3) \( [\; \left[ \matrix{\displaystyle 0&-\frac{3}{5}&\frac{4}{5}\cr \frac{4}{5}&\frac{12}{25}&\frac{9}{25}\cr \frac{3}{5}&-\frac{16}{25}&-\frac{12}{25}} \right],\left[ \matrix{5&25&4\cr 0&25&-10\cr 0&0&10} \right] ]\; \)

Q矩陣和R矩陣相乘得到矩陣A
(%i4) Q.R;
(%o4) \( \left[ \matrix{0&-15&14\cr 4&32&2\cr 3&-1&4} \right] \)

和前一篇用GramSchmidt方法比較
(%i5)
A:matrix([1,2,-1],
              [1,-1,2],
              [-1,1,1],
              [1,-1,2]);

(%o5) \( \left[ \matrix{1&2&-1\cr 1&-1&2\cr -1&1&1 \cr 1&-1&2} \right] \)

執行QR分解
(%i6) [Q,R]: QRbyGivensRotation(A);
1.初始值
\( Q=\left[ \matrix{1&0&0&0\cr 0&1&0&0\cr 0&0&1&0\cr 0&0&0&1} \right],R=\left[ \matrix{1&2&-1\cr 1&-1&2\cr -1&1&1 \cr 1&-1&2} \right] \)
2.以\(R[1,1]=1\)消掉\(R[2,1]=1\)為0的旋轉矩陣\(Q21\)
\( r=sqrt(R[1,1]^2+R[2,1]^2)=\sqrt{2} \)
\( \displaystyle \displaystyle cosφ=R[1,1]/r=1/ \sqrt{2}=\frac{1}{\sqrt{2}} \)
\( \displaystyle sinφ=-R[2,1]/r=-1/ \sqrt{2}=-\frac{1}{\sqrt{2}} \)
\( \displaystyle Q21[1,1]=cosφ=\frac{1}{\sqrt{2}}\),    \( \displaystyle Q21[1,2]=-sinφ=\frac{1}{\sqrt{2}} \)
\( \displaystyle Q21[2,1]=sinφ=-\frac{1}{\sqrt{2}}\),    \( \displaystyle Q21[2,2]=cosφ=\frac{1}{\sqrt{2}} \)
旋轉矩陣\( Q21=\left[ \matrix{\displaystyle \frac{1}{\sqrt{2}}&\frac{1}{\sqrt{2}}&0&0\cr -\frac{1}{\sqrt{2}}&\frac{1}{\sqrt{2}}&0&0\cr 0&0&1&0 \cr 0&0&0&1} \right] \)
3.更新\(Q\)矩陣和\(R\)矩陣
\( Q=Q \cdot Q21^T=\left[ \matrix{1&0&0&0\cr 0&1&0&0\cr 0&0&1&0 \cr 0&0&0&1} \right] \cdot \left[ \matrix{\displaystyle \frac{1}{\sqrt{2}}&-\frac{1}{\sqrt{2}}&0&0\cr \frac{1}{\sqrt{2}}&\frac{1}{\sqrt{2}}&0&0\cr 0&0&1&0 \cr 0&0&0&1} \right]=\left[ \matrix{\displaystyle \frac{1}{\sqrt{2}}&-\frac{1}{\sqrt{2}}&0&0\cr \frac{1}{\sqrt{2}}&\frac{1}{\sqrt{2}}&0&0\cr 0&0&1&0 \cr 0&0&0&1} \right] \)
\( R=Q21 \cdot R=\left[ \matrix{\displaystyle \frac{1}{\sqrt{2}}&\frac{1}{\sqrt{2}}&0&0\cr -\frac{1}{\sqrt{2}}&\frac{1}{\sqrt{2}}&0&0\cr 0&0&1&0 \cr 0&0&0&1} \right] \cdot \left[ \matrix{1&2&-1\cr 1&-1&2\cr -1&1&1 \cr 1&-1&2} \right]=\left[ \matrix{\displaystyle \sqrt{2}&\frac{1}{\sqrt{2}}&\frac{1}{\sqrt{2}}\cr 0&-\frac{3}{\sqrt{2}}&\frac{3}{\sqrt{2}}\cr -1&1&1 \cr 1&-1&2} \right] \)
------------------------
2.以\(R[1,1]=\sqrt{2}\)消掉\(R[3,1]=-1\)為0的旋轉矩陣\(Q31\)
\( r=sqrt(R[1,1]^2+R[3,1]^2)=\sqrt{3} \)
\( \displaystyle cosφ=R[1,1]/r=\sqrt{2}/\sqrt{3}=\frac{\sqrt{2}}{\sqrt{3}} \)
\( \displaystyle sinφ=-R[3,1]/r=--1/ \sqrt{3}=\frac{1}{\sqrt{3}} \)
\( \displaystyle Q31[1,1]=cosφ=\frac{\sqrt{2}}{\sqrt{3}}\),    \( \displaystyle Q31[1,3]=-sinφ=-\frac{1}{\sqrt{3}} \)
\( \displaystyle Q31[3,1]=sinφ=\frac{1}{\sqrt{3}}\),     \( \displaystyle Q31[3,3]=cosφ=\frac{\sqrt{2}}{\sqrt{3}} \)
旋轉矩陣\(Q31=\left[ \matrix{\displaystyle \frac{\sqrt{2}}{\sqrt{3}}&0&-\frac{1}{\sqrt{3}}&0\cr 0&1&0&0\cr \frac{1}{\sqrt{3}}&0&\frac{\sqrt{2}}{\sqrt{3}}&0\cr 0&0&0&1} \right] \)
3.更新\(Q\)矩陣和\(R\)矩陣
\( Q=Q.Q31^T=\left[ \matrix{\displaystyle \frac{1}{\sqrt{2}}&-\frac{1}{\sqrt{2}}&0&0\cr \frac{1}{\sqrt{2}}&\frac{1}{\sqrt{2}}&0&0\cr 0&0&1&0\cr 0&0&0&1} \right] \cdot \left[ \matrix{\displaystyle \frac{\sqrt{2}}{\sqrt{3}}&0&\frac{1}{\sqrt{3}}&0\cr 0&1&0&0 \cr -\frac{1}{\sqrt{3}}&0&\frac{\sqrt{2}}{\sqrt{3}}&0 \cr 0&0&0&1} \right]=\left[ \matrix{\displaystyle \frac{1}{\sqrt{3}}&-\frac{1}{\sqrt{2}}&\frac{1}{\sqrt{2}\sqrt{3}}&0\cr \frac{1}{\sqrt{3}}&\frac{1}{\sqrt{2}}&\frac{1}{\sqrt{2}\sqrt{3}}&0\cr -\frac{1}{\sqrt{3}}&0&\frac{\sqrt{2}}{\sqrt{3}}&0\cr 0&0&0&1} \right]\)
\( R=Q31 \cdot R=\left[ \matrix{\displaystyle \frac{\sqrt{2}}{\sqrt{3}}&0&-\frac{1}{\sqrt{3}}&0\cr 0&1&0&0\cr \frac{1}{\sqrt{3}}&0&\frac{\sqrt{2}}{\sqrt{3}}&0\cr 0&0&0&1} \right] \cdot \left[ \matrix{\displaystyle \sqrt{2}&\frac{1}{\sqrt{2}}&\frac{1}{\sqrt{2}}\cr 0&-\frac{3}{\sqrt{2}}&\frac{3}{\sqrt{2}}\cr -1&1&1\cr 1&-1&2} \right]=\left[ \matrix{\displaystyle \sqrt{3}&0&0\cr 0&-\frac{3}{\sqrt{2}}&\frac{3}{\sqrt{2}}\cr 0&\frac{\sqrt{3}}{\sqrt{2}}&\frac{\sqrt{3}}{\sqrt{2}}\cr 1&-1&2} \right] \)
------------------------
2.以\(R[1,1]=\sqrt{3}\)消掉\(R[4,1]=1\)為0的旋轉矩陣\(Q41\)
\( r=sqrt(R[1,1]^2+R[4,1]^2)=2 \)
\( \displaystyle cosφ=R[1,1]/r=\sqrt{3}/2=\frac{\sqrt{3}}{2} \)
\( \displaystyle sinφ=-R[4,1]/r=-1/2=-\frac{1}{2} \)
\( \displaystyle Q41[1,1]=cosφ=\frac{\sqrt{3}}{2}\),    \( \displaystyle Q41[1,4]=-sinφ=\frac{1}{2} \)
\( \displaystyle Q41[4,1]=sinφ=-\frac{1}{2}\),     \( \displaystyle Q41[4,4]=cosφ=\frac{\sqrt{3}}{2} \)
旋轉矩陣\(Q41=\left[ \matrix{\displaystyle \frac{\sqrt{3}}{2}&0&0&\frac{1}{2}\cr 0&1&0&0\cr 0&0&1&0\cr -\frac{1}{2}&0&0&\frac{\sqrt{3}}{2}} \right]\)
3.更新\(Q\)矩陣和\(R\)矩陣
\( Q=Q \cdot Q41^T=\left[ \matrix{\displaystyle \frac{1}{\sqrt{3}}&-\frac{1}{\sqrt{2}}&\frac{1}{\sqrt{2}\sqrt{3}}&0\cr \frac{1}{\sqrt{3}}&\frac{1}{\sqrt{2}}&\frac{1}{\sqrt{2}\sqrt{3}}&0\cr -\frac{1}{\sqrt{3}}&0&\frac{\sqrt{2}}{\sqrt{3}}&0\cr 0&0&0&1} \right] \cdot \left[ \matrix{\displaystyle \frac{\sqrt{3}}{2}&0&0&-\frac{1}{2}\cr 0&1&0&0\cr 0&0&1&0\cr \frac{1}{2}&0&0&\frac{\sqrt{3}}{2}} \right]=\left[ \matrix{\displaystyle \frac{1}{2}&-\frac{1}{\sqrt{2}}&\frac{1}{\sqrt{2}\sqrt{3}}&-\frac{1}{2\sqrt{3}}\cr \frac{1}{2}&\frac{1}{\sqrt{2}}&\frac{1}{\sqrt{2}\sqrt{3}}&-\frac{1}{2\sqrt{3}}\cr -\frac{1}{2}&0&\frac{\sqrt{2}}{\sqrt{3}}&\frac{1}{2\sqrt{3}}\cr \frac{1}{2}&0&0&\frac{\sqrt{3}}{2}} \right]\)
\( R=Q41 \cdot R=\left[ \matrix{\displaystyle \frac{\sqrt{3}}{2}&0&0&\frac{1}{2}\cr 0&1&0&0\cr 0&0&1&0\cr -\frac{1}{2}&0&0&\frac{\sqrt{3}}{2}} \right] \cdot \left[ \matrix{\displaystyle \sqrt{3}&0&0\cr 0&-\frac{3}{\sqrt{2}}&\frac{3}{\sqrt{2}}\cr 0&\frac{\sqrt{3}}{\sqrt{2}}&\frac{\sqrt{3}}{\sqrt{2}}\cr 1&-1&2} \right]=\left[ \matrix{\displaystyle 2&-\frac{1}{2}&1\cr 0&-\frac{3}{\sqrt{2}}&\frac{3}{\sqrt{2}}\cr 0&\frac{\sqrt{3}}{\sqrt{2}}&\frac{\sqrt{3}}{\sqrt{2}}\cr 0&-\frac{\sqrt{3}}{2}&\sqrt{3}} \right] \)
------------------------
2.以\( \displaystyle R[2,2]=-\frac{3}{\sqrt{2}}\)消掉\(\displaystyle R[3,2]=\frac{\sqrt{3}}{\sqrt{2}} \)為0的旋轉矩陣\(Q32\)
\( r=sqrt(R[2,2]^2+R[3,2]^2)=\sqrt{6} \)
\( \displaystyle cosφ=R[2,2]/r=-\frac{3}{\sqrt{2}}/ \sqrt{6}=-\frac{3}{\sqrt{2}\sqrt{6}} \)
\( \displaystyle sinφ=-R[3,2]/r=-\frac{\sqrt{3}}{\sqrt{2}}/ \sqrt{6}=-\frac{\sqrt{3}}{\sqrt{2}\sqrt{6}} \)
\( \displaystyle Q32[2,2]=cosφ=-\frac{3}{\sqrt{2}\sqrt{6}}\),    \(\displaystyle Q32[2,3]=-sinφ=\frac{\sqrt{3}}{\sqrt{2}\sqrt{6}} \)
\( \displaystyle Q32[3,2]=sinφ=-\frac{\sqrt{3}}{\sqrt{2}\sqrt{6}}\),     \( \displaystyle Q32[3,3]=cosφ=-\frac{3}{\sqrt{2}\sqrt{6}} \)
旋轉矩陣\( Q32=\left[ \matrix{\displaystyle 1&0&0&0\cr 0&-\frac{3}{\sqrt{2}\sqrt{6}}&\frac{\sqrt{3}}{\sqrt{2}\sqrt{6}}&0\cr 0&-\frac{\sqrt{3}}{\sqrt{2}\sqrt{6}}&-\frac{3}{\sqrt{2}\sqrt{6}}&0\cr 0&0&0&1} \right] \)
3.更新\(Q\)矩陣和\(R\)矩陣
\( Q=Q \cdot Q32^T=\left[ \matrix{\displaystyle \frac{1}{2}&-\frac{1}{\sqrt{2}}&\frac{1}{\sqrt{2}\sqrt{3}}&-\frac{1}{2\sqrt{3}}\cr \frac{1}{2}&\frac{1}{\sqrt{2}}&\frac{1}{\sqrt{2}\sqrt{3}}&-\frac{1}{2\sqrt{3}}\cr -\frac{1}{2}&0&\frac{\sqrt{2}}{\sqrt{3}}&\frac{1}{2\sqrt{3}}\cr \frac{1}{2}&0&0&\frac{\sqrt{3}}{2}} \right] \cdot \left[ \matrix{\displaystyle 1&0&0&0\cr 0&-\frac{3}{\sqrt{2}\sqrt{6}}&-\frac{\sqrt{3}}{\sqrt{2}\sqrt{6}}&0 \cr 0&\frac{\sqrt{3}}{\sqrt{2}\sqrt{6}}&-\frac{3}{\sqrt{2}\sqrt{6}}&0\cr 0&0&0&1} \right]=\left[ \matrix{\displaystyle \frac{1}{2}&\frac{2}{\sqrt{6}}&0&-\frac{1}{2\sqrt{3}}\cr \frac{1}{2}&-\frac{1}{\sqrt{6}}&-\frac{\sqrt{3}}{\sqrt{6}}&-\frac{1}{2\sqrt{3}}\cr -\frac{1}{2}&\frac{1}{\sqrt{6}}&-\frac{\sqrt{3}}{\sqrt{6}}&\frac{1}{2\sqrt{3}}\cr \frac{1}{2}&0&0&\frac{\sqrt{3}}{2}} \right] \)
\( R=Q32 \cdot R=\left[ \matrix{\displaystyle 1&0&0&0\cr 0&-\frac{3}{\sqrt{2}\sqrt{6}}&\frac{\sqrt{3}}{\sqrt{2}\sqrt{6}}&0\cr 0&-\frac{\sqrt{3}}{\sqrt{2}\sqrt{6}}&-\frac{3}{\sqrt{2}\sqrt{6}}&0\cr 0&0&0&1} \right] \cdot \left[ \matrix{\displaystyle 2&-\frac{1}{2}&1\cr 0&-\frac{3}{\sqrt{2}}&\frac{3}{\sqrt{2}}\cr 0&\frac{\sqrt{3}}{\sqrt{2}}&\frac{\sqrt{3}}{\sqrt{2}}\cr 0&-\frac{\sqrt{3}}{2}&\sqrt{3}} \right]=\left[ \matrix{\displaystyle 2&-\frac{1}{2}&1\cr 0&\sqrt{6}&-\frac{3}{\sqrt{6}}\cr 0&0&-\frac{3^{3/2}}{\sqrt{6}}\cr 0&-\frac{\sqrt{3}}{2}&\sqrt{3}} \right] \)
------------------------
2.以\(R[2,2]=\sqrt{6}\)消掉\( \displaystyle R[4,2]=-\frac{\sqrt{3}}{{2}}\)為0的旋轉矩陣\(Q42\)
\( \displaystyle r=sqrt(R[2,2]^2+R[4,2]^2)=\frac{3^{3/2}}{2} \)
\( \displaystyle cosφ=R[2,2]/r=\sqrt{6}/\frac{3^{3/2}}{2}=\frac{2 \sqrt{6}}{3^{3/2}} \)
\( \displaystyle sinφ=-R[4,2]/r=--\frac{\sqrt{3}}{2}/ \frac{3^{3/2}}{2}=\frac{1}{3} \)
\( \displaystyle Q42[2,2]=cosφ=\frac{2 \sqrt{6}}{3^{3/2}}\),    \(\displaystyle Q42[2,4]=-sinφ=-\frac{1}{3} \)
\( \displaystyle Q42[4,2]=sinφ=\frac{1}{3}\),     \( \displaystyle Q42[4,4]=cosφ=\frac{2 \sqrt{6}}{3^{3/2}}\)
旋轉矩陣\( Q42=\left[ \matrix{\displaystyle 1&0&0&0\cr 0&\frac{2\sqrt{6}}{3^{3/2}}&0&-\frac{1}{3}\cr 0&0&1&0\cr 0&\frac{1}{3}&0&\frac{2\sqrt{6}}{3^{3/2}}} \right] \)
3.更新\(Q\)矩陣和\(R\)矩陣
\( Q=Q \cdot Q42^T=\left[ \matrix{\displaystyle \frac{1}{2}&\frac{2}{\sqrt{6}}&0&-\frac{1}{2\sqrt{3}}\cr \frac{1}{2}&-\frac{1}{\sqrt{6}}&-\frac{\sqrt{3}}{\sqrt{6}}&-\frac{1}{2\sqrt{3}}\cr -\frac{1}{2}&\frac{1}{\sqrt{6}}&-\frac{\sqrt{3}}{\sqrt{6}}&\frac{1}{2\sqrt{3}}\cr \frac{1}{2}&0&0&\frac{\sqrt{3}}{2}} \right] \cdot \left[ \matrix{\displaystyle 1&0&0&0\cr 0&\frac{2\sqrt{6}}{3^{3/2}}&0&\frac{1}{3}\cr 0&0&1&0\cr 0&-\frac{1}{3}&0&\frac{2 \sqrt{6}}{3^{3/2}}} \right]=\left[ \matrix{\displaystyle \frac{1}{2}&\frac{\sqrt{3}}{2}&0&0\cr \frac{1}{2}&-\frac{1}{2\sqrt{3}}&-\frac{\sqrt{3}}{\sqrt{6}}&-\frac{1}{\sqrt{6}}\cr -\frac{1}{2}&\frac{1}{2\sqrt{3}}&-\frac{\sqrt{3}}{\sqrt{6}}&\frac{1}{\sqrt{6}}\cr \frac{1}{2}&-\frac{1}{2\sqrt{3}}&0&\frac{\sqrt{6}}{3}} \right] \)
\( R=Q42 \cdot R=\left[ \matrix{\displaystyle 1&0&0&0\cr 0&\frac{2\sqrt{6}}{3^{3/2}}&0&-\frac{1}{3}\cr 0&0&1&0 \cr 0&\frac{1}{3}&0&\frac{2\sqrt{6}}{3^{3/2}}} \right]\cdot \left[ \matrix{\displaystyle 2&-\frac{1}{2}&1\cr 0&\sqrt{6}&-\frac{3}{\sqrt{6}}\cr 0&0&-\frac{3^{3/2}}{\sqrt{6}} \cr 0&-\frac{\sqrt{3}}{2}&\sqrt{3}} \right]=\left[ \matrix{\displaystyle 2&-\frac{1}{2}&1\cr 0&\frac{3^{3/2}}{2}&-\sqrt{3} \cr 0&0&-\frac{3^{3/2}}{\sqrt{6}} \cr 0&0&\frac{3}{\sqrt{6}}} \right] \)
------------------------
2.以\(\displaystyle R[3,3]=-\frac{3^{3/2}}{\sqrt{6}}\)消掉\(\displaystyle R[4,3]=\frac{3}{\sqrt{6}}\)為0的旋轉矩陣\(Q43\)
\(r=sqrt(R[3,3]^2+R[4,3]^2)=\sqrt{6} \)
\( \displaystyle cosφ=R[3,3]/r=-\frac{3^{3/2}}{\sqrt{6}}/ \sqrt{6}=-\frac{3^{3/2}}{6} \)
\( \displaystyle sinφ=-R[4,3]/r=-\frac{3}{\sqrt{6}}/ \sqrt{6}=-\frac{1}{2} \)
\( \displaystyle Q43[3,3]=cosφ=-\frac{3^{3/2}}{6}\),    \( \displaystyle Q43[3,4]=-sinφ=\frac{1}{2}\)
\( \displaystyle Q43[4,3]=sinφ=-\frac{1}{2}\),     \( \displaystyle Q43[4,4]=cosφ=-\frac{3^{3/2}}{6}\)
旋轉矩陣\(Q43=\left[ \matrix{\displaystyle 1&0&0&0\cr 0&1&0&0\cr 0&0&-\frac{3^{3/2}}{6}&\frac{1}{2} \cr 0&0&-\frac{1}{2}&-\frac{3^{3/2}}{6}} \right] \)
3.更新\(Q\)矩陣和\(R\)矩陣
\( Q=Q \cdot Q43^T=\left[ \matrix{\displaystyle \frac{1}{2}&\frac{\sqrt{3}}{2}&0&0\cr \frac{1}{2}&-\frac{1}{2\sqrt{3}}&-\frac{\sqrt{3}}{\sqrt{6}}&-\frac{1}{\sqrt{6}}\cr -\frac{1}{2}&\frac{1}{2\sqrt{3}}&-\frac{\sqrt{3}}{\sqrt{6}}&\frac{1}{\sqrt{6}}\cr \frac{1}{2}&-\frac{1}{2\sqrt{3}}&0&\frac{\sqrt{6}}{3}} \right] \cdot \left[ \matrix{\displaystyle 1&0&0&0\cr 0&1&0&0\cr 0&0&-\frac{3^{3/2}}{6}&-\frac{1}{2}\cr 0&0&\frac{1}{2}&-\frac{3^{3/2}}{6}} \right]=\left[ \matrix{\displaystyle \frac{1}{2}&\frac{\sqrt{3}}{2}&0&0\cr \frac{1}{2}&-\frac{1}{2\sqrt{3}}&\frac{1}{\sqrt{6}}&\frac{\sqrt{6}}{2\sqrt{3}}\cr -\frac{1}{2}&\frac{1}{2\sqrt{3}}&\frac{2}{\sqrt{6}}&0\cr \frac{1}{2}&-\frac{1}{2\sqrt{3}}&\frac{1}{\sqrt{6}}&-\frac{\sqrt{3}}{\sqrt{6}}} \right] \)
\( R=Q43\cdot R=\left[ \matrix{\displaystyle 1&0&0&0\cr 0&1&0&0\cr 0&0&-\frac{3^{3/2}}{6}&\frac{1}{2} \cr 0&0&-\frac{1}{2}&-\frac{3^{3/2}}{6}} \right] \cdot \left[ \matrix{\displaystyle 2&-\frac{1}{2}&1\cr 0&\frac{3^{3/2}}{2}&-\sqrt{3} \cr 0&0&-\frac{3^{3/2}}{\sqrt{6}} \cr 0&0&\frac{3}{\sqrt{6}}} \right]=\left[ \matrix{\displaystyle 2&-\frac{1}{2}&1 \cr 0&\frac{3^{3/2}}{2}&-\sqrt{3} \cr 0&0&\sqrt{6} \cr 0&0&0} \right] \)
------------------------
4-1.刪除\(Q\)矩陣的第[4]行
\( \left[ \matrix{\displaystyle
\frac{1}{2}&\frac{\sqrt{3}}{2}&0&0\cr
\frac{1}{2}&-\frac{1}{2 \sqrt{3}}&\frac{1}{\sqrt{6}}&\frac{\sqrt{6}}{2 \sqrt{3}}\cr
-\frac{1}{2}&\frac{1}{2\sqrt{3}}&\frac{2}{\sqrt{6}}&0\cr
\frac{1}{2}&-\frac{1}{2\sqrt{3}}&\frac{1}{\sqrt{6}}&-\frac{\sqrt{3}}{\sqrt{6}}} \right] \)=>\( \left[ \matrix{\displaystyle
\frac{1}{2}&\frac{\sqrt{3}}{2}& 0\cr
\frac{1}{2}&-\frac{1}{2\sqrt{3}}&\frac{1}{\sqrt{6}}\cr
-\frac{1}{2}&\frac{1}{2\sqrt{3}}&\frac{2}{\sqrt{6}}\cr
\frac{1}{2}&-\frac{1}{2\sqrt{3}}&\frac{1}{\sqrt{6}} } \right] \)
4-2.刪除\(R\)矩陣的第[4]列
\( \left[ \matrix{\displaystyle 2&-\frac{1}{2}&1\cr 0&\frac{3^{3/2}}{2}&-\sqrt{3}\cr 0&0&\sqrt{6} \cr 0&0&0} \right] \)=>\( \left[ \matrix{\displaystyle 2&-\frac{1}{2}&1\cr 0&\frac{3^{3/2}}{2}&-\sqrt{3}\cr 0&0&\sqrt{6}} \right] \)
(%o6) \( [\; \left[ \matrix{\displaystyle \frac{1}{2}&\frac{\sqrt{3}}{2}&0\cr \frac{1}{2}&-\frac{1}{2\sqrt{3}}&\frac{1}{\sqrt{6}}\cr -\frac{1}{2}&\frac{1}{2\sqrt{3}}&\frac{2}{\sqrt{6}}\cr \frac{1}{2}&-\frac{1}{2\sqrt{3}}&\frac{1}{\sqrt{6}}} \right],\left[ \matrix{\displaystyle 2&-\frac{1}{2}&1 \cr 0&\frac{3^{3/2}}{2}&-\sqrt{3} \cr 0&0&\sqrt{6}} \right] ]\; \)

Q矩陣和R矩陣相乘得到矩陣A
(%i7) Q.R;
(%o7) \( \left[ \matrix{1&2&-1\cr 1&-1&2\cr -1&1&1\cr 1&-1&2}\right] \)

[ 本帖最後由 bugmens 於 2017-3-5 12:03 編輯 ]
作者: bugmens    時間: 2016-11-12 09:03

https://ccjou.wordpress.com/2011 ... 用/#comment-6583
如果限定\(R\)的主對角元為正值,則非奇異方陣與full col rank矩陣有唯一的\(QR\)分解,不過後者的形式為
\( A=QR=[ \matrix{Q_1&Q_2} ] \left[ \matrix{R_1 \cr 0} \right]=Q_1 R_1 \),\(Q_1\)和\(R_1\)是唯一的,但\(Q_2\)則否。


\( \left[ \matrix{0&-15&14\cr 4&32&2\cr 3&-1&4} \right]=\left[ \matrix{\displaystyle 0&-\frac{3}{5}&\frac{4}{5}\cr \frac{4}{5}&\frac{12}{25}&\frac{9}{25} \cr \frac{3}{5}&-\frac{16}{25}&-\frac{12}{25}} \right]\left[ \matrix{5&25&4\cr 0&25&-10 \cr 0&0&10} \right] \)
測試Maple和Mathematica的執行結果\(R\)矩陣對角線元素為正值。

\( \left[ \matrix{0&-15&14\cr 4&32&2\cr 3&-1&4} \right]=\left[ \matrix{\displaystyle 0&-\frac{3}{5}&-\frac{4}{5}\cr \frac{4}{5}&\frac{12}{25}&-\frac{9}{25} \cr \frac{3}{5}&-\frac{16}{25}&\frac{12}{25}} \right]\left[ \matrix{5&25&4\cr 0&25&-10 \cr 0&0&-10} \right] \)
測試MATLAB和maxima的dgeqrf指令執行結果\(R\)矩陣右下角元素為\(-10\)。

----------------------------------

\( \left[ \matrix{1&2&-1\cr 1&-1&2 \cr -1&1&1 \cr 1&-1&2} \right]=\left[ \matrix{\displaystyle \frac{1}{2}&\frac{\sqrt{3}}{2}&0 \cr \frac{1}{2}&-\frac{1}{2\sqrt{3}}&\frac{1}{\sqrt{6}}\cr -\frac{1}{2}&\frac{1}{2\sqrt{3}}&\frac{\sqrt{6}}{3} \cr \frac{1}{2}&-\frac{1}{2\sqrt{3}}&\frac{1}{\sqrt{6}} } \right] \left[ \matrix{\displaystyle 2&-\frac{1}{2}&1\cr 0&\frac{3^{3/2}}{2}&-\sqrt{3}\cr 0&0&\sqrt{6}} \right] \)
測試Maple和Mathematica會將\(R\)矩陣的0列和\(Q\)矩陣對應的列刪除。

\( \left[ \matrix{1&2&-1\cr 1&-1&2 \cr -1&1&1 \cr 1&-1&2} \right]= \left[ \matrix{\displaystyle \frac{1}{2}&\frac{\sqrt{3}}{2}&0&0\cr \frac{1}{2}&-\frac{1}{2 \sqrt{3}}&\frac{1}{\sqrt{6}}&\frac{\sqrt{6}}{2 \sqrt{3}}\cr -\frac{1}{2}&\frac{1}{2\sqrt{3}}&\frac{2}{\sqrt{6}}&0\cr \frac{1}{2}&-\frac{1}{2\sqrt{3}}&\frac{1}{\sqrt{6}}&-\frac{\sqrt{3}}{\sqrt{6}}} \right] \left[ \matrix{\displaystyle 2&-\frac{1}{2}&1\cr 0&\frac{3^{3/2}}{2}&-\sqrt{3}\cr 0&0&\sqrt{6} \cr 0&0&0} \right] \)
測試Mablab和maxima的dgeqrf指令沒有刪除\(R\)矩陣的0列和\(Q\)矩陣對應的列。

所以我修改前三篇的maxima程式碼,以符合Maple和Mathematica的執行結果。



Maple執行結果


Mathematica執行結果


MATLAB執行結果


LAPACK的dgeqrf執行結果
作者: bugmens    時間: 2017-1-31 09:25

假設矩陣\(A\)的特徵值\(\lambda_1,\lambda_2,\ldots,\lambda_n\)滿足\( |\; \lambda_1 |\;>|\; \lambda_2 |\;>\ldots>|\; \lambda_n |\;>0 \)。QR法的收斂速度在於兩個相鄰特徵值\( |\; \lambda_i |\; >|\; \lambda_{i+1} |\; \)的商\( \displaystyle \Bigg\vert\; \frac{\lambda_{i+1}}{\lambda_{i}} \Bigg\vert\; \)。

設矩陣\(A\)有6個特徵值\( \lambda_1=6,\lambda_2=5,\lambda_3=4,\lambda_4=3,\lambda_5=-1.01,\lambda_6=1 \),其中兩個特徵值\( |\; \lambda_5 |\;=|\;-1.01|\; ,|\; \lambda_6|\; =|\;1|\;\)很接近,QR法會執行很多次。
以下介紹如何給定特徵值來建構出對應的矩陣\(A\)
(1)相伴矩陣
最小多項式\( p(t)=t^n+a_{n-1}t^{n-1}+\ldots+a_1 t+a_0 \)
對應的相伴矩陣為\( C=\left[ \matrix{0&0&\ldots&0&-a_0 \cr 1&0&\ldots&0&-a_1 \cr \vdots&\vdots&\ddots&\vdots&\vdots \cr 0&\ldots&1&0&-a_{n-2} \cr0&0&\ldots&1&-a_{n-1}} \right] \)

詳細內容請參閱線代啟示錄的"多項式的相伴矩陣"
https://ccjou.wordpress.com/2011/01/17/多項式的相伴矩陣/


指定特徵值
\( \lambda_1=6,\lambda_2=5,\lambda_3=4,\lambda_4=3,\lambda_5=-1.01,\lambda_6=1 \)
特徵方程式
\( p(x)=(x-6)(x-5)(x-4)(x-3)(x+1.01)(x-1) \)
  \( =x^6-17.99x^5+117.81x^4-322.63x^3+236.39x^2+349.02x-363.6 \)
相伴矩陣
\( C=\left[ \matrix{0&0&0&0&0&363.6 \cr 1&0&0&0&0&-349.02 \cr 0&1&0&0&0&-236.39 \cr 0&0&1&0&0&322.63 \cr 0&0&0&1&0&-117.81 \cr 0&0&0&0&1&17.99} \right] \)


設定為數值運算
(%i1) numer:true;
(%o1) true

不顯示小數轉換成分數的過程
例如rat: replaced -4.25 by -17/4 = -4.25

(%i2) ratprint:false;
(%o2) false

給定特徵值產生相伴矩陣副程式
(%i3)
CompanionMatrix(eig):=block
([n:length(eig),px],
print("特徵值λ=",eig),
print("特徵方程式p(x)=",px:product((x-eig[ i ]),i,1,n)),
print("多項式展開p(x)=",px:expand(px)),
h[i,j]:=if j=n then (-ratcoef(px,x,i-1))
           else if i-j=1 then (1)
           else (0),
print("相伴矩陣C=",genmatrix(h,n,n))
)$


特徵值6,5,4,3,-1.01,1
(%i4) CompanionMatrix([6,5,4,3,-1.01,1]);
特徵值\( \lambda=\left[ 6,5,4,3,-1.01,1 \right] \)
特徵方程式\( p(x)=(x-6)(x-5)(x-4)(x-3)(x-1)(x+1.01) \)
多項式展開\( p(x)=x^6-17.99x^5+117.81x^4-322.63x^3+236.39x^2+349.02x-363.6 \)
相伴矩陣\( C=\left[ \matrix{0&0&0&0&0&363.6 \cr 1&0&0&0&0&-349.02 \cr 0&1&0&0&0&-236.39 \cr 0&0&1&0&0&322.63 \cr 0&0&0&1&0&-117.81 \cr 0&0&0&0&1&17.99} \right] \)
(%o4) \( \left[ \matrix{0&0&0&0&0&363.6 \cr 1&0&0&0&0&-349.02 \cr 0&1&0&0&0&-236.39 \cr 0&0&1&0&0&322.63 \cr 0&0&0&1&0&-117.81 \cr 0&0&0&0&1&17.99} \right] \)



(2)實對稱矩陣可正交對角化
令\(A\)為一個\(n \times n\)階實對稱矩陣且\( \lambda_1,\ldots,\lambda_n \)為\(A\)的特徵值。所謂正交對角化是指存在一個實正交矩陣(orthogonal matrix)\(Q\),\(Q^T=Q^{-1}\),使得\( Q^T AQ=\Lambda=diag(\lambda_1,\ldots,\lambda_n) \),其中\(Q\)的行向量(column vector)是\(A\)的特徵向量。
以上內容節錄線代啟示錄的"實對稱矩陣可正交對角化的證明"
https://ccjou.wordpress.com/2011 ... 對角化的證明/

利用\(\displaystyle Q=I-2 \frac{vv^T}{v^Tv}\)可以構造出正交矩陣\(Q\)
https://zh.wikipedia.org/wiki/ ... C.E5.8F.98.E6.8D.A2


給定特徵值產生對稱矩陣副程式
(%i1)
OrthDiag(eig):=block
([n:length(eig),Q,D],
print("向量v=",v:genmatrix(lambda([i,j],1),n,1)),
print("正交矩陣Q=I-2(vv^T)/(v^Tv)"),
print("Q=",ident(n),"-2",v,transpose(v),"/",transpose(v),v),
print("Q=",ident(n),"-2",v.transpose(v),"/",transpose(v).v),
print("Q=",Q:ident(n)-2*v.transpose(v)/transpose(v).v),
print("對角矩陣D=",D:genmatrix(lambda([i,j],if i=j then eig[ i ] else 0),n,n)),
print("對稱矩陣A=Q^TDQ"),
print("A=",transpose(Q),D,Q),
print("A=",transpose(Q).D.Q)
)$


特徵值6,5,4,3,-1.01,1
(%i2) OrthDiag([6,5,4,3,-101/100,1]);
向量\( v=\left[ \matrix{1 \cr 1 \cr 1 \cr 1 \cr 1 \cr 1} \right] \)
正交矩陣\(Q=I-2(vv^T)/(v^Tv)\)
\( Q=\left[ \matrix{1&0&0&0&0&0 \cr 0&1&0&0&0&0 \cr 0&0&1&0&0&0 \cr 0&0&0&1&0&0 \cr 0&0&0&0&1&0 \cr 0&0&0&0&0&1} \right]-2 \left[ \matrix{1 \cr 1 \cr 1 \cr 1 \cr 1 \cr 1} \right] \left[ \matrix{1&1&1&1&1&1} \right] / \left[ \matrix{1&1&1&1&1&1} \right] \left[ \matrix{1 \cr 1 \cr 1 \cr 1 \cr 1 \cr 1} \right] \)
\( Q=\left[ \matrix{1&0&0&0&0&0 \cr 0&1&0&0&0&0 \cr 0&0&1&0&0&0 \cr 0&0&0&1&0&0 \cr 0&0&0&0&1&0 \cr 0&0&0&0&0&1} \right]-2 \left[ \matrix{1&1&1&1&1&1 \cr 1&1&1&1&1&1 \cr 1&1&1&1&1&1 \cr 1&1&1&1&1&1 \cr 1&1&1&1&1&1 \cr 1&1&1&1&1&1} \right] /6 \)
\( Q=\left[ \matrix{\displaystyle \frac{2}{3}&-\frac{1}{3}&-\frac{1}{3}&-\frac{1}{3}&-\frac{1}{3}&-\frac{1}{3} \cr -\frac{1}{3}&\frac{2}{3}&-\frac{1}{3}&-\frac{1}{3}&-\frac{1}{3}&-\frac{1}{3} \cr -\frac{1}{3}&-\frac{1}{3}&\frac{2}{3}&-\frac{1}{3}&-\frac{1}{3}&-\frac{1}{3} \cr -\frac{1}{3}&-\frac{1}{3}&-\frac{1}{3}&\frac{2}{3}&-\frac{1}{3}&-\frac{1}{3} \cr -\frac{1}{3}&-\frac{1}{3}&-\frac{1}{3}&-\frac{1}{3}&\frac{2}{3}&-\frac{1}{3} \cr -\frac{1}{3}&-\frac{1}{3}&-\frac{1}{3}&-\frac{1}{3}&-\frac{1}{3}&\frac{2}{3}} \right] \)
對角矩陣\( \displaystyle D=\left[ \matrix{\displaystyle 6&0&0&0&0&0\cr 0&5&0&0&0&0\cr 0&0&4&0&0&0\cr 0&0&0&3&0&0\cr 0&0&0&0&-\frac{101}{100}&0\cr 0&0&0&0&0&1} \right] \)
對稱矩陣\( A=Q^TDQ \)
\( A=\left[ \matrix{\displaystyle \frac{2}{3}&-\frac{1}{3}&-\frac{1}{3}&-\frac{1}{3}&-\frac{1}{3}&-\frac{1}{3} \cr -\frac{1}{3}&\frac{2}{3}&-\frac{1}{3}&-\frac{1}{3}&-\frac{1}{3}&-\frac{1}{3} \cr -\frac{1}{3}&-\frac{1}{3}&\frac{2}{3}&-\frac{1}{3}&-\frac{1}{3}&-\frac{1}{3} \cr -\frac{1}{3}&-\frac{1}{3}&-\frac{1}{3}&\frac{2}{3}&-\frac{1}{3}&-\frac{1}{3} \cr -\frac{1}{3}&-\frac{1}{3}&-\frac{1}{3}&-\frac{1}{3}&\frac{2}{3}&-\frac{1}{3} \cr -\frac{1}{3}&-\frac{1}{3}&-\frac{1}{3}&-\frac{1}{3}&-\frac{1}{3}&\frac{2}{3}} \right]
\left[ \matrix{\displaystyle 6&0&0&0&0&0\cr 0&5&0&0&0&0\cr 0&0&4&0&0&0\cr 0&0&0&3&0&0\cr 0&0&0&0&-\frac{101}{100}&0\cr 0&0&0&0&0&1} \right]
\left[ \matrix{\displaystyle \frac{2}{3}&-\frac{1}{3}&-\frac{1}{3}&-\frac{1}{3}&-\frac{1}{3}&-\frac{1}{3} \cr -\frac{1}{3}&\frac{2}{3}&-\frac{1}{3}&-\frac{1}{3}&-\frac{1}{3}&-\frac{1}{3} \cr -\frac{1}{3}&-\frac{1}{3}&\frac{2}{3}&-\frac{1}{3}&-\frac{1}{3}&-\frac{1}{3} \cr -\frac{1}{3}&-\frac{1}{3}&-\frac{1}{3}&\frac{2}{3}&-\frac{1}{3}&-\frac{1}{3} \cr -\frac{1}{3}&-\frac{1}{3}&-\frac{1}{3}&-\frac{1}{3}&\frac{2}{3}&-\frac{1}{3} \cr -\frac{1}{3}&-\frac{1}{3}&-\frac{1}{3}&-\frac{1}{3}&-\frac{1}{3}&\frac{2}{3}} \right] \)
\( A=\left[ \matrix{ \displaystyle
\frac{3599}{900}&-\frac{1501}{900}&-\frac{1201}{900}&-\frac{901}{900}&\frac{151}{450}&-\frac{301}{900}\cr
-\frac{1501}{900}&\frac{3299}{900}&-\frac{901}{900}&-\frac{601}{900}&\frac{301}{450}&-\frac{1}{900}\cr
-\frac{1201}{900}&-\frac{901}{900}&\frac{2999}{900}&-\frac{301}{900}&\frac{451}{450}&\frac{299}{900}\cr
-\frac{901}{900}&-\frac{601}{900}&-\frac{301}{900}&\frac{2699}{900}&\frac{601}{450}&\frac{599}{900}\cr
\frac{151}{450}&\frac{301}{450}&\frac{451}{450}&\frac{601}{450}&\frac{374}{450}&\frac{901}{450}\cr
-\frac{301}{900}&-\frac{1}{900}&\frac{299}{900}&\frac{599}{900}&\frac{901}{450}&\frac{2099}{900}} \right] \)

(%o2) \( \left[ \matrix{ \displaystyle
\frac{3599}{900}&-\frac{1501}{900}&-\frac{1201}{900}&-\frac{901}{900}&\frac{151}{450}&-\frac{301}{900}\cr
-\frac{1501}{900}&\frac{3299}{900}&-\frac{901}{900}&-\frac{601}{900}&\frac{301}{450}&-\frac{1}{900}\cr
-\frac{1201}{900}&-\frac{901}{900}&\frac{2999}{900}&-\frac{301}{900}&\frac{451}{450}&\frac{299}{900}\cr
-\frac{901}{900}&-\frac{601}{900}&-\frac{301}{900}&\frac{2699}{900}&\frac{601}{450}&\frac{599}{900}\cr
\frac{151}{450}&\frac{301}{450}&\frac{451}{450}&\frac{601}{450}&\frac{374}{450}&\frac{901}{450}\cr
-\frac{301}{900}&-\frac{1}{900}&\frac{299}{900}&\frac{599}{900}&\frac{901}{450}&\frac{2099}{900}} \right] \)



以QR(A,10^-5);測試執行次數
http://math.pro/db/viewthread.php?tid=2561&page=1#pid16149

(1)相伴矩陣
A:matrix([0,0,0,0,0,363.6],
         [1,0,0,0,0,-349.02],
         [0,1,0,0,0,-236.39],
         [0,0,1,0,0,322.63],
         [0,0,0,1,0,-117.81],
         [0,0,0,0,1,17.99]);
(2)實對稱矩陣
A:matrix([3599/900,-1501/900,-1201/900,-901/900,151/450,-301/900],
[-1501/900,3299/900,-901/900,-601/900,301/450,-1/900],
[-1201/900,-901/900,2999/900,-301/900,451/450,299/900],
[-901/900,-601/900,-301/900,2699/900,601/450,599/900],
[151/450,301/450,451/450,601/450,374/225,901/450],
[-301/900,-1/900,299/900,599/900,901/450,2099/900]);

原始的QR法

1142次

348次



[ 本帖最後由 bugmens 於 2017-5-28 11:57 編輯 ]
作者: bugmens    時間: 2017-3-3 23:44

1.減少QR法運算-化簡成Hessenberg矩陣

在執行QR法之前,會將矩陣化簡成\( A=\left[ \matrix{*&*&*&*&* \cr *&*&*&*&* \cr 0&*&*&*&* \cr 0&0&*&*&* \cr 0&0&0&*&*} \right] \)讓左下角元為零,減少QR法運算量。

若對稱矩陣化簡後變成三對角矩陣\( A=\left[ \matrix{*&*&0&0&0 \cr *&*&*&0&0 \cr 0&*&*&*&0 \cr 0&0&*&*&* \cr 0&0&0&*&*} \right] \),減少QR法運算次數。

詳細方法請見線代啟示錄
特殊矩陣 (19):Hessenberg 矩陣
https://ccjou.wordpress.com/2013 ... hessenberg-矩陣/

Hessenberg化簡還需要Householder變換,詳細方法請見線代啟示錄
特殊矩陣 (4):Householder 矩陣
https://ccjou.wordpress.com/2009 ... householder-矩陣/




虛擬碼
Householder(x)
{
\( e1 \)為第1元為1,其餘為0的向量
定義\( sign(x_1)=+1 \)若\( x_1 \ge 0 \)
  \( sign(x_1)=-1 \)若\( x_1 <0 \)
\( \displaystyle v=\frac{x+sign(x_1)\Vert\; x \Vert\; e1}{\Vert\; x+sign(x_1)\Vert\; x \Vert\; e1 \Vert\;} \),其中\( \Vert\; * \Vert\; \)為向量長度
Householder矩陣\(H=I-2vv^T \)
\( return(H) \)
}

Hessenberg(A)
{
\(n\)為矩陣\(A\)行的個數
for i=1 ~ (n-2)
 {擷取\( A \)矩陣第\(i\)列末\(n-i\)元形成\(x\)向量
  將\(x\)向量代入\(Householder(x)\)副程式得到Householder矩陣\(H\)
  構造\( P=\left[ \matrix{I&0 \cr 0&H} \right] \),其中\(I\)為單位矩陣
  計算\(A=PAP\),得到新的矩陣A
 }
若\(A\)有整列的0向量,調整位置放在最後一列
\( return(A) \)
}



要先載入diag才能使用diag指令
(%i1) load(diag);
(%o1) C:\maxima-5.39.0\share\maxima\5.39.0_2_g5a49f11_dirty\share\contrib\diag.mac

轉換成Householder矩陣副程式
(%i2)
Householder(x):=block
([n,e1,normx,v,normv],
n:length(x),
if x=zeromatrix(n,1) then
  (print("x為0向量,H=",ident(n)),
   return(ident(n))
  ),
if x[1,1]>=0 then sign:+1 else sign:-1,/*maxima內建指令signum(0)=0會導致錯誤*/
print("x向量第1元=",x[1,1]," , sign(x_1)=",sign,",長度∥x∥=sqrt(",x,".",x,")=",normx:ratsimp(sqrt(x.x))),
print("第1元是1,其餘為0的向量e1=",e1:genmatrix(lambda([i,j],if i=1 then 1 else 0),n,1)),
print("x+sign(x_1)*∥x∥*e1=",x,"+",sign,"*",normx,"*",e1,"=",v:x+sign*normx*e1),
print("長度∥x+sign(x_1)*∥x∥*e1∥=sqrt(",v,".",v,")=",normv:ratsimp(sqrt(v.v))),
print("v=",v,"/",normv,"=",v:ratsimp(v/normv)),
print("H=I-2*v.v^T=",ident(n),"-2*",v,".",transpose(v),"=",H:ratsimp(ident(n)-2*v.transpose(v))),
return(H)
)$


轉換成Hessenberg矩陣副程式
(%i3)
Hessenberg(A):=block
([n,H,P],
n:length(A),
for i:1 thru n-2 do
  (print("擷取A=",A,"第",i,"行末",n-i,"元,x=",x:genmatrix(lambda([ii,jj],A[i+ii,i]),n-i,1)),
    print("Householder矩陣,H=",H:Householder(x)),
    print("P=",matrix([I,0],[0,"H"]),"=",P:diag([ident(i),H])),
    print("A=PAP=",P,".",A,".",P,"=",A:ratsimp(P.A.P)),
    print("------------------------")
   ),
zerorow:zeromatrix(1,n),
for i:1 thru n-1 do
  (if matrix(A[ i ])=zerorow then
     (print(A,"第",i,"列為0向量,放在最後一行",A:copymatrix(append(submatrix(i,A),zerorow)))
     )
  ),
return(A)
)$


非對稱矩陣A
(%i4)
A:matrix([1,3,2,1],
         [-2,-2,-2,1],
         [2,-2,2,-2],
         [-1,0,2,0]);


(%o4) \( \left[ \matrix{1&3&2&1 \cr -2&-2&-2&1 \cr 2&-2&2&-2 \cr-1&0&2&0} \right] \)

轉換成Hessenberg矩陣
(%5) Hessenberg(A);
擷取\( A=\left[ \matrix{1&3&2&1\cr -2&-2&-2&1\cr 2&-2&2&-2\cr -1&0&2&0} \right] \)第1行末3元,\( x=\left[ \matrix{-2 \cr 2 \cr -1} \right] \)
\(x\)向量第1元\(=-2\),\(sign(x_1)=-1\),長度\( \Vert\; x \Vert\;=sqrt( \left[ \matrix{-2 \cr 2 \cr -1} \right] \cdot \left[ \matrix{-2 \cr 2 \cr -1} \right] )=3 \)
第1元是1,其餘為0的向量\( e1=\left[ \matrix{1\cr 0 \cr 0} \right] \)
\( x+sign(x_1)*\Vert\; x \Vert\;*e1=\left[ \matrix{-2 \cr 2 \cr -1} \right]+-1*3*\left[ \matrix{1\cr 0 \cr 0} \right]=\left[ \matrix{-5 \cr 2 \cr -1} \right] \)
長度\( \Vert\; x+sign(x_1)*\Vert\; x \Vert\;*e1 \Vert\;=sqrt(\left[ \matrix{-5 \cr 2 \cr -1} \right] \cdot \left[ \matrix{-5 \cr 2 \cr -1} \right])=\sqrt{30} \)
\( v=\left[ \matrix{-5 \cr 2 \cr -1} \right]/ \sqrt{30}=\left[ \matrix{\displaystyle -\frac{5}{\sqrt{30}} \cr \frac{2}{\sqrt{30}} \cr -\frac{1}{\sqrt{30}}} \right] \)
\( H=I-2*v \cdot v^T=\left[ \matrix{1&0&0\cr 0&1&0\cr 0&0&1} \right]-2 *\left[ \matrix{\displaystyle -\frac{5}{\sqrt{30}} \cr \frac{2}{\sqrt{30}} \cr -\frac{1}{\sqrt{30}}} \right] \cdot \left[ \matrix{\displaystyle -\frac{5}{\sqrt{30}} & \frac{2}{\sqrt{30}} & -\frac{1}{\sqrt{30}}} \right]=\left[ \matrix{\displaystyle -\frac{2}{3}&\frac{2}{3}&-\frac{1}{3} \cr \frac{2}{3}&\frac{11}{15}&\frac{2}{15} \cr -\frac{1}{3}&\frac{2}{15}&\frac{14}{15}} \right] \)
Householder矩陣,\( H=\left[ \matrix{\displaystyle -\frac{2}{3}&\frac{2}{3}&-\frac{1}{3} \cr \frac{2}{3}&\frac{11}{15}&\frac{2}{15} \cr -\frac{1}{3}&\frac{2}{15}&\frac{14}{15}} \right] \)
\( P=\left[ \matrix{I&0 \cr 0&H} \right]=\left[ \matrix{\displaystyle 1&0&0&0 \cr 0&-\frac{2}{3}&\frac{2}{3}&-\frac{1}{3} \cr 0&\frac{2}{3}&\frac{11}{15}&\frac{2}{15} \cr 0&-\frac{1}{3}&\frac{2}{15}&\frac{14}{15}} \right] \)
\( A=PAP=\left[ \matrix{\displaystyle 1&0&0&0 \cr 0&-\frac{2}{3}&\frac{2}{3}&-\frac{1}{3} \cr 0&\frac{2}{3}&\frac{11}{15}&\frac{2}{15} \cr 0&-\frac{1}{3}&\frac{2}{15}&\frac{14}{15}} \right] \cdot \left[ \matrix{\displaystyle 1&3&2&1\cr -2&-2&-2&1\cr 2&-2&2&-2\cr -1&0&2&0} \right] \cdot \left[ \matrix{\displaystyle 1&0&0&0 \cr 0&-\frac{2}{3}&\frac{2}{3}&-\frac{1}{3} \cr 0&\frac{2}{3}&\frac{11}{15}&\frac{2}{15} \cr 0&-\frac{1}{3}&\frac{2}{15}&\frac{14}{15}} \right]=\left[ \matrix{\displaystyle 1&-1&\frac{18}{5}&\frac{1}{5}\cr 3&2&\frac{6}{5}&-\frac{8}{5}\cr 0&\frac{12}{5}&-\frac{42}{25}&\frac{6}{25}\cr 0&\frac{9}{5}&\frac{56}{25}&-\frac{8}{25}} \right] \)
---------------
擷取\( A=\left[ \matrix{\displaystyle 1&-1&\frac{18}{5}&\frac{1}{5}\cr 3&2&\frac{6}{5}&-\frac{8}{5}\cr 0&\frac{12}{5}&-\frac{42}{25}&\frac{6}{25}\cr 0&\frac{9}{5}&\frac{56}{25}&-\frac{8}{25}} \right] \)第2行末2元,\( x=\left[ \matrix{\displaystyle \frac{12}{5} \cr \frac{9}{5}} \right] \)
\(x\)向量第1元\(\displaystyle =\frac{12}{5}\),\( sign(x_1)=1 \),長度\( \Vert\; x \Vert\;=sqrt(\left[ \matrix{\displaystyle \frac{12}{5} \cr \frac{9}{5}} \right] \cdot \left[ \matrix{\displaystyle \frac{12}{5} \cr \frac{9}{5}} \right])=3 \)
第1元是1,其餘為0的向量\(e1=\left[ \matrix{1 \cr 0} \right] \)
\( x+sign(x_1)*\Vert\; x \Vert\;*e1=\left[ \matrix{\displaystyle \frac{12}{5} \cr \frac{9}{5}} \right]+1*3*\left[ \matrix{1 \cr 0} \right]=\left[ \matrix{\displaystyle \frac{27}{5} \cr \frac{9}{5}} \right] \)
長度\( \displaystyle \Vert\; x+sign(x_1)*\Vert\; x \Vert\;*e1 \Vert\;=sqrt(\left[ \matrix{\displaystyle \frac{27}{5} \cr \frac{9}{5}} \right] \cdot \left[ \matrix{\displaystyle \frac{27}{5} \cr \frac{9}{5}} \right])=\frac{9\sqrt{2}}{\sqrt{5}} \)
\( \displaystyle v=\left[ \matrix{\displaystyle \frac{27}{5}\cr \frac{9}{5}} \right]/ \frac{9\sqrt{2}}{\sqrt{5}}=\left[ \matrix{\displaystyle \frac{3}{\sqrt{2}\sqrt{5}} \cr \frac{1}{\sqrt{2}\sqrt{5}}} \right] \)
\( H=I-2*v \cdot v^T=\left[ \matrix{1&0 \cr 0&1} \right]-2*\left[ \matrix{\displaystyle \frac{3}{\sqrt{2}\sqrt{5}}\cr \frac{1}{\sqrt{2}\sqrt{5}}} \right] \cdot \left[ \matrix{\displaystyle \frac{3}{\sqrt{2}\sqrt{5}}& \frac{1}{\sqrt{2}\sqrt{5}}} \right]=\left[ \matrix{\displaystyle -\frac{4}{5}&-\frac{3}{5}\cr -\frac{3}{5}&\frac{4}{5}} \right] \)
Householder矩陣,\( H=\left[ \matrix{\displaystyle -\frac{4}{5}&-\frac{3}{5}\cr -\frac{3}{5}&\frac{4}{5}} \right] \)
\( P=\left[ \matrix{I&0 \cr 0&H} \right]=\left[ \matrix{\displaystyle 1&0&0&0 \cr 0&1&0&0 \cr 0&0&-\frac{4}{5}&-\frac{3}{5}\cr 0&0&-\frac{3}{5}&\frac{4}{5}} \right] \)
\( A=PAP=\left[ \matrix{\displaystyle 1&0&0&0 \cr 0&1&0&0 \cr 0&0&-\frac{4}{5}&-\frac{3}{5}\cr 0&0&-\frac{3}{5}&\frac{4}{5}} \right] \cdot \left[ \matrix{\displaystyle 1&-1&\frac{18}{5}&\frac{1}{5}\cr 3&2&\frac{6}{5}&-\frac{8}{5}\cr 0&\frac{12}{5}&-\frac{42}{25}&\frac{6}{25}\cr 0&\frac{9}{5}&\frac{56}{25}&-\frac{8}{25}} \right] \cdot \left[ \matrix{\displaystyle 1&0&0&0 \cr 0&1&0&0 \cr 0&0&-\frac{4}{5}&-\frac{3}{5}\cr 0&0&-\frac{3}{5}&\frac{4}{5}} \right]=\left[ \matrix{\displaystyle 1&-1&-3&-2\cr 3&2&0&-2\cr 0&-3&0&0\cr 0&0&-2&-2} \right] \)
---------------
(%o5) \( \left[ \matrix{1&-1&-3&-2\cr 3&2&0&-2\cr0&-3&0&0\cr 0&0&-2&-2} \right] \)

矩陣A為線代啟示錄"特殊矩陣 (4):Householder 矩陣"文章中的範例
(%i6)
A:matrix([4,1,-2,2],
              [1,2,0,1],
              [-2,0,3,-2],
              [2,1,-2,-1]);

(%o6) \( \left[ \matrix{4&1&-2&2 \cr 1&2&0&1 \cr -2&0&3&-2 \cr 2&1&-2&-1} \right] \)

轉換成Hessenberg矩陣
(%i7) Hessenberg(A);
擷取\( A=\left[ \matrix{4&1&-2&2\cr 1&2&0&1\cr -2&0&3&-2\cr 2&1&-2&-1} \right] \)第1行末3元,\( x=\left[ \matrix{1 \cr -2 \cr 2} \right] \)
\(x\)向量第1元\(=1\),\(sign(x_1)=1\),長度\( \Vert\; x \Vert\;=sqrt(\left[ \matrix{1 \cr -2 \cr 2} \right].\left[ \matrix{1 \cr -2 \cr 2} \right])=3 \)
第1元是1,其餘為0的向量\(e1=\left[ \matrix{1 \cr 0 \cr 0} \right] \)
\( x+sign(x_1)* \Vert\; x \Vert\;*e1=\left[ \matrix{1 \cr -2 \cr 2} \right]+1*3*\left[ \matrix{1 \cr 0 \cr 0} \right]=\left[ \matrix{4 \cr -2 \cr 2} \right] \)
長度\( \Vert\; x+sign(x_1)*\Vert\; x \Vert\; *e1 \Vert\;=sqrt(\left[ \matrix{4 \cr -2 \cr 2} \right].\left[ \matrix{4 \cr -2 \cr 2} \right])=2 \sqrt{6} \)
\( v=\left[ \matrix{4 \cr -2 \cr 2} \right]/2\sqrt{6}=\left[ \matrix{\displaystyle \frac{2}{\sqrt{6}} \cr -\frac{1}{\sqrt{6}} \cr \frac{1}{\sqrt{6}}} \right] \)
\( H=I-2*v.v^T=\left[ \matrix{1&0&0\cr 0&1&0\cr 0&0&1} \right]-2*\left[ \matrix{\displaystyle \frac{2}{\sqrt{6}} \cr -\frac{1}{\sqrt{6}} \cr \frac{1}{\sqrt{6}}} \right] \cdot \left[ \matrix{\displaystyle \frac{2}{\sqrt{6}}&-\frac{1}{\sqrt{6}}&\frac{1}{\sqrt{6}}} \right]=
\left[ \matrix{\displaystyle
-\frac{1}{3}&\frac{2}{3}&-\frac{2}{3} \cr
\frac{2}{3}&\frac{2}{3}&\frac{1}{3} \cr
-\frac{2}{3}&\frac{1}{3}&\frac{2}{3}} \right] \)
Householder矩陣,\( H=\left[ \matrix{\displaystyle
-\frac{1}{3}&\frac{2}{3}&-\frac{2}{3} \cr
\frac{2}{3}&\frac{2}{3}&\frac{1}{3} \cr
-\frac{2}{3}&\frac{1}{3}&\frac{2}{3}} \right] \)
\( P=\left[ \matrix{I&0 \cr 0&H} \right]=
\left[ \matrix{\displaystyle
1&0&0&0 \cr
0&-\frac{1}{3}&\frac{2}{3}&-\frac{2}{3} \cr
0&\frac{2}{3}&\frac{2}{3}&\frac{1}{3} \cr
0&-\frac{2}{3}&\frac{1}{3}&\frac{2}{3}} \right] \)
\( A=PAP=\left[ \matrix{\displaystyle
1&0&0&0 \cr
0&-\frac{1}{3}&\frac{2}{3}&-\frac{2}{3} \cr
0&\frac{2}{3}&\frac{2}{3}&\frac{1}{3} \cr
0&-\frac{2}{3}&\frac{1}{3}&\frac{2}{3}} \right] \cdot
\left[ \matrix{4&1&-2&2\cr 1&2&0&1\cr -2&0&3&-2\cr 2&1&-2&-1} \right] \cdot
\left[ \matrix{\displaystyle
1&0&0&0 \cr
0&-\frac{1}{3}&\frac{2}{3}&-\frac{2}{3} \cr
0&\frac{2}{3}&\frac{2}{3}&\frac{1}{3} \cr
0&-\frac{2}{3}&\frac{1}{3}&\frac{2}{3}} \right]=
\left[ \matrix{\displaystyle
4&-3&0&0 \cr
-3&\frac{10}{3}&1&\frac{4}{3} \cr
0&1&\frac{5}{3}&-\frac{4}{3} \cr
0&\frac{4}{3}&-\frac{4}{3}&-1} \right] \)
---------------
擷取\( A=\left[ \matrix{\displaystyle
4&-3&0&0 \cr
-3&\frac{10}{3}&1&\frac{4}{3} \cr
0&1&\frac{5}{3}&-\frac{4}{3} \cr
0&\frac{4}{3}&-\frac{4}{3}&-1} \right] \)第2行末2元,\( x=\left[ \matrix{\displaystyle 1 \cr \frac{4}{3}} \right] \)
\(x\)向量第1元=1,\(sign(x_1)=1\),長度\( \Vert\; x \Vert\;=sqrt(\left[ \matrix{\displaystyle 1 \cr \frac{4}{3}} \right] \cdot \left[ \matrix{\displaystyle 1 \cr \frac{4}{3}} \right])=\displaystyle \frac{5}{3} \)
第1元是1,其餘為0的向量\(e1=\left[ \matrix{1 \cr 0} \right] \)
\( \displaystyle x+sign(x_1)*\Vert\; x \Vert\;*e1=\left[ \matrix{\displaystyle 1 \cr \frac{4}{3}} \right]+1*\frac{5}{3}*\left[ \matrix{1 \cr 0} \right]=\left[ \matrix{\displaystyle \frac{8}{3} \cr \frac{4}{3}} \right] \)
長度\( \Vert\; x+sign(x_1)*\Vert\; x \Vert\;*e1 \Vert\;=sqrt(\left[ \matrix{\displaystyle \frac{8}{3} \cr \frac{4}{3}} \right] \cdot \left[ \matrix{\displaystyle \frac{8}{3} \cr \frac{4}{3}} \right])=\displaystyle \frac{4 \sqrt{5}}{3} \)
\( \displaystyle v=\left[ \matrix{\displaystyle \frac{8}{3} \cr \frac{4}{3}} \right] / \frac{4 \sqrt{5}}{3}=\left[ \matrix{\displaystyle \frac{2}{\sqrt{5}} \cr \frac{1}{\sqrt{5}}} \right] \)
\( H=I-2*v \cdot v^T=\left[ \matrix{1&0 \cr 0&1} \right]-2* \left[ \matrix{\displaystyle \frac{2}{\sqrt{5}} \cr \frac{1}{\sqrt{5}}} \right] \cdot \left[ \matrix{\displaystyle \frac{2}{\sqrt{5}} & \frac{1}{\sqrt{5}}} \right]=\left[ \matrix{\displaystyle -\frac{3}{5}&-\frac{4}{5}\cr -\frac{4}{5}&\frac{3}{5}} \right] \)
Householder矩陣,\( H=\left[ \matrix{\displaystyle -\frac{3}{5}&-\frac{4}{5}\cr-\frac{4}{5}&\frac{3}{5}} \right] \)
\( P=\left[ \matrix{I&0 \cr 0&H} \right]=
\left[ \matrix{\displaystyle 1&0&0&0\cr 0&1&0&0 \cr 0&0&-\frac{3}{5}&-\frac{4}{5}\cr 0&0&-\frac{4}{5}&\frac{3}{5}} \right] \)
\( A=PAP=\left[ \matrix{\displaystyle 1&0&0&0\cr 0&1&0&0 \cr 0&0&-\frac{3}{5}&-\frac{4}{5}\cr 0&0&-\frac{4}{5}&\frac{3}{5}} \right] \cdot
\left[ \matrix{\displaystyle 4&-3&0&0\cr -3&\frac{10}{3}&1&\frac{4}{3} \cr 0&1&\frac{5}{3}&-\frac{4}{3}\cr 0&\frac{4}{3}&-\frac{4}{3}&-1} \right]
\left[ \matrix{\displaystyle 1&0&0&0\cr 0&1&0&0 \cr 0&0&-\frac{3}{5}&-\frac{4}{5}\cr 0&0&-\frac{4}{5}&\frac{3}{5}} \right]=
\left[ \matrix{\displaystyle 4&-3&0&0\cr -3&\frac{10}{3}&-\frac{5}{3}&0 \cr 0&-\frac{5}{3}&-\frac{33}{25}&\frac{68}{75}\cr 0&0&\frac{68}{75}&\frac{149}{75}} \right] \)
---------------
(%o7) \( \left[ \matrix{\displaystyle 4&-3&0&0\cr -3&\frac{10}{3}&-\frac{5}{3}&0 \cr 0&-\frac{5}{3}&-\frac{33}{25}&\frac{68}{75}\cr 0&0&\frac{68}{75}&\frac{149}{75}} \right] \)
作者: bugmens    時間: 2017-3-17 21:56

Hessenberg矩陣已經具有許多零元,我們只要消去主對角下標元即可獲得QR分解的上三角矩陣\(R\)。
我將前一篇文章的Hessenberg矩陣的QR分解過程列出來,讓網友可以更加了解線代啟示錄文章內容。

QRbyGivensRotation(A)副程式取自
http://math.pro/db/viewthread.php?tid=2561&page=2#pid16180

線代啟示錄"特殊矩陣 (19):Hessenberg 矩陣"的"QR分解"
https://ccjou.wordpress.com/2013 ... hessenberg-矩陣/



利用Givens旋轉的QR分解
(%i1)
QRbyGivensRotation(A):=block
([columns:length(A[1]),rows:length(A),rank:rank(A),n,r,c,s,Qji,list,Q,R],
n:max(columns,rows),
print("1.初始值"),
Q:ident(rows),
R:copymatrix(A),
print("Q=",Q,",R=",R),
for i:1 thru min(columns,rows) do
    (for j:i+1 thru rows do
       (if R[j,i]#0 then/*若R[j,i]已經是0就不處理*/
          (print("2.以R[",i,",",i,"]=",R[i,i],"消掉R[",j,",",i,"]=",R[j,i],"為0的旋轉矩陣Q",j,i),
           print("r=sqrt(R[",i,",",i,"]^2+R[",j,",",i,"]^2)=",r:sqrt(R[i,i]^2+R[j,i]^2)),
           c:R[i,i]/r,
           s:-R[j,i]/r,
           print("cosφ=R[",i,",",i,"]/r=",R[i,i],"/",r,"=",c),
           print("sinφ=-R[",j,",",i,"]/r=-",R[j,i],"/",r,"=",s),
           Qji:ident(rows),
           Qji[i,i]:c,    Qji[i,j]:-s,
           Qji[j,i]:s,    Qji[j,j]:c,
           print("Q",j,i,"[",i,",",i,"]=cosφ=",c,",    Q",j,i,"[",i,",",j,"]=-sinφ=",-s),
           print("Q",j,i,"[",j,",",i,"]=sinφ=",s,",     Q",j,i,"[",j,",",j,"]=cosφ=",c),
           print("旋轉矩陣Q",j,i,"=",Qji),
           print("3.更新Q矩陣和R矩陣"),
           print("Q=Q.Q",j,i,"^T=",Q,".",transpose(Qji),"=",Q:ratsimp(Q.transpose(Qji))),
           print("R=Q",j,i,".R=",Qji,".",R,"=",R:ratsimp(Qji.R)),
           print("----------------------")
          )
       )
    ),
if rows#rank then
  (list:makelist(i,i,rank(A)+1,rows),
   print("4-1.刪除Q矩陣的第",list,"行"),
   print(Q,"=>",Q:apply(submatrix,cons(Q,list))),
   print("4-2.刪除R矩陣的第",list,"列"),
   print(R,"=>",R:apply(submatrix,reverse(cons(R,list))))
  ),
for i:1 thru rank do
  (if R[i,i]<0 then
    (print("R[",i,",",i,"]=",R[i,i],"<0"),
     print("R矩陣第",i,"列",R[ i ],",Q矩陣第",i,"行",col(Q,i),"乘上-1倍"),
     R[ i ]:-R[ i ],
     Q:columnop(Q,i,i,2)
    )
  ),
return(ratsimp([Q,R]))
)$


前一篇文章的Hessenberg矩陣
(%i2)
A:matrix([1,-1,-3,-2],
              [3,2,0,-2],
              [0,-3,0,0],
              [0,0,-2,-2]);

(%o2) \( \left[ \matrix{1&-1&-3&-2\cr 3&2&0&-2\cr 0&-3&0&0\cr 0&0&-2&-2} \right] \)

(%i3) [Q,R]: QRbyGivensRotation(A);
1.初始值
\( Q=\left[ \matrix{1&0&0&0\cr 0&1&0&0\cr 0&0&1&0\cr 0&0&0&1} \right],R=\left[ \matrix{1&-1&-3&-2\cr 3&2&0&-2\cr 0&-3&0&0\cr 0&0&-2&-2} \right] \)
2.以\( R[1,1]=1 \)消掉\( R[2,1]=3 \)為0的旋轉矩陣\(Q21\)
\( r=sqrt(R[1,1]^2+R[2,1]^2)=\sqrt{10} \)
\( \displaystyle cos \phi=R[1,1]/r=1/ \sqrt{10}=\frac{1}{\sqrt{10}} \)
\( \displaystyle sin \phi=-R[2,1]/r=-3/ \sqrt{10}=-\frac{3}{\sqrt{10}} \)
\( \displaystyle Q21[1,1]=cos \phi=\frac{1}{\sqrt{10}},Q21[1,2]=-sin \phi=\frac{3}{\sqrt{10}} \)
\( \displaystyle Q21[2,1]=sin \phi=-\frac{3}{\sqrt{10}},Q21[2,2]=cos \phi=\frac{1}{\sqrt{10}} \)
旋轉矩陣\( Q21=\left[ \matrix{\displaystyle \frac{1}{\sqrt{10}}&\frac{3}{\sqrt{10}}&0&0\cr -\frac{3}{\sqrt{10}}&\frac{1}{\sqrt{10}}&0&0\cr 0&0&1&0\cr 0&0&0&1} \right] \)
3.更新\(Q\)矩陣和\(R\)矩陣
\( Q=Q \cdot Q21^T=\left[ \matrix{\displaystyle 1&0&0&0\cr 0&1&0&0\cr 0&0&1&0\cr 0&0&0&1} \right] \cdot \left[ \matrix{\displaystyle \frac{1}{\sqrt{10}}&-\frac{3}{\sqrt{10}}&0&0\cr \frac{3}{\sqrt{10}}&\frac{1}{\sqrt{10}}&0&0\cr 0&0&1&0\cr 0&0&0&1} \right]=\left[ \matrix{\displaystyle \frac{1}{\sqrt{10}}&-\frac{3}{\sqrt{10}}&0&0\cr \frac{3}{\sqrt{10}}&\frac{1}{\sqrt{10}}&0&0\cr 0&0&1&0\cr 0&0&0&1} \right]\)
\( R=Q21 \cdot R=\left[ \matrix{\displaystyle \frac{1}{\sqrt{10}}&\frac{3}{\sqrt{10}}&0&0\cr -\frac{3}{\sqrt{10}}&\frac{1}{\sqrt{10}}&0&0\cr 0&0&1&0\cr 0&0&0&1} \right] \cdot \left[ \matrix{1&-1&-3&-2\cr 3&2&0&-2\cr 0&-3&0&0\cr 0&0&-2&-2} \right]=\left[ \matrix{\displaystyle \sqrt{10}&\frac{5}{\sqrt{10}}&-\frac{3}{\sqrt{10}}&-\frac{8}{\sqrt{10}}\cr 0&\frac{5}{\sqrt{10}}&\frac{9}{\sqrt{10}}&\frac{4}{\sqrt{10}}\cr 0&-3&0&0\cr 0&0&-2&-2} \right] \)
----------------------
2.以\( \displaystyle R[2,2]=\frac{5}{\sqrt{10}}\)消掉\( R[3,2]=-3 \)為0的旋轉矩陣\(Q32\)
\( \displaystyle r=sqrt(R[2,2]^2+R[3,2]^2)=\frac{\sqrt{23}}{\sqrt{2}}\)
\( \displaystyle cos \phi=R[2,2]/r=\frac{5}{\sqrt{10}}/ \frac{\sqrt{23}}{\sqrt{2}}=\frac{5\sqrt{2}}{\sqrt{10}\sqrt{23}} \)
\( \displaystyle sin \phi=-R[3,2]/r=--3/ \frac{\sqrt{23}}{\sqrt{2}}=\frac{3\sqrt{2}}{\sqrt{23}} \)
\( \displaystyle Q32[2,2]=cos \phi=\frac{5\sqrt{2}}{\sqrt{10}\sqrt{23}},Q32[2,3]=-sin \phi=-\frac{3\sqrt{2}}{\sqrt{23}} \)
\( \displaystyle Q32[3,2]=sin \phi=\frac{3\sqrt{2}}{\sqrt{23}},Q32[3,3]=cos \phi=\frac{5\sqrt{2}}{\sqrt{10}\sqrt{23}} \)
旋轉矩陣\(Q32=\left[ \matrix{\displaystyle 1&0&0&0\cr 0&\frac{5\sqrt{2}}{\sqrt{10}\sqrt{23}}&-\frac{3\sqrt{2}}{\sqrt{23}}&0\cr 0&\frac{3\sqrt{2}}{\sqrt{23}}&\frac{5\sqrt{2}}{\sqrt{10}\sqrt{23}}&0\cr 0&0&0&1} \right] \)
更新\(Q\)矩陣和\(R\)矩陣
\( Q=Q \cdot Q32^T=\left[ \matrix{\displaystyle \frac{1}{\sqrt{10}}&-\frac{3}{\sqrt{10}}&0&0\cr \frac{3}{\sqrt{10}}&\frac{1}{\sqrt{10}}&0&0\cr 0&0&1&0\cr 0&0&0&1} \right] \cdot \left[ \matrix{\displaystyle 1&0&0&0\cr 0&\frac{5\sqrt{2}}{\sqrt{10}\sqrt{23}}&\frac{3\sqrt{2}}{\sqrt{23}}&0\cr 0&-\frac{3\sqrt{2}}{\sqrt{23}}&\frac{5\sqrt{2}}{\sqrt{10}\sqrt{23}}&0\cr 0&0&0&1} \right]=\left[ \matrix{\displaystyle \frac{1}{\sqrt{10}}&-\frac{3}{\sqrt{2}\sqrt{23}}&-\frac{9\sqrt{2}}{\sqrt{10}\sqrt{23}}&0\cr \frac{3}{\sqrt{10}}&\frac{1}{\sqrt{2}\sqrt{23}}&\frac{3\sqrt{2}}{\sqrt{10}\sqrt{23}}&0\cr 0&-\frac{3\sqrt{2}}{\sqrt{23}}&\frac{5\sqrt{2}}{\sqrt{10}\sqrt{23}}&0\cr 0&0&0&1} \right] \)
\( R=Q32 \cdot R=\left[ \matrix{\displaystyle 1&0&0&0\cr 0&\frac{5\sqrt{2}}{\sqrt{10}\sqrt{23}}&-\frac{3\sqrt{2}}{\sqrt{23}}&0\cr 0&\frac{3\sqrt{2}}{\sqrt{23}}&\frac{5\sqrt{2}}{\sqrt{10}\sqrt{23}}&0\cr 0&0&0&1} \right] \cdot \left[ \matrix{\displaystyle \sqrt{10}&\frac{5}{\sqrt{10}}&-\frac{3}{\sqrt{10}}&-\frac{8}{\sqrt{10}}\cr 0&\frac{5}{\sqrt{10}}&\frac{9}{\sqrt{10}}&\frac{4}{\sqrt{10}}\cr 0&-3&0&0\cr 0&0&-2&-2} \right]=\left[ \matrix{\displaystyle \sqrt{10}&\frac{5}{\sqrt{10}}&-\frac{3}{\sqrt{10}}&-\frac{8}{\sqrt{10}}\cr 0&\frac{\sqrt{23}}{\sqrt{2}}&\frac{9}{\sqrt{2}\sqrt{23}}&\frac{2^{3/2}}{\sqrt{23}}\cr 0&0&\frac{27\sqrt{2}}{\sqrt{10}\sqrt{23}}&\frac{3 \cdot 2^{5/2}}{\sqrt{10}\sqrt{23}}\cr 0&0&-2&-2} \right] \)
----------------------
2.以\( \displaystyle R[3,3]=\frac{27 \sqrt{2}}{\sqrt{10}\sqrt{23}} \)消掉\(R[4,3]=-2\)為0的旋轉矩陣\(Q43\)
\( \displaystyle r=sqrt(R[3,3]^2+R[4,3]^2)=\frac{\sqrt{1189}}{\sqrt{115}} \)
\( \displaystyle cos \phi=R[3,3]/r=\frac{27\sqrt{2}}{\sqrt{10}\sqrt{23}}/ \frac{\sqrt{1189}}{\sqrt{115}}=\frac{27 \sqrt{2}\sqrt{115}}{\sqrt{10}\sqrt{23}\sqrt{1189}} \)
\( \displaystyle sin \phi=-R[4,3]/r=--2/ \frac{\sqrt{1189}}{\sqrt{115}}=\frac{2 \sqrt{115}}{\sqrt{1189}} \)
\( \displaystyle Q43[3,3]=cos \phi=\frac{27\sqrt{2}\sqrt{115}}{\sqrt{10}\sqrt{23}\sqrt{1189}},Q43[3,4]=-sin \phi=-\frac{2\sqrt{115}}{\sqrt{1189}} \)
\( \displaystyle Q43[4,3]=sin \phi=\frac{2\sqrt{115}}{\sqrt{1189}},Q43[4,4]=cos \phi=\frac{27\sqrt{2}\sqrt{115}}{\sqrt{10}\sqrt{23}\sqrt{1189}} \)
旋轉矩陣\( Q43=\left[ \matrix{\displaystyle 1&0&0&0\cr 0&1&0&0\cr 0&0&\frac{27\sqrt{2}\sqrt{115}}{\sqrt{10}\sqrt{23}\sqrt{1189}}&-\frac{2\sqrt{115}}{\sqrt{1189}}\cr 0&0&\frac{2\sqrt{115}}{\sqrt{1189}}&\frac{27\sqrt{2}\sqrt{115}}{\sqrt{10}\sqrt{23}\sqrt{1189}}} \right] \)
3.更新\(Q\)矩陣和\(R\)矩陣
\( Q=Q.Q43^T=\left[ \matrix{\displaystyle \frac{1}{\sqrt{10}}&-\frac{3}{\sqrt{2}\sqrt{23}}&-\frac{9\sqrt{2}}{\sqrt{10}\sqrt{23}}&0\cr \frac{3}{\sqrt{10}}&\frac{1}{\sqrt{2}\sqrt{23}}&\frac{3\sqrt{2}}{\sqrt{10}\sqrt{23}}&0\cr 0&-\frac{3\sqrt{2}}{\sqrt{23}}&\frac{5\sqrt{2}}{\sqrt{10}\sqrt{23}}&0\cr 0&0&0&1} \right] \cdot \left[ \matrix{\displaystyle 1&0&0&0\cr 0&1&0&0\cr 0&0&\frac{27\sqrt{2}\sqrt{115}}{\sqrt{10}\sqrt{23}\sqrt{1189}}&\frac{2\sqrt{115}}{\sqrt{1189}}\cr 0&0&-\frac{2\sqrt{115}}{\sqrt{1189}}&\frac{27\sqrt{2}\sqrt{115}}{\sqrt{10}\sqrt{23}\sqrt{1189}}} \right]=\left[ \matrix{\displaystyle \frac{1}{\sqrt{10}}&-\frac{3}{\sqrt{2}\sqrt{23}}&-\frac{243}{\sqrt{115}\sqrt{1189}}&-\frac{9 \cdot 2^{3/2}\sqrt{115}}{\sqrt{10}\sqrt{23}\sqrt{1189}}\cr \frac{3}{\sqrt{10}}&\frac{1}{\sqrt{2}\sqrt{23}}&\frac{81}{\sqrt{115}\sqrt{1189}}&\frac{3 \cdot 2^{3/2}\sqrt{115}}{\sqrt{10}\sqrt{23}\sqrt{1189}}\cr 0&-\frac{3\sqrt{2}}{\sqrt{23}}&\frac{27\sqrt{115}}{23\sqrt{1189}}&\frac{5 \cdot 2^{3/2}\sqrt{115}}{\sqrt{10}\sqrt{23}\sqrt{1189}}\cr 0&0&-\frac{2\sqrt{115}}{\sqrt{1189}}&\frac{27\sqrt{2}\sqrt{115}}{\sqrt{10}\sqrt{23}\sqrt{1189}}} \right] \)
\( R=Q43 \cdot R=\left[ \matrix{\displaystyle 1&0&0&0\cr 0&1&0&0\cr 0&0&\frac{27\sqrt{2}\sqrt{115}}{\sqrt{10}\sqrt{23}\sqrt{1189}}&-\frac{2\sqrt{115}}{\sqrt{1189}}\cr 0&0&\frac{2\sqrt{115}}{\sqrt{1189}}&\frac{27\sqrt{2}\sqrt{115}}{\sqrt{10}\sqrt{23}\sqrt{1189}}} \right] \cdot \left[ \matrix{\displaystyle \sqrt{10}&\frac{5}{\sqrt{10}}&-\frac{3}{\sqrt{10}}&-\frac{8}{\sqrt{10}}\cr 0&\frac{\sqrt{23}}{\sqrt{2}}&\frac{9}{\sqrt{2}\sqrt{23}}&\frac{2^{3/2}}{\sqrt{23}}\cr 0&0&\frac{27\sqrt{2}}{\sqrt{10}\sqrt{23}}&\frac{3 \cdot 2^{5/2}}{\sqrt{10}\sqrt{23}}\cr 0&0&-2&-2} \right]=\left[ \matrix{\displaystyle \sqrt{10}&\frac{5}{\sqrt{10}}&-\frac{3}{\sqrt{10}}&-\frac{8}{\sqrt{10}}\cr 0&\frac{\sqrt{23}}{\sqrt{2}}&\frac{9}{\sqrt{2}\sqrt{23}}&\frac{2^{3/2}}{\sqrt{23}}\cr 0&0&\frac{\sqrt{1189}}{\sqrt{115}}&\frac{784}{\sqrt{115}\sqrt{1189}}\cr 0&0&0&-\frac{3\sqrt{2}\sqrt{10}\sqrt{115}}{\sqrt{23}\sqrt{1189}}} \right] \)
----------------------
\( \displaystyle R[4,4]=-\frac{3\sqrt{2}\sqrt{10}\sqrt{115}}{\sqrt{23}\sqrt{1189}}<0 \)
\( R \)矩陣第4列\( \displaystyle [0,0,0,-\frac{3\sqrt{2}\sqrt{10}\sqrt{115}}{\sqrt{23}\sqrt{1189}}],Q \)矩陣第4行\( \left[ \matrix{\displaystyle -\frac{9 \cdot 2^{3/2}\sqrt{115}}{\sqrt{10}\sqrt{23}\sqrt{1189}} \cr\frac{3 \cdot 2^{3/2}\sqrt{115}}{\sqrt{10}\sqrt{23}\sqrt{1189}} \cr\frac{5 \cdot 2^{3/2}\sqrt{115}}{\sqrt{10}\sqrt{23}\sqrt{1189}} \cr\frac{27\sqrt{2}\sqrt{115}}{\sqrt{10}\sqrt{23}\sqrt{1189}}}\right] \)乘上\(-1\)倍
0 errors, 0 warnings
(%o3) \( [\left[ \matrix{\displaystyle \frac{1}{\sqrt{10}}&-\frac{3}{\sqrt{2}\sqrt{23}}&-\frac{243}{\sqrt{115}\sqrt{1189}}&\frac{9\sqrt{2}\sqrt{10}\sqrt{115}}{5\sqrt{23}\sqrt{1189}}\cr \frac{3}{\sqrt{10}}&\frac{1}{\sqrt{2}\sqrt{23}}&\frac{81}{\sqrt{115}\sqrt{1189}}&-\frac{3\sqrt{2}\sqrt{10}\sqrt{115}}{5\sqrt{23}\sqrt{1189}}\cr 0&-\frac{3\sqrt{2}}{\sqrt{23}}&\frac{27\sqrt{115}}{23\sqrt{1189}}&-\frac{\sqrt{2}\sqrt{10}\sqrt{115}}{\sqrt{23}\sqrt{1189}}\cr 0&0&-\frac{2\sqrt{115}}{\sqrt{1189}}&-\frac{27\sqrt{10}\sqrt{115}}{5\sqrt{2}\sqrt{23}\sqrt{1189}}} \right] ,\left[ \matrix{\displaystyle \sqrt{10}&\frac{5}{\sqrt{10}}&-\frac{3}{\sqrt{10}}&-\frac{8}{\sqrt{10}}\cr 0&\frac{\sqrt{23}}{\sqrt{2}}&\frac{9}{\sqrt{2}\sqrt{23}}&\frac{2^{3/2}}{\sqrt{23}}\cr 0&0&\frac{\sqrt{1189}}{\sqrt{115}}&\frac{784}{\sqrt{115}\sqrt{1189}}\cr 0&0&0&\frac{3\sqrt{2}\sqrt{10}\sqrt{115}}{\sqrt{23}\sqrt{1189}}} \right]] \)

\(R.Q\)相乘後仍是Hessenberg矩陣
(%i4) ratsimp(R.Q);
(%o4) \( \left[ \matrix{\displaystyle
\frac{5}{2}&-\frac{7\sqrt{10}}{5 \cdot 2^{3/2}\sqrt{23}}&-\frac{59\sqrt{10}}{\sqrt{115}\sqrt{1189}}&\frac{99 \cdot 2^{3/2}\sqrt{115}}{5\sqrt{23}\sqrt{1189}}\cr
\frac{3\sqrt{23}}{\sqrt{2}\sqrt{10}}&-\frac{31}{46}&\frac{1079\sqrt{2}}{\sqrt{23}\sqrt{115}\sqrt{1189}}&-\frac{168\sqrt{10}}{\sqrt{115}\sqrt{1189}}\cr
0&-\frac{3\sqrt{2}\sqrt{1189}}{\sqrt{23}\sqrt{115}}&-\frac{3961}{27347}&-\frac{16529\sqrt{2}\sqrt{10}}{5945\sqrt{23}}\cr
0&0&-\frac{15 \cdot 2^{3/2}\sqrt{10}\sqrt{23}}{1189}&-\frac{810}{1189}} \right] \)

前一篇文章的三對角矩陣
(%i5)
A:matrix([4,-3,0,0],
              [-3,10/3,-5/3,0],
              [0,-5/3,-33/25,68/75],
              [0,0,68/75,149/75]);

(%o5) \( \left[ \matrix{\displaystyle 4&-3&0&0\cr -3&\frac{10}{3}&-\frac{5}{3}&0\cr 0&-\frac{5}{3}&-\frac{33}{25}&\frac{68}{75}\cr 0&0&\frac{68}{75}&\frac{149}{75}} \right] \)

(%i6) [Q,R]: QRbyGivensRotation(A);
1.初始值
\( Q=\left[ \matrix{1&0&0&0\cr 0&1&0&0\cr 0&0&1&0\cr 0&0&0&1} \right],R=\left[ \matrix{\displaystyle 4&-3&0&0\cr -3&\frac{10}{3}&-\frac{5}{3}&0\cr 0&-\frac{5}{3}&-\frac{33}{25}&\frac{68}{75}\cr 0&0&\frac{68}{75}&\frac{149}{75}} \right] \)
2.以\(R[1,1]=4\)消掉\(R[2,1]=-3\)為0的旋轉矩陣\(Q21\)
\( r=sqrt(R[1,1]^2+R[2,1]^2)=5 \)
\( \displaystyle cos \phi=R[1,1]/r=4/5=\frac{4}{5} \)
\( \displaystyle sin \phi=-R[2,1]/r=--3/5=\frac{3}{5} \)
\( \displaystyle Q21[1,1]=cos \phi=\frac{4}{5},Q21[1,2]=-sin \phi=-\frac{3}{5} \)
\( \displaystyle Q21[2,1]=sin \phi=\frac{3}{5},Q21[2,2]=cos \phi=\frac{4}{5} \)
旋轉矩陣\(Q21=\left[ \matrix{\displaystyle \frac{4}{5}&-\frac{3}{5}&0&0\cr \frac{3}{5}&\frac{4}{5}&0&0\cr 0&0&1&0\cr 0&0&0&1} \right] \)
3.更新\(Q\)矩陣和\(R\)矩陣
\( Q=Q \cdot Q21^T=\left[ \matrix{\displaystyle 1&0&0&0\cr 0&1&0&0\cr 0&0&1&0\cr 0&0&0&1} \right] \cdot \left[ \matrix{\displaystyle \frac{4}{5}&\frac{3}{5}&0&0\cr -\frac{3}{5}&\frac{4}{5}&0&0\cr 0&0&1&0\cr 0&0&0&1} \right]=\left[ \matrix{\displaystyle \frac{4}{5}&\frac{3}{5}&0&0\cr -\frac{3}{5}&\frac{4}{5}&0&0\cr 0&0&1&0\cr 0&0&0&1} \right] \)
\( R=Q21 \cdot R=\left[ \matrix{\displaystyle \frac{4}{5}&-\frac{3}{5}&0&0\cr \frac{3}{5}&\frac{4}{5}&0&0\cr 0&0&1&0\cr 0&0&0&1} \right] \cdot \left[ \matrix{\displaystyle 4&-3&0&0\cr -3&\frac{10}{3}&-\frac{5}{3}&0\cr 0&-\frac{5}{3}&-\frac{33}{25}&\frac{68}{75}\cr 0&0&\frac{68}{75}&\frac{149}{75}} \right]=\left[ \matrix{\displaystyle 5&-\frac{22}{5}&1&0\cr 0&\frac{13}{15}&-\frac{4}{3}&0\cr 0&-\frac{5}{3}&-\frac{33}{25}&\frac{68}{75}\cr 0&0&\frac{68}{75}&\frac{149}{75}} \right] \)
----------------------
2.以\( \displaystyle R[2,2]=\frac{13}{15} \)消掉\( \displaystyle R[3,2]=-\frac{5}{3} \)為0的旋轉矩陣\( Q32 \)
\( \displaystyle r=sqrt(R[2,2]^2+R[3,2]^2)=\frac{\sqrt{794}}{15} \)
\( \displaystyle cos \phi=R[2,2]/r=\frac{13}{15}/ \frac{\sqrt{794}}{15}=\frac{13}{\sqrt{794}} \)
\( \displaystyle sin \phi=-R[3,2]/r=--\frac{5}{3}/ \frac{\sqrt{794}}{15}=\frac{25}{\sqrt{794}} \)
\( \displaystyle Q32[2,2]=cos \phi=\frac{13}{\sqrt{794}},Q32[2,3]=-sin \phi=-\frac{25}{\sqrt{794}} \)
\( \displaystyle Q32[3,2]=sin \phi=\frac{25}{\sqrt{794}},Q32[3,3]=cos \phi=\frac{13}{\sqrt{794}} \)
旋轉矩陣\( Q32=\left[ \matrix{\displaystyle 1&0&0&0\cr 0&\frac{13}{\sqrt{794}}&-\frac{25}{\sqrt{794}}&0\cr 0&\frac{25}{\sqrt{794}}&\frac{13}{\sqrt{794}}&0\cr 0&0&0&1} \right] \)
3.更新\(Q\)矩陣和\(R\)矩陣
\( Q=Q \cdot Q32^T=\left[ \matrix{\displaystyle \frac{4}{5}&\frac{3}{5}&0&0\cr -\frac{3}{5}&\frac{4}{5}&0&0\cr 0&0&1&0\cr 0&0&0&1} \right] \cdot \left[ \matrix{\displaystyle 1&0&0&0\cr 0&\frac{13}{\sqrt{794}}&\frac{25}{\sqrt{794}}&0\cr 0&-\frac{25}{\sqrt{794}}&\frac{13}{\sqrt{794}}&0\cr 0&0&0&1} \right]=\left[ \matrix{\displaystyle \frac{4}{5}&\frac{39}{5\sqrt{794}}&\frac{15}{\sqrt{794}}&0\cr -\frac{3}{5}&\frac{52}{5\sqrt{794}}&\frac{20}{\sqrt{794}}&0\cr 0&-\frac{25}{\sqrt{794}}&\frac{13}{\sqrt{794}}&0\cr 0&0&0&1} \right] \)
\( R=Q32 \cdot R=\left[ \matrix{\displaystyle 1&0&0&0\cr 0&\frac{13}{\sqrt{794}}&-\frac{25}{\sqrt{794}}&0\cr 0&\frac{25}{\sqrt{794}}&\frac{13}{\sqrt{794}}&0\cr 0&0&0&1} \right] \cdot \left[ \matrix{\displaystyle 5&-\frac{22}{5}&1&0\cr 0&\frac{13}{15}&-\frac{4}{3}&0\cr 0&-\frac{5}{3}&-\frac{33}{25}&\frac{68}{75}\cr 0&0&\frac{68}{75}&\frac{149}{75}} \right]=\left[ \matrix{\displaystyle 5&-\frac{22}{5}&1&0\cr 0&\frac{\sqrt{794}}{15}&\frac{47}{3\sqrt{794}}&-\frac{68}{3\sqrt{794}}\cr 0&0&-\frac{3787}{75\sqrt{794}}&\frac{884}{75\sqrt{794}}\cr 0&0&\frac{68}{75}&\frac{149}{75}} \right] \)
----------------------
2.以\( \displaystyle R[3,3]=-\frac{3787}{75\sqrt{794}} \)消掉\( \displaystyle R[4,3]=\frac{68}{75} \)為0的旋轉矩陣\(Q43\)
\( \displaystyle r=sqrt(R[3,3]^2+R[4,3]^2)=\frac{\sqrt{80057}}{5\sqrt{794}} \)
\( \displaystyle cos \phi=R[3,3]/r=-\frac{3787}{75\sqrt{794}}/ \frac{\sqrt{80057}}{5\sqrt{794}}=-\frac{3787}{15\sqrt{80057}} \)
\( \displaystyle sin \phi=-R[4,3]/r=-\frac{68}{75}/ \frac{\sqrt{80057}}{5\sqrt{794}}=-\frac{68\sqrt{794}}{15\sqrt{80057}} \)
\( \displaystyle Q43[3,3]=cos \phi=-\frac{3787}{15\sqrt{80057}},Q43[3,4]=-sin \phi=\frac{68\sqrt{794}}{15\sqrt{80057}} \)
\( \displaystyle Q43[4,3]=sin \phi=-\frac{68\sqrt{794}}{15\sqrt{80057}},Q43[4,4]=cos \phi=-\frac{3787}{15\sqrt{80057}} \)
旋轉矩陣\(Q43=\left[ \matrix{\displaystyle 1&0&0&0\cr 0&1&0&0\cr 0&0&-\frac{3787}{15\sqrt{80057}}&\frac{68\sqrt{794}}{15\sqrt{80057}}\cr 0&0&-\frac{68\sqrt{794}}{15\sqrt{80057}}&-\frac{3787}{15\sqrt{80057}}} \right] \)
3.更新\(Q\)矩陣和\(R\)矩陣
\( Q=Q \cdot Q43^T=\left[ \matrix{\displaystyle \frac{4}{5}&\frac{39}{5\sqrt{794}}&\frac{15}{\sqrt{794}}&0\cr -\frac{3}{5}&\frac{52}{5\sqrt{794}}&\frac{20}{\sqrt{794}}&0\cr 0&-\frac{25}{\sqrt{794}}&\frac{13}{\sqrt{794}}&0\cr 0&0&0&1} \right] \cdot \left[ \matrix{\displaystyle 1&0&0&0\cr 0&1&0&0\cr 0&0&-\frac{3787}{15\sqrt{80057}}&-\frac{68\sqrt{794}}{15\sqrt{80057}}\cr 0&0&\frac{68\sqrt{794}}{15\sqrt{80057}}&-\frac{3787}{15\sqrt{80057}}} \right]=\left[ \matrix{\displaystyle \frac{4}{5}&\frac{39}{5\sqrt{794}}&-\frac{3787}{\sqrt{794}\sqrt{80057}}&-\frac{68}{\sqrt{80057}}\cr -\frac{3}{5}&\frac{52}{5\sqrt{794}}&-\frac{15148}{3\sqrt{794}\sqrt{80057}}&-\frac{272}{3\sqrt{80057}}\cr 0&-\frac{25}{\sqrt{794}}&-\frac{49231}{15\sqrt{794}\sqrt{80057}}&-\frac{884}{15\sqrt{80057}}\cr 0&0&\frac{68\sqrt{794}}{15\sqrt{80057}}&-\frac{3787}{15\sqrt{80057}}} \right] \)
\( R=Q43 \cdot R=\left[ \matrix{\displaystyle 1&0&0&0\cr 0&1&0&0\cr 0&0&-\frac{3787}{15\sqrt{80057}}&\frac{68\sqrt{794}}{15\sqrt{80057}}\cr 0&0&-\frac{68\sqrt{794}}{15\sqrt{80057}}&-\frac{3787}{15\sqrt{80057}}} \right] \cdot \left[ \matrix{\displaystyle 5&-\frac{22}{5}&1&0\cr 0&\frac{\sqrt{794}}{15}&\frac{47}{3\sqrt{794}}&-\frac{68}{3\sqrt{794}}\cr 0&0&-\frac{3787}{75\sqrt{794}}&\frac{884}{75\sqrt{794}}\cr 0&0&\frac{68}{75}&\frac{149}{75}} \right]=\left[ \matrix{\displaystyle 5&-\frac{22}{5}&1&0\cr 0&\frac{\sqrt{794}}{15}&\frac{47}{3\sqrt{794}}&-\frac{68}{3\sqrt{794}}\cr 0&0&\frac{\sqrt{80057}}{5\sqrt{794}}&\frac{20876}{5\sqrt{794}\sqrt{80057}}\cr 0&0&0&-\frac{555}{\sqrt{80057}}} \right] \)
----------------------
\( \displaystyle R[4,4]=-\frac{555}{\sqrt{80057}}<0 \)
\( R \)矩陣第4列\( \displaystyle [0,0,0,-\frac{555}{\sqrt{80057}}],Q \)矩陣第4行\( \left[ \matrix{\displaystyle -\frac{68}{\sqrt{80057}}\cr -\frac{272}{3\sqrt{80057}}\cr -\frac{884}{15\sqrt{80057}}\cr -\frac{3787}{15\sqrt{80057}}} \right] \)乘上\(-1\)倍
(%o6) \( [\left[ \matrix{\displaystyle \frac{4}{5}&\frac{39}{5\sqrt{794}}&-\frac{3787}{\sqrt{794}\sqrt{80057}}&\frac{68}{\sqrt{80057}}\cr -\frac{3}{5}&\frac{52}{5\sqrt{794}}&-\frac{15148}{3\sqrt{794}\sqrt{80057}}&\frac{272}{3\sqrt{80057}}\cr 0&-\frac{25}{\sqrt{794}}&-\frac{49231}{15\sqrt{794}\sqrt{80057}}&\frac{884}{15\sqrt{80057}}\cr 0&0&\frac{68\sqrt{794}}{15\sqrt{80057}}&\frac{3787}{15\sqrt{80057}}} \right],\left[ \matrix{\displaystyle 5&-\frac{22}{5}&1&0\cr 0&\frac{\sqrt{794}}{15}&\frac{47}{3\sqrt{794}}&-\frac{68}{3\sqrt{794}}\cr 0&0&\frac{\sqrt{80057}}{5\sqrt{794}}&\frac{20876}{5\sqrt{794}\sqrt{80057}}\cr 0&0&0&\frac{555}{\sqrt{80057}}} \right]] \)

\(R.Q\)相乘後仍是三對角矩陣
(%i7) ratsimp(R.Q);
(%o7) \( \left[ \matrix{\displaystyle
\frac{166}{25}&-\frac{\sqrt{794}}{25}&0&0\cr
-\frac{\sqrt{794}}{25}&\frac{3971}{19850}&-\frac{5\sqrt{80057}}{794}&0\cr
0&-\frac{5\sqrt{80057}}{794}&-\frac{37521989}{63565258}&\frac{2516\sqrt{794}}{80057}\cr
0&0&\frac{2516\sqrt{794}}{80057}&\frac{140119}{80057}} \right] \)

[ 本帖最後由 bugmens 於 2017-3-21 19:58 編輯 ]
作者: bugmens    時間: 2017-5-28 14:44

拿之前的例子https://math.pro/db/viewthread.php?tid=2561&page=2#pid16487
相伴矩陣\( C=\left[ \matrix{0&0&0&0&0&363.6 \cr 1&0&0&0&0&-349.02 \cr 0&1&0&0&0&-236.39 \cr 0&0&1&0&0&322.63 \cr 0&0&0&1&0&-117.81 \cr 0&0&0&0&1&17.99} \right] \)也是Hessenberg矩陣,QR法執行次數和原來一樣。

實對稱矩陣\( A=\left[ \matrix{ \displaystyle
\frac{3599}{900}&-\frac{1501}{900}&-\frac{1201}{900}&-\frac{901}{900}&\frac{151}{450}&-\frac{301}{900}\cr
-\frac{1501}{900}&\frac{3299}{900}&-\frac{901}{900}&-\frac{601}{900}&\frac{301}{450}&-\frac{1}{900}\cr
-\frac{1201}{900}&-\frac{901}{900}&\frac{2999}{900}&-\frac{301}{900}&\frac{451}{450}&\frac{299}{900}\cr
-\frac{901}{900}&-\frac{601}{900}&-\frac{301}{900}&\frac{2699}{900}&\frac{601}{450}&\frac{599}{900}\cr
\frac{151}{450}&\frac{301}{450}&\frac{451}{450}&\frac{601}{450}&\frac{374}{450}&\frac{901}{450}\cr
-\frac{301}{900}&-\frac{1}{900}&\frac{299}{900}&\frac{599}{900}&\frac{901}{450}&\frac{2099}{900}} \right] \)化簡成三對角矩陣,QR法執行次數反而增加到444次。


Householder,Hessenberg副程式取自https://math.pro/db/viewthread.php?tid=2561&page=2#pid16538
只需要執行結果,不需要計算過程,所以去掉print指令。

要先載入diag才能使用diag指令
(%i1) load(diag);
(%o1) C:\maxima-5.39.0\share\maxima\5.39.0_2_g5a49f11_dirty\share\contrib\diag.mac

轉換成Householder矩陣副程式(去掉print指令)
(%i2)
Householder(x):=block
([n,e1,normx,v,normv],
n:length(x),
if x=zeromatrix(n,1) then
  (return(ident(n))
  ),
if x[1,1]>=0 then sign:+1 else sign:-1,
normx:ratsimp(sqrt(x.x)),
e1:genmatrix(lambda([i,j],if i=1 then 1 else 0),n,1),
v:x+sign*normx*e1,
normv:ratsimp(sqrt(v.v)),
v:ratsimp(v/normv),
H:ratsimp(ident(n)-2*v.transpose(v)),
return(H)
)$


轉換成Hessenberg矩陣副程式(去掉print指令)
(%i3)
Hessenberg(A):=block
([n,H,P],
n:length(A),
for i:1 thru n-2 do
  (x:genmatrix(lambda([ii,jj],A[i+ii,i]),n-i,1),
   H:Householder(x),
   P:diag([ident(i),H]),
   A:ratsimp(P.A.P)
  ),
zerorow:zeromatrix(1,n),
for i:1 thru n-1 do
  (if matrix(A[ i ])=zerorow then
     (A:copymatrix(append(submatrix(i,A),zerorow))
     )
  ),
return(A)
)$


實對稱矩陣
(%i4)
A:matrix([3599/900,-1501/900,-1201/900,-901/900,151/450,-301/900],
[-1501/900,3299/900,-901/900,-601/900,301/450,-1/900],
[-1201/900,-901/900,2999/900,-301/900,451/450,299/900],
[-901/900,-601/900,-301/900,2699/900,601/450,599/900],
[151/450,301/450,451/450,601/450,374/225,901/450],
[-301/900,-1/900,299/900,599/900,901/450,2099/900]);

(%o4) \( \left[ \matrix{ \displaystyle
\frac{3599}{900}&-\frac{1501}{900}&-\frac{1201}{900}&-\frac{901}{900}&\frac{151}{450}&-\frac{301}{900}\cr
-\frac{1501}{900}&\frac{3299}{900}&-\frac{901}{900}&-\frac{601}{900}&\frac{301}{450}&-\frac{1}{900}\cr
-\frac{1201}{900}&-\frac{901}{900}&\frac{2999}{900}&-\frac{301}{900}&\frac{451}{450}&\frac{299}{900}\cr
-\frac{901}{900}&-\frac{601}{900}&-\frac{301}{900}&\frac{2699}{900}&\frac{601}{450}&\frac{599}{900}\cr
\frac{151}{450}&\frac{301}{450}&\frac{451}{450}&\frac{601}{450}&\frac{374}{450}&\frac{901}{450}\cr
-\frac{301}{900}&-\frac{1}{900}&\frac{299}{900}&\frac{599}{900}&\frac{901}{450}&\frac{2099}{900}} \right] \)

設定為數值運算
(%i5) numer:true;
(%o5) true

不顯示小數轉換成分數的過程
例如rat: replaced -4.25 by -17/4 = -4.25

(%i6) ratprint:false;
(%o6) false

設定小數點底下第6位四捨五入
(%i7) fpprintprec:6;
(%o7) 7

轉換成Hessenberg矩陣
(%i8) Hessenberg(A);
(%o8) \( \left[ \matrix{3.99889&2.40601&1.09577\cdot 10^{-15}&1.52435\cdot 10^{-16}&        3.36937\cdot 10^{-15}&-1.96632\cdot 10^{-15} \cr
2.40601&1.68454&-1.93868&-1.46452\cdot 10^{-15}&4.19666\cdot 10^{-15}&-9.19618\cdot 10^{-15} \cr
1.09577\cdot 10^{-15}&-1.93868&2.37077&-1.67177&2.83246\cdot 10^{-16}&5.38814\cdot 10^{-16} \cr
1.52435\cdot 10^{-16}&-1.64384\cdot 10^{-15}&-1.67177&2.35489&1.31944&-6.66134\cdot 10^{-16} \cr
3.36937\cdot 10^{-15}&3.6443\cdot 10^{-15}&-8.64785\cdot 10^{-16}&1.31944&3.60161&-0.725455 \cr
-1.96632\cdot 10^{-15}&-8.90842\cdot 10^{-15}&2.04396\cdot 10^{-16}&        -6.66134\cdot 10^{-16}&-0.725455&        3.9793} \right] \)


接近0的數字當成0,三對角矩陣為\( \left[ \matrix{3.99889&2.40601&0&0&0&0 \cr
2.40601&1.68454&-1.93868&0&0&0 \cr
0&-1.93868&2.37077&-1.67177&0&0 \cr
0&0&-1.67177&2.35489&1.31944&0 \cr
0&0&0&1.31944&3.60161&-0.725455 \cr
0&0&0&0&-0.725455&        3.9793} \right] \)


以QR(A,10^-5);測試執行次數
http://math.pro/db/viewthread.php?tid=2561&page=1#pid16149


(1)相伴矩陣
A:matrix([0,0,0,0,0,363.6],
         [1,0,0,0,0,-349.02],
         [0,1,0,0,0,-236.39],
         [0,0,1,0,0,322.63],
         [0,0,0,1,0,-117.81],
         [0,0,0,0,1,17.99]);
(2)實對稱矩陣
A:matrix([3599/900,-1501/900,-1201/900,-901/900,151/450,-301/900],
[-1501/900,3299/900,-901/900,-601/900,301/450,-1/900],
[-1201/900,-901/900,2999/900,-301/900,451/450,299/900],
[-901/900,-601/900,-301/900,2699/900,601/450,599/900],
[151/450,301/450,451/450,601/450,374/225,901/450],
[-301/900,-1/900,299/900,599/900,901/450,2099/900]);
原始的QR法

1142次

348次

(3)Hessenberg矩陣
A:matrix([0,0,0,0,0,363.6],
         [1,0,0,0,0,-349.02],
         [0,1,0,0,0,-236.39],
         [0,0,1,0,0,322.63],
         [0,0,0,1,0,-117.81],
         [0,0,0,0,1,17.99]);
(4)三對角矩陣
A:matrix([3.99889,2.40601,0,0,0,0],
   [2.40601,1.68454,-1.93868,0,0,0],
   [0,-1.93868,2.37077,-1.67177,0,0],
   [0,0,-1.67177,2.35489,1.31944,0],
   [0,0,0,1.31944,3.60161,-0.72545],
   [0,0,0,0,-0.72545,3.97930]);

原始的QR法

1142次

444次


雖然化簡成Hessenberg矩陣可以減少QR分解的運算量,但QR法執行次數卻增加了。

[ 本帖最後由 bugmens 於 2017-5-28 14:46 編輯 ]
作者: bugmens    時間: 2017-5-30 06:43

2.減少QR法運算-Rayleigh Quotient Shift

之前Power迭代法有介紹利用\( A-\alpha I \)來加快收斂速度。
https://math.pro/db/viewthread.php?tid=2561&page=1#pid16043
在\(QR\)迭代法則取\( \alpha=A[n,n] \)也就是矩陣的右下角元當作位移值。
---------------------------
虛擬碼
\(n\)為\(A\)矩陣的列數或行數
1.對\( k=0,1,2,\ldots \),直到\( \lambda(k) \)收斂為止,計算
2.\( A_k-A[n,n]\cdot I=Q_kR_k \),計算\( A_k-A[n,n]\cdot I \)的\(QR\)分解
3.\( A_{k+1}=R_k Q_k+A[n,n]\cdot I \),將\( Q_k \)和\(R_k\)對調後相乘再加\( A[n,n]\cdot I \)得到下一個矩陣\(A_{k+1}\)
4.特徵值\( \lambda(1),\lambda(2),\ldots,\lambda(n) \)在矩陣\( A_{k+1} \)的對角線上。
---------------------------
計算\( A=\left[ \matrix{0&0&0&0&0&363.6 \cr 1&0&0&0&0&-349.02 \cr 0&1&0&0&0&-236.39 \cr 0&0&1&0&0&322.63 \cr 0&0&0&1&0&-117.81 \cr 0&0&0&0&1&17.99} \right] \)全部的特徵值。
[計算過程]
第一輪:
計算\( A-A[n,n]\cdot I\)的\(QR\)分解
\( \left[ \matrix{0&0&0&0&0&363.6 \cr 1&0&0&0&0&-349.02 \cr 0&1&0&0&0&-236.39 \cr 0&0&1&0&0&322.63 \cr 0&0&0&1&0&-117.81 \cr 0&0&0&0&1&\bbox[border:1px solid blue]{17.99}} \right]-17.99I=\left[ \matrix{-17.99&0&0&0&0&363.6\cr 1&-17.99&0&0&0&-349.02\cr 0&1&-17.99&0&0&-236.39\cr 0&0&1&-17.99&0&322.63\cr 0&0&0&1&-17.99&-117.81\cr 0&0&0&0&1&0} \right]=QR \)
將\(Q\)和\(R\)對調後相乘再加\( A[n,n]\cdot I \)得到下一個矩陣\(A\)
\( RQ+17.99 I= \left[ \matrix{-0.0554152&-0.00307557&-1.70959\cdot 10^{-4}&-9.50302\cdot 10^{-6}&-21.2568&-381.819\cr 0.998463&-1.70696\cdot 10^{-4}&-9.48832\cdot 10^{-6}&-5.27422\cdot 10^{-7}&17.4905&314.168\cr 0&0.999995&-5.2742\cdot 10^{-7}&-2.93174\cdot 10^{-8}&15.1092&271.394\cr 0&0&1.0&-1.62964\cdot 10^{-9}&-17.458&-313.584\cr 0&0&0&1.0&5.6338&101.195\cr 0&0&0&0&-0.310553&12.4118} \right] \)
特徵值\( \left[ -0.0554152,-1.70696\cdot 10^{-4},-5.2742\cdot 10^{-7},-1.62964\cdot 10^{-9},5.6338,12.4118 \right] \)

第二輪:
計算\(A-A[n,n]\cdot I\)的\(QR\)分解
\( \left[ \matrix{-0.0554152&-0.00307557&-1.70959\cdot 10^{-4}&-9.50302\cdot 10^{-6}&-21.2568&-381.819\cr 0.998463&-1.70696\cdot 10^{-4}&-9.48832\cdot 10^{-6}&-5.27422\cdot 10^{-7}&17.4905&314.168\cr 0&0.999995&-5.2742\cdot 10^{-7}&-2.93174\cdot 10^{-8}&15.1092&271.394\cr 0&0&1&-1.62964\cdot 10^{-9}&-17.458&-313.584\cr 0&0&0&1&5.6338&101.195\cr 0&0&0&0&-0.310553&\bbox[border:1px solid blue]{12.4118}} \right]- 12.4118 \cdot I \)
\(= \left[ \matrix{-12.4672&-0.00307557&-1.70959\cdot 10^{-4}&-9.50302\cdot 10^{-6}&-21.2568&-381.819\cr 0.998463&-12.412&-9.48832\cdot 10^{-6}&-5.27422\cdot 10^{-7}&17.4905&314.168\cr 0&0.999995&-12.4118&-2.93174\cdot 10^{-8}&15.1092&271.394\cr 0&0&1&-12.4118&-17.458&-313.584\cr 0&0&0&1&-6.77799&101.195\cr 0&0&0&0&-0.310553&0} \right]=QR \)
將\(Q\)和\(R\)對調後相乘再加\( A[n,n]\cdot I \)得到下一個矩陣\(A\)
\( RQ+ 12.4118 I= \left[ \matrix{-0.134273&-0.0137569&-0.00127716&1.81955&-38.1301&404.512\cr 0.990944&-0.00186405&-1.73055\cdot 10^{-4}&-1.16579&24.4282&-259.152\cr 0&0.999904&-1.7894\cdot 10^{-5}&-1.42457&29.851&-316.681\cr 0&0&0.999999&1.32836&-27.8351&295.295\cr 0&0&0&0.649217&7.37399&-77.7816\cr 0&0&0&0&0.115242&9.4238} \right] \)
特徵值
\( \left[ -0.134273,-0.00186405,-1.7894\cdot 10^{-5},1.32836,7.37399,9.4238 \right] \)

第三輪:
計算\(A-A[n,n]\cdot I\)的\(QR\)分解
\( \left[ \matrix{-0.134273&-0.0137569&-0.00127716&1.81955&-38.1301&404.512\cr 0.990944&-0.00186405&-1.73055\cdot 10^{-4}&-1.16579&24.4282&-259.152\cr 0&0.999904&-1.7894\cdot 10^{-5}&-1.42457&29.851&-316.681\cr 0&0&0.999999&1.32836&-27.8351&295.295\cr 0&0&0&0.649217&7.37399&-77.7816\cr 0&0&0&0&0.115242&\bbox[border:1px solid blue]{9.4238}} \right]- 9.4238 \cdot I\)
\(= \left[ \matrix{-9.55808&-0.0137569&-0.00127716&1.81955&-38.1301&404.512\cr 0.990944&-9.42567&-1.73055\cdot 10^{-4}&-1.16579&24.4282&-259.152\cr 0&0.999904&-9.42382&-1.42457&29.851&-316.681\cr 0&0&0.999999&-8.09544&-27.8351&295.295\cr 0&0&0&0.649217&-2.04981&-77.7816\cr 0&0&0&0&0.115242&0.0} \right]=QR \)
將\(Q\)和\(R\)對調後相乘再加\( A[n,n]\cdot I \)得到下一個矩陣\(A\)
\( RQ+ 9.4238 I= \left[ \matrix{-0.233098&-0.0373848&-0.210022&5.10063&-52.6375&-427.73\cr 0.972453&-0.0089612&0.0852744&-2.15513&22.236&180.689\cr 0&0.999261&0.175089&-4.36603&45.0504&366.077\cr 0&0&0.874009&3.15576&-32.2784&-262.283\cr 0&0&0&0.312162&7.14176&57.2873\cr 0&0&0&0&-0.0484512&7.75945} \right] \)
特徵值\( \left[ -0.233098,-0.0089612,0.175089,3.15576,7.14176,7.75945 \right] \)

反覆計算直到全部特徵值\( \left[ -1.01,1,3,4,5,6 \right] \)



要先載入lapack才能使用dgeqrf指令
(%i1)   load(lapack);
0 errors, 0 warnings
0 errors, 0 warnings
0 errors, 0 warnings
0 errors, 0 warnings
0 errors, 0 warnings
0 errors, 0 warnings
0 errors, 0 warnings
0 errors, 0 warnings
0 errors, 0 warnings
(%o1) C:\maxima-5.39.0\share\maxima\5.39.0_2_g5a49f11_dirty\share\lapack\lapack.mac

有Rayleigh位移的QR分解副程式
(%i2)
QRwithRayleighShift(A,epsilon):=block
([count:0,Q,R,I,n:length(A),eig1,eig2],
eig2:makelist(0,n),
I:ident(n),
do
  (count:count+1,
   [Q,R]:dgeqrf(A-A[n,n]*I),
   A:R.Q+A[n,n]*I,
   eig1:create_list(A[i,i],i,1,n),
   print("特徵值",eig1),
   if lmax(abs(eig1-eig2))<epsilon then
     (print("執行次數",count),
      return(eig1)
     ),
   eig2:eig1
   )
)$


Hessenberg矩陣
(%i3)
A:matrix([0,0,0,0,0,363.6],
         [1,0,0,0,0,-349.02],
         [0,1,0,0,0,-236.39],
         [0,0,1,0,0,322.63],
         [0,0,0,1,0,-117.81],
         [0,0,0,0,1,17.99]);

(%o3) \( \left[ \matrix{0&0&0&0&0&363.6 \cr 1&0&0&0&0&-349.02 \cr 0&1&0&0&0&-236.39 \cr 0&0&1&0&0&322.63 \cr 0&0&0&1&0&-117.81 \cr 0&0&0&0&1&17.99} \right] \)

計算矩陣A全部的特徵值,誤差10^-5
(%i4) QRwithRayleighShift(A,10^-5);
特徵值\( [-0.0554152121071958,-1.706957479434834 \cdot 10^{-4},-5.274195551407956 \cdot 10^{-7},-1.629643975320505 \cdot 10^{-9},5.633796457744232,12.41178997916011] \)
特徵值\( [-0.1342728973892626,-0.001864054657636061,-1.789401310148264 \cdot 10^{-5},1.328361385039951,7.373990728556812,9.423802732463246] \)
特徵值\( [-0.2330984496644373,-0.00896120187027627,0.1750887851007601,3.155759970239055,7.141761495529245,7.75944940066566] \)
特徵值\( [-0.3419389276255256,-0.03277311167899644,0.7656578204246607,4.323032959391513,6.463362526448887,6.812658733039465] \)
特徵值\( [-0.4516838784880166,-0.04859673999350367,1.632578188284038,4.666773060400056,5.895419068145634,6.295510301651798] \)
特徵值\( [-0.5633422880002747,0.02944794977870835,2.355825335361045,4.60082766258795,5.50449211080626,6.062749229466317] \)
特徵值\( [-0.6788276506780742,0.2239951458769376,2.749431534145878,4.436268986900575,5.255300885689792,6.003831098064898] \)
特徵值\( [-0.7863651279227009,0.4527888848794159,2.906886108392509,4.294225869684157,5.12244896958134,6.000015295385284] \)
特徵值\( [-0.8711542188771304,0.6444491049212555,2.960203806951765,4.196380448349329,5.060120858415357,6.00000000023943] \)
特徵值\( [-0.9289699805732585,0.7799453397954821,2.977380864737738,4.131653967421146,5.02998980861915,5.99999999999975] \)
特徵值\( [-0.9645416477980442,0.8674499800082502,2.98345617185446,4.088593345440747,5.015042150494844,5.99999999999975] \)
特徵值\( [-0.9850455588070135,0.9212586824227689,2.986528792091727,4.059703590286082,5.007554494006693,5.99999999999975] \)
特徵值\( [-0.996420748038723,0.9535030515319471,2.988896306535816,4.040228509856105,5.00379288011511,5.99999999999975] \)
特徵值\( [-1.002605207530562,0.9725812027521856,2.991042001366524,4.027079206073314,5.001902797338794,5.99999999999975] \)
特徵值\( [-1.005940275258888,0.9838100028962309,2.992972662236778,4.01820380067515,5.000953809450982,5.99999999999975] \)
特徵值\( [-1.007739426659477,0.9904122174984673,2.994628810107148,4.012220616788556,5.000477782265562,5.99999999999975] \)
特徵值\( [-1.008716781517061,0.994299741198426,2.995984283944792,4.008193554048924,5.000239202325174,5.99999999999975] \)
特徵值\( [-1.009254404197364,0.9965954520976856,2.997051813148713,4.005487429353162,5.000119709598057,5.99999999999975] \)
特徵值\( [-1.009555347544265,0.9979563073390505,2.997867521069139,4.003671627009723,5.000059892126607,5.99999999999975] \)
特徵值\( [-1.009727505038192,0.9987665417094682,2.998476221504767,4.002454783021602,5.00002995880261,5.99999999999975] \)
特徵值\( [-1.00982848582706,0.999251263181379,2.998922039936204,4.001640198987505,5.000014983722228,5.99999999999975] \)
特徵值\( [-1.009889339001543,0.9995427357842255,2.999243733525915,4.001095376371471,5.000007493320185,5.99999999999975] \)
特徵值\( [-1.009927029226854,0.9997189546363359,2.999473089254277,4.000731238185238,5.000003747151255,5.99999999999975] \)
特徵值\( [-1.009950993097018,0.9998261013041958,2.999635017979005,4.000488000073412,5.00000187374066,5.99999999999975] \)
特徵值\( [-1.009966595539297,0.9998916408332965,2.99974842403746,4.000325593743018,5.000000936925779,5.99999999999975] \)
特徵值\( [-1.009976964039406,0.9999319830257809,2.9998273170086,4.000217195523692,5.000000468481591,5.99999999999975] \)
特徵值\( [-1.009983971927121,0.9999569799022288,2.999881892978214,4.000144864799742,5.000000234247194,5.99999999999975] \)
特徵值\( [-1.009988772855428,0.9999725762149927,2.999919468523729,4.000096610991085,5.00000011712588,5.99999999999975] \)
特徵值\( [-1.009992096532979,0.9999823779538515,2.999945235207018,4.000064424808519,5.000000058563849,5.99999999999975] \)
特徵值\( [-1.009994415923166,0.9999885845273688,2.999962843388455,4.000042958725227,5.000000029282374,5.99999999999975] \)
特徵值\( [-1.009996044151435,0.9999925452102545,2.999974840673362,4.000028643626595,5.000000014641483,5.99999999999975] \)
特徵值\( [-1.009997192214406,0.999995092806369,2.999982994075778,4.000019098011532,5.000000007320986,5.99999999999975] \)
執行次數 32
(%o4) \( [-1.009997192214406,0.999995092806369,2.999982994075778,4.000019098011532,5.000000007320986,5.99999999999975] \)

實對稱矩陣
(%i5)
A:matrix([3599/900,-1501/900,-1201/900,-901/900,151/450,-301/900],
[-1501/900,3299/900,-901/900,-601/900,301/450,-1/900],
[-1201/900,-901/900,2999/900,-301/900,451/450,299/900],
[-901/900,-601/900,-301/900,2699/900,601/450,599/900],
[151/450,301/450,451/450,601/450,374/225,901/450],
[-301/900,-1/900,299/900,599/900,901/450,2099/900]);

(%o5) \( \left[ \matrix{ \displaystyle
\frac{3599}{900}&-\frac{1501}{900}&-\frac{1201}{900}&-\frac{901}{900}&\frac{151}{450}&-\frac{301}{900}\cr
-\frac{1501}{900}&\frac{3299}{900}&-\frac{901}{900}&-\frac{601}{900}&\frac{301}{450}&-\frac{1}{900}\cr
-\frac{1201}{900}&-\frac{901}{900}&\frac{2999}{900}&-\frac{301}{900}&\frac{451}{450}&\frac{299}{900}\cr
-\frac{901}{900}&-\frac{601}{900}&-\frac{301}{900}&\frac{2699}{900}&\frac{601}{450}&\frac{599}{900}\cr
\frac{151}{450}&\frac{301}{450}&\frac{451}{450}&\frac{601}{450}&\frac{374}{450}&\frac{901}{450}\cr
-\frac{301}{900}&-\frac{1}{900}&\frac{299}{900}&\frac{599}{900}&\frac{901}{450}&\frac{2099}{900}} \right] \)

計算矩陣A全部的特徵值,誤差10^-5
(%i6) QRwithRayleighShift(A,10^-5);
特徵值\( [4.687463048832852,2.303233931699447,2.895757871614659,4.180419375119289,1.698867537928739,2.224258234805014] \)
特徵值\( [5.0472697019595,1.567035460671847,3.220795257274317,4.165306321709514,1.524463222953819,2.465130035431002] \)
特徵值\( [5.093280274517875,0.9153126236507791,3.918620820796786,4.066872770913875,1.093783248203376,2.902130261917306] \)
特徵值\( [4.670843575256507,0.6452057194127008,4.657952826984372,4.017888431300075,0.9983637095017406,2.9997457375446] \)
特徵值\( [3.93434857995697,1.130324432377812,4.921565821472631,4.004239302946366,0.9995218632488947,2.99999999999732] \)
特徵值\( [3.001259108881703,2.002038670362651,4.985791453569853,4.001029727196395,0.9998810399893925,3.0] \)
特徵值\( [1.989400649137261,3.001349949453158,4.999025079966701,4.000253914617052,0.9999704068258226,3.0] \)
特徵值\( [1.057592981093276,3.931439082990065,5.000912219500666,4.000063077937745,0.999992638478242,3.0] \)
特徵值\( [0.3196378154597554,4.669606730711599,5.000741560680952,4.000015724365658,0.9999981687820307,3.0] \)
特徵值\( [-0.1980838105378795,5.187667036440498,5.000413303579961,4.000003926041754,0.9999995444756622,3.0] \)
特徵值\( [-0.5311792385543539,5.520973849385642,5.00020452153543,4.000000980947106,0.9999998866861723,3.0] \)
特徵值\( [-0.7337023068465403,5.723606010076015,5.000096079783825,4.000000245174043,0.9999999718126538,3.0] \)
特徵值\( [-0.8526274607253512,5.842583415671053,5.0000439907795,4.000000061286531,0.9999999929882648,3.0] \)
特徵值\( [-0.9210400359561284,5.911020150414187,5.000019871965285,4.000000015320857,0.999999998255797,3.0] \)
特徵值\( [-0.9599296300952491,5.94992071499559,5.000008911703409,4.000000003830127,0.99999999956612,3.0] \)
特徵值\( [-0.9818873306755895,5.971883349239707,5.000003980586289,4.000000000957522,0.999999999892069,3.0] \)
特徵值\( [-0.9942375695225221,5.984235795227988,5.000001774082003,4.00000000023938,0.9999999999731504,3.0] \)
特徵值\( [-1.001169057827592,5.991168268066513,5.000000789707915,4.000000000059845,0.99999999999332,3.0] \)
特徵值\( [-1.005054596592398,5.995054245292558,5.000000351286541,4.00000000001496,0.9999999999983373,3.0] \)
特徵值\( [-1.007231207609367,5.997231051402752,5.000000156203289,4.00000000000374,0.9999999999995857,3.0] \)
特徵值\( [-1.008450042880667,5.998449973437261,5.000000069442574,4.000000000000934,0.9999999999998965,3.0] \)
特徵值\( [-1.009132407625798,5.999132376757524,5.000000030868065,4.000000000000233,0.9999999999999738,3.0] \)
特徵值\( [-1.009514383811725,5.999514370091365,5.00000001372031,4.000000000000057,0.9999999999999929,3.0] \)
特徵值\( [-1.009728193294757,5.999728187196538,5.000000006098205,4.000000000000013,0.9999999999999978,3.0] \)
特徵值\( [-1.009847867730331,5.999847865019944,5.000000002710385,4.000000000000003,0.9999999999999991,3.0] \)
特徵值\( [-1.009914851057516,5.999914849852884,5.000000001204633,4.0,0.9999999999999996,3.0] \)
特徵值\( [-1.009952342051302,5.999952341515906,5.000000000535396,3.999999999999999,0.9999999999999996,3.0] \)
特徵值\( [-1.00997332586102,5.999973325623067,5.000000000237954,3.999999999999999,0.9999999999999996,3.0] \)
特徵值\( [-1.009985070512432,5.999985070406677,5.000000000105756,3.999999999999999,0.9999999999999996,3.0] \)
特徵值\( [-1.009991643987654,5.999991643940653,5.000000000047002,3.999999999999999,0.9999999999999996,3.0] \)
執行次數 30
(%o6) \( [-1.009991643987654,5.999991643940653,5.000000000047002,3.999999999999999,0.9999999999999996,3.0] \)

三對角矩陣
(%i7)
A:matrix([3.99889,2.40601,0,0,0,0],
   [2.40601,1.68454,-1.93868,0,0,0],
   [0,-1.93868,2.37077,-1.67177,0,0],
   [0,0,-1.67177,2.35489,1.31944,0],
   [0,0,0,1.31944,3.60161,-0.72545],
   [0,0,0,0,-0.72545,3.97930]);

(%o7) \( \left[ \matrix{3.99889&2.40601&0&0&0&0 \cr
2.40601&1.68454&-1.93868&0&0&0 \cr
0&-1.93868&2.37077&-1.67177&0&0 \cr
0&0&-1.67177&2.35489&1.31944&0 \cr
0&0&0&1.31944&3.60161&-0.725455 \cr
0&0&0&0&-0.725455&        3.9793} \right] \)

計算矩陣A全部的特徵值,誤差10^-5
(%i8) QRwithRayleighShift(A,10^-5);
特徵值\( [1.72387082004926,3.324540397328139,1.33606542554601,3.693330312406069,3.912210035607812,3.999983009062711] \)
特徵值\( [-0.1957077674919923,4.850881309601665,1.378752309771135,4.000089214192345,3.955987137170426,3.99999779675642] \)
特徵值\( [-0.8127503789372117,4.932944318289477,1.873316361922162,4.037006805484861,3.959485096484287,3.999997796756424] \)
特徵值\( [-0.9592847557581403,4.283345647667558,2.665987856408768,4.040426288835855,3.959527166089535,3.999997796756424] \)
特徵值\( [-0.9952758240675927,3.322858800886055,3.662340297890198,4.040634960426104,3.959443968108812,3.999997796756424] \)
特徵值\( [-1.005287026228012,2.395061618114314,4.600198787928333,4.040618030497149,3.959410792931794,3.999997796756424] \)
特徵值\( [-1.008400216221442,1.734394949564231,5.264000184602603,4.040608164880656,3.959399120417531,3.999997796756424] \)
特徵值\( [-1.009439482743673,1.355559926441152,5.643880858248862,4.040607079913188,3.959393821384052,3.999997796756424] \)
特徵值\( [-1.009799761617996,1.164579046933425,5.835223723597451,4.040608967826792,3.959390226503906,3.999997796756424] \)
特徵值\( [-1.00992696494193,1.07452788620796,5.925402526712892,4.04061168529806,3.959387069966598,3.999997796756424] \)
特徵值\( [-1.00997225893254,1.033407889174767,5.966567928871993,4.040614619747304,3.959384024382054,3.999997796756424] \)
特徵值\( [-1.009988449151831,1.014907085280123,5.985084950921726,4.040617609505592,3.959381006687969,3.999997796756424] \)
特徵值\( [-1.009994246304271,1.006638754385619,5.993359085969478,4.040620613208874,3.95937799598388,3.999997796756424] \)
特徵值\( [-1.009996323664509,1.002954616134844,5.99704530333256,4.04062362041098,3.959374987029705,3.999997796756424] \)
特徵值\( [-1.009997068325025,1.001315290801098,5.998685373765045,4.040626628488697,3.959371978513766,3.999997796756424] \)
特徵值\( [-1.009997335300511,1.000586289686123,5.999414641965082,4.040629636784925,3.95936897010796,3.999997796756424] \)
特徵值\( [-1.009997431023016,1.000262196765743,5.999738830635367,4.040632645135246,3.959365961730242,3.999997796756424] \)
特徵值\( [-1.009997465344805,1.000118133210399,5.999882928519348,4.04063565349854,3.959362953360099,3.999997796756424] \)
特徵值\( [-1.009997477651221,1.000054099107653,5.999946974930223,4.040638661864524,3.959359944992403,3.999997796756424] \)
特徵值\( [-1.009997482063836,1.000025637814941,5.999975440635978,4.040641670230628,3.959356936625871,3.999997796756424] \)
特徵值\( [-1.009997483646035,1.000012987820502,5.999988092212724,4.04064467859621,3.959353928260182,3.999997796756424] \)
特徵值\( [-1.009997484213353,1.000007365422972,5.9999937151776,4.04064768696111,3.959350919895256,3.999997796756424] \)
執行次數 22
(%o8) \( [-1.009997484213353,1.000007365422972,5.9999937151776,4.04064768696111,3.959350919895256,3.999997796756424] \)








(1)相伴矩陣
A:matrix([0,0,0,0,0,363.6],
         [1,0,0,0,0,-349.02],
         [0,1,0,0,0,-236.39],
         [0,0,1,0,0,322.63],
         [0,0,0,1,0,-117.81],
         [0,0,0,0,1,17.99]);
(2)實對稱矩陣
A:matrix([3599/900,-1501/900,-1201/900,-901/900,151/450,-301/900],
[-1501/900,3299/900,-901/900,-601/900,301/450,-1/900],
[-1201/900,-901/900,2999/900,-301/900,451/450,299/900],
[-901/900,-601/900,-301/900,2699/900,601/450,599/900],
[151/450,301/450,451/450,601/450,374/225,901/450],
[-301/900,-1/900,299/900,599/900,901/450,2099/900]);
原始的QR法

1142次

348次

Rayleigh Quotient Shift

32次

30次

(3)Hessenberg矩陣
A:matrix([0,0,0,0,0,363.6],
         [1,0,0,0,0,-349.02],
         [0,1,0,0,0,-236.39],
         [0,0,1,0,0,322.63],
         [0,0,0,1,0,-117.81],
         [0,0,0,0,1,17.99]);
(4)三對角矩陣
A:matrix([3.99889,2.40601,0,0,0,0],
   [2.40601,1.68454,-1.93868,0,0,0],
   [0,-1.93868,2.37077,-1.67177,0,0],
   [0,0,-1.67177,2.35489,1.31944,0],
   [0,0,0,1.31944,3.60161,-0.72545],
   [0,0,0,0,-0.72545,3.97930]);

原始的QR法

1142次

444次

Rayleigh Quotient Shift

32次

22次


看得出來\(Rayleigh\) \(Quotient\) \(Shift\)大幅減少\(QR\)迭代次數。

[ 本帖最後由 bugmens 於 2017-7-13 19:20 編輯 ]
作者: bugmens    時間: 2017-7-14 08:26

3-1.減少QR法運算-Wilkinson Shift
取矩陣\( A \)的右下角\( 2 \times 2 \)子矩陣\( \left[ \matrix{A[n-1,n-1]& A[n-1,n]\cr A[n,n-1]&A[n,n]} \right] \),
計算特徵方程式\( \left| \matrix{A[n-1,n-1]-x& A[n-1,n] \cr A[n,n-1]&A[n,n]-x} \right|=0 \),

\( x^2-(A[n-1,n-1]+A[n,n])x+(A[n-1,n-1]A[n,n]-A[n-1,n]A[n,n-1])=0 \)

判別式\( D=(A[n-1,n-1]+A[n,n])^2-4(A[n-1,n-1]A[n,n]-A[n-1,n]A[n,n-1]) \)

\(D=(A[n-1,n-1]-A[n,n])^2+4A[n-1,n]A[n,n-1] \)

\( D\ge 0 \)特徵值為實數,\( D<0 \)特徵值為複數

(1)若特徵値\( \sigma_1,\sigma_2 \)為實數,看哪個特徵値比較接近\( A[n,n] \)當作位移値\( \alpha \)。
if \( \left| \sigma_1-A[n,n] \right|<\left| \sigma_2-A[n,n] \right| \) then
 (\( \alpha=\sigma_1 \))
else
 (\( \alpha=\sigma_2 \))
計算\( A-\alpha I \)的QR分解
計算\( RQ+\alpha I \)得到A矩陣

(2)若特徵值\( \sigma_1,\sigma_2 \)為複數,使用Implicit Double Shift避免複數運算
但\( \sigma_1,\sigma_2 \)為共軛複數,改以\( \sigma,\overline{\sigma} \)表示
個別計算\( A-\sigma I \)和\( A-\overline{\sigma}I \)的QR分解都會遇到複數運算,故將兩次Shift運算結合一起變成實數矩陣再做QR分解
計算\( (A-\sigma I)(A-\overline{\sigma}I)=A^2-(\sigma+\overline{\sigma})A+\sigma \overline{\sigma}I=A^2-2Re(\sigma)A+\left| \sigma \right|^2 I \)為實數矩陣的QR分解
計算\(Q^T \cdot A \cdot Q\)得到A矩陣

參考資料:Implicit Double Shift QR Algorithm
https://books.google.com.tw/book ... ge&q&f=true



要先載入lapack才能使用dgeqrf指令
(%i1) load(lapack);
0 errors, 0 warnings
0 errors, 0 warnings
0 errors, 0 warnings
0 errors, 0 warnings
0 errors, 0 warnings
0 errors, 0 warnings
0 errors, 0 warnings
0 errors, 0 warnings
0 errors, 0 warnings
(%o1) C:\maxima-5.40.0\share\maxima\5.40.0\share\lapack\lapack.mac

Hessenberg矩陣
(%i2)
A:matrix([0,0,0,0,0,363.6],
         [1,0,0,0,0,-349.02],
         [0,1,0,0,0,-236.39],
         [0,0,1,0,0,322.63],
         [0,0,0,1,0,-117.81],
         [0,0,0,0,1,17.99]);

(%o2) \( \left[ \matrix{
0 & 0 & 0 & 0 & 0 & 363.6\cr
1 & 0 & 0 & 0 & 0 & -349.02\cr
0 & 1 & 0 & 0 & 0 & -236.39\cr
0 & 0 & 1 & 0 & 0 & 322.63\cr
0 & 0 & 0 & 1 & 0 & -117.81\cr
0 & 0 & 0 & 0 & 1 & 17.99} \right] \)

設定小數點底下第6位四捨五入
(%i3) fpprintprec:6;
(%o3) 6

右下角2*2矩陣特徵值為實數的Wilkinson Shift
(%i4)
WilkinsonShift(A,eig2by2):=block
(print("特徵值為實數",eig2by2),
if abs(eig2by2[1]-A[n,n])<abs(eig2by2[2]-A[n,n]) then
   (print("特徵值",eig2by2[1],"比較接近A[n,n]=",A[n,n]),
    alpha:eig2by2[1]
   )
else
   (print("特徵值",eig2by2[2],"比較靠近A[n,n]=",A[n,n]),
    alpha:eig2by2[2]
   ),
print("計算A-",alpha,"*I的QR分解=QR"),
[Q,R]:dgeqrf(A-alpha*I),
print("計算A=RQ+",alpha,"*I=",A:R.Q+alpha*I)
)$


右下角2*2矩陣特徵值為複數的Implicit Double Shift
(%i5)
ImplicitDoubleShift(A,eig2by2):=block
(print("特徵值為複數δ=",eig2by2),
print("計算A^2-2Re(δ)A+|δ|^2*I=A^2-2*",realpart(eig2by2[1]),"A+",cabs(eig2by2[1])^2,"I的QR分解"=QR),
[Q,R]:dgeqrf(A.A-2*realpart(eig2by2[1])*A+cabs(eig2by2[2])^2*I),
print("計算A=Q^T.A.Q=",A:transpose(Q).A.Q)
)$


具有WilkinsonShift的QR法副程式
(%i6)
QRwithWilkinsonShift(A,epsilon):=block
([count:0,Q,R,I,n:length(A),subA,eig1,eig2,eigenvalue],
eig2:makelist(0,n),
I:ident(n),
do
  (count:count+1,
   print("第",count,"輪"),
   subA:genmatrix(lambda([i,j],A[n-2+i,n-2+j]),2,2),
   print("A的右下角2*2矩陣",subA),
   print(subA-x*ident(2),"=0,特徵方程式",expand(determinant(subA-x*ident(2))),"=0"),
   eig2by2:dgeev(subA),/*特徵值*/
   if (subA[1,1]-subA[2,2])^2+4*subA[1,2]*subA[2,1]>=0 then/*特徵方程式判別式≧0*/
      (A:WilkinsonShift(A,eig2by2[1])/*特徵值為實數*/
      )
    else
      (A:ImplicitDoubleShift(A,eig2by2[1])/*特徵值為複數*/
      ),
    eig1:create_list(A[i,i],i,1,n),
    print("特徵值",eig1),
    if lmax(abs(eig1-eig2))<epsilon then
      (print("執行次數",count),
      return(eig1)
      ),
    eig2:eig1,
    print("-----------")
  )
)$


計算矩陣A全部的特徵值,誤差10^-5
(%i7) QRwithWilkinsonShift(A,10^-5);
第1輪
\( A \)的右下角2*2矩陣\( \left[ \matrix{0 & -117.81 \cr 1 & 17.99} \right] \)
\( \left[ \matrix{-x & -117.81 \cr 1 & 17.99-x } \right] =0 \),特徵方程式\(x^2-17.99*x+117.81=0 \)
特徵值為複數\( \sigma=[6.07453*\%i+8.995,8.995-6.07453*\%i ] \)
計算\( A^2-2Re(\sigma)A+|\sigma|^2*I=A^2-2*8.995A+117.81I \)的QR分解\( =QR \)
計算\( A=Q^T.A.Q=\left[ \matrix{
-0.15048 & -0.0145152 & -9.53934*10^{-4} & 3.48119 & -21.2753 & -409.554 \cr
0.988613 & -0.00220941 & -1.45201*10^{-4} & -2.10275 & 12.8509 & 247.382 \cr
-2.88398*10^{-17} & 0.999892 & -1.41689*10^{-5} & -2.78352 & 17.0114 & 327.473 \cr
-1.21431*10^{-17} & -2.94903*10^{-17} & 1.0 & 2.48535 & -15.1892 & -292.395 \cr
-1.0842*10^{-18} & -1.73472*10^{-18} & 2.52077*10^{-17} & 0.381684 & 3.84825 & 72.1532 \cr
-2.96462*10^{-20} & -1.6263*10^{-19} & 0.0 & -2.1684*10^{-19} & -0.382882 & 11.8091 } \right] \)
特徵值\( [-0.15048,-0.00220941,-1.41689*10^{-5},2.48535,3.84825,11.8091] \)
-----------
第2輪
\( A \)的右下角2*2矩陣\( \left[ \matrix{3.84825 & 72.1532 \cr -0.382882 & 11.8091 } \right] \)
\( \left[ \matrix{3.84825-x & 72.1532 \cr -0.382882 & 11.8091-x } \right] =0 \),特徵方程式\(x^2-15.6573*x+73.0706=0 \)
特徵值為複數\( \sigma=[3.43255*\%i+7.82867,7.82867-3.43255*\%i ] \)
計算\( A^2-2Re(\sigma)A+|\sigma|^2*I=A^2-2*7.82867A+73.0706I \)的QR分解\( =QR \)
計算\( A=Q^T.A.Q=\left[ \matrix{
-0.340459 & -0.0187684 & -0.846134 & 7.56341 & -44.0982 & 445.365 \cr
0.94026 & -0.034987 & 0.14736 & -1.37467 & 8.01098 & -80.9063 \cr
-1.77764*10^{-17} & 0.94948 & 0.787709 & -6.90939 & 40.2584 & -406.585 \cr
-1.04869*10^{-17} & -2.56953*10^{-17} & 0.571107 & 3.93811 & -22.4175 & 226.378 \cr
-2.61695*10^{-19} & -3.25585*10^{-21} & -1.39862*10^{-17} & 0.155215 & 4.98351 & -48.8466 \cr
-4.16098*10^{-20} & 1.22237*10^{-19} & 0.0 & 1.56125*10^{-17} & 0.136995 & 8.65612} \right] \)
特徵值\( [-0.340459,-0.034987,0.787709,3.93811,4.98351,8.65612 ] \)
-----------
第3輪
\( A \)的右下角2*2矩陣\( \left[ \matrix{4.98351 & -48.8466 \cr0.136995 & 8.65612 } \right] \)
\( \left[ \matrix{4.98351-x & -48.8466 \cr 0.136995 & 8.65612-x } \right] =0 \),特徵方程式\(x^2-13.6396*x+49.8296=0 \)
特徵值為複數\( \sigma=[1.82201*\%i+6.81982,6.81982-1.82201*\%i ] \)
計算\( A^2-2Re(\sigma)A+|\sigma|^2*I=A^2-2*6.81982A+49.8296I \)的QR分解\( =QR \)
計算\( A=Q^T.A.Q=\left[ \matrix{
-0.550722 & 0.160314 & -2.0241 & 10.9002 & -59.6766 & -443.499 \cr
0.843302 & 0.0181879 & -0.597236 & 3.09386 & -16.9466 & -125.941 \cr
-3.643*10^{-17} & 0.660459 & 2.00489 & -10.3831 & 56.7744 & 421.933 \cr
-6.91901*10^{-18} & 7.53929*10^{-17} & 0.225077 & 4.30867 & -22.6477 & -168.297 \cr
3.45526*10^{-19} & 1.51358*10^{-18} & -7.81981*10^{-18} & 0.0606534 & 5.15209 & 36.991 \cr
-1.26193*10^{-20} & 4.42551*10^{-20} & 1.0842*10^{-19} & 4.33681*10^{-18} & -0.0442203 & 7.05688 } \right] \)
特徵值\( [-0.550722,0.0181879,2.00489,4.30867,5.15209,7.05688] \)
-----------
第4輪
\( A \)的右下角2*2矩陣\( \left[ \matrix{5.15209 & 36.991 \cr -0.0442203 & 7.05688 } \right] \)
\( \left[ \matrix{5.15209-x & 36.991 \cr -0.0442203 & 7.05688-x } \right] =0 \),特徵方程式\(x^2-12.209*x+37.9934=0 \)
特徵值為複數\( \sigma=[0.853639*\%i+6.10448,6.10448-0.853639*\%i] \)
計算\( A^2-2Re(\sigma)A+|\sigma|^2*I=A^2-2*6.10448A+37.9934I \)的QR分解\( =QR \)
計算\( A=Q^T.A.Q=\left[ \matrix{
-0.769656 & 0.352202 & -2.62459 & 11.8663 & -61.7088 & 395.945 \cr
0.659697 & 0.377261 & -2.01053 & 8.94582 & -46.53 & 298.552 \cr
-5.61889*10^{-17} & 0.295707 & 2.71313 & -11.7142 & 60.8213 & -390.256 \cr
-3.03024*10^{-18} & 1.56599*10^{-17} & 0.0767723 & 4.28338 & -21.1565 & 135.744 \cr
-3.18495*10^{-20} & -1.6239*10^{-19} & -1.61032*10^{-18} & 0.0181863 & 5.08056 & -31.3718 \cr
4.51695*10^{-22} & -4.18436*10^{-21} & -3.38813*10^{-20} & 4.33681*10^{-19} & 0.011151 & 6.30532 } \right] \)
特徵值\( [-0.769656,0.377261,2.71313,4.28338,5.08056,6.30532] \)
-----------
第5輪
\( A \)的右下角2*2矩陣\( \left[ \matrix{5.08056 & -31.3718 \cr0.011151 & 6.30532 } \right] \)
\( \left[ \matrix{5.08056-x & -31.3718 \cr 0.011151 & 6.30532-x } \right] =0 \),特徵方程式\(x^2-11.3859*x+32.3844=0 \)
特徵值為實數\( [5.53426,5.85162] \)
特徵值5.85162比較靠近\( A[n,n]=6.30532 \)
計算\( A-5.85162*I \)的QR分解\( =QR \)
計算\( A=RQ+5.85162*I=\left[ \matrix{
-0.85821 & 0.360367 & -2.65291 & 11.4251 & -60.8763 & -363.718 \cr
0.537404 & 0.579626 & -2.62892 & 11.1957 & -59.6618 & -356.461 \cr
-3.22653*10^{-17} & 0.177737 & 2.86104 & -11.7597 & 62.5505 & 373.727 \cr
-9.36941*10^{-19} & 4.63981*10^{-18} & 0.0431351 & 4.21815 & -21.2607 & -127.025 \cr
-2.58762*10^{-21} & -5.26611*10^{-20} & -7.55784*10^{-19} & 0.00961362 & 5.23107 & 30.1214 \cr
7.24317*10^{-24} & -8.11741*10^{-23} & -1.063*10^{-21} & 3.12971*10^{-20} & 0.00122958 & 5.95832 } \right] \)
特徵值\( [-0.85821,0.579626,2.86104,4.21815,5.23107,5.95832] \)
-----------
第6輪
\( A \)的右下角2*2矩陣\( \left[ \matrix{5.23107 & 30.1214 \cr 0.00122958 & 5.95832 } \right] \)
\( \left[ \matrix{5.23107-x & 30.1214 \cr 0.00122958 & 5.95832-x } \right] =0 \),特徵方程式\(x^2-11.1894*x+31.1313=0 \)
特徵值為實數\( [5.18328,6.00611] \)
特徵值6.00611比較靠近\( A[n,n]=5.95832 \)
計算\( A-6.00611*I \)的QR分解\( =QR \)
計算\( A=RQ+6.00611*I=\left[ \matrix{
-0.919309 & 0.32341 & -2.5702 & 10.7607 & -55.5116 & 334.865 \cr
0.420282 & 0.730591 & -3.06377 & 12.7235 & -65.6431 & 395.981 \cr
-1.66882*10^{-17} & 0.106845 & 2.92217 & -11.6734 & 60.1084 & -362.601 \cr
-2.71431*10^{-19} & 1.42958*10^{-18} & 0.0258404 & 4.16855 & -20.2876 & 122.381 \cr
-3.60428*10^{-22} & -8.17873*10^{-21} & -1.87791*10^{-19} & 0.00434508 & 5.08827 & -29.5139 \cr
-6.71049*10^{-27} & 9.51864*10^{-26} & 2.0198*10^{-24} & -1.14183*10^{-22} & -8.94925*10^{-6} & 5.99973 } \right] \)
特徵值\( [-0.919309,0.730591,2.92217,4.16855,5.08827,5.99973 ] \)
-----------

省略第7輪到第27輪執行過程

-----------
第28輪
\( A \)的右下角2*2矩陣\( \left[ \matrix{5.0 & 28.9828 \cr 0.0 & 6.0 } \right] \)
\( \left[ \matrix{5.0-x & 28.9828 \cr 0.0 & 6.0-x} \right] =0 \),特徵方程式\(x^2-11.0*x+30.0=0 \)
特徵值為實數\( [5.0,6.0] \)
特徵值6.0比較靠近\( A[n,n]=6.0 \)
計算\( A-6.0*I \)的QR分解\( =QR \)
計算\( A=RQ+6.0*I=\left[ \matrix{
-1.01 & 0.0214797 & -1.96762 & 7.69405 & 38.9703 & 235.303 \cr
2.87689*10^{-4} & 0.999994 & -4.0136 & 15.6682 & 79.3635 & 479.196 \cr
-1.78149*10^{-25} & 1.43282*10^{-6} & 2.99997 & -11.2221 & -56.7202 & -342.484 \cr
-2.56298*10^{-31} & 1.61709*10^{-27} & 2.58843*10^{-6} & 4.00003 & 18.9798 & 114.601 \cr
8.39634*10^{-41} & 2.80728*10^{-36} & 4.40677*10^{-30} & -1.10018*10^{-9} & 5.0 & 28.9828 \cr
0.0 & 0.0 & 0.0 & 0.0 & 0.0 & 6.0} \right] \)
特徵值\( [-1.01,0.999994,2.99997,4.00003,5.0,6.0] \)
-----------
第29輪
\( A\)的右下角2*2矩陣\( \left[ \matrix{5.0 & 28.9828 \cr 0.0 & 6.0} \right] \)
\( \left[ \matrix{5.0-x & 28.9828 \cr 0.0 & 6.0-x} \right] =0 \),特徵方程式\(x^2-11.0*x+30.0=0 \)
特徵值為實數\( [5.0,6.0] \)
特徵值6.0比較靠近\( A[n,n]=6.0\)
計算\( A-6.0*I \)的QR分解\( =QR \)
計算\( A=RQ+6.0*I=\left[ \matrix{-1.01 & 0.0213978 & -1.96746 & 7.6934 & -38.967 & -235.283 \cr
2.052*10^{-4} & 0.999996 & -4.01369 & 15.6686 & -79.3651 & -479.206 \cr
-7.62419*10^{-26} & 8.59697*10^{-7} & 2.99998 & -11.2221 & 56.7202 & 342.484 \cr
-7.31228*10^{-32} & 6.46829*10^{-28} & 1.72559*10^{-6} & 4.00002 & -18.9798 & -114.601 \cr
-1.19777*10^{-41} & -5.61456*10^{-37} & -1.46891*10^{-30} & 5.50096*10^{-10} & 5.0 & 28.9828 \cr
0.0 & 0.0 & 0.0 & 0.0 & 0.0 & 6.0} \right] \)
特徵值\( [-1.01,0.999996,2.99998,4.00002,5.0,6.0 ] \)
執行次數29
(%o7) \( [-1.01,0.999996,2.99998,4.00002,5.0,6.0] \)

[ 本帖最後由 bugmens 於 2017-7-19 07:15 編輯 ]
作者: bugmens    時間: 2017-7-19 22:31

3-2.減少QR法運算-對稱矩陣的Wilkinson Shift
若矩陣\(A\)是對稱矩陣,則計算過程可以再簡化。
矩陣\(A\)的右下角\(2\times2\)子矩陣\( \left[ \matrix{A[n-1,n-1]&A[n-1,n]\cr A[n,n-1]&A[n,n]} \right] \),以簡化的\( \left[ \matrix{a_{n-1}&b_{n-1}\cr b_{n-1}&a_n} \right] \)代替
\( \left[ \matrix{a_{n-1}-x & b_{n-1}\cr b_{n-1}&a_n-x} \right]=0 \),特徵方程式\( x^2-(a_{n-1}+a_n)x+(a_{n-1}a_n-b_{n-1}^2)=0 \)
特徵值\( \displaystyle \lambda_{1,2}=\frac{a_{n-1}+a_n}{2}\pm \sqrt{\left( \frac{a_{n-1}-a_{n}}{2}\right)^2+b_{n-1}^2} \)
令\( \displaystyle \delta=\frac{a_{n-1}-a_{n}}{2} \)
\( \displaystyle \lambda_{1,2}=a_n+\delta\pm \sqrt{\delta+b_{n-1}^2} \)
\( \lambda_{1,2}=a_n+x_{1,2} \),其中\( x_{1,2} \)是方程式\( x^2-2\delta x-b_{n-1}^2=0 \)的根
設\( x_1 \)是方程式比較大的根,\( \left| x_1 \right|=sign(\delta)(\left| \delta \right|+\sqrt{\delta^2+b_{n-1}^2})=\delta+sign(\delta)\sqrt{\delta^2+b_{n-1}^2} \)
利用根與係數的關係\( x_1 \cdot x_2=-b_{n-1}^2 \),求比較小的根\( \displaystyle x_2=\frac{-b_{n-1}^2}{\delta+sign(\delta)\sqrt{\delta^2+b_{n-1}^2}} \)
位移值\( \displaystyle \alpha=a_n-\frac{b_{n-1}^2}{\delta+sign(\delta)\sqrt{\delta^2+b_{n-1}^2}} \)


當\( a_{n-1}=a_n \),\( \delta=0 \)時
\( \lambda_{1,2}=a_n \pm \left| b_{n-1} \right| \)
位移值\( \alpha=a_n-\left| b_{n-1} \right| \)

參考資料
https://books.google.com.tw/book ... e&q&f=false


以簡單的整數為例子
矩陣\(A\)的右下角\(2\times2\)子矩陣\( \left[ \matrix{a_{n-1}&b_{n-1}\cr b_{n-1}&a_n} \right]=\left[ \matrix{1&2 \cr 2&3} \right] \)
\( \left[ \matrix{1-x&2 \cr 2 &3-x} \right]=0 \),特徵方程式\( x^2-4x-1=0 \)
特徵值\( \lambda_{1,2}=2 \pm \sqrt{5} \)
\( 2+\sqrt{5} \)比較接近\( A[n,n]=3 \)
位移值\( \alpha=2+\sqrt{5} \)


代公式
\( \displaystyle \delta=\frac{a_{n-1}-a_{n}}{2}=\frac{1-3}{2}=-1 \)
\( sign(\delta)=-1 \)
位移值\( \displaystyle \alpha=a_n-\frac{b_{n-1}^2}{\delta+sign(\delta)\sqrt{\delta^2+b_{n-1}^2}}=3-\frac{2^2}{-1+(-1)\sqrt{(-1)^2+2^2}}=2+\sqrt{5} \)


\(a_{n-1}=a_{n}\)的例子
矩陣\(A\)的右下角\( 2 \times 2 \)子矩陣\( \left[ \matrix{a_{n-1} & b_{n-1}\cr b_{n-1}&a_n} \right]=\left[ \matrix{1&2 \cr 2&1} \right] \)
\( \left[ \matrix{1-x& 2 \cr 2 &1-x} \right]=0 \),特徵方程式\(x^2-2x-3=0\)
特徵值\(\lambda_{1,2}=-1,3\)
\(-1,3\)和\(A[n,n]=1\)距離相同
位移值\(\alpha=-1\)或\(\alpha=3\)


代公式
\( \displaystyle \delta=\frac{a_{n-1}-a_{n}}{2}=0 \)
\( \lambda_{1,2}=a_n \pm \left| b_{n-1} \right|=-1,3 \)
位移值\( \alpha=a_n-\left| b_{n-1} \right|=-1 \)


位移值\(\alpha\)的maxima程式
delta: (A[n-1,n-1]-A[n,n])/2,
if delta=0 then
  (alpha:A[n,n]-abs(A[n-1,n])
  )
else
  (alpha:float(A[n,n]-A[n-1,n]^2/(delta+signum(delta)*sqrt(delta^2+A[n-1,n]^2)))
  ),



要先載入lapack才能使用dgeqrf指令
(%i1) load(lapack);
0 errors, 0 warnings
0 errors, 0 warnings
0 errors, 0 warnings
0 errors, 0 warnings
0 errors, 0 warnings
0 errors, 0 warnings
0 errors, 0 warnings
0 errors, 0 warnings
0 errors, 0 warnings
(%o1) C:\maxima-5.40.0\share\maxima\5.40.0\share\lapack\lapack.mac

設定小數點底下第6位四捨五入
(%i2) fpprintprec:6;
(%o2) 6

對稱矩陣的WilkinsonShift
(%i3)
SymmetricWilkinsonShift(A):=block
([subA,delta,alpha],   
subA:genmatrix(lambda([i,j],A[n-2+i,n-2+j]),2,2),
print("A的右下角2*2矩陣",subA),
delta: (A[n-1,n-1]-A[n,n])/2,
if delta=0 then
  (alpha:A[n,n]-abs(A[n-1,n])
  )
else
  (alpha:float(A[n,n]-A[n-1,n]^2/(delta+signum(delta)*sqrt(delta^2+A[n-1,n]^2)))
  ),
print("計算A-",alpha,"*I的QR分解=QR"),
[Q,R]:dgeqrf(A-alpha*I),
print("計算A=RQ+",alpha,"*I=",A:R.Q+alpha*I)
)$


具有WilkinsonShift的QR法副程式
(%i4)
QRwithWilkinsonShift(A,epsilon):=block
([count:0,Q,R,I,n:length(A),subA,eig1,eig2,eigenvalue],
eig2:makelist(0,n),
I:ident(n),
do
  (count:count+1,
   print("第",count,"輪"),
   A:SymmetricWilkinsonShift(A),
   eig1:create_list(A[i,i],i,1,n),
   print("特徵值",eig1),
   if lmax(abs(eig1-eig2))<epsilon then
     (print("執行次數",count),
     return(eig1)
     ),
   eig2:eig1,
   print("-----------")
  )
)$


對稱矩陣
(%i5)
A:matrix([3599/900,-1501/900,-1201/900,-901/900,151/450,-301/900],
[-1501/900,3299/900,-901/900,-601/900,301/450,-1/900],
[-1201/900,-901/900,2999/900,-301/900,451/450,299/900],
[-901/900,-601/900,-301/900,2699/900,601/450,599/900],
[151/450,301/450,451/450,601/450,374/225,901/450],
[-301/900,-1/900,299/900,599/900,901/450,2099/900]);

(%o5) \( \left[ \matrix{ \displaystyle
\frac{3599}{900}&-\frac{1501}{900}&-\frac{1201}{900}&-\frac{901}{900}&\frac{151}{450}&-\frac{301}{900}\cr
-\frac{1501}{900}&\frac{3299}{900}&-\frac{901}{900}&-\frac{601}{900}&\frac{301}{450}&-\frac{1}{900}\cr
-\frac{1201}{900}&-\frac{901}{900}&\frac{2999}{900}&-\frac{301}{900}&\frac{451}{450}&\frac{299}{900}\cr
-\frac{901}{900}&-\frac{601}{900}&-\frac{301}{900}&\frac{2699}{900}&\frac{601}{450}&\frac{599}{900}\cr
\frac{151}{450}&\frac{301}{450}&\frac{451}{450}&\frac{601}{450}&\frac{374}{450}&\frac{901}{450}\cr
-\frac{301}{900}&-\frac{1}{900}&\frac{299}{900}&\frac{599}{900}&\frac{901}{450}&\frac{2099}{900}} \right] \)

計算矩陣A全部的特徵值,誤差10^-5
(%i6) QRwithWilkinsonShift(A,10^-5);
第1輪
\(A\)的右下角2*2矩陣\( \left[ \matrix{
374/225 & 901/450 \cr
901/450 & 2099/900} \right] \)
計算\( A-4.02728*I \)的QR分解\( =QR \)
計算\( A=RQ+4.02728*I=\left[ \matrix{
1.62809 & 2.80636 & 0.881862 & -0.487506 & 0.72242 & 0.0113621 \cr
2.80636 & 3.91942 & -0.110835 & 0.32049 & -0.475787 & 0.00735244 \cr
0.881862 & -0.110835 & 5.07773 & 0.0843398 & -0.127243 & 0.0369233 \cr
-0.487506 & 0.32049 & 0.0843398 & 2.89772 & 0.153266 & -0.0269407 \cr
0.72242 & -0.475787 & -0.127243 & 0.153266 & 0.467693 & -0.0602476 \cr
0.0113621 & 0.00735244 & 0.0369233 & -0.0269407 & -0.0602476 & 3.99934} \right] \)

特徵值\( [1.62809,3.91942,5.07773,2.89772,0.467693,3.99934] \)
-----------
第2輪
\(A\)的右下角2*2矩陣\( \left[ \matrix{
0.467693 & -0.0602476 \cr
-0.0602476 & 3.99934} \right] \)
計算\( A-4.00037*I \)的QR分解\( =QR \)
計算\( A=RQ+4.00037*I=\left[ \matrix{
-0.229037 & -1.82178 & -0.262006 & -0.138638 & 0.613611 & -1.08914*10^{-6} \cr
-1.82178 & 5.40498 & 0.165062 & -0.0407398 & 0.180314 & 2.27869*10^{-6} \cr
-0.262006 & 0.165062 & 5.05008 & -0.00508227 & 0.022494 & 1.13371*10^{-5} \cr
-0.138638 & -0.0407398 & -0.00508227 & 2.99446 & 0.0245179 & 9.86431*10^{-6} \cr
0.613611 & 0.180314 & 0.022494 & 0.0245179 & 0.769524 & 6.97715*10^{-6} \cr
-1.08914*10^{-6} & 2.27869*10^{-6} & 1.13371*10^{-5} & 9.86431*10^{-6} & 6.97715*10^{-6} & 4.0} \right] \)

特徵值\( [-0.229037,5.40498,5.05008,2.99446,0.769524,4.0] \)
-----------
第3輪
\(A\)的右下角2*2矩陣\( \left[ \matrix{
0.769524 & 6.97715*10^{-6} \cr
6.97715*10^{-6} & 4.0} \right] \)
計算\( A-4.0*I \)的QR分解\( =QR \)
計算\( A=RQ+4.0*I=\left[ \matrix{
-0.818759 & 0.830858 & 0.0586549 & -0.0309183 & 0.41086 & -2.45934*10^{-16} \cr
0.830858 & 5.88358 & 0.115146 & 0.00377153 & -0.0501182 & 5.74485*10^{-16} \cr
0.0586549 & 0.115146 & 5.01463 & 2.32382*10^{-4} & -0.00308803 & 2.84037*10^{-16} \cr
-0.0309183 & 0.00377153 & 2.32382*10^{-4} & 2.99975 & 0.00328852 & -3.02836*10^{-16} \cr
0.41086 & -0.0501182 & -0.00308803 & 0.00328852 & 0.910799 & -2.75267*10^{-16} \cr
5.58974*10^{-18} & 2.99876*10^{-17} & 2.54508*10^{-16} & -2.35496*10^{-16} & -5.41112*10^{-17} & 4.0} \right] \)

特徵值\( [-0.818759,5.88358,5.01463,2.99975,0.910799,4.0] \)
-----------

省略第4輪到第10輪執行過程

-----------
第11輪
\(A\)的右下角2*2矩陣\( \left[ \matrix{
0.999929 & -2.81267*10^{-16} \cr
0.0 & 4.0} \right] \)
計算\( A-4.0*I \)的QR分解\( =QR \)
計算\( A=RQ+4.0*I=\left[ \matrix{
-1.00997 & 5.64188*10^{-4} & 1.55186*10^{-7} & -8.21073*10^{-8} & 0.00715985 & -2.63617*10^{-16} \cr
5.64188*10^{-4} & 6.0 & 4.8144*10^{-4} & 6.60776*10^{-12} & -5.7625*10^{-7} & 5.09567*10^{-16} \cr
1.55186*10^{-7} & 4.8144*10^{-4} & 5.0 & 1.41015*10^{-15} & -1.09379*10^{-10} & -3.51744*10^{-17} \cr
-8.21073*10^{-8} & 6.6083*10^{-12} & 1.23425*10^{-15} & 3.0 & 1.33342*10^{-10} & -6.4829*10^{-17} \cr
0.00715985 & -5.7625*10^{-7} & -1.09378*10^{-10} & 1.33342*10^{-10} & 0.999974 & -2.81897*10^{-16} \cr
0.0 & 0.0 & 0.0 & 0.0 & 0.0 & 4.0} \right] \)

特徵值\( [-1.00997,6.0,5.0,3.0,0.999974,4.0] \)
-----------
第12輪
\(A\)的右下角2*2矩陣\( \left[ \matrix{
0.999974 & -2.81897*10^{-16} \cr
0.0 & 4.0} \right] \)
計算\( A-4.0*I \)的QR分解\( =QR \)
計算\( A=RQ+4.0*I=\left[ \matrix{
-1.00999 & -2.25226*10^{-4} & -3.09753*10^{-8} & -1.63887*10^{-8} & -0.00428737 & 2.63271*10^{-16} \cr
-2.25226*10^{-4} & 6.0 & 2.4072*10^{-4} & -5.27096*10^{-13} & -1.3775*10^{-7} & -5.09529*10^{-16} \cr
-3.09753*10^{-8} & 2.4072*10^{-4} & 5.0 & 4.69149*10^{-16} & 7.14311*10^{-11} & 3.5297*10^{-17} \cr
-1.63887*10^{-8} & -5.26559*10^{-13} & 2.93119*10^{-16} & 3.0 & 2.22622*10^{-11} & 6.4829*10^{-17} \cr
-0.00428737 & -1.3775*10^{-7} & 7.14309*10^{-11} & 2.22623*10^{-11} & 0.999991 & -2.82273*10^{-16} \cr
0.0 & 0.0 & 0.0 & 0.0 & 0.0 & 4.0} \right] \)

特徵值\( [-1.00999,6.0,5.0,3.0,0.999991,4.0] \)
-----------
第13輪
\(A\)的右下角2*2矩陣\( \left[ \matrix{
0.999991 & -2.82273*10^{-16} \cr
0.0 & 4.0} \right] \)
計算\( A-4.0*I \)的QR分解\( =QR \)
計算\( A=RQ+4.0*I=\left[ \matrix{
-1.01 & 8.99109*10^{-5} & 6.18255*10^{-9} & -3.27108*10^{-9} & 0.00256729 & -2.63006*10^{-16} \cr
8.99109*10^{-5} & 6.0 & 1.2036*10^{-4} & 4.14181*10^{-14} & -3.29283*10^{-8} & 5.09536*10^{-16} \cr
6.18255*10^{-9} & 1.2036*10^{-4} & 5.0 & -1.77861*10^{-16} & 2.6205*10^{-10} & -3.53584*10^{-17} \cr
-3.27108*10^{-9} & 4.19552*10^{-14} & -3.53956*10^{-16} & 3.0 & -1.1726*10^{-10} & -6.4829*10^{-17} \cr
0.00256729 & -3.29283*10^{-8} & 2.6205*10^{-10} & -1.1726*10^{-10} & 0.999997 & -2.82498*10^{-16} \cr
0.0 & 0.0 & 0.0 & 0.0 & 0.0 & 4.0} \right] \)

特徵值\( [-1.01,6.0,5.0,3.0,0.999997,4.0] \)
執行次數13
(%o6) \( [-1.01,6.0,5.0,3.0,0.999997,4.0] \)

三對角矩陣
(%i7)
A:matrix([3.99889,2.40601,0,0,0,0],
   [2.40601,1.68454,-1.93868,0,0,0],
   [0,-1.93868,2.37077,-1.67177,0,0],
   [0,0,-1.67177,2.35489,1.31944,0],
   [0,0,0,1.31944,3.60161,-0.72545],
   [0,0,0,0,-0.72545,3.97930]);

(%o7) \( \left[ \matrix{3.99889&2.40601&0&0&0&0 \cr
2.40601&1.68454&-1.93868&0&0&0 \cr
0&-1.93868&2.37077&-1.67177&0&0 \cr
0&0&-1.67177&2.35489&1.31944&0 \cr
0&0&0&1.31944&3.60161&-0.725455 \cr
0&0&0&0&-0.725455&        3.9793} \right] \)

計算矩陣A全部的特徵值,誤差10^-5
(%i8) QRwithWilkinsonShift(A,10^-5);
第1輪
\(A\)的右下角2*2矩陣\( \left[ \matrix{
3.60161 & -0.72545\cr
-0.72545 & 3.9793} \right] \)

計算\( A-4.54008*I\)的QR分解\(=QR\)
計算\(A=RQ+4.54008*I=\left[ \matrix{
0.765738 & -2.52898 & -2.22045*10^{-16} & 1.66533*10^{-16} & 1.11022*10^{-16} & -2.22045*10^{-16}\cr
-2.52898 & 3.9155 & 1.8145 & 1.11022*10^{-16} & 5.55112*10^{-17} & -1.11022*10^{-16}\cr
0.0 & 1.8145 & 1.78123 & -1.07006 & 1.66533*10^{-16} & -4.996*10^{-16}\cr
0.0 & 0.0 & -1.07006 & 2.8407 & -0.672845 & -2.22045*10^{-16}\cr
0.0 & 0.0 & 0.0 & -0.672845 & 4.37745 & -0.527042\cr
0.0 & 0.0 & 0.0 & 0.0 & -0.527042 & 4.30939} \right] \)

特徵值\( [0.765738,3.9155,1.78123,2.8407,4.37745,4.30939] \)
-----------
第2輪
\(A\)的右下角2*2矩陣\( \left[ \matrix{
4.37745 & -0.527042\cr
-0.527042 & 4.30939} \right] \)
計算\( A-3.81528*I\)的QR分解\(=QR\)
計算\(A=RQ+3.81528*I=\left[ \matrix{
-0.436081 & 1.58352 & -1.41226*10^{-16} & 2.11044*10^{-16} & -1.5642*10^{-18} & 2.62182*10^{-16}\cr
1.58352 & 4.72567 & -1.92785 & 1.32422*10^{-16} & 1.76772*10^{-16} & 2.95571*10^{-16}\cr
0.0 & -1.92785 & 1.74151 & -0.364505 & 2.62527*10^{-16} & 4.26581*10^{-16}\cr
0.0 & 0.0 & -0.364505 & 3.28272 & 0.738764 & 1.11022*10^{-16}\cr
0.0 & 0.0 & 0.0 & 0.738764 & 4.6657 & 0.123502\cr
0.0 & 0.0 & 0.0 & 0.0 & 0.123502 & 4.01048} \right] \)

特徵值\( [-0.436081,4.72567,1.74151,3.28272,4.6657,4.01048] \)
-----------
第3輪
\(A\)的右下角2*2矩陣\( \left[ \matrix{
4.6657 & 0.123502\cr
0.123502 & 4.01048} \right] \)
計算\( A-3.98797*I\)的QR分解\(=QR\)
計算\(A=RQ+3.98797*I=\left[ \matrix{
-0.854732 & -0.770321 & -4.23756*10^{-16} & 1.93093*10^{-16} & 1.10126*10^{-16} & -1.34706*10^{-16}\cr
-0.770321 & 4.36891 & 2.32945 & 1.44982*10^{-17} & -2.19437*10^{-16} & 1.36724*10^{-16}\cr
0.0 & 2.32945 & 2.47901 & -0.131906 & 4.97574*10^{-16} & -4.90434*10^{-16}\cr
0.0 & 0.0 & -0.131906 & 3.3263 & -0.743098 & -1.66533*10^{-16}\cr
0.0 & 0.0 & 0.0 & -0.743098 & 4.67051 & -0.00148842\cr
0.0 & 0.0 & 0.0 & 0.0 & -0.00148842 & 4.0} \right] \)

特徵值\( [-0.854732,4.36891,2.47901,3.3263,4.67051,4.0] \)
-----------

省略第4輪到第18輪執行過程

-----------
第19輪
\(A\)的右下角2*2矩陣\( \left[ \matrix{
4.67044 & -1.3167*10^{-16}\cr
0.0 & 4.0} \right] \)
計算\( A-4.0*I\)的QR分解\(=QR\)
計算\(A=RQ+4.0*I=\left[ \matrix{
-1.01 & 1.22187*10^{-4} & 6.31425*10^{-16} & 1.99522*10^{-16} & -1.43708*10^{-17} & -3.9486*10^{-17}\cr
1.22187*10^{-4} & 1.00003 & 0.0114425 & 1.0112*10^{-16} & -4.9102*10^{-16} & -5.11954*10^{-16}\cr
0.0 & 0.0114425 & 5.99997 & 1.10762*10^{-6} & 1.03762*10^{-16} & 1.39068*10^{-16}\cr
0.0 & 0.0 & 1.10762*10^{-6} & 3.32956 & 0.741963 & -1.46328*10^{-16}\cr
0.0 & 0.0 & 0.0 & 0.741963 & 4.67043 & -3.44542*10^{-17}\cr
0.0 & 0.0 & 0.0 & 0.0 & 0.0 & 4.0} \right] \)

特徵值\( [-1.01,1.00003,5.99997,3.32956,4.67043,4.0] \)
-----------
第20輪
\(A\)的右下角2*2矩陣\( \left[ \matrix{
4.67043 & -3.44542*10^{-17}\cr
0.0 & 4.0} \right] \)
計算\( A-4.0*I\)的QR分解\(=QR\)
計算\(A=RQ+4.0*I=\left[ \matrix{
-1.01 & 7.31656*10^{-5} & 6.29722*10^{-16} & 1.44419*10^{-16} & -1.3841*10^{-16} & 3.94735*10^{-17}\cr
7.31656*10^{-5} & 1.00001 & -0.00762834 & 4.32693*10^{-16} & 2.54117*10^{-16} & 5.12481*10^{-16}\cr
0.0 & -0.00762834 & 5.99999 & -5.53806*10^{-7} & 1.35945*10^{-17} & -1.37114*10^{-16}\cr
0.0 & 0.0 & -5.53806*10^{-7} & 3.32957 & 0.741964 & 7.25401*10^{-17}\cr
0.0 & 0.0 & 0.0 & 0.741964 & 4.67043 & -1.3167*10^{-16}\cr
0.0 & 0.0 & 0.0 & 0.0 & 0.0 & 4.0} \right] \)

特徵值\( [-1.01,1.00001,5.99999,3.32957,4.67043,4.0] \)
-----------
第21輪
\(A\)的右下角2*2矩陣\( \left[ \matrix{
4.67043 & -1.3167*10^{-16}\cr
0.0 & 4.0} \right] \)
計算\( A-4.0*I\)的QR分解\(=QR\)
計算\(A=RQ+4.0*I=\left[ \matrix{
-1.01 & 4.38117*10^{-5} & 6.30883*10^{-16} & 1.99518*10^{-16} & -1.43522*10^{-17} & -3.9466*10^{-17}\cr
4.38117*10^{-5} & 1.00001 & 0.00508558 & 1.01268*10^{-16} & -4.91152*10^{-16} & -5.12132*10^{-16}\cr
0.0 & 0.00508558 & 5.99999 & 2.76901*10^{-7} & 1.03138*10^{-16} & 1.38417*10^{-16}\cr
0.0 & 0.0 & 2.76901*10^{-7} & 3.32957 & 0.741966 & -1.46329*10^{-16}\cr
0.0 & 0.0 & 0.0 & 0.741966 & 4.67043 & -3.44539*10^{-17}\cr
0.0 & 0.0 & 0.0 & 0.0 & 0.0 & 4.0} \right] \)
特徵值\( [-1.01,1.00001,5.99999,3.32957,4.67043,4.0] \)
執行次數21
(%o8) \( [-1.01,1.00001,5.99999,3.32957,4.67043,4.0] \)

[ 本帖最後由 bugmens 於 2017-7-20 04:18 編輯 ]
作者: bugmens    時間: 2017-10-22 22:03

3-3.減少QR法運算-三對角矩陣的Implicit Shift
前一篇文章所述對稱矩陣偏移值的計算可以簡化成\( \displaystyle \alpha=a_n-\frac{b_{n-1}^2}{\delta+sign(\delta)\sqrt{\delta^2+b_{n-1}^2}} \)
再計算\(A-\alpha I\)的\(QR\)分解
計算\(RQ+\alpha I\)得到下一輪的\(A\)矩陣
這是\(Explicit\) \(Single-Shift\) \(QR\)疊代方法

這篇文章介紹\( Implicit \) \(Shift\)疊代方法
以\(n=6\)舉例說明
將對稱矩陣先轉換成三對角矩陣
https://math.pro/db/viewthread.php?tid=2561&page=2#pid16538
\( A=\left[ \matrix{\times & \times & \times & \times & \times & \times \cr
\times & \times & \times & \times & \times & \times \cr
\times & \times & \times & \times & \times & \times \cr
\times & \times & \times & \times & \times & \times \cr
\times & \times & \times & \times & \times & \times \cr
\times & \times & \times & \times & \times & \times} \right] \Rightarrow T=\left[ \matrix{
\times & \times & 0 & 0 & 0 & 0 \cr
\times & \times & \times & 0 & 0 & 0 \cr
0 & \times & \times & \times & 0 & 0 \cr
0 & 0 & \times & \times & \times & 0 \cr
0 & 0 & 0 & \times & \times & \times \cr
0 & 0 & 0 & 0 & \times & \times} \right] \)
找一個\(2 \times 2\)旋轉矩陣\(G\),使得\( \left[ \matrix{cos\theta & -sin\theta \cr sin\theta&cos\theta} \right] \left[ \matrix{T[1,1]-\alpha \cr T[2,1]} \right]=\left[ \matrix{* \cr 0} \right] \)
設\(G_1=diag(\left[ \matrix{cos\theta& -sin\theta \cr sin\theta & cos\theta} \right],I_4)\),計算\( T_1=G_1 T G_1^T=\left[ \matrix{
\times & \times & + & 0 & 0 & 0 \cr
\times & \times & \times & 0 & 0 & 0 \cr
+ & \times & \times & \times & 0 & 0 \cr
0 & 0 & \times & \times & \times & 0 \cr
0 & 0 & 0 & \times & \times & \times \cr
0 & 0 & 0 & 0 & \times & \times} \right] \) 其中\( \matrix{&+ \cr +&} \)形成凸起(bulge)
接下來將凸起往右下角推
取\( \left[ \matrix{T_1[2,1]\cr T_1[3,1]} \right]=\left[ \matrix{\times \cr +} \right] \)找一個\(2 \times 2\)旋轉矩陣,使得\( \left[ \matrix{cos \theta & -sin \theta \cr sin \theta & cos \theta} \right] \left[ \matrix{\times \cr +} \right]=\left[ \matrix{* \cr 0} \right] \)
設\(G_2=diag(I_1,\left[ \matrix{cos\theta& -sin\theta \cr sin\theta & cos\theta} \right],I_3)\),計算\( T_2=G_2 T_1 G_2^T=\left[ \matrix{
\times & \times & 0 & 0 & 0 & 0 \cr
\times & \times & \times & + & 0 & 0 \cr
0 & \times & \times & \times & 0 & 0 \cr
0 & + & \times & \times & \times & 0 \cr
0 & 0 & 0 & \times & \times & \times \cr
0 & 0 & 0 & 0 & \times & \times} \right] \) 將凸起\( \matrix{&+ \cr +&} \)往右下角推
取\( \left[ \matrix{T_2[3,2]\cr T_2[4,2]} \right]=\left[ \matrix{\times \cr +} \right] \)找一個\(2 \times 2\)旋轉矩陣,使得\( \left[ \matrix{cos \theta & -sin \theta \cr sin \theta & cos \theta} \right] \left[ \matrix{\times \cr +} \right]=\left[ \matrix{* \cr 0} \right] \)
設\(G_3=diag(I_2,\left[ \matrix{cos\theta& -sin\theta \cr sin\theta & cos\theta} \right],I_2)\),計算\( T_3=G_3 T_2 G_3^T=\left[ \matrix{
\times & \times & 0 & 0 & 0 & 0 \cr
\times & \times & \times & 0 & 0 & 0 \cr
0 & \times & \times & \times & + & 0 \cr
0 & 0 & \times & \times & \times & 0 \cr
0 & 0 & + & \times & \times & \times \cr
0 & 0 & 0 & 0 & \times & \times} \right] \) 將凸起\( \matrix{&+ \cr +&} \)往右下角推
取\( \left[ \matrix{T_3[4,3]\cr T_3[5,3]} \right]=\left[ \matrix{\times \cr +} \right] \)找一個\(2 \times 2\)旋轉矩陣,使得\( \left[ \matrix{cos \theta & -sin \theta \cr sin \theta & cos \theta} \right] \left[ \matrix{\times \cr +} \right]=\left[ \matrix{* \cr 0} \right] \)
設\(G_4=diag(I_3,\left[ \matrix{cos\theta& -sin\theta \cr sin\theta & cos\theta} \right],I_1)\),計算\( T_4=G_4 T_3 G_4^T=\left[ \matrix{
\times & \times & 0 & 0 & 0 & 0 \cr
\times & \times & \times & 0 & 0 & 0 \cr
0 & \times & \times & \times & 0 & 0 \cr
0 & 0 & \times & \times & \times & + \cr
0 & 0 & 0 & \times & \times & \times \cr
0 & 0 & 0 & + & \times & \times} \right] \) 將凸起\( \matrix{&+ \cr +&} \)往右下角推
取\( \left[ \matrix{T_4[5,4]\cr T_4[6,4]} \right]=\left[ \matrix{\times \cr +} \right] \)找一個\(2 \times 2\)旋轉矩陣,使得\( \left[ \matrix{cos \theta & -sin \theta \cr sin \theta & cos \theta} \right] \left[ \matrix{\times \cr +} \right]=\left[ \matrix{* \cr 0} \right] \)
設\(G_5=diag(I_4,\left[ \matrix{cos\theta& -sin\theta \cr sin\theta & cos\theta} \right])\),計算\( T_5=G_5 T_4 G_5^T=\left[ \matrix{
\times & \times & 0 & 0 & 0 & 0 \cr
\times & \times & \times & 0 & 0 & 0 \cr
0 & \times & \times & \times & 0 & 0 \cr
0 & 0 & \times & \times & \times & 0 \cr
0 & 0 & 0 & \times & \times & \times \cr
0 & 0 & 0 & 0 & \times & \times} \right] \) 仍是三對角矩陣


---------------------------
虛擬碼
TridiagonalImplicitShift(T)
{
\( \displaystyle \delta=\frac{T[n-1,n-1]-T[n,n]}{2} \)
偏移值\( \displaystyle \alpha=T[n,n]-\frac{T[n,n-1]^2}{\delta+sign(\delta)\sqrt{\delta^2+T[n,n-1]^2}} \)
\( x=T[1,1]-\alpha \)
\( y=T[2,1] \)
for k=1 ~ (n-1)
  {\( \displaystyle cos \theta=\frac{x}{\sqrt{x^2+y^2}} \)
   \( \displaystyle sin \theta=-\frac{y}{\sqrt{x^2+y^2}} \)
   旋轉矩陣\( G=\left[ \matrix{cos \theta & -sin \theta \cr sin \theta & cos \theta} \right] \)
   \( G_k=diag(I_{k-1},G,I_{n-k-1}) \)
   計算\( T=G_k \cdot T \cdot G_k^T \)
  }
\(return(T)\),處理後仍是三對角矩陣
}
---------------------------
參考資料https://books.google.com.tw/book ... e&q&f=false

註:本文章採用第一種旋轉矩陣
若旋轉矩陣採用\( G=\left[ \matrix{cos \theta & -sin \theta \cr sin \theta & cos \theta} \right] \),則下一輪三對角矩陣計算方式為\(T_k=G_k.T_{k-1}.G_k^T\)
WIKI https://en.wikipedia.org/wiki/Givens_rotation
線代啟示錄 https://ccjou.wordpress.com/2010 ... qr-分解的應用/

別的書採用另一種旋轉矩陣
若旋轉矩陣採用\( G=\left[ \matrix{cos \theta & sin \theta \cr -sin \theta & cos \theta} \right] \),則下一輪三對角矩陣計算方式為\(T_k=G_k^T.T_{k-1}.G_k\)
Matrix Computations https://books.google.com.tw/book ... e&q&f=false
Handbook of Linear Algebra https://books.google.com.tw/book ... e&q&f=false




要先載入diag才能使用diag指令
(%i1) load(diag);
(%o1) C:\maxima-5.41.0\share\maxima\5.41.0\share\contrib\diag.mac

針對三對角矩陣Implicit Shift副程式
(%i2)
TridiagonalImplicitShift(T):=block
([n:length(T),delta,alpha,x,y,r,cos,sin],
print("δ=(T[",n-1,",",n-1,"]-T[",n,",",n,"])/2=(",T[n-1,n-1],"-",T[n,n],")/2=",delta: (T[n-1,n-1]-T[n,n])/2),
if delta=0 then
  (print("偏移值α=T[",n,",",n,"]-|T[",n-1,",",n,"]|=",T[n,n],"-|",T[n-1,n],"|=",alpha:T[n,n]-abs(T[n-1,n])))
else
  (print("偏移值α=T[",n,",",n,"]-T[",n,",",n-1,"]^2/(δ+sign(δ)*sqrt(δ^2+T[",n,",",n-1,"]^2))"),
   print("偏移值α=",T[n,n],"-(",T[n,n-1],")^2/(",delta,"+",signum(delta),"*sqrt((",delta,")^2+(",T[n,n-1],")^2))=",
   alpha:T[n,n]-T[n-1,n]^2/(delta+signum(delta)*sqrt(delta^2+T[n-1,n]^2)))
  ),
print("取",matrix(["T[1,1]-α"],["T[2,1]"]),"=",matrix(["x"],["y"]),"=",matrix([x:T[1,1]-alpha],[y:T[2,1]])),
for k:1 thru n-1 do
   (print("------------------------"),
    print("k=",k),
    print("r=sqrt(x^2+y^2)=",r:sqrt(x^2+y^2)),
    print("cosθ=x/r=",cos:x/r," , sinθ=-y/r=",sin:-y/r),
    G:ident(n),
    G[k,k]:cos,     G[k,k+1]:-sin,  
    G[k+1,k]:sin, G[k+1,k+1]:cos,
    print("取",matrix([x],[y]),"找一個2*2旋轉矩陣,使得",matrix([cos,-sin],[sin,cos]),matrix([x],[y]),"=",matrix([cos,-sin],[sin,cos]).matrix([x],[y])),
    print("設G_",k,"=",G,",計算T_",k,"=G_",k,".T_",k-1,".G_",k,"^T=",T:ratsimp(G.T.transpose(G))),
    if k<n-1 then
      (x:T[k+1,k],
       y:T[k+2,k],
       print("取",matrix([concat("T",k,"[",k+1,",",k,"]")],[concat("T",k,"[",k+2,",",k,"]")]),"=",matrix(["x"],["y"]),"=",matrix([x],[y]))
      )
   ),
return(T)
)$


三對角矩陣
(%i3)
T:matrix([1,4,0,0,0,0],
              [4,25/12,1,0,0,0],
              [0,1,-31/20,1,0,0],
              [0,0,1,1/20,1,0],
              [0,0,0,1,2,2],
              [0,0,0,0,2,-1]);

(%o3) \(\left[ \matrix{\displaystyle 1&4&0&0&0&0 \cr 4&\frac{25}{12}&1&0&0&0 \cr 0&1&-\frac{31}{20}&1&0&0 \cr 0&0&1&\frac{1}{20}&1&0 \cr 0&0&0&1&2&2 \cr 0&0&0&0&2&-1} \right] \)

執行三對角矩陣的Implicit Shift
(%i4) TridiagonalImplicitShift(T);
\( \displaystyle \delta=(T[5,5]-T[6,6])/2=(2--1)/2=\frac{3}{2} \)
偏移值\( \alpha=T[6,6]-T[6,5]^2/(\delta+sign(\delta)*sqrt(\delta^2+T[6,5]^2)) \)
偏移值\( \displaystyle \alpha=-1-(2)^2/(\frac{3}{2}+1*sqrt((\frac{3}{2})^2+(2)^2))=-2 \)
取\( \left[ \matrix{T[1,1]-\alpha \cr T[2,1]} \right]=\left[ \matrix{x \cr y} \right]=\left[ \matrix{3 \cr 4} \right] \)
------------------------
\( k=1 \)
\( r=sqrt(x^2+y^2)=5 \)
\( \displaystyle cos \theta=x/r=\frac{3}{5},sin \theta=-y/r=-\frac{4}{5} \)
取\( \left[ \matrix{3 \cr 4} \right] \)找一個2*2旋轉矩陣,使得\( \left[ \matrix{\displaystyle \frac{3}{5}&\frac{4}{5}\cr -\frac{4}{5}&\frac{3}{5}} \right] \left[ \matrix{3 \cr 4} \right]=\left[ \matrix{5 \cr 0} \right] \)
設\( G_1=\left[ \matrix{\displaystyle \frac{3}{5}&\frac{4}{5}&0&0&0&0 \cr -\frac{4}{5}&\frac{3}{5}&0&0&0&0 \cr 0&0&1&0&0&0 \cr 0&0&0&1&0&0 \cr 0&0&0&0&1&0 \cr 0&0&0&0&0&1} \right] \),計算\( T_1=G_1 \cdot T_0 \cdot G_1^T=\left[ \matrix{\displaystyle
\frac{83}{15}&-\frac{3}{5}&\frac{4}{5}&0&0&0 \cr
-\frac{3}{5}&-\frac{49}{20}&\frac{3}{5}&0&0&0 \cr
\frac{4}{5}&\frac{3}{5}&-\frac{31}{20}&1&0&0 \cr
0&0&1&\frac{1}{20}&1&0 \cr
0&0&0&1&2&2 \cr
0&0&0&0&2&-1} \right] \)
取\( \left[ \matrix{T1[2,1] \cr T1[3,1]} \right]=\left[ \matrix{x \cr y} \right]=\left[ \matrix{\displaystyle -\frac{3}{5} \cr \frac{4}{5}} \right] \)
------------------------
\( k=2 \)
\( r=sqrt(x^2+y^2)=1 \)
\( \displaystyle cos \theta=x/r=-\frac{3}{5},sin \theta=-y/r=-\frac{4}{5} \)
取\( \left[ \matrix{\displaystyle -\frac{3}{5}\cr \frac{4}{5}} \right] \)找一個2*2旋轉矩陣,使得\( \left[ \matrix{\displaystyle -\frac{3}{5}&\frac{4}{5}\cr -\frac{4}{5}&-\frac{3}{5}} \right] \left[ \matrix{\displaystyle -\frac{3}{5}\cr \frac{4}{5}} \right]=\left[ \matrix{1 \cr 0}\right] \)
設\( G_2=\left[ \matrix{\displaystyle 1&0&0&0&0&0 \cr 0&-\frac{3}{5}&\frac{4}{5}&0&0&0 \cr 0&-\frac{4}{5}&-\frac{3}{5}&0&0&0 \cr 0&0&0&1&0&0 \cr 0&0&0&0&1&0 \cr 0&0&0&0&0&1} \right] \),計算\( T_2=G_2 \cdot T_1 \cdot G_2^T=\left[ \matrix{\displaystyle
\frac{83}{15}&1&0&0&0&0 \cr
1&-\frac{49}{20}&-\frac{3}{5}&\frac{4}{5}&0&0 \cr
0&-\frac{3}{5}&-\frac{31}{20}&-\frac{3}{5}&0&0 \cr
0&\frac{4}{5}&-\frac{3}{5}&\frac{1}{20}&1&0 \cr
0&0&0&1&2&2 \cr
0&0&0&0&2&-1} \right] \)
取\( \left[ \matrix{T2[3,2] \cr T2[4,2]} \right]=\left[ \matrix{x \cr y} \right]=\left[ \matrix{\displaystyle -\frac{3}{5} \cr \frac
{4}{5}} \right] \)
------------------------
\( k=3 \)
\( r=sqrt(x^2+y^2)=1 \)
\( \displaystyle cos \theta=x/r=-\frac{3}{5},sin \theta=-y/r=-\frac{4}{5} \)
取\( \left[ \matrix{\displaystyle -\frac{3}{5} \cr \frac{4}{5}} \right] \)找一個2*2旋轉矩陣,使得\( \left[ \matrix{\displaystyle -\frac{3}{5}&\frac{4}{5}\cr -\frac{4}{5}&-\frac{3}{5}} \right] \left[ \matrix{\displaystyle -\frac{3}{5} \cr \frac{4}{5}} \right]=\left[ \matrix{1 \cr 0} \right] \)
設\( G_3=\left[ \matrix{\displaystyle 1&0&0&0&0&0 \cr 0&1&0&0&0&0 \cr 0&0&-\frac{3}{5}&\frac{4}{5}&0&0 \cr 0&0&-\frac{4}{5}&-\frac{3}{5}&0&0 \cr 0&0&0&0&1&0 \cr 0&0&0&0&0&1} \right] \),計算\( T_3=G_3 \cdot T_2 \cdot G_3^T=\left[ \matrix{\displaystyle
\frac{83}{15}&1&0&0&0&0 \cr
1&-\frac{49}{20}&1&0&0&0 \cr
0&1&\frac{1}{20}&-\frac{3}{5}&\frac{4}{5}&0 \cr
0&0&-\frac{3}{5}&-\frac{31}{20}&-\frac{3}{5}&0 \cr
0&0&\frac{4}{5}&-\frac{3}{5}&2&2 \cr
0&0&0&0&2&-1} \right] \)
取\( \left[ \matrix{T3[4,3] \cr T3[5,3]} \right]=\left[ \matrix{x \cr y} \right]=\left[ \matrix{\displaystyle -\frac{3}{5} \cr \frac{4}{5}} \right] \)
------------------------
\( k=4 \)
\( r=sqrt(x^2+y^2)=1 \)
\( \displaystyle cos \theta=x/r=-\frac{3}{5},sin \theta=-y/r=-\frac{4}{5} \)
取\( \left[ \matrix{\displaystyle -\frac{3}{5}\cr \frac{4}{5}} \right] \)找一個2*2旋轉矩陣,使得\( \left[ \matrix{\displaystyle -\frac{3}{5}&\frac{4}{5}\cr -\frac{4}{5}&-\frac{3}{5}} \right] \left[ \matrix{\displaystyle -\frac{3}{5} \cr \frac{4}{5}} \right]=\left[ \matrix{1 \cr 0} \right] \)
設\( G_4=\left[ \matrix{\displaystyle 1&0&0&0&0&0 \cr 0&1&0&0&0&0 \cr 0&0&1&0&0&0 \cr 0&0&0&-\frac{3}{5}&\frac{4}{5}&0 \cr 0&0&0&-\frac{4}{5}&-\frac{3}{5}&0 \cr 0&0&0&0&0&1} \right] \),計算\( T_4=G_4 \cdot T_3 \cdot G_4^T=\left[ \matrix{\displaystyle
\frac{83}{15}&1&0&0&0&0 \cr
1&-\frac{49}{20}&1&0&0&0 \cr
0&1&\frac{1}{20}&1&0&0 \cr
0&0&1&\frac{649}{500}&-\frac{192}{125}&\frac{8}{5} \cr
0&0&0&-\frac{192}{125}&-\frac{106}{125}&-\frac{6}{5} \cr
0&0&0&\frac{8}{5}&-\frac{6}{5}&-1} \right] \)
取\( \left[ \matrix{T4[5,4] \cr T4[6,4]} \right]=\left[ \matrix{x \cr y} \right]=\left[ \matrix{\displaystyle -\frac{192}{125} \cr \frac{8}{5}} \right] \)
------------------------
\( k=5 \)
\( \displaystyle r=sqrt(x^2+y^2)=\frac{8\sqrt{1201}}{125} \)
\( \displaystyle cos \theta=x/r=-\frac{24}{\sqrt{1201}},sin \theta=-y/r=-\frac{25}{\sqrt{1201}} \)
取\( \left[ \matrix{\displaystyle -\frac{192}{125} \cr \frac{8}{5}} \right] \)找一個2*2旋轉矩陣,使得\(\left[ \matrix{\displaystyle -\frac{24}{\sqrt{1201}}&\frac{25}{\sqrt{1201}}\cr -\frac{25}{\sqrt{1201}}&-\frac{24}{\sqrt{1201}}} \right]
\left[ \matrix{\displaystyle -\frac{192}{125} \cr \frac{8}{5}} \right]=\left[ \matrix{\displaystyle \frac{8\sqrt{1201}}{125} \cr 0} \right] \)
設\( G_5=\left[ \matrix{\displaystyle 1&0&0&0&0&0 \cr 0&1&0&0&0&0 \cr 0&0&1&0&0&0 \cr 0&0&0&0&-\frac{24}{\sqrt{1201}}&\frac{25}{\sqrt{1201}} \cr 0&0&0&0&-\frac{25}{\sqrt{1201}}&-\frac{24}{\sqrt{1201}}} \right] \),計算\( T_5=G_5 \cdot T_4 \cdot G_5^T=\left[ \matrix{\displaystyle
\frac{83}{15}&1&0&0&0&0 \cr
1&-\frac{49}{20}&1&0&0&0 \cr
0&1&\frac{1}{20}&1&0&0 \cr
0&0&1&\frac{649}{500}&\frac{8\sqrt{1201}}{125}&0 \cr
0&0&0&\frac{8\sqrt{1201}}{125}&\frac{40819}{150125}&\frac{150}{1201} \cr
0&0&0&0&\frac{150}{1201}&-\frac{2546}{1201}} \right] \)
(%o4) \( \left[ \matrix{\displaystyle
\frac{83}{15}&1&0&0&0&0 \cr
1&-\frac{49}{20}&1&0&0&0 \cr
0&1&\frac{1}{20}&1&0&0 \cr
0&0&1&\frac{649}{500}&\frac{8\sqrt{1201}}{125}&0 \cr
0&0&0&\frac{8\sqrt{1201}}{125}&\frac{40819}{150125}&\frac{150}{1201} \cr
0&0&0&0&\frac{150}{1201}&-\frac{2546}{1201}} \right] \)

[ 本帖最後由 bugmens 於 2017-11-4 09:55 編輯 ]
作者: bugmens    時間: 2017-11-4 09:54

3-4.減少QR法運算-Hessenberg矩陣的Implicit Double Shift
前面文章提到https://math.pro/db/viewthread.php?tid=2561&page=2#pid17755

取矩陣\( A \)的右下角\( 2 \times 2 \)子矩陣\( \left[ \matrix{A[n-1,n-1]& A[n-1,n]\cr A[n,n-1]&A[n,n]} \right] \),
計算特徵方程式\( \left| \matrix{A[n-1,n-1]-x& A[n-1,n] \cr A[n,n-1]&A[n,n]-x} \right|=0 \),

\( x^2-(A[n-1,n-1]+A[n,n])x+(A[n-1,n-1]A[n,n]-A[n-1,n]A[n,n-1])=0 \)

判別式\( D=(A[n-1,n-1]+A[n,n])^2-4(A[n-1,n-1]A[n,n]-A[n-1,n]A[n,n-1]) \)

\(D=(A[n-1,n-1]-A[n,n])^2+4A[n-1,n]A[n,n-1] \)

\( D\ge 0 \)特徵值為實數,\( D<0 \)特徵值為複數

(1)若特徵值\( \sigma_1,\sigma_2 \)為實數,直接計算\( (A-\sigma_1I)(A- \sigma_2I) \)

(2)若特徵值\( \sigma_1,\sigma_2 \)為複數,使用Implicit Double Shift避免複數運算
但\( \sigma_1,\sigma_2 \)為共軛複數,改以\( \sigma,\overline{\sigma} \)表示
個別計算\( A-\sigma I \)和\( A-\overline{\sigma}I \)的QR分解都會遇到複數運算,故將兩次Shift運算結合一起變成實數矩陣再做QR分解
計算\( (A-\sigma I)(A-\overline{\sigma}I)=A^2-(\sigma+\overline{\sigma})A+\sigma \overline{\sigma}I=A^2-2Re(\sigma)A+\left| \sigma \right|^2 I \)為實數矩陣的QR分解
計算\(Q^T \cdot A \cdot Q\)得到\(A\)矩陣

但\(A^2\)矩陣相乘是\( O(n^3) \)的運算,可以先將\(A\)矩陣化簡成\(Hessenberg\)矩陣再進行\( Implicit\) \(Double\) \(Shift \),只需要\(O(n^2)\)的運算。
以\(n=6\)舉例說明
將\(A\)矩陣轉換成\(Hessenberg\)矩陣
\( A=\left[ \matrix{\times & \times & \times & \times & \times & \times \cr
\times & \times & \times & \times & \times & \times \cr
\times & \times & \times & \times & \times & \times \cr
\times & \times & \times & \times & \times & \times \cr
\times & \times & \times & \times & \times & \times \cr
\times & \times & \times & \times & \times & \times} \right] \Rightarrow H=\left[ \matrix{\times & \times & \times & \times & \times & \times \cr
\times & \times & \times & \times & \times & \times \cr
0 & \times & \times & \times & \times & \times \cr
0 & 0 & \times & \times & \times & \times \cr
0 & 0 & 0 & \times & \times & \times \cr
0 & 0 & 0 & 0 & \times & \times} \right] \)
計算\( H^2-2Re(\sigma)H+\left| \sigma \right|^2 I \)的第一列\( n_1=\left[ \matrix{x \cr y \cr z \cr 0 \cr 0 \cr 0} \right] \),前三個元素非零,其餘元素為0
取向量\( \left[ \matrix{x \cr y \cr z} \right] \)找一個\( 3\times 3 Householder \)矩陣\( \hat{P_0} \),使得\( \hat{P_0}\left[ \matrix{x \cr y \cr z} \right]=\left[ \matrix{* \cr 0 \cr 0} \right] \)
設\( P_0=diag(\hat{P_0},I_3) \),計算\( H_0=P_0^T H P_0 =\left[ \matrix{\times & \times & \times & \times & \times & \times \cr
\times & \times & \times & \times & \times & \times \cr
+ & \times & \times & \times & \times & \times \cr
+ & + & \times & \times & \times & \times \cr
0 & 0 & 0 & \times & \times & \times \cr
0 & 0 & 0 & 0 & \times & \times} \right] \) 其中\( \matrix{+& \cr +& +} \)形成凸起(bulge)

接下來將凸起往右下角推
取向量\( \left[ \matrix{H_0[2,1] \cr H_0[3,1] \cr H_0[4,1]} \right]=\left[ \matrix{\times \cr + \cr +} \right] \)找一個\( 3\times 3 Householder \)矩陣\( \hat{P_1} \),使得\( \hat{P_1}\left[ \matrix{\times \cr + \cr +} \right]=\left[ \matrix{* \cr 0 \cr 0} \right] \)
設\( P_1=diag(I_1,\hat{P_1},I_2) \),計算\( H_1=P_1^T H_0 P_1 =\left[ \matrix{\times & \times & \times & \times & \times & \times \cr
\times & \times & \times & \times & \times & \times \cr
0 & \times & \times & \times & \times & \times \cr
0 & + & \times & \times & \times & \times \cr
0 & + & + & \times & \times & \times \cr
0 & 0 & 0 & 0 & \times & \times} \right] \) 將凸起\( \matrix{+& \cr +& +} \)往右下角推

取向量\( \left[ \matrix{H_1[3,2] \cr H_1[4,2] \cr H_1[5,2]} \right]=\left[ \matrix{\times \cr + \cr +} \right] \)找一個\( 3\times 3 Householder \)矩陣\( \hat{P_2} \),使得\( \hat{P_2}\left[ \matrix{\times \cr + \cr +} \right]=\left[ \matrix{* \cr 0 \cr 0} \right] \)
設\( P_2=diag(I_2,\hat{P_2},I_1) \),計算\( H_2=P_2^T H_1 P_2 =\left[ \matrix{\times & \times & \times & \times & \times & \times \cr
\times & \times & \times & \times & \times & \times \cr
0 & \times & \times & \times & \times & \times \cr
0 & 0 & \times & \times & \times & \times \cr
0 & 0 & + & \times & \times & \times \cr
0 & 0 & + & + & \times & \times} \right] \) 將凸起\( \matrix{+& \cr +& +} \)往右下角推

取向量\( \left[ \matrix{H_2[4,3] \cr H_2[5,3] \cr H_2[6,3]} \right]=\left[ \matrix{\times \cr + \cr +} \right] \)找一個\( 3\times 3 Householder \)矩陣\( \hat{P_3} \),使得\( \hat{P_3}\left[ \matrix{\times \cr + \cr +} \right]=\left[ \matrix{* \cr 0 \cr 0} \right] \)
設\( P_3=diag(I_3,\hat{P_3}) \),計算\( H_3=P_3^T H_2 P_3 =\left[ \matrix{\times & \times & \times & \times & \times & \times \cr
\times & \times & \times & \times & \times & \times \cr
0 & \times & \times & \times & \times & \times \cr
0 & 0 & \times & \times & \times & \times \cr
0 & 0 & 0 & \times & \times & \times \cr
0 & 0 & 0 & + & \times & \times} \right] \) 將凸起+往右下角推

取向量\( \left[ \matrix{H_3[5,4] \cr H_3[6,4]} \right]=\left[ \matrix{\times \cr +} \right] \)找一個\( 2\times 2 Householder \)矩陣\( \hat{P_4} \),使得\( \hat{P_4}\left[ \matrix{\times \cr +} \right]=\left[ \matrix{* \cr 0} \right] \)
設\( P_4=diag(I_4,\hat{P_4}) \),計算\( H_4=P_4^T H_3 P_4=\left[ \matrix{\times & \times & \times & \times & \times & \times \cr
\times & \times & \times & \times & \times & \times \cr
0 & \times & \times & \times & \times & \times \cr
0 & 0 & \times & \times & \times & \times \cr
0 & 0 & 0 & \times & \times & \times \cr
0 & 0 & 0 & 0 & \times & \times} \right] \) 仍是\(Hessenberg\)矩陣


---------------------------
虛擬碼
Householder(x)
{
\( e1 \)為第1元為1,其餘為0的向量
定義\( sign(x_1)=+1 \)若\( x_1 \ge 0 \)
  \( sign(x_1)=-1 \)若\( x_1 <0 \)
\( \displaystyle v=\frac{x+sign(x_1)\Vert\; x \Vert\; e1}{\Vert\; x+sign(x_1)\Vert\; x \Vert\; e1 \Vert\;} \),其中\( \Vert\; * \Vert\; \)為向量長度
Householder矩陣\(H=I-2vv^T \)
\( return(H) \)
}

HessenbergImplicitDoubleShift(H)
{
\( t=H[n-1,n-1]+H[n,n] \)
\( d=H[n-1,n-1]H[n,n]-H[n,n-1]H[n-1,n] \)
\( \cases{x=H[1,1]^2-tH[1,1]+d+H[1,2]H[2,1] \cr y=H[2,1](H[1,1]+H[2,2]-t) \cr z=H[2,1]H[3,2]} \),計算\( H^2-tH+dI \)的第一列的前三元\( \left[ \matrix{x \cr y \cr z} \right] \)
for k=1 ~ (n-3)
 {\( \hat{P_k}=Householder(\left[ \matrix{x \cr y \cr z} \right]) \),找一個\( 3 \times 3 Householder \)矩陣\( \hat{P_k} \),使得\( \hat{P_k}\left[ \matrix{x \cr y \cr z} \right]=\left[ \matrix{* \cr 0 \cr 0} \right] \),*為非零數字
  \( P_k=diag(I_k,\hat{P_k},I_{n-k-3}) \)
  \( H=P_k^T H P_k \)
  \( x=H[k+2,k+1] \)
  \( y=H[k+3,k+1] \)
  if \(k<n-3\) then
  \( \{\; z=H[k+4,k+1] \}\; \)
 }
\( \hat{P}_{n-2}=Householder(\left[ \matrix{x \cr y} \right]) \),找一個\( 2 \times 2 Householder \)矩陣\( \hat{P}_{n-2} \),使得\( \hat{P}_{n-2}\left[ \matrix{x \cr y} \right]=\left[ \matrix{* \cr 0} \right] \),*為非零數字
\( P_{n-2}=diag(I_{n-2},\hat{P}_{n-2}) \)
\( H=P_{n-2}^T H P_{n-2} \)
\( return(H) \),處理後仍是\( Hessenberg \)矩陣
}
---------------------------
參考資料https://books.google.com.tw/book ... e&q&f=false



要先載入diag才能使用diag指令
(%i1) load(diag);
(%o1) C:\maxima-5.40.0\share\maxima\5.40.0\share\contrib\diag.mac

轉換成Householder矩陣副程式
(%i2)
Householder(X):=block
([n,e1,normx,v,normv,P_hat],
n:length(X),
if X=zeromatrix(n,1) then
  (print("X為0向量,H=",ident(n)),
   return(ident(n))
  ),
if X[1,1]>=0 then sign:+1 else sign:-1,
print("X向量=",X,"第1元=",X[1,1]," , sign(X_1)=",sign,",長度∥X∥=sqrt(",X,".",X,")=",normx:ratsimp(sqrt(X.X))),
print("第1元是1,其餘為0的向量e1=",e1:genmatrix(lambda([i,j],if i=1 then 1 else 0),n,1)),
print("X+sign(X_1)*∥X∥*e1=",X,"+",sign,"*",normx,"*",e1,"=",v:X+sign*normx*e1),
print("長度∥X+sign(X_1)*∥X∥*e1∥=sqrt(",v,".",v,")=",normv:ratsimp(sqrt(v.v))),
print("v=",v,"/",normv,"=",v:ratsimp(v/normv)),
print("P_hat=I-2*v.v^T=",ident(n),"-2*",v,".",transpose(v),"=",P_hat:ratsimp(ident(n)-2*v.transpose(v))),
return(P_hat)
)$


針對Hessenberg矩陣Implicit Double Shift副程式
(%i3)
HessenbergImplicitDoubleShift(H):=block
([n,t,d,x,y,z,k,X,P_hat_k],
n:length(H),
print("t=H[",n-1,",",n-1,"]+H[",n,",",n,"]=",H[n-1,n-1],"+",H[n,n],"=",t:H[n-1,n-1]+H[n,n]),
print("d=H[",n-1,",",n-1,"]*H[",n,",",n,"]-H[",n,",",n-1,"]*H[",n-1,",",n,"]=",
         H[n-1,n-1],"*",H[n,n],"-",H[n,n-1],"*",H[n-1,n],"=",d:H[n-1,n-1]*H[n,n]-H[n,n-1]*H[n-1,n]),
print("x=H[1,1]^2-t*H[1,1]+d+H[1,2]*H[2,1]=("
            ,H[1,1],")^2-",t,"*",H[1,1],"+",d,"+",H[1,2],"*",H[2,1],"=",x:H[1,1]^2-t*H[1,1]+d+H[1,2]*H[2,1]),
print("y=H[2,1]*(H[1,1]+H[2,2]-t)=",H[2,1],"*(",H[1,1],"+",H[2,2],"-",t,")=",y:H[2,1]*(H[1,1]+H[2,2]-t)),
print("z=H[2,1]*H[3,2]=",H[2,1],"*",H[3,2],"=",z:H[2,1]*H[3,2]),
print("取向量",matrix(["x"],["y"],["z"]),"=",matrix([x],[y],[z])),
print("------------------------"),
for k:0 thru n-3 do
  (print("k=",k),
   print("取向量",X:matrix([x],[y],[z]),"找一個3*3Householder矩陣P_hat_",k,",使得",P_hat_k:Householder(X),X,"=",P_hat_k.X),
   if k=0 then
      (P_k:diag([P_hat_k,ident(n-k-3)]))
    else if k=n-3 then
      (P_k:diag([ident(n-3),P_hat_k]))
    else
      (P_k:diag([ident(k),P_hat_k,ident(n-k-3)])),
   print("設P_",k,"=",P_k,",計算H_",k,"=P_",k,"^T.H_",k-1,".P_",k,"=",H:ratsimp(transpose(P_k).H.P_k)),
   x:H[k+2,k+1],
   y:H[k+3,k+1],
   if k<n-3 then
     (z:H[k+4,k+1],
      print("取向量",matrix([concat("H",k,"[",k+2,",",k+1,"]")],[concat("H",k,"[",k+3,",",k+1,"]")],[concat("H",k,"[",k+4,",",k+1,"]")]),"=",matrix([x],[y],[z]))
     )
   else
     (print("取向量",matrix([concat("H",k,"[",k+2,",",k+1,"]")],[concat("H",k,"[",k+3,",",k+1,"]")]),"=",matrix([x],[y]))
     ),
   print("------------------------")
  ),
  k:n-2,
  print("取向量",X:matrix([x],[y]),"找一個2*2Householder矩陣P_hat_",k,",使得",P_hat_k:Householder(X),X,"=",P_hat_k.X),
  print("設P_",k,"=",P_k:diag([ident(n-2),P_hat_k]),",計算H",k,"=P_",k,"^T.H_",k-1,".P_",k,"=",H:ratsimp(transpose(P_k).H.P_k))
)$


Hessenberg矩陣,各元數字經過設計,\( \left[ \matrix{x \cr y \cr z} \right] \)向量長度均為完全平方數,處理後的\( Hessenberg \)矩陣各元最多是分數,不會出現根號數字
(%i4)
H:matrix([-1,-1,2/5,-1/4,11/8,31/80],
             [1,-2,2,11/5,-11/5,-21/5],
             [0,-2,1,2,2,-10],
             [0,0,3,-3,0,10],
             [0,0,0,3,-2,-2],
             [0,0,0,0,1,-2]);

(%o4) \( \left[ \matrix{\displaystyle
-1& -1 & \frac{2}{5} & -\frac{1}{4} & \frac{11}{8} & \frac{31}{80} \cr
1 & -2 & 2 & \frac{11}{5} & -\frac{11}{5} & -\frac{21}{5} \cr
0& -2 & 1 & 2 & 2 & -10 \cr
0& 0 & 3 & -3 & 0 & 10 \cr
0& 0 & 0 & 3 & -2 & -2 \cr
0& 0 & 0 & 0 & 1 & -2} \right] \)

執行\(Hessenberg\)矩陣的\(Implicit\) \(Double\) \(Shift\)
(%i5) HessenbergImplicitDoubleShift(H);
\( t=H[5,5]+H[6,6]=-2+-2=-4 \)
\( d=H[5,5]*H[6,6]-H[6,5]*H[5,6]=-2*-2-1*-2=6 \)
\( x=H[1,1]^2-t*H[1,1]+d+H[1,2]*H[2,1]=(-1)^2--4*-1+6+-1*1=2 \)
\( y=H[2,1]*(H[1,1]+H[2,2]-t)=1*(-1+-2--4 )=1 \)
\( z=H[2,1]*H[3,2]=1*-2=-2 \)
取向量\( \left[ \matrix{x \cr y \cr z} \right]=\left[ \matrix{2 \cr 1 \cr -2} \right] \)
------------------------
\( k=0 \)
\( X \)向量\( =\left[ \matrix{2 \cr 1 \cr -2} \right] \)第1元\(=2\),\(sign(X_1)=1\),長度\( \Vert\; X \Vert\;=sqrt(\left[ \matrix{2 \cr 1 \cr -2} \right] \cdot \left[ \matrix{2 \cr 1 \cr -2} \right])=3 \)
第1元是1,其餘為0的向量\( e1=\left[ \matrix{1 \cr 0 \cr 0} \right] \)
\( X+sign(X_1)*\Vert\; X \Vert\;*e1=\left[ \matrix{2 \cr 1 \cr -2} \right]+1*3*\left[ \matrix{1 \cr 0 \cr 0} \right]=\left[ \matrix{5 \cr 1 \cr -2} \right] \)
長度\( \Vert\; X+sign(X_1)*\Vert\; X \Vert\;*e1 \Vert\;=sqrt(\left[ \matrix{5 \cr 1 \cr -2} \right] \cdot \left[ \matrix{5 \cr 1 \cr -2} \right])=\sqrt{30} \)
\( v=\left[ \matrix{5 \cr 1 \cr -2} \right]/ \sqrt{30}=\left[ \matrix{\displaystyle \frac{5}{\sqrt{30}}\cr \frac{1}{\sqrt{30}}\cr -\frac{2}{\sqrt{30}}} \right] \)
\( \hat{P}=I-2*v.v^T=\left[ \matrix{1&0&0 \cr 0&1&0 \cr 0&0&1} \right]-2*\left[ \matrix{\displaystyle \frac{5}{\sqrt{30}} \cr \frac{1}{\sqrt{30}} \cr -\frac{2}{\sqrt{30}}} \right] \cdot \left[ \matrix{\displaystyle \frac{5}{\sqrt{30}}& \frac{1}{\sqrt{30}}& -\frac{2}{\sqrt{30}}} \right]=\left[ \matrix{\displaystyle -\frac{2}{3}&-\frac{1}{3}&\frac{2}{3} \cr
-\frac{1}{3}&\frac{14}{15}&\frac{2}{15} \cr
\frac{2}{3}&\frac{2}{15}&\frac{11}{15}} \right]\)
取向量\( \left[ \matrix{2 \cr 1 \cr -2} \right] \)找一個3*3\( Householder \)矩陣\( \hat{P_0} \),使得\( \left[ \matrix{\displaystyle -\frac{2}{3}&-\frac{1}{3}&\frac{2}{3} \cr
-\frac{1}{3}&\frac{14}{15}&\frac{2}{15} \cr
\frac{2}{3}&\frac{2}{15}&\frac{11}{15}} \right] \left[ \matrix{2 \cr 1 \cr -2} \right]=\left[ \matrix{-3 \cr 0 \cr 0} \right] \)
設\( P_0=\left[ \matrix{\displaystyle -\frac{2}{3}&-\frac{1}{3}&\frac{2}{3}&0&0&0 \cr
-\frac{1}{3}&\frac{14}{15}&\frac{2}{15}&0&0&0 \cr
\frac{2}{3}&\frac{2}{15}&\frac{11}{15}&0&0&0 \cr
0&0&0&1&0&0 \cr
0&0&0&0&1&0 \cr
0&0&0&0&0&1 } \right] \),計算\(H_0=P_0^T.H_{-1}.P_0=\left[ \matrix{\displaystyle
-\frac{2}{5}&-\frac{11}{75}&\frac{2}{75}&\frac{23}{30}&\frac{23}{20}&-\frac{221}{40} \cr
1&-\frac{139}{75}&\frac{148}{75}&\frac{721}{300}&-\frac{449}{200}&-\frac{2153}{400} \cr
2&-\frac{142}{75}&\frac{19}{75}&\frac{239}{150}&\frac{209}{100}&-\frac{1527}{200} \cr
2&\frac{2}{5}&\frac{11}{5}&-3&0&10 \cr
0&0&0&3&-2&-2 \cr
0&0&0&0&1&-2 \cr} \right]\)
取向量\( \left[ \matrix{H0[2,1]\cr H0[3,1]\cr H0[4,1]} \right]=\left[ \matrix{1 \cr 2 \cr 2} \right] \)
------------------------
\(k=1\)
\( X \)向量\( =\left[ \matrix{1 \cr 2 \cr 2} \right] \)第1元\(=1\),\(sign(X_1)=1\),長度\( \Vert\; X \Vert\;=sqrt(\left[ \matrix{1 \cr 2 \cr 2} \right] \cdot \left[ \matrix{1 \cr 2 \cr 2} \right])=3 \)
第1元是1,其餘為0的向量\( e1=\left[ \matrix{1 \cr 0 \cr 0} \right] \)
\( X+sign(X_1)*\Vert\; X \Vert\;*e1=\left[ \matrix{1 \cr 2 \cr 2} \right]+1*3*\left[ \matrix{1 \cr 0 \cr 0} \right]=\left[ \matrix{4 \cr 2 \cr 2} \right] \)
長度\( \Vert\; X+sign(X_1)*\Vert\; X \Vert\;*e1 \Vert\;=sqrt(\left[ \matrix{4 \cr 2 \cr 2} \right] \cdot \left[ \matrix{4 \cr 2 \cr 2} \right])=2\sqrt{6} \)
\( v=\left[ \matrix{4 \cr 2 \cr 2} \right]/ 2\sqrt{6}=\left[ \matrix{\displaystyle \frac{2}{\sqrt{6}}\cr \frac{1}{\sqrt{6}}\cr \frac{1}{\sqrt{6}}} \right] \)
\( \hat{P}=I-2*v.v^T=\left[ \matrix{1&0&0 \cr 0&1&0 \cr 0&0&1} \right]-2*\left[ \matrix{\displaystyle \frac{2}{\sqrt{6}} \cr \frac{1}{\sqrt{6}} \cr \frac{1}{\sqrt{6}}} \right] \cdot \left[ \matrix{\displaystyle \frac{2}{\sqrt{6}}& \frac{1}{\sqrt{6}}& \frac{1}{\sqrt{6}}} \right]=\left[ \matrix{\displaystyle -\frac{1}{3}&-\frac{2}{3}& -\frac{2}{3} \cr
-\frac{2}{3}&\frac{2}{3}&-\frac{1}{3} \cr
-\frac{2}{3}&-\frac{1}{3}&\frac{2}{3}} \right]\)
取向量\( \left[ \matrix{1 \cr 2 \cr 2} \right] \)找一個3*3\( Householder \)矩陣\( \hat{P_1} \),使得\( \left[ \matrix{\displaystyle -\frac{1}{3}&-\frac{2}{3}&-\frac{2}{3} \cr
-\frac{2}{3}&\frac{2}{3}&-\frac{1}{3} \cr
-\frac{2}{3}&-\frac{1}{3}&\frac{2}{3}} \right] \left[ \matrix{1 \cr 2 \cr 2} \right]=\left[ \matrix{-3 \cr 0 \cr 0} \right] \)
設\( P_1=\left[ \matrix{\displaystyle 1&0&0&0&0&0 \cr
0&-\frac{1}{3}&-\frac{2}{3}&-\frac{2}{3}&0&0 \cr
0&-\frac{2}{3}&\frac{2}{3}&-\frac{1}{3}&0&0 \cr
0&-\frac{2}{3}&-\frac{1}{3}&\frac{2}{3}&0&0 \cr
0&0&0&0&1&0 \cr
0&0&0&0&0&1 } \right] \),計算\(H_1=P_1^T.H_0.P_1=\left[ \matrix{\displaystyle
-\frac{2}{5}&-\frac{12}{25}&-\frac{7}{50}&\frac{3}{5}&\frac{23}{20}&-\frac{221}{40} \cr
-3&\frac{9}{10}&-\frac{53}{20}&-\frac{11}{50}&-\frac{129}{200}&\frac{87}{400} \cr
0&1&-\frac{13}{10}&\frac{26}{25}&\frac{289}{100}&-\frac{967}{200} \cr
0&2&0&-\frac{21}{5}&\frac{4}{5}&\frac{64}{5} \cr
0&-2&-1&2&-2&-2 \cr
0&0&0&0&1&-2 \cr} \right]\)
取向量\( \left[ \matrix{H1[3,2]\cr H1[4,2]\cr H1[5,2]} \right]=\left[ \matrix{1 \cr 2 \cr -2} \right] \)
------------------------
\(k=2\)
\( X \)向量\( =\left[ \matrix{1 \cr 2 \cr -2} \right] \)第1元\(=1\),\(sign(X_1)=1\),長度\( \Vert\; X \Vert\;=sqrt(\left[ \matrix{1 \cr 2 \cr -2} \right] \cdot \left[ \matrix{1 \cr 2 \cr -2} \right])=3 \)
第1元是1,其餘為0的向量\( e1=\left[ \matrix{1 \cr 0 \cr 0} \right] \)
\( X+sign(X_1)*\Vert\; X \Vert\;*e1=\left[ \matrix{1 \cr 2 \cr -2} \right]+1*3*\left[ \matrix{1 \cr 0 \cr 0} \right]=\left[ \matrix{4 \cr 2 \cr -2} \right] \)
長度\( \Vert\; X+sign(X_1)*\Vert\; X \Vert\;*e1 \Vert\;=sqrt(\left[ \matrix{4 \cr 2 \cr -2} \right] \cdot \left[ \matrix{4 \cr 2 \cr -2} \right])=2\sqrt{6} \)
\( v=\left[ \matrix{4 \cr 2 \cr -2} \right]/ 2\sqrt{6}=\left[ \matrix{\displaystyle \frac{2}{\sqrt{6}}\cr \frac{1}{\sqrt{6}}\cr -\frac{1}{\sqrt{6}}} \right] \)
\( \hat{P}=I-2*v.v^T=\left[ \matrix{1&0&0 \cr 0&1&0 \cr 0&0&1} \right]-2*\left[ \matrix{\displaystyle \frac{2}{\sqrt{6}} \cr \frac{1}{\sqrt{6}} \cr -\frac{1}{\sqrt{6}}} \right] \cdot \left[ \matrix{\displaystyle \frac{2}{\sqrt{6}}& \frac{1}{\sqrt{6}}& -\frac{1}{\sqrt{6}}} \right]=\left[ \matrix{\displaystyle -\frac{1}{3}&-\frac{2}{3}& \frac{2}{3} \cr
-\frac{2}{3}&\frac{2}{3}&\frac{1}{3} \cr
\frac{2}{3}&\frac{1}{3}&\frac{2}{3}} \right]\)
取向量\( \left[ \matrix{1 \cr 2 \cr -2} \right] \)找一個3*3\( Householder \)矩陣\( \hat{P_2} \),使得\( \left[ \matrix{\displaystyle -\frac{1}{3}&-\frac{2}{3}&\frac{2}{3} \cr
-\frac{2}{3}&\frac{2}{3}&\frac{1}{3} \cr
\frac{2}{3}&\frac{1}{3}&\frac{2}{3}} \right] \left[ \matrix{1 \cr 2 \cr -2} \right]=\left[ \matrix{-3 \cr 0 \cr 0} \right] \)
設\( P_2=\left[ \matrix{\displaystyle 1&0&0&0&0&0 \cr
0&1&0&0&0&0 \cr
0&0&-\frac{1}{3}&-\frac{2}{3}&\frac{2}{3}&0 \cr
0&0&-\frac{2}{3}&\frac{2}{3}&\frac{1}{3}&0 \cr
0&0&\frac{2}{3}&\frac{1}{3}&\frac{2}{3}&0 \cr
0&0&0&0&0&1 } \right] \),計算\(H_2=P_2^T.H_1.P_2=\left[ \matrix{\displaystyle
-\frac{2}{5}&-\frac{12}{25}&\frac{31}{75}&\frac{263}{300}&\frac{131}{150}&-\frac{221}{40} \cr
-3&\frac{9}{10}&\frac{3}{5}&\frac{281}{200}&-\frac{227}{100}&\frac{87}{400} \cr
0&-3&-\frac{13}{3}&\frac{521}{300}&-\frac{39}{50}&-\frac{1651}{200} \cr
0&0&\frac{1}{3}&-\frac{439}{150}&-\frac{49}{25}&\frac{1109}{100} \cr
0&0&\frac{2}{3}&\frac{259}{150}&-\frac{6}{25}&-\frac{29}{100} \cr
0&0&\frac{2}{3}&\frac{1}{3}&\frac{2}{3}&-2 \cr} \right]\)
取向量\( \left[ \matrix{H2[4,3]\cr H2[5,3]\cr H2[6,3]} \right]=\left[ \matrix{\displaystyle \frac{1}{3} \cr \frac{2}{3} \cr \frac{2}{3}} \right] \)
------------------------
\(k=3\)
\( X \)向量\( =\left[ \matrix{\displaystyle \frac{1}{3} \cr \frac{2}{3} \cr \frac{2}{3}} \right] \)第1元\(=\displaystyle \frac{1}{3}\),\(sign(X_1)=1\),長度\( \Vert\; X \Vert\;=sqrt(\left[ \matrix{\displaystyle \frac{1}{3} \cr \frac{2}{3} \cr \frac{2}{3}} \right] \cdot \left[ \matrix{\displaystyle \frac{1}{3} \cr \frac{2}{3} \cr \frac{2}{3}} \right])=1 \)
第1元是1,其餘為0的向量\( e1=\left[ \matrix{1 \cr 0 \cr 0} \right] \)
\( X+sign(X_1)*\Vert\; X \Vert\;*e1=\left[ \matrix{\displaystyle \frac{1}{3} \cr \frac{2}{3} \cr \frac{2}{3}} \right]+1*1*\left[ \matrix{1 \cr 0 \cr 0} \right]=\left[ \matrix{\displaystyle \frac{4}{3} \cr \frac{2}{3} \cr \frac{2}{3}} \right] \)
長度\( \Vert\; X+sign(X_1)*\Vert\; X \Vert\;*e1 \Vert\;=sqrt(\left[ \matrix{\displaystyle \frac{4}{3} \cr \frac{2}{3} \cr \frac{2}{3}} \right] \cdot \left[ \matrix{\displaystyle \frac{4}{3} \cr \frac{2}{3} \cr \frac{2}{3}} \right])=\frac{2^{3/2}}{\sqrt{3}} \)
\( v=\left[ \matrix{\displaystyle \frac{4}{3} \cr \frac{2}{3} \cr \frac{2}{3}} \right]/ \frac{2^{3/2}}{\sqrt{3}}=\left[ \matrix{\displaystyle \frac{\sqrt{2}}{\sqrt{3}}\cr \frac{1}{\sqrt{2}\sqrt{3}}\cr \frac{1}{\sqrt{2}\sqrt{3}}} \right] \)
\( \hat{P}=I-2*v.v^T=\left[ \matrix{1&0&0 \cr 0&1&0 \cr 0&0&1} \right]-2*\left[ \matrix{\displaystyle \frac{\sqrt{2}}{\sqrt{3}} \cr \frac{1}{\sqrt{2}\sqrt{3}} \cr \frac{1}{\sqrt{2}\sqrt{3}}} \right] \cdot \left[ \matrix{\displaystyle \frac{\sqrt{2}}{\sqrt{3}}& \frac{1}{\sqrt{2}\sqrt{3}}& \frac{1}{\sqrt{2}\sqrt{3}}} \right]=\left[ \matrix{\displaystyle -\frac{1}{3}&-\frac{2}{3}& -\frac{2}{3} \cr
-\frac{2}{3}&\frac{2}{3}&-\frac{1}{3} \cr
-\frac{2}{3}&-\frac{1}{3}&\frac{2}{3}} \right]\)
取向量\( \left[ \matrix{\displaystyle \frac{1}{3} \cr \frac{2}{3} \cr \frac{2}{3}} \right] \)找一個3*3\( Householder \)矩陣\( \hat{P_3} \),使得\( \left[ \matrix{\displaystyle -\frac{1}{3}&-\frac{2}{3}&-\frac{2}{3} \cr
-\frac{2}{3}&\frac{2}{3}&-\frac{1}{3} \cr
-\frac{2}{3}&-\frac{1}{3}&\frac{2}{3}} \right] \left[ \matrix{\displaystyle \frac{1}{3} \cr \frac{2}{3} \cr \frac{2}{3}} \right]=\left[ \matrix{-1 \cr 0 \cr 0} \right] \)
設\( P_3=\left[ \matrix{\displaystyle 1&0&0&0&0&0 \cr
0&1&0&0&0&0 \cr
0&0&1&0&0&0 \cr
0&0&0&-\frac{1}{3}&-\frac{2}{3}&-\frac{2}{3} \cr
0&0&0&-\frac{2}{3}&\frac{2}{3}&-\frac{1}{3} \cr
0&0&0&-\frac{2}{3}&-\frac{1}{3}&\frac{2}{3} } \right] \),計算\(H_3=P_3^T.H_2.P_3=\left[ \matrix{\displaystyle
-\frac{2}{5}&-\frac{12}{25}&\frac{31}{75}&\frac{632}{225}&\frac{3311}{1800}&-\frac{4103}{900} \cr
-3&\frac{9}{10}&\frac{3}{5}&\frac{9}{10}&-\frac{1009}{400}&-\frac{7}{200} \cr
0&-3&-\frac{13}{3}&\frac{49}{9}&\frac{1933}{1800}&-\frac{5761}{900} \cr
0&0&-1&\frac{4}{3}&\frac{1111}{900}&-\frac{587}{450} \cr
0&0&0&3&\frac{209}{225}&-\frac{1556}{225} \cr
0&0&0&4&\frac{2729}{900}&-\frac{3343}{450} \cr} \right]\)
取向量\( \left[ \matrix{H3[5,4]\cr H3[6,4]} \right]=\left[ \matrix{\displaystyle 3 \cr 4} \right] \)
------------------------
\( X \)向量\( =\left[ \matrix{3 \cr 4} \right] \)第1元\(=3\),\(sign(X_1)=1\),長度\( \Vert\; X \Vert\;=sqrt(\left[ \matrix{3 \cr 4} \right] \cdot \left[ \matrix{3 \cr 4} \right])=5 \)
第1元是1,其餘為0的向量\( e1=\left[ \matrix{1 \cr 0} \right] \)
\( X+sign(X_1)*\Vert\; X \Vert\;*e1=\left[ \matrix{3 \cr 4} \right]+1*5*\left[ \matrix{1 \cr 0} \right]=\left[ \matrix{8 \cr 4 } \right] \)
長度\( \Vert\; X+sign(X_1)*\Vert\; X \Vert\;*e1 \Vert\;=sqrt(\left[ \matrix{8 \cr 4} \right] \cdot \left[ \matrix{8 \cr 4} \right])=4\sqrt{5} \)
\( v=\left[ \matrix{8 \cr 4} \right]/ 4\sqrt{5}=\left[ \matrix{\displaystyle \frac{2}{\sqrt{5}}\cr \frac{1}{\sqrt{5}}} \right] \)
\( \hat{P}=I-2*v.v^T=\left[ \matrix{1&0 \cr 0&1} \right]-2*\left[ \matrix{\displaystyle \frac{2}{\sqrt{5}} \cr \frac{1}{\sqrt{5}}} \right] \cdot \left[ \matrix{\displaystyle \frac{2}{\sqrt{5}}& \frac{1}{\sqrt{5}}} \right]=\left[ \matrix{\displaystyle -\frac{3}{5}&-\frac{4}{5} \cr
-\frac{4}{5}&\frac{3}{5}} \right]\)
取向量\( \left[ \matrix{3 \cr 4} \right] \)找一個2*2\( Householder \)矩陣\( \hat{P_4} \),使得\( \left[ \matrix{\displaystyle -\frac{3}{5}&-\frac{4}{5} \cr
-\frac{4}{5}&\frac{3}{5}} \right] \left[ \matrix{3 \cr 4} \right]=\left[ \matrix{-5 \cr 0 \cr} \right] \)
設\( P_4=\left[ \matrix{\displaystyle 1&0&0&0&0&0 \cr
0&1&0&0&0&0 \cr
0&0&1&0&0&0 \cr
0&0&0&1&0&0 \cr
0&0&0&0&-\frac{3}{5}&-\frac{4}{5} \cr
0&0&0&0&-\frac{4}{5}&\frac{3}{5} } \right] \),計算\(H_4=P_4^T.H_3.P_4=\left[ \matrix{\displaystyle
-\frac{2}{5}&-\frac{12}{25}&\frac{31}{75}&\frac{632}{225}&\frac{22891}{9000}&-\frac{18931}{4500} \cr
-3&\frac{9}{10}&\frac{3}{5}&\frac{9}{10}&\frac{3083}{2000}&\frac{1997}{1000} \cr
0&-3&-\frac{13}{3}&\frac{49}{9}&\frac{40289}{9000}&-\frac{21149}{4500} \cr
0&0&-1&\frac{4}{3}&\frac{1363}{4500}&-\frac{3983}{2250} \cr
0&0&0&-5&-\frac{35348}{5625}&\frac{47486}{5625} \cr
0&0&0&0&-\frac{33881}{22500}&-\frac{2429}{11250} \cr} \right]\)
(%o5) \( \left[ \matrix{\displaystyle
-\frac{2}{5}&-\frac{12}{25}&\frac{31}{75}&\frac{632}{225}&\frac{22891}{9000}&-\frac{18931}{4500} \cr
-3&\frac{9}{10}&\frac{3}{5}&\frac{9}{10}&\frac{3083}{2000}&\frac{1997}{1000} \cr
0&-3&-\frac{13}{3}&\frac{49}{9}&\frac{40289}{9000}&-\frac{21149}{4500} \cr
0&0&-1&\frac{4}{3}&\frac{1363}{4500}&-\frac{3983}{2250} \cr
0&0&0&-5&-\frac{35348}{5625}&\frac{47486}{5625} \cr
0&0&0&0&-\frac{33881}{22500}&-\frac{2429}{11250} \cr} \right]\)
作者: bugmens    時間: 2018-11-11 12:02

3-5.減少QR法運算-Hessenberg矩陣的Implicit Multishift
前一篇\( Double Shift \)文章取矩陣\(H\)的右下角\(2 \times 2\)子矩陣得到2個特徵值\( \mu_1,\mu_2 \),以\((H-\mu_1I)(H-\mu_2I)\)來計算\(QR\)分解。
而\(Multishift\)則是取矩陣\(H\)的右下角\(k \times k\)子矩陣得到\(k\)個特徵值\(\mu_1,\mu_2,\ldots,\mu_{k}\),以\((H-\mu_1I)(H-\mu_2I)...(H-\mu_{k}I)\)來計算\(QR\)分解。
\(k\)個特徵值中若\(\mu\)為實數則直接計算\((H-\mu I)\),若\(\mu,\overline{\mu}\)互為共軛複數,則計算\((H-\mu I)(H-\overline{\mu}I)=H^2-2Re(\mu)H+|\; \mu |\;^2I\)以避免複數運算。

以\(n=9\)舉例說明
將\(A\)矩陣轉換成\(Hessenberg\)矩陣
\( A=\left[ \matrix{\times & \times & \times & \times & \times & \times & \times & \times & \times \cr
\times & \times & \times & \times & \times & \times & \times & \times & \times \cr
\times & \times & \times & \times & \times & \times & \times & \times & \times \cr
\times & \times & \times & \times & \times & \times & \times & \times & \times \cr
\times & \times & \times & \times & \times & \times & \times & \times & \times \cr
\times & \times & \times & \times & \times & \times & \times & \times & \times \cr
\times & \times & \times & \times & \times & \times & \times & \times & \times \cr
\times & \times & \times & \times & \times & \times & \times & \times & \times \cr
\times & \times & \times & \times & \times & \times & \times & \times & \times} \right] \Rightarrow H=\left[ \matrix{\times & \times & \times & \times & \times & \times & \times & \times & \times \cr
\times & \times & \times & \times & \times & \times & \times & \times & \times \cr
0 & \times & \times & \times & \times & \times & \times & \times & \times \cr
0 & 0 & \times & \times & \times & \times & \times & \times & \times \cr
0 & 0 & 0 & \times & \times & \times & \times & \times & \times \cr
0 & 0 & 0 & 0 & \times & \times & \times & \times & \times \cr
0 & 0 & 0 & 0 & 0 & \times & \times & \times & \times \cr
0 & 0 & 0 & 0 & 0 & 0 & \times & \times & \times \cr
0 & 0 & 0 & 0 & 0 & 0 & 0 & \times & \times} \right] \)

取\(H\)右下角\(4 \times 4\)子矩陣\(T=\left[ \matrix{\times & \times & \times & \times \cr \times & \times & \times & \times \cr 0 & \times & \times & \times \cr 0 & 0 & \times & \times} \right]\)為例
計算\(det(T-\mu I)=0\),得到4個特徵值\(\mu=\mu_1,\mu_2,\mu_3,\mu_4\)
計算\((H-\mu_1I)(H-\mu_2I)(H-\mu_3I)(H-\mu_4I)e1=\left[ \matrix{\times \cr \times \cr \times \cr \times \cr \times \cr 0 \cr 0 \cr 0 \cr 0} \right]\)
其中\(e1\)為第一元為1,其餘為0的向量,換句話說就是取\( (H-\mu_1I)(H-\mu_2I)(H-\mu_3I)(H-\mu_4I) \)相乘後的第1行

取前5個非0元形成向量\( \left[ \matrix{\times \cr \times \cr \times \cr \times \cr \times} \right] \)找一個\( 5\times 5 Householder \)矩陣\( \hat{P_0} \),使得\( \hat{P_0}\left[ \matrix{\times \cr \times \cr \times \cr \times \cr \times} \right]=\left[ \matrix{* \cr 0 \cr 0 \cr 0 \cr 0} \right] \)
設\( P_0=diag(\hat{P_0},I_4) \),計算\( H_0=P_0^T H P_0 =\left[\matrix{
\times & \times & \times & \times & \times & \times & \times & \times & \times \cr
\times & \times & \times & \times & \times & \times & \times & \times & \times \cr
+ & \times & \times & \times & \times & \times & \times & \times & \times \cr
+ & + & \times & \times & \times & \times & \times & \times & \times \cr
+ & + & + & \times & \times & \times & \times & \times & \times \cr
+ & + & + & + & \times & \times & \times & \times & \times \cr
0 & 0 & 0 & 0 & 0 & \times & \times & \times & \times \cr
0 & 0 & 0 & 0 & 0 & 0 & \times & \times & \times \cr
0 & 0 & 0 & 0 & 0 & 0 & 0 & \times & \times} \right] \) 其中\( \matrix{+& & & \cr + & + & & \cr + &+&+&\cr + & + & +&+} \)形成凸起(bulge)

接下來將凸起往右下角推
取向量\( \left[ \matrix{H_0[2,1] \cr H_0[3,1] \cr H_0[4,1] \cr H_0[5,1] \cr H_0[6,1]} \right]=\left[ \matrix{\times \cr + \cr + \cr + \cr +} \right] \)找一個\( 5\times 5 Householder \)矩陣\( \hat{P_1} \),使得\( \hat{P_1}\left[ \matrix{\times \cr + \cr + \cr + \cr +} \right]=\left[ \matrix{* \cr 0 \cr 0 \cr 0 \cr 0} \right] \)
設\( P_1=diag(I_1,\hat{P_1},I_3) \),計算\( H_1=P_1^T H_0 P_1 =\left[\matrix{
\times & \times & \times & \times & \times & \times & \times & \times & \times \cr
\times & \times & \times & \times & \times & \times & \times & \times & \times \cr
0 & \times & \times & \times & \times & \times & \times & \times & \times \cr
0 & + & \times & \times & \times & \times & \times & \times & \times \cr
0 & + & + & \times & \times & \times & \times & \times & \times \cr
0 & + & + & + & \times & \times & \times & \times & \times \cr
0 & + & + & + & + & \times & \times & \times & \times \cr
0 & 0 & 0 & 0 & 0 & 0 & \times & \times & \times \cr
0 & 0 & 0 & 0 & 0 & 0 & 0 & \times & \times} \right] \) 將凸起\( \matrix{+& & & \cr + & + & & \cr + &+&+&\cr + & + & +&+} \)往右下角推

取向量\( \left[ \matrix{H_1[3,2] \cr H_1[4,2] \cr H_1[5,2] \cr H_1[6,2] \cr H_1[7,2]} \right]=\left[ \matrix{\times \cr + \cr + \cr + \cr +} \right] \)找一個\( 5\times 5 Householder \)矩陣\( \hat{P_2} \),使得\( \hat{P_2}\left[ \matrix{\times \cr + \cr + \cr + \cr +} \right]=\left[ \matrix{* \cr 0 \cr 0 \cr 0 \cr 0} \right] \)
設\( P_2=diag(I_2,\hat{P_2},I_2) \),計算\( H_2=P_2^T H_1 P_2 =\left[\matrix{
\times & \times & \times & \times & \times & \times & \times & \times & \times \cr
\times & \times & \times & \times & \times & \times & \times & \times & \times \cr
0 & \times & \times & \times & \times & \times & \times & \times & \times \cr
0 & 0 & \times & \times & \times & \times & \times & \times & \times \cr
0 & 0 & + & \times & \times & \times & \times & \times & \times \cr
0 & 0 & + & + & \times & \times & \times & \times & \times \cr
0 & 0 & + & + & + & \times & \times & \times & \times \cr
0 & 0 & + & + & + & + & \times & \times & \times \cr
0 & 0 & 0 & 0 & 0 & 0 & 0 & \times & \times} \right] \) 將凸起\( \matrix{+& & & \cr + & + & & \cr + &+&+&\cr + & + & +&+} \)往右下角推

取向量\( \left[ \matrix{H_2[4,3] \cr H_2[5,3] \cr H_2[6,3] \cr H_2[7,3] \cr H_2[8,3]} \right]=\left[ \matrix{\times \cr + \cr + \cr + \cr +} \right] \)找一個\( 5\times 5 Householder \)矩陣\( \hat{P_3} \),使得\( \hat{P_3}\left[ \matrix{\times \cr + \cr + \cr + \cr +} \right]=\left[ \matrix{* \cr 0 \cr 0 \cr 0 \cr 0} \right] \)
設\( P_3=diag(I_3,\hat{P_3},I_1) \),計算\( H_3=P_3^T H_2 P_3 =\left[\matrix{
\times & \times & \times & \times & \times & \times & \times & \times & \times \cr
\times & \times & \times & \times & \times & \times & \times & \times & \times \cr
0 & \times & \times & \times & \times & \times & \times & \times & \times \cr
0 & 0 & \times & \times & \times & \times & \times & \times & \times \cr
0 & 0 & 0 & \times & \times & \times & \times & \times & \times \cr
0 & 0 & 0 & + & \times & \times & \times & \times & \times \cr
0 & 0 & 0 & + & + & \times & \times & \times & \times \cr
0 & 0 & 0 & + & + & + & \times & \times & \times \cr
0 & 0 & 0 & + & + & + & + & \times & \times} \right] \) 將凸起\( \matrix{+& & & \cr + & + & & \cr + &+&+&\cr + & + & +&+} \)往右下角推

取向量\( \left[ \matrix{H_3[5,4] \cr H_3[6,4] \cr H_3[7,4] \cr H_3[8,4] \cr H_2[9,4]} \right]=\left[ \matrix{\times \cr + \cr + \cr + \cr +} \right] \)找一個\( 5\times 5 Householder \)矩陣\( \hat{P_4} \),使得\( \hat{P_4}\left[ \matrix{\times \cr + \cr + \cr + \cr +} \right]=\left[ \matrix{* \cr 0 \cr 0 \cr 0 \cr 0} \right] \)
設\( P_4=diag(I_4,\hat{P_4}) \),計算\( H_4=P_4^T H_3 P_4 =\left[\matrix{
\times & \times & \times & \times & \times & \times & \times & \times & \times \cr
\times & \times & \times & \times & \times & \times & \times & \times & \times \cr
0 & \times & \times & \times & \times & \times & \times & \times & \times \cr
0 & 0 & \times & \times & \times & \times & \times & \times & \times \cr
0 & 0 & 0 & \times & \times & \times & \times & \times & \times \cr
0 & 0 & 0 & 0 & \times & \times & \times & \times & \times \cr
0 & 0 & 0 & 0 & + & \times & \times & \times & \times \cr
0 & 0 & 0 & 0 & + & + & \times & \times & \times \cr
0 & 0 & 0 & 0 & + & + & + & \times & \times} \right] \) 將凸起\( \matrix{+& & \cr +&+& \cr +&+&+} \)往右下角推

取向量\( \left[ \matrix{H_4[6,5] \cr H_4[7,5] \cr H_4[8,5] \cr H_4[9,5]} \right]=\left[ \matrix{\times \cr + \cr + \cr +} \right] \)找一個\( 4\times 4 Householder \)矩陣\( \hat{P_5} \),使得\( \hat{P_5}\left[ \matrix{\times \cr + \cr + \cr +} \right]=\left[ \matrix{* \cr 0 \cr 0 \cr 0} \right] \)
設\( P_5=diag(I_5,\hat{P_5}) \),計算\( H_5=P_5^T H_4 P_5 =\left[\matrix{
\times & \times & \times & \times & \times & \times & \times & \times & \times \cr
\times & \times & \times & \times & \times & \times & \times & \times & \times \cr
0 & \times & \times & \times & \times & \times & \times & \times & \times \cr
0 & 0 & \times & \times & \times & \times & \times & \times & \times \cr
0 & 0 & 0 & \times & \times & \times & \times & \times & \times \cr
0 & 0 & 0 & 0 & \times & \times & \times & \times & \times \cr
0 & 0 & 0 & 0 & 0 & \times & \times & \times & \times \cr
0 & 0 & 0 & 0 & 0 & + & \times & \times & \times \cr
0 & 0 & 0 & 0 & 0 & + & + & \times & \times} \right] \) 將凸起\( \matrix{+& \cr +&+} \)往右下角推

取向量\( \left[ \matrix{H_5[7,6] \cr H_5[8,6] \cr H_5[9,6]} \right]=\left[ \matrix{\times \cr + \cr +} \right] \)找一個\( 3\times 3 Householder \)矩陣\( \hat{P_6} \),使得\( \hat{P_6}\left[ \matrix{\times \cr + \cr +} \right]=\left[ \matrix{* \cr 0 \cr 0} \right] \)
設\( P_6=diag(I_6,\hat{P_6}) \),計算\( H_6=P_6^T H_5 P_6 =\left[\matrix{
\times & \times & \times & \times & \times & \times & \times & \times & \times \cr
\times & \times & \times & \times & \times & \times & \times & \times & \times \cr
0 & \times & \times & \times & \times & \times & \times & \times & \times \cr
0 & 0 & \times & \times & \times & \times & \times & \times & \times \cr
0 & 0 & 0 & \times & \times & \times & \times & \times & \times \cr
0 & 0 & 0 & 0 & \times & \times & \times & \times & \times \cr
0 & 0 & 0 & 0 & 0 & \times & \times & \times & \times \cr
0 & 0 & 0 & 0 & 0 & 0 & \times & \times & \times \cr
0 & 0 & 0 & 0 & 0 & 0 & + & \times & \times} \right] \) 將凸起\(+\)往右下角推

取向量\( \left[ \matrix{H_6[8,7] \cr H_6[9,7]} \right]=\left[ \matrix{\times \cr +} \right] \)找一個\( 2\times 2 Householder \)矩陣\( \hat{P_7} \),使得\( \hat{P_7}\left[ \matrix{\times \cr +} \right]=\left[ \matrix{* \cr 0} \right] \)
設\( P_7=diag(I_7,\hat{P_7}) \),計算\( H_7=P_7^T H_6 P_7 =\left[\matrix{
\times & \times & \times & \times & \times & \times & \times & \times & \times \cr
\times & \times & \times & \times & \times & \times & \times & \times & \times \cr
0 & \times & \times & \times & \times & \times & \times & \times & \times \cr
0 & 0 & \times & \times & \times & \times & \times & \times & \times \cr
0 & 0 & 0 & \times & \times & \times & \times & \times & \times \cr
0 & 0 & 0 & 0 & \times & \times & \times & \times & \times \cr
0 & 0 & 0 & 0 & 0 & \times & \times & \times & \times \cr
0 & 0 & 0 & 0 & 0 & 0 & \times & \times & \times \cr
0 & 0 & 0 & 0 & 0 & 0 & 0 & \times & \times} \right] \) 仍是\(Hessenberg\)矩陣

---------------------------
虛擬碼
Householder(X)
{
\( e1 \)為第1元為1,其餘為0的向量
定義\( sign(x_1)=+1 \)若\( x_1 \ge 0 \)
  \( sign(x_1)=-1 \)若\( x_1 <0 \)
\( \displaystyle v=\frac{x+sign(x_1)\Vert\; x \Vert\; e1}{\Vert\; x+sign(x_1)\Vert\; x \Vert\; e1 \Vert\;} \),其中\( \Vert\; * \Vert\; \)為向量長度
Householder矩陣\(H=I-2vv^T \)
\( return(H) \)
}

theFirstColumnofΠk(H,Bulge)
{
\(H\)為\(n \times n\)的\(Hessenberg\)矩陣,取右下角\(Bulge \times Bulge\)子矩陣\(T\)
計算子矩陣\(T\)的特徵值\(\lambda=\mu_1,\mu_2,\ldots,\mu_{Bulge}\)
若特徵值\(\mu\)為實數則計算\((H-\mu I)\)
若特徵值\(\mu,\overline{\mu}\)為共軛複數則計算\((H-\mu I)(H-\overline{\mu}I)=H^2-2Re(\mu)H+|\;\mu|\;^2I\)
\(Column1=(H-\mu_1I)(H-\mu_2I)\ldots(H-\mu_{Bulge}I)e_1\),其中\(e_1=\left[\matrix{1 \cr 0 \cr \vdots \cr 0} \right]\)
換句話說取\( \displaystyle \prod_{i=1}^{Bulge}(H-\mu_i I)\)的第一行向量
取\(Column1\)前\(Bulge+1\)個非零0元得到向量\(X\)
}

HessenbergImplicitMultishift(H,Bulge)
{
計算\( \displaystyle \prod_{i=1}^{Bulge}(H-\mu_i I)\)的第一行向量
theFirstColumnofΠk(H,Bulge)
for k=0 ~ (n-2)
  {\(\hat{P_k}=Householder(X)\),找一個\(Householder\)矩陣\(\hat{P_k}\),使得\(\hat{P_k}X=\left[ \matrix{* \cr 0 \cr \vdots \cr 0} \right]\),*為非零數字
   \(P_k=diag(I_k,\hat{P_k},I_{n-Bulge-1-k})\)
   \(H=P_k^THP_k\)
   if \(k=n-2\) then,已將凸起完全消除
     {\(return(H)\),\(H\)仍是\(Hessenberg\)矩陣
     }
   若\(k=0 \sim (n-Bulge-2)\)
     {\(Xrows=Bulge+1\)}
   若\(k=(n-Bulge-1) \sim(n-3)\)
     {\(Xrows=n-k-1\)}
   取\(X=\left[ \matrix{H[k+1+1,k+1] \cr H[k+1+2,k+1] \cr \vdots \cr H[k+1+Xrows,k+1]} \right]\)做為下一次Householder(X)使用
  }
}
---------------------------
參考資料
Z. Bai and J.W. Demmel(1989). "On a Block Implementation of Hessenberg Multishift QR Iteration," Int. J. High Speed Comput. 1,97-112.
http://www.netlib.org/lapack/lawnspdf/lawn08.pdf


要先載入diag才能使用diag指令
(%i1) load(diag);
(%o1) C:\maxima-5.41.0\share\maxima\5.41.0\share\contrib\diag.mac

轉換成Householder矩陣副程式
(%i2)
Householder(X):=block
([n,e1,normx,v,normv,P_hat],
n:length(X),
if X=zeromatrix(n,1) then
  (print("X為0向量,H=",ident(n)),
   return(ident(n))
  ),
if X[1,1]>=0 then sign:+1 else sign:-1,
print("X向量=",X,"第1元=",X[1,1]," , sign(X_1)=",sign,",長度∥X∥=sqrt(",X,".",X,")=",normx:ratsimp(sqrt(X.X))),
print("第1元是1,其餘為0的向量e1=",e1:genmatrix(lambda([i,j],if i=1 then 1 else 0),n,1)),
print("X+sign(X_1)*∥X∥*e1=",X,"+",sign,"*",normx,"*",e1,"=",v:X+sign*normx*e1),
print("長度∥X+sign(X_1)*∥X∥*e1∥=sqrt(",v,".",v,")=",normv:ratsimp(sqrt(v.v))),
print("v=",v,"/",normv,"=",v:ratsimp(v/normv)),
print(" P_hat=I-2*v.v^T=",ident(n),"-2*",v,".",transpose(v),"=",P_hat:ratsimp(ident(n)-2*v.transpose(v))),
return(P_hat)
)$


計算(H-u1I)(H-u2I)...(H-ukI)第一行
(%i3)
theFirstColumnofΠk(H,Bulge):=block
([n:length(H),T,equation,column1,mu,eigenvalue,realpart,cabs,HH,X],
print("取H矩陣右下角",Bulge,"*",Bulge,"子矩陣T=",
       T:genmatrix(lambda([i,j],H[n-Bulge+i,n-Bulge+j]),Bulge,Bulge)),
print("計算T的特徵值"),
print("det(T-μ*I)=0,",equation:T-%mu*ident(Bulge),"=0"),
print("展開行列式",equation:factor(determinant(equation)),"=0"),
print("特徵值μ=",eigenvalue:map(rhs,solve(equation,%mu))),
print("第1元為1,其餘為0的向量e1=column1=",column1:genmatrix(lambda([i,j],if i=1 then 1 else 0),n,1)),
for i:1 thru Bulge do
    (mu:eigenvalue[ i ],
     if numberp(mu)=true then
        (print("當特徵值為實數μ=",mu),
         print("計算column1=(H-",mu,"I).column1=",column1: (H-mu*ident(n)).column1)
        )
     else
        (realpart:realpart(mu),
         cabs:cabs(mu)^2,
         print("當特徵值為共軛複數μ=",eigenvalue[ i ],",",eigenvalue[i+1],",Re(μ)=",realpart,",|μ|^2=",cabs),
         print("column1=(H^2-2*(",realpart,")H+",cabs,"I).column1"),
         HH:H.H-2*realpart*H+cabs*ident(n),
         print("column1=",HH,".",column1,"=",column1:HH.column1),
         i:i+1
        )
    ),
print("column1=(H-μ1I)(H-μ2I)...(H-μ",Bulge,"I)e1=",column1),
print("擷取第1行前",Bulge+1,"元數字X=",X:genmatrix(lambda([i,j],column1[i,1]),Bulge+1,1)),
return(X)
)$


針對Hessenberg矩陣Implicit Multishift副程式
(%i4)
HessenbergImplicitMultishift(H,X):=block
([n:length(H),Bulge:length(X)-1,P_hat_k,P_k,Xrows:length(X)],
for k:0 thru n-2 do
  (print("k=",k),
   print("取向量",X,"找一個Householder矩陣P_hat_",k,",使得",P_hat_k:Householder(X),X,"=",P_hat_k.X),
   if k=0 then
     (P_k:diag([P_hat_k,ident(n-Bulge-1)]))
   else if k>n-Bulge-2 then
     (P_k:diag([ident(k),P_hat_k]))
   else
     (P_k:diag([ident(k),P_hat_k,ident(n-k-Bulge-1)])),
   print("設P_",k,"=",P_k),
   print("計算H_",k,"=P_",k,"^T.H_",k-1,".P_",k,"=",H:ratsimp(transpose(P_k).H.P_k)),
   if k=n-2 then
     (numer:false,
      return(H)
     ),
   if k+Bulge>n-2 then
     (Xrows:n-k-1),
   print("取向量",genmatrix(lambda([i,j],concat("H",k,"[",i+k+1,",",k+1,"]")),Xrows,1),"=",
                   X:genmatrix(lambda([i,j],H[i+k+1,k+1]),Xrows,1)),
   print("------------------------"),
   if numer=false and ratnump(sqrt(X.X))=false then
     (print("向量X長度=",sqrt(X.X),"不是完全平方數,改用浮點數計算"),
      numer:true,
      ratprint:false
     )
  )
)$


Hessenberg矩陣
(%i5)
H:matrix([-2,1,-1, 1,2,-2/3,-3,0,29/6],
             [-1,-1,1,2,-2,1,2/3,-5/2,-23/6],
             [0,-1,1,2,-1,1,6,-8/3,-65/6],
             [0,0,-1,-2,0,3,-1,7/2,10/3],
             [0,0,0,-1,-1,2,-1,-1,-4],
             [0,0,0,0,-2,1,2,2,-1],
             [0,0,0,0,0,-1,-2,0,1],
             [0,0,0,0,0,0,1,-1,-2],
             [0,0,0,0,0,0,0,1,-1] );

(%o5) \( \left[ \matrix{\displaystyle -2&1&-1&1&2&-\frac{2}{3}&-3&0&\frac{29}{6}\cr
-1&-1&1&2&-2&1&\frac{2}{3}&-\frac{5}{2}&-\frac{23}{6}\cr
0&-1&1&2&-1&1&6&-\frac{8}{3}&-\frac{65}{6}\cr
0&0&-1&-2&0&3&-1&\frac{7}{2}&\frac{10}{3}\cr
0&0&0&-1&-1&2&-1&-1&-4\cr
0&0&0&0&-2&1&2&2&-1\cr
0&0&0&0&0&-1&-2&0&1\cr
0&0&0&0&0&0&1&-1&-2\cr
0&0&0&0&0&0&0&1&-1} \right] \)

取H右下角4*4矩陣計算特徵值,計算(H-u1I)(H-u2I)...(H-u4I)第一行
(%i6) X:theFirstColumnofΠk(H,4);
取\(H\)矩陣右下角\(4*4\)子矩陣\(T=\left[ \matrix{1&2&2&-1 \cr-1&-2&0&1 \cr0&1&-1&-2 \cr0&0&1&-1}\right] \)
計算\(T\)的特徵值
\(det(T-\mu*I)=0,\left[ \matrix{1-\mu&2&2&-1 \cr-1&-\mu-2&0&1 \cr0&1&-\mu-1&-2 \cr0&0&1&-\mu-1}\right] \)=0
展開行列式\((\mu^2+\mu+1)(\mu^2+2\mu+2)=0\)
特徵值\( \displaystyle \mu=[-\%i-1,\%i-1,-\frac{\sqrt{3}\%i+1}{2},\frac{\sqrt{3}\%i-1}{2} \)
第1元為1,其餘為0的向量\(e1=column1=\left[ \matrix{1 \cr0 \cr0 \cr0 \cr0 \cr0 \cr0 \cr0 \cr0}\right] \)
當特徵值為共軛複數\(\mu=-\%i-1,\%i-1,Re(\mu)=-1,|\; \mu |\;^2=2 \)
\(column1=(H^2-2*(-1)H+2I).column1\)
\(column1=\left[ \matrix{ \displaystyle 1&0&-1&-4&-\frac{5}{3}&\frac{28}{3}&-\frac{11}{3}&\frac{31}{6}&-\frac{29}{6} \cr1&-1&1&1&-5&5&\frac{47}{6}&\frac{9}{2}&\frac{11}{3} \cr1&-2&2&1&-2&1&\frac{11}{3}&-\frac{11}{3}&\frac{19}{6} \cr0&1&-1&0&-5&3&\frac{11}{2}&\frac{17}{2}&-\frac{7}{2} \cr0&0&1&1&-3&2&5&-\frac{7}{2}&-\frac{13}{3} \cr0&0&0&2&-4&-1&6&5&4 \cr0&0&0&0&2&-1&0&-1&0 \cr0&0&0&0&0&-1&-1&-1&1 \cr0&0&0&0&0&0&1&0&-1}\right].
\left[ \matrix{1 \cr0 \cr0 \cr0 \cr0 \cr0 \cr0 \cr0 \cr0}\right]=\left[ \matrix{ \displaystyle 1 \cr1 \cr1 \cr0 \cr0 \cr0 \cr0 \cr0 \cr0}\right] \)
當特徵值為共軛複數\( \displaystyle \mu=-\frac{\sqrt{3}\%i+1}{2},\frac{\sqrt{3}\%i-1}{2},Re(\mu)=-\frac{1}{2},|\; \mu |\;^2=1 \)
\(column1=(\displaystyle H^2-2*(-\frac{1}{2})H+1I).column1\)
\(column1=\left[ \matrix{ \displaystyle 2&-1&0&-5&-\frac{11}{3}&10&-\frac{2}{3}&\frac{31}{6}&-\frac{29}{3} \cr2&-1&0&-1&-3&4&\frac{43}{6}&7&\frac{15}{2} \cr1&-1&0&-1&-1&0&-\frac{7}{3}&-1&14 \cr0&1&0&1&-5&0&\frac{13}{2}&5&-\frac{41}{6} \cr0&0&1&2&-3&0&6&-\frac{5}{2}&-\frac{1}{3} \cr0&0&0&2&-2&-3&4&3&5 \cr0&0&0&0&2&0&1&-1&-1 \cr0&0&0&0&0&-1&-2&-1&3 \cr0&0&0&0&0&0&1&-1&-1}\right].
\left[ \matrix{ \displaystyle 1 \cr1 \cr1 \cr0 \cr0 \cr0 \cr0 \cr0 \cr0}\right]=
\left[ \matrix{ \displaystyle 1 \cr1 \cr0 \cr1 \cr1 \cr0 \cr0 \cr0 \cr0}\right] \)
\(column1=(H-\mu 1I)(H-\mu 2I)...(H-\mu 4I)e1=\left[ \matrix{ \displaystyle 1 \cr1 \cr0 \cr1 \cr1 \cr0 \cr0 \cr0 \cr0}\right] \)
擷取第1行前5元數字\(X=\left[ \matrix{ \displaystyle 1 \cr1 \cr0 \cr1 \cr1}\right]\)
(%o6) \(\left[ \matrix{1 \cr1 \cr0 \cr1 \cr1}\right] \)

(%i7) HessenbergImplicitMultishift(H,X);
\(k=0\)
\(X\)向量\(=\left[ \matrix{\displaystyle 1\cr 1\cr 0\cr 1\cr 1}\right]\)第1元\(=1,sign(X_1)=1\),長度\( \Vert\;X \Vert\;=sqrt(\left[ \matrix{\displaystyle 1\cr 1\cr 0\cr 1\cr 1}\right].\left[ \matrix{\displaystyle 1\cr 1\cr 0\cr 1\cr 1}\right] )=2\)
第1元是1,其餘為0的向量\( e1=\left[ \matrix{\displaystyle 1\cr 0\cr 0\cr 0\cr 0}\right] \)
\(X+sign(X_1)*\Vert\; X \Vert\;*e1=\left[ \matrix{\displaystyle 1\cr 1\cr 0\cr 1\cr 1}\right]+1*2*\left[ \matrix{\displaystyle 1\cr 0\cr 0\cr 0\cr 0}\right]=\left[ \matrix{\displaystyle 3\cr 1\cr 0\cr 1\cr 1}\right] \)
長度\(\Vert\; X+sign(X_1)*\Vert\; X \Vert\;*e1 \Vert\;=sqrt(\left[ \matrix{\displaystyle 3\cr 1\cr 0\cr 1\cr 1}\right].\left[ \matrix{\displaystyle 3\cr 1\cr 0\cr 1\cr 1}\right] )=2\sqrt{3} \)
\(v=\left[ \matrix{\displaystyle 3\cr 1\cr 0\cr 1\cr 1}\right]/2\sqrt{3}=\left[\matrix{\displaystyle \frac{\sqrt{3}}{2}\cr \frac{1}{2\sqrt{3}}\cr 0\cr \frac{1}{2\sqrt{3}}\cr \frac{1}{2\sqrt{3}}}\right] \)
\( \hat{P}=I-2*v.v^T=\left[ \matrix{\displaystyle 1&0&0&0&0\cr 0&1&0&0&0\cr 0&0&1&0&0\cr 0&0&0&1&0\cr 0&0&0&0&1}\right]-2*\left[ \matrix{\displaystyle \frac{\sqrt{3}}{2}\cr \frac{1}{2\sqrt{3}}\cr 0\cr \frac{1}{2\sqrt{3}}\cr \frac{1}{2\sqrt{3}}}\right].\left[ \matrix{\displaystyle \frac{\sqrt{3}}{2}&\frac{1}{2\sqrt{3}}&0&\frac{1}{2\sqrt{3}}&\frac{1}{2\sqrt{3}}}\right]=\left[ \matrix{\displaystyle -\frac{1}{2}&-\frac{1}{2}&0&-\frac{1}{2}&-\frac{1}{2}\cr -\frac{1}{2}&\frac{5}{6}&0&-\frac{1}{6}&-\frac{1}{6}\cr 0&0&1&0&0\cr -\frac{1}{2}&-\frac{1}{6}&0&\frac{5}{6}&-\frac{1}{6}\cr -\frac{1}{2}&-\frac{1}{6}&0&-\frac{1}{6}&\frac{5}{6}}\right] \)
取向量\( \left[ \matrix{\displaystyle 1\cr 1\cr 0\cr 1\cr 1}\right] \)找一個\(Householder\)矩陣\( \hat{P}_0\),使得\(\left[ \matrix{\displaystyle -\frac{1}{2}&-\frac{1}{2}&0&-\frac{1}{2}&-\frac{1}{2}\cr -\frac{1}{2}&\frac{5}{6}&0&-\frac{1}{6}&-\frac{1}{6}\cr 0&0&1&0&0\cr -\frac{1}{2}&-\frac{1}{6}&0&\frac{5}{6}&-\frac{1}{6}\cr -\frac{1}{2}&-\frac{1}{6}&0&-\frac{1}{6}&\frac{5}{6}}\right] \left[ \matrix{1\cr 1\cr 0\cr 1\cr 1}\right]=\left[ \matrix{\displaystyle -2\cr 0\cr 0\cr 0\cr 0}\right] \)
設\(P_0=\left[ \matrix{\displaystyle -\frac{1}{2}&-\frac{1}{2}&0&-\frac{1}{2}&-\frac{1}{2}&0&0&0&0\cr -\frac{1}{2}&\frac{5}{6}&0&-\frac{1}{6}&-\frac{1}{6}&0&0&0&0\cr 0&0&1&0&0&0&0&0&0\cr -\frac{1}{2}&-\frac{1}{6}&0&\frac{5}{6}&-\frac{1}{6}&0&0&0&0\cr -\frac{1}{2}&-\frac{1}{6}&0&-\frac{1}{6}&\frac{5}{6}&0&0&0&0\cr 0&0&0&0&0&1&0&0&0\cr 0&0&0&0&0&0&1&0&0\cr 0&0&0&0&0&0&0&1&0\cr 0&0&0&0&0&0&0&0&1}\right] \)
計算\(H_0=P_0^T.H_{-1}.P_0=\left[ \matrix{\displaystyle -1&-\frac{5}{6}&\frac{1}{2}&-\frac{5}{6}&-\frac{1}{3}&-\frac{8}{3}&\frac{13}{6}&0&-\frac{1}{6}\cr
1&-\frac{19}{18}&\frac{3}{2}&\frac{35}{18}&-\frac{20}{9}&\frac{1}{3}&\frac{43}{18}&-\frac{5}{2}&-\frac{11}{2}\cr
0&-1&1&2&-1&1&6&-\frac{8}{3}&-\frac{65}{6}\cr
1&-\frac{7}{18}&-\frac{1}{2}&-\frac{43}{18}&-\frac{5}{9}&\frac{7}{3}&\frac{13}{18}&\frac{7}{2}&\frac{5}{3}\cr
1&-\frac{7}{18}&\frac{1}{2}&-\frac{25}{18}&-\frac{14}{9}&\frac{4}{3}&\frac{13}{18}&-1&-\frac{17}{3}\cr 1&\frac{1}{3}&0&\frac{1}{3}&-\frac{5}{3}&1&2&2&-1\cr 0&0&0&0&0&-1&-2&0&1\cr 0&0&0&0&0&0&1&-1&-2\cr 0&0&0&0&0&0&0&1&-1}\right] \)
取向量\( \left[ \matrix{\displaystyle H0[2,1]\cr H0[3,1]\cr H0[4,1]\cr H0[5,1]\cr H0[6,1]}\right]=\left[ \matrix{\displaystyle 1\cr 0\cr 1\cr 1\cr 1}\right] \)
------------------------
\(k=1\)
\(X\)向量\(=\left[ \matrix{\displaystyle 1\cr 0\cr 1\cr 1\cr 1}\right] \)第1元\(=1,sign(X_1)=1\),長度\( \Vert\;X \Vert\;=sqrt(\left[ \matrix{\displaystyle 1\cr 0\cr 1\cr 1\cr 1}\right].\left[ \matrix{\displaystyle 1\cr 0\cr 1\cr 1\cr 1}\right] )=2\)
第1元是1,其餘為0的向量\( e1=\left[ \matrix{\displaystyle 1\cr 0\cr 0\cr 0\cr 0}\right] \)
\(X+sign(X_1)*\Vert\; X \Vert\;*e1=\left[ \matrix{\displaystyle 1\cr 0\cr 1\cr 1\cr 1}\right]+1*2*\left[ \matrix{\displaystyle 1\cr 0\cr 0\cr 0\cr 0}\right]=\left[ \matrix{\displaystyle 3\cr 0\cr 1\cr 1\cr 1}\right] \)
長度\(\Vert\; X+sign(X_1)*\Vert\; X \Vert\;*e1 \Vert\;=sqrt(\left[ \matrix{\displaystyle 3\cr 0\cr 1\cr 1\cr 1}\right].\left[ \matrix{\displaystyle 3\cr 0\cr 1\cr 1\cr 1}\right] )=2\sqrt{3}\)
\(v=\left[ \matrix{\displaystyle 3\cr 0\cr 1\cr 1\cr 1}\right]/2\sqrt{3}=\left[ \matrix{\displaystyle \frac{\sqrt{3}}{2}\cr 0\cr \frac{1}{2\sqrt{3}}\cr \frac{1}{2\sqrt{3}}\cr \frac{1}{2\sqrt{3}}}\right] \)
\(\hat{P}=I-2*v.v^T=\left[ \matrix{\displaystyle 1&0&0&0&0\cr 0&1&0&0&0\cr 0&0&1&0&0\cr 0&0&0&1&0\cr 0&0&0&0&1}\right]-2*\left[ \matrix{\displaystyle \frac{\sqrt{3}}{2}\cr 0\cr \frac{1}{2\sqrt{3}}\cr \frac{1}{2\sqrt{3}}\cr \frac{1}{2\sqrt{3}}}\right].\left[ \matrix{\displaystyle \frac{\sqrt{3}}{2}&0&\frac{1}{2\sqrt{3}}&\frac{1}{2\sqrt{3}}&\frac{1}{2\sqrt{3}}}\right]=\left[\matrix{\displaystyle-\frac{1}{2}&0&-\frac{1}{2}&-\frac{1}{2}&-\frac{1}{2}\cr 0&1&0&0&0\cr -\frac{1}{2}&0&\frac{5}{6}&-\frac{1}{6}&-\frac{1}{6}\cr -\frac{1}{2}&0&-\frac{1}{6}&\frac{5}{6}&-\frac{1}{6}\cr -\frac{1}{2}&0&-\frac{1}{6}&-\frac{1}{6}&\frac{5}{6}}\right] \)
取向量\( \left[ \matrix{\displaystyle 1\cr 0\cr 1\cr 1\cr 1}\right] \)找一個\(Householder\)矩陣\(\hat{P}_1\),使得\(\left[ \matrix{\displaystyle -\frac{1}{2}&0&-\frac{1}{2}&-\frac{1}{2}&-\frac{1}{2}\cr 0&1&0&0&0\cr -\frac{1}{2}&0&\frac{5}{6}&-\frac{1}{6}&-\frac{1}{6}\cr -\frac{1}{2}&0&-\frac{1}{6}&\frac{5}{6}&-\frac{1}{6}\cr -\frac{1}{2}&0&-\frac{1}{6}&-\frac{1}{6}&\frac{5}{6}}\right] \left[ \matrix{1\cr 0\cr 1\cr 1\cr 1}\right]=\left[ \matrix{\displaystyle -2\cr 0\cr 0\cr 0\cr 0}\right] \)
設\(P_1=\left[ \matrix{\displaystyle 1&0&0&0&0&0&0&0&0\cr 0&-\frac{1}{2}&0&-\frac{1}{2}&-\frac{1}{2}&-\frac{1}{2}&0&0&0\cr 0&0&1&0&0&0&0&0&0\cr 0&-\frac{1}{2}&0&\frac{5}{6}&-\frac{1}{6}&-\frac{1}{6}&0&0&0\cr 0&-\frac{1}{2}&0&-\frac{1}{6}&\frac{5}{6}&-\frac{1}{6}&0&0&0\cr 0&-\frac{1}{2}&0&-\frac{1}{6}&-\frac{1}{6}&\frac{5}{6}&0&0&0\cr 0&0&0&0&0&0&1&0&0\cr 0&0&0&0&0&0&0&1&0\cr 0&0&0&0&0&0&0&0&1}\right] \)
計算\(H_1=P_1^T.H_0.P_1=\left[ \matrix{\displaystyle -1&\frac{7}{3}&\frac{1}{2}&\frac{2}{9}&\frac{13}{18}&-\frac{29}{18}&\frac{13}{6}&0&-\frac{1}{6}\cr -2&-1&-\frac{3}{4}&\frac{1}{6}&\frac{29}{12}&-\frac{37}{12}&-\frac{35}{12}&-1&\frac{21}{4}\cr 0&-\frac{1}{2}&1&\frac{13}{6}&-\frac{5}{6}&\frac{7}{6}&6&-\frac{8}{3}&-\frac{65}{6}\cr 0&0&-\frac{5}{4}&-\frac{463}{162}&\frac{361}{324}&\frac{427}{324}&-\frac{113}{108}&4&\frac{21}{4}\cr 0&\frac{1}{2}&-\frac{1}{4}&-\frac{137}{81}&\frac{91}{324}&\frac{157}{324}&-\frac{113}{108}&-\frac{1}{2}&-\frac{25}{12}\cr 0&-\frac{1}{2}&-\frac{3}{4}&-\frac{44}{81}&-\frac{131}{324}&-\frac{137}{324}&\frac{25}{108}&\frac{5}{2}&\frac{31}{12}\cr 0&\frac{1}{2}&0&\frac{1}{6}&\frac{1}{6}&-\frac{5}{6}&-2&0&1\cr 0&0&0&0&0&0&1&-1&-2\cr 0&0&0&0&0&0&0&1&-1}\right] \)
取向量\( \left[ \matrix{\displaystyle H1[3,2]\cr H1[4,2]\cr H1[5,2]\cr H1[6,2]\cr H1[7,2]}\right]=\left[ \matrix{\displaystyle -\frac{1}{2}\cr 0\cr \frac{1}{2}\cr -\frac{1}{2}\cr \frac{1}{2}}\right] \)
------------------------
\(k=2\)
\(X\)向量\(=\left[ \matrix{\displaystyle -\frac{1}{2}\cr 0\cr \frac{1}{2}\cr -\frac{1}{2}\cr \frac{1}{2}}\right] \)第1元\(\displaystyle =-\frac{1}{2},sign(X_1)=-1\),長度\( \Vert\;X \Vert\;=sqrt(\left[ \matrix{\displaystyle -\frac{1}{2}\cr 0\cr \frac{1}{2}\cr -\frac{1}{2}\cr \frac{1}{2}}\right].\left[ \matrix{\displaystyle -\frac{1}{2}\cr 0\cr \frac{1}{2}\cr -\frac{1}{2}\cr \frac{1}{2}}\right] )=1\)
第1元是1,其餘為0的向量\( e1=\left[ \matrix{\displaystyle 1\cr 0\cr 0\cr 0\cr 0}\right] \)
\(X+sign(X_1)*\Vert\; X \Vert\;*e1=\left[ \matrix{\displaystyle -\frac{1}{2}\cr 0\cr \frac{1}{2}\cr -\frac{1}{2}\cr \frac{1}{2}}\right]+-1*1*\left[ \matrix{\displaystyle 1\cr 0\cr 0\cr 0\cr 0}\right]=\left[ \matrix{\displaystyle -\frac{3}{2}\cr 0\cr \frac{1}{2}\cr -\frac{1}{2}\cr \frac{1}{2}}\right] \)
長度\(\Vert\; X+sign(X_1)*\Vert\; X \Vert\;*e1 \Vert\;=sqrt(\left[ \matrix{\displaystyle -\frac{3}{2}\cr 0\cr \frac{1}{2}\cr -\frac{1}{2}\cr \frac{1}{2}}\right].\left[ \matrix{\displaystyle -\frac{3}{2}\cr 0\cr \frac{1}{2}\cr -\frac{1}{2}\cr \frac{1}{2}}\right] )=\sqrt{3}\)
\(v=\left[ \matrix{\displaystyle -\frac{3}{2}\cr 0\cr \frac{1}{2}\cr -\frac{1}{2}\cr \frac{1}{2}}\right]/\sqrt{3}=\left[ \matrix{\displaystyle -\frac{\sqrt{3}}{2}\cr 0\cr \frac{1}{2\sqrt{3}}\cr -\frac{1}{2\sqrt{3}}\cr \frac{1}{2\sqrt{3}}}\right] \)
\(\hat{P}=I-2*v.v^T=\left[ \matrix{\displaystyle 1&0&0&0&0\cr 0&1&0&0&0\cr 0&0&1&0&0\cr 0&0&0&1&0\cr 0&0&0&0&1}\right]-2*\left[ \matrix{\displaystyle -\frac{\sqrt{3}}{2}\cr 0\cr \frac{1}{2\sqrt{3}}\cr -\frac{1}{2\sqrt{3}}\cr \frac{1}{2\sqrt{3}}}\right].\left[ \matrix{\displaystyle -\frac{\sqrt{3}}{2}&0&\frac{1}{2\sqrt{3}}&-\frac{1}{2\sqrt{3}}&\frac{1}{2\sqrt{3}}}\right]=\left[\matrix{\displaystyle -\frac{1}{2}&0&\frac{1}{2}&-\frac{1}{2}&\frac{1}{2}\cr 0&1&0&0&0\cr \frac{1}{2}&0&\frac{5}{6}&\frac{1}{6}&-\frac{1}{6}\cr -\frac{1}{2}&0&\frac{1}{6}&\frac{5}{6}&\frac{1}{6}\cr \frac{1}{2}&0&-\frac{1}{6}&\frac{1}{6}&\frac{5}{6}}\right] \)
取向量\( \left[ \matrix{\displaystyle -\frac{1}{2}\cr 0\cr \frac{1}{2}\cr -\frac{1}{2}\cr \frac{1}{2}}\right]\)找一個\(Householder\)矩陣\( \hat{P}_2\),使得\(\left[ \matrix{\displaystyle -\frac{1}{2}&0&\frac{1}{2}&-\frac{1}{2}&\frac{1}{2}\cr 0&1&0&0&0\cr \frac{1}{2}&0&\frac{5}{6}&\frac{1}{6}&-\frac{1}{6}\cr -\frac{1}{2}&0&\frac{1}{6}&\frac{5}{6}&\frac{1}{6}\cr \frac{1}{2}&0&-\frac{1}{6}&\frac{1}{6}&\frac{5}{6}}\right] \left[ \matrix{\displaystyle -\frac{1}{2}\cr 0\cr \frac{1}{2}\cr -\frac{1}{2}\cr \frac{1}{2}}\right]=\left[ \matrix{\displaystyle 1\cr 0\cr 0\cr 0\cr 0}\right] \)
設\(P_2=\left[ \matrix{\displaystyle 1&0&0&0&0&0&0&0&0\cr 0&1&0&0&0&0&0&0&0\cr 0&0&-\frac{1}{2}&0&\frac{1}{2}&-\frac{1}{2}&\frac{1}{2}&0&0\cr 0&0&0&1&0&0&0&0&0\cr 0&0&\frac{1}{2}&0&\frac{5}{6}&\frac{1}{6}&-\frac{1}{6}&0&0\cr 0&0&-\frac{1}{2}&0&\frac{1}{6}&\frac{5}{6}&\frac{1}{6}&0&0\cr 0&0&\frac{1}{2}&0&-\frac{1}{6}&\frac{1}{6}&\frac{5}{6}&0&0\cr 0&0&0&0&0&0&0&1&0\cr 0&0&0&0&0&0&0&0&1}\right] \)
計算\(H_2=P_2^T.H_1.P_2=\left[ \matrix{\displaystyle -1&\frac{7}{3}&2&\frac{2}{9}&\frac{2}{9}&-\frac{10}{9}&\frac{5}{3}&0&-\frac{1}{6}\cr -2&-1&\frac{5}{3}&\frac{1}{6}&\frac{29}{18}&-\frac{41}{18}&-\frac{67}{18}&-1&\frac{21}{4}\cr 0&1&-\frac{3}{2}&-\frac{85}{54}&\frac{34}{27}&-\frac{26}{27}&-\frac{38}{9}&-\frac{1}{6}&\frac{43}{12}\cr
0&0&0&-\frac{463}{162}&\frac{113}{162}&\frac{281}{162}&-\frac{79}{54}&4&\frac{21}{4}\cr 0&0&\frac{1}{2}&-\frac{4}{9}&-\frac{7}{18}&\frac{7}{6}&\frac{43}{18}&-\frac{4}{3}&-\frac{62}{9}\cr 0&0&-\frac{1}{2}&-\frac{145}{81}&-\frac{11}{162}&-\frac{125}{162}&-\frac{191}{54}&\frac{10}{3}&\frac{133}{18}\cr 0&0&\frac{1}{2}&\frac{229}{162}&-\frac{34}{81}&-\frac{19}{81}&\frac{41}{27}&-\frac{5}{6}&-\frac{137}{36}\cr 0&0&\frac{1}{2}&0&-\frac{1}{6}&\frac{1}{6}&\frac{5}{6}&-1&-2\cr 0&0&0&0&0&0&0&1&-1}\right] \)
取向量\( \left[ \matrix{\displaystyle H2[4,3]\cr H2[5,3]\cr H2[6,3]\cr H2[7,3]\cr H2[8,3]}\right]=\left[ \matrix{\displaystyle 0\cr \frac{1}{2}\cr -\frac{1}{2}\cr \frac{1}{2}\cr \frac{1}{2}}\right] \)
------------------------
\(k=3\)
\(X\)向量\(=\left[ \matrix{\displaystyle 0\cr \frac{1}{2}\cr -\frac{1}{2}\cr \frac{1}{2}\cr \frac{1}{2}}\right]\)第1元\(=0,sign(X_1)=1\),長度\( \Vert\;X \Vert\;=sqrt(\left[ \matrix{\displaystyle 0\cr \frac{1}{2}\cr -\frac{1}{2}\cr \frac{1}{2}\cr \frac{1}{2}}\right].\left[ \matrix{\displaystyle 0\cr \frac{1}{2}\cr -\frac{1}{2}\cr \frac{1}{2}\cr \frac{1}{2}}\right] )=1 \)
第1元是1,其餘為0的向量\( e1=\left[ \matrix{\displaystyle 1\cr 0\cr 0\cr 0\cr 0}\right] \)
\(X+sign(X_1)*\Vert\; X \Vert\;*e1=\left[ \matrix{\displaystyle 0\cr \frac{1}{2}\cr -\frac{1}{2}\cr \frac{1}{2}\cr \frac{1}{2}}\right]+1*1*\left[ \matrix{\displaystyle 1\cr 0\cr 0\cr 0\cr 0}\right]=\left[ \matrix{\displaystyle 1\cr \frac{1}{2}\cr -\frac{1}{2}\cr \frac{1}{2}\cr \frac{1}{2}}\right] \)
長度\(\Vert\; X+sign(X_1)*\Vert\; X \Vert\;*e1 \Vert\;=sqrt(\left[ \matrix{\displaystyle 1\cr \frac{1}{2}\cr -\frac{1}{2}\cr \frac{1}{2}\cr \frac{1}{2}}\right].\left[ \matrix{\displaystyle 1\cr \frac{1}{2}\cr -\frac{1}{2}\cr \frac{1}{2}\cr \frac{1}{2}}\right] )=\sqrt{2} \)
\(v=\left[ \matrix{\displaystyle 1\cr \frac{1}{2}\cr -\frac{1}{2}\cr \frac{1}{2}\cr \frac{1}{2}}\right]/\sqrt{2}=\left[ \matrix{\displaystyle \frac{1}{\sqrt{2}}\cr \frac{1}{2^{3/2}}\cr -\frac{1}{2^{3/2}}\cr \frac{1}{2^{3/2}}\cr \frac{1}{2^{3/2}}}\right] \)
\(\hat{P}=I-2*v.v^T=\left[ \matrix{\displaystyle 1&0&0&0&0\cr 0&1&0&0&0\cr 0&0&1&0&0\cr 0&0&0&1&0\cr 0&0&0&0&1}\right]-2*\left[ \matrix{\displaystyle \frac{1}{\sqrt{2}}\cr \frac{1}{2^{3/2}}\cr -\frac{1}{2^{3/2}}\cr \frac{1}{2^{3/2}}\cr \frac{1}{2^{3/2}}}\right].\left[ \matrix{\displaystyle \frac{1}{\sqrt{2}}&\frac{1}{2^{3/2}}&-\frac{1}{2^{3/2}}&\frac{1}{2^{3/2}}&\frac{1}{2^{3/2}}}\right]=\left[\matrix{\displaystyle 0&-\frac{1}{2}&\frac{1}{2}&-\frac{1}{2}&-\frac{1}{2}\cr -\frac{1}{2}&\frac{3}{4}&\frac{1}{4}&-\frac{1}{4}&-\frac{1}{4}\cr \frac{1}{2}&\frac{1}{4}&\frac{3}{4}&\frac{1}{4}&\frac{1}{4}\cr -\frac{1}{2}&-\frac{1}{4}&\frac{1}{4}&\frac{3}{4}&-\frac{1}{4}\cr -\frac{1}{2}&-\frac{1}{4}&\frac{1}{4}&-\frac{1}{4}&\frac{3}{4}}\right] \)
取向量\( \left[ \matrix{\displaystyle 0\cr \frac{1}{2}\cr -\frac{1}{2}\cr \frac{1}{2}\cr \frac{1}{2}}\right]\)找一個\(Householder\)矩陣\( \hat{P}_3\),使得\(\left[ \matrix{\displaystyle 0&-\frac{1}{2}&\frac{1}{2}&-\frac{1}{2}&-\frac{1}{2}\cr -\frac{1}{2}&\frac{3}{4}&\frac{1}{4}&-\frac{1}{4}&-\frac{1}{4}\cr \frac{1}{2}&\frac{1}{4}&\frac{3}{4}&\frac{1}{4}&\frac{1}{4}\cr -\frac{1}{2}&-\frac{1}{4}&\frac{1}{4}&\frac{3}{4}&-\frac{1}{4}\cr -\frac{1}{2}&-\frac{1}{4}&\frac{1}{4}&-\frac{1}{4}&\frac{3}{4}}\right] \left[ \matrix{\displaystyle 0\cr \frac{1}{2}\cr -\frac{1}{2}\cr \frac{1}{2}\cr \frac{1}{2}}\right]=\left[ \matrix{\displaystyle -1\cr 0\cr 0\cr 0\cr 0}\right] \)
設\(P_3=\left[ \matrix{\displaystyle 1&0&0&0&0&0&0&0&0\cr 0&1&0&0&0&0&0&0&0\cr 0&0&1&0&0&0&0&0&0\cr 0&0&0&0&-\frac{1}{2}&\frac{1}{2}&-\frac{1}{2}&-\frac{1}{2}&0\cr 0&0&0&-\frac{1}{2}&\frac{3}{4}&\frac{1}{4}&-\frac{1}{4}&-\frac{1}{4}&0\cr 0&0&0&\frac{1}{2}&\frac{1}{4}&\frac{3}{4}&\frac{1}{4}&\frac{1}{4}&0\cr 0&0&0&-\frac{1}{2}&-\frac{1}{4}&\frac{1}{4}&\frac{3}{4}&-\frac{1}{4}&0\cr 0&0&0&-\frac{1}{2}&-\frac{1}{4}&\frac{1}{4}&-\frac{1}{4}&\frac{3}{4}&0\cr 0&0&0&0&0&0&0&0&1}\right] \)
計算\(H_3=P_3^T.H_2.P_3=\left[ \matrix{\displaystyle -1&\frac{7}{3}&2&-\frac{3}{2}&-\frac{23}{36}&-\frac{1}{4}&\frac{29}{36}&-\frac{31}{36}&-\frac{1}{6}\cr -2&-1&\frac{5}{3}&\frac{5}{12}&\frac{125}{72}&-\frac{173}{72}&-\frac{259}{72}&-\frac{7}{8}&\frac{21}{4}\cr 0&1&-\frac{3}{2}&\frac{13}{12}&\frac{559}{216}&-\frac{55}{24}&-\frac{625}{216}&\frac{251}{216}&\frac{43}{12}\cr 0&0&-1&-\frac{1}{4}&\frac{55}{54}&-\frac{3}{2}&-\frac{193}{54}&\frac{103}{27}&\frac{241}{24}\cr0&0&0&\frac{1}{2}&-\frac{529}{1296}&-\frac{13}{48}&\frac{1495}{1296}&-\frac{2081}{1296}&-\frac{647}{144}\cr0&0&0&-\frac{1}{2}&\frac{1385}{1296}&-\frac{65}{144}&-\frac{1535}{1296}&\frac{6121}{1296}&\frac{719}{144}\cr 0&0&0&0&-\frac{233}{144}&-\frac{71}{144}&-\frac{43}{48}&-\frac{329}{144}&-\frac{203}{144}\cr 0&0&0&\frac{1}{2}&-\frac{529}{1296}&-\frac{151}{144}&-\frac{809}{1296}&-\frac{1937}{1296}&\frac{19}{48}\cr 0&0&0&-\frac{1}{2}&-\frac{1}{4}&\frac{1}{4}&-\frac{1}{4}&\frac{3}{4}&-1}\right] \)
取向量\( \left[ \matrix{\displaystyle H3[5,4]\cr H3[6,4]\cr H3[7,4]\cr H3[8,4]\cr H3[9,4]}\right]=\left[ \matrix{\displaystyle \frac{1}{2}\cr -\frac{1}{2}\cr 0\cr \frac{1}{2}\cr -\frac{1}{2}}\right] \)
------------------------
\(k=4\)
\(X\)向量\(=\left[ \matrix{\displaystyle \frac{1}{2}\cr -\frac{1}{2}\cr 0\cr \frac{1}{2}\cr -\frac{1}{2}}\right]\)第1元\(\displaystyle=\frac{1}{2},sign(X_1)=1\),長度\( \Vert\;X \Vert\;=sqrt(\left[ \matrix{\displaystyle \frac{1}{2}\cr -\frac{1}{2}\cr 0\cr \frac{1}{2}\cr -\frac{1}{2}}\right].\left[ \matrix{\displaystyle \frac{1}{2}\cr -\frac{1}{2}\cr 0\cr \frac{1}{2}\cr -\frac{1}{2}}\right] )=1\)
第1元是1,其餘為0的向量\( e1=\left[ \matrix{\displaystyle 1\cr 0\cr 0\cr 0\cr 0}\right] \)
\(X+sign(X_1)*\Vert\; X \Vert\;*e1=\left[ \matrix{\displaystyle \frac{1}{2}\cr -\frac{1}{2}\cr 0\cr \frac{1}{2}\cr -\frac{1}{2}}\right]+1*1*\left[ \matrix{\displaystyle 1\cr 0\cr 0\cr 0\cr 0}\right]=\left[ \matrix{\displaystyle \frac{3}{2}\cr -\frac{1}{2}\cr 0\cr \frac{1}{2}\cr -\frac{1}{2}}\right] \)
長度\(\Vert\; X+sign(X_1)*\Vert\; X \Vert\;*e1 \Vert\;=sqrt(\left[ \matrix{\displaystyle \frac{3}{2}\cr -\frac{1}{2}\cr 0\cr \frac{1}{2}\cr -\frac{1}{2}}\right].\left[ \matrix{\displaystyle \frac{3}{2}\cr -\frac{1}{2}\cr 0\cr \frac{1}{2}\cr -\frac{1}{2}}\right] )=\sqrt{3}\)
\(v=\left[ \matrix{\displaystyle \frac{3}{2}\cr -\frac{1}{2}\cr 0\cr \frac{1}{2}\cr -\frac{1}{2}}\right]/\sqrt{3}=\left[ \matrix{\displaystyle \frac{\sqrt{3}}{2}\cr -\frac{1}{2\sqrt{3}}\cr 0\cr \frac{1}{2\sqrt{3}}\cr -\frac{1}{2\sqrt{3}}}\right] \)
\(\hat{P}=I-2*v.v^T=\left[ \matrix{\displaystyle 1&0&0&0&0\cr 0&1&0&0&0\cr 0&0&1&0&0\cr 0&0&0&1&0\cr 0&0&0&0&1}\right]-2*\left[ \matrix{\displaystyle \frac{\sqrt{3}}{2}\cr -\frac{1}{2\sqrt{3}}\cr 0\cr \frac{1}{2\sqrt{3}}\cr -\frac{1}{2\sqrt{3}}}\right].\left[ \matrix{\displaystyle \frac{\sqrt{3}}{2}&-\frac{1}{2\sqrt{3}}&0&\frac{1}{2\sqrt{3}}&-\frac{1}{2\sqrt{3}}}\right]=\left[ \matrix{\displaystyle -\frac{1}{2}&\frac{1}{2}&0&-\frac{1}{2}&\frac{1}{2}\cr \frac{1}{2}&\frac{5}{6}&0&\frac{1}{6}&-\frac{1}{6}\cr 0&0&1&0&0\cr -\frac{1}{2}&\frac{1}{6}&0&\frac{5}{6}&\frac{1}{6}\cr \frac{1}{2}&-\frac{1}{6}&0&\frac{1}{6}&\frac{5}{6}}\right] \)
取向量\( \left[ \matrix{\displaystyle \frac{1}{2}\cr -\frac{1}{2}\cr 0\cr \frac{1}{2}\cr -\frac{1}{2}}\right] \)找一個\(Householder\)矩陣\( \hat{P}_4\),使得\(\left[ \matrix{\displaystyle -\frac{1}{2}&\frac{1}{2}&0&-\frac{1}{2}&\frac{1}{2}\cr \frac{1}{2}&\frac{5}{6}&0&\frac{1}{6}&-\frac{1}{6}\cr 0&0&1&0&0\cr -\frac{1}{2}&\frac{1}{6}&0&\frac{5}{6}&\frac{1}{6}\cr \frac{1}{2}&-\frac{1}{6}&0&\frac{1}{6}&\frac{5}{6}}\right] \left[ \matrix{\displaystyle \frac{1}{2}\cr -\frac{1}{2}\cr 0\cr \frac{1}{2}\cr -\frac{1}{2}}\right]=\left[ \matrix{\displaystyle -1\cr 0\cr 0\cr 0\cr 0}\right] \)
設\(P_4=\left[ \matrix{\displaystyle 1&0&0&0&0&0&0&0&0\cr 0&1&0&0&0&0&0&0&0\cr 0&0&1&0&0&0&0&0&0\cr 0&0&0&1&0&0&0&0&0\cr 0&0&0&0&-\frac{1}{2}&\frac{1}{2}&0&-\frac{1}{2}&\frac{1}{2}\cr 0&0&0&0&\frac{1}{2}&\frac{5}{6}&0&\frac{1}{6}&-\frac{1}{6}\cr 0&0&0&0&0&0&1&0&0\cr 0&0&0&0&-\frac{1}{2}&\frac{1}{6}&0&\frac{5}{6}&\frac{1}{6}\cr 0&0&0&0&\frac{1}{2}&-\frac{1}{6}&0&\frac{1}{6}&\frac{5}{6}}\right] \)
計算\(H_4=P_4^T.H_3.P_4=\left[ \matrix{\displaystyle -1&\frac{7}{3}&2&-\frac{3}{2}&\frac{13}{24}&-\frac{139}{216}&\frac{29}{36}&-\frac{101}{216}&-\frac{121}{216}\cr-2&-1&\frac{5}{3}&\frac{5}{12}&\frac{143}{144}&-\frac{931}{432}&-\frac{259}{72}&-\frac{485}{432}&\frac{2375}{432}\cr 0&1&-\frac{3}{2}&\frac{13}{12}&-\frac{59}{48}&-\frac{1321}{1296}&-\frac{625}{216}&-\frac{143}{1296}&\frac{6293}{1296}\cr 0&0&-1&-\frac{1}{4}&\frac{89}{48}&-\frac{2305}{1296}&-\frac{193}{54}&\frac{5305}{1296}&\frac{12653}{1296}\cr 0&0&0&-1&-\frac{1}{4}&\frac{3557}{3888}&-\frac{2545}{2592}&\frac{15283}{3888}&\frac{17111}{3888}\cr 0&0&0&0&-1&-\frac{679}{3888}&-\frac{1225}{2592}&\frac{8575}{3888}&\frac{10499}{3888}\cr 0&0&0&0&1&-\frac{295}{216}&-\frac{43}{48}&-\frac{305}{216}&-\frac{493}{216}\cr 0&0&0&0&1&-\frac{4297}{3888}&-\frac{3463}{2592}&\frac{3121}{3888}&\frac{11309}{3888}\cr 0&0&0&0&-1&\frac{113}{1296}&\frac{133}{288}&-\frac{1721}{1296}&-\frac{4837}{1296}}\right] \)
取向量\( \left[ \matrix{\displaystyle H4[6,5]\cr H4[7,5]\cr H4[8,5]\cr H4[9,5]}\right]=\left[ \matrix{\displaystyle -1\cr 1\cr 1\cr -1}\right] \)
------------------------
\(k=5\)
\(X\)向量\(=\left[ \matrix{-1\cr 1\cr 1\cr -1}\right] \)第1元\(=-1,sign(X_1)=-1\),長度\( \Vert\;X \Vert\;=sqrt(\left[ \matrix{-1\cr 1\cr 1\cr -1}\right].\left[ \matrix{-1\cr 1\cr 1\cr -1}\right] )=2\)
第1元是1,其餘為0的向量\( e1=\left[ \matrix{1\cr 0\cr 0\cr 0}\right] \)
\(X+sign(X_1)*\Vert\; X \Vert\;*e1=\left[ \matrix{-1\cr 1\cr 1\cr -1}\right]+-1*2*\left[ \matrix{\displaystyle 1\cr 0\cr 0\cr 0}\right]=\left[ \matrix{-3\cr 1\cr 1\cr -1}\right] \)
長度\(\Vert\; X+sign(X_1)*\Vert\; X \Vert\;*e1 \Vert\;=sqrt(\left[ \matrix{-3\cr 1\cr 1\cr -1}\right].\left[ \matrix{-3\cr 1\cr 1\cr -1}\right] )=2\sqrt{3}\)
\(v=\left[ \matrix{-3\cr 1\cr 1\cr -1}\right]/2\sqrt{3}=\left[ \matrix{\displaystyle -\frac{\sqrt{3}}{2}\cr \frac{1}{2\sqrt{3}}\cr \frac{1}{2\sqrt{3}}\cr -\frac{1}{2\sqrt{3}}}\right] \)
\(\hat{P}=I-2*v.v^T=\left[ \matrix{\displaystyle 1&0&0&0\cr 0&1&0&0\cr 0&0&1&0\cr 0&0&0&1}\right]-2*\left[ \matrix{\displaystyle -\frac{\sqrt{3}}{2}\cr \frac{1}{2\sqrt{3}}\cr \frac{1}{2\sqrt{3}}\cr -\frac{1}{2\sqrt{3}}}\right].\left[ \matrix{\displaystyle -\frac{\sqrt{3}}{2}&\frac{1}{2\sqrt{3}}&\frac{1}{2\sqrt{3}}&-\frac{1}{2\sqrt{3}}}\right]=\left[ \matrix{\displaystyle -\frac{1}{2}&\frac{1}{2}&\frac{1}{2}&-\frac{1}{2}\cr \frac{1}{2}&\frac{5}{6}&-\frac{1}{6}&\frac{1}{6}\cr \frac{1}{2}&-\frac{1}{6}&\frac{5}{6}&\frac{1}{6}\cr -\frac{1}{2}&\frac{1}{6}&\frac{1}{6}&\frac{5}{6}}\right] \)
取向量\( \left[ \matrix{-1\cr 1\cr 1\cr -1}\right] \)找一個\(Householder\)矩陣\( \hat{P}_5\),使得\(\left[ \matrix{\displaystyle -\frac{1}{2}&\frac{1}{2}&\frac{1}{2}&-\frac{1}{2}\cr \frac{1}{2}&\frac{5}{6}&-\frac{1}{6}&\frac{1}{6}\cr \frac{1}{2}&-\frac{1}{6}&\frac{5}{6}&\frac{1}{6}\cr -\frac{1}{2}&\frac{1}{6}&\frac{1}{6}&\frac{5}{6}}\right] \left[ \matrix{-1\cr 1\cr 1\cr -1}\right]=\left[ \matrix{\displaystyle 2\cr 0\cr 0\cr 0}\right] \)
設\(P_5=\left[ \matrix{\displaystyle 1&0&0&0&0&0&0&0&0\cr 0&1&0&0&0&0&0&0&0\cr 0&0&1&0&0&0&0&0&0\cr 0&0&0&1&0&0&0&0&0\cr 0&0&0&0&1&0&0&0&0\cr 0&0&0&0&0&-\frac{1}{2}&\frac{1}{2}&\frac{1}{2}&-\frac{1}{2}\cr 0&0&0&0&0&\frac{1}{2}&\frac{5}{6}&-\frac{1}{6}&\frac{1}{6}\cr 0&0&0&0&0&\frac{1}{2}&-\frac{1}{6}&\frac{5}{6}&\frac{1}{6}\cr 0&0&0&0&0&-\frac{1}{2}&\frac{1}{6}&\frac{1}{6}&\frac{5}{6}}\right] \)
計算\(H_5=P_5^T.H_4.P_5=\left[ \matrix{\displaystyle -1&\frac{7}{3}&2&-\frac{3}{2}&\frac{13}{24}&\frac{37}{48}&\frac{433}{1296}&-\frac{1217}{1296}&-\frac{115}{1296}\cr -2&-1&\frac{5}{3}&\frac{5}{12}&\frac{143}{144}&-\frac{129}{32}&-\frac{7703}{2592}&-\frac{1289}{2592}&\frac{12629}{2592}\cr 0&1&-\frac{3}{2}&\frac{13}{12}&-\frac{59}{48}&-\frac{985}{288}&-\frac{16277}{7776}&\frac{5365}{7776}&\frac{31535}{7776}\cr 0&0&-1&-\frac{1}{4}&\frac{89}{48}&-\frac{1075}{288}&-\frac{22727}{7776}&\frac{36895}{7776}&\frac{70853}{7776}\cr 0&0&0&-1&-\frac{1}{4}&-\frac{2045}{1728}&-\frac{13177}{46656}&\frac{216029}{46656}&\frac{172699}{46656}\cr 0&0&0&0&2&-\frac{859}{1152}&-\frac{39167}{31104}&-\frac{27749}{31104}&\frac{30413}{31104}\cr 0&0&0&0&0&\frac{8155}{10368}&-\frac{360449}{279936}&-\frac{289307}{279936}&-\frac{295117}{279936}\cr 0&0&0&0&0&-\frac{10907}{10368}&-\frac{287807}{279936}&\frac{526555}{279936}&\frac{962189}{279936}\cr 0&0&0&0&0&\frac{13199}{10368}&-\frac{72541}{279936}&-\frac{789199}{279936}&-\frac{1077113}{279936}}\right] \)
取向量\( \left[ \matrix{\displaystyle H5[7,6]\cr H5[8,6]\cr H5[9,6]}\right]=\left[ \matrix{\displaystyle \frac{8155}{10368}\cr -\frac{10907}{10368}\cr \frac{13199}{10368}}\right] \)
------------------------
向量\(X\)長度\(\displaystyle=\frac{5\sqrt{1598579}}{3456}\)不是完全平方數,改用浮點數計算
\(k=6\)
\(X\)向量\(=\left[ \matrix{\displaystyle \frac{8155}{10368}\cr -\frac{10907}{10368}\cr \frac{13199}{10368}}\right]\)第1元\(=0.7865547839506173,sign(X_1)=1\),長度\( \Vert\;X \Vert\;=sqrt(\left[ \matrix{\displaystyle \frac{8155}{10368}\cr -\frac{10907}{10368}\cr \frac{13199}{10368}}\right].\left[ \matrix{\displaystyle \frac{8155}{10368}\cr -\frac{10907}{10368}\cr \frac{13199}{10368}}\right] )=1.829208969513448\)
第1元是1,其餘為0的向量\( e1=\left[ \matrix{1\cr 0\cr 0}\right] \)
\(X+sign(X_1)*\Vert\; X \Vert\;*e1=\left[ \matrix{\displaystyle \frac{8155}{10368}\cr -\frac{10907}{10368}\cr \frac{13199}{10368}}\right]+1*1.829208969513448*\left[ \matrix{\displaystyle 1\cr 0\cr 0}\right]=\left[ \matrix{\displaystyle 2.615763753464065\cr -1.05198688271605\cr 1.273051697530864}\right] \)
長度\(\Vert\; X+sign(X_1)*\Vert\; X \Vert\;*e1 \Vert\;=sqrt(\left[ \matrix{2.615763753464065\cr -1.05198688271605\cr 1.273051697530864}\right].\left[ \matrix{2.615763753464065\cr -1.05198688271605\cr 1.273051697530864}\right] )=3.093470064495413 \)
\(v=\left[ \matrix{2.615763753464065\cr -1.05198688271605\cr 1.273051697530864}\right]/3.093470064495413=\left[ \matrix{0.8455759063214779\cr -0.3400669347959709\cr 0.4115286946339066}\right] \)
\(\hat{P}=I-2*v.v^T=\left[ \matrix{1&0&0\cr 0&1&0\cr 0&0&1}\right]-2*\left[ \matrix{0.8455759063214779\cr -0.3400669347959709\cr 0.4115286946339066}\right].\left[ \matrix{ 0.8455759063214779&-0.3400669347959709&0.4115286946339066}\right]=\)
\(\left[ \matrix{ -0.4299972267027781&0.5751048132001405&-0.6959574978847209\cr 0.5751048132001405&0.7687089597169466&0.2798946035294795\cr -0.6959574978847209&0.2798946035294795&0.661288266985825}\right] \)
取向量\( \left[ \matrix{\displaystyle \frac{8155}{10368}\cr -\frac{10907}{10368}\cr \frac{13199}{10368}}\right] \)找一個\(Householder\)矩陣\( \hat{P}_6\),使得\(\left[ \matrix{ -0.4299972267027781&0.5751048132001405&-0.6959574978847209\cr 0.5751048132001405&0.7687089597169466&0.2798946035294795\cr -0.6959574978847209&0.2798946035294795&0.661288266985825}\right] \left[ \matrix{\displaystyle \frac{8155}{10368}\cr -\frac{10907}{10368}\cr \frac{13199}{10368}}\right]=\left[ \matrix{-1.829208969513457\cr 7.771561172376096*10^{-16}\cr -2.886579864025407*10^{-15}}\right] \)
設\(P_6=\left[ \matrix{1&0&0&0&0&0&0&0&0\cr 0&1&0&0&0&0&0&0&0\cr 0&0&1&0&0&0&0&0&0\cr 0&0&0&1&0&0&0&0&0\cr 0&0&0&0&1&0&0&0&0\cr 0&0&0&0&0&1&0&0&0\cr 0&0&0&0&0&0&-0.4299972267027781&0.5751048132001405&-0.6959574978847209\cr 0&0&0&0&0&0&0.5751048132001405&0.7687089597169466&0.2798946035294795\cr 0&0&0&0&0&0&-0.6959574978847209&0.2798946035294795&0.661288266985825}\right] \)
計算\(H_6=P_6^T.H_5.P_6=\left[ \matrix{ -1&2.333333333333333&2&-1.5&0.5416666666666666&0.7708333333333334&-0.6219569788349768&-0.5545418975815991&-0.5540350924250236\cr -2&-1&1.666666666666667&0.4166666666666667&0.9930555555555556&-4.03125&-2.39903113723365&-0.7276656161967713&5.151074840285687\cr 0&1&-1.5&1.083333333333333&-1.229166666666667&-3.420138888888889&-1.525529515680916&0.4616279381076218&4.331720709928399\cr 0&0&-1&-0.25&1.854166666666667&-3.732638888888889&-2.355920465963594&4.516780134134694&9.387602241820074\cr 0&0&0&-1&-0.25&-1.183449074074074&0.2082096026209324&4.4329301669687&3.940325053062458\cr 0&0&0&0&2&-0.7456597222222222&-0.6521038280089702&-1.136304030499402&1.273261703644264\cr 0&0&0&0&0&-1.829208969513453&-1.610172351137944&4.19317878751438&4.254779928150215\cr 0&0&0&0&0&7.7715611723761*10^{-16}&0.0579988841413669&-0.605664190520036&1.767500658619938\cr 0&0&0&0&0&-2.886579864025409*10^{-15}&0.04700389329356565&-0.4617357301648267&-1.038503736119814}\right] \)
取向量\( \left[ \matrix{H6[8,7]\cr H6[9,7]}\right]=\left[ \matrix{\displaystyle 0.0579988841413669\cr 0.04700389329356565}\right] \)
------------------------
\(k=7\)
\(X\)向量\(=\left[ \matrix{\displaystyle 0.0579988841413669\cr 0.04700389329356565}\right]\)第1元\(=0.0579988841413669,sign(X_1)=1\),長度\( \Vert\;X \Vert\;=sqrt(\left[ \matrix{0.0579988841413669\cr 0.04700389329356565}\right].\left[ \matrix{0.0579988841413669\cr 0.04700389329356565}\right] )=0.07465411272258612 \)
第1元是1,其餘為0的向量\( e1=\left[ \matrix{1\cr 0}\right] \)
\(X+sign(X_1)*\Vert\; X \Vert\;*e1=\left[ \matrix{0.0579988841413669\cr 0.04700389329356565}\right]+1*0.07465411272258612*\left[ \matrix{1\cr 0}\right]=\left[ \matrix{0.132652996863953\cr 0.04700389329356565}\right] \)
長度\(\Vert\; X+sign(X_1)*\Vert\; X \Vert\;*e1 \Vert\;=sqrt(\left[ \matrix{0.132652996863953\cr 0.04700389329356565}\right].\left[ \matrix{0.132652996863953\cr 0.04700389329356565}\right] )=0.1407344434093547 \)
\(v=\left[ \matrix{\displaystyle 0.132652996863953\cr 0.04700389329356565}\right]/0.1407344434093547=\left[ \matrix{0.9425766262357311\cr 0.3339899754095393}\right] \)
\(\hat{P}=I-2*v.v^T=\left[ \matrix{\displaystyle 1&0\cr 0&1}\right]-2*\left[ \matrix{0.9425766262357311\cr 0.3339899754095393}\right].\left[ \matrix{0.9425766262357311&0.3339899754095393}\right]=\left[\matrix{-0.7769013926518663&-0.6296222884361566\cr -0.6296222884361566&0.7769013926518692}\right] \)
取向量\( \left[ \matrix{0.0579988841413669\cr 0.04700389329356565}\right]\)找一個\(Householder\)矩陣\( \hat{P}_7\),使得\(\left[ \matrix{-0.7769013926518663&-0.6296222884361566\cr -0.6296222884361566&0.7769013926518692}\right] \left[ \matrix{0.0579988841413669\cr 0.04700389329356565}\right]=\left[ \matrix{-0.0746541127225859\cr 6.245004513516506*10^{-17}}\right] \)
設\(P_7=\left[ \matrix{1&0&0&0&0&0&0&0&0\cr 0&1&0&0&0&0&0&0&0\cr 0&0&1&0&0&0&0&0&0\cr 0&0&0&1&0&0&0&0&0\cr 0&0&0&0&1&0&0&0&0\cr 0&0&0&0&0&1&0&0&0\cr 0&0&0&0&0&0&1&0&0\cr 0&0&0&0&0&0&0&-0.7769013926518663&-0.6296222884361566\cr 0&0&0&0&0&0&0&-0.6296222884361566&0.7769013926518692}\right] \)
計算\(H_7=P_7^T.H_6.P_7=\left[ \matrix{-1&2.333333333333333&2&-1.5&0.5416666666666666&0.7708333333333334&-0.6219569788349768&0.7796572152815339&-0.08127869629395262\cr -2&-1&1.666666666666667&0.4166666666666667&0.9930555555555556&-4.03125&-2.39903113723365&-2.677907098238435&4.460031707558076\cr 0&1&-1.5&1.083333333333333&-1.229166666666667&-3.420138888888889&-1.525529515680916&-3.085987294254227&3.074668613324933\cr 0&0&-1&-0.25&1.854166666666667&-3.732638888888889&-2.355920465963594&-9.419736382934685&4.449375810914967\cr 0&0&0&-1&-0.25&-1.183449074074074&0.2082096026209324&-5.924866097337959&0.2701723850207669\cr 0&0&0&0&2&-0.7456597222222222&-0.6521038280089702&0.08112223614429317&1.704641134813782\cr 0&0&0&0&0&-1.829208969513453&-1.610172351137944&-5.936590714812356&0.6654256275903683\cr 0&0&0&0&0&1.213681349951592*10^{-15}&-0.07465411272258583&-0.1385315695498772&-1.038138425003079\cr 0&0&0&0&0&-2.731902729369484*10^{-15}&6.245004513516506*10^{-17}&1.191097963781674&-1.505636357089966}\right] \)
(%o7) \( \left[\matrix{-1&2.333333333333333&2&-1.5&0.5416666666666666&0.7708333333333334&-0.6219569788349768&0.7796572152815339&-0.08127869629395262\cr -2&-1&1.666666666666667&0.4166666666666667&0.9930555555555556&-4.03125&-2.39903113723365&-2.677907098238435&4.460031707558076\cr 0&1&-1.5&1.083333333333333&-1.229166666666667&-3.420138888888889&-1.525529515680916&-3.085987294254227&3.074668613324933\cr 0&0&-1&-0.25&1.854166666666667&-3.732638888888889&-2.355920465963594&-9.419736382934685&4.449375810914967\cr 0&0&0&-1&-0.25&-1.183449074074074&0.2082096026209324&-5.924866097337959&0.2701723850207669\cr 0&0&0&0&2&-0.7456597222222222&-0.6521038280089702&0.08112223614429317&1.704641134813782\cr 0&0&0&0&0&-1.829208969513453&-1.610172351137944&-5.936590714812356&0.6654256275903683\cr 0&0&0&0&0&1.213681349951592*10^{-15}&-0.07465411272258583&-0.1385315695498772&-1.038138425003079\cr 0&0&0&0&0&-2.731902729369484*10^{-15}&6.245004513516506*10^{-17}&1.191097963781674&-1.505636357089966}\right]\)
作者: bugmens    時間: 2019-8-3 04:45

上一篇文章找\(H\)右下角\(4 \times 4\)子矩陣\(T=\left[ \matrix{\times & \times & \times & \times \cr \times & \times & \times & \times \cr 0 & \times & \times & \times \cr 0 & 0 & \times & \times} \right]\)
計算\(det(T-\mu I)=0\),得到4個特徵值\(\mu=\mu_1,\mu_2,\mu_3,\mu_4\)
計算\((H-\mu_1 I)(H-\mu_2 I)(H-\mu_3 I)(H-\mu_4 I)e1=\left[\matrix{\times \cr \times \cr \times \cr \times \cr \times \cr 0 \cr 0 \cr 0 \cr 0} \right]\)
其中\(e1\)為第一元為1,其餘為0的向量,換句話說就是取\((H−\mu_1I)(H−\mu_2I)(H−\mu_3I)(H−\mu_4I)\)相乘後的第1行

但實際上不需要計算子矩陣\(T\)的特徵值,可利用遞迴式求得偏移向量。
論文遞迴式符號解釋
\(w_1=e_1\),\(z_1=h_1-t_{11}e_1\),
\(w_i=t_{ii-1}^{-1}z_{i-1}\)
\(\displaystyle z_i=Hw_i-\sum_{j=1}^i t_{ji}w_j\)  \(i=2,\ldots,m.\)
\(e_1\)為第一元為1其餘為0的向量
\(h_1\)為\(H\)矩陣的第一行
\(t_{ii-1}\)為子矩陣\(T\)第\(i\)列,第\(i-1\)行數字
\(m\)為子矩陣\(T\)的大小
最後得到的\(z_m\)就是\((H-\mu_1 I)(H-\mu_2 I)(H-\mu_3 I)(H-\mu_4 I)\)相乘後第一行,論文中稱為\(shift\) \(vector\)。

參考資料
A. Dubrulle, G. Golub, A multishift QR iteration without computation of the shifts, Numer. Algorithms 7 (1994) 173-181.
https://www.hpl.hp.com/techreports/93/HPL-93-54.pdf



使用遞迴式得到(H-u1I)(H-u2I)...(H-ukI)第一行
(%i1)
theFirstColumnofΠk(H,Bulge):=block
([n:length(H),T,e1,W,Z,X],
powerdisp : true,/*改變變數顯示順序*/
print("取H矩陣右下角",Bulge,"*",Bulge,"子矩陣T=",
       T:genmatrix(lambda([i,j],H[n-Bulge+i,n-Bulge+j]),Bulge,Bulge)),
e1:genmatrix(lambda([i,j],if i=1 then 1 else 0),n,1),
W:create_list(0,i,1,Bulge),
Z:create_list(0,i,1,Bulge),
print("第1元為1,其餘為0的向量",w[1],"=",e[1],"=",W[1]:e1),
print(z[1],"=",h[1],"-",t[1,1],e[1],"=",col(H,1),"-",T[1,1],e1,"=",Z[1]:col(H,1)-T[1,1]*e1),
for i:2 thru Bulge do
    (print(w[ i ],"=1/",t[i,i-1],z[i-1],"=1/",T[i,i-1],Z[i-1],"=",W[ i ]:1/T[i,i-1]*Z[i-1]),
     print(z[ i ],"=H",w[ i ],"-(",apply("+",create_list(t[j,i]*w[j],j,1,i)),")"),
     printList:[z[ i ],"=",H.W[ i ],"-("],/*先將要印的東西放在printList*/
     for j:1 thru i do
        (if T[j,i]>=0 then
            (printList:append(printList,["+",T[j,i],W[j]]))/*T[j,i]是正的,加上+號*/
         else
            (printList:append(printList,[T[j,i],W[j]]))/*T[j,i]是負的,原本就有-號,不需要再加*/
        ),
     apply(print,append(printList,[")=",Z[ i ]:H.W[ i ]-sum(T[j,i]*W[j],j,1,i)]))/*再用apply(print,)將全部內容印在同一行*/
    ),
print("擷取",z[Bulge],"前",Bulge+1,"元數字X=",X:genmatrix(lambda([i,j],Z[Bulge][i,j]),Bulge+1,1)),
return(X)
)$


Hessenberg矩陣
(%i2)
H:matrix([-2,1,-1, 1,2,-2/3,-3,0,29/6],
             [-1,-1,1,2,-2,1,2/3,-5/2,-23/6],
             [0,-1,1,2,-1,1,6,-8/3,-65/6],
             [0,0,-1,-2,0,3,-1,7/2,10/3],
             [0,0,0,-1,-1,2,-1,-1,-4],
             [0,0,0,0,-2,1,2,2,-1],
             [0,0,0,0,0,-1,-2,0,1],
             [0,0,0,0,0,0,1,-1,-2],
             [0,0,0,0,0,0,0,1,-1] );

(H) \( \left[ \matrix{\displaystyle -2&1&-1&1&2&-\frac{2}{3}&-3&0&\frac{29}{6}\cr
-1&-1&1&2&-2&1&\frac{2}{3}&-\frac{5}{2}&-\frac{23}{6}\cr
0&-1&1&2&-1&1&6&-\frac{8}{3}&-\frac{65}{6}\cr
0&0&-1&-2&0&3&-1&\frac{7}{2}&\frac{10}{3}\cr
0&0&0&-1&-1&2&-1&-1&-4\cr
0&0&0&0&-2&1&2&2&-1\cr
0&0&0&0&0&-1&-2&0&1\cr
0&0&0&0&0&0&1&-1&-2\cr
0&0&0&0&0&0&0&1&-1} \right] \)

取H右下角4*4子矩陣,使用遞迴式得到(H-u1I)(H-u2I)...(H-u4I)第一行
(%i3) X:theFirstColumnofΠk(H,4);
取\(H\)矩陣右下角\(4*4\)子矩陣\(T=\left[\matrix{1&2&2&-1\cr -1&-2&0&1\cr 0&1&-1&-2\cr 0&0&1&-1}\right]\)
第1元為1,其餘為0的向量\(w_1=e_1=\left[\matrix{1\cr 0\cr 0\cr 0\cr 0\cr 0\cr 0\cr 0\cr 0}\right]\)
\(z_1=h_1-t_{1,1}e_1=\left[\matrix{-2\cr -1\cr 0\cr 0\cr 0\cr 0\cr 0\cr 0\cr 0}\right]-1 \left[\matrix{1\cr 0\cr 0\cr 0\cr 0\cr 0\cr 0\cr 0\cr 0}\right]=\left[\matrix{-3\cr -1\cr 0\cr 0\cr 0\cr 0\cr 0\cr 0\cr 0}\right]\)
\(w_2=1/t_{2,1}z_1=1/-1\left[\matrix{-3\cr -1\cr 0\cr 0\cr 0\cr 0\cr 0\cr 0\cr 0}\right]=\left[\matrix{3\cr 1\cr 0\cr 0\cr 0\cr 0\cr 0\cr 0\cr 0}\right]\)
\(z_2=Hw_2-(w_1t_{1,2}+w_2t_{2,2})\)
\(z_2=\left[\matrix{-5\cr -4\cr -1\cr 0\cr 0\cr 0\cr 0\cr 0\cr 0}\right]-(+2\left[\matrix{1\cr 0\cr 0\cr 0\cr 0\cr 0\cr 0\cr 0\cr 0}\right]-2\left[\matrix{3\cr 1\cr 0\cr 0\cr 0\cr 0\cr 0\cr 0\cr 0}\right])=\left[\matrix{-1\cr -2\cr -1\cr 0\cr 0\cr 0\cr 0\cr 0\cr 0}\right]\)
\(w_3=1/t_{3,2}z_2=1/1 \left[\matrix{-1\cr -2\cr -1\cr 0\cr 0\cr 0\cr 0\cr 0\cr 0}\right]=\left[\matrix{-1\cr -2\cr -1\cr 0\cr 0\cr 0\cr 0\cr 0\cr 0}\right]\)
\(z_3=Hw_3-(w_1 t_{1,3}+w_2 t_{2,3}+w_3 t_{3,3})\)
\(z_3=\left[\matrix{1\cr 2\cr 1\cr 1\cr 0\cr 0\cr 0\cr 0\cr 0}\right]-(+2\left[\matrix{1\cr 0\cr 0\cr 0\cr 0\cr 0\cr 0\cr 0\cr 0}\right]+0\left[\matrix{3\cr 1\cr 0\cr 0\cr 0\cr 0\cr 0\cr 0\cr 0}\right]-1\left[\matrix{-1\cr -2\cr -1\cr 0\cr 0\cr 0\cr 0\cr 0\cr 0}\right])=\left[\matrix{-2\cr 0\cr 0\cr 1\cr 0\cr 0\cr 0\cr 0\cr 0}\right]\)
\(w_4=1/t_{4,3}z_3=1/1 \left[\matrix{-2\cr 0\cr 0\cr 1\cr 0\cr 0\cr 0\cr 0\cr 0}\right]=\left[\matrix{-2\cr 0\cr 0\cr 1\cr 0\cr 0\cr 0\cr 0\cr 0}\right]\)
\(z_4=Hw_4-(w_1 t_{1,4}+w_2 t_{2,4}+w_3 t_{3,4}+w_4 t_{4,4})\)
\(z_4=\left[\matrix{5\cr 4\cr 2\cr -2\cr -1\cr 0\cr 0\cr 0\cr 0}\right]-(-1\left[\matrix{1\cr 0\cr 0\cr 0\cr 0\cr 0\cr 0\cr 0\cr 0}\right]+1\left[\matrix{3\cr 1\cr 0\cr 0\cr 0\cr 0\cr 0\cr 0\cr 0}\right]-2\left[\matrix{-1\cr -2\cr -1\cr 0\cr 0\cr 0\cr 0\cr 0\cr 0}\right]-1\left[\matrix{-2\cr 0\cr 0\cr 1\cr 0\cr 0\cr 0\cr 0\cr 0}\right])=\left[\matrix{-1\cr -1\cr 0\cr -1\cr -1\cr 0\cr 0\cr 0\cr 0}\right]\)
擷取\(z_4\)前5元數字\(X=\left[\matrix{-1\cr -1\cr 0\cr -1\cr -1}\right]\)
(X) \(\left[\matrix{-1\cr -1\cr 0\cr -1\cr -1}\right]\)




歡迎光臨 Math Pro 數學補給站 (https://math.pro/db/) 論壇程式使用 Discuz! 6.1.0