Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <array>
  4.  
  5. int main() {
  6.  
  7. std::array<unsigned int, 5> bin_combinations = {1, 1, 0, 0, 0};
  8. double A[2][5] = {
  9. {1.0, 0.0, 1.0, -1.0, 2.0},
  10. {0.0, 1.0, -1.0, 2.0, -1.0}
  11. };
  12. double b[] = {2.0, 1.0};
  13.  
  14.  
  15. do {
  16. unsigned int tmp[2];
  17.  
  18. size_t pos_tmp = 0;
  19. for (int i = 0; i != bin_combinations.size(); ++i)
  20. if (bin_combinations[i])
  21. tmp[pos_tmp++] = i;
  22.  
  23. size_t x1 = tmp[0], x2 = tmp[1];
  24.  
  25. double tA[2][2];
  26. double tb[2];
  27.  
  28. bool key = true;
  29.  
  30. if (A[0][x1] == 0)
  31. key = !key;
  32.  
  33. for (int row = 0; row != 2; ++row) {
  34. tA[row][x1] = A[row][key ? x1 : x2];
  35. tA[row][x2] = A[row][key ? x1 : x2];
  36. tb[row] = b[key? row : 1 - row];
  37. }
  38.  
  39.  
  40. tb[0] /= tA[0][0];
  41. tA[0][1] = tA[0][1] / tA[0][0];
  42. tA[0][0] = tA[0][0] / tA[0][0];
  43.  
  44. if (tA[1][0] != 0) {
  45. double k;
  46. k = tA[1][0] / tA[0][0];
  47. tA[1][0] -= k * tA[0][0];
  48. tA[1][1] -= k * tA[0][1];
  49. tb[1] -= tb[0] * k;
  50. }
  51.  
  52. if (tA[1][1] != 0) {
  53. double k;
  54.  
  55. tb[1] /= tA[1][1];
  56. tA[1][1] /= tA[1][1];
  57.  
  58. k = tA[0][1] / tA[1][1];
  59. tA[0][1] -= k * tA[1][1];
  60. tb[0] -= k * tb[1];
  61. }
  62.  
  63. std::cout << "x" << x1 << ": " << tb[0] << ", x" << x2 << ": " << tb[1] << std::endl;
  64.  
  65. } while(std::prev_permutation(bin_combinations.begin(), bin_combinations.end()));
  66.  
  67. return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement