Keyword: 実行列, LU分解
概要
本サンプルは実行列のLU分解を行うC言語によるサンプルプログラムです。 本サンプルは以下に示される行列AのLU分解を行いその結果を出力します。
※本サンプルはnAG Cライブラリに含まれる関数 nag_dgetrf() のExampleコードです。本サンプル及び関数の詳細情報は nag_dgetrf のマニュアルページをご参照ください。
ご相談やお問い合わせはこちらまで
入力データ
(本関数の詳細はnag_dgetrf のマニュアルページを参照)1 2 3 4 5 6
このデータをダウンロード |
nag_dgetrf (f07adc) Example Program Data 4 4 :Values of M and 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の行数(m)と列数(n)を指定しています。
- 3~6行目に行列Aの要素を指定しています。
出力結果
(本関数の詳細はnag_dgetrf のマニュアルページを参照)1 2 3 4 5 6 7 8 9 10 11
この出力例をダウンロード |
nag_dgetrf (f07adc) Example Program Results Details of factorization 1 2 3 4 1 5.2500 -2.9500 -0.9500 -3.8000 2 0.3429 3.8914 2.3757 0.4129 3 0.3010 -0.4631 -1.5139 0.2948 4 -0.2114 -0.3299 0.0047 0.1314 ipiv 2 2 3 4
- 5~9行目にLU分解の結果が出力されています。
- 12行目にピボット指数が出力されています。
ソースコード
(本関数の詳細はnag_dgetrf のマニュアルページを参照)
※本サンプルソースコードは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
このソースコードをダウンロード |
/* nag_dgetrf (f07adc) 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, ipiv_len, j, m, 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_dgetrf (f07adc) Example Program Results\n\n"); /* Skip heading in data file */ scanf("%*[^\n] "); scanf("%ld%ld%*[^\n] ", &m, &n); ipiv_len = MIN(m, n); #ifdef nAG_COLUMN_MAJOR pda = m; #else pda = n; #endif /* Allocate memory */ if (!(a = nAG_ALLOC(m * n, double)) || !(ipiv = nAG_ALLOC(ipiv_len, Integer))) { printf("Allocation failure\n"); exit_status = -1; goto END; } /* Read A from data file */ for (i = 1; i <= m; ++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, m, 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; } /* Print details of factorization */ /* 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, m, n, a, pda, "Details of factorization", 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; } /* Print pivot indices */ printf("\nipiv\n"); for (i = 1; i <= MIN(m, n); ++i) printf("%6ld%s", ipiv[i - 1], i % 7 == 0 ? "\n" : " "); printf("\n"); END: nAG_FREE(a); nAG_FREE(ipiv); return exit_status; }