저는 수학에 관한 기본적인 질문을 가지고 있습니다. 그러나 트릭은 C++에서 필요합니다. 나는 현재 Wikipedia에 주어진 pseudocode를 따르고있다. 위키 백과에서가우스 제거 C++의 선형 방정식
createMatrixForAllSolutions(*this);
std::cout << equationMatrix.to_string() << endl;
bool solved = false;
int rows = equationMatrix.getRows();
int cols = equationMatrix.getCols();
int i = 0;
int j = 0;
int maxi = 0;
double current = 0;
double eqnValue = 0;
double solValue = 0;
std::vector<char> reversedVars;
int sum = 0;
int tempValue;
int tempRHS;
int newValue;
int neRHS;
while (i < rows && j < cols) {
maxi = i;
for (int k = i + 1; k < rows; k++) {
if (abs(equationMatrix.get_element(k, j)) > abs(equationMatrix.get_element(maxi, j)))
maxi = k;
}
if (equationMatrix.get_element(maxi, j) != 0) {
current = equationMatrix.get_element(i, j);
for (int x = 0; x < cols; x++) {
tempValue = equationMatrix.get_element(i, x);
newValue = equationMatrix.get_element(maxi, x);
equationMatrix.set_element(i, x, newValue/current);
equationMatrix.set_element(maxi, x, tempValue);
}
tempRHS = solutionMatrix.get_element(i, 0);
neRHS = solutionMatrix.get_element(maxi, 0);
solutionMatrix.set_element(i, 0, neRHS/current);
solutionMatrix.set_element(maxi, 0, tempRHS);
//SWAP rows i and maxi
//SWAP RHS i and maxi
//DIVIDE each entry in row i by current
//DIVIDE RHS i by current
for (int u = i + 1; u < rows; u++) {
eqnValue = equationMatrix.get_element(u, j) - equationMatrix.get_element(i, j) * equationMatrix.get_element(u, j);
std::cout << "Equation Value: " << eqnValue << endl;
equationMatrix.set_element(u, j, eqnValue);
solValue = solutionMatrix.get_element(u, 0) - solutionMatrix.get_element(i, 0) * solutionMatrix.get_element(u, 0);
std::cout << "Solution Value: " << solValue << endl;
solutionMatrix.set_element(u, 0, solValue);
}
i++;
}
j++;
}
그리고 난 다음입니다 의사 코드입니다 : 여기 내 시도 내 최고의 경기를 한 적이
i := 1
j := 1
while (i ≤ m and j ≤ n) do
Find pivot in column j, starting in row i:
maxi := i
for k := i+1 to m do
if abs(A[k,j]) > abs(A[maxi,j]) then
maxi := k
end if
end for
if A[maxi,j] ≠ 0 then
swap rows i and maxi, but do not change the value of i
Now A[i,j] will contain the old value of A[maxi,j].
divide each entry in row i by A[i,j]
Now A[i,j] will have the value 1.
for u := i+1 to m do
subtract A[u,j] * row i from row u
Now A[u,j] will be 0, since A[u,j] - A[i,j] * A[u,j] = A[u,j] - 1 * A[u,j] = 0.
end for
i := i + 1
end if
j := j + 1
end while
그것은 지금까지하지만 아무도 그 이유를 알아낼 수 있다면 내 미상은 작동하지 않습니다. 사랑 스러울 것입니다. 감사!
코드는 어떻게됩니까? 그것은 컴파일합니까? 그것은 추락합니까? 그것은 잘못된 대답을 제공합니까? –
잘못된 답변을 제공합니다 ... 사실 매우 큰 숫자입니다. – Brandon