// File: j-faria[P]kima/src/RVmodel.cpp
// Function: RVmodel::calculate_C()
void RVmodel::calculate_C()
{
    // Get the data
    auto data = Data::get_instance();
    const vector<double>& t = data.get_t();
    const vector<double>& sig = data.get_sig();
    const vector<int>& obsi = data.get_obsi();
    int N = data.N();
    double jit;

    #if TIMING
    auto begin = std::chrono::high_resolution_clock::now();  // start timing
    #endif

    switch (kernel)
    {
    case standard:
        {
            /* This implements the "standard" quasi-periodic kernel, see R&W2006 */
            for(size_t i=0; i<N; i++)
            {
                for(size_t j=i; j<N; j++)
                {
                    C(i, j) = eta1*eta1*exp(-0.5*pow((t[i] - t[j])/eta2, 2)
                                -2.0*pow(sin(M_PI*(t[i] - t[j])/eta3)/eta4, 2) );

                    if(i==j)
                    {
                        if (multi_instrument)
                        {
                            jit = jitters[obsi[i]-1];
                            C(i, j) += sig[i]*sig[i] + jit*jit;
                        }
                        else
                        {
                            C(i, j) += sig[i]*sig[i] + extra_sigma*extra_sigma;
                        }
                    }
                    else
                    {
                        C(j, i) = C(i, j);
                    }
                }
            }

            break;
        }

    case celerite:
        {
            /*
            This implements a celerite quasi-periodic kernel devised by Andrew Collier Cameron,
            which satisfies k(tau=0)=amp and k'(tau=0)=0
            The kernel defined in the celerite paper (Eq 56 in Foreman-Mackey et al. 2017)
            does not satisfy k'(tau=0)=0
            This new kernel has only 3 parameters, eta1, eta2, eta3
            corresponding to an amplitude, decay timescale and period.
            It approximates the standard kernel with eta4=0.5
            */

            double wbeat, wrot, amp, c, d, x, a, b, e, f, g;
            wbeat = 1 / eta2;
            wrot = 2*M_PI/ eta3;
            amp = eta1*eta1;
            c = wbeat; d = wrot; x = c/d;
            a = amp/2; b = amp*x/2;
            e = amp/8; f = amp*x/4;
            g = amp*(3./8. + 0.001);

            VectorXd a_real, c_real, 
                    a_comp(3),
                    b_comp(3),
                    c_comp(3),
                    d_comp(3);
        
            // a_real is empty
            // c_real is empty
            a_comp << a, e, g;
            b_comp << b, f, 0.0;
            c_comp << c, c, c;
            d_comp << d, 2*d, 0.0;

            VectorXd yvar(t.size()), tt(t.size());
            for (int i = 0; i < t.size(); ++i){
                yvar(i) = sig[i] * sig[i];
                tt(i) = t[i];
            }

            solver.compute(
                extra_sigma*extra_sigma,
                a_real, c_real,
                a_comp, b_comp, c_comp, d_comp,
                tt, yvar  // Note: this is the measurement _variance_
            );

            break;
        }

    default:
        cout << "error: `kernel` should be 'standard' or 'celerite'" << endl;
        std::abort();
        break;
    }

    #if TIMING
    auto end = std::chrono::high_resolution_clock::now();
    cout << "GP build matrix: ";
    cout << std::chrono::duration_cast<std::chrono::nanoseconds>(end-begin).count();
    cout << " ns" << "\t"; // << std::endl;
    #endif
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x440, %rsp            # imm = 0x440
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x320(%rbp)
callq 0xab10
movq %rax, %rsi
leaq -0x110(%rbp), %rdi
movq %rdi, -0x318(%rbp)
callq 0x269d0
movq -0x318(%rbp), %rdi
callq 0x271b0
movq %rax, -0x310(%rbp)
jmp 0x1f70a
movq -0x310(%rbp), %rax
movq %rax, -0x118(%rbp)
leaq -0x110(%rbp), %rdi
callq 0x271c0
movq %rax, -0x328(%rbp)
jmp 0x1f72d
movq -0x328(%rbp), %rax
movq %rax, -0x130(%rbp)
leaq -0x110(%rbp), %rdi
callq 0x271e0
movq %rax, -0x330(%rbp)
jmp 0x1f750
movq -0x330(%rbp), %rax
movq %rax, -0x138(%rbp)
leaq -0x110(%rbp), %rdi
callq 0x27200
movl %eax, -0x334(%rbp)
jmp 0x1f772
movq -0x320(%rbp), %rax
movl -0x334(%rbp), %ecx
movl %ecx, -0x13c(%rbp)
movl 0x1a8(%rax), %eax
movl %eax, -0x338(%rbp)
testl %eax, %eax
je 0x1f7c2
jmp 0x1f797
movl -0x338(%rbp), %eax
subl $0x1, %eax
je 0x1fbf5
jmp 0x20330
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x120(%rbp)
movl %eax, -0x124(%rbp)
jmp 0x2037b
movq $0x0, -0x150(%rbp)
movq -0x150(%rbp), %rax
movslq -0x13c(%rbp), %rcx
cmpq %rcx, %rax
jae 0x1fbf0
movq -0x150(%rbp), %rax
movq %rax, -0x158(%rbp)
movq -0x158(%rbp), %rax
movslq -0x13c(%rbp), %rcx
cmpq %rcx, %rax
jae 0x1fbd7
movq -0x320(%rbp), %rax
movsd 0x1b0(%rax), %xmm0
mulsd %xmm0, %xmm0
movsd %xmm0, -0x350(%rbp)
movq -0x118(%rbp), %rdi
movq -0x150(%rbp), %rsi
callq 0xa6a0
movsd (%rax), %xmm0
movsd %xmm0, -0x348(%rbp)
movq -0x118(%rbp), %rdi
movq -0x158(%rbp), %rsi
callq 0xa6a0
movsd -0x348(%rbp), %xmm0
movq %rax, %rcx
movq -0x320(%rbp), %rax
movsd (%rcx), %xmm1
subsd %xmm1, %xmm0
movsd 0x1b8(%rax), %xmm1
divsd %xmm1, %xmm0
movl $0x2, %edi
callq 0x16850
movsd %xmm0, -0x340(%rbp)
jmp 0x1f890
movq -0x118(%rbp), %rdi
movq -0x150(%rbp), %rsi
callq 0xa6a0
movsd (%rax), %xmm0
movsd %xmm0, -0x360(%rbp)
movq -0x118(%rbp), %rdi
movq -0x158(%rbp), %rsi
callq 0xa6a0
movsd -0x360(%rbp), %xmm0
movq %rax, %rcx
movq -0x320(%rbp), %rax
movsd (%rcx), %xmm1
subsd %xmm1, %xmm0
movsd 0x97ddc(%rip), %xmm1    # 0xb76c0
mulsd %xmm1, %xmm0
movsd 0x1c0(%rax), %xmm1
divsd %xmm1, %xmm0
callq 0x60e0
movq -0x320(%rbp), %rax
movsd 0x1c8(%rax), %xmm1
divsd %xmm1, %xmm0
movl $0x2, %edi
callq 0x16850
movsd %xmm0, -0x358(%rbp)
jmp 0x1f920
movsd -0x340(%rbp), %xmm0
movsd -0x358(%rbp), %xmm1
addsd %xmm1, %xmm1
movsd 0x976fc(%rip), %xmm2    # 0xb7038
mulsd %xmm2, %xmm0
subsd %xmm1, %xmm0
callq 0x6870
movq -0x320(%rbp), %rdi
movaps %xmm0, %xmm1
movsd -0x350(%rbp), %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, -0x370(%rbp)
addq $0x398, %rdi            # imm = 0x398
movq -0x150(%rbp), %rsi
movq -0x158(%rbp), %rdx
callq 0x27220
movq %rax, -0x368(%rbp)
jmp 0x1f98a
movq -0x368(%rbp), %rax
movsd -0x370(%rbp), %xmm0
movsd %xmm0, (%rax)
movq -0x150(%rbp), %rax
cmpq -0x158(%rbp), %rax
jne 0x1fb44
leaq 0xa5ff0(%rip), %rax     # 0xc59a8
testb $0x1, (%rax)
je 0x1faa7
movq -0x320(%rbp), %rax
addq $0x140, %rax            # imm = 0x140
movq %rax, -0x390(%rbp)
movq -0x138(%rbp), %rdi
movq -0x150(%rbp), %rsi
callq 0x166f0
movq -0x390(%rbp), %rdi
movl (%rax), %eax
decl %eax
movslq %eax, %rsi
callq 0xc6a0
movsd (%rax), %xmm0
movsd %xmm0, -0x148(%rbp)
movq -0x130(%rbp), %rdi
movq -0x150(%rbp), %rsi
callq 0xa6a0
movsd (%rax), %xmm0
movsd %xmm0, -0x388(%rbp)
movq -0x130(%rbp), %rdi
movq -0x150(%rbp), %rsi
callq 0xa6a0
movsd -0x388(%rbp), %xmm0
movq -0x320(%rbp), %rdi
movsd (%rax), %xmm2
movsd -0x148(%rbp), %xmm1
mulsd %xmm1, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x380(%rbp)
addq $0x398, %rdi            # imm = 0x398
movq -0x150(%rbp), %rsi
movq -0x158(%rbp), %rdx
callq 0x27220
movq %rax, -0x378(%rbp)
jmp 0x1fa8b
movq -0x378(%rbp), %rax
movsd -0x380(%rbp), %xmm0
addsd (%rax), %xmm0
movsd %xmm0, (%rax)
jmp 0x1fb42
movq -0x130(%rbp), %rdi
movq -0x150(%rbp), %rsi
callq 0xa6a0
movsd (%rax), %xmm0
movsd %xmm0, -0x3a8(%rbp)
movq -0x130(%rbp), %rdi
movq -0x150(%rbp), %rsi
callq 0xa6a0
movsd -0x3a8(%rbp), %xmm0
movq -0x320(%rbp), %rdi
movsd (%rax), %xmm2
movsd 0x198(%rdi), %xmm1
mulsd %xmm1, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x3a0(%rbp)
addq $0x398, %rdi            # imm = 0x398
movq -0x150(%rbp), %rsi
movq -0x158(%rbp), %rdx
callq 0x27220
movq %rax, -0x398(%rbp)
jmp 0x1fb2b
movq -0x398(%rbp), %rax
movsd -0x3a0(%rbp), %xmm0
addsd (%rax), %xmm0
movsd %xmm0, (%rax)
jmp 0x1fbbe
movq -0x320(%rbp), %rdi
addq $0x398, %rdi            # imm = 0x398
movq -0x150(%rbp), %rsi
movq -0x158(%rbp), %rdx
callq 0x27220
movq %rax, -0x3b0(%rbp)
jmp 0x1fb6e
movq -0x320(%rbp), %rdi
movq -0x3b0(%rbp), %rax
movsd (%rax), %xmm0
movsd %xmm0, -0x3c0(%rbp)
addq $0x398, %rdi            # imm = 0x398
movq -0x158(%rbp), %rsi
movq -0x150(%rbp), %rdx
callq 0x27220
movq %rax, -0x3b8(%rbp)
jmp 0x1fbab
movq -0x3b8(%rbp), %rax
movsd -0x3c0(%rbp), %xmm0
movsd %xmm0, (%rax)
jmp 0x1fbc0
movq -0x158(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x158(%rbp)
jmp 0x1f7f2
jmp 0x1fbd9
movq -0x150(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x150(%rbp)
jmp 0x1f7cd
jmp 0x20366
movq -0x320(%rbp), %rax
movsd 0x1b8(%rax), %xmm1
movsd 0x97424(%rip), %xmm0    # 0xb7030
divsd %xmm1, %xmm0
movsd %xmm0, -0x160(%rbp)
movsd 0x1c0(%rax), %xmm1
movsd 0x97530(%rip), %xmm0    # 0xb7158
divsd %xmm1, %xmm0
movsd %xmm0, -0x168(%rbp)
movsd 0x1b0(%rax), %xmm0
mulsd %xmm0, %xmm0
movsd %xmm0, -0x170(%rbp)
movsd -0x160(%rbp), %xmm0
movsd %xmm0, -0x178(%rbp)
movsd -0x168(%rbp), %xmm0
movsd %xmm0, -0x180(%rbp)
movsd -0x178(%rbp), %xmm0
movsd -0x180(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x188(%rbp)
movsd -0x170(%rbp), %xmm0
movsd 0x97504(%rip), %xmm1    # 0xb7198
mulsd %xmm1, %xmm0
movsd %xmm0, -0x190(%rbp)
movsd -0x170(%rbp), %xmm0
movsd -0x188(%rbp), %xmm2
mulsd %xmm2, %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, -0x198(%rbp)
movsd -0x170(%rbp), %xmm0
movsd 0x979d8(%rip), %xmm1    # 0xb76a8
mulsd %xmm1, %xmm0
movsd %xmm0, -0x1a0(%rbp)
movsd -0x170(%rbp), %xmm0
movsd -0x188(%rbp), %xmm1
mulsd %xmm1, %xmm0
movsd 0x979b8(%rip), %xmm1    # 0xb76b0
mulsd %xmm1, %xmm0
movsd %xmm0, -0x1a8(%rbp)
movsd -0x170(%rbp), %xmm0
movsd 0x979a4(%rip), %xmm1    # 0xb76b8
mulsd %xmm1, %xmm0
movsd %xmm0, -0x1b0(%rbp)
leaq -0x1c0(%rbp), %rdi
callq 0x272e0
jmp 0x1fd2e
leaq -0x1d0(%rbp), %rdi
callq 0x272e0
jmp 0x1fd3c
movl $0x3, -0x1e4(%rbp)
leaq -0x1e0(%rbp), %rdi
leaq -0x1e4(%rbp), %rsi
callq 0x27330
jmp 0x1fd5b
movl $0x3, -0x1fc(%rbp)
leaq -0x1f8(%rbp), %rdi
leaq -0x1fc(%rbp), %rsi
callq 0x27330
jmp 0x1fd7a
movl $0x3, -0x214(%rbp)
leaq -0x210(%rbp), %rdi
leaq -0x214(%rbp), %rsi
callq 0x27330
jmp 0x1fd99
movl $0x3, -0x22c(%rbp)
leaq -0x228(%rbp), %rdi
leaq -0x22c(%rbp), %rsi
callq 0x27330
jmp 0x1fdb8
leaq -0x250(%rbp), %rdi
leaq -0x1e0(%rbp), %rsi
leaq -0x190(%rbp), %rdx
callq 0x27390
jmp 0x1fdd4
leaq -0x250(%rbp), %rdi
leaq -0x1a0(%rbp), %rsi
callq 0x273d0
movq %rax, -0x3c8(%rbp)
jmp 0x1fdf0
movq -0x3c8(%rbp), %rdi
leaq -0x1b0(%rbp), %rsi
callq 0x273d0
jmp 0x1fe05
leaq -0x250(%rbp), %rdi
callq 0x27540
leaq -0x270(%rbp), %rdi
leaq -0x1f8(%rbp), %rsi
leaq -0x198(%rbp), %rdx
callq 0x27390
jmp 0x1fe2d
leaq -0x270(%rbp), %rdi
leaq -0x1a8(%rbp), %rsi
callq 0x273d0
movq %rax, -0x3d0(%rbp)
jmp 0x1fe49
movq -0x3d0(%rbp), %rdi
movq $0x0, -0x278(%rbp)
leaq -0x278(%rbp), %rsi
callq 0x273d0
jmp 0x1fe69
leaq -0x270(%rbp), %rdi
callq 0x27540
leaq -0x298(%rbp), %rdi
leaq -0x210(%rbp), %rsi
leaq -0x178(%rbp), %rdx
callq 0x27390
jmp 0x1fe91
leaq -0x298(%rbp), %rdi
leaq -0x178(%rbp), %rsi
callq 0x273d0
movq %rax, -0x3d8(%rbp)
jmp 0x1fead
movq -0x3d8(%rbp), %rdi
leaq -0x178(%rbp), %rsi
callq 0x273d0
jmp 0x1fec2
leaq -0x298(%rbp), %rdi
callq 0x27540
leaq -0x2b8(%rbp), %rdi
leaq -0x228(%rbp), %rsi
leaq -0x180(%rbp), %rdx
callq 0x27390
jmp 0x1feea
movsd -0x180(%rbp), %xmm0
addsd %xmm0, %xmm0
movsd %xmm0, -0x2c0(%rbp)
leaq -0x2b8(%rbp), %rdi
leaq -0x2c0(%rbp), %rsi
callq 0x273d0
movq %rax, -0x3e0(%rbp)
jmp 0x1ff1a
movq -0x3e0(%rbp), %rdi
movq $0x0, -0x2c8(%rbp)
leaq -0x2c8(%rbp), %rsi
callq 0x273d0
jmp 0x1ff3a
leaq -0x2b8(%rbp), %rdi
callq 0x27540
movq -0x118(%rbp), %rdi
callq 0x7c90
movq %rax, -0x2e0(%rbp)
leaq -0x2d8(%rbp), %rdi
leaq -0x2e0(%rbp), %rsi
callq 0x27570
jmp 0x1ff6e
movq -0x118(%rbp), %rdi
callq 0x7c90
movq %rax, -0x2f8(%rbp)
leaq -0x2f0(%rbp), %rdi
leaq -0x2f8(%rbp), %rsi
callq 0x27570
jmp 0x1ff96
movl $0x0, -0x2fc(%rbp)
movslq -0x2fc(%rbp), %rax
movq %rax, -0x3e8(%rbp)
movq -0x118(%rbp), %rdi
callq 0x7c90
movq %rax, %rcx
movq -0x3e8(%rbp), %rax
cmpq %rcx, %rax
jae 0x201f8
movq -0x130(%rbp), %rdi
movslq -0x2fc(%rbp), %rsi
callq 0xa6a0
movsd (%rax), %xmm0
movsd %xmm0, -0x400(%rbp)
movq -0x130(%rbp), %rdi
movslq -0x2fc(%rbp), %rsi
callq 0xa6a0
movsd -0x400(%rbp), %xmm0
movsd (%rax), %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, -0x3f8(%rbp)
movslq -0x2fc(%rbp), %rsi
leaq -0x2d8(%rbp), %rdi
callq 0x275d0
movq %rax, -0x3f0(%rbp)
jmp 0x20033
movq -0x3f0(%rbp), %rax
movsd -0x3f8(%rbp), %xmm0
movsd %xmm0, (%rax)
movq -0x118(%rbp), %rdi
movslq -0x2fc(%rbp), %rsi
callq 0xa6a0
movsd (%rax), %xmm0
movsd %xmm0, -0x410(%rbp)
movslq -0x2fc(%rbp), %rsi
leaq -0x2f0(%rbp), %rdi
callq 0x275d0
movq %rax, -0x408(%rbp)
jmp 0x20081
movq -0x408(%rbp), %rax
movsd -0x410(%rbp), %xmm0
movsd %xmm0, (%rax)
movl -0x2fc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2fc(%rbp)
jmp 0x1ffa0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x120(%rbp)
movl %eax, -0x124(%rbp)
jmp 0x20322
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x120(%rbp)
movl %eax, -0x124(%rbp)
jmp 0x20316
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x120(%rbp)
movl %eax, -0x124(%rbp)
jmp 0x2030a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x120(%rbp)
movl %eax, -0x124(%rbp)
jmp 0x202fe
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x120(%rbp)
movl %eax, -0x124(%rbp)
jmp 0x202f2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x120(%rbp)
movl %eax, -0x124(%rbp)
jmp 0x202e6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x120(%rbp)
movl %eax, -0x124(%rbp)
leaq -0x250(%rbp), %rdi
callq 0x27540
jmp 0x202e6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x120(%rbp)
movl %eax, -0x124(%rbp)
leaq -0x270(%rbp), %rdi
callq 0x27540
jmp 0x202e6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x120(%rbp)
movl %eax, -0x124(%rbp)
leaq -0x298(%rbp), %rdi
callq 0x27540
jmp 0x202e6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x120(%rbp)
movl %eax, -0x124(%rbp)
leaq -0x2b8(%rbp), %rdi
callq 0x27540
jmp 0x202e6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x120(%rbp)
movl %eax, -0x124(%rbp)
jmp 0x202da
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x120(%rbp)
movl %eax, -0x124(%rbp)
leaq -0x2f0(%rbp), %rdi
callq 0x278c0
jmp 0x202da
movq -0x320(%rbp), %rax
movq %rax, %rdi
addq $0x220, %rdi            # imm = 0x220
movsd 0x198(%rax), %xmm0
mulsd %xmm0, %xmm0
movsd %xmm0, -0x308(%rbp)
movq %rsp, %rax
leaq -0x2d8(%rbp), %rcx
movq %rcx, 0x18(%rax)
leaq -0x2f0(%rbp), %rcx
movq %rcx, 0x10(%rax)
leaq -0x228(%rbp), %rcx
movq %rcx, 0x8(%rax)
leaq -0x210(%rbp), %rcx
movq %rcx, (%rax)
leaq -0x308(%rbp), %rsi
leaq -0x1c0(%rbp), %rdx
leaq -0x1d0(%rbp), %rcx
leaq -0x1e0(%rbp), %r8
leaq -0x1f8(%rbp), %r9
callq 0x27660
jmp 0x20275
leaq -0x2f0(%rbp), %rdi
callq 0x278c0
leaq -0x2d8(%rbp), %rdi
callq 0x278c0
leaq -0x228(%rbp), %rdi
callq 0x278c0
leaq -0x210(%rbp), %rdi
callq 0x278c0
leaq -0x1f8(%rbp), %rdi
callq 0x278c0
leaq -0x1e0(%rbp), %rdi
callq 0x278c0
leaq -0x1d0(%rbp), %rdi
callq 0x278c0
leaq -0x1c0(%rbp), %rdi
callq 0x278c0
jmp 0x20366
leaq -0x2d8(%rbp), %rdi
callq 0x278c0
leaq -0x228(%rbp), %rdi
callq 0x278c0
leaq -0x210(%rbp), %rdi
callq 0x278c0
leaq -0x1f8(%rbp), %rdi
callq 0x278c0
leaq -0x1e0(%rbp), %rdi
callq 0x278c0
leaq -0x1d0(%rbp), %rdi
callq 0x278c0
leaq -0x1c0(%rbp), %rdi
callq 0x278c0
jmp 0x2037b
movq 0xf9c61(%rip), %rdi     # 0x119f98
leaq 0x9771f(%rip), %rsi     # 0xb7a5d
callq 0x64f0
movq %rax, -0x418(%rbp)
jmp 0x2034c
movq -0x418(%rbp), %rdi
movq 0xf9bbe(%rip), %rsi     # 0x119f18
callq 0x6530
jmp 0x20361
callq 0x62a0
leaq -0x110(%rbp), %rdi
callq 0x155c0
addq $0x440, %rsp            # imm = 0x440
popq %rbp
retq
leaq -0x110(%rbp), %rdi
callq 0x155c0
movq -0x120(%rbp), %rdi
callq 0x6830
nopw %cs:(%rax,%rax)