#include <stdio.h>
#include <math.h>

#define N 4         
#define EPS 1e-6    
#define MAX_ITER 100

void print_matrix(double mat[N][N]) {
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            printf("%7.4f ", mat[i][j]);
        }
        printf("\n");
    }
}

int main() {
    double A[N][N] = {
        {5.0, 4.0, 1.0, 1.0},
        {4.0, 5.0, 1.0, 1.0},
        {1.0, 1.0, 4.0, 2.0},
        {1.0, 1.0, 2.0, 4.0}
    };

    double A_orig[N][N];
    for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++)
            A_orig[i][j] = A[i][j];

    double P[N][N] = {0};
    for (int i = 0; i < N; i++) P[i][i] = 1.0;

    int iter = 0;
    
    // Clean Tabled Header for Convergence History [cite: 13]
    printf("=== CONVERGENCE HISTORY ===\n");
    printf("%-5s   %-16s   %-8s\n", "Iter", "Max Off-Diagonal", "Position");
    printf("---------------------------------------\n");

    while (iter < MAX_ITER) {
        int p = 0, q = 1;
        double max_val = fabs(A[0][1]);
        for (int i = 0; i < N; i++) {
            for (int j = i + 1; j < N; j++) {
                if (fabs(A[i][j]) > max_val) {
                    max_val = fabs(A[i][j]);
                    p = i;
                    q = j;
                }
            }
        }

        // Tidy row output alignment
        printf("%-5d   %-16.6f   A[%d][%d]\n", iter, max_val, p, q);

        if (max_val < EPS) {
            printf("---------------------------------------\n");
            printf("Status: Successfully converged.\n\n");
            break;
        }

        double phi, cos_t, sin_t;
        if (fabs(A[p][p] - A[q][q]) < 1e-12) {
            phi = acos(-1.0) / 4.0; 
        } else {
            phi = 0.5 * atan2(2.0 * A[p][q], A[p][p] - A[q][q]);
        }
        cos_t = cos(phi);
        sin_t = sin(phi);

        double Ap_old = A[p][p];
        double Aq_old = A[q][q];
        
        A[p][p] = Ap_old * cos_t * cos_t + Aq_old * sin_t * sin_t + 2.0 * A[p][q] * sin_t * cos_t;
        A[q][q] = Ap_old * sin_t * sin_t + Aq_old * cos_t * cos_t - 2.0 * A[p][q] * sin_t * cos_t;
        A[p][q] = A[q][p] = 0.0; 

        for (int i = 0; i < N; i++) {
            if (i != p && i != q) {
                double a_ip = A[i][p];
                double a_iq = A[i][q];
                A[i][p] = A[p][i] = a_ip * cos_t + a_iq * sin_t;
                A[i][q] = A[q][i] = -a_ip * sin_t + a_iq * cos_t;
            }
        }

        for (int i = 0; i < N; i++) {
            double p_ip = P[i][p];
            double p_iq = P[i][q];
            P[i][p] = p_ip * cos_t + p_iq * sin_t;
            P[i][q] = -p_ip * sin_t + p_iq * cos_t;
        }

        iter++;
    }

    // === Output Final Results ===
    printf("=== EIGENVALUES & EIGENVECTORS ===\n");
    for (int j = 0; j < N; j++) {
        printf("Eigenvalue %d: %.4f\n", j + 1, A[j][j]);
        printf("Eigenvector %d:\n", j + 1);
        for (int i = 0; i < N; i++) {
            printf("  %7.4f\n", P[i][j]); 
        }
        printf("\n");
    }

    // === Automated Verification Step ===
    printf("=== CODE-LEVEL VERIFICATION (Ax = lambda * x) ===\n");
    
    int all_passed = 1;
    double tolerance = 1e-5; // Threshold for acceptable math verification error

    for (int j = 0; j < N; j++) {
        double lambda = A[j][j];
        
        for (int i = 0; i < N; i++) {
            double Ax_i = 0.0;
            for (int k = 0; k < N; k++) {
                Ax_i += A_orig[i][k] * P[k][j];
            }
            double lambda_x_i = lambda * P[i][j];
            
            // Programmatically verify the absolute difference
            if (fabs(Ax_i - lambda_x_i) > tolerance) {
                all_passed = 0;
            }
        }
    }

    if (all_passed) {
        printf("Verification Status: PASSED (All equations hold within error tolerances)\n");
    } else {
        printf("Verification Status: FAILED (Discrepancy detected)\n");
    }

    return 0;
}
