Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <cmath>
- #include <cstring>
- #include <chrono>
- #include <cstdlib>
- void initializeMatrix(int s, int n, double *A, const char *filename);
- void computeVectorB(int n, double *A, double *b);
- int gauss(int n, double *A, double *b, double *x);
- void printMatrix(int l, int n, double *A, int r);
- void computeResiduals(int n, double *A, double *b, double *x, double &r1, double &r2);
- int main(int argc, char *argv[]) {
- if (argc < 4) {
- std::cerr << "Usage: " << argv[0] << " n r s [filename]" << std::endl;
- return 1;
- }
- int n = std::atoi(argv[1]);
- int r = std::atoi(argv[2]);
- int s = std::atoi(argv[3]);
- const char *filename = (s == 0 && argc > 4) ? argv[4] : nullptr;
- double *A = new double[n * n];
- double *b = new double[n];
- double *x = new double[n];
- initializeMatrix(s, n, A, filename);
- computeVectorB(n, A, b);
- std::cout << "Initial matrix A:" << std::endl;
- printMatrix(n, n, A, r);
- auto start = std::chrono::high_resolution_clock::now();
- int result = gauss(n, A, b, x);
- auto end = std::chrono::high_resolution_clock::now();
- double t1 = std::chrono::duration<double>(end - start).count();
- double r1 = -1.0;
- double r2 = -1.0;
- double t2 = 0.0;
- if (result == 0) {
- initializeMatrix(s, n, A, filename);
- computeVectorB(n, A, b);
- start = std::chrono::high_resolution_clock::now();
- computeResiduals(n, A, b, x, r1, r2);
- end = std::chrono::high_resolution_clock::now();
- t2 = std::chrono::duration<double>(end - start).count();
- std::cout << "Solution x:" << std::endl;
- printMatrix(1, n, x, r);
- }
- printf("%s : Task = %d Res1 = %e Res2 = %e T1 = %.2f T2 = %.2f S = %d N = %d\n",
- argv[0], 1, r1, r2, t1, t2, s, n);
- delete[] A;
- delete[] b;
- delete[] x;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment