// File: /workspace/github_c_makefile_star2_O2/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, %ebx
movq %rsi, %r15
movq %rdi, %r12
movslq %edx, %rbp
leaq (,%rbp,4), %rdi
callq 0x21d0
movq %rax, %r14
leaq 0x2835f(%rip), %rdi     # 0x54800
pushq $0x27
popq %rsi
pushq $0x1
popq %rdx
movq %r13, 0x58(%rsp)
movq %r13, %rcx
callq 0x2280
xorl %eax, %eax
testl %ebp, %ebp
movl $0x0, %r13d
movl %ebx, %ebp
cmovgl %ebx, %r13d
movss 0x24d24(%rip), %xmm0    # 0x511f0
cmpq %rax, %r13
je 0x2c4e3
movss (%r12,%rax,4), %xmm1
minss %xmm0, %xmm1
incq %rax
movaps %xmm1, %xmm0
jmp 0x2c4cc
cvtss2sd %xmm0, %xmm0
addsd 0x27721(%rip), %xmm0    # 0x53c10
cvtsd2ss %xmm0, %xmm0
xorps 0x24366(%rip), %xmm0    # 0x50860
movaps %xmm0, 0x90(%rsp)
movss 0x1eb06(%rip), %xmm0    # 0x4b010
xorps %xmm1, %xmm1
movaps %xmm1, 0x70(%rsp)
movaps %xmm0, %xmm3
xorps %xmm2, %xmm2
movaps %xmm1, 0x30(%rsp)
movss 0x2770f(%rip), %xmm1    # 0x53c34
ucomiss %xmm3, %xmm1
movaps %xmm2, 0x80(%rsp)
jb 0x2c7a7
movaps %xmm3, 0x40(%rsp)
ucomiss %xmm0, %xmm3
jne 0x2c563
jp 0x2c563
xorl %ebx, %ebx
cmpq %rbx, %r13
je 0x2c596
cvtss2sd (%r15,%rbx,4), %xmm0
callq 0x2120
cvtsd2ss %xmm0, %xmm0
movss %xmm0, (%r14,%rbx,4)
incq %rbx
jmp 0x2c544
cvtss2sd 0x70(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
xorl %ebx, %ebx
cmpq %rbx, %r13
je 0x2c596
cvtss2sd (%r15,%rbx,4), %xmm0
movsd 0x8(%rsp), %xmm1
callq 0x20a0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, (%r14,%rbx,4)
incq %rbx
jmp 0x2c571
movq %r12, %rdi
movq %r14, %rsi
movl %ebp, %edx
leaq 0x1c(%rsp), %rcx
leaq 0x2c(%rsp), %r8
callq 0xfe18
movss %xmm0, 0x28(%rsp)
movss 0x1ea55(%rip), %xmm0    # 0x4b010
movaps 0x40(%rsp), %xmm1
ucomiss %xmm0, %xmm1
jne 0x2c63a
jp 0x2c63a
xorps %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
xorl %ebx, %ebx
cmpq %rbx, %r13
je 0x2c712
movsd %xmm0, 0x8(%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 0x10(%rsp), %xmm2
addsd %xmm1, %xmm2
movsd %xmm2, 0x10(%rsp)
cvtss2sd %xmm0, %xmm0
callq 0x22a0
movaps 0x40(%rsp), %xmm1
movsd 0x8(%rsp), %xmm2
addsd %xmm0, %xmm2
movsd %xmm2, 0x8(%rsp)
movsd 0x8(%rsp), %xmm0
incq %rbx
jmp 0x2c5d2
addss 0x1e9c2(%rip), %xmm1    # 0x4b004
movaps %xmm1, 0x70(%rsp)
divss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x60(%rsp)
movss 0x2c(%rsp), %xmm2
movaps %xmm2, %xmm0
divss 0x1c(%rsp), %xmm0
cmpltss 0x1e99f(%rip), %xmm2    # 0x4b00c
movaps %xmm2, %xmm1
andps 0x90(%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)
cmpq %rbx, %r13
je 0x2c707
movss (%r12,%rbx,4), %xmm0
addss 0xa0(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
callq 0x2120
mulsd 0x60(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp)
callq 0x22a0
movsd 0x8(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rsp)
cvtss2sd (%r15,%rbx,4), %xmm0
movsd %xmm0, 0x68(%rsp)
callq 0x2120
subsd 0x20(%rsp), %xmm0
mulsd 0x68(%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x10(%rsp)
incq %rbx
jmp 0x2c697
movaps 0x40(%rsp), %xmm1
movsd 0x8(%rsp), %xmm0
cvtss2sd %xmm1, %xmm1
movsd %xmm1, 0x20(%rsp)
callq 0x2120
movsd 0x10(%rsp), %xmm1
addsd %xmm0, %xmm1
movss 0x28(%rsp), %xmm2
movaps %xmm2, %xmm0
movaps 0x30(%rsp), %xmm3
maxss %xmm3, %xmm0
movss %xmm0, 0x8(%rsp)
cmpltss %xmm2, %xmm3
movaps %xmm3, %xmm0
andps 0x40(%rsp), %xmm0
andnps 0x80(%rsp), %xmm3
orps %xmm0, %xmm3
movaps %xmm3, 0x30(%rsp)
cvtss2sd %xmm2, %xmm2
movq 0x58(%rsp), %rdi
leaq 0x280b8(%rip), %rsi     # 0x54828
movsd 0x20(%rsp), %xmm0
movb $0x3, %al
callq 0x2190
movaps 0x40(%rsp), %xmm3
addss 0x24a6a(%rip), %xmm3    # 0x511f4
movaps 0x30(%rsp), %xmm2
movss 0x8(%rsp), %xmm0
movaps %xmm0, 0x30(%rsp)
movss 0x1e86e(%rip), %xmm0    # 0x4b010
jmp 0x2c51d
cvtss2sd %xmm2, %xmm0
leaq 0x28098(%rip), %rsi     # 0x5484a
movq 0x58(%rsp), %rbp
movq %rbp, %rdi
movb $0x1, %al
callq 0x2190
cvtss2sd 0x30(%rsp), %xmm0
leaq 0x28098(%rip), %rsi     # 0x54866
movq %rbp, %rdi
movb $0x1, %al
callq 0x2190
movq %r14, %rdi
callq 0x2050
movaps 0x80(%rsp), %xmm0
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq