Keyword: 複素行列, 逆行列
概要
本サンプルは複素行列の逆行列を求めるC言語によるサンプルプログラムです。 本サンプルは以下に示される行列Aの逆行列を求めその結果を出力します。
※本サンプルはnAG Cライブラリに含まれる関数 nag_zgetri() のExampleコードです。本サンプル及び関数の詳細情報は nag_zgetri のマニュアルページをご参照ください。
ご相談やお問い合わせはこちらまで
入力データ
(本関数の詳細はnag_zgetri のマニュアルページを参照)1 2 3 4 5 6
このデータをダウンロード |
nag_zgetri (f07awc) Example Program Data 4 :Value of N (-1.34, 2.55) ( 0.28, 3.17) (-6.39,-2.20) ( 0.72,-0.92) (-0.17,-1.41) ( 3.31,-0.15) (-0.15, 1.34) ( 1.29, 1.38) (-3.29,-2.39) (-1.91, 4.42) (-0.14,-1.35) ( 1.72, 1.35) ( 2.41, 0.39) (-0.56, 1.47) (-0.83,-0.69) (-1.96, 0.67) :End of matrix A
- 1行目はタイトル行で読み飛ばされます。
- 2行目に行列Aの次数(n)を指定しています。
- 3~6行目に行列Aの要素を指定しています。
出力結果
(本関数の詳細はnag_zgetri のマニュアルページを参照)1 2 3 4 5 6 7 8
この出力例をダウンロード |
nag_zgetri (f07awc) Example Program Results Inverse 1 2 3 4 1 ( 0.0757,-0.4324) ( 1.6512,-3.1342) ( 1.2663, 0.0418) ( 3.8181, 1.1195) 2 (-0.1942, 0.0798) (-1.1900,-0.1426) (-0.2401,-0.5889) (-0.0101,-1.4969) 3 (-0.0957,-0.0491) ( 0.7371,-0.4290) ( 0.3224, 0.0776) ( 0.6887, 0.7891) 4 ( 0.3702,-0.5040) ( 3.7253,-3.1813) ( 1.7014, 0.7267) ( 3.9367, 3.3255)
- 6~9行目に逆行列が出力されています。
ソースコード
(本関数の詳細はnag_zgetri のマニュアルページを参照)
※本サンプルソースコードは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_zgetri (f07awc) 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, n, pda; Integer exit_status = 0; NagError fail; Nag_OrderType order; /* Arrays */ Complex *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_zgetri (f07awc) 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 ipiv_len = n; /* Allocate memory */ if (!(a = nAG_ALLOC(n * n, Complex)) || !(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 , %lf )", &A(i, j).re, &A(i, j).im); } scanf("%*[^\n] "); /* Factorize A */ /* nag_zgetrf (f07arc). * LU factorization of complex m by n matrix */ nag_zgetrf(order, n, n, a, pda, ipiv, &fail); if (fail.code != NE_NOERROR) { printf("Error from nag_zgetrf (f07arc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Compute inverse of A */ /* nag_zgetri (f07awc). * Inverse of complex matrix, matrix already factorized by * nag_zgetrf (f07arc) */ nag_zgetri(order, n, a, pda, ipiv, &fail); if (fail.code != NE_NOERROR) { printf("Error from nag_zgetri (f07awc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Print inverse */ /* nag_gen_complx_mat_print_comp (x04dbc). * Print complex general matrix (comprehensive) */ fflush(stdout); nag_gen_complx_mat_print_comp(order, Nag_GeneralMatrix, Nag_NonUnitDiag, n, n, a, pda, Nag_BracketForm, "%7.4f", "Inverse", Nag_IntegerLabels, 0, Nag_IntegerLabels, 0, 80, 0, 0, &fail); if (fail.code != NE_NOERROR) { printf("Error from nag_gen_complx_mat_print_comp (x04dbc).\n%s\n", fail.message); exit_status = 1; goto END; } END: nAG_FREE(a); nAG_FREE(ipiv); return exit_status; }