#define EPS 0.000001 #include <stdio.h> #include <math.h> int Jacobi(double matrix[][5], double vec[][5], int maxt, int n) { int it, p, q, i, j; // 函数返回值 double temp, t, cn, sn, max_element, vip, viq, aip, aiq, apj, aqj; // 临时变量 for (it = 0; it < maxt; ++it) { max_element = 0; for (p = 0; p < n-1; ++p) { for (q = p + 1; q < n; ++q) { if (fabs(matrix[p][q]) > max_element) // 记录非对角线元素最大值 max_element = fabs(matrix[p][q]); if (fabs(matrix[p][q]) > EPS) // 非对角线元素非0时才执行Jacobi变换 { // 计算Givens旋转矩阵的重要元素:cos(theta), 即cn, sin(theta), 即sn temp = (matrix[q][q] - matrix[p][p]) / matrix[p][q] / 2.0; if (temp >= 0) // t为方程 t^2 + 2*t*temp - 1 = 0的根, 取绝对值较小的根为t t = -temp + sqrt(1 + temp * temp); else t = -temp - sqrt(1 + temp * temp); cn = 1 / sqrt(1