// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x208, %rsp            # imm = 0x208
movq %rdi, %rbx
leaq 0x565a3(%rip), %rsi     # 0x64580
leaq 0x100(%rsp), %rdi
callq 0x11aa2
movl 0x1a8(%rbx), %eax
cmpl $0x1, %eax
je 0xe1d7
testl %eax, %eax
jne 0xe5d7
movq 0x108(%rsp), %rax
subq 0x100(%rsp), %rax
shrq $0x3, %rax
cltq
movq %rax, 0x80(%rsp)
leaq 0x398(%rbx), %r14
leaq 0x38fe7(%rip), %rax     # 0x47014
movb (%rax), %al
movb %al, 0x90(%rsp)
xorl %r15d, %r15d
movq %r15, %r13
movq 0x80(%rsp), %rax
cmpq %r15, %rax
cmovaq %rax, %r13
je 0xe5b8
xorl %ebp, %ebp
movq %r15, %r12
cmpq %r12, %r13
je 0xe1cf
movq 0x100(%rsp), %rax
movsd (%rax,%r15,8), %xmm0
subsd (%rax,%r12,8), %xmm0
movsd 0x1b0(%rbx), %xmm1
movsd %xmm1, 0x18(%rsp)
movapd %xmm0, %xmm1
divsd 0x1b8(%rbx), %xmm1
mulsd %xmm1, %xmm1
movsd %xmm1, (%rsp)
mulsd 0x2e5ea(%rip), %xmm0    # 0x3c688
divsd 0x1c0(%rbx), %xmm0
callq 0x60b0
divsd 0x1c8(%rbx), %xmm0
mulsd %xmm0, %xmm0
movsd (%rsp), %xmm1
mulsd 0x2df74(%rip), %xmm1    # 0x3c038
addsd %xmm0, %xmm0
subsd %xmm0, %xmm1
movapd %xmm1, %xmm0
callq 0x6700
movsd %xmm0, (%rsp)
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x11dc0
movsd 0x18(%rsp), %xmm0
mulsd %xmm0, %xmm0
mulsd (%rsp), %xmm0
movsd %xmm0, (%rax)
subq $0x1, %rbp
jae 0xe14d
testb $0x1, 0x90(%rsp)
je 0xe179
movq 0x130(%rsp), %rax
movq 0x148(%rsp), %rcx
movslq (%rcx,%r15,4), %rcx
movq 0x140(%rbx), %rdx
movsd -0x8(%rdx,%rcx,8), %xmm0
movsd %xmm0, 0x18(%rsp)
movsd (%rax,%r15,8), %xmm0
movsd %xmm0, (%rsp)
movq %r14, %rdi
movq %r15, %rsi
movq %r15, %rdx
callq 0x11dc0
jmp 0xe1a8
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x11dc0
movsd (%rax), %xmm0
movsd %xmm0, (%rsp)
movq %r14, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x11dc0
movsd (%rsp), %xmm0
jmp 0xe1c3
movq 0x130(%rsp), %rax
movsd (%rax,%r15,8), %xmm0
movsd %xmm0, (%rsp)
movsd 0x198(%rbx), %xmm0
movsd %xmm0, 0x18(%rsp)
movq %r14, %rdi
movq %r15, %rsi
movq %r15, %rdx
callq 0x11dc0
movsd 0x18(%rsp), %xmm1
mulsd %xmm1, %xmm1
movsd (%rsp), %xmm0
mulsd %xmm0, %xmm0
addsd %xmm1, %xmm0
addsd (%rax), %xmm0
movsd %xmm0, (%rax)
incq %r12
jmp 0xe056
incq %r15
jmp 0xe039
movupd 0x1b8(%rbx), %xmm0
movapd 0x2e539(%rip), %xmm1    # 0x3c720
divpd %xmm0, %xmm1
movsd 0x1b0(%rbx), %xmm2
mulsd %xmm2, %xmm2
movlpd %xmm1, 0x58(%rsp)
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
movapd %xmm1, 0x90(%rsp)
movapd %xmm0, 0x80(%rsp)
divsd %xmm0, %xmm1
mulsd %xmm2, %xmm1
movsd 0x2e449(%rip), %xmm0    # 0x3c670
mulsd %xmm2, %xmm0
movsd %xmm0, 0xf8(%rsp)
movsd 0x2e43c(%rip), %xmm0    # 0x3c678
movapd %xmm1, (%rsp)
mulsd %xmm1, %xmm0
movsd %xmm0, 0xf0(%rsp)
movsd 0x2e42a(%rip), %xmm0    # 0x3c680
movsd %xmm2, 0x18(%rsp)
mulsd %xmm2, %xmm0
movsd %xmm0, 0xe8(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0xb0(%rsp)
movapd %xmm0, 0xa0(%rsp)
leaq 0x20(%rsp), %rsi
movl $0x3, (%rsi)
leaq 0x70(%rsp), %r13
movq %r13, %rdi
callq 0x11e0a
leaq 0x20(%rsp), %rsi
movl $0x3, (%rsi)
leaq 0x60(%rsp), %r12
movq %r12, %rdi
callq 0x11e0a
leaq 0x20(%rsp), %rsi
movl $0x3, (%rsi)
leaq 0xd8(%rsp), %r14
movq %r14, %rdi
callq 0x11e0a
leaq 0x20(%rsp), %rsi
movl $0x3, (%rsi)
leaq 0xc8(%rsp), %r15
movq %r15, %rdi
callq 0x11e0a
movsd 0x18(%rsp), %xmm0
mulsd 0x2dea5(%rip), %xmm0    # 0x3c198
leaq 0x20(%rsp), %rdi
movq %r13, (%rdi)
andq $0x0, 0x8(%rdi)
pushq $0x1
popq %rbp
movq %rbp, 0x10(%rdi)
movq %rbp, 0x18(%rdi)
movq 0x70(%rsp), %rax
movsd %xmm0, (%rax)
leaq 0xf8(%rsp), %rsi
callq 0x11e3a
leaq 0xe8(%rsp), %rsi
movq %rax, %rdi
callq 0x11e3a
movapd (%rsp), %xmm0
mulsd 0x2de5a(%rip), %xmm0    # 0x3c198
movapd %xmm0, (%rsp)
leaq 0x20(%rsp), %r13
movq %r13, %rdi
callq 0x11eec
movq %r12, (%r13)
andq $0x0, 0x8(%r13)
movq %rbp, 0x10(%r13)
movq %rbp, 0x18(%r13)
movq 0x60(%rsp), %rax
movapd (%rsp), %xmm0
movsd %xmm0, (%rax)
leaq 0x20(%rsp), %rdi
leaq 0xf0(%rsp), %rsi
callq 0x11e3a
leaq 0x40(%rsp), %rsi
andq $0x0, (%rsi)
movq %rax, %rdi
callq 0x11e3a
leaq 0x20(%rsp), %r12
movq %r12, %rdi
callq 0x11eec
movq %r14, (%r12)
andq $0x0, 0x8(%r12)
movq %rbp, 0x10(%r12)
movq %rbp, 0x18(%r12)
movq 0xd8(%rsp), %rax
movapd 0x90(%rsp), %xmm0
movsd %xmm0, (%rax)
leaq 0x20(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x11e3a
leaq 0x58(%rsp), %rsi
movq %rax, %rdi
callq 0x11e3a
leaq 0x20(%rsp), %r12
movq %r12, %rdi
callq 0x11eec
movq %r15, (%r12)
andq $0x0, 0x8(%r12)
movq %rbp, 0x10(%r12)
movq %rbp, 0x18(%r12)
movq 0xc8(%rsp), %rax
movapd 0x80(%rsp), %xmm0
movsd %xmm0, (%rax)
addsd %xmm0, %xmm0
leaq 0x40(%rsp), %rsi
movsd %xmm0, (%rsi)
leaq 0x20(%rsp), %rdi
callq 0x11e3a
leaq 0x50(%rsp), %rsi
andq $0x0, (%rsi)
movq %rax, %rdi
callq 0x11e3a
leaq 0x20(%rsp), %rdi
callq 0x11eec
movq 0x108(%rsp), %rax
subq 0x100(%rsp), %rax
sarq $0x3, %rax
leaq 0x40(%rsp), %rsi
movq %rax, (%rsi)
leaq 0x20(%rsp), %rdi
callq 0x11efc
movq 0x108(%rsp), %rax
subq 0x100(%rsp), %rax
sarq $0x3, %rax
leaq 0x50(%rsp), %rsi
movq %rax, (%rsi)
leaq 0x40(%rsp), %rdi
callq 0x11efc
xorl %ebp, %ebp
leaq 0x20(%rsp), %r13
leaq 0x40(%rsp), %r12
movq 0x108(%rsp), %rax
subq 0x100(%rsp), %rax
sarq $0x3, %rax
cmpq %rbp, %rax
jbe 0xe512
movq 0x130(%rsp), %rax
movsd (%rax,%rbp,8), %xmm0
movsd %xmm0, (%rsp)
movq %r13, %rdi
movq %rbp, %rsi
callq 0x11f2c
movsd (%rsp), %xmm0
mulsd %xmm0, %xmm0
movsd %xmm0, (%rax)
movq 0x100(%rsp), %rax
movsd (%rax,%rbp,8), %xmm0
movsd %xmm0, (%rsp)
movq %r12, %rdi
movq %rbp, %rsi
callq 0x11f2c
movsd (%rsp), %xmm0
movsd %xmm0, (%rax)
incq %rbp
jmp 0xe4a4
movsd 0x198(%rbx), %xmm0
addq $0x220, %rbx            # imm = 0x220
mulsd %xmm0, %xmm0
leaq 0x50(%rsp), %rsi
movsd %xmm0, (%rsi)
leaq 0xb0(%rsp), %rdx
leaq 0xa0(%rsp), %rcx
leaq 0x70(%rsp), %r8
leaq 0x60(%rsp), %r9
movq %rbx, %rdi
pushq %r13
pushq %r12
pushq %r15
pushq %r14
callq 0x11f62
addq $0x20, %rsp
movq 0x40(%rsp), %rdi
callq 0x64a0
movq 0x20(%rsp), %rdi
callq 0x64a0
movq 0xc8(%rsp), %rdi
callq 0x64a0
movq 0xd8(%rsp), %rdi
callq 0x64a0
movq 0x60(%rsp), %rdi
callq 0x64a0
movq 0x70(%rsp), %rdi
callq 0x64a0
movq 0xa0(%rsp), %rdi
callq 0x64a0
movq 0xb0(%rsp), %rdi
callq 0x64a0
leaq 0x100(%rsp), %rdi
callq 0xb0be
addq $0x208, %rsp            # imm = 0x208
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x559ba(%rip), %rdi     # 0x63f98
leaq 0x2e474(%rip), %rsi     # 0x3ca59
callq 0x6430
movq %rax, %rdi
callq 0x61c0
callq 0x6230
jmp 0xe698
jmp 0xe637
movq %rax, %rbx
jmp 0xe644
movq %rax, %rbx
jmp 0xe64e
jmp 0xe628
jmp 0xe628
jmp 0xe628
jmp 0xe628
movq %rax, %rbx
jmp 0xe65b
movq %rax, %rbx
jmp 0xe668
movq %rax, %rbx
jmp 0xe672
movq %rax, %rbx
xorl %edi, %edi
jmp 0xe684
jmp 0xe628
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x11eec
jmp 0xe64e
movq %rax, %rbx
movq 0x40(%rsp), %rdi
callq 0x64a0
movq 0x20(%rsp), %rdi
callq 0x64a0
movq 0xc8(%rsp), %rdi
callq 0x64a0
movq 0xd8(%rsp), %rdi
callq 0x64a0
movq 0x60(%rsp), %rdi
callq 0x64a0
movq 0x70(%rsp), %rdi
callq 0x64a0
movq 0xa0(%rsp), %rdi
callq 0x64a0
movq 0xb0(%rsp), %rdi
callq 0x64a0
jmp 0xe69b
movq %rax, %rbx
leaq 0x100(%rsp), %rdi
callq 0xb0be
movq %rbx, %rdi
callq 0x66e0