示例#1
0
/***********************************************************************
 * Copy a motif from one place to another.
 ***********************************************************************/
void copy_motif
  (MOTIF_T* source,
   MOTIF_T* dest)
{
  strcpy(dest->id, source->id);
  strcpy(dest->id2, source->id2);
  dest->length = source->length;
  dest->alph_size = source->alph_size;
  dest->ambigs = source->ambigs;
  dest->evalue = source->evalue;
  dest->num_sites = source->num_sites;
  dest->complexity = source->complexity;
  dest->trim_left = source->trim_left;
  dest->trim_right = source->trim_right;
  if (source->freqs) {
    // Allocate memory for the matrix.
    dest->freqs = allocate_matrix(dest->length, dest->alph_size + dest->ambigs);
    // Copy the matrix.
    copy_matrix(source->freqs, dest->freqs);
  } else {
    dest->freqs = NULL;
  }
  if (source->scores) {
    // Allocate memory for the matrix. Note that scores don't contain ambigs.
    dest->scores = allocate_matrix(get_num_rows(source->scores), get_num_cols(source->scores));
    // Copy the matrix.
    copy_matrix(source->scores, dest->scores);
  } else {
    dest->scores = NULL;
  }
  copy_string(&(dest->url), source->url);
}
/*****************************************************************************
 * MEME > motifs > motif
 * Construct the skeleton of a motif.
 ****************************************************************************/
void mxml_start_motif(void *ctx, char *id, char *name, char *alt, int width, double sites, 
    double llr, double ic, double re, double bayes_threshold,
    double log10_evalue, double elapsed_time, char *url) {
  CTX_T *data;
  MOTIF_T *motif;
  
  data = (CTX_T*)ctx;
  data->mscope.motif = mm_malloc(sizeof(MOTIF_T));
  motif = data->mscope.motif;
  memset(motif, 0, sizeof(MOTIF_T));
  set_motif_id(name, strlen(name), motif);
  set_motif_id2(alt, sizeof(alt), motif);
  set_motif_strand('+', motif);
  motif->length = width;
  motif->num_sites = sites;
  motif->url = strdup(url);
  motif->log_evalue = log10_evalue;
  motif->evalue = pow(10.0, log10_evalue);
  // calculate alphabet size
  motif->alph = alph_hold(data->alph);
  motif->flags = (data->fscope.strands == 2 ? MOTIF_BOTH_STRANDS : 0);
  // allocate matricies
  motif->freqs = allocate_matrix(motif->length, alph_size_core(motif->alph));
  init_matrix(-1, motif->freqs);
  motif->scores = allocate_matrix(motif->length, alph_size_core(motif->alph));
  init_matrix(NO_SCORE, motif->scores);
  // should be set by a post processing method
  motif->complexity = -1;
  motif->trim_left = 0;
  motif->trim_right = 0;
  // cache motif position
  if (data->options & SCANNED_SITES) {
    rbtree_put(data->motif_lookup, id, &(data->current_motif));
  }
}
示例#3
0
文件: matrix.c 项目: IceNerd/hogwarts
main()
{
	clock_t start, stop;
	a = allocate_matrix( NUM );
	b = allocate_matrix( NUM );
	c = allocate_matrix( NUM );
	d = allocate_matrix( NUM );
	printf("Address a = %x\n", a );
	printf("Address b = %x\n", b );
	printf("Address c = %x\n", c );

// initialize the arrays with data
	init_arr(3,-2,1,a);
	init_arr(-2,1,3,b);

	//start timing the matrix multiply code
	start = clock();
	multiply_mt();
	stop = clock();

// print elapsed time
	printf("Elapsed time = %lf seconds\n",
    		((double)(stop - start)) / CLOCKS_PER_SEC);
	free_matrix( a );
	free_matrix( b );
	free_matrix( c );
	free_matrix( d );


}
示例#4
0
// ----------------------------------------------------------------
// Randomly populates lattice bonds, marks all clusters, and determines whether
// the center site is in the largest cluster.  Plots the result to the screen.
// One may then visually verify the membership computation.
static void test_A_in_C(int argc, char** argv)
{
	int   M = 18;
	int   N = 18;
	double p = 0.6;
	int print_lattice = 0;
	int argi;
	int** vbonds;
	int** hbonds;
	int** site_marks;
	int num_clusters;
	int* cluster_sizes;
	int C_clno; // Number of largest cluster
	int A[d], A_clno;

	for (argi = 2; argi < argc; argi++) {
		if (sscanf(argv[argi], "M=%d", &M) == 1)
			;
		else if (sscanf(argv[argi], "N=%d", &N) == 1)
			;
		else if (sscanf(argv[argi], "MN=%d", &M) == 1)
			N = M;
		else if (sscanf(argv[argi], "p=%lf", &p) == 1)
			;
		else if (sscanf(argv[argi], "pl=%d", &print_lattice) == 1)
			;
		else
			usage(argv[0], argv[1], 0);
	}
	if ((M < 3) || (N < 3))
		usage(argv[0], argv[1], 1);

	vbonds = allocate_matrix(M, N, 0);
	hbonds = allocate_matrix(M, N, 0);
	site_marks = allocate_matrix(M, N, SITECHAR);
	set_A1(A, M, N);

	populate_bonds(vbonds, hbonds, M, N, p);
	mark_cluster_numbers(site_marks, vbonds, hbonds, M, N, &num_clusters);
	if (print_lattice)
		print_lattice_and_cluster_numbers(site_marks, vbonds, hbonds, M, N);
	sanity_check_cluster_numbers(site_marks, vbonds, hbonds, M, N);
	cluster_sizes = (int*)malloc_or_die(sizeof(int) * num_clusters);
	get_cluster_sizes(site_marks, M, N, num_clusters, cluster_sizes,
		&C_clno);

	A_clno = site_marks[A[0]][A[1]];
	if (print_lattice) {
		printf("A at %d, %d\n", A[0], A[1]);
		printf("A cluster number = %d\n", A_clno);
		printf("Largest cluster  = %d\n", C_clno);
	}
	printf("%s\n", (A_clno == C_clno) ? "Yes" : "No");

	free_matrix(vbonds,     M, N);
	free_matrix(hbonds,     M, N);
	free_matrix(site_marks, M, N);
	free(cluster_sizes);
}
示例#5
0
// ----------------------------------------------------------------
// Randomly populates lattice bonds, marks all clusters, and computes cluster
// sizes.  One may then visually verify the cluster-size computation.
static void test_cluster_sizes(int argc, char** argv)
{
	int   M = 18;
	int   N = 18;
	double p = 0.6;
	int print_lattice = 0;
	int argi;
	int** vbonds;
	int** hbonds;
	int** site_marks;
	int num_clusters;
	int* cluster_sizes;
	int C_clno; // Number of largest cluster
	int k;

	for (argi = 2; argi < argc; argi++) {
		if (sscanf(argv[argi], "M=%d", &M) == 1)
			;
		else if (sscanf(argv[argi], "N=%d", &N) == 1)
			;
		else if (sscanf(argv[argi], "MN=%d", &M) == 1)
			N = M;
		else if (sscanf(argv[argi], "p=%lf", &p) == 1)
			;
		else if (sscanf(argv[argi], "pl=%d", &print_lattice) == 1)
			;
		else
			usage(argv[0], argv[1], 0);
	}
	if ((M < 3) || (N < 3))
		usage(argv[0], argv[1], 1);

	vbonds = allocate_matrix(M, N, 0);
	hbonds = allocate_matrix(M, N, 0);
	site_marks = allocate_matrix(M, N, SITECHAR);

	populate_bonds(vbonds, hbonds, M, N, p);
	mark_cluster_numbers(site_marks, vbonds, hbonds, M, N, &num_clusters);
	if (print_lattice)
		print_lattice_and_cluster_numbers(site_marks, vbonds, hbonds, M, N);
	sanity_check_cluster_numbers(site_marks, vbonds, hbonds, M, N);
	cluster_sizes = (int*)malloc_or_die(sizeof(int) * num_clusters);
	get_cluster_sizes(site_marks, M, N, num_clusters, cluster_sizes,
		&C_clno);
	for (k = 0; k < num_clusters; k++) {
		printf("Cluster    %3d", k);
		printf(" size %6d %7.4lf", cluster_sizes[k],
			(double)cluster_sizes[k]/(double)M/(double)N);
		if (k == C_clno)
			printf(" *");
		printf("\n");
	}

	free_matrix(vbonds,     M, N);
	free_matrix(hbonds,     M, N);
	free_matrix(site_marks, M, N);
	free(cluster_sizes);
}
示例#6
0
文件: subst-matrix.c 项目: CPFL/gmeme
extern MATRIX_T* gen_pam_matrix(
  ALPH_T alph,                  /* alphabet */
  int dist,			/* PAM distance */
  BOOLEAN_T logodds		/* true: generate log-odds matrix 
				   false: generate target frequency matrix 
				*/
)
{
  assert(alph == DNA_ALPH || alph == PROTEIN_ALPH);
  int i, j;
  MATRIX_T *matrix, *mul;
  BOOLEAN_T dna = (alph == DNA_ALPH);
  double *pfreq = dna ? pam_dna_freq : pam_prot_freq;	// standard frequencies
  int alen = alph_size(alph, ALPH_SIZE);  // length of standard alphabet
  double factor = dist < 170 ? 2/log(2) : 3/log(2);	// same as in "pam" Version 1.0.6

  /* create the array for the joint probability matrix */
  matrix = allocate_matrix(alen, alen);
  mul = allocate_matrix(alen, alen);

  /* initialize the matrix: PAM 1:
     due to roundoff, take the average of the two estimates of the joint frequency
     of i and j as the joint, then compute the conditionals for the matrix
  */
  for (i=0; i<alen; i++) {
    for (j=0; j<=i; j++) {
      double vij = dna ? trans[i][j] : dayhoff[i][j];
      double vji = dna ? trans[j][i] : dayhoff[j][i];
      double joint = ((vij * pfreq[j]) + (vji * pfreq[i]))/20000;/* use average to fix rndoff */
      set_matrix_cell(i, j, joint/pfreq[j], matrix);
      if (i!=j) set_matrix_cell(j, i, joint/pfreq[i], matrix);
    }
  }

  /* take PAM matrix to desired power to scale it */ 
  copy_matrix(matrix, mul);
  for (i=dist; i>1; i--) {
    MATRIX_T *product = matrix_multiply(matrix, mul);
    SWAP(MATRIX_T*, product, matrix)
    free_matrix(product);
  } 
  free_matrix(mul);

  /* convert to joint or logodds matrix:
     target:  J_ij = Pr(i,j) = Mij pr(j) 
     logodds: L_ij = log (Pr(i,j)/(Pr(i)Pr(j)) = log (Mij Pr(j)/Pr(i)Pr(j)) = log(Mij/pr(i)) 
  */
  for (i=0; i<alen; i++) {
    for (j=0; j<alen; j++) {
      double vij = get_matrix_cell(i, j, matrix);
      vij = logodds ? nint(factor * log((vij+EPSILON)/pfreq[i])) : vij * pfreq[j];
      set_matrix_cell(i, j, vij, matrix);
    }
  }

  return matrix;
} /* gen_pam_matrix */
示例#7
0
// ----------------------------------------------------------------
// Randomly populates lattice bonds, prints the lattice, and lists the bonded
// neighbors of the center site.  One may then visually verify the
// identification of neighbors.
static void test_get_bonded_neighbors(int argc, char** argv)
{
	int   M = 18;
	int   N = 18;
	double p = 0.6;
	int argi;
	int** vbonds;
	int** hbonds;
	int** site_marks;
	int A1[d];
	int A2[d];
	int neighbors[MAXNEI][d];
	int numnei;
	int k;

	for (argi = 2; argi < argc; argi++) {
		if (sscanf(argv[argi], "M=%d", &M) == 1)
			;
		else if (sscanf(argv[argi], "N=%d", &N) == 1)
			;
		else if (sscanf(argv[argi], "MN=%d", &M) == 1)
			N = M;
		else if (sscanf(argv[argi], "p=%lf", &p) == 1)
			;
		else
			usage(argv[0], argv[1], 0);
	}
	if ((M < 3) || (N < 3))
		usage(argv[0], argv[1], 1);

	vbonds = allocate_matrix(M, N, 0);
	hbonds = allocate_matrix(M, N, 0);
	site_marks = allocate_matrix(M, N, SITECHAR);
	set_A1_A2(A1, A2, M, N);

	populate_bonds(vbonds, hbonds, M, N, p);
	print_lattice(site_marks, vbonds, hbonds, M, N, A1, A2);
	printf("\n");

	get_bonded_neighbors(vbonds, hbonds, M, N, A1, neighbors, &numnei);

	printf("Bonded neighbors of A1 at (%d, %d):\n", A1[0], A1[1]);

	if (numnei == 0)
		printf("(none)\n");

	for (k = 0; k < numnei; k++) {
		printf("  ( %3d, %3d )\n", neighbors[k][0], neighbors[k][1]);
	}

	free_matrix(vbonds,     M, N);
	free_matrix(hbonds,     M, N);
	free_matrix(site_marks, M, N);
}
示例#8
0
// ----------------------------------------------------------------
// Randomly populates lattice bonds, marks the cluster containing the center
// site, and plots the results as a PPM file.  One may then visually verify
// the cluster-marking computation.
static void test_plot_cluster(int argc, char** argv)
{
	int   M = 200;
	int   N = 200;
	double p = 0.6;
	int argi;
	int** vbonds;
	int** hbonds;
	int** site_marks;
	int A1[d];
	int A2[d];
	char* image_file_name = "p2.ppm";
	int compact = 0;

	for (argi = 2; argi < argc; argi++) {
		if (sscanf(argv[argi], "M=%d", &M) == 1)
			;
		else if (sscanf(argv[argi], "N=%d", &N) == 1)
			;
		else if (sscanf(argv[argi], "MN=%d", &M) == 1)
			N = M;
		else if (sscanf(argv[argi], "p=%lf", &p) == 1)
			;
		else if (strncmp(argv[argi], "f=", 2) == 0)
			image_file_name = &argv[argi][2];
		else if (sscanf(argv[argi], "c=%d", &compact) == 1)
			;
		else
			usage(argv[0], argv[1], 0);
	}
	if ((M < 3) || (N < 3))
		usage(argv[0], argv[1], 1);

	vbonds = allocate_matrix(M, N, 0);
	hbonds = allocate_matrix(M, N, 0);
	site_marks = allocate_matrix(M, N, SITECHAR);
	set_A1_A2(A1, A2, M, N);

	populate_bonds(vbonds, hbonds, M, N, p);
	mark_one_cluster(site_marks, vbonds, hbonds, M, N, A1, VISITEDCHAR);
	if (compact)
		plot_one_cluster_compactly(site_marks, vbonds, hbonds, M, N,
			A1, A2, image_file_name);
	else
		plot_lattice_and_one_cluster(site_marks, vbonds, hbonds, M, N,
			A1, A2, image_file_name);
	printf("Wrote %s.\n", image_file_name);

	free_matrix(vbonds,     M, N);
	free_matrix(hbonds,     M, N);
	free_matrix(site_marks, M, N);
}
示例#9
0
// ----------------------------------------------------------------
// Randomly populates lattice bonds and plots it to the screen using ASCII art.
static void test_print_lattice(int argc, char** argv)
{
	int   M = 18;
	int   N = 18;
	double p = 0.6;
	int argi;
	int** vbonds;
	int** hbonds;
	int** site_marks;
	int A1[d];
	int A2[d];

	// Parse the command line.
	for (argi = 2; argi < argc; argi++) {
		if (sscanf(argv[argi], "M=%d", &M) == 1)
			;
		else if (sscanf(argv[argi], "N=%d", &N) == 1)
			;
		else if (sscanf(argv[argi], "MN=%d", &M) == 1)
			N = M;
		else if (sscanf(argv[argi], "p=%lf", &p) == 1)
			;
		else
			usage(argv[0], argv[1], 0);
	}
	// Validate parameters.
	if ((M < 3) || (N < 3))
		usage(argv[0], argv[1], 1);

	// Allocate storage for the lattice.
	vbonds = allocate_matrix(M, N, 0);
	hbonds = allocate_matrix(M, N, 0);
	site_marks = allocate_matrix(M, N, SITECHAR);

	// Pick center points of the lattice.
	set_A1_A2(A1, A2, M, N);

	// Populate the bonds with probability p.
	populate_bonds(vbonds, hbonds, M, N, p);

	// Print the lattice.
	print_lattice(site_marks, vbonds, hbonds, M, N, A1, A2);

	// Free the lattice storage.
	free_matrix(vbonds,     M, N);
	free_matrix(hbonds,     M, N);
	free_matrix(site_marks, M, N);
}
示例#10
0
文件: subst-matrix.c 项目: CPFL/gmeme
MATRIX_T *reorder_matrix(
  const char *alpha1,				/* current alphabet */
  const char *alpha2,				/* new alphabet; must be subset */
  MATRIX_T *in_matrix			/* matrix to reorder */
)
{
  int i, j;
  int alen1 = strlen(alpha1);
  int alen2 = strlen(alpha2);
  MATRIX_T *out_matrix;

  if (alen2 > alen1) 
    die("The new alphabet %s must be a subset of the old alphabet %s.\n", alpha2, alpha1);

  out_matrix = allocate_matrix(alen2, alen2);
  for (i=0; i<alen2; i++) {
    int ii = strchr(alpha1, alpha2[i]) - alpha1;
    for (j=0; j<alen2; j++) {
      int jj;
      char *ptr = strchr(alpha1, alpha2[j]);
      if (!ptr)
        die("The new alphabet %s must be a subset of the old alphabet %s\n", alpha2, alpha1);
      jj = ptr - alpha1;
      set_matrix_cell(i, j, get_matrix_cell(ii, jj, in_matrix), out_matrix);
    }
  }
  return(out_matrix);
} /* reorder_matrix */
TransitionParameters::TransitionParameters()
{
    // initialize training data
    TransitionTrainingData& td = training_data;
    td.n_matches = 0;
    td.n_merges = 0;
    td.n_skips = 0;

    //
    allocate_matrix(td.state_transitions, 3, 3);
    for(unsigned i = 0; i < td.state_transitions.n_rows; ++i) {
        for(unsigned j = 0; j < td.state_transitions.n_cols; ++j) {
            set(td.state_transitions, i, j, 0);
        }
    }

    //
    // Initialize transition parameters
    //

    // these are fixed across all models
    skip_bin_width = 0.5;
    skip_probabilities.resize(30);
 
    trans_start_to_clip = 0.5f;
    trans_clip_self = 0.90f;
}
示例#12
0
void dxml_start_motif(void *ctx, char *id, char *seq, int length,
                      long num_sites, long p_hits, long n_hits,
                      double pvalue, double evalue, double uevalue) {
    CTX_T *data;
    MOTIF_T *motif;

    data = (CTX_T*)ctx;
    data->motif = (MOTIF_T*)mm_malloc(sizeof(MOTIF_T));
    motif = data->motif;
    memset(motif, 0, sizeof(MOTIF_T));
    set_motif_id(seq, strlen(seq), motif);
    set_motif_id2("", 0, motif);
    set_motif_strand('+', motif);
    motif->length = length;
    motif->num_sites = num_sites;
    motif->evalue = evalue;
    // both DNA and RNA have 4 letters
    motif->alph = data->fscope.alphabet;
    motif->flags = MOTIF_BOTH_STRANDS; // DREME does not support the concept of single strand scanning (yet)
    // allocate the matrix
    motif->freqs = allocate_matrix(motif->length, alph_size(motif->alph, ALPH_SIZE));
    motif->scores = NULL; // no scores in DREME xml
    // no url in DREME
    motif->url = strdup("");
    // set by postprocessing
    motif->complexity = -1;
    motif->trim_left = 0;
    motif->trim_right = 0;
}
示例#13
0
文件: pssm.c 项目: a1aks/Haystack
/**************************************************************************
*
	hash_pssm

	Hash a PSSM (in place) where each n columns are combined into
	a single column on the alphabet hashed to the nth power.
*
**************************************************************************/
void hash_pssm(
  PSSM_T* pssm,				// PSSM to hash
  int n 				// hash n columns to 1
)
{
  int w = get_pssm_w(pssm);			// width of pssm
  int alen = get_pssm_alphsize(pssm);		// length of alphabet
  int hashed_w = (w+n-1)/n;			// width of hashed pssm
  int hashed_alen = pow(alen+1, n) + 1;		// length of hashed alphabet

  // Allocate the hashed PSSM.
  MATRIX_T* pssm_matrix = pssm->matrix;
  MATRIX_T* hashed_pssm_matrix = allocate_matrix(hashed_w, hashed_alen);

  int pos, hashed_pos;
  for (pos=hashed_pos=0; pos<w; pos+=n, hashed_pos++) {	// position in pssm
    hash_pssm_matrix_pos(pssm_matrix, hashed_pssm_matrix, pos, hashed_pos, n, 0, 0);
  } // position in pssm

  free_matrix(pssm_matrix);
  pssm->matrix = hashed_pssm_matrix;
  pssm->w = hashed_w;
  pssm->alphsize = hashed_alen;

} // hash_pssm
/******************************************************************************
  * This function returns a copy of the matrix for time t of a transition 
  * matrix array. Returns NUL is no matrix is found matching time t.
  * Caller is responsible for free the returned copy.
******************************************************************************/
MATRIX_T* get_substmatrix_for_time(
  SUBSTMATRIX_ARRAY_T *a, 
  double t
) {

  assert(a != NULL);
  // Create a copy of our matrix
  int i;
  MATRIX_T* src = NULL;
  for (i = 0; i < a->length; i++) {
    if (a->t[i] == t) {
      src = a->substmatrix[i];
      break;
    }
  }
  MATRIX_T* dst = NULL;
  if (src != NULL) {
    int num_rows = get_num_rows(src);
    int num_cols = get_num_cols(src);
    dst = allocate_matrix(num_rows, num_cols);
    copy_matrix(src, dst);
  }

  return dst;
}
示例#15
0
/************************************************************************
 * See .h file for description.
 ************************************************************************/
MHMM_T* allocate_mhmm(ALPH_T* alph, int num_states) {

  /* First allocate the struct itself. */
  MHMM_T* an_mhmm = (MHMM_T*) mm_calloc(1, sizeof(MHMM_T));

  /* Then allocate the states. */
  an_mhmm->states
    = (MHMM_STATE_T *) mm_calloc(num_states, sizeof(MHMM_STATE_T));
  an_mhmm->num_states = num_states;

  /* ... and the transition matrix. */
  an_mhmm->trans = allocate_matrix(num_states, num_states);

  /* Allocate the alphabet. */
  an_mhmm->alph = alph_hold(alph);

  // Allocate the background distribution.
  an_mhmm->background = allocate_array(alph_size_full(an_mhmm->alph));

  // Set other stuff to NULL.
  an_mhmm->description = NULL;
  an_mhmm->motif_file = NULL;
  an_mhmm->sequence_file = NULL;
  an_mhmm->hot_states = NULL;
  an_mhmm->num_hot_states = 0;

  return an_mhmm;
}
示例#16
0
// ----------------------------------------------------------------
// Randomly populates lattice bonds and determines if there exists a path from
// the center site to the site diagonally below and to the right.  One may then
// visually verify the path-detection computation.
static void test_A1_oo_A2(int argc, char** argv)
{
	int   M = 18;
	int   N = 18;
	double p = 0.6;
	int argi;
	int** vbonds;
	int** hbonds;
	int** site_marks;
	int A1[d];
	int A2[d];
	int ctd;

	for (argi = 2; argi < argc; argi++) {
		if (sscanf(argv[argi], "M=%d", &M) == 1)
			;
		else if (sscanf(argv[argi], "N=%d", &N) == 1)
			;
		else if (sscanf(argv[argi], "MN=%d", &M) == 1)
			N = M;
		else if (sscanf(argv[argi], "p=%lf", &p) == 1)
			;
		else
			usage(argv[0], argv[1], 0);
	}
	if ((M < 3) || (N < 3))
		usage(argv[0], argv[1], 1);

	vbonds = allocate_matrix(M, N, 0);
	hbonds = allocate_matrix(M, N, 0);
	site_marks = allocate_matrix(M, N, SITECHAR);
	set_A1_A2(A1, A2, M, N);

	populate_bonds(vbonds, hbonds, M, N, p);
	print_lattice(site_marks, vbonds, hbonds, M, N, A1, A2);
	printf("\n");
	ctd = A1_oo_A2(site_marks, vbonds, hbonds, M, N, A1, A2);
	print_lattice(site_marks, vbonds, hbonds, M, N, A1, A2);
	if (ctd)
		printf("Yes\n");
	else
		printf("No\n");

	free_matrix(vbonds,     M, N);
	free_matrix(hbonds,     M, N);
	free_matrix(site_marks, M, N);
}
示例#17
0
// ----------------------------------------------------------------
// Over a specified number of repetitions: randomly populates lattice bonds,
// and (for p < p_c) computes the size of the largest cluster or (for p > p_c)
// computes the size of the second-largest cluster.  Averages those over the
// number of lattice instantiations.
static void test_mean_finite_C0_size(int argc, char** argv)
{
	int   M = 18;
	int   N = 18;
	double p = 0.6;
	int   reps = 1000;
	int argi;
	int** vbonds;
	int** hbonds;
	int** site_marks;
	int A1[d];
	int A2[d];
	double mean_finite_C0_size;

	for (argi = 2; argi < argc; argi++) {
		if (sscanf(argv[argi], "M=%d", &M) == 1)
			;
		else if (sscanf(argv[argi], "N=%d", &N) == 1)
			;
		else if (sscanf(argv[argi], "MN=%d", &M) == 1)
			N = M;
		else if (sscanf(argv[argi], "p=%lf", &p) == 1)
			;
		else if (sscanf(argv[argi], "reps=%d", &reps) == 1)
			;
		else
			usage(argv[0], argv[1], 1);
	}
	if ((M < 3) || (N < 3))
		usage(argv[0], argv[1], 1);

	vbonds = allocate_matrix(M, N, 0);
	hbonds = allocate_matrix(M, N, 0);
	site_marks = allocate_matrix(M, N, SITECHAR);
	set_A1_A2(A1, A2, M, N);

	mean_finite_C0_size = get_mean_finite_C0_size(
		site_marks, vbonds, hbonds, M, N, p, reps, A1);
	printf("M=%d N=%d p=%.4lf reps=%d <size>=%11.7lf <density>=%11.7lf\n",
		M, N, p, reps, mean_finite_C0_size,
		mean_finite_C0_size/M/N);

	free_matrix(vbonds,     M, N);
	free_matrix(hbonds,     M, N);
	free_matrix(site_marks, M, N);
}
示例#18
0
int main(int argc, char * argv[]) {
    mtype_t * matrix = NULL ;
    int i, MAX_SIZE;
    double avg_time;
    support_init();
    
    MAX_ITERS = 100;
    
    if(argc==2)
        if(atoi(argv[1])>=2)
            MAX_SIZE = atoi(argv[1]);
        else{
        printf("The argument must be an integer and larger than or equal to 2");
        return -1;
        }
    else if(argc>2){
        printf("This program takes one or no arguments, you entered too many.");
        return -1;
    }
    else MAX_SIZE = 1024;
    for ( i = 2; i <= MAX_SIZE; i = i * 2 ) {
        // Allocate a matrix of size : buffer_side x buffer_side
        allocate_matrix (& matrix , i );
        // Clear / Initialize the matrix
        clear_matrix (matrix , i );
        
        avg_time = run_experiment_ij (matrix, 5.0, i);
        printf("The average time to do scalar multiplication ij on a %d x %d matrix is %lf \n", i, i, avg_time);
    }
    printf("------------------------------------------------------------------------------\n");
    for ( i = 2; i <= MAX_SIZE; i = i * 2 ) {
        // Allocate a matrix of size : buffer_side x buffer_side
        allocate_matrix (& matrix , i );
        // Clear / Initialize the matrix
        clear_matrix (matrix , i );
        
        avg_time = run_experiment_ji (matrix, 5.0, i);
        printf("The average time to do scalar multiplication ji on a %d x %d matrix is %lf \n", i, i, avg_time);
    }

    support_finalize();
    
    return 0;
}
示例#19
0
void work(int size)
{
	matrix a, b, result1, result2, result3;

	// Allocate memory for matrices
	allocate_matrix(&a, size);
	allocate_matrix(&b, size);
	allocate_matrix(&result1, size);
	//allocate_matrix(&result2, size) ;
	allocate_matrix(&result3, size) ;
	
	// Initialize matrix elements
	init_matrix(a);
	init_matrix(b);
	init_matrix_zero(result1);


	// Perform sequential matrix multiplication
	mm(a, b, result1);

	//init_matrix_zero(result2);
	//mm_fast(a, b, result2) ;
	//if (compare_matrix(result1, result2)==1)
	//	printf("true") ;
	//free_matrix(result2);

	init_matrix_zero(result3);
	mm_block(a, b, result3) ;



	if (compare_matrix(result1, result3)==1)
		printf("true") ;

	// Print the result1 matrix
//	print_matrix(result1);
	
	free_matrix(a);
	free_matrix(b);

	free_matrix(result1);
	free_matrix(result3);
}
示例#20
0
int main(int argc, char* argv[])
{
    if(argc!=5){
	fprintf(stderr,"USAGE:\n\t%s <input file> <num steps> <output file> <max number of threads> \n", argv[0]);
	fprintf(stderr, "EXAMPLE:\n\t%s random.txt 500 result.txt 8\n", argv[0]);
	exit(EXIT_FAILURE);
    }

    char* init_fname = argv[1];
    sscanf(argv[2],"%d", &steps);
    char* out_fname = argv[3];
    int max_n_threads = atoi(argv[4]);

    long time_vs_threads[max_n_threads+1];
    int tmp[max_n_threads];
    threads_arg=tmp;

    char** init_matrix = dump_matrix_file(init_fname);
    char** zero_matrix = allocate_matrix(width, height);
    matrix_from = allocate_matrix(width, height);
    matrix_to = allocate_matrix(width, height);

    // run by different number of threads, and measure the time
    for(n_threads=1; n_threads <= max_n_threads; n_threads++){
	copy_matrix(init_matrix, matrix_from, width+2, height+2);
	copy_matrix(zero_matrix, matrix_to, width+2, height+2);
	
	time_vs_threads[n_threads] = walltime_of_threads(n_threads);
	printf("walltime when run by %d threads: %d microseconds\n", n_threads, time_vs_threads[n_threads]);
    }

    save_matrix_file(out_fname, matrix_from,width,height);

    char time_fname[20];
    sprintf(time_fname,"time-%d-%d.txt",width,height);
    save_vector(time_fname, time_vs_threads+1, max_n_threads);
   
    free_matrix(zero_matrix,width,height);
    free_matrix(init_matrix,width,height);
    free_matrix(matrix_from,width,height);
    free_matrix(matrix_to,width,height);
    return 0;
}
示例#21
0
// ----------------------------------------------------------------
// For given lattice dimensions and bond density, estimates the probability
// that there exists a path from the center site to the site diagonally below
// and to the right.
static void test_P_A1_oo_A2(int argc, char** argv)
{
	int   M = 18;
	int   N = 18;
	double p = 0.6;
	int   reps = 1000;
	int argi;
	int** vbonds;
	int** hbonds;
	int** site_marks;
	int A1[d];
	int A2[d];
	double P;

	for (argi = 2; argi < argc; argi++) {
		if (sscanf(argv[argi], "M=%d", &M) == 1)
			;
		else if (sscanf(argv[argi], "N=%d", &N) == 1)
			;
		else if (sscanf(argv[argi], "MN=%d", &M) == 1)
			N = M;
		else if (sscanf(argv[argi], "p=%lf", &p) == 1)
			;
		else if (sscanf(argv[argi], "reps=%d", &reps) == 1)
			;
		else
			usage(argv[0], argv[1], 1);
	}
	if ((M < 3) || (N < 3))
		usage(argv[0], argv[1], 1);

	vbonds = allocate_matrix(M, N, 0);
	hbonds = allocate_matrix(M, N, 0);
	site_marks = allocate_matrix(M, N, SITECHAR);
	set_A1_A2(A1, A2, M, N);

	P = P_A1_oo_A2(site_marks, vbonds, hbonds, M, N, p, reps, A1, A2);
	printf("M=%d N=%d p=%.4lf reps=%d PA1ooA2=%11.7lf\n", M, N, p, reps, P);

	free_matrix(vbonds,     M, N);
	free_matrix(hbonds,     M, N);
	free_matrix(site_marks, M, N);
}
示例#22
0
// ----------------------------------------------------------------
// Randomly populates lattice bonds and marks all clusters, plotting the
// results to the screen.  One may then visually verify the path-detection
// computation.
static void test_cluster_numbers(int argc, char** argv)
{
	int   M = 18;
	int   N = 18;
	double p = 0.6;
	int argi;
	int** vbonds;
	int** hbonds;
	int** site_marks;
	int A1[d] = {-1, -1}; // Not used here
	int A2[d] = {-1, -1}; // Not used here

	for (argi = 2; argi < argc; argi++) {
		if (sscanf(argv[argi], "M=%d", &M) == 1)
			;
		else if (sscanf(argv[argi], "N=%d", &N) == 1)
			;
		else if (sscanf(argv[argi], "MN=%d", &M) == 1)
			N = M;
		else if (sscanf(argv[argi], "p=%lf", &p) == 1)
			;
		else
			usage(argv[0], argv[1], 0);
	}
	if ((M < 3) || (N < 3))
		usage(argv[0], argv[1], 1);

	vbonds = allocate_matrix(M, N, 0);
	hbonds = allocate_matrix(M, N, 0);
	site_marks = allocate_matrix(M, N, SITECHAR);

	populate_bonds(vbonds, hbonds, M, N, p);
	print_lattice(site_marks, vbonds, hbonds, M, N, A1, A2);
	printf("\n");

	mark_cluster_numbers(site_marks, vbonds, hbonds, M, N, 0);
	print_lattice_and_cluster_numbers(site_marks, vbonds, hbonds, M, N);
	sanity_check_cluster_numbers(site_marks, vbonds, hbonds, M, N);

	free_matrix(vbonds,     M, N);
	free_matrix(hbonds,     M, N);
	free_matrix(site_marks, M, N);
}
示例#23
0
文件: task1.c 项目: vsynnes/inf3380
void read_matrix_binaryformat (char* filename, double*** matrix, int* num_rows, int* num_cols) {
    int i;
    FILE* fp = fopen (filename,"rb");
    fread (num_rows, sizeof(int), 1, fp);
    fread (num_cols, sizeof(int), 1, fp);

    allocate_matrix(matrix,(*num_rows),(*num_cols));

    /* read in the entire matrix */
    fread ((*matrix)[0], sizeof(double), (*num_rows)*(*num_cols), fp);
    fclose (fp);
}
示例#24
0
文件: motif.c 项目: CPFL/gmeme
/***********************************************************************
 * Copy a motif from one place to another.
 ***********************************************************************/
