// File: /workspace/github_cpp_makefile_star2_O2/myudelson[P]hmm-scalable/HMMProblemSlicedA.cpp
// Function: HMMProblemSlicedA::doLinearStep(FitBitSlicedA*)
NUMBER HMMProblemSlicedA::doLinearStep(FitBitSlicedA *fb) {
	NPAR i,j,m, z;
    NPAR nS = fb->nS, nO = this->p->nO, nZ = this->p->nZ;
    fb->doLog10ScaleGentle(FBS_GRAD);
	
    NCAT xndat = fb->xndat;
    struct data **x_data = fb->x_data;
    fb->init(FBS_PARcopy);
    
	NUMBER e = this->p->ArmijoSeed; // step seed
	bool compliesArmijo = false;
//	bool compliesWolfe2 = false; // second wolfe condition is turned off
	NUMBER f_xk = HMMProblemSlicedA::getSumLogPOPara(xndat, x_data);
	NUMBER f_xkplus1 = 0;
	
    fb->copy(FBS_PAR, FBS_PARcopy);
	// compute p_k * -p_k
	NUMBER p_k_by_neg_p_k = 0;
    for(i=0; i<nS; i++)
    {
        if(fb->pi != NULL)
            p_k_by_neg_p_k -= fb->gradPI[i]*fb->gradPI[i];
        if(fb->A  != NULL)
            for(z=0; z<nZ; z++)
                for(j=0; j<nS; j++)
                    p_k_by_neg_p_k -= fb->gradA[z][i][j]*fb->gradA[z][i][j];
        if(fb->B  != NULL)
            for(m=0; m<nO; m++)
                p_k_by_neg_p_k -= fb->gradB[i][m]*fb->gradB[i][m];
    }
	int iter = 0; // limit iter steps to 20, via ArmijoMinStep (now 10)
	while( !(compliesArmijo /*&& compliesWolfe2*/) && e > this->p->ArmijoMinStep) {
		// update
        for(i=0; i<nS; i++) {
            if(fb->pi != NULL) fb->pi[i] = fb->PIcopy[i] - e * fb->gradPI[i];
            if(fb->A  != NULL)
                for(z=0; z<nZ; z++)
                    for(j=0; j<nS; j++)
                        fb->A[z][i][j] = fb->Acopy[z][i][j] - e * fb->gradA[z][i][j];
            if(fb->B  != NULL)
                for(m=0; m<nO; m++)
                    fb->B[i][m] = fb->Bcopy[i][m] - e * fb->gradB[i][m];
        }
        // project parameters to simplex if needs be
        if(fb->projecttosimplex==1) {
            // scale
            if( !this->hasNon01Constraints() ) {
                if(fb->pi != NULL) projectsimplex(fb->pi, nS);
                for(i=0; i<nS; i++) {
                    for(z=0; z<nZ; z++)
                        if(fb->A  != NULL) projectsimplex(fb->A[z][i], nS);
                    if(fb->B  != NULL) projectsimplex(fb->B[i], nS);
                }
            } else {
                if(fb->pi != NULL) projectsimplexbounded(fb->pi, this->getLbPI(), this->getUbPI(), nS);
                for(i=0; i<nS; i++) {
                    for(z=0; z<nZ; z++)
                        if(fb->A  != NULL) projectsimplexbounded(fb->A[z][i], this->getLbA()[z][i], this->getUbA()[z][i], nS); // tag is current slice
                    if(fb->B  != NULL) projectsimplexbounded(fb->B[i], this->getLbB()[i], this->getUbB()[i], nO);
                }
            }
        }
        
		// recompute alpha and p(O|param)
		computeAlphaAndPOParam(xndat, x_data);
		// compute f(x_{k+1})
		f_xkplus1 = HMMProblemSlicedA::getSumLogPOPara(xndat, x_data);
		// compute Armijo compliance
		compliesArmijo = (f_xkplus1 <= (f_xk + (this->p->ArmijoC1 * e * p_k_by_neg_p_k)));
        
//        // compute Wolfe 2
//        NUMBER p_k_by_neg_p_kp1 = 0;
//        computeGradients(fb);
//        fb->doLog10ScaleGentle(FBS_GRAD);
//        for(i=0; i<nS; i++)
//        {
//            if(fb->pi != NULL) p_k_by_neg_p_kp1 -= fb->gradPI[i]*fb->gradPI[i];
//            if(fb->A  != NULL)
//                for(z=0; z<nZ; z++)
//                    for(j=0; j<nS; j++)
//                        p_k_by_neg_p_kp1 -= fb->gradA[z][i][j]*fb->gradA[z][i][j];
//            if(fb->B  != NULL)
//                for(z=0; z<nZ; z++)
//                    for(m=0; m<nO; m++)
//                        p_k_by_neg_p_kp1 -= fb->gradB[z][i][m]*fb->gradB[z][i][m];
//        }
//        compliesWolfe2 = (p_k_by_neg_p_kp1 >= this->p->ArmijoC2 * p_k_by_neg_p_k); // Wolfe condition
//        // OR
//        compliesWolfe2 = fabs(p_k_by_neg_p_kp1) <= fabs(this->p->ArmijoC2 * p_k_by_neg_p_k); // strong Wolfe condition
        
		e /= (compliesArmijo /*&& compliesWolfe2*/)?1:this->p->ArmijoReduceFactor;
		iter++;
	} // armijo loop
    if(!(compliesArmijo /*&& compliesWolfe2*/)) { // we couldn't step away from current, copy the inital point back
        e = 0;
        fb->copy(FBS_PARcopy, FBS_PAR);
        f_xkplus1 = f_xk;
    }
    fb->destroy(FBS_PARcopy);
    return f_xkplus1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rsi, %rbx
movq %rdi, %r14
movzbl 0x1(%rsi), %eax
movl %eax, (%rsp)
movq 0x88(%rdi), %rax
movzbl 0x8b9(%rax), %ebp
movl 0x8c8(%rax), %r12d
pushq $0x3
popq %rsi
movq %rbx, %rdi
callq 0x9d84
movl 0xd0(%rbx), %r15d
movq 0xd8(%rbx), %r13
pushq $0x5
popq %rsi
movq %rbx, %rdi
callq 0x99dc
movq 0x88(%r14), %rax
movsd 0x968(%rax), %xmm0
movapd %xmm0, 0x30(%rsp)
movq %r14, 0x8(%rsp)
movq %r14, %rdi
movl %r15d, 0x18(%rsp)
movl %r15d, %esi
movq %r13, 0x28(%rsp)
movq %r13, %rdx
callq 0xe400
movsd %xmm0, 0x20(%rsp)
pushq $0x1
popq %rsi
movq %rbx, %rdi
pushq $0x5
popq %rdx
callq 0x9b06
movl %ebp, %r8d
movl (%rsp), %edi
xorl %eax, %eax
testb %dil, %dil
movl %edi, %r15d
cmovlel %eax, %r15d
testb %r12b, %r12b
cmovlel %eax, %r12d
testb %r8b, %r8b
cmovlel %eax, %ebp
movl $0xff, %ecx
andl %ecx, %r15d
andl %ecx, %r12d
andl %ecx, %ebp
xorpd %xmm3, %xmm3
cmpq %r15, %rax
je 0x290eb
cmpq $0x0, 0x10(%rbx)
je 0x29083
movq 0x58(%rbx), %rcx
movsd (%rcx,%rax,8), %xmm0
mulsd %xmm0, %xmm0
subsd %xmm0, %xmm3
cmpq $0x0, 0x18(%rbx)
je 0x290bb
xorl %ecx, %ecx
cmpq %r12, %rcx
je 0x290bb
xorl %edx, %edx
cmpq %rdx, %r15
je 0x290b6
movq 0x60(%rbx), %rsi
movq (%rsi,%rcx,8), %rsi
movq (%rsi,%rax,8), %rsi
movsd (%rsi,%rdx,8), %xmm0
mulsd %xmm0, %xmm0
subsd %xmm0, %xmm3
incq %rdx
jmp 0x29093
incq %rcx
jmp 0x2908c
cmpq $0x0, 0x20(%rbx)
je 0x290e3
xorl %ecx, %ecx
cmpq %rcx, %rbp
je 0x290e3
movq 0x68(%rbx), %rdx
movq (%rdx,%rax,8), %rdx
movsd (%rdx,%rcx,8), %xmm0
mulsd %xmm0, %xmm0
subsd %xmm0, %xmm3
incq %rcx
jmp 0x290c4
incq %rax
jmp 0x29062
xorpd %xmm1, %xmm1
movsbl %r8b, %eax
movl %eax, 0x1c(%rsp)
movsbl %dil, %eax
movl %eax, (%rsp)
xorl %eax, %eax
movapd 0x30(%rsp), %xmm2
movsd %xmm3, 0x48(%rsp)
testb $0x1, %al
jne 0x29404
movq 0x8(%rsp), %rax
movq 0x88(%rax), %rax
ucomisd 0x970(%rax), %xmm2
jbe 0x293f0
movapd %xmm2, %xmm0
xorpd 0x3806(%rip), %xmm0     # 0x2c940
xorl %eax, %eax
cmpq %r15, %rax
je 0x2920b
movq 0x10(%rbx), %rcx
testq %rcx, %rcx
je 0x2916c
movq 0x58(%rbx), %rdx
movq 0x88(%rbx), %rsi
movsd (%rdx,%rax,8), %xmm1
mulsd %xmm0, %xmm1
addsd (%rsi,%rax,8), %xmm1
movsd %xmm1, (%rcx,%rax,8)
movq 0x18(%rbx), %rcx
testq %rcx, %rcx
je 0x291c4
xorl %edx, %edx
cmpq %r12, %rdx
je 0x291c4
xorl %esi, %esi
cmpq %rsi, %r15
je 0x291bf
movq 0x60(%rbx), %rdi
movq 0x90(%rbx), %r8
movq (%r8,%rdx,8), %r8
movq (%r8,%rax,8), %r8
movq (%rdi,%rdx,8), %rdi
movq (%rdi,%rax,8), %rdi
movsd (%rdi,%rsi,8), %xmm1
mulsd %xmm0, %xmm1
addsd (%r8,%rsi,8), %xmm1
movq (%rcx,%rdx,8), %rdi
movq (%rdi,%rax,8), %rdi
movsd %xmm1, (%rdi,%rsi,8)
incq %rsi
jmp 0x2917e
incq %rdx
jmp 0x29177
movq 0x20(%rbx), %rcx
testq %rcx, %rcx
je 0x29203
xorl %edx, %edx
cmpq %rdx, %rbp
je 0x29203
movq 0x68(%rbx), %rsi
movq 0x98(%rbx), %rdi
movq (%rdi,%rax,8), %rdi
movq (%rsi,%rax,8), %rsi
movsd (%rsi,%rdx,8), %xmm1
mulsd %xmm0, %xmm1
addsd (%rdi,%rdx,8), %xmm1
movq (%rcx,%rax,8), %rsi
movsd %xmm1, (%rsi,%rdx,8)
incq %rdx
jmp 0x291cf
incq %rax
jmp 0x2913c
movapd %xmm2, 0x30(%rsp)
cmpb $0x1, 0xe0(%rbx)
jne 0x29377
movq 0x8(%rsp), %rdi
callq 0xe1de
movq 0x10(%rbx), %r13
testb %al, %al
je 0x2931f
testq %r13, %r13
je 0x29266
movq 0x8(%rsp), %r14
movq %r14, %rdi
callq 0xe2a0
movq %rax, 0x10(%rsp)
movq %r14, %rdi
callq 0xe2d6
movq %r13, %rdi
movq 0x10(%rsp), %rsi
movq %rax, %rdx
movl (%rsp), %ecx
callq 0x5d80
xorl %r13d, %r13d
cmpq %r15, %r13
je 0x29377
xorl %r14d, %r14d
cmpq %r14, %r12
je 0x292d0
movq 0x18(%rbx), %rax
testq %rax, %rax
je 0x292cb
movq 0x8(%rsp), %rdx
cmpb $0x0, 0x80(%rdx)
movq (%rax,%r14,8), %rax
movq 0x98(%rdx), %rcx
movl $0x0, %r8d
cmoveq %r8, %rcx
movq (%rax,%r13,8), %rdi
movq (%rcx,%r14,8), %rax
movq (%rax,%r13,8), %rsi
movq 0xa0(%rdx), %rax
cmoveq %r8, %rax
movq (%rax,%r14,8), %rax
movq (%rax,%r13,8), %rdx
movl (%rsp), %ecx
callq 0x5d80
incq %r14
jmp 0x29275
movq 0x20(%rbx), %rax
testq %rax, %rax
je 0x29317
movq (%rax,%r13,8), %rax
movq %rax, 0x10(%rsp)
movq 0x8(%rsp), %r14
movq %r14, %rdi
callq 0xe2c4
movq (%rax,%r13,8), %rax
movq %rax, 0x50(%rsp)
movq %r14, %rdi
callq 0xe2fa
movq (%rax,%r13,8), %rdx
movq 0x10(%rsp), %rdi
movq 0x50(%rsp), %rsi
movl 0x1c(%rsp), %ecx
callq 0x5d80
incq %r13
jmp 0x29269
testq %r13, %r13
je 0x2932f
movq %r13, %rdi
movl (%rsp), %esi
callq 0x5600
xorl %r13d, %r13d
cmpq %r15, %r13
je 0x29377
xorl %r14d, %r14d
cmpq %r14, %r12
je 0x2935d
movq 0x18(%rbx), %rax
testq %rax, %rax
je 0x29358
movq (%rax,%r14,8), %rax
movq (%rax,%r13,8), %rdi
movl (%rsp), %esi
callq 0x5600
incq %r14
jmp 0x2933a
movq 0x20(%rbx), %rax
testq %rax, %rax
je 0x29372
movq (%rax,%r13,8), %rdi
movl (%rsp), %esi
callq 0x5600
incq %r13
jmp 0x29332
movq 0x8(%rsp), %r14
movq %r14, %rdi
movl 0x18(%rsp), %r13d
movl %r13d, %esi
movq 0x28(%rsp), %rdx
callq 0x25848
movq %r14, %rdi
movl %r13d, %esi
movq 0x28(%rsp), %rdx
callq 0xe400
movapd %xmm0, %xmm1
movq 0x88(%r14), %rcx
movsd 0x950(%rcx), %xmm0
movapd 0x30(%rsp), %xmm2
mulsd %xmm2, %xmm0
movsd 0x48(%rsp), %xmm3
mulsd %xmm3, %xmm0
addsd 0x20(%rsp), %xmm0
ucomisd %xmm1, %xmm0
setae %al
movsd 0x3513(%rip), %xmm0     # 0x2c8f0
jae 0x293e7
movsd 0x960(%rcx), %xmm0
divsd %xmm0, %xmm2
jmp 0x2910c
pushq $0x5
popq %rsi
pushq $0x1
popq %rdx
movq %rbx, %rdi
callq 0x9b06
movsd 0x20(%rsp), %xmm1
movsd %xmm1, (%rsp)
pushq $0x5
popq %rsi
movq %rbx, %rdi
callq 0x9a8c
movsd (%rsp), %xmm0
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq