// File: /workspace/github_c_makefile_star2_O0/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
subq $0x1b0, %rsp            # imm = 0x1B0
movb %cl, %al
leaq 0x10(%rbp), %rcx
movq %rcx, -0x1b0(%rbp)
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movb %al, -0x21(%rbp)
movq %r8, -0x30(%rbp)
movq -0x18(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x34(%rbp)
movq -0x18(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x38(%rbp)
movslq -0x38(%rbp), %rdi
shlq $0x3, %rdi
callq 0x1180
movq %rax, -0x48(%rbp)
movl -0x34(%rbp), %eax
movq %rsp, %rcx
movq %rcx, -0x50(%rbp)
leaq 0xf(,%rax,4), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x1a8(%rbp)
movq %rcx, %rsp
movq %rax, -0x58(%rbp)
movl -0x38(%rbp), %eax
leaq 0xf(,%rax,4), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x1a0(%rbp)
movq %rcx, %rsp
movq %rax, -0x60(%rbp)
movl $0x0, -0x64(%rbp)
movl -0x64(%rbp), %eax
cmpl -0x38(%rbp), %eax
jge 0x363f3
movq -0x1b0(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x64(%rbp), %rcx
movss (%rax,%rcx,4), %xmm0
cvtss2sd %xmm0, %xmm0
callq 0x1220
movq -0x1a0(%rbp), %rax
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x68(%rbp)
movss -0x68(%rbp), %xmm0
movq -0x10(%rbp), %rcx
movq 0x8(%rcx), %rcx
movslq -0x64(%rbp), %rdx
mulss (%rcx,%rdx,4), %xmm0
movslq -0x64(%rbp), %rcx
movss %xmm0, (%rax,%rcx,4)
movslq -0x34(%rbp), %rdi
shlq $0x2, %rdi
callq 0x1180
movq %rax, %rdx
movq -0x48(%rbp), %rax
movslq -0x64(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x48(%rbp), %rax
movslq -0x64(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x70(%rbp)
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x34(%rbp), %eax
jge 0x363e3
movss -0x68(%rbp), %xmm0
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x3c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x64(%rbp), %rcx
mulss (%rax,%rcx,4), %xmm0
movq -0x70(%rbp), %rax
movss %xmm0, (%rax)
movq -0x70(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x70(%rbp)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x3639e
jmp 0x363e5
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x36313
movq -0x20(%rbp), %rdx
leaq 0x47b9a(%rip), %rsi     # 0x7df98
xorl %eax, %eax
leaq -0x140(%rbp), %rdi
callq 0x1200
movslq -0x34(%rbp), %rdi
shlq $0x2, %rdi
callq 0x1180
movq -0x1a8(%rbp), %rdx
movq -0x1a0(%rbp), %r9
movq %rax, -0x188(%rbp)
movq -0x30(%rbp), %rax
movss (%rax), %xmm0
movss %xmm0, -0x17c(%rbp)
movl -0x38(%rbp), %eax
leaq 0xf(,%rax,4), %rcx
andq $-0x10, %rcx
movq %rsp, %rsi
subq %rcx, %rsi
movq %rsi, %rsp
movq %rax, -0x190(%rbp)
leaq -0x140(%rbp), %rcx
movq -0x48(%rbp), %r8
movl -0x38(%rbp), %r10d
movl -0x34(%rbp), %eax
leaq -0x188(%rbp), %rdi
subq $0x20, %rsp
movl %r10d, (%rsp)
movl %eax, 0x8(%rsp)
movsbl -0x21(%rbp), %eax
movl %eax, 0x10(%rsp)
callq 0x3d040
addq $0x20, %rsp
movss %xmm0, -0x194(%rbp)
movq -0x48(%rbp), %rdi
movl -0x38(%rbp), %esi
callq 0x69240
movss -0x17c(%rbp), %xmm0
movq -0x30(%rbp), %rax
movss %xmm0, (%rax)
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x34(%rbp), %eax
jge 0x364f2
movq -0x188(%rbp), %rax
movslq -0x3c(%rbp), %rcx
movss (%rax,%rcx,4), %xmm0
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x3c(%rbp), %rcx
movss %xmm0, (%rax,%rcx,4)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x364be
movq -0x188(%rbp), %rdi
callq 0x1040
movss 0x3fb6a(%rip), %xmm0    # 0x76070
ucomiss -0x194(%rbp), %xmm0
jbe 0x36522
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x198(%rbp)
jmp 0x36533
movl $0x0, -0x4(%rbp)
movl $0x1, -0x198(%rbp)
movq -0x50(%rbp), %rax
movq %rax, %rsp
movl -0x4(%rbp), %eax
movq %rbp, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)