概要
本サンプルはFortran言語によりLAPACKルーチンDGELSYを利用するサンプルプログラムです。
以下の線形最小二乗問題を解きます。最小ノルム解を

入力データ
(本ルーチンの詳細はDGELSY のマニュアルページを参照)| このデータをダウンロード |
DGELSY Example Program Data 6 5 :Values of M and N -0.09 0.14 -0.46 0.68 1.29 -1.56 0.20 0.29 1.09 0.51 -1.48 -0.43 0.89 -0.71 -0.96 -1.09 0.84 0.77 2.11 -1.27 0.08 0.55 -1.13 0.14 1.74 -1.59 -0.72 1.06 1.24 0.34 :End of matrix A 7.4 4.2 -8.3 1.8 8.6 2.1 :End of vector b
出力結果
(本ルーチンの詳細はDGELSY のマニュアルページを参照)| この出力例をダウンロード |
DGELSY Example Program Results
Least squares solution
0.6344 0.9699 -1.4402 3.3678 3.3992
Tolerance used to estimate the rank of A
1.00E-02
Estimated rank of A
4
ソースコード
(本ルーチンの詳細はDGELSY のマニュアルページを参照)※本サンプルソースコードのご利用手順は「サンプルのコンパイル及び実行方法」をご参照下さい。
| このソースコードをダウンロード |
Program dgelsy_example
! DGELSY Example Program Text
! Copyright 2017, Numerical Algorithms Group Ltd. http://www.nag.com
! .. Use Statements ..
Use lapack_interfaces, Only: dgelsy
Use lapack_precision, Only: dp
! .. Implicit None Statement ..
Implicit None
! .. Parameters ..
Integer, Parameter :: nb = 64, nin = 5, nout = 6
! .. Local Scalars ..
Real (Kind=dp) :: rcond
Integer :: i, info, lda, lwork, m, n, rank
! .. Local Arrays ..
Real (Kind=dp), Allocatable :: a(:, :), b(:), work(:)
Integer, Allocatable :: jpvt(:)
! .. Executable Statements ..
Write (nout, *) 'DGELSY Example Program Results'
Write (nout, *)
! Skip heading in data file
Read (nin, *)
Read (nin, *) m, n
lda = m
lwork = 3*n + nb*(n+1)
Allocate (a(lda,n), b(m), work(lwork), jpvt(n))
! Read A and B from data file
Read (nin, *)(a(i,1:n), i=1, m)
Read (nin, *) b(1:m)
! Initialize JPVT to be zero so that all columns are free
jpvt(1:n) = 0
! Choose RCOND to reflect the relative accuracy of the input data
rcond = 0.01_dp
! Solve the least squares problem min( norm2(b - Ax) ) for the x
! of minimum norm.
Call dgelsy(m, n, 1, a, lda, b, m, jpvt, rcond, rank, work, lwork, info)
! Print solution
Write (nout, *) 'Least squares solution'
Write (nout, 100) b(1:n)
! Print the effective rank of A
Write (nout, *)
Write (nout, *) 'Tolerance used to estimate the rank of A'
Write (nout, 110) rcond
Write (nout, *) 'Estimated rank of A'
Write (nout, 120) rank
100 Format (1X, 7F11.4)
110 Format (3X, 1P, E11.2)
120 Format (1X, I6)
End Program