void copy_motif
  (MOTIF_T* source,
   MOTIF_T* dest)
{
  ALPH_SIZE_T size;
  memset(dest, 0, sizeof(MOTIF_T));
  strcpy(dest->id, source->id);
  strcpy(dest->id2, source->id2);
  dest->length = source->length;
  dest->alph = source->alph;
  dest->flags = source->flags;
  dest->evalue = source->evalue;
  dest->num_sites = source->num_sites;
  dest->complexity = source->complexity;
  dest->trim_left = source->trim_left;
  dest->trim_right = source->trim_right;
  if (source->freqs) {
    size = (dest->flags & MOTIF_HAS_AMBIGS ? ALL_SIZE : ALPH_SIZE);
    // Allocate memory for the matrix.
    dest->freqs = allocate_matrix(dest->length, alph_size(dest->alph, size));
    // Copy the matrix.
    copy_matrix(source->freqs, dest->freqs);
  } else {
    dest->freqs = NULL;
  }
  if (source->scores) {
    // Allocate memory for the matrix. Note that scores don't contain ambigs.
    dest->scores = allocate_matrix(dest->length, alph_size(dest->alph, ALPH_SIZE));
    // Copy the matrix.
    copy_matrix(source->scores, dest->scores);
  } else {
    dest->scores = NULL;
  }
  if (dest->url != NULL) {
    free(dest->url);
    dest->url = NULL;
  }
  copy_string(&(dest->url), source->url);
}
示例#25
0
short evaluate_black(struct board *board)
	{
	short key,oracle;
	register x;
	char **matrix;
	short i,*bpos;

	board->sp=0;

	board->intgp.j=0;
	board->intgp.k=0;

	memset(board->sqused,0xff,(BOARDX+1)*(BOARDY+2)*sizeof(short));

	for(i=0;i<GROUPS;i++)
		{
		if(threat_group(board,i,BLACK))
			{
			board->intgp.tgroups[i]=YES;
			board->intgp.j++;
			}
		else board->intgp.tgroups[i]=NO;

		if(threat_group(board,i,WHITE))
			{
			board->intgp.mygroups[i]=YES;
			board->intgp.k++;
			}
		else board->intgp.mygroups[i]=NO;
		}

	claimeven(board);
	baseinverse(board);
	vertical(board);
	aftereven(board);
	lowinverse(board);
	highinverse(board);
	baseclaim(board);
	before(board);

	if(board->intgp.j==0) return YES;
	if(board->sp==0) return NO;

	matrix=(char **)allocate_matrix(board);
	build_adjacency_matrix(matrix,board);

    oracle=problem_solver(board,matrix,NO,NULL);
	free_matrix(matrix,board);

	return oracle;
	}
int main (void) {
	int vertex_count = 0;

	int ** matrix = load_square (&vertex_count);
	int ** spanning_tree = allocate_matrix (vertex_count, vertex_count, 0);
	bool * vertex_connected = allocate_array (vertex_count, (bool) false);

	if (vertex_count == 0)
		return 1;

	vertex_connected[0] = true;

	for (int pass = 1; pass < vertex_count; pass++) {
		int minimum_edge = -1;
		int min_i = 0;
		int min_j = 0;

		for (int i = 0; i < vertex_count; i++) {
			if (!vertex_connected[i])
				continue;

			for (int j = 0; j < vertex_count; j++) {
				if (matrix[i][j] == 0)
					continue;

				if (vertex_connected[j])
					continue;

				if (minimum_edge == -1 || matrix[i][j] < minimum_edge) {
					minimum_edge = matrix[i][j];
					min_i = i;
					min_j = j;
				}
			}
		}

		spanning_tree[min_i][min_j] = minimum_edge;
		spanning_tree[min_j][min_i] = minimum_edge;

		vertex_connected[min_j] = true;
	}

	printf ("%d\n", graph_weight (matrix, vertex_count) - graph_weight (spanning_tree, vertex_count));

	free_matrix (matrix, vertex_count);
	free_matrix (spanning_tree, vertex_count);
	free (vertex_connected);

	return 0;
}
示例#27
0
matrix_t *multiply_matrix(matrix_t *a, matrix_t *b)
{
	if (a->columns != b->rows) {
		fprintf(stderr, "multiply_matrix(): cannot multiply\n");
		exit(1);
	}

	matrix_t *c = allocate_matrix(a->rows, b->columns);

	stopwatch_start();
	__multiply_matrix(c, a, b);
	stopwatch_stop();

	return c;
}
/******************************************************************************
  * This function sets the transistion prob. matrix for the ith element of 
  a transition matrix array to a copy of the provided matrix.
******************************************************************************/
static void set_substmatrix_matrix(
  SUBSTMATRIX_ARRAY_T *a, 
  int i,
  MATRIX_T* src
) {
  assert(a != NULL);
  assert(i < a->length);
  MATRIX_T* dst = NULL;
  if (src != NULL) {
    int num_rows = get_num_rows(src);
    int num_cols = get_num_rows(src);
    dst = allocate_matrix(num_rows, num_cols);
    copy_matrix(src, dst);
  }
  a->substmatrix[i] = dst;
}
/******************************************************************************
  * This function returns a copy of the ith matrix of a transition matrix array.
  * Caller is responsible for freeing the returned copy.
******************************************************************************/
static MATRIX_T* get_substmatrix_matrix(
  SUBSTMATRIX_ARRAY_T *a, 
  int i
) {
  assert(a != NULL);
  assert(i < a->length);
  // Create a copy of our matrix
  MATRIX_T* src = a->substmatrix[i];
  MATRIX_T* dst = NULL;
  if (src != NULL) {
    int num_rows = get_num_rows(src);
    int num_cols = get_num_cols(src);
    dst = allocate_matrix(num_rows, num_cols);
    copy_matrix(src, dst);
  }
  return dst;
}
// Return the longest common subseuqence of k-mers between the two strings
kLCSResult kLCS(const std::string& a, const std::string& b, const int k)
{
    uint32_t n_kmers_a = a.size() - k + 1;
    uint32_t n_kmers_b = b.size() - k + 1;

    uint32_t n_rows = n_kmers_a + 1;
    uint32_t n_cols = n_kmers_b + 1;

    UInt32Matrix m;
    allocate_matrix(m, n_rows, n_cols);

    // Initialize first row/col to zero
    for(uint32_t row = 0; row < m.n_rows; ++row)
        set(m, row, 0, 0);
    for(uint32_t col = 0; col < m.n_cols; ++col)
        set(m, 0, col, 0);
    
    // Fill matrix
    for(uint32_t row = 1; row < m.n_rows; ++row) {
        for(uint32_t col = 1; col < m.n_cols; ++col) {
    
            const char* ka = a.c_str() + row - 1;
            const char* kb = b.c_str() + col - 1;

            uint32_t score = 0;
            if(strncmp(ka, kb, k) == 0) {
                uint32_t diag = get(m, row - 1, col - 1);
                score = diag + 1;
            } else {
                uint32_t left = get(m, row, col - 1);
                uint32_t up = get(m, row - 1, col);
                score = std::max(left, up);
            }
            set(m, row, col, score);
        }
    }

    kLCSResult result;
    _kLCSBacktrack(m, a, b, k, n_rows - 1, n_cols -  1, result);

    // Backtrack appends from the end to the start, reverse the vector of matches
    std::reverse(result.begin(), result.end());
    free_matrix(m);
    return result;
}