// File: /workspace/github_c_makefile_star2_O0/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
subq $0x1a0, %rsp            # imm = 0x1A0
movb 0x28(%rbp), %al
movq 0x20(%rbp), %rax
movl 0x18(%rbp), %eax
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movl $0xb, -0x34(%rbp)
movl $0x0, -0x3c(%rbp)
movslq 0x18(%rbp), %rdi
shlq $0x3, %rdi
callq 0x1180
movq %rax, -0x48(%rbp)
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl 0x18(%rbp), %eax
jge 0x3c248
movslq -0x34(%rbp), %rdi
shlq $0x2, %rdi
callq 0x1180
movq %rax, %rdx
movq -0x48(%rbp), %rax
movslq -0x38(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x48(%rbp), %rax
movslq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x50(%rbp)
movq -0x30(%rbp), %rax
movslq -0x38(%rbp), %rcx
movss (%rax,%rcx,4), %xmm0
movq -0x50(%rbp), %rax
movss %xmm0, (%rax)
movq -0x50(%rbp), %rax
movss 0x39f40(%rip), %xmm0    # 0x76090
movss %xmm0, 0x4(%rax)
movq 0x10(%rbp), %rax
movslq -0x3c(%rbp), %rcx
movss (%rax,%rcx,4), %xmm0
movss %xmm0, -0x54(%rbp)
movq 0x10(%rbp), %rax
movl -0x3c(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movss (%rax,%rcx,4), %xmm0
movss %xmm0, -0x58(%rbp)
movq 0x10(%rbp), %rax
movl -0x3c(%rbp), %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movss (%rax,%rcx,4), %xmm0
movss %xmm0, -0x5c(%rbp)
movl -0x3c(%rbp), %eax
addl $0x3, %eax
movl %eax, -0x3c(%rbp)
movss -0x54(%rbp), %xmm0
movq -0x50(%rbp), %rax
movss %xmm0, 0x8(%rax)
movss -0x58(%rbp), %xmm0
movq -0x50(%rbp), %rax
movss %xmm0, 0xc(%rax)
movss -0x5c(%rbp), %xmm0
movq -0x50(%rbp), %rax
movss %xmm0, 0x10(%rax)
movss -0x54(%rbp), %xmm0
mulss -0x54(%rbp), %xmm0
movq -0x50(%rbp), %rax
movss %xmm0, 0x14(%rax)
movss -0x54(%rbp), %xmm0
mulss -0x58(%rbp), %xmm0
movq -0x50(%rbp), %rax
movss %xmm0, 0x18(%rax)
movss -0x54(%rbp), %xmm0
mulss -0x5c(%rbp), %xmm0
movq -0x50(%rbp), %rax
movss %xmm0, 0x1c(%rax)
movss -0x58(%rbp), %xmm0
mulss -0x58(%rbp), %xmm0
movq -0x50(%rbp), %rax
movss %xmm0, 0x20(%rax)
movss -0x58(%rbp), %xmm0
mulss -0x5c(%rbp), %xmm0
movq -0x50(%rbp), %rax
movss %xmm0, 0x24(%rax)
movss -0x5c(%rbp), %xmm0
mulss -0x5c(%rbp), %xmm0
movq -0x50(%rbp), %rax
movss %xmm0, 0x28(%rax)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x3c0f7
leaq -0xc0(%rbp), %rdi
movq 0x20(%rbp), %rdx
leaq 0x42731(%rip), %rsi     # 0x7e98b
movb $0x0, %al
callq 0x1200
cmpl $0x0, 0x60160(%rip)     # 0x9c3c8
je 0x3c281
movq -0x48(%rbp), %rdi
movq -0x28(%rbp), %rsi
movl 0x18(%rbp), %edx
movl -0x34(%rbp), %ecx
movq 0x20(%rbp), %r8
callq 0x3c7e0
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl 0x18(%rbp), %eax
jge 0x3c2aa
movq -0x10(%rbp), %rax
movslq -0x38(%rbp), %rcx
movl $0x0, (%rax,%rcx,4)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x3c288
movl 0x18(%rbp), %eax
movl %eax, -0xc4(%rbp)
movl $0x40200000, -0xc8(%rbp) # imm = 0x40200000
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
movl 0x18(%rbp), %edx
movss -0xc8(%rbp), %xmm0
callq 0x3c940
movl %eax, %ecx
movl -0xc4(%rbp), %eax
subl %ecx, %eax
movl %eax, -0xc4(%rbp)
movl 0x18(%rbp), %esi
movl -0xc4(%rbp), %eax
subl %eax, %esi
leaq 0x426a1(%rip), %rdi     # 0x7e998
xorl %eax, %eax
callq 0x10d0
movl -0xc4(%rbp), %eax
movq %rsp, %rcx
movq %rcx, -0xd0(%rbp)
leaq 0xf(,%rax,4), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x188(%rbp)
movq %rcx, %rsp
movq %rax, -0xd8(%rbp)
movl $0x0, -0xdc(%rbp)
movslq -0xc4(%rbp), %rdi
shlq $0x3, %rdi
callq 0x1180
movq %rax, -0xe8(%rbp)
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl 0x18(%rbp), %eax
jge 0x3c44f
movq -0x10(%rbp), %rax
movslq -0x38(%rbp), %rcx
cmpl $0x0, (%rax,%rcx,4)
je 0x3c378
jmp 0x3c441
movslq -0x34(%rbp), %rdi
shlq $0x2, %rdi
callq 0x1180
movq %rax, %rdx
movq -0xe8(%rbp), %rax
movslq -0xdc(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0xe8(%rbp), %rax
movslq -0xdc(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0xf0(%rbp)
movq -0x48(%rbp), %rax
movslq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0xf8(%rbp)
movl $0x0, -0xfc(%rbp)
movl -0xfc(%rbp), %eax
cmpl -0x34(%rbp), %eax
jge 0x3c412
movq -0xf8(%rbp), %rax
movslq -0xfc(%rbp), %rcx
movss (%rax,%rcx,4), %xmm0
movq -0xf0(%rbp), %rax
movslq -0xfc(%rbp), %rcx
movss %xmm0, (%rax,%rcx,4)
movl -0xfc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xfc(%rbp)
jmp 0x3c3d0
movq -0x188(%rbp), %rax
movq -0x28(%rbp), %rcx
movslq -0x38(%rbp), %rdx
movss (%rcx,%rdx,4), %xmm0
movslq -0xdc(%rbp), %rcx
movss %xmm0, (%rax,%rcx,4)
movl -0xdc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xdc(%rbp)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x3c359
leaq 0x4256c(%rip), %rdi     # 0x7e9c2
xorl %eax, %eax
callq 0x10d0
movl -0x34(%rbp), %eax
leaq 0xf(,%rax,4), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x198(%rbp)
movq %rcx, %rsp
movq %rax, -0x108(%rbp)
movslq -0x34(%rbp), %rdi
shlq $0x2, %rdi
callq 0x1180
movq -0x198(%rbp), %rdx
movq -0x188(%rbp), %r9
movq %rax, -0x150(%rbp)
movq -0x20(%rbp), %rsi
movq -0xe8(%rbp), %r8
movl -0xc4(%rbp), %ecx
movl -0x34(%rbp), %edi
movsbl 0x28(%rbp), %r10d
subq $0x20, %rsp
movq %rsp, %rax
movl %r10d, 0x10(%rax)
movl %edi, 0x8(%rax)
movl %ecx, (%rax)
leaq -0x150(%rbp), %rdi
leaq -0xc0(%rbp), %rcx
callq 0x3d040
addq $0x20, %rsp
movq -0x8(%rbp), %rax
movss %xmm0, (%rax)
movl -0x34(%rbp), %eax
leaq 0xf(,%rax,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x190(%rbp)
movq %rcx, %rsp
movq %rax, -0x160(%rbp)
movl $0x0, -0x154(%rbp)
movl -0x154(%rbp), %eax
cmpl -0x34(%rbp), %eax
jge 0x3c54f
movq -0x190(%rbp), %rax
movslq -0x154(%rbp), %rcx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl -0x154(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x154(%rbp)
jmp 0x3c51d
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl 0x18(%rbp), %eax
jge 0x3c639
movq -0x48(%rbp), %rax
movslq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x168(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x170(%rbp)
movl $0x0, -0x154(%rbp)
movl -0x154(%rbp), %eax
cmpl -0x34(%rbp), %eax
jge 0x3c612
movq -0x190(%rbp), %rax
movq -0x168(%rbp), %rcx
movslq -0x154(%rbp), %rdx
movss (%rcx,%rdx,4), %xmm0
movq -0x150(%rbp), %rcx
movslq -0x154(%rbp), %rdx
mulss (%rcx,%rdx,4), %xmm0
cvtss2sd %xmm0, %xmm0
addsd -0x170(%rbp), %xmm0
movsd %xmm0, -0x170(%rbp)
movq -0x168(%rbp), %rcx
movslq -0x154(%rbp), %rdx
movss (%rcx,%rdx,4), %xmm0
cvtss2sd %xmm0, %xmm0
movslq -0x154(%rbp), %rcx
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl -0x154(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x154(%rbp)
jmp 0x3c58a
movsd -0x170(%rbp), %xmm0
cvtsd2ss %xmm0, %xmm0
movq -0x20(%rbp), %rax
movslq -0x38(%rbp), %rcx
movss %xmm0, (%rax,%rcx,4)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x3c556
movq -0x150(%rbp), %rax
movss (%rax), %xmm0
movss %xmm0, -0x174(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x180(%rbp)
movl $0x0, -0x154(%rbp)
movl -0x154(%rbp), %eax
cmpl -0x34(%rbp), %eax
jge 0x3c6bf
movq -0x190(%rbp), %rax
movq -0x150(%rbp), %rcx
movslq -0x154(%rbp), %rdx
movss (%rcx,%rdx,4), %xmm0
cvtss2sd %xmm0, %xmm0
movslq -0x154(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm2
movsd -0x180(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x180(%rbp)
movl -0x154(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x154(%rbp)
jmp 0x3c661
movq -0x190(%rbp), %rax
movq -0x150(%rbp), %rcx
movss (%rcx), %xmm0
cvtss2sd %xmm0, %xmm0
mulsd (%rax), %xmm0
divsd -0x180(%rbp), %xmm0
cvtsd2ss %xmm0, %xmm0
movq -0x18(%rbp), %rax
movss %xmm0, (%rax)
movq -0x150(%rbp), %rax
movss 0x4(%rax), %xmm0
cvtsi2ssl 0x18(%rbp), %xmm1
mulss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
divsd -0x180(%rbp), %xmm0
cvtsd2ss %xmm0, %xmm0
movq -0x18(%rbp), %rax
movss %xmm0, 0x4(%rax)
movq -0x18(%rbp), %rax
movss 0x39969(%rip), %xmm0    # 0x76090
subss (%rax), %xmm0
movq -0x18(%rbp), %rax
subss 0x4(%rax), %xmm0
movq -0x18(%rbp), %rax
movss %xmm0, 0x8(%rax)
movq -0x150(%rbp), %rdi
callq 0x1040
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl 0x18(%rbp), %eax
jge 0x3c774
movq -0x48(%rbp), %rax
movslq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x1040
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x3c750
movq -0x48(%rbp), %rdi
callq 0x1040
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0xc4(%rbp), %eax
jge 0x3c7ae
movq -0xe8(%rbp), %rax
movslq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x1040
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x3c784
movq -0xe8(%rbp), %rdi
callq 0x1040
movss -0x174(%rbp), %xmm0
movq -0xd0(%rbp), %rax
movq %rax, %rsp
movq %rbp, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)