// File: /workspace/github_c_makefile_star2_O1/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, -0x38(%rbp)
movq %rcx, -0x68(%rbp)
movq %rdx, -0x70(%rbp)
movq %rsi, -0x30(%rbp)
movq %rdi, -0x58(%rbp)
movslq 0x18(%rbp), %r14
leaq (,%r14,8), %rdi
callq 0x21d0
movq %rax, %rbx
movl %r14d, %r12d
movq %r14, -0x48(%rbp)
testq %r14, %r14
jle 0x2a752
movq 0x10(%rbp), %r14
addq $0x8, %r14
xorl %r15d, %r15d
movl $0x2c, %edi
callq 0x21d0
movq %rax, (%rbx,%r15,8)
movss (%r13,%r15,4), %xmm0
movss %xmm0, (%rax)
movl $0x3f800000, 0x4(%rax)  # imm = 0x3F800000
movss -0x8(%r14), %xmm0
movss -0x4(%r14), %xmm1
movss (%r14), %xmm2
movss %xmm0, 0x8(%rax)
movss %xmm1, 0xc(%rax)
movss %xmm2, 0x10(%rax)
movaps %xmm0, %xmm3
mulss %xmm0, %xmm3
movss %xmm3, 0x14(%rax)
movaps %xmm0, %xmm3
mulss %xmm1, %xmm3
movss %xmm3, 0x18(%rax)
mulss %xmm2, %xmm0
movss %xmm0, 0x1c(%rax)
movaps %xmm1, %xmm0
mulss %xmm1, %xmm0
movss %xmm0, 0x20(%rax)
mulss %xmm2, %xmm1
movss %xmm1, 0x24(%rax)
mulss %xmm2, %xmm2
movss %xmm2, 0x28(%rax)
incq %r15
addq $0xc, %r14
cmpq %r15, %r12
jne 0x2a6c3
leaq 0x2f1c0(%rip), %rsi     # 0x59919
leaq -0x120(%rbp), %rdi
movq 0x20(%rbp), %r14
movq %r14, %rdx
xorl %eax, %eax
callq 0x2260
cmpl $0x0, 0x4ec93(%rip)     # 0x79408
movq -0x48(%rbp), %r15
je 0x2a792
movq %rbx, %rdi
movq -0x38(%rbp), %rsi
movl %r15d, %edx
movl $0xb, %ecx
movq %r14, %r8
callq 0x2aa3d
testl %r15d, %r15d
movq -0x30(%rbp), %r14
jle 0x2a7ad
leaq (,%r12,4), %rdx
movq %r14, %rdi
xorl %esi, %esi
callq 0x2110
movss 0x2edeb(%rip), %xmm0    # 0x595a0
movq %r14, %rdi
movq -0x38(%rbp), %rsi
movl %r15d, %edx
callq 0x2ab3f
movl %r15d, %r13d
subl %eax, %r13d
leaq 0x2f155(%rip), %rdi     # 0x59926
xorl %r14d, %r14d
movl %eax, %esi
xorl %eax, %eax
callq 0x20f0
movq %rsp, -0x60(%rbp)
leaq 0xf(,%r13,4), %rax
andq $-0x10, %rax
movq %rsp, %rcx
subq %rax, %rcx
movq %rcx, -0x50(%rbp)
movq %rcx, %rsp
movq %r13, -0x40(%rbp)
movslq %r13d, %rdi
shlq $0x3, %rdi
callq 0x21d0
movq %rax, %r13
testl %r15d, %r15d
movq -0x30(%rbp), %rax
jle 0x2a86e
xorl %r15d, %r15d
cmpl $0x0, (%rax,%r15,4)
jne 0x2a866
movl $0x2c, %edi
callq 0x21d0
movslq %r14d, %r14
movq %rax, (%r13,%r14,8)
movq (%rbx,%r15,8), %rcx
xorl %edx, %edx
movss (%rcx,%rdx,4), %xmm0
movss %xmm0, (%rax,%rdx,4)
incq %rdx
cmpq $0xb, %rdx
jne 0x2a838
movq -0x38(%rbp), %rax
movss (%rax,%r15,4), %xmm0
movq -0x50(%rbp), %rax
movss %xmm0, (%rax,%r14,4)
incl %r14d
movq -0x30(%rbp), %rax
incq %r15
cmpq %r12, %r15
jne 0x2a819
leaq 0x2f1fb(%rip), %rdi     # 0x59a70
callq 0x2090
leaq -0x30(%rsp), %r14
movq %r14, %rsp
movl $0x2c, %edi
callq 0x21d0
leaq -0xb8(%rbp), %rdi
movq %rax, (%rdi)
subq $0x8, %rsp
movsbl 0x28(%rbp), %eax
leaq -0x120(%rbp), %rcx
movq -0x68(%rbp), %r15
movq %r15, %rsi
movq %r14, %rdx
movq %r13, %r8
movq -0x50(%rbp), %r9
pushq %rax
pushq $0xb
pushq -0x40(%rbp)
callq 0x2aff0
addq $0x20, %rsp
movq -0x58(%rbp), %rax
movss %xmm0, (%rax)
movq %rsp, %rcx
leaq -0x60(%rcx), %rax
movq %rax, %rsp
xorpd %xmm0, %xmm0
movapd %xmm0, -0x20(%rcx)
movapd %xmm0, -0x30(%rcx)
movapd %xmm0, -0x40(%rcx)
movapd %xmm0, -0x50(%rcx)
movapd %xmm0, -0x60(%rcx)
movq $0x0, -0x10(%rcx)
movq -0x48(%rbp), %r14
testl %r14d, %r14d
jle 0x2a957
movq -0xb8(%rbp), %rcx
xorl %edx, %edx
movq (%rbx,%rdx,8), %rsi
xorpd %xmm0, %xmm0
xorl %edi, %edi
movss (%rsi,%rdi,4), %xmm1
movss (%rcx,%rdi,4), %xmm2
mulss %xmm1, %xmm2
cvtss2sd %xmm2, %xmm2
cvtss2sd %xmm1, %xmm1
addsd (%rax,%rdi,8), %xmm1
addsd %xmm2, %xmm0
movsd %xmm1, (%rax,%rdi,8)
incq %rdi
cmpq $0xb, %rdi
jne 0x2a918
cvtsd2ss %xmm0, %xmm0
movss %xmm0, (%r15,%rdx,4)
incq %rdx
cmpq %r12, %rdx
jne 0x2a90e
movq -0xb8(%rbp), %rdi
movss (%rdi), %xmm2
xorpd %xmm0, %xmm0
xorl %ecx, %ecx
xorps %xmm1, %xmm1
cvtss2sd (%rdi,%rcx,4), %xmm1
mulsd (%rax,%rcx,8), %xmm1
addsd %xmm1, %xmm0
incq %rcx
cmpq $0xb, %rcx
jne 0x2a968
movss %xmm2, -0x30(%rbp)
xorps %xmm1, %xmm1
cvtss2sd %xmm2, %xmm1
mulsd (%rax), %xmm1
divsd %xmm0, %xmm1
cvtsd2ss %xmm1, %xmm1
xorps %xmm2, %xmm2
cvtsi2ss %r14d, %xmm2
movq -0x70(%rbp), %rax
movss %xmm1, (%rax)
mulss 0x4(%rdi), %xmm2
cvtss2sd %xmm2, %xmm2
divsd %xmm0, %xmm2
xorps %xmm0, %xmm0
cvtsd2ss %xmm2, %xmm0
movss %xmm0, 0x4(%rax)
movss 0x26645(%rip), %xmm2    # 0x51010
subss %xmm1, %xmm2
subss %xmm0, %xmm2
movss %xmm2, 0x8(%rax)
callq 0x2050
testl %r14d, %r14d
jle 0x2a9f6
xorl %r14d, %r14d
movq (%rbx,%r14,8), %rdi
callq 0x2050
incq %r14
cmpq %r14, %r12
jne 0x2a9e5
movq %rbx, %rdi
callq 0x2050
movq -0x40(%rbp), %rax
testl %eax, %eax
jle 0x2aa1d
movl %eax, %ebx
xorl %r14d, %r14d
movq (%r13,%r14,8), %rdi
callq 0x2050
incq %r14
cmpq %r14, %rbx
jne 0x2aa0b
movq %r13, %rdi
callq 0x2050
movq -0x60(%rbp), %rsp
movss -0x30(%rbp), %xmm0
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq