Exemplo n.º 1
0
GaussFunc::GaussFunc(int nIndexes, double n_mean = 0, double n_variance = 1.0, double coef = 1.0)
:	size(nIndexes)
{
	this->pdf_list = new double [nIndexes];
	double k = coef / std::sqrt(2 * PI * n_variance);
	init_prob(n_mean, k, n_variance);
}
Exemplo n.º 2
0
static int bond_cir1d(double r0,double k,double t0, double sigma,double theta,double T,long Nt,double *price)
{
  int j;
  double val,val1;

  /*Compute probabilities*/
  init_prob(k,sigma,theta,T,t0,Nt);

  /*Compute Zero Coupon Prices*/
  zcb_cir(Nt);

  /*Linear Interpolation*/
  j=0;
  while(r_vect[j]<r0)
    j++;
  val= Ps[0][j];
  val1= Ps[0][j-1];

  /*Price*/
  *price=val+(val-val1)*(r0-SQR(r_vect[j]))/(SQR(r_vect[j])-SQR(r_vect[j-1]));

  
  /*Memory Disallocation*/
  free_memory(Nt);


  return OK;
}
Exemplo n.º 3
0
void driver ( int nx, int ny, int nz, long int it_max, double tol,
              double xlo, double ylo, double zlo,
              double xhi, double yhi, double zhi, int io_interval){
    double *f, *u;
    char * rb;
    int i,j,k;
    double secs = -1.0;
    struct timespec start, finish;

    /* Allocate and initialize  */
    f = ( double * ) malloc ( nx * ny * nz * sizeof ( double ) );  
    u = ( double * ) malloc ( nx * ny * nz * sizeof ( double ) );
    rb = ( char * ) malloc (nx * ny * nz * sizeof ( char ) );

    get_time( &start );
    omp_set_num_threads(6);
    #pragma omp parallel for default(none)\
    	shared(nx, ny, nz, u) private(i, j, k) 
    for ( k = 0; k < nz; k++ ) 
      for ( j = 0; j < ny; j++ ) 
        for ( i = 0; i < nx; i++ ) 
          U(i,j,k) = 0.0;  /* note use of array indexing macro */

    /* set rhs, and exact bcs in u */
    init_prob ( nx, ny, nz, f, u , xlo, ylo, zlo, xhi, yhi, zhi);
    // rb has the red and black positions
    rb_vector(rb, nx, ny, nz);
    /* Solve the Poisson equation  */
    //jacobi ( nx, ny, nz, u, f, tol, it_max, xlo, ylo, zlo, xhi, yhi, zhi, io_interval );
    //gauss_seidel ( nx, ny, nz, u, f, tol, it_max, xlo, ylo, zlo, xhi, yhi, zhi, io_interval, rb );

    SOR( nx, ny, nz, u, f, tol, it_max, xlo, ylo, zlo, xhi, yhi, zhi, io_interval , rb);
    /* Determine the error  */
    calc_error ( nx, ny, nz, u, f, xlo, ylo, zlo, xhi, yhi, zhi );

    /* get time for initialization and iteration.  */
    get_time(&finish);
    secs = timespec_diff(start,finish);
    printf(" Total time: %15.7e seconds\n",secs);

    free ( u );
    free ( f );
    free (rb);
    return;
}
Exemplo n.º 4
0
MaxResults adjust_max_map(Model B, PoSition **Pos,  double *adj_map)

   /*=======================================================================*/
   /* FUNCTION NAME : adjust_max_map                                        */
   /*                                                                       */
   /* DESCRIPTION : Improves the maximal alignment by including motif       */
   /*               motif elements whose inclusion (whether it is forward   */
   /*               or reverse complement) improves upon the maximum        */
   /*               calculated map value                                    */
   /*=======================================================================*/
 
{
  double     map_fwd, map_rev, map_same;
  int        n, t, newpos; 
  MaxResults M; /** Have to set these -- will then be returned **/
  int        last_inc;
  double     dMax, dLocMax;
  double     **dProbArray;
  int        maxnum;
  ProbStruct P;		/* BT 2/21/97 */
  int        start, end;
  int        nSeq;
  int        typ;
  int        p;
  int        len;

  init_maxdata(&M);
  map_fwd = map_rev = map_same = (*adj_map);
  for( nSeq = 0; nSeq < B->IP->nNumSequences; nSeq += SeqInc( B, nSeq ) )
    {
      len = SequenceLength( B, nSeq ); 
      start = SequenceStartPos( B, nSeq );
      end = SequenceEndPos( B, nSeq );
      for(n = start; n <= end; n++) 
	{      
	  for(t = 0; t < B->IP->nNumMotifTypes; t++) 
	    {
	      if(((B->IP->is_defined[cl_E] && 
		   B->RP->nSites[nSeq] < B->RP->nMaxBlocks) || 
		  (! B->IP->is_defined[cl_E])) &&
		 PossibleStartPos( Pos, n, t, len, nSeq, B) &&
		 !OverlapsPrevMotif(n,B->IP->nNumMotifTypes,Pos) &&  
		 !Pos[t][n].nMotifStartPos) 
		{
		  newpos = -1;	/* BT 3/12/97 */
		  if( AnyOverlap(B, n, t, Pos, &newpos, &typ) )
		    {
		      for( p = 0; p < SeqInc( B, nSeq ); p++ )
			{
			  adjust_counts(B,DELETE,newpos + p * len, typ, Pos[typ][newpos + p * len].RevComp);
			  B->IP->nNumMotifs[typ][Pos[typ][newpos + p * len].RevComp]--;
			  B->RP->nSites[nSeq + p]--;
			  not_in_motif(Pos, newpos + p * len,B,typ);
			}
		    } 
		  
		  /* Calculate the map values for the current position */
		  /* being excluded (map_same), included as a forward  */
		  /* motif (map_fwd), or being included as a reverse   */
		  /* complement motif (map_rev).                       */
		  
		  map_same = CalcMapProb(B, B->IP->is_defined[cl_R]); 
		  
		  if( PossibleStartPos( Pos, n, t, len, nSeq, B) )
		    {		  
		      for( p = 0; p < SeqInc( B, nSeq ); p++ )
			{
			  adjust_counts(B, ADD, n + p * len,t,FALSE);
			  B->RP->nSites[nSeq+p]++;	      
			  B->IP->nNumMotifs[t][FORWARD]++;		/* BT 3/12/97 */
			}
		      map_fwd = CalcMapProb(B, B->IP->is_defined[cl_R]);	      
		      for( p = 0; p < SeqInc( B, nSeq ); p++ )
			{
			  adjust_counts(B, DELETE, n + p * len,t,FALSE);
			  B->RP->nSites[nSeq+p]--;
			  B->IP->nNumMotifs[t][FORWARD]--;		/* BT 3/12/97 */
			}
		      
		      if(B->IP->RevComplement) 
			{
			  for( p = 0; p < SeqInc( B, nSeq ); p++ )
			    {
			      adjust_counts(B, ADD, n + p * len, t,TRUE);
			      B->RP->nSites[nSeq+p]++;
			      B->IP->nNumMotifs[t][REVERSE]++;		/* BT 3/12/97 */
			    }
			  map_rev = CalcMapProb(B, B->IP->is_defined[cl_R]);
			  for( p = 0; p < SeqInc( B, nSeq ); p++ )
			    {
			      adjust_counts(B, DELETE, n + p * len, t, TRUE);
			      B->RP->nSites[nSeq+p]--;
			      B->IP->nNumMotifs[t][REVERSE]--;		/* BT 3/12/97 */
			    }
			}
		      
		      /* See if the forward map improves the maximal */
		      /* map calculated so far                       */
		      
		      if((map_fwd >= map_same)  && (map_fwd >= map_rev) &&
			 (map_fwd >= (*adj_map)))
			{
			  for( p = 0; p < SeqInc( B, nSeq ); p++ )
			    {
			      adjust_counts(B, ADD, n + p * len,t, FALSE);
			      B->RP->nSites[nSeq+p]++;
			      B->IP->nNumMotifs[t][FORWARD]++;
			      set_in_motif(Pos,n + p * len,B,t,FALSE);
			    }
			  (*adj_map) = map_fwd;
			  
			  /* as this segment in alignment, we need to check */
			  /* segments do not overlap with this segment */
			  n += (MotifWidth( B, t ) - 1);
			}		
		      
		      /* See if the reverse complement map improves */
		      /* the maximal map calculated so far          */
		      
		      else if(B->IP->RevComplement && 
			      (map_rev >= map_same) && (map_rev >= map_fwd) && 
			      (map_rev >= (*adj_map))) 
			{
			  for( p = 0; p < SeqInc( B, nSeq ); p++ )
			    {
			      adjust_counts(B, ADD, n + p * len,t, TRUE);
			      B->RP->nSites[nSeq+p]++;
			      B->IP->nNumMotifs[t][REVERSE]++;
			      set_in_motif(Pos,n + p * len,B,t,TRUE);
			    }
			  (*adj_map) = map_rev;
			  /* as this segment in alignment, we need to check */
			  /* segments do not overlap with this segment */
			  n += (MotifWidth( B, t ) - 1);
			}     		  
		      /* if we removed a motif and things did not improve when we */
		      /* added n, put the old motif back */
		      /* BT 3/12/97 */ 
		      else if( newpos >= 0 )
			{
			  for( p = 0; p < SeqInc( B, nSeq ); p++ )
			    {
			      adjust_counts(B, ADD,newpos + p * len,typ,Pos[typ][newpos + p * len].RevComp);
			      B->IP->nNumMotifs[typ][Pos[typ][newpos+p*len].RevComp]++;
			      set_in_motif(Pos,newpos + p*len,B,typ, Pos[typ][newpos+p*len].RevComp);
			      B->RP->nSites[nSeq+p]++;
			    }
			}           		
		      else if(Pos[t][n].nMotifStartPos) 
			/* as this segment in alignment, we need to check */
			/* segments do not overlap with this segment */
			n += (MotifWidth( B, t ) - 1);
		    }
		}
	    }
	}
    }
   
   maxnum = findMaxNumMotif(B->IP);
      
   init_prob( &P, B->IP );			/* BT 2/21/97 */
   update_prob( &P, B, TRUE  );
   update_posterior_prob(B);		/* BT 3/17/97 */

   dProbArray = setElementProb( B, Pos, P );

   M = setMaxData(B->F, B->IP, (*adj_map), 1, Pos, &last_inc, &dMax,
                  &dLocMax, M, dProbArray, B);
   FREEP(dProbArray, maxnum); 
   M.frequency = NULL; 

   free_prob( &P, B->IP );
    
   return M;
}