# include iostream include vector using namespace std typedef vector dou

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157``` ```#include #include using namespace std; typedef vector Vec; typedef vector Matrix; int n; double h(Vec x, int k) { return x[k] - x[k - 1]; } double fff(Vec x, Vec y, int k) { return y[k - 1] / (x[k - 1] - x[k]) / (x[k - 1] - x[k + 1]) + y[k] / (x[k] - x[k - 1]) / (x[k] - x[k + 1]) + y[k + 1] / (x[k + 1] - x[k - 1]) / (x[k + 1] - x[k]); } double ff(Vec x, Vec y, int k) { return y[k - 1] / (x[k - 1] - x[k]) + y[k] / (x[k] - x[k - 1]); } void SolveTridiagonal(Vec A, Vec B, Vec C, Vec b, Vec &x) { int n = A.size(); Vec B1(n); Vec b1(n); B1[0] = B[0]; b1[0] = b[0]; for (int i = 1; i < n; ++i) { B1[i] = B[i] - A[i] * C[i - 1] / B1[i - 1]; b1[i] = b[i] - A[i] * b1[i - 1] / B1[i - 1]; } x.resize(n); x[n - 1] = b1[n - 1] / B1[n - 1]; for (int i = n - 2; i >= 0; --i) { x[i] = (b1[i] - C[i] * x[i + 1]) / B1[i]; } } void BuildSpline(Vec &knotX, Vec knotY, Matrix &Coef, double A, double B) { Vec a(n-2); Vec b(n-2); Vec c(n-2); Vec f(n - 2); for (int i = 2; i <= n - 1; ++i) { int id = i - 2; if (i != n - 1) c[id] = h(knotX, i - 1) / (h(knotX, i - 1) + h(knotX, i)); if (i != 2) B = h(knotX, i) / (h(knotX, i - 1) + h(knotX, i)); b[id] = 3; if (i == 2) f[id] = 4 * fff(knotX, knotY, i - 1) - A * h(knotX, 1) / 2 / (h(knotX, 1) + h(knotX, 2)); else if (i == n - 3) f[id] = 4 * fff(knotX, knotY, i - 1) - B* h(knotX, n - 1) / 2 / (h(knotX, n - 2) + h(knotX, n - 1)); else f[id] = 4 * fff(knotX, knotY, i - 1); } Vec x; SolveTridiagonal(a, b, c, f, x); Vec coeffA(n); Vec coeffB(n); Vec coeffC(n); Vec X(n); for (int i = 0; i < n - 1; ++i) X[i] = (knotX[i] + knotX[i + 1]) / 2; X[n-1] = knotX[n-1]; coeffC[n - 1] = B / 2; coeffC[0] = A / 2; for (int i = 1; i < n - 1; ++i) { coeffC[i] = x[i - 1]; } for (int i = 0; i < n - 1; ++i) { coeffB[i] = h(knotX,i+1) / 4 * (coeffC[i] - coeffC[i + 1]) + ff(knotX, knotY,i+1); } coeffB[n - 1] = coeffB[n - 2] + B / 2 * h(knotX, n - 1); for (int i = 0; i < n - 1; ++i) { coeffA[i] = h(knotX, i+1) / 2 * (coeffB[i] - h(knotX,i+1) / 2 * coeffC[i]) + knotY[i]; } coeffA[n - 1] = knotY[n - 1]; for (int j = 0; j < n; j++) { Coef[j][0] = coeffA[j]; Coef[j][1] = coeffB[j]; Coef[j][2] = coeffC[j]; } knotX = X; } double Interpolate(double x, Vec knotX, Matrix Coef) { int ind = 0; if (x <= knotX[0]) ind = 1; else if (x >= knotX[n - 1]) ind = n - 1; else { int i = 0, j = n - 1; while (i + 1 < j) { std::size_t k = i + (j - i) / 2; if (x <= knotX[k]) j = k; else i = k; } ind = j; } double dx = x - knotX[ind]; return Coef[ind][0] + Coef[ind][1] * dx + Coef[ind][2] * dx*dx; } int main() { freopen("output.txt", "w", stdout); Vec knotX = { -3.14, -2.44, -1.74, -1.04, -0.34, 0.36, 1.06, 1.76, 2.46 }; Vec knotY = { -0.0016, -0.6454, -0.9857, -0.8624, -0.3335, 0.3523, 0.8724, 0.9822, 0.6300 }; n = knotX.size(); Matrix Coef(n, Vec(3, 0)); BuildSpline(knotX, knotY, Coef, 0, 0); cout << "[ "; for (double x = -3.14; x <= 2.46; x += 0.1) { cout << x << " "; double temp = Interpolate(x, knotX, Coef); cout << temp << endl; } cout << " ]"; return 0; } ```