Keyword: 実スパース, 連立方程式, 誤差限界, 解の改良
概要
本サンプルは実スパース連立方程式の誤差限界をもつ解の改良を行うC言語によるサンプルプログラムです。 本サンプルは以下に示される実スパース連立方程式を反復改良を用いて解き、解と前進誤差と後退誤差を出力します。
※本サンプルはnAG Cライブラリに含まれる関数 nag_superlu_refine_lu() のExampleコードです。本サンプル及び関数の詳細情報は nag_superlu_refine_lu のマニュアルページをご参照ください。
ご相談やお問い合わせはこちらまで
入力データ
(本関数の詳細はnag_superlu_refine_lu のマニュアルページを参照)1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
このデータをダウンロード |
nag_superlu_refine_lu (f11mhc) Example Program Data 5 2 n, nrhs 1 3 5 7 9 12 icolzp(i) i=0..n 2. 1 4. 3 1. 1 -2. 5 1. 2 1. 3 -1. 2 1. 4 1. 3 2. 4 3. 5 a(i) irowix(i) i=0..nnz-1 1.56 -.25 3.6 1.33 .52 3.12 -.50 7.2 2.66 1.04 matrix x
- 1行目はタイトル行で読み飛ばされます。
- 2行目に行列Aの次数(n)とBの右辺の数(nrhs)を指定しています。
- 3~8行目に行列Aの列の先頭の非ゼロ要素のインデックス(icolzp)を指定しています。
- 9~19行目に行列Aの非ゼロ値とその行インデックス(irowix)を指定しています。
- 20~21行目に解の行列Xの要素の値を指定しています。
出力結果
(本関数の詳細はnag_superlu_refine_lu のマニュアルページを参照)1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
この出力例をダウンロード |
nag_superlu_refine_lu (f11mhc) Example Program Results Solutions 1 2 1 0.7000 1.4000 2 0.1600 0.3200 3 0.5200 1.0400 4 0.7700 1.5400 5 0.2800 0.5600 Estimated Forward Error 5e-15 5e-15 Backward Error 3.6e-17 3.6e-17
- 6~11行目に改良された解が出力されています。
- 14~15行目に前進誤差限界の推定値が出力されています。
- 18~19行目に後退誤差限界が出力されています。
ソースコード
(本関数の詳細はnag_superlu_refine_lu のマニュアルページを参照)
※本サンプルソースコードは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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
このソースコードをダウンロード |
/* nag_superlu_refine_lu (f11mhc) Example Program. * * CLL6I261D/CLL6I261DL Version. * * Copyright 2017 Numerical Algorithms Group. * * Mark 26.1, 2017. */ #include <stdio.h> #include <nag.h> #include <nagx04.h> #include <nag_stdlib.h> #include <nagf11.h> /* Table of constant values */ static Integer c__1 = 1; static Integer c__80 = 80; static Integer c__0 = 0; int main(void) { double flop, thresh; Integer exit_status = 0, i, j; Integer n, nnz, nnzl, nnzu, nrhs, nzlmx, nzlumx, nzumx; double *a = 0, *b = 0, *berr = 0, *ferr = 0, *lval = 0; double *uval = 0, *x = 0; Integer *icolzp = 0, *il = 0, *iprm = 0, *irowix = 0; Integer *iu = 0; /* Nag types */ Nag_OrderType order = Nag_ColMajor; Nag_MatrixType matrix = Nag_GeneralMatrix; Nag_DiagType diag = Nag_NonUnitDiag; Nag_ColumnPermutationType ispec; Nag_TransType trans; NagError fail; INIT_FAIL(fail); printf("nag_superlu_refine_lu (f11mhc) Example Program Results\n\n"); /* Skip heading in data file */ scanf("%*[^\n] "); /* Read order of matrix and number of right hand sides */ scanf("%ld%ld%*[^\n] ", &n, &nrhs); /* Read the matrix A */ if (!(icolzp = nAG_ALLOC(n + 1, Integer))) { printf("Allocation failure\n"); exit_status = -1; goto END; } for (i = 1; i <= n + 1; ++i) scanf("%ld%*[^\n] ", &icolzp[i - 1]); nnz = icolzp[n] - 1; /* Allocate memory */ if (!(irowix = nAG_ALLOC(nnz, Integer)) || !(a = nAG_ALLOC(nnz, double)) || !(il = nAG_ALLOC(7 * n + 8 * nnz + 4, Integer)) || !(iu = nAG_ALLOC(2 * n + 8 * nnz + 1, Integer)) || !(uval = nAG_ALLOC(8 * nnz, double)) || !(lval = nAG_ALLOC(8 * nnz, double)) || !(b = nAG_ALLOC(n * nrhs, double)) || !(x = nAG_ALLOC(n * nrhs, double)) || !(berr = nAG_ALLOC(nrhs, double)) || !(ferr = nAG_ALLOC(nrhs, double)) || !(iprm = nAG_ALLOC(7 * n, Integer))) { printf("Allocation failure\n"); exit_status = -1; goto END; } for (i = 0; i < nnz; ++i) scanf("%lf%ld%*[^\n] ", &a[i], &irowix[i]); /* Read the right hand sides */ for (j = 0; j < nrhs; ++j) { for (i = 0; i < n; ++i) { scanf("%lf", &x[j * n + i]); b[j * n + i] = x[j * n + i]; } scanf("%*[^\n] "); } /* Calculate COLAMD permutation */ ispec = Nag_Sparse_Colamd; /* nag_superlu_column_permutation (f11mdc). * Real sparse nonsymmetric linear systems, setup for * nag_superlu_lu_factorize (f11mec) */ nag_superlu_column_permutation(ispec, n, icolzp, irowix, iprm, &fail); if (fail.code != NE_NOERROR) { printf("Error from nag_superlu_column_permutation (f11mdc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Factorise */ thresh = 1.; nzlmx = 8 * nnz; nzlumx = 8 * nnz; nzumx = 8 * nnz; /* nag_superlu_lu_factorize (f11mec). * LU factorization of real sparse matrix */ nag_superlu_lu_factorize(n, irowix, a, iprm, thresh, nzlmx, &nzlumx, nzumx, il, lval, iu, uval, &nnzl, &nnzu, &flop, &fail); if (fail.code != NE_NOERROR) { printf("Error from nag_superlu_lu_factorize (f11mec).\n%s\n", fail.message); exit_status = 1; goto END; } /* Compute solution in array X */ trans = Nag_NoTrans; /* nag_superlu_solve_lu (f11mfc). * Solution of real sparse simultaneous linear equations * (coefficient matrix already factorized) */ nag_superlu_solve_lu(order, trans, n, iprm, il, lval, iu, uval, nrhs, x, n, &fail); if (fail.code != NE_NOERROR) { printf("Error from nag_superlu_solve_lu (f11mfc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Improve solution, and compute backward errors and estimated */ /* bounds on the forward errors */ /* nag_superlu_refine_lu (f11mhc). * Refined solution with error bounds of real system of * linear equations, multiple right-hand sides */ nag_superlu_refine_lu(order, trans, n, icolzp, irowix, a, iprm, il, lval, iu, uval, nrhs, b, n, x, n, ferr, berr, &fail); if (fail.code != NE_NOERROR) { printf("Error from nag_superlu_refine_lu (f11mhc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Print solution */ printf("\n"); /* nag_gen_real_mat_print (x04cac). * Print real general matrix (easy-to-use) */ fflush(stdout); nag_gen_real_mat_print(order, matrix, diag, n, nrhs, x, n, "Solutions", 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; } /* nag_gen_real_mat_print_comp (x04cbc). * Print real general matrix (comprehensive) */ fflush(stdout); nag_gen_real_mat_print_comp(order, matrix, diag, nrhs, c__1, ferr, nrhs, "%8.2g", "Estimated Forward Error", Nag_NoLabels, NULL, Nag_NoLabels, NULL, c__80, c__0, 0, &fail); if (fail.code != NE_NOERROR) { printf("Error from nag_gen_real_mat_print_comp (x04cbc).\n%s\n", fail.message); exit_status = 1; goto END; } /* nag_gen_real_mat_print_comp (x04cbc), see above. */ fflush(stdout); nag_gen_real_mat_print_comp(order, matrix, diag, nrhs, c__1, berr, nrhs, "%8.2g", "Backward Error", Nag_NoLabels, NULL, Nag_NoLabels, NULL, c__80, c__0, 0, &fail); if (fail.code != NE_NOERROR) { printf("Error from nag_gen_real_mat_print_comp (x04cbc).\n%s\n", fail.message); exit_status = 1; goto END; } END: nAG_FREE(a); nAG_FREE(b); nAG_FREE(berr); nAG_FREE(ferr); nAG_FREE(lval); nAG_FREE(uval); nAG_FREE(x); nAG_FREE(icolzp); nAG_FREE(il); nAG_FREE(iprm); nAG_FREE(irowix); nAG_FREE(iu); return exit_status; }