実線形最小二乗: QR 分解もしく LQ 分解

LAPACKサンプルソースコード : 使用ルーチン名:DGELS

概要

本サンプルはFortran言語によりLAPACKルーチンDGELSを利用するサンプルプログラムです。

以下の線形最小二乗問題を解きます。

\begin{displaymath}\min_{x} \left\Vert b - A x \right\Vert _{2}\end{displaymath}

ここで

\begin{displaymath}A = \left(\begin{array}{rrrr}-0.57 & -1.28 & -0.39 & 0.......\\3.34 \\-0.77 \\0.48 \\4.10\end{array} \right).\end{displaymath}

入力データ (本ルーチンの詳細はDGELS のマニュアルページを参照)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

このデータをダウンロード
DGELS Example Program Data

  6      4                  :Values of M and N

 -0.57  -1.28  -0.39   0.25
 -1.93   1.08  -0.31  -2.14
  2.30   0.24   0.40  -0.35
 -1.93   0.64  -0.66   0.08
  0.15   0.30   0.15  -2.13
 -0.02   1.03  -1.43   0.50 :End of matrix A

 -2.67
 -0.55
  3.34
 -0.77
  0.48
  4.10                      :End of vector b

出力結果

(本ルーチンの詳細はDGELS のマニュアルページを参照)
1
2
3
4
5
6
7

この出力例をダウンロード
 DGELS Example Program Results

 Least squares solution
      1.5339     1.8707    -1.5241     0.0392

 Square root of the residual sum of squares
      2.22E-02

ソースコード

(本ルーチンの詳細はDGELS のマニュアルページを参照)

※本サンプルソースコードのご利用手順は「サンプルのコンパイル及び実行方法」をご参照下さい。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

このソースコードをダウンロード
    Program dgels_example

!     DGELS Example Program Text

!     Copyright 2017, Numerical Algorithms Group Ltd. http://www.nag.com

!     .. Use Statements ..
      Use blas_interfaces, Only: dnrm2
      Use lapack_interfaces, Only: dgels
      Use lapack_precision, Only: dp
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Integer, Parameter :: nb = 64, nin = 5, nout = 6
!     .. Local Scalars ..
      Real (Kind=dp) :: rnorm
      Integer :: i, info, lda, ldb, lwork, m, n, nrhs
!     .. Local Arrays ..
      Real (Kind=dp), Allocatable :: a(:, :), b(:), work(:)
!     .. Executable Statements ..
      Write (nout, *) 'DGELS Example Program Results'
      Write (nout, *)
!     Skip heading in data file
      Read (nin, *)
      Read (nin, *) m, n
      lda = m
      lwork = n + nb*m
      Allocate (a(lda,n), b(m), work(lwork))

!     Read A and B from data file

      Read (nin, *)(a(i,1:n), i=1, m)
      Read (nin, *) b(1:m)

!     Solve the least squares problem min( norm2(b - Ax) ) for x

      nrhs = 1
      ldb = m

      Call dgels('No transpose', m, n, nrhs, a, lda, b, ldb, work, lwork, &
        info)

!     Print solution

      Write (nout, *) 'Least squares solution'
      Write (nout, 100) b(1:n)

!     Compute and print estimate of the square root of the residual
!     sum of squares

      rnorm = dnrm2(m-n, b(n+1), 1)
      Write (nout, *)
      Write (nout, *) 'Square root of the residual sum of squares'
      Write (nout, 110) rnorm

100   Format (1X, 7F11.4)
110   Format (3X, 1P, E11.2)
    End Program


ご案内
関連情報
© 日本ニューメリカルアルゴリズムズグループ株式会社 2025
Privacy Policy  /  Trademarks