Esempio n. 1
0
int main(void)
{
   int n=400, noisy=0, i,j;
   int nr=10, ir, TimeSquare=10, algorithm; /* TimeSquare should be larger for large t */
   double t=5, *Q, *pi, *space, s;
   char timestr[96], *AlgStr[2]={"repeated squaring", "eigensolution"};
   
   if((Q=(double*)malloc(n*n*5*sizeof(double))) ==NULL) error2("oom");
   pi=Q+n*n; space=pi+n;

   for(algorithm=0; algorithm<2; algorithm++) {
      starttime();
      SetSeed(1234567);
      for (i=0; i<n; i++)  pi[i]=rndu();
      s=sum(pi,n);
      for (i=0; i<n; i++)  pi[i]/=s;

      for(ir=0; ir<nr; ir++) {
         printf("Replicate %d/%d ", ir+1,nr);

   	   for (i=0; i<n; i++)  
            for (j=0,Q[i*n+i]=0; j<i; j++)
               Q[i*n+j]=Q[j*n+i] = square(rndu());
         for (i=0; i<n; i++)
            for (j=0; j<n; j++)
               Q[i*n+j] *= pi[j];
         for(i=0,s=0; i<n; i++)  {  /* rescaling Q so that average rate is 1 */
            Q[i*n+i]=0; Q[i*n+i]=-sum(Q+i*n, n); 
            s-=pi[i]*Q[i*n+i];
         }

         if(noisy) {
            matout(stdout, pi, 1, n);
            matout(stdout, Q, n, n);
         }

         if(algorithm==0) 
            matexp(Q, 1, n, TimeSquare, space);
         else 
            PMatQRev(Q, pi, 1, n, space);
         printf("%s, time: %s\n", AlgStr[algorithm], printtime(timestr));
         if(noisy) 
            matout(stdout, Q, n, n);
      }
   }
   return (0);
}
Esempio n. 2
0
int matout_string(char *filename,char *varname,char *string, char *mode)
{
  int err;

  err = matout(filename,varname,string,1,strlen(string),'t',mode,'L');
  // 'L' mode doesn't matter for strings.

  return (err);
}
Esempio n. 3
0
int between_f_and_x (void)
{
/* this helps with the exponential transform for frequency parameters */
   int i,n,fromf=0;
   double x[100];

   for(;;) {
      printf("\ndirection (0:x=>f; 1:f=>x; -1:end)  &  #classes? ");
      scanf("%d",&fromf);    
      if(fromf==-1) return(0);
      scanf("%d", &n);  if(n>100) error2("too many classes");
      printf("input the first %d values for %s? ",n-1,(fromf?"f":"x"));
      FOR(i,n-1) scanf("%lf",&x[i]);
      x[n-1]=(fromf?1-sum(x,n-1):0);
      f_and_x(x, x, n, fromf, 1);
      matout(F0,x,1,n);
   }
}