void troca(Indice *a, int i, int j){ Indice temp; temp.peso = get_M_peso(a,i); temp.objeto_linha = get_M_objeto_linha(a,i); temp.objeto_coluna = get_M_objeto_coluna(a,i); set_M(a,i,get_M_peso(a,j),get_M_objeto_linha(a,j),get_M_objeto_coluna(a,j)); set_M(a,j,temp.peso,temp.objeto_linha,temp.objeto_coluna); }
int partition(Indice *a, int l, int r){ int i = l+1, j = r; Indice v = a[l]; while(1) { while(menor(v,a[i]) && i < r) i++; while(maior(a[j],v)) j--; if(i >= j) break; troca(a,i,j); i++; j--; } set_M(a,l,a[j].peso,a[j].objeto_linha,a[j].objeto_coluna); set_M(a,j,v.peso,v.objeto_linha,v.objeto_coluna); return i; }
//Determine values for the orbital elements for the requested JD, then //call KSAsteroid::findGeocentricPosition() bool KSPluto::findGeocentricPosition( const KSNumbers *num, const KSPlanetBase *Earth ) { //First, set the orbital element values according to the current epoch double t = num->julianCenturies(); set_a( a0 + a1*t ); set_e( e0 + e1*t ); set_i( i0.Degrees() + i1*t ); set_N( N0.Degrees() + N1*t ); set_M( M0.Degrees() + M1*t ); set_P( 365.2568984 * pow((a0+a1*t), 1.5) ); //set period from Kepler's 3rd law setJD( num->julianDay() ); return KSAsteroid::findGeocentricPosition( num, Earth ); }
void qam_mod::set_size(int m) { double max_val; double norm_max_val; try { M=m; set_M(M); } catch (...) { throw sci_exception("qam_mod.set_size - bad size", m); } // [-1,1] constelation max_val = (sqrt((double)M)-1.0)/sqrt(2*((double)M-1.0)/3); norm_max_val = 1.0-1.0/sqrt((double)M); scale = norm_max_val/max_val; }
void psk_mod::set_size(int m) { try { M=m; set_M(M); } catch (...) { throw sci_exception("psk_mod.set_size - bad size", m); } // [-1,1] -> QPSK <-> 4PSK // constelation for qpsk R^2 = I^2+Q^2 = 1/2 if (M==2) { scale = 0.5; //BPSK } else { scale = 1/sqrt(2.0); } }
void pam_mod::set_size(int m) { double max_val, norm_max_val; try { M=m; set_M(M); } catch (...) { throw sci_exception("pam_mod.set_size - bad size", m); } max_val = ((double)M-1.0)/sqrt(((double)M*M-1.0)/3); // [-1,1] constelation norm_max_val = 1.0-1.0/(double)M; // - to get +0.5 for 1, and so on scale = -norm_max_val/max_val; }
/*************************** * banded global alignment * ***************************/ uint32_t *ka_global_core(uint8_t *seq1, int len1, uint8_t *seq2, int len2, const ka_param_t *ap, int *_score, int *n_cigar) { int i, j; dpcell_t **dpcell, *q; dpscore_t *curr, *last, *s; int b1, b2, tmp_end; int *mat, end, max = 0; uint8_t type, ctype; uint32_t *cigar = 0; int gap_open, gap_ext, gap_end, b; int *score_matrix, N_MATRIX_ROW; /* initialize some align-related parameters. just for compatibility */ gap_open = ap->gap_open; gap_ext = ap->gap_ext; gap_end = ap->gap_end; b = ap->band_width; score_matrix = ap->matrix; N_MATRIX_ROW = ap->row; *n_cigar = 0; if (len1 == 0 || len2 == 0) return 0; /* calculate b1 and b2 */ if (len1 > len2) { b1 = len1 - len2 + b; b2 = b; } else { b1 = b; b2 = len2 - len1 + b; } if (b1 > len1) b1 = len1; if (b2 > len2) b2 = len2; --seq1; --seq2; /* allocate memory */ end = (b1 + b2 <= len1)? (b1 + b2 + 1) : (len1 + 1); dpcell = (dpcell_t**)malloc(sizeof(dpcell_t*) * (len2 + 1)); for (j = 0; j <= len2; ++j) dpcell[j] = (dpcell_t*)malloc(sizeof(dpcell_t) * end); for (j = b2 + 1; j <= len2; ++j) dpcell[j] -= j - b2; curr = (dpscore_t*)malloc(sizeof(dpscore_t) * (len1 + 1)); last = (dpscore_t*)malloc(sizeof(dpscore_t) * (len1 + 1)); /* set first row */ SET_INF(*curr); curr->M = 0; for (i = 1, s = curr + 1; i < b1; ++i, ++s) { SET_INF(*s); set_end_D(s->D, dpcell[0] + i, s - 1); } s = curr; curr = last; last = s; /* core dynamic programming, part 1 */ tmp_end = (b2 < len2)? b2 : len2 - 1; for (j = 1; j <= tmp_end; ++j) { q = dpcell[j]; s = curr; SET_INF(*s); set_end_I(s->I, q, last); end = (j + b1 <= len1 + 1)? (j + b1 - 1) : len1; mat = score_matrix + seq2[j] * N_MATRIX_ROW; ++s; ++q; for (i = 1; i != end; ++i, ++s, ++q) { set_M(s->M, q, last + i - 1, mat[seq1[i]]); /* this will change s->M ! */ set_I(s->I, q, last + i); set_D(s->D, q, s - 1); } set_M(s->M, q, last + i - 1, mat[seq1[i]]); set_D(s->D, q, s - 1); if (j + b1 - 1 > len1) { /* bug fixed, 040227 */ set_end_I(s->I, q, last + i); } else s->I = MINOR_INF; s = curr; curr = last; last = s; } /* last row for part 1, use set_end_D() instead of set_D() */ if (j == len2 && b2 != len2 - 1) { q = dpcell[j]; s = curr; SET_INF(*s); set_end_I(s->I, q, last); end = (j + b1 <= len1 + 1)? (j + b1 - 1) : len1; mat = score_matrix + seq2[j] * N_MATRIX_ROW; ++s; ++q; for (i = 1; i != end; ++i, ++s, ++q) { set_M(s->M, q, last + i - 1, mat[seq1[i]]); /* this will change s->M ! */ set_I(s->I, q, last + i); set_end_D(s->D, q, s - 1); } set_M(s->M, q, last + i - 1, mat[seq1[i]]); set_end_D(s->D, q, s - 1); if (j + b1 - 1 > len1) { /* bug fixed, 040227 */ set_end_I(s->I, q, last + i); } else s->I = MINOR_INF; s = curr; curr = last; last = s; ++j; } /* core dynamic programming, part 2 */ for (; j <= len2 - b2 + 1; ++j) { SET_INF(curr[j - b2]); mat = score_matrix + seq2[j] * N_MATRIX_ROW; end = j + b1 - 1; for (i = j - b2 + 1, q = dpcell[j] + i, s = curr + i; i != end; ++i, ++s, ++q) { set_M(s->M, q, last + i - 1, mat[seq1[i]]); set_I(s->I, q, last + i); set_D(s->D, q, s - 1); } set_M(s->M, q, last + i - 1, mat[seq1[i]]); set_D(s->D, q, s - 1); s->I = MINOR_INF; s = curr; curr = last; last = s; } /* core dynamic programming, part 3 */ for (; j < len2; ++j) { SET_INF(curr[j - b2]); mat = score_matrix + seq2[j] * N_MATRIX_ROW; for (i = j - b2 + 1, q = dpcell[j] + i, s = curr + i; i < len1; ++i, ++s, ++q) { set_M(s->M, q, last + i - 1, mat[seq1[i]]); set_I(s->I, q, last + i); set_D(s->D, q, s - 1); } set_M(s->M, q, last + len1 - 1, mat[seq1[i]]); set_end_I(s->I, q, last + i); set_D(s->D, q, s - 1); s = curr; curr = last; last = s; } /* last row */ if (j == len2) { SET_INF(curr[j - b2]); mat = score_matrix + seq2[j] * N_MATRIX_ROW; for (i = j - b2 + 1, q = dpcell[j] + i, s = curr + i; i < len1; ++i, ++s, ++q) { set_M(s->M, q, last + i - 1, mat[seq1[i]]); set_I(s->I, q, last + i); set_end_D(s->D, q, s - 1); } set_M(s->M, q, last + len1 - 1, mat[seq1[i]]); set_end_I(s->I, q, last + i); set_end_D(s->D, q, s - 1); s = curr; curr = last; last = s; } *_score = last[len1].M; if (n_cigar) { /* backtrace */ path_t *p, *path = (path_t*)malloc(sizeof(path_t) * (len1 + len2 + 2)); i = len1; j = len2; q = dpcell[j] + i; s = last + len1; max = s->M; type = q->Mt; ctype = FROM_M; if (s->I > max) { max = s->I; type = q->It; ctype = FROM_I; } if (s->D > max) { max = s->D; type = q->Dt; ctype = FROM_D; } p = path; p->ctype = ctype; p->i = i; p->j = j; /* bug fixed 040408 */ ++p; do { switch (ctype) { case FROM_M: --i; --j; break; case FROM_I: --j; break; case FROM_D: --i; break; } q = dpcell[j] + i; ctype = type; switch (type) { case FROM_M: type = q->Mt; break; case FROM_I: type = q->It; break; case FROM_D: type = q->Dt; break; } p->ctype = ctype; p->i = i; p->j = j; ++p; } while (i || j); cigar = ka_path2cigar32(path, p - path - 1, n_cigar); free(path); } /* free memory */ for (j = b2 + 1; j <= len2; ++j) dpcell[j] += j - b2; for (j = 0; j <= len2; ++j) free(dpcell[j]); free(dpcell); free(curr); free(last); return cigar; }
void ler_entrada(char *arquivo, Indice **Ml, Indice **Mh){ FILE *entrada; /* Ponteiro para o arquivo de entrada */ char c, buf[TAMANHO_BUFFER]; int objeto_linha, objeto_coluna; /* v,w respectivamente*/ int parametros = 0; int i = 0, j = 0; int linhas_ml, linhas_mh, valor; /* Leitura da qnt de objetos */ entrada = fopen(arquivo,"r"); if(entrada == NULL) erro(ARQUIVONAOEXISTE); while(!parametros){ c = fgetc(entrada); while(caracter_valido(c)) { buf[i] = c; i++; c = fgetc(entrada); } buf[i] = '\0'; n = atoi(buf); parametros++; i = 0; } tamanho_M = ((n * (1 + n))/2) - (n-1) - 1; /* (Soma de PA de 1 ate n) - enesimo termo */ linhas_ml = 0; linhas_mh = 0; *Ml = malloc(tamanho_M*sizeof(Indice)); *Mh = malloc(tamanho_M*sizeof(Indice)); if(*Ml == NULL) erro(MALLOC_lh); if(*Mh == NULL) erro(MALLOC_lh); /* Leitura de Mh */ objeto_linha = 0; objeto_coluna = objeto_linha + 1; while(linhas_ml < n-1){ reset_buffer(buf); c = fgetc(entrada); while(caracter_valido(c)){ buf[i] = c; i++; c = fgetc(entrada); } valor = atoi(buf); set_M(*Ml,j,valor,objeto_linha,objeto_coluna); j++; objeto_coluna++; if(c == '\n') { objeto_linha = objeto_linha + 1; objeto_coluna = objeto_linha + 1; linhas_ml++; } i = 0; } /* Leitura de Ml */ j = 0; objeto_linha = 0; objeto_coluna = objeto_linha + 1; while(linhas_mh < n-1){ reset_buffer(buf); c = fgetc(entrada); while(caracter_valido(c)){ buf[i] = c; i++; c = fgetc(entrada); } valor = atoi(buf); set_M(*Mh,j,valor,objeto_linha,objeto_coluna); j++; objeto_coluna++; if(c == '\n') { objeto_linha = objeto_linha + 1; objeto_coluna = objeto_linha + 1; linhas_mh++; } i = 0; } fclose(entrada); }