関数solution_Cramer マニュアル

(The documentation of function solution_Cramer)

Last Update: 2021/12/6


◆機能・用途(Purpose)

クラメルの公式を用いて連立1次方程式 \[\begin{equation} \myvector{A}\myvector{x}=\myvector{b} \label{eq.problem} \end{equation}\] を解く。 ここで\(\myvector{A}\)は既知の正方行列、 \(\myvector{b}\)は既知の列ベクトル、 \(\myvector{x}\)は未知の列ベクトルである。 現在のところ、未知数の個数が3つ以内の連立方程式にのみ対応している。
Solve a simultaneous linear equation (\ref{eq.problem}) using Cramer's formula, where where \(\myvector{A}\) is a known square matrix, \(\myvector{b}\) is a known column vector, and \(\myvector{x}\) is an unknown column vector. Currently, this function supports simultaneous equations with less than or equal to 3 unknown parameters.


◆形式(Format)

#include <matrix/inverse.h>
inline struct columnvector solution_Cramer
(const struct matrix A,const struct columnvector b)


◆引数(Arguments)

A 連立方程式の左辺の係数行列 ((\ref{eq.problem})式の\(\myvector{A}\))。
A matrix composed of the coefficients in the left hand side of the simultaneous equation; the matrix \(\myvector{A}\) of eq. (\ref{eq.problem}).
b 連立方程式の右辺の定数ベクトル ((\ref{eq.problem})式の\(\myvector{b}\))。
A vector composed of the constants in the right hand side of the simultaneous equation; the column vector \(\myvector{b}\) of eq. (\ref{eq.problem}).


◆戻り値(Return value)

方程式(\ref{eq.problem})の解\(\myvector{x}\)を表す列ベクトル。
A column vector representing the solution \(\myvector{x}\) of eq. (\ref{eq.problem}).


◆使用例(Example)

struct matrix A;
struct columnvector b;
struct columnvector x=solution_Cramer(A,b);


◆計算式(Formula)


1. 一般形 (General formula)

\(\myvector{A}\)の第\(j\)列を\(\myvector{b}\)で置き換えた行列を \(\myvector{A_j}\)と書くことにすると、 方程式(\ref{eq.problem})の解\(\myvector{x}\)の第\(j\)成分\(x_j\)は クラメルの公式より \[\begin{equation} x_j=\frac{|\myvector{A_j}|}{|\myvector{A}|} \label{eq.solution} \end{equation}\] と書ける。
Let \(\myvector{A_j}\) be a matrix obtained by replacing the \(j\)th column of \(\myvector{A}\) with \(\myvector{b}\). Then the \(j\)th component of the solution \(\myvector{x}\) of eq. (\ref{eq.problem}), hereafter denoted by \(x_j\), is given by eq. (\ref{eq.solution}) according to Cramer's formula.

この関数では(\ref{eq.solution})式に基づいて 解の各成分を具体的に書き下した式を用いて計算する。 したがって、モデルパラメータ数(\(\myvector{x}\)の次元)が少なく 具体的に書き下すのが容易な場合についてのみ この関数を利用することができる。 現在のところ、モデルパラメータ数が3個以内の場合のみ この関数を利用できる。
This function computes the individual components of the solution based on eq. (\ref{eq.solution}). Thus the function is available only when the formula of the solution can be easily written (i.e., there are only a few model parameters; the dimension of \(\myvector{x}\) is small). Currently, this function supports only the problems with less than or equal to 3 model parameters.

以下ではモデルパラメータ数ごとに (\ref{eq.solution})式を具体的に書き下していく。 その際、プログラムとの対応関係を容易にするために 行列の添字番号を0から始まるものとして書くことにする。
Below, we provide the formula corresponding to eq. (\ref{eq.solution}) for each number of model parameters. In the formulas, the indices of the matrix components begin with 0, in order to make the relation between the formula and computer code clearer.


2. モデルパラメータ数1の場合 (In cases where there is one model parameter)

(\ref{eq.solution})式を具体的に書き下すと \[\begin{equation} x_0 = \frac{b_0}{A_{00}} \label{eq.solution.1} \end{equation}\] となる。
Eq. (\ref{eq.solution}) is written as (\ref{eq.solution.1}).


3. モデルパラメータ数2の場合 (In cases where there are two model parameters)

(\ref{eq.solution})式を具体的に書き下すと \[\begin{eqnarray} x_0 &=& \frac{a_{11}b_0 - a_{01}b_1}{a_{00}a_{11} - a_{01}a_{10}} \nonumber \\ x_1 &=& \frac{a_{00}b_1 - a_{10}b_0}{a_{00}a_{11} - a_{01}a_{10}} \label{eq.solution.2} \end{eqnarray}\] となる。
Eq. (\ref{eq.solution}) is written as (\ref{eq.solution.2}).


4. モデルパラメータ数3の場合 (In cases where there are three model parameters)

(\ref{eq.solution})式を具体的に書き下すと \[\begin{eqnarray} x_0 &=& \frac{(a_{11}a_{22}-a_{12}a_{21})b_0 -(a_{01}a_{22}-a_{02}a_{21})b_1 +(a_{01}a_{12}-a_{02}a_{11})b_2} {a_{00}a_{11}a_{22}+a_{01}a_{12}a_{20}+a_{02}a_{10}a_{21} -a_{00}a_{12}a_{21}-a_{01}a_{10}a_{22}-a_{02}a_{11}a_{20}} \nonumber \\ x_1 &=& \frac{-(a_{10}a_{22}-a_{12}a_{20})b_0 +(a_{00}a_{22}-a_{02}a_{20})b_1 -(a_{00}a_{12}-a_{02}a_{10})b_2} {a_{00}a_{11}a_{22}+a_{01}a_{12}a_{20}+a_{02}a_{10}a_{21} -a_{00}a_{12}a_{21}-a_{01}a_{10}a_{22}-a_{02}a_{11}a_{20}} \nonumber \\ x_2 &=& \frac{(a_{10}a_{21}-a_{11}a_{20})b_0 -(a_{00}a_{21}-a_{01}a_{20})b_1 +(a_{00}a_{11}-a_{01}a_{10})b_2} {a_{00}a_{11}a_{22}+a_{01}a_{12}a_{20}+a_{02}a_{10}a_{21} -a_{00}a_{12}a_{21}-a_{01}a_{10}a_{22}-a_{02}a_{11}a_{20}} \label{eq.solution.3} \end{eqnarray}\] となる。
Eq. (\ref{eq.solution}) is written as (\ref{eq.solution.3}).


◆検証(Validation)

この関数が正しく動作することは以下の手順により確認した。
This function was checked by the procedure below:
  1. 乱数を用いて行列\(\myvector{A}\), \(\myvector{b}\)を作成する (関数random_matrix, random_columnvector (matrix/initialize.h)使用)。
    Create matrices \(\myvector{A}\) and \(\myvector{b}\) using random values (functions random_matrix and random_columnvector in matrix/initialize.h were used);
  2. それらを用いてこの関数により解\(\myvector{x}\)を求める。
    Solve for \(\myvector{x}\) using this function;
  3. 得られた解を使って積\(\myvector{A}\myvector{x}\)を計算する。
    Using the solution, compute a product \(\myvector{A}\myvector{x}\); and
  4. 結果を\(\myvector{b}\)と比較する。
    Compare the result with \(\myvector{b}\).
この検証の結果は以下の通りであった。
Results from this check were as below.


(1) モデルパラメータ数1の場合 (In cases where there is one model parameter)

\[\begin{equation*} \myvector{A}= \begin{pmatrix} -5.542348e-10 \end{pmatrix} \end{equation*}\] \[\begin{equation*} \myvector{b}= \begin{pmatrix} 1.180734e-09 \end{pmatrix} \end{equation*}\] \[\begin{equation*} \myvector{x}= \begin{pmatrix} -2.130386e+00 \end{pmatrix} \end{equation*}\] \[\begin{equation*} \myvector{A}\myvector{x}= \begin{pmatrix} 1.180734e-09 \end{pmatrix} \end{equation*}\]

(2) モデルパラメータ数2の場合 (In cases where there are two model parameters)

\[\begin{equation*} \myvector{A}= \begin{pmatrix} -5.946389e-10 & -5.832139e-10 \\ -5.107910e-10 & -2.357166e-09 \end{pmatrix} \end{equation*}\] \[\begin{equation*} \myvector{b}= \begin{pmatrix} 1.389110e-09 \\ 6.061486e-10 \end{pmatrix} \end{equation*}\] \[\begin{equation*} \myvector{x}= \begin{pmatrix} -2.646266e+00 \\ 3.162868e-01 \end{pmatrix} \end{equation*}\] \[\begin{equation*} \myvector{A}\myvector{x}= \begin{pmatrix} 1.389110e-09 \\ 6.061486e-10 \end{pmatrix} \end{equation*}\]

(3) モデルパラメータ数3の場合 (In cases where there are three model parameters)

\[\begin{equation*} \myvector{A}= \begin{pmatrix} -1.676399e-09 & -8.405894e-10 & 9.754172e-10 \\ -7.404720e-10 & 1.276538e-09 & -9.070130e-10 \\ -4.890220e-10 & 5.082556e-10 & 7.325038e-10 \end{pmatrix} \end{equation*}\] \[\begin{equation*} \myvector{b}= \begin{pmatrix} 6.491890e-10 \\ 3.288509e-09 \\ 7.671914e-10 \end{pmatrix} \end{equation*}\] \[\begin{equation*} \myvector{x}= \begin{pmatrix} -1.431726e+00 \\ 1.212761e+00 \\ -7.499572e-01 \end{pmatrix} \end{equation*}\] \[\begin{equation*} \myvector{A}\myvector{x}= \begin{pmatrix} 6.491890e-10 \\ 3.288509e-09 \\ 7.671914e-10 \end{pmatrix} \end{equation*}\]

いずれのケースにおいても 与えた\(\myvector{b}\)と 解を元に計算した\(\myvector{A}\myvector{x}\)は一致しており、 解を正しく計算できたことが分かる。
In any cases, the given \(\myvector{b}\) is consistent with \(\myvector{A}\myvector{x}\) calculated by the solution, indicating that the solution was correct.