// File: /workspace/github_c_makefile_star2_O3/ugobas[P]tnm/confchange.c
// Function: Test_Renyi
float Test_Renyi(float *wminus2, float *P, int N, char *name, FILE *file1)
{
  /* Tests the Renyi probabilities P^(q-1)\propto -ene -mu
     for various values of the Reniy parameter q
     Returns the value of q that yields best correlation */
  int Newfile=0, i;
  float *y=malloc(N*sizeof(float));
  float q, q1=0, r, slope, offset, qmax=0, rmax=0;
  FILE *file2; char filename[80];
  if(Newfile){
    sprintf(filename, "Renyi_%s.dat", name);
    file2=fopen(filename, "w");
    fprintf(file2, "# Correlation between w_a^-2 and P_q(a)=c_a^2(q-1)\n");
    fprintf(file2, "# q Corr\n");
  }else{
    fprintf(file1, "# P_q(a)=c_a^2(q-1), d_KL r[P_q,1/w^2]\n");
  }

  float wmin=1000, mu;
  for(i=0; i<N; i++)if(wminus2[i]<wmin)wmin=wminus2[i];
  wmin-=0.0001;
  for(q=1; q<=3.5; q+=0.25){
    double d_KL=0, q2=0, Z=0, lnp;
    if(q==1){for(i=0; i<N; i++)y[i]=log(P[i]);}  // q=1: Shannon
    else{for(i=0; i<N; i++)y[i]=pow(P[i], q1);}
    r= Corr_coeff(wminus2, y, N, &slope, &offset);
    if(q==1){
      for(i=0; i<N; i++){
	d_KL+=P[i]*(y[i]-slope*wminus2[i]);
	Z+=exp(slope*wminus2[i]);
      }
    }else{
      q1=q-1; q2=1/q1; 
      if(offset<0){mu=-wmin;}else{mu=offset/slope;}
      for(i=0; i<N; i++){
	lnp=q2*log(wminus2[i]+mu); Z+=exp(lnp);
	d_KL+=P[i]*(log(P[i])-lnp);
      }
    }
    d_KL+=log(Z);
    if(r > rmax){rmax=r; qmax=q;}
    if(Newfile){fprintf(file2, "%.3f  %.3f  %.3f\n", q, d_KL, r);}
    else{fprintf(file1, "q=%.2f                %.3f  %.3f\n",q,d_KL,r);}
  }
  fprintf(file1, "Renyi parameter qopt  %.3f\n", qmax);
  fprintf(file1, "r[P_qopt,1/w^2]       %.3f\n#\n", rmax);
  if(Newfile){
    fclose(file2);
    printf("Writing %s\n", filename);
  }
  free(y);
  return(qmax);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %r8, %r13
movl %edx, %ebp
movq %rsi, %r15
movq %rdi, %r12
movslq %edx, %rbx
leaq (,%rbx,4), %rdi
callq 0x21e0
movq %rax, %r14
leaq 0x2b166(%rip), %rdi     # 0x5a8a0
movl $0x27, %esi
movl $0x1, %edx
movq %r13, 0x28(%rsp)
movq %r13, %rcx
callq 0x2290
movl %ebp, %r13d
testl %ebx, %ebx
jle 0x2f789
movss 0x27b40(%rip), %xmm0    # 0x572a0
xorl %eax, %eax
movaps %xmm0, %xmm1
movss (%r12,%rax,4), %xmm0
minss %xmm1, %xmm0
incq %rax
cmpq %rax, %r13
jne 0x2f762
cvtss2sd %xmm0, %xmm0
addsd 0x2a52d(%rip), %xmm0    # 0x59cb0
cvtsd2ss %xmm0, %xmm0
jmp 0x2f791
movss 0x2a543(%rip), %xmm0    # 0x59cd4
xorps 0x21898(%rip), %xmm0    # 0x51030
movaps %xmm0, 0x80(%rsp)
movss 0x21974(%rip), %xmm1    # 0x5111c
movsd 0x21998(%rip), %xmm2    # 0x51148
xorps %xmm0, %xmm0
movss %xmm0, 0x8(%rsp)
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x70(%rsp)
movaps %xmm1, 0x40(%rsp)
movss 0x8(%rsp), %xmm0
movaps %xmm0, 0x90(%rsp)
ucomiss 0x2193f(%rip), %xmm1    # 0x5111c
movsd %xmm2, 0x50(%rsp)
jne 0x2f813
jp 0x2f813
testl %ebp, %ebp
jle 0x2f8f3
xorl %ebx, %ebx
xorps %xmm0, %xmm0
cvtss2sd (%r15,%rbx,4), %xmm0
callq 0x2120
cvtsd2ss %xmm0, %xmm0
movss %xmm0, (%r14,%rbx,4)
incq %rbx
cmpq %rbx, %r13
jne 0x2f7f1
jmp 0x2f852
testl %ebp, %ebp
jle 0x2f915
xorps %xmm0, %xmm0
cvtss2sd 0x70(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
xorl %ebx, %ebx
xorps %xmm0, %xmm0
cvtss2sd (%r15,%rbx,4), %xmm0
movsd 0x8(%rsp), %xmm1
callq 0x20a0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, (%r14,%rbx,4)
incq %rbx
cmpq %rbx, %r13
jne 0x2f82c
movq %r12, %rdi
movq %r14, %rsi
movl %ebp, %edx
leaq 0x14(%rsp), %rcx
leaq 0x1c(%rsp), %r8
callq 0x108db
movss %xmm0, 0x18(%rsp)
movaps 0x40(%rsp), %xmm0
ucomiss 0x218a1(%rip), %xmm0    # 0x5111c
jne 0x2f937
jp 0x2f937
testl %ebp, %ebp
jle 0x2fa20
xorps %xmm1, %xmm1
xorl %ebx, %ebx
xorps %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
movsd %xmm1, 0x20(%rsp)
movss 0x14(%rsp), %xmm0
mulss (%r12,%rbx,4), %xmm0
movss (%r14,%rbx,4), %xmm1
subss %xmm0, %xmm1
mulss (%r15,%rbx,4), %xmm1
cvtss2sd %xmm1, %xmm1
movsd 0x8(%rsp), %xmm2
addsd %xmm1, %xmm2
movsd %xmm2, 0x8(%rsp)
cvtss2sd %xmm0, %xmm0
callq 0x22b0
movsd 0x20(%rsp), %xmm1
addsd %xmm0, %xmm1
incq %rbx
cmpq %rbx, %r13
jne 0x2f89d
jmp 0x2fa2c
movq %r12, %rdi
movq %r14, %rsi
movl %ebp, %edx
leaq 0x14(%rsp), %rcx
leaq 0x1c(%rsp), %r8
callq 0x108db
movss %xmm0, 0x18(%rsp)
jmp 0x2fa20
movq %r12, %rdi
movq %r14, %rsi
movl %ebp, %edx
leaq 0x14(%rsp), %rcx
leaq 0x1c(%rsp), %r8
callq 0x108db
movss %xmm0, 0x18(%rsp)
movaps 0x40(%rsp), %xmm0
movaps %xmm0, %xmm1
addss 0x217ce(%rip), %xmm1    # 0x51110
testl %ebp, %ebp
movaps %xmm1, 0x70(%rsp)
jle 0x2fa20
movss 0x217c5(%rip), %xmm0    # 0x5111c
divss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x58(%rsp)
movss 0x1c(%rsp), %xmm2
movaps %xmm2, %xmm0
divss 0x14(%rsp), %xmm0
cmpltss 0x2179b(%rip), %xmm2    # 0x51118
movaps %xmm2, %xmm1
andps 0x80(%rsp), %xmm1
andnps %xmm0, %xmm2
orps %xmm1, %xmm2
movaps %xmm2, 0xa0(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x20(%rsp)
xorl %ebx, %ebx
movsd %xmm0, 0x8(%rsp)
movss (%r12,%rbx,4), %xmm0
addss 0xa0(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
callq 0x2120
mulsd 0x58(%rsp), %xmm0
movsd %xmm0, 0x68(%rsp)
callq 0x22b0
movsd 0x20(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x20(%rsp)
xorps %xmm0, %xmm0
cvtss2sd (%r15,%rbx,4), %xmm0
movsd %xmm0, 0x60(%rsp)
callq 0x2120
subsd 0x68(%rsp), %xmm0
mulsd 0x60(%rsp), %xmm0
movsd 0x8(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rsp)
incq %rbx
cmpq %rbx, %r13
jne 0x2f9a7
movsd 0x20(%rsp), %xmm1
jmp 0x2fa2c
xorps %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
xorps %xmm1, %xmm1
movaps %xmm1, %xmm0
callq 0x2120
movsd 0x8(%rsp), %xmm1
addsd %xmm0, %xmm1
movss 0x18(%rsp), %xmm2
movaps %xmm2, %xmm0
movaps 0x90(%rsp), %xmm3
maxss %xmm3, %xmm0
movss %xmm0, 0x8(%rsp)
cmpltss %xmm2, %xmm3
movaps %xmm3, %xmm0
andps 0x40(%rsp), %xmm0
andnps 0x30(%rsp), %xmm3
orps %xmm0, %xmm3
movaps %xmm3, 0x30(%rsp)
cvtss2sd %xmm2, %xmm2
movq 0x28(%rsp), %rdi
leaq 0x2ae45(%rip), %rsi     # 0x5a8c8
movsd 0x50(%rsp), %xmm0
movb $0x3, %al
callq 0x21a0
movaps 0x40(%rsp), %xmm1
addss 0x27807(%rip), %xmm1    # 0x572a4
xorps %xmm2, %xmm2
cvtss2sd %xmm1, %xmm2
movss 0x2a22c(%rip), %xmm0    # 0x59cd8
ucomiss %xmm1, %xmm0
jae 0x2f7c3
xorps %xmm0, %xmm0
cvtss2sd 0x30(%rsp), %xmm0
leaq 0x2ae25(%rip), %rsi     # 0x5a8ea
movq 0x28(%rsp), %rbx
movq %rbx, %rdi
movb $0x1, %al
callq 0x21a0
xorps %xmm0, %xmm0
cvtss2sd 0x8(%rsp), %xmm0
leaq 0x2ae22(%rip), %rsi     # 0x5a906
movq %rbx, %rdi
movb $0x1, %al
callq 0x21a0
movq %r14, %rdi
callq 0x2050
movaps 0x30(%rsp), %xmm0
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq