Keyword: 実行列, 条件数
概要
本サンプルは実行列の条件数の推定を行うC言語によるサンプルプログラムです。 本サンプルは以下に示される行列Aの条件数の推定を行いその結果を出力します。
※本サンプルはnAG Cライブラリに含まれる関数 nag_dgecon() のExampleコードです。本サンプル及び関数の詳細情報は nag_dgecon のマニュアルページをご参照ください。
ご相談やお問い合わせはこちらまで
入力データ
(本関数の詳細はnag_dgecon のマニュアルページを参照)| このデータをダウンロード |
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 のマニュアルページを参照)- 3行目に条件数の推定値が出力されています。
ソースコード
(本関数の詳細はnag_dgecon のマニュアルページを参照)
※本サンプルソースコードはnAG数値計算ライブラリ(Windows, Linux, MAC等に対応)の関数を呼び出します。
サンプルのコンパイル及び実行方法
| このソースコードをダウンロード |
/* 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;
}
