計算ルーチン: 実一般三角-五角行列の QR 分解

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

ホーム > LAPACKサンプルプログラム目次 > 計算ルーチン > 実一般三角-五角行列の QR 分解

概要

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

入力データ

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

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

  6      4      2           : m, n and nrhs

 -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 : matrix A

 -2.67   0.41
 -0.55  -3.10
  3.34  -4.01
 -0.77   2.76
  0.48  -6.17
  4.10   0.21               : matrix B

出力結果

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

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

 solution(s) for n rows
             1          2
 1      1.5179    -1.5850
 2      1.8629     0.5531
 3     -1.4608     1.3485
 4      0.0398     2.9619

 Least squares solution(s) for all rows
             1          2
 1      1.5339    -1.5753
 2      1.8707     0.5559
 3     -1.5241     1.3119
 4      0.0392     2.9585

 Square root(s) of the residual sum(s) of squares
        2.22E-02   1.38E-02

ソースコード

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

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


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

!     DTPQRT Example Program Text

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

!     .. Use Statements ..
      Use blas_interfaces, Only: dnrm2
      Use lapack_example_aux, Only: nagf_file_print_matrix_real_gen
      Use lapack_interfaces, Only: dgemqrt, dgeqrt, dtpmqrt, dtpqrt, dtrtrs
      Use lapack_precision, Only: dp
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Integer, Parameter :: nbmax = 64, nin = 5, nout = 6
!     .. Local Scalars ..
      Integer :: i, ifail, info, j, lda, ldb, ldt, lwork, m, n, nb, nrhs
!     .. Local Arrays ..
      Real (Kind=dp), Allocatable :: a(:, :), b(:, :), c(:, :), rnorm(:), &
        t(:, :), work(:)
!     .. Intrinsic Procedures ..
      Intrinsic :: max, min
!     .. Executable Statements ..
      Write (nout, *) 'DTPQRT Example Program Results'
      Write (nout, *)
      Flush (nout)
!     Skip heading in data file
      Read (nin, *)
      Read (nin, *) m, n, nrhs
      lda = m
      ldb = m
      nb = min(m, n, nbmax)
      ldt = nb
      lwork = nb*max(n, m)
      Allocate (a(lda,n), b(ldb,nrhs), c(ldb,nrhs), rnorm(nrhs), t(ldt,min(m, &
        n)), work(lwork))

!     Read A and B from data file

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

      c(1:m, 1:nrhs) = b(1:m, 1:nrhs)
!     Compute the QR factorization of first n rows of A
      Call dgeqrt(n, n, nb, a, lda, t, ldt, work, info)

!     Compute C = (C1) = (Q**T)*B, storing the result in C
!                  (C2)
      Call dgemqrt('Left', 'Transpose', n, nrhs, n, nb, a, lda, t, ldt, c, &
        ldb, work, info)

      b(1:n, 1:nrhs) = c(1:n, 1:nrhs)
!     Compute least squares solutions for first n rows by back-substitution in
!     R*X = C1
      Call dtrtrs('Upper', 'No transpose', 'Non-Unit', n, nrhs, a, lda, c, &
        ldb, info)

      If (info>0) Then
        Write (nout, *) 'The upper triangular factor, R, of A is singular, '
        Write (nout, *) 'the least squares solution could not be computed'
      Else

!       Print solution using first n rows

!       ifail: behaviour on error exit
!              =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
        ifail = 0
        Call nagf_file_print_matrix_real_gen('General', ' ', n, nrhs, c, ldb, &
          'solution(s) for n rows', ifail)

      End If

!     Now add the remaining rows and perform QR update
      Call dtpqrt(m-n, n, 0, nb, a, lda, a(n+1,1), lda, t, ldt, work, info)

!     Apply orthogonal transformations to C
      Call dtpmqrt('Left', 'Transpose', m-n, nrhs, n, 0, nb, a(n+1,1), lda, t, &
        ldt, b, ldb, b(5,1), ldb, work, info)
!     Compute least squares solutions for first n rows by bac-substitution in
!     R*X = C1
      Call dtrtrs('Upper', 'No transpose', 'Non-Unit', n, nrhs, a, lda, b, &
        ldb, info)

      If (info>0) Then
        Write (nout, *) 'The upper triangular factor, R, of A is singular, '
        Write (nout, *) 'the least squares solution could not be computed'
      Else

!       Print least squares solutions
        Write (nout, *)
        Flush (nout)
        ifail = 0
        Call nagf_file_print_matrix_real_gen('G', ' ', n, nrhs, b, ldb, &
          'Least squares solution(s) for all rows', ifail)

!       Compute and print estimates of the square roots of the residual
!       sums of squares

        Do j = 1, nrhs
          rnorm(j) = dnrm2(m-n, b(n+1,j), 1)
        End Do

        Write (nout, *)
        Write (nout, *) 'Square root(s) of the residual sum(s) of squares'
        Write (nout, 100) rnorm(1:nrhs)
      End If

100   Format (5X, 1P, 7E11.2)
    End Program


ご案内
関連情報
Privacy Policy  /  Trademarks