Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <iostream>
- #include <array>
- int main() {
- std::array<unsigned int, 5> bin_combinations = {1, 1, 0, 0, 0};
- double A[2][5] = {
- {1.0, 0.0, 1.0, -1.0, 2.0},
- {0.0, 1.0, -1.0, 2.0, -1.0}
- };
- double b[] = {2.0, 1.0};
- do {
- unsigned int tmp[2];
- size_t pos_tmp = 0;
- for (int i = 0; i != bin_combinations.size(); ++i)
- if (bin_combinations[i])
- tmp[pos_tmp++] = i;
- size_t x1 = tmp[0], x2 = tmp[1];
- double tA[2][2];
- double tb[2];
- bool key = true;
- if (A[0][x1] == 0)
- key = !key;
- for (int row = 0; row != 2; ++row) {
- tA[row][x1] = A[row][key ? x1 : x2];
- tA[row][x2] = A[row][key ? x1 : x2];
- tb[row] = b[key? row : 1 - row];
- }
- tb[0] /= tA[0][0];
- tA[0][1] = tA[0][1] / tA[0][0];
- tA[0][0] = tA[0][0] / tA[0][0];
- if (tA[1][0] != 0) {
- double k;
- k = tA[1][0] / tA[0][0];
- tA[1][0] -= k * tA[0][0];
- tA[1][1] -= k * tA[0][1];
- tb[1] -= tb[0] * k;
- }
- if (tA[1][1] != 0) {
- double k;
- tb[1] /= tA[1][1];
- tA[1][1] /= tA[1][1];
- k = tA[0][1] / tA[1][1];
- tA[0][1] -= k * tA[1][1];
- tb[0] -= k * tb[1];
- }
- std::cout << "x" << x1 << ": " << tb[0] << ", x" << x2 << ": " << tb[1] << std::endl;
- } while(std::prev_permutation(bin_combinations.begin(), bin_combinations.end()));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement