// File: /workspace/github_c_makefile_star2_O3/ugobas[P]tnm/nma.c
// Function: Convert_cart2torsion_fit
int Convert_cart2torsion_fit(struct Tors *D, struct Reference Ref,
			     struct Jacobian *J, char *nameout,
			     char type, float *Lambda)
{
  /* type of fit: C= specific heat M= Max penalty O= Ordinary
     L: known value of Lambda */

  /* Minimize |sum_a J_ia Delta_phi_a - sqrt(m_i) Delta_r_i|^2 where:
     J is the jacobian matrix dr_int_i/d_phi_a
     M_i is the diagonal mass matrix 
     The fit is performed through the ridge regression approach
     Lambda=0: ==> L^t Delta_phi = L^{-1} M Delta_r = Jtilde M Delta_r
     L=T_sqrt (lower diagonal matrix if obtained from Cholevsky decomposition)
     T=JMJ^t = LL^t
  */
  
  int Npar=J->N_axes, Nsam=J->N_Cart, a;
  float **X=malloc(Nsam*sizeof(float *));
  float D_out[Npar], Y[Nsam];
  for(int i=0; i<Nsam; i++){
    float sm=sqrt(Ref.mass_coord[i]);
    Y[i]=sm*D->Cart[i];
    X[i]=malloc(Npar*sizeof(float));
    float *Xi=X[i];
    for(a=0; a<Npar; a++){*Xi=sm*J->Jacobian_ar[a][i]; Xi++;}
  }

  //printf("Fitting %d Cartesian dofs with %d torsion angles", Nsam, Npar);
  //printf(" using ridge regression of type %c\n", type);
  char out[200]; sprintf(out, "%s.Ridge_regression", nameout);
  struct ridge_fit fit; fit.A=malloc(Npar*sizeof(float));
  fit.Lambda=*Lambda;
  float y_pred[Nsam];
  float r=Ridge_regression(&fit, y_pred, D_out, out, X, Y, Nsam, Npar, type);
  Empty_matrix_f(X, Nsam);
  *Lambda=fit.Lambda;
  for(a=0; a<Npar; a++)D->Tors[a]=fit.A[a];
  free(fit.A);

  if(r<-1)return(-1);
  // Compute Masswtd_Tors_dev = T_sqrt Delta_theta
  //Compute_MW(D, J); // WARNING: The J matrix has not the correct T,T_sqrt
  return(0);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x158, %rsp            # imm = 0x158
movq %r8, %r15
movl %ecx, %r13d
movq %rdx, -0x60(%rbp)
movq %rdi, -0x38(%rbp)
movl (%rsi), %r12d
movq %rsi, -0x70(%rbp)
movslq 0x4(%rsi), %rbx
leaq (,%rbx,8), %rdi
callq 0x21e0
movq %rax, -0x48(%rbp)
movq %rsp, %rcx
leaq 0xf(,%r12,4), %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, -0x68(%rbp)
movq %rcx, %rsp
movl %ebx, %eax
movq %rsp, %r14
movq %rax, -0x40(%rbp)
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
movq %rax, -0x58(%rbp)
subq %rax, %r14
movq %r14, %rsp
testq %rbx, %rbx
jle 0x264aa
movl %r13d, -0x30(%rbp)
movq %r15, -0x50(%rbp)
leaq 0x10(%rbp), %rax
movslq %r12d, %rbx
movq 0x18(%rax), %r15
shlq $0x2, %rbx
xorl %r13d, %r13d
movss (%r15,%r13,4), %xmm0
ucomiss 0x2acea(%rip), %xmm0    # 0x51118
jb 0x26436
sqrtss %xmm0, %xmm0
jmp 0x2643b
callq 0x21d0
movss %xmm0, -0x2c(%rbp)
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movss (%rax,%r13,4), %xmm1
mulss %xmm0, %xmm1
movss %xmm1, (%r14,%r13,4)
movq %rbx, %rdi
callq 0x21e0
movss -0x2c(%rbp), %xmm1
movq -0x48(%rbp), %rcx
movq %rax, (%rcx,%r13,8)
testl %r12d, %r12d
jle 0x26497
movq -0x70(%rbp), %rcx
movq 0x10(%rcx), %rcx
xorl %edx, %edx
movq (%rcx,%rdx,8), %rsi
movss (%rsi,%r13,4), %xmm0
mulss %xmm1, %xmm0
movss %xmm0, (%rax,%rdx,4)
incq %rdx
cmpq %rdx, %r12
jne 0x2647c
incq %r13
cmpq -0x40(%rbp), %r13
jne 0x26421
movq -0x50(%rbp), %r15
movl -0x30(%rbp), %r13d
jmp 0x264b1
movslq %r12d, %rbx
shlq $0x2, %rbx
leaq 0x328fc(%rip), %rsi     # 0x58db4
leaq -0x180(%rbp), %rdi
movq -0x60(%rbp), %rdx
xorl %eax, %eax
callq 0x2270
movq %rbx, %rdi
callq 0x21e0
leaq -0xb8(%rbp), %rbx
movq %rax, (%rbx)
movss (%r15), %xmm0
movss %xmm0, 0xc(%rbx)
movq %rsp, %rsi
subq -0x58(%rbp), %rsi
movq %rsi, %rsp
subq $0x8, %rsp
movsbl %r13b, %eax
movq %rbx, %rdi
movq -0x68(%rbp), %rdx
leaq -0x180(%rbp), %rcx
movq -0x48(%rbp), %r13
movq %r13, %r8
movq %r14, %r9
pushq %rax
pushq %r12
movq -0x40(%rbp), %r14
pushq %r14
callq 0x2ac4c
addq $0x20, %rsp
movss %xmm0, -0x2c(%rbp)
movq %r13, %rdi
movl %r14d, %esi
callq 0x49678
movss 0xc(%rbx), %xmm0
movss %xmm0, (%r15)
movq (%rbx), %rdi
testl %r12d, %r12d
jle 0x26560
movq -0x38(%rbp), %rax
movq 0x18(%rax), %rax
xorl %ecx, %ecx
movss (%rdi,%rcx,4), %xmm0
movss %xmm0, (%rax,%rcx,4)
incq %rcx
cmpq %rcx, %r12
jne 0x2654e
callq 0x2050
movss 0x2aba3(%rip), %xmm0    # 0x51110
xorl %eax, %eax
ucomiss -0x2c(%rbp), %xmm0
seta %al
negl %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq