Keyword: 実行列, 誤差限界, 改良
概要
本サンプルは実連立一次方程式の誤差限界をもつ解の改良を行うFortranによるサンプルプログラムです。 本サンプルは行列Aと行列Bが以下で示される場合の連立一次方程式AX=Bについて反復改良を用いて解を改良し、算出された解と前進/後退誤差限界を出力します。
※本サンプルはnAG Fortranライブラリに含まれるルーチン f07ahf() のExampleコードです。本サンプル及びルーチンの詳細情報は f07ahf のマニュアルページをご参照ください。
ご相談やお問い合わせはこちらまで
入力データ
(本ルーチンの詳細はf07ahf のマニュアルページを参照)| このデータをダウンロード |
F07AHF Example Program Data 4 2 :Values of N and NRHS 1.80 2.88 2.05 -0.89 5.25 -2.95 -0.95 -3.80 1.58 -2.69 -2.90 -1.04 -1.11 -0.66 -0.59 0.80 :End of matrix A 9.52 18.47 24.35 2.25 0.77 -13.28 -6.22 -6.21 :End of matrix B
- 1行目はタイトル行で読み飛ばされます。
- 2行目に行列Aの次数(n)と右辺の数(nrhs)を指定しています。
- 3〜6行目に行列Aの要素を指定しています。
- 7〜10行目に行列Bの要素を指定しています。
出力結果
(本ルーチンの詳細はf07ahf のマニュアルページを参照)| この出力例をダウンロード |
F07AHF Example Program Results
Solution(s)
1 2
1 1.0000 3.0000
2 -1.0000 2.0000
3 3.0000 4.0000
4 -5.0000 1.0000
Backward errors (machine-dependent)
4.7E-17 3.7E-17
Estimated forward error bounds (machine-dependent)
2.3E-14 3.3E-14
- 5〜8行目にxの解が出力されています。
- 11行目に後退誤差限界が出力されています。
- 13行目に推定された前進誤差限界が出力されています。
ソースコード
(本ルーチンの詳細はf07ahf のマニュアルページを参照)
※本サンプルソースコードは科学技術・統計計算ライブラリである「nAG Fortranライブラリ」のルーチンを呼び出します。
サンプルのコンパイル及び実行方法
| このソースコードをダウンロード |
PROGRAM f07ahfe
! F07AHF Example Program Text
! Mark 23 Release. nAG Copyright 2011.
! .. Use Statements ..
USE nag_library, ONLY : dgerfs, dgetrf, dgetrs, nag_wp, x04caf
! .. Implicit None Statement ..
IMPLICIT NONE
! .. Parameters ..
INTEGER, PARAMETER :: nin = 5, nout = 6
CHARACTER (1), PARAMETER :: trans = 'N'
! .. Local Scalars ..
INTEGER :: i, ifail, info, lda, ldaf, ldb, ldx, &
n, nrhs
! .. Local Arrays ..
REAL (KIND=nag_wp), ALLOCATABLE :: a(:,:), af(:,:), b(:,:), berr(:), &
ferr(:), work(:), x(:,:)
INTEGER, ALLOCATABLE :: ipiv(:), iwork(:)
! .. Executable Statements ..
WRITE (nout,*) 'F07AHF Example Program Results'
! Skip heading in data file
READ (nin,*)
READ (nin,*) n, nrhs
lda = n
ldaf = n
ldb = n
ldx = n
ALLOCATE (a(lda,n),af(ldaf,n),b(ldb,nrhs),berr(nrhs),ferr(nrhs), &
work(3*n),x(ldx,n),ipiv(n),iwork(n))
! Read A and B from data file, and copy A to AF and B to X
READ (nin,*) (a(i,1:n),i=1,n)
READ (nin,*) (b(i,1:nrhs),i=1,n)
af(1:n,1:n) = a(1:n,1:n)
x(1:n,1:nrhs) = b(1:n,1:nrhs)
! Factorize A in the array AF
! The nAG name equivalent of dgetrf is f07adf
CALL dgetrf(n,n,af,ldaf,ipiv,info)
WRITE (nout,*)
FLUSH (nout)
IF (info==0) THEN
! Compute solution in the array X
! The nAG name equivalent of dgetrs is f07aef
CALL dgetrs(trans,n,nrhs,af,ldaf,ipiv,x,ldx,info)
! Improve solution, and compute backward errors and
! estimated bounds on the forward errors
! The nAG name equivalent of dgerfs is f07ahf
CALL dgerfs(trans,n,nrhs,a,lda,af,ldaf,ipiv,b,ldb,x,ldx,ferr,berr, &
work,iwork,info)
! Print solution
! ifail: behaviour on error exit
! =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
ifail = 0
CALL x04caf('General',' ',n,nrhs,x,ldx,'Solution(s)',ifail)
WRITE (nout,*)
WRITE (nout,*) 'Backward errors (machine-dependent)'
WRITE (nout,99999) berr(1:nrhs)
WRITE (nout,*) 'Estimated forward error bounds (machine-dependent)'
WRITE (nout,99999) ferr(1:nrhs)
ELSE
WRITE (nout,*) 'The factor U is singular'
END IF
99999 FORMAT ((3X,1P,7E11.1))
END PROGRAM f07ahfe
