greannmhar

computeResiduals

Nov 5th, 2025
8
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. #include <cmath>
  2.  
  3. void computeResiduals(int n, double *A, double *b, double *x, double &r1, double &r2) {
  4. double norm_b = 0.0;
  5. for (int i = 0; i < n; i++) {
  6. norm_b += std::fabs(b[i]);
  7. }
  8.  
  9. double norm_residual = 0.0;
  10. for (int i = 0; i < n; i++) {
  11. double Ax = 0.0;
  12. for (int j = 0; j < n; j++) {
  13. Ax += A[i * n + j] * x[j];
  14. }
  15. norm_residual += std::fabs(Ax - b[i]);
  16. }
  17. r1 = norm_residual / norm_b;
  18.  
  19. double norm_x_hat = 0.0;
  20. double norm_diff = 0.0;
  21. for (int i = 0; i < n; i++) {
  22. double x_hat_i = (i + 1) % 2;
  23. norm_x_hat += std::fabs(x_hat_i);
  24. norm_diff += std::fabs(x[i] - x_hat_i);
  25. }
  26. r2 = norm_diff / norm_x_hat;
  27. }
Advertisement
Add Comment
Please, Sign In to add comment