// File: /workspace/github_c_makefile_star2_O2/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 $0x168, %rsp            # imm = 0x168
movq %r8, -0x80(%rbp)
movl %ecx, -0x30(%rbp)
movq %rdx, -0x68(%rbp)
movq %rsi, %r15
movq %rdi, -0x50(%rbp)
movslq (%rsi), %r12
movl 0x4(%rsi), %ebx
movslq %ebx, %r13
leaq (,%r13,8), %rdi
callq 0x21d0
movq %rax, -0x48(%rbp)
movl %r12d, %eax
movq %rsp, %rcx
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, -0x60(%rbp)
movq %rcx, %rsp
movq %rsp, %rax
movq %rbx, -0x78(%rbp)
leaq 0xf(,%rbx,4), %rcx
andq $-0x10, %rcx
movq %rcx, -0x58(%rbp)
subq %rcx, %rax
movq %rax, -0x40(%rbp)
movq %rax, %rsp
movq 0x28(%rbp), %r14
movq %r12, %rcx
movq %r12, %rax
shlq $0x2, %rax
movq %rax, -0x38(%rbp)
xorl %ebx, %ebx
testl %ecx, %ecx
movl $0x0, %r12d
movq %rcx, -0x70(%rbp)
cmovgl %ecx, %r12d
testl %r13d, %r13d
cmovlel %ebx, %r13d
cmpq %r13, %rbx
je 0x23daf
movss (%r14,%rbx,4), %xmm0
ucomiss 0x272c6(%rip), %xmm0    # 0x4b00c
jb 0x23d4e
sqrtss %xmm0, %xmm0
jmp 0x23d53
callq 0x21c0
movss %xmm0, -0x2c(%rbp)
movq -0x50(%rbp), %rax
movq 0x8(%rax), %rax
movss (%rax,%rbx,4), %xmm1
mulss %xmm0, %xmm1
movq -0x40(%rbp), %rax
movss %xmm1, (%rax,%rbx,4)
movq -0x38(%rbp), %rdi
callq 0x21d0
movss -0x2c(%rbp), %xmm1
movq -0x48(%rbp), %rcx
movq %rax, (%rcx,%rbx,8)
xorl %ecx, %ecx
cmpq %rcx, %r12
je 0x23daa
movq 0x10(%r15), %rdx
movq (%rdx,%rcx,8), %rdx
movss (%rdx,%rbx,4), %xmm0
mulss %xmm1, %xmm0
movss %xmm0, (%rax,%rcx,4)
incq %rcx
jmp 0x23d8a
incq %rbx
jmp 0x23d34
leaq 0x2ef5e(%rip), %rsi     # 0x52d14
xorl %ebx, %ebx
leaq -0x190(%rbp), %r15
movq %r15, %rdi
movq -0x68(%rbp), %rdx
xorl %eax, %eax
callq 0x2260
movq -0x38(%rbp), %rdi
callq 0x21d0
leaq -0xc8(%rbp), %rcx
movq %rax, (%rcx)
movq -0x80(%rbp), %r14
movss (%r14), %xmm0
movss %xmm0, 0xc(%rcx)
movq %rcx, %rdi
movq %rsp, %rsi
subq -0x58(%rbp), %rsi
movq %rsi, %rsp
subq $0x8, %rsp
movsbl -0x30(%rbp), %eax
movq -0x60(%rbp), %rdx
movq %r15, %rcx
movq -0x48(%rbp), %r15
movq %r15, %r8
movq -0x40(%rbp), %r9
pushq %rax
pushq -0x70(%rbp)
movq -0x78(%rbp), %r13
pushq %r13
callq 0x27fe8
addq $0x20, %rsp
movss %xmm0, -0x2c(%rbp)
movq %r15, %rdi
movl %r13d, %esi
callq 0x436f0
leaq -0xc8(%rbp), %rax
movss 0xc(%rax), %xmm0
movss %xmm0, (%r14)
movq (%rax), %rdi
cmpq %rbx, %r12
je 0x23e68
movss (%rdi,%rbx,4), %xmm0
movq -0x50(%rbp), %rax
movq 0x18(%rax), %rax
movss %xmm0, (%rax,%rbx,4)
incq %rbx
jmp 0x23e4c
callq 0x2050
movss 0x2718f(%rip), %xmm0    # 0x4b004
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