実行列の逆行列

C言語によるサンプルソースコード : 使用関数名:nag_dgetri (f07ajc)

Keyword: 実行列, 逆行列

概要

本サンプルは実行列の逆行列を求めるC言語によるサンプルプログラムです。 本サンプルは以下に示される行列Aの逆行列を求めその結果を出力します。

実行列のデータ 

※本サンプルはnAG Cライブラリに含まれる関数 nag_dgetri() のExampleコードです。本サンプル及び関数の詳細情報は nag_dgetri のマニュアルページをご参照ください。
ご相談やお問い合わせはこちらまで

入力データ

(本関数の詳細はnag_dgetri のマニュアルページを参照)
1
2
3
4
5
6

このデータをダウンロード
nag_dgetri (f07ajc) Example Program Data
  4                           :Value of N
  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

  • 1行目はタイトル行で読み飛ばされます。
  • 2行目に行列Aの次数(n)を指定しています。
  • 3~6行目に行列Aの要素を指定しています。

出力結果

(本関数の詳細はnag_dgetri のマニュアルページを参照)
1
2
3
4
5
6
7
8

この出力例をダウンロード
nag_dgetri (f07ajc) Example Program Results

 Inverse
            1          2          3          4
 1      1.7720     0.5757     0.0843     4.8155
 2     -0.1175    -0.4456     0.4114    -1.7126
 3      0.1799     0.4527    -0.6676     1.4824
 4      2.4944     0.7650    -0.0360     7.6119

  • 6~9行目に逆行列が出力されています。

ソースコード

(本関数の詳細はnag_dgetri のマニュアルページを参照)

※本サンプルソースコードはnAG数値計算ライブラリ(Windows, Linux, MAC等に対応)の関数を呼び出します。
サンプルのコンパイル及び実行方法

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108

このソースコードをダウンロード
/* nag_dgetri (f07ajc) Example Program.
 *
 * CLL6I261D/CLL6I261DL Version.
 *
 * Copyright 2017 Numerical Algorithms Group.
 *
 * Mark 26.1, 2017.
 */

#include <stdio.h>
#include <nag.h>
#include <nag_stdlib.h>
#include <nagf07.h>
#include <nagx04.h>

int main(void)
{
  /* Scalars */
  Integer i, j, n, pda;
  Integer exit_status = 0;
  NagError fail;
  Nag_OrderType order;
  /* Arrays */
  double *a = 0;
  Integer *ipiv = 0;
#ifdef nAG_LOAD_FP
  /* The following line is needed to force the Microsoft linker
     to load floating point support */
  float force_loading_of_ms_float_support = 0;
#endif /* nAG_LOAD_FP */

#ifdef nAG_COLUMN_MAJOR
#define A(I, J) a[(J-1)*pda + I - 1]
  order = Nag_ColMajor;
#else
#define A(I, J) a[(I-1)*pda + J - 1]
  order = Nag_RowMajor;
#endif

  INIT_FAIL(fail);

  printf("nag_dgetri (f07ajc) Example Program Results\n\n");

  /* Skip heading in data file */
  scanf("%*[^\n] ");
  scanf("%ld%*[^\n] ", &n);

#ifdef nAG_COLUMN_MAJOR
  pda = n;
#else
  pda = n;
#endif

  /* Allocate memory */
  if (!(a = nAG_ALLOC(n * n, double)) || !(ipiv = nAG_ALLOC(n, Integer)))
  {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  /* Read A from data file */
  for (i = 1; i <= n; ++i) {
    for (j = 1; j <= n; ++j)
      scanf("%lf", &A(i, j));
  }
  scanf("%*[^\n] ");

  /* Factorize A */
  /* nag_dgetrf (f07adc).
   * LU factorization of real m by n matrix
   */
  nag_dgetrf(order, n, n, a, pda, ipiv, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_dgetrf (f07adc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }
  /* Compute inverse of A */
  /* nag_dgetri (f07ajc).
   * Inverse of real matrix, matrix already factorized by
   * nag_dgetrf (f07adc)
   */
  nag_dgetri(order, n, a, pda, ipiv, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_dgetri (f07ajc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* Print inverse */
  /* nag_gen_real_mat_print (x04cac).
   * Print real general matrix (easy-to-use)
   */
  fflush(stdout);
  nag_gen_real_mat_print(order, Nag_GeneralMatrix, Nag_NonUnitDiag, n, n, a,
                         pda, "Inverse", 0, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_gen_real_mat_print (x04cac).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }
END:
  nAG_FREE(a);
  nAG_FREE(ipiv);

  return exit_status;
}


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