# # Numerical Mathematics and Computing, Fifth Edition # Ward Cheney & David Kincaid # Brooks/Cole Publ. Co. # (c) 2003 # # file: penta # # Pentadiagonal Solver # To test, take size n, either even or odd, and # create a random pentadiagonal system. # # Contents of vector a,b,c,d,e,f will change so # Original data kept in aa,bb,cc,dd,ee,ff # with(linalg): # Select size of pentadiagonal matrix n:=8; randvector(n):a:=map(evalf,%):aa:=evalm(a): randvector(n):b:=map(evalf,%):bb:=evalm(b): randvector(n):c:=map(evalf,%):cc:=evalm(c): randvector(n):d:=map(evalf,%):dd:=evalm(d): randvector(n):e:=map(evalf,%):ee:=evalm(e): randvector(n):f:=map(evalf,%):ff:=evalm(f): # Fix original augmented matrix as M M:=matrix(n,n+1,0): for i from 1 to n do for j from 1 to n+1 do if j=i-2 then M[i,j]:=ee[j] end if; if j=i-1 then M[i,j]:=aa[j] end if; if j=i then M[i,j]:=dd[i] end if; if j=i+1 then M[i,j]:=cc[i] end if; if j=i+2 then M[i,j]:=ff[i] end if; end do: M[i,n+1]:=bb[i]: end do: map(evalf,M); # This is the procedure that solves the system. # # Forward solve for i from 2 to floor(n/2) do # ood = one over determinant; then use adjoint matrix ood:=1/(d[2*i-3]*d[2*i-2]-c[2*i-3]*a[2*i-3]): Xml[1,1]:=ood*(e[2*i-3]*d[2*i-2]-a[2*i-2]*a[2*i-3]): Xml[1,2]:=ood*(-e[2*i-3]*c[2*i-3]+a[2*i-2]*d[2*i-3]): Xml[2,1]:=ood*(-e[2*i-2]*a[2*i-3]): Xml[2,2]:=ood*(e[2*i-2]*d[2*i-3]): # Temporary 2 x 2 storage matrix, since still working with elements in block t1[1,1]:=d[2*i-1]-Xml[1,1]*f[2*i-3]-Xml[1,2]*c[2*i-2]: t1[1,2]:=c[2*i-1]-Xml[1,2]*f[2*i-2]: t1[2,1]:=a[2*i-1]-Xml[2,1]*f[2*i-3]-Xml[2,2]*c[2*i-2]: t1[2,2]:=d[2*i]-Xml[2,2]*f[2*i-2]: if t1[1,1]*t1[2,2]-t1[1,2]*t1[2,1]=0 then ERROR(`Singular matrix.`) end if: # Done working, store results d[2*i-1]:=t1[1,1]: c[2*i-1]:=t1[1,2]: a[2*i-1]:=t1[2,1]: d[2*i]:=t1[2,2]: # Temporary 2 x 1 storage matrix t2[1,1]:=b[2*i-1]-Xml[1,1]*b[2*i-3]-Xml[1,2]*b[2*i-2]: t2[2,1]:=b[2*i]-Xml[2,1]*b[2*i-3]-Xml[2,2]*b[2*i-2]: # Done working, record b[2*i-1]:=t2[1,1]: b[2*i]:=t2[2,1]: end do: if n/2