greannmhar

main

Nov 5th, 2025
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cmath>
  4. #include <cstring>
  5. #include <chrono>
  6. #include <cstdlib>
  7.  
  8. void initializeMatrix(int s, int n, double *A, const char *filename);
  9. void computeVectorB(int n, double *A, double *b);
  10. int gauss(int n, double *A, double *b, double *x);
  11. void printMatrix(int l, int n, double *A, int r);
  12. void computeResiduals(int n, double *A, double *b, double *x, double &r1, double &r2);
  13.  
  14. int main(int argc, char *argv[]) {
  15. if (argc < 4) {
  16. std::cerr << "Usage: " << argv[0] << " n r s [filename]" << std::endl;
  17. return 1;
  18. }
  19.  
  20. int n = std::atoi(argv[1]);
  21. int r = std::atoi(argv[2]);
  22. int s = std::atoi(argv[3]);
  23. const char *filename = (s == 0 && argc > 4) ? argv[4] : nullptr;
  24.  
  25. double *A = new double[n * n];
  26. double *b = new double[n];
  27. double *x = new double[n];
  28.  
  29. initializeMatrix(s, n, A, filename);
  30. computeVectorB(n, A, b);
  31.  
  32. std::cout << "Initial matrix A:" << std::endl;
  33. printMatrix(n, n, A, r);
  34.  
  35. auto start = std::chrono::high_resolution_clock::now();
  36. int result = gauss(n, A, b, x);
  37. auto end = std::chrono::high_resolution_clock::now();
  38. double t1 = std::chrono::duration<double>(end - start).count();
  39.  
  40. double r1 = -1.0;
  41. double r2 = -1.0;
  42. double t2 = 0.0;
  43.  
  44. if (result == 0) {
  45. initializeMatrix(s, n, A, filename);
  46. computeVectorB(n, A, b);
  47. start = std::chrono::high_resolution_clock::now();
  48. computeResiduals(n, A, b, x, r1, r2);
  49. end = std::chrono::high_resolution_clock::now();
  50. t2 = std::chrono::duration<double>(end - start).count();
  51.  
  52. std::cout << "Solution x:" << std::endl;
  53. printMatrix(1, n, x, r);
  54. }
  55.  
  56. printf("%s : Task = %d Res1 = %e Res2 = %e T1 = %.2f T2 = %.2f S = %d N = %d\n",
  57. argv[0], 1, r1, r2, t1, t2, s, n);
  58.  
  59. delete[] A;
  60. delete[] b;
  61. delete[] x;
  62.  
  63. return 0;
  64. }
  65.  
Advertisement
Add Comment
Please, Sign In to add comment