Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cmath>
- void computeResiduals(int n, double *A, double *b, double *x, double &r1, double &r2) {
- double norm_b = 0.0;
- for (int i = 0; i < n; i++) {
- norm_b += std::fabs(b[i]);
- }
- double norm_residual = 0.0;
- for (int i = 0; i < n; i++) {
- double Ax = 0.0;
- for (int j = 0; j < n; j++) {
- Ax += A[i * n + j] * x[j];
- }
- norm_residual += std::fabs(Ax - b[i]);
- }
- r1 = norm_residual / norm_b;
- double norm_x_hat = 0.0;
- double norm_diff = 0.0;
- for (int i = 0; i < n; i++) {
- double x_hat_i = (i + 1) % 2;
- norm_x_hat += std::fabs(x_hat_i);
- norm_diff += std::fabs(x[i] - x_hat_i);
- }
- r2 = norm_diff / norm_x_hat;
- }
Advertisement
Add Comment
Please, Sign In to add comment