// File: /workspace/github_cpp_makefile_star2_O1/myudelson[P]hmm-scalable/HMMProblemSlicedAB.cpp
// Function: HMMProblemSlicedAB::doLinearStep(FitBitSlicedAB*)
NUMBER HMMProblemSlicedAB::doLinearStep(FitBitSlicedAB *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 = HMMProblemSlicedAB::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(j=0; j<nS; j++)
                for(z=0; z<nZ; z++)
                    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++)
                for(z=0; z<nZ; z++)
                    p_k_by_neg_p_k -= fb->gradB[z][i][m]*fb->gradB[z][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(j=0; j<nS; j++)
                    for(z=0; z<nZ; z++)
                        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++)
                    for(z=0; z<nZ; z++)
                        fb->B[z][i][m] = fb->Bcopy[z][i][m] - e * fb->gradB[z][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[z][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[z][i], this->getLbB()[z][i], this->getUbB()[z][i], nO);
                    }
                }
            }
        }
        
		// recompute alpha and p(O|param)
		computeAlphaAndPOParam(xndat, x_data);
		// compute f(x_{k+1})
		f_xkplus1 = HMMProblemSlicedAB::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 $0x38, %rsp
movq %rsi, %rbx
movq %rdi, %r14
movsbq 0x1(%rsi), %r15
movq 0x88(%rdi), %rax
movzbl 0x8b9(%rax), %r13d
movzbl 0x8c8(%rax), %ebp
movq %rsi, %rdi
movl $0x3, %esi
callq 0xd7a6
movl 0xd0(%rbx), %r12d
movq 0xd8(%rbx), %rax
movq %rax, 0x8(%rsp)
movq %rbx, %rdi
movl $0x5, %esi
callq 0xd392
movq 0x88(%r14), %rax
movsd 0x968(%rax), %xmm0
movapd %xmm0, 0x20(%rsp)
movq %r14, %rdi
movl %r12d, 0x14(%rsp)
movl %r12d, %esi
movq 0x8(%rsp), %rdx
callq 0x1161c
movsd %xmm0, 0x18(%rsp)
movq %rbx, %rdi
movl $0x1, %esi
movl $0x5, %edx
callq 0xd4bc
xorpd %xmm0, %xmm0
xorpd %xmm3, %xmm3
testq %r15, %r15
jle 0x2a130
movq 0x10(%rbx), %rax
movq 0x18(%rbx), %rcx
cmpq $0x0, 0x20(%rbx)
sete %sil
testb %r13b, %r13b
setle %dl
orb %sil, %dl
xorl %esi, %esi
testq %rax, %rax
je 0x2a0b3
movq 0x58(%rbx), %rdi
movsd (%rdi,%rsi,8), %xmm1
mulsd %xmm1, %xmm1
subsd %xmm1, %xmm3
testq %rcx, %rcx
je 0x2a0ec
xorl %edi, %edi
testb %bpl, %bpl
jle 0x2a0e4
movq 0x60(%rbx), %r8
xorl %r9d, %r9d
movq (%r8,%r9,8), %r10
movq (%r10,%rsi,8), %r10
movsd (%r10,%rdi,8), %xmm1
mulsd %xmm1, %xmm1
subsd %xmm1, %xmm3
incq %r9
cmpq %r9, %rbp
jne 0x2a0c6
incq %rdi
cmpq %r15, %rdi
jne 0x2a0ba
testb %dl, %dl
jne 0x2a124
xorl %edi, %edi
testb %bpl, %bpl
jle 0x2a11c
movq 0x68(%rbx), %r8
xorl %r9d, %r9d
movq (%r8,%r9,8), %r10
movq (%r10,%rsi,8), %r10
movsd (%r10,%rdi,8), %xmm1
mulsd %xmm1, %xmm1
subsd %xmm1, %xmm3
incq %r9
cmpq %r9, %rbp
jne 0x2a0fe
incq %rdi
cmpq %r13, %rdi
jne 0x2a0f2
incq %rsi
cmpq %r15, %rsi
jne 0x2a09d
movq 0x88(%r14), %rax
movapd 0x20(%rsp), %xmm2
ucomisd 0x970(%rax), %xmm2
jbe 0x2a48a
movq %r14, (%rsp)
movsd %xmm3, 0x30(%rsp)
testb %r15b, %r15b
jle 0x2a26c
testb %r13b, %r13b
setle %dil
movq 0x10(%rbx), %rax
movq 0x18(%rbx), %rcx
movapd %xmm2, %xmm0
xorpd 0xa7c7(%rip), %xmm0     # 0x34940
movq 0x20(%rbx), %rdx
testq %rdx, %rdx
sete %sil
orb %dil, %sil
xorl %edi, %edi
testq %rax, %rax
je 0x2a1ae
movq 0x58(%rbx), %r8
movq 0x88(%rbx), %r9
movsd (%r8,%rdi,8), %xmm1
mulsd %xmm0, %xmm1
addsd (%r9,%rdi,8), %xmm1
movsd %xmm1, (%rax,%rdi,8)
testq %rcx, %rcx
je 0x2a207
xorl %r8d, %r8d
testb %bpl, %bpl
jle 0x2a1ff
movq 0x60(%rbx), %r9
movq 0x90(%rbx), %r10
xorl %r11d, %r11d
movq (%r10,%r11,8), %r14
movq (%r14,%rdi,8), %r14
movq (%r9,%r11,8), %r12
movq (%r12,%rdi,8), %r12
movsd (%r12,%r8,8), %xmm1
mulsd %xmm0, %xmm1
addsd (%r14,%r8,8), %xmm1
movq (%rcx,%r11,8), %r14
movq (%r14,%rdi,8), %r14
movsd %xmm1, (%r14,%r8,8)
incq %r11
cmpq %r11, %rbp
jne 0x2a1c9
incq %r8
cmpq %r15, %r8
jne 0x2a1b6
testb %sil, %sil
jne 0x2a260
xorl %r8d, %r8d
testb %bpl, %bpl
jle 0x2a258
movq 0x68(%rbx), %r9
movq 0x98(%rbx), %r10
xorl %r11d, %r11d
movq (%r10,%r11,8), %r14
movq (%r14,%rdi,8), %r14
movq (%r9,%r11,8), %r12
movq (%r12,%rdi,8), %r12
movsd (%r12,%r8,8), %xmm1
mulsd %xmm0, %xmm1
addsd (%r14,%r8,8), %xmm1
movq (%rdx,%r11,8), %r14
movq (%r14,%rdi,8), %r14
movsd %xmm1, (%r14,%r8,8)
incq %r11
cmpq %r11, %rbp
jne 0x2a222
incq %r8
cmpq %r13, %r8
jne 0x2a20f
incq %rdi
cmpq %r15, %rdi
jne 0x2a189
movapd %xmm2, 0x20(%rsp)
cmpb $0x1, 0xe0(%rbx)
jne 0x2a3fe
movq (%rsp), %r14
movq %r14, %rdi
callq 0x114a8
testb %al, %al
je 0x2a2b6
movq 0x10(%rbx), %rdi
testq %rdi, %rdi
je 0x2a32e
cmpb $0x0, 0x80(%r14)
je 0x2a322
movq 0xa0(%r14), %rsi
movq 0xb8(%r14), %rdx
jmp 0x2a326
movq 0x10(%rbx), %rdi
testq %rdi, %rdi
je 0x2a2c7
movl %r15d, %esi
callq 0x692d
testb %r15b, %r15b
jle 0x2a3fe
xorl %r14d, %r14d
testb %bpl, %bpl
jle 0x2a315
xorl %r12d, %r12d
movq 0x18(%rbx), %rax
testq %rax, %rax
je 0x2a2f4
movq (%rax,%r12,8), %rax
movq (%rax,%r14,8), %rdi
movl %r15d, %esi
callq 0x692d
movq 0x20(%rbx), %rax
testq %rax, %rax
je 0x2a30d
movq (%rax,%r12,8), %rax
movq (%rax,%r14,8), %rdi
movl %r15d, %esi
callq 0x692d
incq %r12
cmpq %r12, %rbp
jne 0x2a2db
incq %r14
cmpq %r15, %r14
jne 0x2a2d3
jmp 0x2a3fe
xorl %esi, %esi
xorl %edx, %edx
movl %r15d, %ecx
callq 0x79f5
testb %r15b, %r15b
jle 0x2a3fe
xorl %r12d, %r12d
testb %bpl, %bpl
jle 0x2a3f2
xorl %r14d, %r14d
movq 0x18(%rbx), %rax
testq %rax, %rax
je 0x2a396
movq (%rsp), %rdx
cmpb $0x0, 0x80(%rdx)
movq (%rax,%r14,8), %rax
movq 0xa8(%rdx), %rcx
movl $0x0, %r8d
cmoveq %r8, %rcx
movq (%rax,%r12,8), %rdi
movq (%rcx,%r14,8), %rax
movq (%rax,%r12,8), %rsi
movq 0xc0(%rdx), %rax
cmoveq %r8, %rax
movq (%rax,%r14,8), %rax
movq (%rax,%r12,8), %rdx
movl %r15d, %ecx
callq 0x79f5
movq 0x20(%rbx), %rax
testq %rax, %rax
je 0x2a3e6
movq (%rax,%r14,8), %rax
movq (%rsp), %rcx
cmpb $0x0, 0x80(%rcx)
movq (%rax,%r12,8), %rdi
movq 0xb0(%rcx), %rax
movl $0x0, %edx
cmoveq %rdx, %rax
movq (%rax,%r14,8), %rax
movq 0xc8(%rcx), %rcx
cmoveq %rdx, %rcx
movq (%rax,%r12,8), %rsi
movq (%rcx,%r14,8), %rax
movq (%rax,%r12,8), %rdx
movsbl %r13b, %ecx
callq 0x79f5
incq %r14
cmpq %r14, %rbp
jne 0x2a346
incq %r12
cmpq %r15, %r12
jne 0x2a33a
movq (%rsp), %r14
movq %r14, %rdi
movl 0x14(%rsp), %r12d
movl %r12d, %esi
movq 0x8(%rsp), %rdx
callq 0x25d92
movq %r14, %rdi
movl %r12d, %esi
movq 0x8(%rsp), %rdx
callq 0x1161c
movq 0x88(%r14), %rcx
movsd 0x950(%rcx), %xmm4
movapd 0x20(%rsp), %xmm2
mulsd %xmm2, %xmm4
movsd 0x30(%rsp), %xmm3
mulsd %xmm3, %xmm4
addsd 0x18(%rsp), %xmm4
ucomisd %xmm0, %xmm4
setae %al
movsd 0xa491(%rip), %xmm1     # 0x348f0
jae 0x2a469
movsd 0x960(%rcx), %xmm1
ucomisd %xmm0, %xmm4
jae 0x2a4a8
divsd %xmm1, %xmm2
movq 0x88(%r14), %rcx
ucomisd 0x970(%rcx), %xmm2
ja 0x2a155
jmp 0x2a48c
xorl %eax, %eax
testb %al, %al
jne 0x2a4a8
movq %rbx, %rdi
movl $0x5, %esi
movl $0x1, %edx
callq 0xd4bc
movsd 0x18(%rsp), %xmm0
movsd %xmm0, (%rsp)
movq %rbx, %rdi
movl $0x5, %esi
callq 0xd442
movsd (%rsp), %xmm0
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq