// File: /workspace/github_c_makefile_star2_O2/ugobas[P]tnm/nma.c
// Function: Compare_anisou
int Compare_anisou(float ***aniso_pred, float ***aniso_exp, int nca,
		   char *model, char *inter, char *file_name, char *prot_name,
		   int N, int *weight, float *dot_aniso, float *ov_aniso,
		   float *delta_aniso)
{
  //FILE *file_out=fopen(file_name, "a");
  int ica, i, j, n_sum=0;
  float sum_overlap=0, sum_dot=0, sum_delta=0;
  float delta, overlap, dot, dd;
  float **e_vec1=Allocate_mat2_f(3,3), e_val1[3];
  float **e_vec2=Allocate_mat2_f(3,3), e_val2[3];
  float **e_vec12=Allocate_mat2_f(3,3), e_val12[3];
  float **u_12=Allocate_mat2_f(3,3);

  for(ica=0; ica<nca; ica++){
    if(weight[ica]){
      float **u1=aniso_exp[ica],  norm1=0;
      float **u2=aniso_pred[ica], norm2=0;
      float prod_lambda=1.0, prod_lambda_12=1.0;
      float isotropy;

      // Exclude quasi-spherical distributions, isotropy >= 0.5
      f_Diagonalize(3, u1, e_val1, e_vec1, 1);
      isotropy=e_val1[2]/e_val1[0]; //printf("%.2f\n", anisotropy);
      if(isotropy >= 0.5)continue;
      n_sum++;

      /****************  Overlap *********************/
      // Normalize by the trace
      for(i=0; i<3; i++)norm1+=u1[i][i];
      for(i=0; i<3; i++)norm2+=u2[i][i];
      for(i=0; i<3; i++)for(j=i; j<3; j++){
	  u1[i][j]/=norm1; u2[i][j]/=norm2;
	  u_12[i][j]=u1[i][j]+u2[i][j];
	}
      for(i=0; i<3; i++)for(j=0; j<i; j++){
	  u1[i][j]=u1[j][i]; u2[i][j]=u2[j][i]; u_12[i][j]=u_12[j][i];
	}

      for(i=0; i<3; i++)e_val1[i]/=norm1;
      f_Diagonalize(3, u2, e_val2, e_vec2, 1);
      f_Diagonalize(3, u_12, e_val12, e_vec12, 1);
      for(i=0; i<3; i++){
	prod_lambda*=(e_val1[i]*e_val2[i]);
	prod_lambda_12*=e_val12[i];
      }
      overlap=sqrt(8.*sqrt(prod_lambda)/prod_lambda_12);
      sum_overlap += overlap;
      // printf("overlap: %.3f\n", overlap);

      /*****   dot product   **************/
      /*
	dot=0; dot_norm=0;
	for(i=0; i<3; i++){
	float w=e_val1[i]*e_val2[i];
	dot+=w*Scalar_product_3(e_vec1[i], e_vec2[i]);
	dot_norm+=w;
	}
	dot /= dot_norm;
      */
      dot=Scalar_product_3(e_vec1[0], e_vec2[0]);
      if(dot<0){dot=-dot;} sum_dot += dot;

      /***********   delta   **************/
      delta=0;           // delta = sqrt(sum_ij(U1_ij-U2_ij)^2/9)
      for(i=0; i<3; i++){
	dd=u1[i][i]-u2[i][i]; delta+=dd*dd;
	for(j=0; j<i; j++){dd=u1[i][j]-u2[i][j]; delta+=2*dd*dd;}
      }
      sum_delta+=delta;

      /*
      //fprintf(file_out, "%6.3f %6.3f\n", overlap, dot);
      for(i=0; i<3; i++)for(j=0; j<=i; j++){
      fprintf(file_out, "%6.3f %6.3f ", u1[i][j], u2[i][j]);
      }
      fprintf(file_out, "\n");
      */
    }
  }
  *ov_aniso=sum_overlap/n_sum;
  *dot_aniso=sum_dot/n_sum;
  *delta_aniso=sqrt(sum_delta/(9*n_sum));

  /*
    fprintf(file_out, "%6.3f  %s %8s %6.3f %2.0f %8s %3d\n",
	  sum_overlap/n_sum, model, inter, sum_dot/n_sum, n_sum, prot_name, N);
  fclose(file_out);
  */

  // Clean
  Empty_matrix_f(e_vec1, 3);
  Empty_matrix_f(e_vec2, 3);
  Empty_matrix_f(e_vec12, 3);
  Empty_matrix_f(u_12, 3);

  return(n_sum);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movl %edx, %r13d
movq %rsi, 0x58(%rsp)
movq %rdi, 0x50(%rsp)
pushq $0x3
popq %rbp
movl %ebp, %edi
movl %ebp, %esi
callq 0x4379e
movq %rax, 0x28(%rsp)
movl %ebp, %edi
movl %ebp, %esi
callq 0x4379e
movq %rax, 0x20(%rsp)
movl %ebp, %edi
movl %ebp, %esi
callq 0x4379e
movq %rax, 0x40(%rsp)
movl %ebp, %edi
movl %ebp, %esi
callq 0x4379e
movq %rax, %r12
xorl %r15d, %r15d
testl %r13d, %r13d
cmovlel %r15d, %r13d
xorps %xmm0, %xmm0
xorps %xmm1, %xmm1
movss %xmm1, 0x10(%rsp)
movss %xmm1, 0xc(%rsp)
movq $0x0, 0x18(%rsp)
pushq $0x1
popq %rbx
cmpq %r13, %r15
je 0x25410
movq 0xc0(%rsp), %rax
cmpl $0x0, (%rax,%r15,4)
je 0x25408
movss %xmm0, 0x14(%rsp)
movq 0x58(%rsp), %rax
movq (%rax,%r15,8), %r14
movq 0x50(%rsp), %rax
movq (%rax,%r15,8), %rbp
pushq $0x3
popq %rdi
movq %r14, %rsi
leaq 0x34(%rsp), %rdx
movq 0x28(%rsp), %rcx
pushq $0x1
popq %r8
callq 0x44178
movss 0x3c(%rsp), %xmm0
divss 0x34(%rsp), %xmm0
ucomiss 0x2a97c(%rip), %xmm0    # 0x4fb14
jae 0x251b3
xorps %xmm0, %xmm0
xorl %eax, %eax
cmpq $0x3, %rax
je 0x251be
movq (%r14,%rax,8), %rcx
addss (%rcx,%rax,4), %xmm0
incq %rax
jmp 0x2519f
movss 0x14(%rsp), %xmm0
jmp 0x25408
xorps %xmm1, %xmm1
xorl %eax, %eax
cmpq $0x3, %rax
je 0x251d8
movq (%rbp,%rax,8), %rcx
addss (%rcx,%rax,4), %xmm1
incq %rax
jmp 0x251c3
xorl %eax, %eax
cmpq $0x3, %rax
je 0x25226
movq %rax, %rcx
cmpq $0x3, %rcx
je 0x25221
movq (%r14,%rax,8), %rdx
movss (%rdx,%rcx,4), %xmm2
divss %xmm0, %xmm2
movss %xmm2, (%rdx,%rcx,4)
movq (%rbp,%rax,8), %rsi
movss (%rsi,%rcx,4), %xmm2
divss %xmm1, %xmm2
movss %xmm2, (%rsi,%rcx,4)
addss (%rdx,%rcx,4), %xmm2
movq (%r12,%rax,8), %rdx
movss %xmm2, (%rdx,%rcx,4)
incq %rcx
jmp 0x251e3
incq %rax
jmp 0x251da
xorl %eax, %eax
cmpq $0x3, %rax
je 0x25277
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0x25272
movq (%r14,%rcx,8), %rdx
movss (%rdx,%rax,4), %xmm1
movq (%r14,%rax,8), %rdx
movss %xmm1, (%rdx,%rcx,4)
movq (%rbp,%rcx,8), %rdx
movss (%rdx,%rax,4), %xmm1
movq (%rbp,%rax,8), %rdx
movss %xmm1, (%rdx,%rcx,4)
movq (%r12,%rcx,8), %rdx
movss (%rdx,%rax,4), %xmm1
movq (%r12,%rax,8), %rdx
movss %xmm1, (%rdx,%rcx,4)
incq %rcx
jmp 0x25230
incq %rax
jmp 0x25228
xorl %eax, %eax
cmpq $0x3, %rax
je 0x25294
movss 0x34(%rsp,%rax,4), %xmm1
divss %xmm0, %xmm1
movss %xmm1, 0x34(%rsp,%rax,4)
incq %rax
jmp 0x25279
movq %r15, 0x48(%rsp)
movq %r13, %r15
pushq $0x3
popq %r13
movl %r13d, %edi
movq %rbp, %rsi
leaq 0x6c(%rsp), %rdx
movq 0x20(%rsp), %rcx
movl %ebx, %r8d
callq 0x44178
movl %r13d, %edi
movq %r15, %r13
movq 0x48(%rsp), %r15
movq %r12, %rsi
leaq 0x60(%rsp), %rdx
movq 0x40(%rsp), %rcx
movl %ebx, %r8d
callq 0x44178
xorl %eax, %eax
movss 0x25d2e(%rip), %xmm1    # 0x4b010
movaps %xmm1, %xmm0
movaps %xmm1, %xmm2
cmpq $0x3, %rax
je 0x25309
movss 0x34(%rsp,%rax,4), %xmm1
mulss 0x6c(%rsp,%rax,4), %xmm1
mulss %xmm1, %xmm0
mulss 0x60(%rsp,%rax,4), %xmm2
incq %rax
jmp 0x252e8
cvtss2sd %xmm0, %xmm0
ucomisd 0x25d3b(%rip), %xmm0    # 0x4b050
jb 0x2531d
sqrtsd %xmm0, %xmm0
jmp 0x2532e
movss %xmm2, 0x8(%rsp)
callq 0x2290
movss 0x8(%rsp), %xmm2
mulsd 0x2bfaa(%rip), %xmm0    # 0x512e0
cvtss2sd %xmm2, %xmm1
divsd %xmm1, %xmm0
ucomisd 0x25d0a(%rip), %xmm0    # 0x4b050
jb 0x2534e
sqrtsd %xmm0, %xmm0
jmp 0x25353
callq 0x2290
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x8(%rsp)
movq 0x28(%rsp), %rax
movq (%rax), %rdi
movq 0x20(%rsp), %rax
movq (%rax), %rsi
callq 0xf94a
movaps %xmm0, %xmm1
xorps 0x2b4e4(%rip), %xmm1    # 0x50860
maxss %xmm0, %xmm1
xorps %xmm4, %xmm4
xorl %eax, %eax
cmpq $0x3, %rax
je 0x253d0
movq (%r14,%rax,8), %rcx
movss (%rcx,%rax,4), %xmm2
movq (%rbp,%rax,8), %rdx
subss (%rdx,%rax,4), %xmm2
mulss %xmm2, %xmm2
addss %xmm2, %xmm4
xorl %esi, %esi
cmpq %rsi, %rax
je 0x253cb
movss (%rcx,%rsi,4), %xmm2
subss (%rdx,%rsi,4), %xmm2
movaps %xmm2, %xmm3
addss %xmm2, %xmm3
mulss %xmm2, %xmm3
addss %xmm3, %xmm4
incq %rsi
jmp 0x253a8
incq %rax
jmp 0x25385
movq 0x18(%rsp), %rax
incl %eax
movq %rax, 0x18(%rsp)
movss 0xc(%rsp), %xmm0
addss 0x8(%rsp), %xmm0
movss %xmm0, 0xc(%rsp)
movss 0x10(%rsp), %xmm0
addss %xmm1, %xmm0
movss %xmm0, 0x10(%rsp)
movss 0x14(%rsp), %xmm0
addss %xmm4, %xmm0
incq %r15
jmp 0x25138
movq 0x18(%rsp), %r14
cvtsi2ss %r14d, %xmm1
movss 0xc(%rsp), %xmm2
divss %xmm1, %xmm2
movq 0xd0(%rsp), %rax
movss %xmm2, (%rax)
movss 0x10(%rsp), %xmm2
divss %xmm1, %xmm2
movq 0xc8(%rsp), %rax
movss %xmm2, (%rax)
leal (%r14,%r14,8), %eax
cvtsi2ss %eax, %xmm1
divss %xmm1, %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jb 0x25460
sqrtss %xmm0, %xmm0
jmp 0x25465
callq 0x21c0
movq 0xd8(%rsp), %rax
movss %xmm0, (%rax)
pushq $0x3
popq %rbp
movq 0x28(%rsp), %rdi
movl %ebp, %esi
callq 0x436f0
movq 0x20(%rsp), %rdi
movl %ebp, %esi
callq 0x436f0
movq 0x40(%rsp), %rdi
movl %ebp, %esi
callq 0x436f0
movq %r12, %rdi
movl %ebp, %esi
callq 0x436f0
movl %r14d, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq