// File: /workspace/github_c_makefile_star2_O1/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, -0x70(%rbp)
movl %ecx, -0x30(%rbp)
movq %rdx, -0x60(%rbp)
movq %rdi, -0x40(%rbp)
movl (%rsi), %r12d
movslq %r12d, %rax
movq %rax, -0x38(%rbp)
movq %rsi, -0x78(%rbp)
movslq 0x4(%rsi), %rbx
leaq (,%rbx,8), %rdi
callq 0x21d0
movq %rax, -0x50(%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, -0x48(%rbp)
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
movq %rax, -0x58(%rbp)
subq %rax, %r14
movq %r14, %rsp
testq %rbx, %rbx
jle 0x2692d
leaq 0x10(%rbp), %rax
movq 0x18(%rax), %r15
movq -0x38(%rbp), %rax
leaq (,%rax,4), %rbx
xorl %r13d, %r13d
movss (%r15,%r13,4), %xmm0
ucomiss 0x2a751(%rip), %xmm0    # 0x5100c
jb 0x268c3
sqrtss %xmm0, %xmm0
jmp 0x268c8
callq 0x21c0
movss %xmm0, -0x2c(%rbp)
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rax
movss (%rax,%r13,4), %xmm1
mulss %xmm0, %xmm1
movss %xmm1, (%r14,%r13,4)
movq %rbx, %rdi
callq 0x21d0
movss -0x2c(%rbp), %xmm1
movq -0x50(%rbp), %rcx
movq %rax, (%rcx,%r13,8)
testl %r12d, %r12d
jle 0x26924
movq -0x78(%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 0x26909
incq %r13
cmpq -0x48(%rbp), %r13
jne 0x268ae
leaq 0x324cc(%rip), %rsi     # 0x58e00
leaq -0x190(%rbp), %r13
movq %r13, %rdi
movq -0x60(%rbp), %rdx
xorl %eax, %eax
callq 0x2260
movq -0x38(%rbp), %r15
leaq (,%r15,4), %rdi
callq 0x21d0
leaq -0xc0(%rbp), %rbx
movq %rax, (%rbx)
movq -0x70(%rbp), %r12
movss (%r12), %xmm0
movss %xmm0, 0xc(%rbx)
movq %rsp, %rsi
subq -0x58(%rbp), %rsi
movq %rsi, %rsp
subq $0x8, %rsp
movsbl -0x30(%rbp), %eax
movq %rbx, %rdi
movq -0x68(%rbp), %rdx
movq %r13, %rcx
movq -0x50(%rbp), %r13
movq %r13, %r8
movq %r14, %r9
pushq %rax
pushq %r15
movq -0x48(%rbp), %r14
pushq %r14
callq 0x2aff0
addq $0x20, %rsp
movss %xmm0, -0x2c(%rbp)
movq %r13, %rdi
movl %r14d, %esi
callq 0x4960c
movss 0xc(%rbx), %xmm0
movss %xmm0, (%r12)
testl %r15d, %r15d
jle 0x269ee
movq -0xc0(%rbp), %rax
movq -0x40(%rbp), %rcx
movq 0x18(%rcx), %rcx
xorl %edx, %edx
movss (%rax,%rdx,4), %xmm0
movss %xmm0, (%rcx,%rdx,4)
incq %rdx
cmpq %rdx, %r15
jne 0x269dc
movq -0xc0(%rbp), %rdi
callq 0x2050
movss 0x2a602(%rip), %xmm0    # 0x51004
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