// File: /workspace/github_cpp_makefile_star2_O3/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 0x414df(%rip), %rsi     # 0x50400
leaq 0x80(%rsp), %rdi
callq 0x15552
leaq 0xe0(%rsp), %rsi
leaq 0x58(%rsp), %rdi
callq 0x15b98
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 0xef86
leaq 0xf0(%rbx), %r14
incl %eax
movl %eax, 0x3b0(%rbx)
jmp 0xf1e0
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 0x18948
movl $0x0, 0x3b0(%rbx)
leaq 0x33918(%rip), %rax     # 0x428df
cmpb $0x1, (%rax)
jne 0xf0a0
leaq 0x80(%rsp), %rdi
callq 0x15c32
movq 0x80(%rsp), %rax
cmpq %rax, 0x88(%rsp)
je 0xf0a0
movapd %xmm0, %xmm1
xorl %r15d, %r15d
xorl %r12d, %r12d
movsd %xmm0, 0x18(%rsp)
movsd (%rax,%r12,8), %xmm0
subsd %xmm1, %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, 0x10(%rsp)
movsd 0x2e677(%rip), %xmm1    # 0x3d6c0
callq 0x66f0
movsd 0x18(%rsp), %xmm1
mulsd 0x10(%rsp), %xmm0
addsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x50(%rsp)
movq 0x380(%rbx), %rax
fldt (%rax,%r15)
faddl 0x50(%rsp)
fstpt (%rax,%r15)
incq %r12
movq 0x80(%rsp), %rax
movq 0x88(%rsp), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
addq $0x10, %r15
cmpq %rcx, %r12
jb 0xf003
leaq 0x3383d(%rip), %rax     # 0x428e4
cmpb $0x1, (%rax)
jne 0xf132
movq 0x128(%rbx), %rax
movq 0x130(%rbx), %rcx
subq %rax, %rcx
je 0xf132
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
leaq 0x1(%r9), %r10
cmpq %rdx, %rsi
je 0xf12a
movq (%r14), %r11
xorl %r15d, %r15d
movslq (%r8,%r15,4), %r12
cmpq %r12, %r10
jne 0xf11e
fldt (%r11)
faddl (%rax,%r9,8)
fstpt (%r11)
incq %r15
addq $0x10, %r11
cmpq %r15, %rdi
jne 0xf10b
movq %r10, %r9
cmpq %rcx, %r10
jne 0xf0fc
cmpb $0x1, 0xf8(%rsp)
jne 0xf1c5
movq 0x88(%rsp), %rax
subq 0x80(%rsp), %rax
je 0xf1c5
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 0xf1bd
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 0xf195
fstpt (%r9)
incq %r8
cmpq %rax, %r8
jne 0xf17c
leaq 0xc0(%rbx), %r14
leaq 0x33712(%rip), %rax     # 0x428e5
cmpb $0x1, (%rax)
jne 0xf1e0
movq %rbx, %rdi
callq 0xfe16
movq (%r14), %rax
cmpq %rax, 0x8(%r14)
je 0xf335
leaq 0x336ea(%rip), %rcx     # 0x428de
movb (%rcx), %bpl
xorl %r15d, %r15d
leaq (%r15,%r15,2), %r12
movq (%rax,%r12,8), %rdx
movsd (%rdx), %xmm1
testb %bpl, %bpl
je 0xf21f
movapd %xmm1, %xmm0
callq 0x6730
movapd %xmm0, %xmm1
movq (%r14), %rax
movq (%rax,%r12,8), %rdx
movq 0x80(%rsp), %rcx
cmpq %rcx, 0x88(%rsp)
je 0xf310
movsd 0x8(%rdx), %xmm0
movsd %xmm0, 0x28(%rsp)
movsd 0x18(%rdx), %xmm0
movsd %xmm0, 0x8(%rsp)
movsd 0x20(%rdx), %xmm0
movsd %xmm0, 0x10(%rsp)
movsd 0x10(%rdx), %xmm0
mulsd %xmm1, %xmm0
divsd 0x2dee9(%rip), %xmm0    # 0x3d150
movsd %xmm0, 0x20(%rsp)
xorl %r13d, %r13d
xorl %r12d, %r12d
movsd %xmm1, 0x30(%rsp)
movsd (%rcx,%r12,8), %xmm0
movsd 0x180(%rsp), %xmm3
subsd 0x20(%rsp), %xmm3
movsd 0x30(%rsp), %xmm1
movsd 0x8(%rsp), %xmm2
callq 0xffbc
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), %rcx
movq 0x88(%rsp), %rax
subq %rcx, %rax
sarq $0x3, %rax
addq $0x10, %r13
cmpq %rax, %r12
jb 0xf279
movq (%r14), %rax
incq %r15
movq 0x8(%r14), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
movabsq $-0x5555555555555555, %rdx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rdx, %rcx
cmpq %rcx, %r15
jb 0xf1fa
leaq 0x335a1(%rip), %rax     # 0x428dd
cmpb $0x0, (%rax)
je 0xf3fa
movq 0x80(%rsp), %rax
movq 0x88(%rsp), %rcx
subq %rax, %rcx
cmpq $0x9, %rcx
jb 0xf3fa
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 0x2e3cf(%rip), %xmm0    # 0x3d760
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 0xf36e
leaq 0x58(%rsp), %rdi
callq 0xbb88
leaq 0x80(%rsp), %rdi
callq 0xb8d2
addq $0x188, %rsp            # imm = 0x188
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xf427
jmp 0xf427
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0xbb88
jmp 0xf439
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0xb8d2
movq %rbx, %rdi
callq 0x6710