// File: /workspace/github_cpp_makefile_star2_O1/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 $0x1e8, %rsp            # imm = 0x1E8
movq %rdi, %rbx
leaq 0x44097(%rip), %rsi     # 0x53400
leaq 0xe0(%rsp), %rdi
callq 0x1560c
movl 0x1a8(%rbx), %eax
cmpl $0x1, %eax
je 0xf554
testl %eax, %eax
jne 0xfb7d
movq 0xe8(%rsp), %rax
subq 0xe0(%rsp), %rax
shrq $0x3, %rax
movq %rax, %rcx
shlq $0x20, %rcx
je 0xfb1c
movslq %eax, %r14
leaq 0x35fd0(%rip), %rax     # 0x45388
movb (%rax), %al
movb %al, 0x70(%rsp)
cmpq $0x1, %r14
movq %r14, %rax
adcq $0x0, %rax
movq %rax, 0xb8(%rsp)
xorl %r12d, %r12d
xorl %ebp, %ebp
xorl %r15d, %r15d
movq %rbp, %r13
movq 0xe0(%rsp), %rax
movsd (%rax,%rbp,8), %xmm0
subsd (%rax,%r13,8), %xmm0
movsd 0x1b0(%rbx), %xmm1
movsd %xmm1, 0x78(%rsp)
movapd %xmm0, %xmm1
divsd 0x1b8(%rbx), %xmm1
mulsd %xmm1, %xmm1
movsd %xmm1, 0x80(%rsp)
mulsd 0x3029a(%rip), %xmm0    # 0x3f6b8
divsd 0x1c0(%rbx), %xmm0
callq 0x60b0
divsd 0x1c8(%rbx), %xmm0
mulsd %xmm0, %xmm0
movsd 0x80(%rsp), %xmm1
mulsd 0x2fbf0(%rip), %xmm1    # 0x3f038
addsd %xmm0, %xmm0
subsd %xmm0, %xmm1
movapd %xmm1, %xmm0
callq 0x6730
movq 0x3a0(%rbx), %rcx
cmpq %rbp, %rcx
jle 0xfb3b
movq 0x3a8(%rbx), %rsi
cmpq %r13, %rsi
jle 0xfb3b
movq 0x398(%rbx), %rdx
movq %rcx, %rax
imulq %r13, %rax
leaq (%rdx,%rax,8), %rax
movsd 0x78(%rsp), %xmm2
mulsd %xmm2, %xmm2
mulsd %xmm0, %xmm2
movsd %xmm2, (%rax,%rbp,8)
subq $0x1, %r15
jae 0xf4e3
testb $0x1, 0x70(%rsp)
je 0xf504
movq 0x110(%rsp), %rcx
movq 0x128(%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 0xf529
cmpq %r13, %rcx
jle 0xfb3d
cmpq %rbp, %rsi
jle 0xfb3d
imulq %r12, %rcx
addq %rcx, %rdx
movsd %xmm2, (%rdx,%r13,8)
jmp 0xf52e
movq 0x110(%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 0xf3dc
incq %rbp
addq $0x8, %r12
cmpq 0xb8(%rsp), %rbp
jne 0xf3d6
jmp 0xfb1c
movsd 0x2fad4(%rip), %xmm1    # 0x3f030
divsd 0x1b8(%rbx), %xmm1
movsd 0x2fbe4(%rip), %xmm0    # 0x3f150
divsd 0x1c0(%rbx), %xmm0
movsd 0x1b0(%rbx), %xmm2
mulsd %xmm2, %xmm2
movsd %xmm1, 0x88(%rsp)
movsd %xmm0, 0x70(%rsp)
divsd %xmm0, %xmm1
mulsd %xmm2, %xmm1
movsd 0x30101(%rip), %xmm0    # 0x3f6a0
mulsd %xmm2, %xmm0
movsd %xmm0, 0xd8(%rsp)
movsd 0x300f4(%rip), %xmm0    # 0x3f6a8
movsd %xmm1, 0x78(%rsp)
mulsd %xmm1, %xmm0
movsd %xmm0, 0xd0(%rsp)
movsd 0x300e1(%rip), %xmm0    # 0x3f6b0
movsd %xmm2, 0x80(%rsp)
mulsd %xmm2, %xmm0
movsd %xmm0, 0xc8(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0xa0(%rsp)
movapd %xmm0, 0x90(%rsp)
movapd %xmm0, 0x60(%rsp)
cmpq $0x3, 0x68(%rsp)
je 0xf637
movq 0x60(%rsp), %rdi
callq 0x64d0
movl $0x18, %edi
callq 0x6520
testb $0xf, %al
jne 0xfc48
testq %rax, %rax
je 0xfc4d
movq %rax, 0x60(%rsp)
xorpd %xmm0, %xmm0
movq $0x3, 0x68(%rsp)
movapd %xmm0, 0x50(%rsp)
cmpq $0x3, 0x58(%rsp)
je 0xf678
movq 0x50(%rsp), %rdi
callq 0x64d0
movl $0x18, %edi
callq 0x6520
testb $0xf, %al
jne 0xfc7b
testq %rax, %rax
je 0xfc80
movq %rax, 0x50(%rsp)
movq $0x3, 0x58(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x40(%rsp)
cmpq $0x3, 0x48(%rsp)
je 0xf6c1
movq 0x40(%rsp), %rdi
callq 0x64d0
movl $0x18, %edi
callq 0x6520
testb $0xf, %al
jne 0xfcae
testq %rax, %rax
je 0xfcb0
movq %rax, 0x40(%rsp)
xorpd %xmm0, %xmm0
movq $0x3, 0x48(%rsp)
movapd %xmm0, 0x30(%rsp)
cmpq $0x3, 0x38(%rsp)
je 0xf702
movq 0x30(%rsp), %rdi
callq 0x64d0
movl $0x18, %edi
callq 0x6520
testb $0xf, %al
jne 0xfcde
testq %rax, %rax
je 0xfce0
movq %rax, 0x30(%rsp)
movsd 0x80(%rsp), %xmm0
mulsd 0x2fa85(%rip), %xmm0    # 0x3f198
movq $0x3, 0x38(%rsp)
leaq 0x60(%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 0xd8(%rsp), %rsi
callq 0x159e0
leaq 0xc8(%rsp), %rsi
movq %rax, %rdi
callq 0x159e0
movq 0x18(%rsp), %rax
addq 0x8(%rsp), %rax
movq (%rsp), %rcx
cmpq 0x8(%rcx), %rax
jne 0xfba6
cmpq $0x1, 0x10(%rsp)
jne 0xfba6
movsd 0x78(%rsp), %xmm0
mulsd 0x2fa06(%rip), %xmm0    # 0x3f198
leaq 0x50(%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 0xd0(%rsp), %rsi
callq 0x159e0
leaq 0x20(%rsp), %rsi
movq $0x0, (%rsi)
movq %rax, %rdi
callq 0x159e0
movq 0x18(%rsp), %rax
addq 0x8(%rsp), %rax
movq (%rsp), %rcx
cmpq 0x8(%rcx), %rax
jne 0xfba8
cmpq $0x1, 0x10(%rsp)
jne 0xfba8
leaq 0x40(%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 0x159e0
leaq 0x88(%rsp), %rsi
movq %rax, %rdi
callq 0x159e0
movq 0x18(%rsp), %rax
addq 0x8(%rsp), %rax
movq (%rsp), %rcx
cmpq 0x8(%rcx), %rax
jne 0xfbaa
cmpq $0x1, 0x10(%rsp)
jne 0xfbaa
leaq 0x30(%rsp), %rcx
movq %rsp, %r14
movq %rcx, (%r14)
movq $0x0, 0x8(%r14)
movl $0x1, %eax
movq %rax, 0x10(%r14)
movq %rax, 0x18(%r14)
movq (%rcx), %rax
movsd 0x70(%rsp), %xmm0
movsd %xmm0, (%rax)
addsd %xmm0, %xmm0
leaq 0x20(%rsp), %rsi
movsd %xmm0, (%rsi)
movq %r14, %rdi
callq 0x159e0
leaq 0xc0(%rsp), %rsi
movq $0x0, (%rsi)
movq %rax, %rdi
callq 0x159e0
movq 0x18(%rsp), %rax
addq 0x8(%rsp), %rax
movq (%rsp), %rcx
cmpq 0x8(%rcx), %rax
jne 0xfbac
cmpq $0x1, 0x10(%rsp)
jne 0xfbac
movq 0xe0(%rsp), %rbp
movq 0xe8(%rsp), %r15
movq %r15, %r12
subq %rbp, %r12
movq %r12, %r13
sarq $0x3, %r13
xorpd %xmm0, %xmm0
movapd %xmm0, (%rsp)
js 0xfbcb
cmpq %r13, 0x8(%rsp)
je 0xf968
movq (%rsp), %rdi
callq 0x64d0
cmpq %rbp, %r15
je 0xf960
movq %r13, %rax
shrq $0x3d, %rax
jne 0xfbec
movq %r12, %rdi
callq 0x6520
cmpq $0x8, %r12
setne %cl
testb $0xf, %al
setne %dl
testb %dl, %cl
jne 0xfd0e
testq %rax, %rax
je 0xfbec
movq %rax, (%rsp)
jmp 0xf968
movq $0x0, (%rsp)
movq %r13, 0x8(%rsp)
movq 0xe0(%rsp), %rbp
movq 0xe8(%rsp), %r15
movq %r15, %r13
subq %rbp, %r13
movq %r13, %r12
sarq $0x3, %r12
xorpd %xmm0, %xmm0
movapd %xmm0, 0x20(%rsp)
js 0xfbcd
cmpq %r12, 0x28(%rsp)
je 0xf9f2
movq 0x20(%rsp), %rdi
callq 0x64d0
cmpq %rbp, %r15
je 0xf9e9
movq %r12, %rax
shrq $0x3d, %rax
jne 0xfc1a
movq %r13, %rdi
callq 0x6520
cmpq $0x8, %r13
setne %cl
testb $0xf, %al
setne %dl
testb %dl, %cl
jne 0xfd10
testq %rax, %rax
je 0xfc1a
movq %rax, 0x20(%rsp)
jmp 0xf9f2
movq $0x0, 0x20(%rsp)
movq %r12, 0x28(%rsp)
movq 0xe0(%rsp), %rax
movq 0xe8(%rsp), %rcx
subq %rax, %rcx
je 0xfa6e
sarq $0x3, %rcx
movq (%rsp), %rdx
movq 0x8(%rsp), %rsi
movq 0x110(%rsp), %rdi
xorl %r8d, %r8d
testq %rsi, %rsi
cmovleq %r8, %rsi
movq 0x20(%rsp), %r9
cmpq $0x1, %rcx
adcq $0x0, %rcx
cmpq %r8, %rsi
je 0xfb5c
movsd (%rdi,%r8,8), %xmm0
mulsd %xmm0, %xmm0
movsd %xmm0, (%rdx,%r8,8)
cmpq %r8, %r12
je 0xfb5e
movsd (%rax,%r8,8), %xmm0
movsd %xmm0, (%r9,%r8,8)
incq %r8
cmpq %r8, %rcx
jne 0xfa38
movsd 0x198(%rbx), %xmm0
addq $0x220, %rbx            # imm = 0x220
mulsd %xmm0, %xmm0
leaq 0xc0(%rsp), %rsi
movsd %xmm0, (%rsi)
leaq 0xa0(%rsp), %rdx
leaq 0x90(%rsp), %rcx
leaq 0x60(%rsp), %r8
leaq 0x50(%rsp), %r9
movq %rbx, %rdi
pushq %r14
leaq 0x28(%rsp), %rax
pushq %rax
leaq 0x40(%rsp), %rax
pushq %rax
leaq 0x58(%rsp), %rax
pushq %rax
callq 0x15ade
addq $0x20, %rsp
movq 0x20(%rsp), %rdi
callq 0x64d0
movq (%rsp), %rdi
callq 0x64d0
movq 0x30(%rsp), %rdi
callq 0x64d0
movq 0x40(%rsp), %rdi
callq 0x64d0
movq 0x50(%rsp), %rdi
callq 0x64d0
movq 0x60(%rsp), %rdi
callq 0x64d0
movq 0x90(%rsp), %rdi
callq 0x64d0
movq 0xa0(%rsp), %rdi
callq 0x64d0
leaq 0xe0(%rsp), %rdi
callq 0xb832
addq $0x1e8, %rsp            # imm = 0x1E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xfb3d
leaq 0x30278(%rip), %rdi     # 0x3fdbc
leaq 0x366b0(%rip), %rsi     # 0x461fb
leaq 0x3029f(%rip), %rcx     # 0x3fdf1
movl $0x16f, %edx            # imm = 0x16F
callq 0x62b0
jmp 0xfb5e
leaq 0x3079a(%rip), %rdi     # 0x402ff
leaq 0x3668f(%rip), %rsi     # 0x461fb
leaq 0x307a9(%rip), %rcx     # 0x4031c
movl $0x1ab, %edx            # imm = 0x1AB
callq 0x62b0
movq 0x43414(%rip), %rdi     # 0x52f98
leaq 0x2fee2(%rip), %rsi     # 0x3fa6d
movl $0x32, %edx
callq 0x64a0
movq 0x433fc(%rip), %rdi     # 0x52f98
callq 0x61c0
callq 0x6230
jmp 0xfbac
jmp 0xfbac
jmp 0xfbac
leaq 0x304b8(%rip), %rdi     # 0x4006b
leaq 0x3054f(%rip), %rsi     # 0x40109
leaq 0x30570(%rip), %rcx     # 0x40131
movl $0x78, %edx
callq 0x62b0
jmp 0xfbcd
leaq 0x302ad(%rip), %rdi     # 0x3fe81
leaq 0x357c3(%rip), %rsi     # 0x4539e
leaq 0x30329(%rip), %rcx     # 0x3ff0b
movl $0x138, %edx            # imm = 0x138
callq 0x62b0
movl $0x8, %edi
callq 0x61d0
movq 0x4336b(%rip), %rcx     # 0x52f68
addq $0x10, %rcx
movq %rcx, (%rax)
movq 0x43395(%rip), %rsi     # 0x52fa0
movq 0x4333e(%rip), %rdx     # 0x52f50
movq %rax, %rdi
callq 0x66c0
movl $0x8, %edi
callq 0x61d0
movq 0x4333d(%rip), %rcx     # 0x52f68
addq $0x10, %rcx
movq %rcx, (%rax)
movq 0x43367(%rip), %rsi     # 0x52fa0
movq 0x43310(%rip), %rdx     # 0x52f50
movq %rax, %rdi
callq 0x66c0
jmp 0xfd10
movl $0x8, %edi
callq 0x61d0
movq 0x4330a(%rip), %rcx     # 0x52f68
addq $0x10, %rcx
movq %rcx, (%rax)
movq 0x43334(%rip), %rsi     # 0x52fa0
movq 0x432dd(%rip), %rdx     # 0x52f50
movq %rax, %rdi
callq 0x66c0
jmp 0xfd10
movl $0x8, %edi
callq 0x61d0
movq 0x432d7(%rip), %rcx     # 0x52f68
addq $0x10, %rcx
movq %rcx, (%rax)
movq 0x43301(%rip), %rsi     # 0x52fa0
movq 0x432aa(%rip), %rdx     # 0x52f50
movq %rax, %rdi
callq 0x66c0
jmp 0xfd10
movl $0x8, %edi
callq 0x61d0
movq 0x432a7(%rip), %rcx     # 0x52f68
addq $0x10, %rcx
movq %rcx, (%rax)
movq 0x432d1(%rip), %rsi     # 0x52fa0
movq 0x4327a(%rip), %rdx     # 0x52f50
movq %rax, %rdi
callq 0x66c0
jmp 0xfd10
movl $0x8, %edi
callq 0x61d0
movq 0x43277(%rip), %rcx     # 0x52f68
addq $0x10, %rcx
movq %rcx, (%rax)
movq 0x432a1(%rip), %rsi     # 0x52fa0
movq 0x4324a(%rip), %rdx     # 0x52f50
movq %rax, %rdi
callq 0x66c0
jmp 0xfd10
leaq 0x30266(%rip), %rdi     # 0x3ff7d
leaq 0x356ad(%rip), %rsi     # 0x453cb
leaq 0x30313(%rip), %rcx     # 0x40038
movl $0xb8, %edx
callq 0x62b0
movq %rax, %rbx
jmp 0xfd7c
movq %rax, %rbx
jmp 0xfd86
movq %rax, %rbx
jmp 0xfd90
movq %rax, %rbx
jmp 0xfd9a
jmp 0xfd4f
movq %rax, %rbx
jmp 0xfd5c
movq %rax, %rbx
jmp 0xfdbe
movq %rax, %rbx
movq 0x20(%rsp), %rdi
callq 0x64d0
movq (%rsp), %rdi
callq 0x64d0
jmp 0xfd7c
jmp 0xfd71
jmp 0xfd71
jmp 0xfd71
jmp 0xfd71
jmp 0xfd71
movq %rax, %rbx
movq %rsp, %rdi
callq 0x15aa4
movq 0x30(%rsp), %rdi
callq 0x64d0
movq 0x40(%rsp), %rdi
callq 0x64d0
movq 0x50(%rsp), %rdi
callq 0x64d0
movq 0x60(%rsp), %rdi
callq 0x64d0
movq 0x90(%rsp), %rdi
callq 0x64d0
movq 0xa0(%rsp), %rdi
callq 0x64d0
leaq 0xe0(%rsp), %rdi
callq 0xb832
movq %rbx, %rdi
callq 0x6710
nop