// File: /workspace/github_c_makefile_star2_O1/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 0x21d0
movq %rax, %r14
leaq 0x2aef6(%rip), %rdi     # 0x5a8fc
movl $0x27, %esi
movl $0x1, %edx
movq %r13, 0x28(%rsp)
movq %r13, %rcx
callq 0x2280
movl %ebp, %r13d
testl %ebx, %ebx
jle 0x2fa55
movss 0x278b4(%rip), %xmm0    # 0x572e0
xorl %eax, %eax
movaps %xmm0, %xmm1
movss (%r12,%rax,4), %xmm0
minss %xmm1, %xmm0
incq %rax
cmpq %rax, %r13
jne 0x2fa2e
cvtss2sd %xmm0, %xmm0
addsd 0x2a2b9(%rip), %xmm0    # 0x59d08
cvtsd2ss %xmm0, %xmm0
jmp 0x2fa5d
movss 0x2a2cf(%rip), %xmm0    # 0x59d2c
xorps 0x26ecc(%rip), %xmm0    # 0x56930
movaps %xmm0, 0x80(%rsp)
movss 0x2159c(%rip), %xmm1    # 0x51010
movsd 0x215c4(%rip), %xmm2    # 0x51040
xorps %xmm0, %xmm0
movss %xmm0, 0x8(%rsp)
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x70(%rsp)
movaps %xmm1, 0x40(%rsp)
movsd %xmm2, 0x50(%rsp)
movss 0x8(%rsp), %xmm0
movaps %xmm0, 0x90(%rsp)
ucomiss 0x21561(%rip), %xmm1    # 0x51010
jne 0x2fadb
jp 0x2fadb
testl %ebp, %ebp
jle 0x2fb16
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 0x2fab9
jmp 0x2fb16
testl %ebp, %ebp
jle 0x2fb16
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 0x2faf0
movq %r12, %rdi
movq %r14, %rsi
movl %ebp, %edx
leaq 0x1c(%rsp), %rcx
leaq 0x24(%rsp), %r8
callq 0x10d91
movss %xmm0, 0x20(%rsp)
movaps 0x40(%rsp), %xmm0
ucomiss 0x214d1(%rip), %xmm0    # 0x51010
jne 0x2fbaf
jp 0x2fbaf
testl %ebp, %ebp
jle 0x2fc98
xorps %xmm1, %xmm1
xorl %ebx, %ebx
xorps %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
movsd %xmm1, 0x10(%rsp)
movss 0x1c(%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 0x22a0
movsd 0x10(%rsp), %xmm1
addsd %xmm0, %xmm1
incq %rbx
cmpq %rbx, %r13
jne 0x2fb59
jmp 0x2fca4
movaps %xmm0, %xmm1
addss 0x2144a(%rip), %xmm1    # 0x51004
testl %ebp, %ebp
movaps %xmm1, 0x70(%rsp)
jle 0x2fc98
movss 0x21441(%rip), %xmm0    # 0x51010
divss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x58(%rsp)
movss 0x24(%rsp), %xmm2
movaps %xmm2, %xmm0
divss 0x1c(%rsp), %xmm0
cmpltss 0x21417(%rip), %xmm2    # 0x5100c
movaps %xmm2, %xmm1
andps 0x80(%rsp), %xmm1
andnps %xmm0, %xmm2
orps %xmm1, %xmm2
movaps %xmm2, 0xa0(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x10(%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 0x22a0
movsd 0x10(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x10(%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 0x2fc1f
movsd 0x10(%rsp), %xmm1
jmp 0x2fca4
xorps %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
xorps %xmm1, %xmm1
movaps %xmm1, %xmm0
callq 0x2120
movsd 0x8(%rsp), %xmm1
addsd %xmm0, %xmm1
movss 0x20(%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 0x2ac29(%rip), %rsi     # 0x5a924
movsd 0x50(%rsp), %xmm0
movb $0x3, %al
callq 0x2190
movaps 0x40(%rsp), %xmm1
addss 0x275cf(%rip), %xmm1    # 0x572e4
xorps %xmm2, %xmm2
cvtss2sd %xmm1, %xmm2
movss 0x2a00c(%rip), %xmm0    # 0x59d30
ucomiss %xmm1, %xmm0
jae 0x2fa8f
xorps %xmm0, %xmm0
cvtss2sd 0x30(%rsp), %xmm0
leaq 0x2ac09(%rip), %rsi     # 0x5a946
movq 0x28(%rsp), %rbx
movq %rbx, %rdi
movb $0x1, %al
callq 0x2190
xorps %xmm0, %xmm0
cvtss2sd 0x8(%rsp), %xmm0
leaq 0x2ac06(%rip), %rsi     # 0x5a962
movq %rbx, %rdi
movb $0x1, %al
callq 0x2190
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