// File: /workspace/github_cpp_makefile_star2_O1/j-faria[P]kima/src/RVmodel.cpp
// Function: RVmodel::calculate_mu()
void RVmodel::calculate_mu()
{
    auto data = Data::get_instance();
    // Get the times from the data
    const vector<double>& t = data.get_t();
    // only really needed if multi_instrument
    const vector<int>& obsi = data.get_obsi();
    auto actind = data.get_actind();

    // Update or from scratch?
    bool update = (planets.get_added().size() < planets.get_components().size()) &&
            (staleness <= 10);

    // Get the components
    const vector< vector<double> >& components = (update)?(planets.get_added()):
                (planets.get_components());
    // at this point, components has:
    //  if updating: only the added planets' parameters
    //  if from scratch: all the planets' parameters

    // Zero the signal
    if(!update) // not updating, means recalculate everything
    {
        mu.assign(mu.size(), background);
        staleness = 0;
        if(trend)
        {
            double tmid = data.get_t_middle();
            for(size_t i=0; i<t.size(); i++)
            {
                mu[i] += slope*(t[i]-tmid) + quadr*pow(t[i]-tmid, 2) + cubic*pow(t[i]-tmid, 3);
            }
        }

        if(multi_instrument)
        {
            for(size_t j=0; j<offsets.size(); j++)
            {
                for(size_t i=0; i<t.size(); i++)
                {
                    if (obsi[i] == j+1) { mu[i] += offsets[j]; }
                }
            }
        }

        if(data.indicator_correlations)
        {
            for(size_t i=0; i<t.size(); i++)
            {
                for(size_t j = 0; j < data.number_indicators; j++)
                   mu[i] += betas[j] * actind[j][i];
            }   
        }

        if (known_object) { // KO mode!
            add_known_object();
        }
    }
    else // just updating (adding) planets
        staleness++;


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


    double f, v, ti;
    double P, K, phi, ecc, omega, Tp;
    for(size_t j=0; j<components.size(); j++)
    {
        if(hyperpriors)
            P = exp(components[j][0]);
        else
            P = components[j][0];

        K = components[j][1];
        phi = components[j][2];
        ecc = components[j][3];
        omega = components[j][4];

        for(size_t i=0; i<t.size(); i++)
        {
            ti = t[i];
            Tp = data.M0_epoch-(P*phi)/(2.*M_PI);
            f = kepler::true_anomaly(ti, P, ecc, Tp);
            v = K*(cos(f+omega) + ecc*cos(omega));
            mu[i] += v;
        }
    }


    if(MA)
    {
        const vector<double>& y = data.get_y();
        for(size_t i=1; i<t.size(); i++) // the loop starts at the 2nd point
        {
            // y[i-1] - mu[i-1] is the residual at the i-1 observation
            mu[i] += sigmaMA * exp(-fabs(t[i-1] - t[i]) / tauMA) * (y[i-1] - mu[i-1]);
        }   
    }



    #if TIMING
    auto end = std::chrono::high_resolution_clock::now();
    cout << "Model eval took " << std::chrono::duration_cast<std::chrono::nanoseconds>(end-begin).count()*1E-6 << " ms" << std::endl;
    #endif

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x188, %rsp            # imm = 0x188
movq %rdi, %rbx
leaq 0x445f3(%rip), %rsi     # 0x53400
leaq 0x80(%rsp), %rdi
callq 0x1560c
leaq 0xe0(%rsp), %rsi
leaq 0x58(%rsp), %rdi
callq 0x15c4c
leaq 0xf0(%rbx), %rcx
movq 0xf8(%rbx), %rax
subq 0xf0(%rbx), %rax
movq 0xc8(%rbx), %rdx
leaq 0xc0(%rbx), %r15
subq 0xc0(%rbx), %rdx
cmpq %rdx, %rax
setb %dl
movl 0x3b0(%rbx), %eax
cmpl $0xb, %eax
setb %sil
testb %sil, %dl
cmovneq %rcx, %r15
je 0xee7f
incl %eax
movl %eax, 0x3b0(%rbx)
jmp 0xf0e0
movq 0x388(%rbx), %rsi
subq 0x380(%rbx), %rsi
leaq 0x380(%rbx), %r14
sarq $0x4, %rsi
fldl 0x120(%rbx)
fstpt 0x70(%rsp)
leaq 0x70(%rsp), %rdx
movq %r14, %rdi
callq 0x1901c
movl $0x0, 0x3b0(%rbx)
leaq 0x364c3(%rip), %rax     # 0x45383
cmpb $0x1, (%rax)
jne 0xefa7
leaq 0x80(%rsp), %rdi
callq 0x15ce6
movq 0x80(%rsp), %rax
cmpq %rax, 0x88(%rsp)
je 0xefa7
movapd %xmm0, %xmm1
xorl %r12d, %r12d
xorl %r13d, %r13d
movsd %xmm0, 0x18(%rsp)
movsd (%rax,%r13,8), %xmm2
subsd %xmm1, %xmm2
movapd %xmm2, %xmm0
mulsd %xmm2, %xmm0
mulsd 0x178(%rbx), %xmm0
mulsd 0x170(%rbx), %xmm2
addsd %xmm0, %xmm2
movsd %xmm2, 0x8(%rsp)
movsd 0x180(%rbx), %xmm0
movsd %xmm0, 0x10(%rsp)
movq 0x80(%rsp), %rax
movsd (%rax,%r13,8), %xmm0
subsd %xmm1, %xmm0
movsd 0x30748(%rip), %xmm1    # 0x3f698
callq 0x66f0
movsd 0x18(%rsp), %xmm1
mulsd 0x10(%rsp), %xmm0
addsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x50(%rsp)
movq 0x380(%rbx), %rax
fldt (%rax,%r12)
faddl 0x50(%rsp)
fstpt (%rax,%r12)
incq %r13
movq 0x80(%rsp), %rax
movq 0x88(%rsp), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
addq $0x10, %r12
cmpq %rcx, %r13
jb 0xeefc
leaq 0x363da(%rip), %rax     # 0x45388
cmpb $0x1, (%rax)
jne 0xf039
movq 0x128(%rbx), %rax
movq 0x130(%rbx), %rcx
subq %rax, %rcx
je 0xf039
sarq $0x3, %rcx
movq 0x80(%rsp), %rdx
movq 0x88(%rsp), %rsi
movq %rsi, %rdi
subq %rdx, %rdi
sarq $0x3, %rdi
movq 0xc8(%rsp), %r8
cmpq $0x1, %rdi
adcq $0x0, %rdi
cmpq $0x1, %rcx
adcq $0x0, %rcx
xorl %r9d, %r9d
cmpq %rdx, %rsi
je 0xf031
leaq 0x1(%r9), %r10
movq (%r14), %r11
xorl %r12d, %r12d
movslq (%r8,%r12,4), %r13
cmpq %r13, %r10
jne 0xf025
fldt (%r11)
faddl (%rax,%r9,8)
fstpt (%r11)
incq %r12
addq $0x10, %r11
cmpq %r12, %rdi
jne 0xf012
incq %r9
cmpq %rcx, %r9
jne 0xf003
cmpb $0x1, 0xf8(%rsp)
jne 0xf0cc
movq 0x88(%rsp), %rax
subq 0x80(%rsp), %rax
je 0xf0cc
sarq $0x3, %rax
movslq 0xfc(%rsp), %rcx
movq 0x158(%rbx), %rdx
movq 0x58(%rsp), %rsi
movq 0x380(%rbx), %rdi
cmpq $0x1, %rax
adcq $0x0, %rax
xorl %r8d, %r8d
testl %ecx, %ecx
je 0xf0c4
movq %r8, %r10
shlq $0x4, %r10
leaq (%rdi,%r10), %r9
fldt (%rdi,%r10)
movq %rsi, %r10
xorl %r11d, %r11d
movsd (%rdx,%r11,8), %xmm0
movq (%r10), %r14
mulsd (%r14,%r8,8), %xmm0
movsd %xmm0, 0x48(%rsp)
faddl 0x48(%rsp)
incq %r11
addq $0x18, %r10
cmpq %r11, %rcx
jne 0xf09c
fstpt (%r9)
incq %r8
cmpq %rax, %r8
jne 0xf083
leaq 0x362b6(%rip), %rax     # 0x45389
cmpb $0x1, (%rax)
jne 0xf0e0
movq %rbx, %rdi
callq 0xfdd4
movq (%r15), %rax
cmpq %rax, 0x8(%r15)
je 0xf235
leaq 0x3628e(%rip), %rcx     # 0x45382
movb (%rcx), %bpl
xorl %r14d, %r14d
leaq (%r14,%r14,2), %r12
movq (%rax,%r12,8), %rax
movsd (%rax), %xmm1
testb %bpl, %bpl
je 0xf118
movapd %xmm1, %xmm0
callq 0x6730
movapd %xmm0, %xmm1
movq 0x80(%rsp), %rax
cmpq %rax, 0x88(%rsp)
je 0xf20d
movq (%r15), %rcx
movq (%rcx,%r12,8), %rcx
movsd 0x8(%rcx), %xmm0
movsd %xmm0, 0x28(%rsp)
movsd 0x18(%rcx), %xmm0
movsd %xmm0, 0x8(%rsp)
movsd 0x20(%rcx), %xmm0
movsd %xmm0, 0x10(%rsp)
movsd 0x10(%rcx), %xmm0
mulsd %xmm1, %xmm0
divsd 0x2ffe9(%rip), %xmm0    # 0x3f150
movsd %xmm0, 0x20(%rsp)
xorl %r13d, %r13d
xorl %r12d, %r12d
movsd %xmm1, 0x30(%rsp)
movsd (%rax,%r12,8), %xmm0
movsd 0x180(%rsp), %xmm3
subsd 0x20(%rsp), %xmm3
movsd 0x30(%rsp), %xmm1
movsd 0x8(%rsp), %xmm2
callq 0xff71
addsd 0x10(%rsp), %xmm0
callq 0x6790
movsd %xmm0, 0x18(%rsp)
movsd 0x10(%rsp), %xmm0
callq 0x6790
mulsd 0x8(%rsp), %xmm0
addsd 0x18(%rsp), %xmm0
mulsd 0x28(%rsp), %xmm0
movsd %xmm0, 0x40(%rsp)
movq 0x380(%rbx), %rax
fldt (%rax,%r13)
faddl 0x40(%rsp)
fstpt (%rax,%r13)
incq %r12
movq 0x80(%rsp), %rax
movq 0x88(%rsp), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
addq $0x10, %r13
cmpq %rcx, %r12
jb 0xf179
incq %r14
movq (%r15), %rax
movq 0x8(%r15), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
movabsq $-0x5555555555555555, %rdx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rdx, %rcx
cmpq %rcx, %r14
jb 0xf0fa
leaq 0x36145(%rip), %rax     # 0x45381
cmpb $0x0, (%rax)
je 0xf2fa
movq 0x80(%rsp), %rax
movq 0x88(%rsp), %rcx
subq %rax, %rcx
cmpq $0x9, %rcx
jb 0xf2fa
movl $0x1, %r14d
movl $0x10, %r15d
movsd -0x8(%rax,%r14,8), %xmm0
subsd (%rax,%r14,8), %xmm0
movsd 0x188(%rbx), %xmm1
movsd %xmm1, 0x8(%rsp)
orpd 0x304af(%rip), %xmm0    # 0x3f740
divsd 0x190(%rbx), %xmm0
callq 0x6730
mulsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x38(%rsp)
movq 0x98(%rsp), %rax
movq 0x380(%rbx), %rcx
fldt -0x10(%rcx,%r15)
fsubrl -0x8(%rax,%r14,8)
fldt (%rcx,%r15)
fxch %st(1)
fmull 0x38(%rsp)
faddp %st, %st(1)
fstpt (%rcx,%r15)
incq %r14
movq 0x80(%rsp), %rax
movq 0x88(%rsp), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
addq $0x10, %r15
cmpq %rcx, %r14
jb 0xf26e
leaq 0x58(%rsp), %rdi
callq 0xbacc
leaq 0x80(%rsp), %rdi
callq 0xb832
addq $0x188, %rsp            # imm = 0x188
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xf327
jmp 0xf327
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0xbacc
jmp 0xf339
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0xb832
movq %rbx, %rdi
callq 0x6710