// File: /workspace/github_cpp_makefile_star2_O3/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 0x40f97(%rip), %rsi     # 0x50400
leaq 0x100(%rsp), %rdi
callq 0x15552
movl 0x1a8(%rbx), %eax
cmpl $0x1, %eax
je 0xf65a
testl %eax, %eax
jne 0xfba9
movq 0x108(%rsp), %rax
subq 0x100(%rsp), %rax
shrq $0x3, %rax
movq %rax, %rcx
shlq $0x20, %rcx
je 0xfb4a
movslq %eax, %r14
leaq 0x3342c(%rip), %rax     # 0x428e4
movb (%rax), %al
movb %al, 0x90(%rsp)
cmpq $0x1, %r14
movq %r14, %rax
adcq $0x0, %rax
movq %rax, 0xd8(%rsp)
xorl %r12d, %r12d
xorl %ebp, %ebp
xorl %r15d, %r15d
movq %rbp, %r13
movq 0x100(%rsp), %rax
movsd (%rax,%rbp,8), %xmm0
subsd (%rax,%r13,8), %xmm0
movsd 0x1b0(%rbx), %xmm1
movsd %xmm1, 0xa0(%rsp)
movapd %xmm0, %xmm1
divsd 0x1b8(%rbx), %xmm1
mulsd %xmm1, %xmm1
movsd %xmm1, 0x48(%rsp)
mulsd 0x2e1bf(%rip), %xmm0    # 0x3d6e0
divsd 0x1c0(%rbx), %xmm0
callq 0x60b0
divsd 0x1c8(%rbx), %xmm0
mulsd %xmm0, %xmm0
movsd 0x48(%rsp), %xmm1
mulsd 0x2daf0(%rip), %xmm1    # 0x3d038
addsd %xmm0, %xmm0
subsd %xmm0, %xmm1
movapd %xmm1, %xmm0
callq 0x6730
movq 0x3a0(%rbx), %rcx
cmpq %rbp, %rcx
jle 0xfb69
movq 0x3a8(%rbx), %rsi
cmpq %r13, %rsi
jle 0xfb69
movq 0x398(%rbx), %rdx
movq %rcx, %rax
imulq %r13, %rax
leaq (%rdx,%rax,8), %rax
movsd 0xa0(%rsp), %xmm2
mulsd %xmm2, %xmm2
mulsd %xmm0, %xmm2
movsd %xmm2, (%rax,%rbp,8)
subq $0x1, %r15
jae 0xf5e9
testb $0x1, 0x90(%rsp)
je 0xf60a
movq 0x130(%rsp), %rcx
movq 0x148(%rsp), %rdx
movslq (%rdx,%rbp,4), %rdx
movq 0x140(%rbx), %rsi
movsd -0x8(%rsi,%rdx,8), %xmm0
movsd (%rcx,%rbp,8), %xmm1
mulsd %xmm0, %xmm0
mulsd %xmm1, %xmm1
addsd %xmm0, %xmm1
addsd %xmm1, %xmm2
jmp 0xf62f
cmpq %r13, %rcx
jle 0xfb6b
cmpq %rbp, %rsi
jbe 0xfb6b
imulq %r12, %rcx
addq %rcx, %rdx
movsd %xmm2, (%rdx,%r13,8)
jmp 0xf634
movq 0x130(%rsp), %rcx
movsd (%rcx,%rbp,8), %xmm0
movsd 0x198(%rbx), %xmm1
mulsd %xmm1, %xmm1
mulsd %xmm0, %xmm0
addsd %xmm1, %xmm0
addsd %xmm0, %xmm2
movsd %xmm2, (%rax,%rbp,8)
incq %r13
cmpq %r14, %r13
jb 0xf4df
incq %rbp
addq $0x8, %r12
cmpq 0xd8(%rsp), %rbp
jne 0xf4d9
jmp 0xfb4a
movupd 0x1b8(%rbx), %xmm0
movapd 0x2e106(%rip), %xmm1    # 0x3d770
divpd %xmm0, %xmm1
movsd 0x1b0(%rbx), %xmm2
mulsd %xmm2, %xmm2
movlpd %xmm1, 0x88(%rsp)
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
movapd %xmm0, 0x90(%rsp)
divsd %xmm0, %xmm1
mulsd %xmm2, %xmm1
movsd 0x2e024(%rip), %xmm0    # 0x3d6c8
mulsd %xmm2, %xmm0
movsd %xmm0, 0xf8(%rsp)
movsd 0x2e017(%rip), %xmm0    # 0x3d6d0
movapd %xmm1, 0xa0(%rsp)
mulsd %xmm1, %xmm0
movsd %xmm0, 0xf0(%rsp)
movsd 0x2e001(%rip), %xmm0    # 0x3d6d8
movsd %xmm2, 0x48(%rsp)
mulsd %xmm2, %xmm0
movsd %xmm0, 0xe8(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0xc0(%rsp)
movapd %xmm0, 0xb0(%rsp)
movapd %xmm0, 0x30(%rsp)
movl $0x18, %edi
callq 0x6520
movq %rax, %r14
testb $0xf, %r14b
jne 0xfbd2
testq %r14, %r14
je 0xfbd7
movq %r14, 0x30(%rsp)
movq $0x3, 0x38(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x70(%rsp)
movl $0x18, %edi
callq 0x6520
testb $0xf, %al
jne 0xfc05
testq %rax, %rax
je 0xfc0a
movq %rax, 0x70(%rsp)
movq $0x3, 0x78(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x60(%rsp)
movl $0x18, %edi
callq 0x6520
testb $0xf, %al
jne 0xfc38
testq %rax, %rax
je 0xfc3d
movq %rax, 0x60(%rsp)
movq $0x3, 0x68(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x50(%rsp)
movl $0x18, %edi
callq 0x6520
testb $0xf, %al
jne 0xfc6b
testq %rax, %rax
je 0xfc70
movsd 0x48(%rsp), %xmm0
mulsd 0x2d9cb(%rip), %xmm0    # 0x3d198
movq %rax, 0x50(%rsp)
movq $0x3, 0x58(%rsp)
leaq 0x30(%rsp), %rax
movq %rsp, %rdi
movq %rax, (%rdi)
movq $0x0, 0x8(%rdi)
movl $0x1, %eax
movq %rax, 0x10(%rdi)
movq %rax, 0x18(%rdi)
movsd %xmm0, (%r14)
leaq 0xf8(%rsp), %rsi
callq 0x1592c
leaq 0xe8(%rsp), %rsi
movq %rax, %rdi
callq 0x1592c
movq 0x18(%rsp), %rax
addq 0x8(%rsp), %rax
movq (%rsp), %rcx
cmpq 0x8(%rcx), %rax
jne 0xfc9e
cmpq $0x1, 0x10(%rsp)
jne 0xfc9e
movapd 0xa0(%rsp), %xmm0
mulsd 0x2d946(%rip), %xmm0    # 0x3d198
leaq 0x70(%rsp), %rax
movq %rsp, %rdi
movq %rax, (%rdi)
movq $0x0, 0x8(%rdi)
movl $0x1, %ecx
movq %rcx, 0x10(%rdi)
movq %rcx, 0x18(%rdi)
movq (%rax), %rax
movsd %xmm0, (%rax)
leaq 0xf0(%rsp), %rsi
callq 0x1592c
leaq 0x20(%rsp), %rsi
movq $0x0, (%rsi)
movq %rax, %rdi
callq 0x1592c
movq 0x18(%rsp), %rax
addq 0x8(%rsp), %rax
movq (%rsp), %rcx
cmpq 0x8(%rcx), %rax
jne 0xfca0
cmpq $0x1, 0x10(%rsp)
jne 0xfca0
leaq 0x60(%rsp), %rcx
movq %rsp, %rdi
movq %rcx, (%rdi)
movq $0x0, 0x8(%rdi)
movl $0x1, %eax
movq %rax, 0x10(%rdi)
movq %rax, 0x18(%rdi)
leaq 0x88(%rsp), %rsi
movsd (%rsi), %xmm0
movq (%rcx), %rax
movsd %xmm0, (%rax)
callq 0x1592c
leaq 0x88(%rsp), %rsi
movq %rax, %rdi
callq 0x1592c
movq 0x18(%rsp), %rax
addq 0x8(%rsp), %rax
movq (%rsp), %rcx
cmpq 0x8(%rcx), %rax
jne 0xfca2
cmpq $0x1, 0x10(%rsp)
jne 0xfca2
leaq 0x50(%rsp), %rcx
movq %rsp, %rdi
movq %rcx, (%rdi)
movq $0x0, 0x8(%rdi)
movl $0x1, %eax
movq %rax, 0x10(%rdi)
movq %rax, 0x18(%rdi)
movq (%rcx), %rax
movapd 0x90(%rsp), %xmm0
movsd %xmm0, (%rax)
addsd %xmm0, %xmm0
leaq 0x20(%rsp), %rsi
movsd %xmm0, (%rsi)
callq 0x1592c
leaq 0xe0(%rsp), %rsi
movq $0x0, (%rsi)
movq %rax, %rdi
callq 0x1592c
movq 0x18(%rsp), %rax
addq 0x8(%rsp), %rax
movq (%rsp), %rcx
cmpq 0x8(%rcx), %rax
jne 0xfca4
cmpq $0x1, 0x10(%rsp)
jne 0xfca4
movq 0x100(%rsp), %rbp
movq 0x108(%rsp), %r14
movq %r14, %r15
subq %rbp, %r15
movq %r15, %r13
sarq $0x3, %r13
xorpd %xmm0, %xmm0
movapd %xmm0, (%rsp)
js 0xfcc3
cmpq %rbp, %r14
je 0xfa40
movq %r15, %rdi
callq 0x6520
movq %rax, %r12
cmpq $0x8, %r15
setne %al
testb $0xf, %r12b
setne %cl
testb %cl, %al
jne 0xfce2
testq %r12, %r12
je 0xfce4
movq %r12, (%rsp)
movq %r13, 0x8(%rsp)
movq $0x0, 0x28(%rsp)
movq %r15, %rdi
callq 0x6520
cmpq $0x8, %r15
setne %cl
testb $0xf, %al
setne %dl
testb %dl, %cl
jne 0xfd12
testq %rax, %rax
je 0xfd31
movq %rax, 0x20(%rsp)
jmp 0xfa50
movq %r13, 0x8(%rsp)
movapd %xmm0, 0x20(%rsp)
xorl %r12d, %r12d
xorl %eax, %eax
movq %r13, 0x28(%rsp)
cmpq %rbp, %r14
je 0xfa9a
movq 0x130(%rsp), %rcx
cmpq $0x1, %r13
movq %r13, %rdx
adcq $0x0, %rdx
xorl %esi, %esi
cmpq %rsi, %r13
je 0xfb8a
movsd (%rcx,%rsi,8), %xmm0
mulsd %xmm0, %xmm0
movsd %xmm0, (%r12,%rsi,8)
movsd (%rbp,%rsi,8), %xmm0
movsd %xmm0, (%rax,%rsi,8)
incq %rsi
cmpq %rsi, %rdx
jne 0xfa6f
movsd 0x198(%rbx), %xmm0
addq $0x220, %rbx            # imm = 0x220
mulsd %xmm0, %xmm0
leaq 0xe0(%rsp), %rsi
movsd %xmm0, (%rsi)
leaq 0xc0(%rsp), %rdx
leaq 0xb0(%rsp), %rcx
leaq 0x30(%rsp), %r8
leaq 0x70(%rsp), %r9
movq %rbx, %rdi
movq %rsp, %rax
pushq %rax
leaq 0x28(%rsp), %rax
pushq %rax
leaq 0x60(%rsp), %rax
pushq %rax
leaq 0x78(%rsp), %rax
pushq %rax
callq 0x15a2a
addq $0x20, %rsp
movq 0x20(%rsp), %rdi
callq 0x64d0
movq (%rsp), %rdi
callq 0x64d0
movq 0x50(%rsp), %rdi
callq 0x64d0
movq 0x60(%rsp), %rdi
callq 0x64d0
movq 0x70(%rsp), %rdi
callq 0x64d0
movq 0x30(%rsp), %rdi
callq 0x64d0
movq 0xb0(%rsp), %rdi
callq 0x64d0
movq 0xc0(%rsp), %rdi
callq 0x64d0
leaq 0x100(%rsp), %rdi
callq 0xb8d2
addq $0x208, %rsp            # imm = 0x208
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xfb6b
leaq 0x2e286(%rip), %rdi     # 0x3ddf8
leaq 0x33bde(%rip), %rsi     # 0x43757
leaq 0x2e2ad(%rip), %rcx     # 0x3de2d
movl $0x16f, %edx            # imm = 0x16F
callq 0x62b0
leaq 0x2e7aa(%rip), %rdi     # 0x3e33b
leaq 0x33bbf(%rip), %rsi     # 0x43757
leaq 0x2e7b9(%rip), %rcx     # 0x3e358
movl $0x1ab, %edx            # imm = 0x1AB
callq 0x62b0
movq 0x403e8(%rip), %rdi     # 0x4ff98
leaq 0x2def2(%rip), %rsi     # 0x3daa9
movl $0x32, %edx
callq 0x64a0
movq 0x403d0(%rip), %rdi     # 0x4ff98
callq 0x61c0
callq 0x6230
jmp 0xfd12
movl $0x8, %edi
callq 0x61d0
movq 0x40380(%rip), %rcx     # 0x4ff68
addq $0x10, %rcx
movq %rcx, (%rax)
movq 0x403aa(%rip), %rsi     # 0x4ffa0
movq 0x40353(%rip), %rdx     # 0x4ff50
movq %rax, %rdi
callq 0x66c0
jmp 0xfd12
movl $0x8, %edi
callq 0x61d0
movq 0x4034d(%rip), %rcx     # 0x4ff68
addq $0x10, %rcx
movq %rcx, (%rax)
movq 0x40377(%rip), %rsi     # 0x4ffa0
movq 0x40320(%rip), %rdx     # 0x4ff50
movq %rax, %rdi
callq 0x66c0
jmp 0xfd12
movl $0x8, %edi
callq 0x61d0
movq 0x4031a(%rip), %rcx     # 0x4ff68
addq $0x10, %rcx
movq %rcx, (%rax)
movq 0x40344(%rip), %rsi     # 0x4ffa0
movq 0x402ed(%rip), %rdx     # 0x4ff50
movq %rax, %rdi
callq 0x66c0
jmp 0xfd12
movl $0x8, %edi
callq 0x61d0
movq 0x402e7(%rip), %rcx     # 0x4ff68
addq $0x10, %rcx
movq %rcx, (%rax)
movq 0x40311(%rip), %rsi     # 0x4ffa0
movq 0x402ba(%rip), %rdx     # 0x4ff50
movq %rax, %rdi
callq 0x66c0
jmp 0xfca4
jmp 0xfca4
jmp 0xfca4
leaq 0x2e3fc(%rip), %rdi     # 0x3e0a7
leaq 0x2e493(%rip), %rsi     # 0x3e145
leaq 0x2e4b4(%rip), %rcx     # 0x3e16d
movl $0x78, %edx
callq 0x62b0
leaq 0x2e1f3(%rip), %rdi     # 0x3debd
leaq 0x32c29(%rip), %rsi     # 0x428fa
leaq 0x2e26f(%rip), %rcx     # 0x3df47
movl $0x138, %edx            # imm = 0x138
callq 0x62b0
jmp 0xfd12
movl $0x8, %edi
callq 0x61d0
movq 0x40273(%rip), %rcx     # 0x4ff68
addq $0x10, %rcx
movq %rcx, (%rax)
movq 0x4029d(%rip), %rsi     # 0x4ffa0
movq 0x40246(%rip), %rdx     # 0x4ff50
movq %rax, %rdi
callq 0x66c0
leaq 0x2e2a0(%rip), %rdi     # 0x3dfb9
leaq 0x32c07(%rip), %rsi     # 0x42927
leaq 0x2e34d(%rip), %rcx     # 0x3e074
movl $0xb8, %edx
callq 0x62b0
movl $0x8, %edi
callq 0x61d0
movq 0x40226(%rip), %rcx     # 0x4ff68
addq $0x10, %rcx
movq %rcx, (%rax)
movq 0x40250(%rip), %rsi     # 0x4ffa0
movq 0x401f9(%rip), %rdx     # 0x4ff50
movq %rax, %rdi
callq 0x66c0
movq %rax, %rbx
jmp 0xfd7f
jmp 0xfd91
movq %rax, %rbx
jmp 0xfe00
movq %rax, %rbx
movq 0x20(%rsp), %rdi
callq 0x64d0
movq (%rsp), %r12
movq %r12, %rdi
callq 0x64d0
jmp 0xfdbe
jmp 0xfdb3
jmp 0xfdb3
jmp 0xfdb3
jmp 0xfdb3
movq %rax, %rbx
jmp 0xfdbe
movq %rax, %rbx
jmp 0xfdc8
movq %rax, %rbx
jmp 0xfdd2
movq %rax, %rbx
movq 0x30(%rsp), %rdi
callq 0x64d0
xorl %edi, %edi
jmp 0xfdee
jmp 0xfdb3
movq %rax, %rbx
movq %rsp, %rdi
callq 0x159f0
movq 0x50(%rsp), %rdi
callq 0x64d0
movq 0x60(%rsp), %rdi
callq 0x64d0
movq 0x70(%rsp), %rdi
callq 0x64d0
movq 0x30(%rsp), %rdi
callq 0x64d0
movq 0xb0(%rsp), %rdi
callq 0x64d0
movq 0xc0(%rsp), %rdi
callq 0x64d0
leaq 0x100(%rsp), %rdi
callq 0xb8d2
movq %rbx, %rdi
callq 0x6710
nop