Keyword: 実行列, 条件数
概要
本サンプルは実行列の条件数の推定を行うC言語によるサンプルプログラムです。 本サンプルは以下に示される行列Aの条件数の推定を行いその結果を出力します。
※本サンプルはnAG Cライブラリに含まれる関数 nag_dgecon() のExampleコードです。本サンプル及び関数の詳細情報は nag_dgecon のマニュアルページをご参照ください。
ご相談やお問い合わせはこちらまで
入力データ
(本関数の詳細はnag_dgecon のマニュアルページを参照)1 2 3 4 5 6
このデータをダウンロード |
nag_dgecon (f07agc) 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_dgecon のマニュアルページを参照)1 2 3
この出力例をダウンロード |
nag_dgecon (f07agc) Example Program Results Estimate of condition number = 1.52e+02
- 3行目に条件数の推定値が出力されています。
ソースコード
(本関数の詳細はnag_dgecon のマニュアルページを参照)
※本サンプルソースコードは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
このソースコードをダウンロード |
/* nag_dgecon (f07agc) 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 <nagf16.h> #include <nagx02.h> #include <math.h> int main(void) { /* Scalars */ double anorm, rcond; Integer exit_status = 0; Integer i, ipiv_len, j, n, pda; NagError fail; Nag_OrderType order; /* Arrays */ double *a = 0; Integer *ipiv = 0; #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_dgecon (f07agc) Example Program Results\n"); /* Skip heading in data file */ scanf("%*[^\n] "); scanf("%ld%*[^\n] ", &n); pda = n; ipiv_len = n; /* Allocate memory */ if (!(a = nAG_ALLOC(n * 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 <= n; ++i) { for (j = 1; j <= n; ++j) scanf("%lf", &A(i, j)); } scanf("%*[^\n] "); /* Compute norm of A */ /* nag_dge_norm (f16rac). * 1-norm, infinity-norm, Frobenius norm, largest absolute * element, real general matrix */ nag_dge_norm(order, Nag_OneNorm, n, n, a, pda, &anorm, &fail); if (fail.code != NE_NOERROR) { printf("Error from nag_dge_norm (f16rac).\n%s\n", fail.message); exit_status = 1; goto END; } /* 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; } printf("\n"); /* Estimate condition number */ /* nag_dgecon (f07agc). * Estimate condition number of real matrix, matrix already * factorized by nag_dgetrf (f07adc) */ nag_dgecon(order, Nag_OneNorm, n, a, pda, anorm, &rcond, &fail); if (fail.code != NE_NOERROR) { printf("Error from nag_dgecon (f07agc).\n%s\n", fail.message); exit_status = 1; goto END; } /* nag_machine_precision (x02ajc). * The machine precision */ if (rcond >= nag_machine_precision) { printf("Estimate of condition number =%11.2e\n", 1.0 / rcond); } else printf("A is singular to working precision\n"); END: nAG_FREE(a); nAG_FREE(ipiv); return exit_status; }