// File: /workspace/github_c_makefile_star2_O2/ugobas[P]tnm/Fit_B_factors.c
// Function: Bfactors_fit
float Bfactors_fit(float *r, int *outlier, float *dof,
		   float *B_pred_all, float *B_PDB, float *B_ENM,
		   float *R_eq, int N, char *name, char type)
{
  /* It fits B factors as the combination of the internal motion
     predicted by the ENM and the rigid body motion dr_i = t+I X r_i,
     which leads to
     B^exp_i ~ A0 B^ENM_i + A1(translations) +
               A2*r_ix+A3*r_iy+A+A4*r_iz (rototranslation) +
	       A5*r_ix*r_ix+...A10*r_iz*r_iz (rotations)
     The 11 free parameters A0... A10 are fitted through ridge regression.

     OUTPUT file <name>_Bfact_fit.dat
     For each examined Lambda we report:
     Lambda, RangeRisk, GenCrossVal(Golub_etal_1979), Error, dE/dLambda, A0
     The program outputs results of three kinds of fit:
     (1) B^exp_i ~ A0 B^ENM_i + A1 (only translations, fit for Lambda=0)
     (2) 11 parameter fit for Lambda that minimizes RangeRisk (Mallows, 1973,
     as reported in Golub, Heath & Wahba 1979);
     (3) 11 parameters, for Lambda that maximizes the "specific heat"
     dE/dLambda (Bastolla, unpublished)
     For each fit (1), (2), (3) we report:
     Force constant (A0), Lambda, Relative error of the fit,
     Fraction of B^exp atributed to internal motion, fraction attributed to
     rotation and rototranslation.
   */


  int Npar=11, i, j=0;
  float **Xall=malloc(N*sizeof(float *));
  for(i=0; i<N; i++){
    Xall[i]=malloc(Npar*sizeof(float));
    float *x=Xall[i];
    x[0]=B_ENM[i]; x[1]=1;
    float rx=R_eq[j],ry=R_eq[j+1],rz=R_eq[j+2]; j+=3;
    x[2]=rx; x[3]=ry; x[4]=rz;
    x[5]=rx*rx; x[6]=rx*ry; x[7]=rx*rz;
    x[8]=ry*ry; x[9]=ry*rz; x[10]=rz*rz;
  }
  char nameout[100];
  sprintf(nameout, "%s.Bfact_fit", name);
  if(VBS)Print_data(Xall, B_PDB, N, Npar, name);

  // Eliminate outliers
  for(i=0; i<N; i++)outlier[i]=0;
  int num=N; float sigma=2.5;
  // num-=
  // Removed UGO 03/02/2021
  // It is dangerous to remove outliers in the normal modes because this may
  // prevent from fitting some modes that only move outliers
  num-=Find_outliers_mean(outlier, B_PDB, N, sigma);
  /*float sigma=3;
  num-=Find_outliers_Z(outlier, B_ENM, N, sigma); 
  num-=Find_outliers_Z(outlier, B_PDB, N, sigma);*/

  printf("%d outliers eliminated for B factors fit\n", N-num);
  float Y[num]; int k=0;
  float **X=malloc(num*sizeof(float *));
  for(i=0; i<N; i++){
    if(outlier[i])continue;
    X[k]=malloc(Npar*sizeof(float));
    float *x=X[k], *z=Xall[i];
    for(int a=0; a<Npar; a++)x[a]=z[a];
    Y[k]=B_PDB[i]; k++;
  }

  // Complete fit:
  // char type='M'; C= specific heat M= Max penalty
  printf("Fitting B factors with internal and rigid body motions\n");
  float D_out[Npar];
  struct ridge_fit fit; fit.A=malloc(Npar*sizeof(float));
  *r=Ridge_regression(&fit, B_pred_all, D_out, nameout,
		      X, Y, num, Npar, type);

  // Returned results
  int a; double sum_x[Npar];
  for(a=0; a<Npar; a++)sum_x[a]=0;
  for(i=0; i<N; i++){
    float *Xi=Xall[i]; double Yp=0; 
    for(a=0; a<Npar; a++){
      Yp+=Xi[a]*fit.A[a]; sum_x[a]+=Xi[a];
    }
    B_pred_all[i]=Yp;
  }
  float slope=fit.A[0];
  // Degrees of freedom
  double sum=0; for(a=0; a<Npar; a++)sum+=fit.A[a]*sum_x[a];
  dof[0]=fit.A[0]*sum_x[0]/sum; // Internal fraction
  dof[1]=fit.A[1]*N/sum;  // Translations
  dof[2]=1-dof[0]-dof[1]; // Rotations*/
  free(fit.A);
  for(i=0; i<N; i++){free(Xall[i]);} free(Xall);
  for(i=0; i<num; i++){free(X[i]);} free(X);
  return(slope);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq %r9, %r13
movq %r8, -0x30(%rbp)
movq %rcx, -0x68(%rbp)
movq %rdx, -0x70(%rbp)
movq %rsi, %r15
movq %rdi, -0x50(%rbp)
movslq 0x18(%rbp), %r12
movq 0x10(%rbp), %r14
movq %r12, %rdi
shlq $0x3, %rdi
callq 0x21d0
movq %rax, -0x38(%rbp)
xorl %ebx, %ebx
movq %r12, %rax
testl %eax, %eax
movl $0x0, %r12d
movq %rax, -0x40(%rbp)
cmovgl %eax, %r12d
addq $0x8, %r14
cmpq %rbx, %r12
je 0x27808
pushq $0x2c
popq %rdi
callq 0x21d0
movq -0x38(%rbp), %rcx
movq %rax, (%rcx,%rbx,8)
movss (%r13,%rbx,4), %xmm0
movss %xmm0, (%rax)
movl $0x3f800000, 0x4(%rax)  # imm = 0x3F800000
movss (%r14), %xmm0
movss %xmm0, 0x10(%rax)
movsd -0x8(%r14), %xmm1
movaps %xmm1, %xmm2
shufps $0x55, %xmm1, %xmm2     # xmm2 = xmm2[1,1],xmm1[1,1]
movaps %xmm1, %xmm3
movlhps %xmm1, %xmm3            # xmm3 = xmm3[0],xmm1[0]
movsd %xmm1, 0x8(%rax)
movaps %xmm0, %xmm4
shufps $0xd4, %xmm1, %xmm4     # xmm4 = xmm4[0,1],xmm1[1,3]
shufps $0x80, %xmm4, %xmm1     # xmm1 = xmm1[0,0],xmm4[0,2]
mulps %xmm3, %xmm1
movups %xmm1, 0x14(%rax)
mulss %xmm0, %xmm2
movss %xmm2, 0x24(%rax)
mulss %xmm0, %xmm0
movss %xmm0, 0x28(%rax)
incq %rbx
addq $0xc, %r14
jmp 0x27792
leaq 0x2c012(%rip), %rsi     # 0x53821
leaq -0x120(%rbp), %rdi
movq 0x20(%rbp), %rbx
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2260
cmpl $0x0, 0x4cbdd(%rip)     # 0x74408
je 0x27844
pushq $0xb
popq %rcx
movq -0x38(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq %rbx, %r8
callq 0x27aaf
xorl %eax, %eax
cmpq %rax, %r12
je 0x27855
andl $0x0, (%r15,%rax,4)
incq %rax
jmp 0x27846
movss 0x2bc4b(%rip), %xmm0    # 0x534a8
movq %r15, %rdi
movq -0x30(%rbp), %rsi
movq -0x40(%rbp), %rbx
movl %ebx, %edx
callq 0x27b9f
subl %eax, %ebx
leaq 0x2bfb6(%rip), %rdi     # 0x5382e
xorl %r14d, %r14d
movl %eax, %esi
xorl %eax, %eax
callq 0x20f0
movq %rsp, -0x58(%rbp)
leaq 0xf(,%rbx,4), %rax
andq $-0x10, %rax
movq %rsp, %rcx
subq %rax, %rcx
movq %rcx, -0x48(%rbp)
movq %rcx, %rsp
movq %rbx, -0x60(%rbp)
movslq %ebx, %rdi
shlq $0x3, %rdi
callq 0x21d0
movq %rax, %r13
xorl %ebx, %ebx
cmpq %r12, %r14
je 0x2790b
cmpl $0x0, (%r15,%r14,4)
jne 0x27906
pushq $0x2c
popq %rdi
callq 0x21d0
movslq %ebx, %rbx
movq %rax, (%r13,%rbx,8)
movq -0x38(%rbp), %rcx
movq (%rcx,%r14,8), %rcx
xorl %edx, %edx
cmpq $0xb, %rdx
je 0x278f1
movss (%rcx,%rdx,4), %xmm0
movss %xmm0, (%rax,%rdx,4)
incq %rdx
jmp 0x278dc
movq -0x30(%rbp), %rax
movss (%rax,%r14,4), %xmm0
movq -0x48(%rbp), %rax
movss %xmm0, (%rax,%rbx,4)
incl %ebx
incq %r14
jmp 0x278b6
leaq 0x2c066(%rip), %rdi     # 0x53978
callq 0x2090
leaq -0x30(%rsp), %r15
movq %r15, %rsp
pushq $0x2c
popq %rdi
callq 0x21d0
leaq -0xb8(%rbp), %rdi
movq %rax, (%rdi)
subq $0x8, %rsp
movsbl 0x28(%rbp), %eax
leaq -0x120(%rbp), %rcx
movq -0x68(%rbp), %rbx
movq %rbx, %rsi
movq %r15, %rdx
movq %r13, %r8
movq -0x48(%rbp), %r9
pushq %rax
pushq $0xb
movq -0x60(%rbp), %r15
pushq %r15
callq 0x27fe8
addq $0x20, %rsp
movq -0x50(%rbp), %rax
movss %xmm0, (%rax)
movq %rsp, %rax
addq $-0x60, %rax
movq %rax, %rsp
xorl %ecx, %ecx
movq -0x38(%rbp), %r14
cmpq $0xb, %rcx
je 0x2798b
andq $0x0, (%rax,%rcx,8)
incq %rcx
jmp 0x2797b
movq -0xb8(%rbp), %rdi
xorl %ecx, %ecx
cmpq %r12, %rcx
je 0x279e0
movq (%r14,%rcx,8), %rdx
xorpd %xmm0, %xmm0
xorl %esi, %esi
cmpq $0xb, %rsi
je 0x279d2
movss (%rdx,%rsi,4), %xmm1
movss (%rdi,%rsi,4), %xmm2
mulss %xmm1, %xmm2
cvtss2sd %xmm2, %xmm2
addsd %xmm2, %xmm0
cvtss2sd %xmm1, %xmm1
addsd (%rax,%rsi,8), %xmm1
movsd %xmm1, (%rax,%rsi,8)
incq %rsi
jmp 0x279a3
cvtsd2ss %xmm0, %xmm0
movss %xmm0, (%rbx,%rcx,4)
incq %rcx
jmp 0x27994
movss (%rdi), %xmm2
xorpd %xmm0, %xmm0
xorl %ecx, %ecx
cmpq $0xb, %rcx
je 0x27a03
cvtss2sd (%rdi,%rcx,4), %xmm1
mulsd (%rax,%rcx,8), %xmm1
addsd %xmm1, %xmm0
incq %rcx
jmp 0x279ea
movss %xmm2, -0x30(%rbp)
cvtss2sd %xmm2, %xmm1
mulsd (%rax), %xmm1
divsd %xmm0, %xmm1
cvtsd2ss %xmm1, %xmm1
cvtsi2ssl -0x40(%rbp), %xmm2
movq -0x70(%rbp), %rax
movss %xmm1, (%rax)
mulss 0x4(%rdi), %xmm2
cvtss2sd %xmm2, %xmm2
divsd %xmm0, %xmm2
cvtsd2ss %xmm2, %xmm0
movss %xmm0, 0x4(%rax)
movss 0x235cd(%rip), %xmm2    # 0x4b010
subss %xmm1, %xmm2
subss %xmm0, %xmm2
movss %xmm2, 0x8(%rax)
callq 0x2050
xorl %ebx, %ebx
cmpq %rbx, %r12
je 0x27a6a
movq (%r14,%rbx,8), %rdi
callq 0x2050
incq %rbx
jmp 0x27a57
movq %r14, %rdi
callq 0x2050
xorl %ebx, %ebx
testl %r15d, %r15d
cmovlel %ebx, %r15d
cmpq %rbx, %r15
je 0x27a8f
movq (%r13,%rbx,8), %rdi
callq 0x2050
incq %rbx
jmp 0x27a7b
movq %r13, %rdi
callq 0x2050
movq -0x58(%rbp), %rsp
movss -0x30(%rbp), %xmm0
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq