#include #include #include #include #include #include #include using Real = double; using Complex = std::complex; using namespace std::complex_literals; inline Real fP(unsigned p, Real q) { return 0.5 * pow(q, p); } inline Real dfP(unsigned p, Real q) { return 0.5 * p * pow(q, p - 1); } inline Real ddfP(unsigned p, Real q) { return 0.5 * p * (p - 1) * pow(q, p - 2); } inline Real f(Real λ, unsigned p, unsigned s, Real q) { return (1 - λ) * fP(p, q) + λ * fP(s, q); } inline Real df(Real λ, unsigned p, unsigned s, Real q) { return (1 - λ) * dfP(p, q) + λ * dfP(s, q); } inline Real ddf(Real λ, unsigned p, unsigned s, Real q) { return (1 - λ) * ddfP(p, q) + λ * ddfP(s, q); } class FourierTransform { private: std::vector a; std::vector â; fftw_plan plan_r2c; fftw_plan plan_c2r; Real Δω; Real Δτ; public: FourierTransform(unsigned n, Real Δω, Real Δτ) : a(2 * n), â(n + 1), Δω(Δω), Δτ(Δτ) { plan_r2c = fftw_plan_dft_r2c_1d(2 * n, a.data(), reinterpret_cast(â.data()), 0); plan_c2r = fftw_plan_dft_c2r_1d(2 * n, reinterpret_cast(â.data()), a.data(), 0); } ~FourierTransform() { fftw_destroy_plan(plan_r2c); fftw_destroy_plan(plan_c2r); fftw_cleanup(); } std::vector fourier(const std::vector& c) { a = c; fftw_execute(plan_r2c); std::vector ĉ(â.size()); for (unsigned i = 0; i < â.size(); i++) { ĉ[i] = â[i] * (Δτ * M_PI); } return ĉ; } std::vector inverse(const std::vector& ĉ) { â = ĉ; fftw_execute(plan_c2r); std::vector c(a.size()); for (unsigned i = 0; i < a.size(); i++) { c[i] = a[i] * (Δω / (2 * M_PI)); } return c; } }; int main(int argc, char* argv[]) { unsigned p = 2; unsigned s = 2; Real λ = 0.5; Real τ₀ = 0; Real y₀ = 0; Real yₘₐₓ = 0.5; Real Δy = 0.05; unsigned log2n = 8; Real τₘₐₓ = 20; int opt; while ((opt = getopt(argc, argv, "p:s:2:T:t:0:y:d:")) != -1) { switch (opt) { case 'p': p = atoi(optarg); break; case 's': s = atoi(optarg); break; case '2': log2n = atoi(optarg); break; case 'T': τₘₐₓ = atof(optarg); break; case 't': τ₀ = atof(optarg); break; case '0': y₀ = atof(optarg); break; case 'y': yₘₐₓ = atof(optarg); break; case 'd': Δy = atof(optarg); break; default: exit(1); } } unsigned n = pow(2, log2n); Real Δτ = τₘₐₓ / M_PI / n; Real Δω = M_PI / τₘₐₓ; Real y = y₀; FourierTransform fft(n, Δω, Δτ); while (y += Δy, y <= yₘₐₓ) { std::vector C(2 * n); std::vector R(2 * n); std::string file_end = std::to_string(p) + "_" + std::to_string(s) + "_" + std::to_string(λ) + "_" + std::to_string(τ₀) + "_" + std::to_string(y) + "_" + std::to_string(log2n) + "_" + std::to_string(τₘₐₓ) + ".dat"; std::ifstream cfile("C_"+file_end, std::ios::binary); cfile.read((char*)(C.data()), C.size() * sizeof(Real)); cfile.close(); std::ifstream rfile("R_"+file_end, std::ios::binary); rfile.read((char*)(R.data()), R.size() * sizeof(Real)); rfile.close(); Real energy = 0; for (unsigned i = 0; i < n; i++) { energy += y * R[i] * df(λ, p, s, C[i]) * M_PI * Δτ; } std::vector Ct = fft.fourier(C); std::cerr << y << " " << energy << " " << Ct[0].real() << std::endl; } return 0; }