// File: 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
movq %rsp, %rbp
subq $0x3b0, %rsp            # imm = 0x3B0
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x238(%rbp)
callq 0xab10
movq %rax, %rsi
leaq -0x110(%rbp), %rdi
movq %rdi, -0x230(%rbp)
callq 0x269d0
movq -0x230(%rbp), %rdi
callq 0x271b0
movq %rax, -0x228(%rbp)
jmp 0x1eb7a
movq -0x228(%rbp), %rax
movq %rax, -0x118(%rbp)
leaq -0x110(%rbp), %rdi
callq 0x271e0
movq %rax, -0x240(%rbp)
jmp 0x1eb9d
movq -0x240(%rbp), %rax
movq %rax, -0x130(%rbp)
leaq -0x110(%rbp), %rdi
callq 0x278e0
movq %rax, -0x248(%rbp)
jmp 0x1ebc0
movq -0x248(%rbp), %rsi
leaq -0x148(%rbp), %rdi
callq 0x27900
jmp 0x1ebd5
movq -0x238(%rbp), %rdi
addq $0x8, %rdi
callq 0x279f0
movq %rax, -0x250(%rbp)
jmp 0x1ebee
movq -0x250(%rbp), %rdi
callq 0x15d80
movq -0x238(%rbp), %rdi
movq %rax, -0x260(%rbp)
addq $0x8, %rdi
callq 0x27a10
movq %rax, -0x258(%rbp)
jmp 0x1ec1a
movq -0x258(%rbp), %rdi
callq 0x15d80
movq -0x260(%rbp), %rcx
movq %rax, %rdx
xorl %eax, %eax
cmpq %rdx, %rcx
movb %al, -0x261(%rbp)
jae 0x1ec54
movq -0x238(%rbp), %rax
cmpl $0xa, 0x3b0(%rax)
setbe %al
movb %al, -0x261(%rbp)
movb -0x261(%rbp), %al
andb $0x1, %al
movb %al, -0x149(%rbp)
testb $0x1, -0x149(%rbp)
je 0x1ec94
movq -0x238(%rbp), %rdi
addq $0x8, %rdi
callq 0x279f0
movq %rax, -0x270(%rbp)
jmp 0x1ec84
movq -0x270(%rbp), %rax
movq %rax, -0x278(%rbp)
jmp 0x1ecbd
movq -0x238(%rbp), %rdi
addq $0x8, %rdi
callq 0x27a10
movq %rax, -0x280(%rbp)
jmp 0x1ecad
movq -0x280(%rbp), %rax
movq %rax, -0x278(%rbp)
jmp 0x1ecbd
movq -0x278(%rbp), %rax
movq %rax, -0x158(%rbp)
testb $0x1, -0x149(%rbp)
jne 0x1f1d0
movq -0x238(%rbp), %rdi
addq $0x380, %rdi            # imm = 0x380
movq %rdi, -0x288(%rbp)
callq 0x27a60
movq -0x288(%rbp), %rdi
movq %rax, %rsi
movq -0x238(%rbp), %rax
fldl 0x120(%rax)
fstpt -0x170(%rbp)
leaq -0x170(%rbp), %rdx
callq 0x27a30
jmp 0x1ed1d
movq -0x238(%rbp), %rax
movl $0x0, 0x3b0(%rax)
leaq 0xa6c6e(%rip), %rax     # 0xc59a3
testb $0x1, (%rax)
je 0x1ef5e
leaq -0x110(%rbp), %rdi
callq 0x27a80
movsd %xmm0, -0x290(%rbp)
jmp 0x1ed54
movsd -0x290(%rbp), %xmm0
movsd %xmm0, -0x178(%rbp)
movq $0x0, -0x180(%rbp)
movq -0x180(%rbp), %rax
movq %rax, -0x298(%rbp)
movq -0x118(%rbp), %rdi
callq 0x7c90
movq %rax, %rcx
movq -0x298(%rbp), %rax
cmpq %rcx, %rax
jae 0x1ef5c
movq -0x238(%rbp), %rax
movsd 0x170(%rax), %xmm0
movsd %xmm0, -0x2b8(%rbp)
movq -0x118(%rbp), %rdi
movq -0x180(%rbp), %rsi
callq 0xa6a0
movq %rax, %rcx
movq -0x238(%rbp), %rax
movsd (%rcx), %xmm0
movsd -0x178(%rbp), %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, -0x2b0(%rbp)
movsd 0x178(%rax), %xmm0
movsd %xmm0, -0x2a8(%rbp)
movq -0x118(%rbp), %rdi
movq -0x180(%rbp), %rsi
callq 0xa6a0
movsd (%rax), %xmm0
movsd -0x178(%rbp), %xmm1
subsd %xmm1, %xmm0
movl $0x2, %edi
callq 0x16850
movsd %xmm0, -0x2a0(%rbp)
jmp 0x1ee2f
movq -0x238(%rbp), %rax
movsd -0x2b0(%rbp), %xmm2
movsd -0x2b8(%rbp), %xmm0
movsd -0x2a0(%rbp), %xmm3
movsd -0x2a8(%rbp), %xmm1
mulsd %xmm3, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x2d0(%rbp)
movsd 0x180(%rax), %xmm0
movsd %xmm0, -0x2c8(%rbp)
movq -0x118(%rbp), %rdi
movq -0x180(%rbp), %rsi
callq 0xa6a0
movsd (%rax), %xmm0
movsd -0x178(%rbp), %xmm1
subsd %xmm1, %xmm0
movl $0x3, %edi
callq 0x16850
movsd %xmm0, -0x2c0(%rbp)
jmp 0x1eeb1
movq -0x238(%rbp), %rdi
movsd -0x2d0(%rbp), %xmm1
movsd -0x2c0(%rbp), %xmm2
movsd -0x2c8(%rbp), %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x218(%rbp)
fldl -0x218(%rbp)
fstpt -0x2dc(%rbp)
addq $0x380, %rdi            # imm = 0x380
movq -0x180(%rbp), %rsi
callq 0x27ad0
fldt -0x2dc(%rbp)
fldt (%rax)
faddp %st, %st(1)
fstpt (%rax)
movq -0x180(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x180(%rbp)
jmp 0x1ed6f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x120(%rbp)
movl %eax, -0x124(%rbp)
jmp 0x1f6a2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x120(%rbp)
movl %eax, -0x124(%rbp)
leaq -0x148(%rbp), %rdi
callq 0x16030
jmp 0x1f6a2
jmp 0x1ef5e
leaq 0xa6a43(%rip), %rax     # 0xc59a8
testb $0x1, (%rax)
je 0x1f087
movq $0x0, -0x188(%rbp)
movq -0x238(%rbp), %rdi
movq -0x188(%rbp), %rax
movq %rax, -0x2e8(%rbp)
addq $0x128, %rdi            # imm = 0x128
callq 0x7c90
movq %rax, %rcx
movq -0x2e8(%rbp), %rax
cmpq %rcx, %rax
jae 0x1f085
movq $0x0, -0x190(%rbp)
movq -0x190(%rbp), %rax
movq %rax, -0x2f0(%rbp)
movq -0x118(%rbp), %rdi
callq 0x7c90
movq %rax, %rcx
movq -0x2f0(%rbp), %rax
cmpq %rcx, %rax
jae 0x1f06c
movq -0x130(%rbp), %rdi
movq -0x190(%rbp), %rsi
callq 0x166f0
movslq (%rax), %rax
movq -0x188(%rbp), %rcx
addq $0x1, %rcx
cmpq %rcx, %rax
jne 0x1f053
movq -0x238(%rbp), %rdi
addq $0x128, %rdi            # imm = 0x128
movq -0x188(%rbp), %rsi
callq 0xc6a0
movq -0x238(%rbp), %rdi
fldl (%rax)
fstpt -0x2fc(%rbp)
addq $0x380, %rdi            # imm = 0x380
movq -0x190(%rbp), %rsi
callq 0x27ad0
fldt -0x2fc(%rbp)
fldt (%rax)
faddp %st, %st(1)
fstpt (%rax)
jmp 0x1f055
movq -0x190(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x190(%rbp)
jmp 0x1efb8
jmp 0x1f06e
movq -0x188(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x188(%rbp)
jmp 0x1ef79
jmp 0x1f087
testb $0x1, -0x98(%rbp)
je 0x1f1b2
movq $0x0, -0x198(%rbp)
movq -0x198(%rbp), %rax
movq %rax, -0x308(%rbp)
movq -0x118(%rbp), %rdi
callq 0x7c90
movq %rax, %rcx
movq -0x308(%rbp), %rax
cmpq %rcx, %rax
jae 0x1f1b0
movq $0x0, -0x1a0(%rbp)
movq -0x1a0(%rbp), %rax
movslq -0x94(%rbp), %rcx
cmpq %rcx, %rax
jae 0x1f197
movq -0x238(%rbp), %rdi
addq $0x158, %rdi            # imm = 0x158
movq -0x1a0(%rbp), %rsi
callq 0xc6a0
movsd (%rax), %xmm0
movsd %xmm0, -0x320(%rbp)
movq -0x1a0(%rbp), %rsi
leaq -0x148(%rbp), %rdi
callq 0x15d30
movq %rax, %rdi
movq -0x198(%rbp), %rsi
callq 0xc6a0
movsd -0x320(%rbp), %xmm0
movq -0x238(%rbp), %rdi
movsd (%rax), %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, -0x210(%rbp)
fldl -0x210(%rbp)
fstpt -0x314(%rbp)
addq $0x380, %rdi            # imm = 0x380
movq -0x198(%rbp), %rsi
callq 0x27ad0
fldt -0x314(%rbp)
fldt (%rax)
faddp %st, %st(1)
fstpt (%rax)
movq -0x1a0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x1a0(%rbp)
jmp 0x1f0d7
jmp 0x1f199
movq -0x198(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x198(%rbp)
jmp 0x1f09f
jmp 0x1f1b2
leaq 0xa67f0(%rip), %rax     # 0xc59a9
testb $0x1, (%rax)
je 0x1f1ce
movq -0x238(%rbp), %rdi
callq 0x203a0
jmp 0x1f1cc
jmp 0x1f1ce
jmp 0x1f1e6
movq -0x238(%rbp), %rax
movl 0x3b0(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x3b0(%rax)
movq $0x0, -0x1f0(%rbp)
movq -0x1f0(%rbp), %rax
movq %rax, -0x328(%rbp)
movq -0x158(%rbp), %rdi
callq 0x15d80
movq %rax, %rcx
movq -0x328(%rbp), %rax
cmpq %rcx, %rax
jae 0x1f4dd
leaq 0xa677d(%rip), %rax     # 0xc59a2
testb $0x1, (%rax)
je 0x1f25c
movq -0x158(%rbp), %rdi
movq -0x1f0(%rbp), %rsi
callq 0x27af0
movq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0xa6a0
movsd (%rax), %xmm0
callq 0x6870
movsd %xmm0, -0x1c0(%rbp)
jmp 0x1f287
movq -0x158(%rbp), %rdi
movq -0x1f0(%rbp), %rsi
callq 0x27af0
movq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0xa6a0
movsd (%rax), %xmm0
movsd %xmm0, -0x1c0(%rbp)
movq -0x158(%rbp), %rdi
movq -0x1f0(%rbp), %rsi
callq 0x27af0
movq %rax, %rdi
movl $0x1, %esi
callq 0xa6a0
movsd (%rax), %xmm0
movsd %xmm0, -0x1c8(%rbp)
movq -0x158(%rbp), %rdi
movq -0x1f0(%rbp), %rsi
callq 0x27af0
movq %rax, %rdi
movl $0x2, %esi
callq 0xa6a0
movsd (%rax), %xmm0
movsd %xmm0, -0x1d0(%rbp)
movq -0x158(%rbp), %rdi
movq -0x1f0(%rbp), %rsi
callq 0x27af0
movq %rax, %rdi
movl $0x3, %esi
callq 0xa6a0
movsd (%rax), %xmm0
movsd %xmm0, -0x1d8(%rbp)
movq -0x158(%rbp), %rdi
movq -0x1f0(%rbp), %rsi
callq 0x27af0
movq %rax, %rdi
movl $0x4, %esi
callq 0xa6a0
movsd (%rax), %xmm0
movsd %xmm0, -0x1e0(%rbp)
movq $0x0, -0x1f8(%rbp)
movq -0x1f8(%rbp), %rax
movq %rax, -0x330(%rbp)
movq -0x118(%rbp), %rdi
callq 0x7c90
movq %rax, %rcx
movq -0x330(%rbp), %rax
cmpq %rcx, %rax
jae 0x1f4c4
movq -0x118(%rbp), %rdi
movq -0x1f8(%rbp), %rsi
callq 0xa6a0
movsd (%rax), %xmm0
movsd %xmm0, -0x1b8(%rbp)
movsd -0x10(%rbp), %xmm0
movsd -0x1c0(%rbp), %xmm1
movsd -0x1d0(%rbp), %xmm2
mulsd %xmm2, %xmm1
movsd 0x982f1(%rip), %xmm2    # 0xb76a0
divsd %xmm2, %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, -0x1e8(%rbp)
movsd -0x1b8(%rbp), %xmm0
movsd -0x1c0(%rbp), %xmm1
movsd -0x1d8(%rbp), %xmm2
movsd -0x1e8(%rbp), %xmm3
callq 0x20730
movsd %xmm0, -0x338(%rbp)
jmp 0x1f3ee
movsd -0x338(%rbp), %xmm0
movsd %xmm0, -0x1a8(%rbp)
movsd -0x1c8(%rbp), %xmm0
movsd %xmm0, -0x350(%rbp)
movsd -0x1a8(%rbp), %xmm0
movsd -0x1e0(%rbp), %xmm1
addsd %xmm1, %xmm0
callq 0x68e0
movsd %xmm0, -0x358(%rbp)
movsd -0x1d8(%rbp), %xmm0
movsd %xmm0, -0x360(%rbp)
movsd -0x1e0(%rbp), %xmm0
callq 0x68e0
movsd -0x360(%rbp), %xmm1
movsd -0x358(%rbp), %xmm2
movq -0x238(%rbp), %rdi
movaps %xmm0, %xmm3
movsd -0x350(%rbp), %xmm0
mulsd %xmm3, %xmm1
addsd %xmm2, %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, -0x1b0(%rbp)
fldl -0x1b0(%rbp)
fstpt -0x344(%rbp)
addq $0x380, %rdi            # imm = 0x380
movq -0x1f8(%rbp), %rsi
callq 0x27ad0
fldt -0x344(%rbp)
fldt (%rax)
faddp %st, %st(1)
fstpt (%rax)
movq -0x1f8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x1f8(%rbp)
jmp 0x1f342
jmp 0x1f4c6
movq -0x1f0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x1f0(%rbp)
jmp 0x1f1f1
leaq 0xa64bd(%rip), %rax     # 0xc59a1
testb $0x1, (%rax)
je 0x1f681
leaq -0x110(%rbp), %rdi
callq 0x27b10
movq %rax, -0x368(%rbp)
jmp 0x1f502
movq -0x368(%rbp), %rax
movq %rax, -0x200(%rbp)
movq $0x1, -0x208(%rbp)
movq -0x208(%rbp), %rax
movq %rax, -0x370(%rbp)
movq -0x118(%rbp), %rdi
callq 0x7c90
movq %rax, %rcx
movq -0x370(%rbp), %rax
cmpq %rcx, %rax
jae 0x1f67f
movq -0x238(%rbp), %rax
movsd 0x188(%rax), %xmm0
movsd %xmm0, -0x3a8(%rbp)
movq -0x118(%rbp), %rdi
movq -0x208(%rbp), %rsi
decq %rsi
callq 0xa6a0
movsd (%rax), %xmm0
movsd %xmm0, -0x3b0(%rbp)
movq -0x118(%rbp), %rdi
movq -0x208(%rbp), %rsi
callq 0xa6a0
movsd -0x3b0(%rbp), %xmm0
movq %rax, %rcx
movq -0x238(%rbp), %rax
movsd (%rcx), %xmm1
subsd %xmm1, %xmm0
movaps 0x9818b(%rip), %xmm1    # 0xb7740
por %xmm1, %xmm0
movsd 0x190(%rax), %xmm1
divsd %xmm1, %xmm0
callq 0x6870
movaps %xmm0, %xmm1
movsd -0x3a8(%rbp), %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, -0x220(%rbp)
fldl -0x220(%rbp)
fstpt -0x37c(%rbp)
movq -0x200(%rbp), %rdi
movq -0x208(%rbp), %rsi
decq %rsi
callq 0xa6a0
movq -0x238(%rbp), %rdi
fldl (%rax)
fstpt -0x39c(%rbp)
addq $0x380, %rdi            # imm = 0x380
movq %rdi, -0x390(%rbp)
movq -0x208(%rbp), %rsi
decq %rsi
callq 0x27ad0
fldt -0x39c(%rbp)
movq -0x390(%rbp), %rdi
fldt (%rax)
fsubrp %st, %st(1)
fstpt -0x388(%rbp)
movq -0x208(%rbp), %rsi
callq 0x27ad0
fldt -0x388(%rbp)
fldt -0x37c(%rbp)
fldt (%rax)
fxch %st(1)
fmulp %st, %st(2)
faddp %st, %st(1)
fstpt (%rax)
movq -0x208(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x208(%rbp)
jmp 0x1f51b
jmp 0x1f681
leaq -0x148(%rbp), %rdi
callq 0x16030
leaq -0x110(%rbp), %rdi
callq 0x155c0
addq $0x3b0, %rsp            # imm = 0x3B0
popq %rbp
retq
leaq -0x110(%rbp), %rdi
callq 0x155c0
movq -0x120(%rbp), %rdi
callq 0x6830
nopw (%rax,%rax)