// File: /workspace/github_cpp_makefile_star2_O2/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 0x56a4f(%rip), %rsi     # 0x64580
leaq 0x80(%rsp), %rdi
callq 0x11aa2
leaq 0xe0(%rsp), %rsi
leaq 0x58(%rsp), %rdi
callq 0x120ba
movq 0xc8(%rbx), %rax
movq 0xf8(%rbx), %rcx
subq 0xf0(%rbx), %rcx
subq 0xc0(%rbx), %rax
cmpq %rax, %rcx
setb %cl
movl 0x3b0(%rbx), %eax
cmpl $0xb, %eax
setb %dl
testb %dl, %cl
je 0xdb96
leaq 0xf0(%rbx), %r14
incl %eax
movl %eax, 0x3b0(%rbx)
jmp 0xdd9a
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 0x15398
andl $0x0, 0x3b0(%rbx)
leaq 0x3943b(%rip), %rax     # 0x4700f
cmpb $0x1, (%rax)
jne 0xdc90
leaq 0x80(%rsp), %rdi
callq 0x12114
movsd %xmm0, 0x10(%rsp)
xorl %r15d, %r15d
xorl %r12d, %r12d
movq 0x80(%rsp), %rax
movq 0x88(%rsp), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %rcx, %r12
jae 0xdc90
movsd (%rax,%r12,8), %xmm0
subsd 0x10(%rsp), %xmm0
movapd %xmm0, %xmm1
mulsd %xmm0, %xmm1
mulsd 0x178(%rbx), %xmm1
movsd 0x170(%rbx), %xmm2
mulsd %xmm0, %xmm2
addsd %xmm1, %xmm2
movsd %xmm2, 0x8(%rsp)
movsd 0x180(%rbx), %xmm1
movsd %xmm1, 0x18(%rsp)
movsd 0x2ea0e(%rip), %xmm1    # 0x3c668
callq 0x66c0
mulsd 0x18(%rsp), %xmm0
addsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x48(%rsp)
movq 0x380(%rbx), %rax
fldt (%rax,%r15)
faddl 0x48(%rsp)
fstpt (%rax,%r15)
incq %r12
addq $0x10, %r15
jmp 0xdbf6
leaq 0x3937d(%rip), %rax     # 0x47014
cmpb $0x1, (%rax)
jne 0xdd03
movq 0x128(%rbx), %rax
movq 0x130(%rbx), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
movq 0x88(%rsp), %rdx
subq 0x80(%rsp), %rdx
sarq $0x3, %rdx
xorl %esi, %esi
cmpq %rcx, %rsi
je 0xdd03
movq 0xc8(%rsp), %r8
leaq 0x1(%rsi), %rdi
movq (%r14), %r9
xorl %r10d, %r10d
cmpq %r10, %rdx
je 0xdcfe
movslq (%r8,%r10,4), %r11
cmpq %r11, %rdi
jne 0xdcf5
fldt (%r9)
faddl (%rax,%rsi,8)
fstpt (%r9)
incq %r10
addq $0x10, %r9
jmp 0xdcde
movq %rdi, %rsi
jmp 0xdcc7
cmpb $0x1, 0xf8(%rsp)
jne 0xdd7f
movq 0x88(%rsp), %rax
subq 0x80(%rsp), %rax
sarq $0x3, %rax
movslq 0xfc(%rsp), %rcx
xorl %edx, %edx
cmpq %rax, %rdx
je 0xdd7f
movq 0x158(%rbx), %rsi
movq 0x58(%rsp), %rdi
movq %rdx, %r8
shlq $0x4, %r8
addq 0x380(%rbx), %r8
xorl %r9d, %r9d
cmpq %r9, %rcx
je 0xdd7a
movsd (%rsi,%r9,8), %xmm0
movq (%rdi), %r10
mulsd (%r10,%rdx,8), %xmm0
movsd %xmm0, 0x50(%rsp)
fldt (%r8)
faddl 0x50(%rsp)
fstpt (%r8)
incq %r9
addq $0x18, %rdi
jmp 0xdd4d
incq %rdx
jmp 0xdd2b
leaq 0xc0(%rbx), %r14
leaq 0x39288(%rip), %rax     # 0x47015
cmpb $0x1, (%rax)
jne 0xdd9a
movq %rbx, %rdi
callq 0xe6b0
leaq 0x3926d(%rip), %rax     # 0x4700e
movb (%rax), %bpl
xorl %r15d, %r15d
movq (%r14), %rcx
movq 0x8(%r14), %rax
subq %rcx, %rax
cqto
pushq $0x18
popq %rsi
idivq %rsi
cmpq %rax, %r15
jae 0xdec7
imulq $0x18, %r15, %r12
movq (%rcx,%r12), %rax
movsd (%rax), %xmm1
testb $0x1, %bpl
je 0xdde8
movapd %xmm1, %xmm0
callq 0x6700
movapd %xmm0, %xmm1
movq (%r14), %rax
movq (%rax,%r12), %rax
movsd 0x8(%rax), %xmm0
movsd %xmm0, 0x28(%rsp)
movsd 0x18(%rax), %xmm2
movsd 0x20(%rax), %xmm0
movsd %xmm0, 0x8(%rsp)
movsd 0x10(%rax), %xmm0
mulsd %xmm1, %xmm0
divsd 0x2e33c(%rip), %xmm0    # 0x3c150
movsd %xmm0, 0x10(%rsp)
xorl %r13d, %r13d
xorl %r12d, %r12d
movsd %xmm1, 0x30(%rsp)
movsd %xmm2, 0x20(%rsp)
movq 0x80(%rsp), %rax
movq 0x88(%rsp), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %rcx, %r12
jae 0xdebf
movsd (%rax,%r12,8), %xmm0
movsd 0x180(%rsp), %xmm3
subsd 0x10(%rsp), %xmm3
callq 0xe83e
addsd 0x8(%rsp), %xmm0
callq 0x6750
movsd %xmm0, 0x18(%rsp)
movsd 0x8(%rsp), %xmm0
callq 0x6750
movsd 0x20(%rsp), %xmm2
mulsd %xmm2, %xmm0
addsd 0x18(%rsp), %xmm0
mulsd 0x28(%rsp), %xmm0
movsd %xmm0, 0x38(%rsp)
movq 0x380(%rbx), %rax
fldt (%rax,%r13)
faddl 0x38(%rsp)
fstpt (%rax,%r13)
incq %r12
addq $0x10, %r13
movsd 0x30(%rsp), %xmm1
jmp 0xde2c
incq %r15
jmp 0xdda7
leaq 0x3913f(%rip), %rax     # 0x4700d
cmpb $0x0, (%rax)
je 0xdf6b
pushq $0x1
popq %r14
xorl %r15d, %r15d
movq 0x80(%rsp), %rax
movq 0x88(%rsp), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %rcx, %r14
jae 0xdf6b
movsd (%r15,%rax), %xmm0
subsd 0x8(%r15,%rax), %xmm0
movsd 0x188(%rbx), %xmm1
movsd %xmm1, 0x8(%rsp)
orpd 0x2e7f3(%rip), %xmm0    # 0x3c710
divsd 0x190(%rbx), %xmm0
callq 0x6700
mulsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x40(%rsp)
movq 0x98(%rsp), %rax
movq 0x380(%rbx), %rcx
fldt (%rcx,%r15,2)
fsubrl (%rax,%r15)
fldt 0x10(%rcx,%r15,2)
fxch %st(1)
fmull 0x40(%rsp)
faddp %st, %st(1)
fstpt 0x10(%rcx,%r15,2)
incq %r14
addq $0x8, %r15
jmp 0xdede
leaq 0x58(%rsp), %rdi
callq 0xb29c
leaq 0x80(%rsp), %rdi
callq 0xb0be
addq $0x188, %rsp            # imm = 0x188
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xdf9f
jmp 0xdf9f
jmp 0xdf9f
movq %rax, %rbx
jmp 0xdfac
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0xb29c
leaq 0x80(%rsp), %rdi
callq 0xb0be
movq %rbx, %rdi
callq 0x66e0
nop