void plot_add_model(plot *p, model_func *f, const mat *x_0, const size_t j, \ const mat *par, void *f_param, const double xmin, \ const double xmax, const size_t npt, const strbuf title,\ const strbuf color) { mat *x,*y,*X; size_t i; double x_i,y_i; x = mat_create(npt,1); y = mat_create(npt,1); X = mat_create_from_mat(x_0); for (i=0;i<npt;i++) { x_i = xmin + (xmax-xmin)*DRATIO(i,npt-1); mat_set(X,j,0,x_i); y_i = f(X,par,f_param); mat_set(x,i,0,x_i); mat_set(y,i,0,y_i); } plot_add_line(p,x,y,title,color); mat_destroy(x); mat_destroy(y); mat_destroy(X); }
//Brief: QR decomposition void qrdec(mat *A, mat* R){ double proj = 0; double norm = 0; // Allocates pointers of type *vec //vec *coli = (vec*)malloc(sizeof(vec)); //vec *colj = (vec*)malloc(sizeof(vec)); vec* coli = vec_new(A->row); vec* colj = vec_new(A->row); for(int i=0; i<A->col; i++){ mat_get_col(coli, A, i); norm = vec_norm(coli); mat_set(R, i, i, norm); vec_scale(coli, 1.0/norm); mat_set_col(A, coli, i); for(int j=i+1; j<A->col; j++){ mat_get_col(colj, A, j); proj = vec_dot(coli,colj); vec_add(colj,coli,-proj); mat_set_col(A, colj, j); mat_set(R, i, j, proj); } } // Free pointers vec_free(coli); vec_free(colj); }
extern inline void mat_exchange(Matrix dest,int i,int j){ int k; double s; for(k=0;k<dest.column;k++){ s=mat_get(dest,i,k); mat_set(dest,i,k,mat_get(dest,j,k)); mat_set(dest,j,k,s); } }
extern inline Matrix mat_I(int m,int n){ Matrix I; int i,j; mat_init(&I,m,n); for(i=0;i<m;i++){ for(j=0;j<m;j++){ if(i==j) mat_set(I,i,j,1.0); else mat_set(I,i,j,0.0); } } return I; }
/* Read substitution scores from specified file and return as a kind of pseudo substitution matrix. All nonspecified elements in matrix will be equal to NEGINFTY, which is to be interpretted as "NA" */ Matrix* read_subst_scores(TreeModel *mod, FILE *F) { Matrix *retval = mat_new(mod->rate_matrix->size, mod->rate_matrix->size); String *line = str_new(STR_MED_LEN), *tuple1, *tuple2; List *l = lst_new_ptr(3); int alph_size = (int)strlen(mod->rate_matrix->states); int *inv_alph = mod->rate_matrix->inv_states; double val; mat_set_all(retval, NEGINFTY); while (str_readline(line, F) != EOF) { str_double_trim(line); if (str_starts_with_charstr(line, "#") || line->length == 0) continue; str_split(line, NULL, l); if (lst_size(l) < 3) { die("ERROR: wrong number of columns in subst. score file.\n"); } tuple1 = lst_get_ptr(l, 0); tuple2 = lst_get_ptr(l, 1); if (str_as_dbl(lst_get_ptr(l, 2), &val) != 0) { die("ERROR: bad value in subst. score file.\n"); } mat_set(retval, tuple_index(tuple1->chars, inv_alph, alph_size), tuple_index(tuple2->chars, inv_alph, alph_size), val); str_free(tuple1); str_free(tuple2); str_free(lst_get_ptr(l, 2)); } lst_free(l); str_free(line); return retval; }
Matrix* unproject_rates(TreeModel *mod_tuples, TreeModel *mod_single) { int dim = mod_tuples->rate_matrix->size; int alph_size = (int)strlen(mod_tuples->rate_matrix->states); char tuple_i[mod_tuples->order+1], tuple_j[mod_tuples->order+1]; int position, i, j; Matrix *retval = mat_new(dim, dim); mat_zero(retval); for (i = 0; i < dim; i++) { get_tuple_str(tuple_i, i, mod_tuples->order+1, mod_tuples->rate_matrix->states); for (j = 0; j < dim; j++) { if (i == j || mm_get(mod_tuples->rate_matrix, i, j) == 0) continue; /* WARNING: we'll ignore any rate matrix elements that have *actually been estimated* to be zero */ get_tuple_str(tuple_j, j, mod_tuples->order+1, mod_tuples->rate_matrix->states); position = mod_tuples->order - (int)(floor(log(abs(i - j))/log(alph_size))); mat_set(retval, i, j, mm_get(mod_single->rate_matrix, mod_single->rate_matrix->inv_states[(int)tuple_i[position]], mod_single->rate_matrix->inv_states[(int)tuple_j[position]])); } } return retval; }
/* Read an amino acid rate matrix in the format used by PAML. Reorder the rows and columns to match 'alph'. Warning: the ordering in the file is assumed to match that used in the files in the PAML distribution (alphabetical order of 3-letter codes), which is also the order of AA_ALPHABET (therefore AA_ALPHABET may not be changed!). Equilibrium frequencies are ignored. */ Matrix *read_paml_matrix(FILE *F, char *alph) { char *paml_alph = "ARNDCQEGHILKMFPSTWYV$"; int size = (int)strlen(paml_alph); Matrix *retval = mat_new(size, size); List *fields = lst_new_ptr(100); String *line = str_new(STR_MED_LEN); int i, j; if (strcmp(alph, paml_alph) != 0) die("ERROR read_paml_matrix (alph (%s) != paml_alph (%s))\n", alph, paml_alph); mat_zero(retval); for (i = 1; i < size-1 && str_readline(line, F) != EOF; ) { /* NOTE: size of matrix allows for stop, but stop not included in file; therefore, only read size-1 lines */ str_double_trim(line); if (line->length == 0) continue; str_split(line, NULL, fields); if (lst_size(fields) != i) { die("ERROR: row %d of matrix must have %d columns.\n", i+1, i); } for (j = 0; j < lst_size(fields); j++) { double val; if (str_as_dbl(lst_get_ptr(fields, j), &val) != 0) { die("ERROR: non-numeric matrix element in subst. matrix ('%s')\n", ((String*)lst_get_ptr(fields, j+1))->chars); } str_free(lst_get_ptr(fields, j)); if (j >= size) die("ERROR read_paml_matrix j (%i) should be < size (%i)\n", j, size); mat_set(retval, i, j, val); mat_set(retval, j, i, val); } i++; } if (i != size - 1) { die("ERROR: too few rows in subst. matrix.\n"); } lst_free(fields); str_free(line); return retval; }
extern inline void mat_cp_rt(Matrix dest,Matrix src){ int i,j; int r_max=MIN(dest.raw,src.raw); int c_max=MIN(dest.column,src.column); for(i=0;i<r_max;i++){ for(j=0;j<c_max;j++){ mat_set(dest,i,dest.column-j-1,mat_get(src,i,src.column-j-1)); } } }
void plot_add_fit_predband(plot *p, const fit_data *d, const size_t ky,\ const mat *x_ex, const size_t kx, \ const rs_sample *par, const double xmin, \ const double xmax, const size_t npt, \ const strbuf color) { mat *x,*yp,*ym,*y_i_err,*X; rs_sample *s_y_i; size_t i; double x_i,yp_i,ym_i; x = mat_create(npt,1); yp = mat_create(npt,1); ym = mat_create(npt,1); y_i_err = mat_create(1,1); s_y_i = rs_sample_create(1,1,rs_sample_get_nsample(par)); X = mat_create_from_mat(x_ex); for (i=0;i<npt;i++) { x_i = xmin + (xmax-xmin)*DRATIO(i,npt-1); mat_set(X,kx,0,x_i); fit_data_model_rs_xeval(s_y_i,d,ky,X,par); rs_sample_var(y_i_err,s_y_i); mat_eqsqrt(y_i_err); yp_i = mat_get(rs_sample_pt_cent_val(s_y_i),0,0) + mat_get(y_i_err,0,0); ym_i = mat_get(rs_sample_pt_cent_val(s_y_i),0,0) - mat_get(y_i_err,0,0); mat_set(yp,i,0,yp_i); mat_set(ym,i,0,ym_i); mat_set(x,i,0,x_i); } plot_add_line(p,x,yp,"",color); plot_add_line(p,x,ym,"",color); mat_destroy(x); mat_destroy(yp); mat_destroy(ym); mat_destroy(y_i_err); rs_sample_destroy(s_y_i); mat_destroy(X); }
int mat_lup_decompose(mat_t out, long *pi, const mat_t mat, const ctx_t ctx) { if (!(mat->m == out->m && mat->n && out->n && mat->m == mat->n)) { printf("ERROR (mat_lup_decompose).\n\n"); abort(); } mat_set(out, mat, ctx); return _mat_lup_decompose(pi, out->rows, out->m, ctx); }
void plot_add_func(plot *p, univar_func *f, void *f_param, const double xmin,\ const double xmax, const size_t npt, const strbuf title, \ const strbuf color) { mat *x,*y; size_t i; double x_i,y_i; x = mat_create(npt,1); y = mat_create(npt,1); for (i=0;i<npt;i++) { x_i = xmin + (xmax-xmin)*DRATIO(i,npt-1); y_i = f(x_i,f_param); mat_set(x,i,0,x_i); mat_set(y,i,0,y_i); } plot_add_line(p,x,y,title,color); mat_destroy(x); mat_destroy(y); }
latan_errno finite_diff(mat *ddat, const mat *dat) { size_t i,j; double ddat_ij; if (nrow(ddat) != nrow(dat) - 2) { LATAN_ERROR("derivative matrix have wrong dimensions",LATAN_EBADLEN); } FOR_VAL(ddat,i,j) { ddat_ij = 0.5*(mat_get(dat,i+2,j) - mat_get(dat,i,j)); mat_set(ddat,i,j,ddat_ij); }
t_mat *mat_translation(t_mat *dest, t_pt3f direction) { if (dest == NULL) dest = mat_new(4, 4); mat_zero(dest); mat_set(dest, 0, 0, 1); mat_set(dest, 1, 1, 1); mat_set(dest, 2, 2, 1); mat_set(dest, 3, 3, 1); mat_set(dest, 3, 0, direction.x); mat_set(dest, 3, 1, direction.y); mat_set(dest, 3, 2, direction.z); return (dest); }
/* Invert square, real, nonsymmetric matrix. Uses LU decomposition (LAPACK routines dgetrf and dgetri). Returns 0 on success, 1 on failure. */ int mat_invert(Matrix *M_inv, Matrix *M) { #ifdef SKIP_LAPACK die("ERROR: LAPACK required for matrix inversion.\n"); #else int i, j; LAPACK_INT info, n = (LAPACK_INT)M->nrows, ipiv[n], lwork=(LAPACK_INT)n; LAPACK_DOUBLE tmp[n][n], work[lwork]; if (!(M->nrows == M->ncols && M_inv->nrows == M_inv->ncols && M->nrows == M_inv->nrows)) die("ERROR mat_invert: bad dimensions\n"); for (i = 0; i < n; i++) for (j = 0; j < n; j++) tmp[i][j] = (LAPACK_DOUBLE)mat_get(M, j, i); #ifdef R_LAPACK F77_CALL(dgetrf)(&n, &n, (LAPACK_DOUBLE*)tmp, &n, ipiv, &info); #else dgetrf_(&n, &n, (LAPACK_DOUBLE*)tmp, &n, ipiv, &info); #endif if (info != 0) { fprintf(stderr, "ERROR: unable to compute LU factorization of matrix (for matrix inversion); dgetrf returned value of %d.\n", (int)info); return 1; } #ifdef R_LAPACK F77_CALL(dgetri)(&n, (LAPACK_DOUBLE*)tmp, &n, ipiv, work, &lwork, &info); #else dgetri_(&n, (LAPACK_DOUBLE*)tmp, &n, ipiv, work, &lwork, &info); #endif if (info != 0) { if (info > 0) fprintf(stderr, "ERROR: matrix is singular -- cannot invert.\n"); else fprintf(stderr, "ERROR: unable to invert matrix. Element %d had an illegal value (according to dgetri).\n", (int)info); return 1; } for (i = 0; i < M->nrows; i++) for (j = 0; j < M->nrows; j++) mat_set(M_inv, i, j, (double)tmp[j][i]); #endif return 0; }
/* multiply two complex matrices whose product is expected to be real */ void zmat_mult_real(Matrix *prod, Zmatrix *m1, Zmatrix *m2) { int i, j, k; if (!(m1->ncols == m2->nrows && m1->nrows == m2->ncols && prod->nrows == m1->nrows && prod->ncols == m2->ncols)) die("ERROR zmat_mult_real: bad dimensions\n"); mat_zero(prod); for (i = 0; i < prod->nrows; i++) { for (j = 0; j < prod->ncols; j++) { Complex z = z_set(0, 0); for (k = 0; k < m1->ncols; k++) z = z_add(z, z_mul(m1->data[i][k], m2->data[k][j])); if (z.y > 1e-6) die("ERROR in zmat_mult_real: product of complex matrices not real.\n"); mat_set(prod, i, j, z.x); } } }
static void get_inertia_factor(const double *inertia, const mat_t *rotmat, mat_t *inertia_fact) { double fact[3]; for (size_t i = 0; i < 3; i++) fact[i] = inertia[i] < EPSILON ? 0.0 : 1.0 / sqrt(inertia[i]); for (size_t i = 0; i < 3; i++) { for (size_t j = 0; j < 3; j++) { double sum = 0.0; for (size_t k = 0; k < 3; k++) sum += fact[k] * mat_get(rotmat, i, k) * mat_get(rotmat, j, k); mat_set(inertia_fact, i, j, sum); } } }
extern inline int mat_mul(Matrix dest,Matrix a,Matrix b){ int i,j,k; double s; if(dest.raw==a.raw&&dest.column==b.column&&a.column==b.raw){ for(i=0;i<dest.raw;i++){ for(j=0;j<dest.column;j++){ s=0; for(k=0;k<a.column;k++){ s+=mat_get(a,i,k)*mat_get(b,k,j); } mat_set(dest,i,j,s); } } return 0; }else{ fprintf(stderr,"Error:乗法を行おうとしている行列の大きさが合っていません\n"); return -1; } }
static void rotate_step(size_t a1, size_t a2, double angle, vec_t *angmom, mat_t *rotmat) { mat_t rot = { 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0 }; double cosa = cos(angle); double sina = sin(angle); mat_set(&rot, a1, a1, cosa); mat_set(&rot, a2, a2, cosa); mat_set(&rot, a1, a2, sina); mat_set(&rot, a2, a1, -sina); *angmom = mat_vec(&rot, angmom); /* transpose */ mat_set(&rot, a1, a2, -sina); mat_set(&rot, a2, a1, sina); *rotmat = mat_mat(rotmat, &rot); }
int main(void) { int i, result; flint_rand_t state; printf("transpose... "); fflush(stdout); _randinit(state); /* Check aliasing */ /* Unmanaged element type (long) */ for (i = 0; i < 100; i++) { long m, n; ctx_t ctx; mat_t A, B, C; m = n_randint(state, 50) + 1; n = m; ctx_init_long(ctx); mat_init(A, m, n, ctx); mat_init(B, m, n, ctx); mat_init(C, m, n, ctx); mat_randtest(A, state, ctx); mat_set(B, A, ctx); mat_transpose(C, B, ctx); mat_transpose(B, B, ctx); result = mat_equal(B, C, ctx); if (!result) { printf("FAIL:\n\n"); printf("Matrix A\n"); mat_print(A, ctx); printf("\n"); printf("Matrix B\n"); mat_print(B, ctx); printf("\n"); printf("Matrix C\n"); mat_print(C, ctx); printf("\n"); } mat_clear(A, ctx); mat_clear(B, ctx); mat_clear(C, ctx); ctx_clear(ctx); } /* Check that (A^t)^t == A */ /* Unmanaged element type (long) */ for (i = 0; i < 100; i++) { long m, n; ctx_t ctx; mat_t A, B, C; m = n_randint(state, 50) + 1; n = n_randint(state, 50) + 1; ctx_init_long(ctx); mat_init(A, m, n, ctx); mat_init(B, n, m, ctx); mat_init(C, m, n, ctx); mat_randtest(A, state, ctx); mat_transpose(B, A, ctx); mat_transpose(C, B, ctx); result = mat_equal(A, C, ctx); if (!result) { printf("FAIL:\n\n"); printf("Matrix A\n"); mat_print(A, ctx); printf("\n"); printf("Matrix B\n"); mat_print(B, ctx); printf("\n"); printf("Matrix C\n"); mat_print(C, ctx); printf("\n"); } mat_clear(A, ctx); mat_clear(B, ctx); mat_clear(C, ctx); ctx_clear(ctx); } _randclear(state); _fmpz_cleanup(); printf("PASS\n"); return EXIT_SUCCESS; }
void sc_light_updateSpotMtx(ENTITY* inLight) { SC_OBJECT* ObjData = (SC_OBJECT*)(inLight.SC_SKILL); float fTexAdj[16] = { 0.5, 0.0, 0.0, 0.0, 0.0,-0.5, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0 }; fTexAdj[12] = 0.5 + ((float)0.5/(float)256); fTexAdj[13] = 0.5 + ((float)0.5/(float)256); VECTOR lightDir; //lightDir = malloc(sizeof(VECTOR)); if(inLight.tilt > 89.9 && inLight.tilt < 90.1 || inLight.tilt < -89.9 && inLight.tilt > -90.1) { vec_set(lightDir, inLight.pan); lightDir.y += 0.2; vec_for_angle(lightDir,lightDir); } else vec_for_angle(lightDir, inLight.pan); sc_skill(inLight, SC_OBJECT_LIGHT_DIR, lightDir); //if(lightDir.y == -90) lightDir.y = 80; //might have to comment this in again (?) /* //if spotlight has a shadowview attached if(ObjData.light.view != NULL) { D3DXMATRIX matView_, matProj_; view_to_matrix(ObjData.light.view, &matView_, &matProj_); mat_multiply(&matView_, &matProj_); mat_multiply(&matView_, fTexAdj); sc_skill(inLight, SC_OBJECT_LIGHT_MATRIX, &matView_); ptr_remove(matView_); ptr_remove(matProj_); return; } */ //create lightViewMatrix D3DXVECTOR3 vEyePt; D3DXVECTOR3 vLookatPt; D3DXVECTOR3 vUpVec; //vEyePt = sys_malloc(sizeof(D3DXVECTOR3)); //vLookatPt = sys_malloc(sizeof(D3DXVECTOR3)); //vUpVec = sys_malloc(sizeof(D3DXVECTOR3)); vEyePt.x = inLight.x; vEyePt.y = inLight.z; vEyePt.z = inLight.y; vLookatPt.x = inLight.x + lightDir.x; vLookatPt.y = inLight.z + lightDir.z; vLookatPt.z = inLight.y + lightDir.y; vUpVec.x = 0; vUpVec.y = 1; vUpVec.z = 0; D3DXMATRIX mtxLightView; //mtxLightView = sys_malloc(sizeof(D3DXMATRIX)); D3DXMatrixLookAtLH( &mtxLightView, vEyePt, vLookatPt, vUpVec); //create lightProjectionMatrix D3DXMATRIX mtxLightProj; D3DXMatrixPerspectiveFovLH(&mtxLightProj, (((float)ObjData.light.arc/(float)180))*SC_PI, (float)1, (float)1, (float)ObjData.light.range ); //pass projection matrix to light D3DXMATRIX mtxLightWorldViewProj; //mtxLightWorldViewProj = sys_malloc(sizeof(D3DXMATRIX)); mat_set(&mtxLightWorldViewProj, &mtxLightView); mat_multiply(&mtxLightWorldViewProj, &mtxLightProj); mat_multiply(&mtxLightWorldViewProj, fTexAdj); sc_skill(inLight, SC_OBJECT_LIGHT_MATRIX, &mtxLightWorldViewProj); /* //this lags like hell sys_free(vEyePt); sys_free(vLookatPt); sys_free(vUpVec); sys_free(mtxLightView); sys_free(mtxLightProj); sys_free(mtxLightWorldViewProj); */ //better ptr_remove(vEyePt); ptr_remove(vLookatPt); ptr_remove(vUpVec); ptr_remove(mtxLightView); ptr_remove(mtxLightProj); ptr_remove(mtxLightWorldViewProj); }
List *pwm_read(const char *filename) { List *result; Matrix *pwm = NULL; int i, currBase, nBases = 0; FILE * F; // char *motifName; String *line = str_new(STR_MED_LEN); List *l = lst_new_ptr(3); List *probabilitiesStr = lst_new_ptr(4); List *probabilitiesDbl; Regex *pssm_re = NULL; Regex *motif_name_re = NULL; int alphabetLength; result = lst_new_ptr(1); //letter-probability matrix: alength= 4 w= 8 nsites= 2 E= 1.5e+004 pssm_re = str_re_new("^letter-probability matrix: alength= ([0-9]+) w= ([0-9]+)"); motif_name_re = str_re_new("^MOTIF[[:space:]]+(.+?)[[:space:]].*"); //open PWM file F = phast_fopen(filename, "r"); currBase = 0; nBases = -1; //For each line in the MEME file while ((str_readline(line, F)) != EOF) { //If line matches Motif name if (str_re_match(line, motif_name_re, l, 1) > 0) { // motifName = copy_charstr(((String*)lst_get_ptr(l, 1))->chars); //printf("motifName=%s\n", motifName); } //If line matches beginning of a probability matrix else if (str_re_match(line, pssm_re, l, 2) > 0) { //Extract the alphabet size & number of bases in matrix if (str_as_int((String*)lst_get_ptr(l, 1), &alphabetLength) != 0) die("ERROR: Unable to parse 'alength=' from MEME file, expected integer, read %s", ((String*)lst_get_ptr(l, 1))->chars); if (str_as_int((String*)lst_get_ptr(l, 2), &nBases) != 0) die("ERROR: Unable to parse 'w=' from MEME file, expected integer, read %s ", ((String*)lst_get_ptr(l, 2))->chars); currBase = 0; if (nBases <= 0) //We must have at least one base in the PWM die("ERROR: No Position Weight Matrices were detected in the provided PWM file"); if (alphabetLength <= 0) //We must have a positive alphabet length die("ERROR: Alphabet lengh specified in PWM file must be greater than zero"); pwm = mat_new(nBases, alphabetLength); mat_set_all(pwm, -1); continue; //If this row contains matrix data } else if (currBase < nBases) { //Parse row of probabilities str_double_trim(line); str_split(line, NULL, probabilitiesStr); probabilitiesDbl = str_list_as_dbl(probabilitiesStr); for (i = 0; i < lst_size(probabilitiesDbl); i++) mat_set(pwm, currBase, i, log(lst_get_dbl(probabilitiesDbl, i))); currBase++; } else if ((currBase == nBases) && (pwm != NULL)) { //Push full matrix lst_push_ptr(result, pwm); pwm = NULL; } } if (currBase == nBases && pwm != NULL) lst_push_ptr(result, pwm); else if (pwm != NULL) die("Premature end of PWM file\n"); str_re_free(motif_name_re); str_re_free(pssm_re); phast_fclose(F); return result; }
Matrix *mm_build_helper(MS *inputMS, int norder, int pseudoCount, int considerReverse) { int alph_size, i, j, ignore, tup_idx, l, alph_idx, seqLen; double sum = 0, val; Vector *freqs = vec_new(int_pow(4, norder+1)); Matrix *mm; char c; if(inputMS == NULL) die("ERROR: GC% group passed to mm_build_helper was null"); if (inputMS->nseqs <= 0) //Must have at least one sequence die("ERROR: At least one sequence must be present to build a markov model"); if (norder < 0)//Order of Markov matrix must be positive die("Order of markov model to create must be zero or greater"); vec_zero(freqs); alph_size = (int)strlen(inputMS->alphabet); //Apply Pseudo-Counts vec_set_all(freqs, pseudoCount); //For each sequence for (j = 0; j < inputMS->nseqs; j++) { seqLen = (int)strlen(inputMS->seqs[j]); //For each site for (i = 0; i < seqLen; i++) { checkInterruptN(i, 10000); ignore = 0; tup_idx = 0; //For each base in the tuple for (l = 0; !ignore && l <= norder; l++) { c = inputMS->seqs[j][i + l]; if ((alph_idx = inputMS->inv_alphabet[(int)c]) == -1) //If we get an unknown base ignore = 1; else tup_idx += alph_idx * int_pow(alph_size, (norder - l)); } if (!ignore) vec_set(freqs, tup_idx, vec_get(freqs, tup_idx) + 1); } } //Take into account reverse complement frequencies if(considerReverse == 1) { //For each sequence for (j = 0; j < inputMS->nseqs; j++) { //For each site for (i = (int)strlen(inputMS->seqs[j]); i >= 0 ; i--) { checkInterruptN(i, 10000); ignore = 0; tup_idx = 0; //For each base in the tuple for (l = 0; !ignore && l <= norder; l++) { c = inputMS->seqs[j][i - l]; switch(c) { case 'A': c = 'T'; break; case 'C': c = 'G'; break; case 'G': c = 'C'; break; case 'T': c = 'A'; break; } if ((alph_idx = inputMS->inv_alphabet[(int)c]) == -1) //If we get an unknown base ignore = 1; else tup_idx += alph_idx * int_pow(alph_size, (norder - l)); } if (!ignore) vec_set(freqs, tup_idx, vec_get(freqs, tup_idx) + 1); } } } mm = mat_new(int_pow(alph_size, norder), alph_size); //Transform count vector into Markov Matrix of order norder for (i = 0; i < freqs->size; i = i + alph_size) { sum = 0; for (j = 0; j < alph_size; j++) //Calculate sum i.e. for AA = count(AAA) + count(AAC) + count(AAG) + count(AAT) sum += vec_get(freqs, i + j); for (j = 0; j < alph_size; j++) { //For each base in alphabet if (sum == 0) //Handle unknown <prefix, base> tuples val = 1.0/alph_size; else val = vec_get(freqs, i + j) / sum; //i.e. for AAT, AA identified by i, T defined by j; #AAT/#AA if((val < 0) || (val > 1)) //Value should be a probability between 0 and 1 die("ERROR: Generating Markov Models, generated probability must be between 0 and 1"); mat_set(mm, i / alph_size, j, val); } } return mm; }
int main(int argc, char *argv[]) { int i,j,k,nn; int imax,jmax,kmax,mimax,mjmax,mkmax,msize[3]; float gosa; double cpu0,cpu1,cpu,flop; // hardcode to S size msize[0]= 64; msize[1]= 64; msize[2]= 128; mimax= msize[0]; mjmax= msize[1]; mkmax= msize[2]; imax= mimax-1; jmax= mjmax-1; kmax= mkmax-1; printf("mimax = %d mjmax = %d mkmax = %d\n",mimax,mjmax,mkmax); printf("imax = %d jmax = %d kmax =%d\n",imax,jmax,kmax); /* * Initializing matrixes */ newMat(&p,1,mimax,mjmax,mkmax); newMat(&bnd,1,mimax,mjmax,mkmax); newMat(&wrk1,1,mimax,mjmax,mkmax); newMat(&wrk2,1,mimax,mjmax,mkmax); newMat(&a,4,mimax,mjmax,mkmax); newMat(&b,3,mimax,mjmax,mkmax); newMat(&c,3,mimax,mjmax,mkmax); mat_set_init(&p); mat_set(&bnd,0,1.0); mat_set(&wrk1,0,0.0); mat_set(&wrk2,0,0.0); mat_set(&a,0,1.0); mat_set(&a,1,1.0); mat_set(&a,2,1.0); mat_set(&a,3,1.0/6.0); mat_set(&b,0,0.0); mat_set(&b,1,0.0); mat_set(&b,2,0.0); mat_set(&c,0,1.0); mat_set(&c,1,1.0); mat_set(&c,2,1.0); /* * Start measuring */ nn= 64; gosa = jacobi(nn,&a,&b,&c,&p,&bnd,&wrk1,&wrk2); printf(" Loop executed for %d times\n",nn); printf(" Gosa : %e \n",gosa); /* * Matrix free */ clearMat(&p); clearMat(&bnd); clearMat(&wrk1); clearMat(&wrk2); clearMat(&a); clearMat(&b); clearMat(&c); return (0); }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { double M; double log_thr; int Xs,D,Ys,i,j,ep=0; int maxentep; int pxy_size; double mx; Matrix phi,psi,phi_tr,psi_tr,phi_exp_d,psi_exp_d,psi_exp_m,phi_exp_m,grad_phi,grad_psi; double *px0,*py0,*px,*py,*pa,*pb,*grad_a,*grad_b; double Z,loglik; int xi,yi,di; int b_cond; /* Says if the model is conditioned on x, or joint */ double *p0_i,*p0_j,*p0_val; int Np0; MatFromMLab(prhs[0],&phi); /* Size NX,D */ MatFromMLab(prhs[1],&psi); /* Size NX,D */ MatFromMLab(prhs[2],&phi_tr); /* Size D,NX */ MatFromMLab(prhs[3],&psi_tr); /* Size D,NY */ px0 = mxGetPr(prhs[4]); py0 = mxGetPr(prhs[5]); MatFromMLab(prhs[6],&phi_exp_d); MatFromMLab(prhs[7],&psi_exp_d); pa = mxGetPr(prhs[8]); pb = mxGetPr(prhs[9]); b_cond = *mxGetPr(prhs[10]); p0_i = mxGetPr(prhs[11]); p0_j = mxGetPr(prhs[12]); p0_val = mxGetPr(prhs[13]); Xs = mxGetM(prhs[0]); D = mxGetN(prhs[0]); Ys = mxGetM(prhs[1]); Np0 = mxGetNumberOfElements(prhs[13]); pxy_size = Xs*Ys; plhs[0] = mxCreateDoubleMatrix(D,Xs,mxREAL); plhs[1] = mxCreateDoubleMatrix(D,Ys,mxREAL); plhs[2] = mxCreateDoubleMatrix(1,Xs,mxREAL); plhs[3] = mxCreateDoubleMatrix(1,Ys,mxREAL); plhs[4] = mxCreateDoubleMatrix(1,1,mxREAL); grad_phi.dat = mxGetPr(plhs[0]); grad_psi.dat = mxGetPr(plhs[1]); grad_phi.nx = D; grad_phi.ny = Xs; grad_phi.nall = Xs*D; grad_psi.nx = D; grad_psi.ny = Ys; grad_psi.nall = Ys*D; grad_a = mxGetPr(plhs[2]); grad_b = mxGetPr(plhs[3]); AllocMat(D,Ys,&phi_exp_m); AllocMat(D,Xs,&psi_exp_m); px = malloc(Xs*sizeof(double)); py = malloc(Ys*sizeof(double)); if (px==NULL || py==NULL) printf("Error in allocation\n"); if (!b_cond) mat_logemb_model(phi_tr, psi_tr, pa, pb, phi_exp_m, psi_exp_m, px, py, p0_i, p0_j, p0_val, Np0, mxGetPr(plhs[4]),b_cond) ; else { /* Run logemb model one time to get the x partition function in px */ mat_logemb_model(phi_tr, psi_tr, pa, pb, phi_exp_m, psi_exp_m, px, py, p0_i, p0_j, p0_val, Np0, mxGetPr(plhs[4]),b_cond) ; /* Create new x factor */ for (xi=0; xi<Xs; xi++) grad_a[xi] = pa[xi]-log(px[xi])+log(px0[xi]); mat_logemb_model(phi_tr, psi_tr, grad_a, pb, phi_exp_m, psi_exp_m, px, py, p0_i, p0_j, p0_val, Np0, mxGetPr(plhs[4]),0) ; } /* Matlab line: diag(px)*phi-pxy*psi - (diag(px0)*phi-pxy0*psi); /* equivalent to diag(px-px0)*phi+psi_expect_d - psi_expect_m */ vec_subtract(px,px0,Xs); mat_set(grad_phi,phi_tr); /* printf("\n Phi1 Max=%g MaxPx=%g\n",mat_max_abs(grad_phi.dat,grad_phi.nall),mat_max_abs(px,Xs)); */ mat_mul_cols(grad_phi,px); /* printf("\n Phi2 Max=%g MaxPx=%g\n",mat_max_abs(grad_phi.dat,grad_phi.nall),mat_max_abs(px,Xs)); */ mat_subtract(psi_exp_d.dat,psi_exp_m.dat,psi_exp_d.nall); /* printf("\n Phi3 Max=%g\n",mat_max_abs(grad_phi.dat,grad_phi.nall)); */ mat_add(grad_phi.dat,psi_exp_d.dat,psi_exp_d.nall); /* printf("\n Phi4 Max=%g\n",mat_max_abs(grad_phi.dat,grad_phi.nall)); */ vec_subtract(py,py0,Ys); mat_set(grad_psi,psi_tr); mat_mul_cols(grad_psi,py); mat_subtract(phi_exp_d.dat,phi_exp_m.dat,phi_exp_d.nall); mat_add(grad_psi.dat,phi_exp_d.dat,phi_exp_d.nall); /* Generate gradient for a and b. Remember px already has px subtracted. Just need to flip sign */ memcpy(grad_a,px,Xs*sizeof(double)); vec_mul_scalar(grad_a,-1,Xs); memcpy(grad_b,py,Ys*sizeof(double)); vec_mul_scalar(grad_b,-1,Ys); free(psi_exp_m.dat); free(phi_exp_m.dat); free(px); free(py); }
int main(int argc, char* argv[]) { /* parsing arguments */ /********************************************/ double latspac_nu; qcd_options *opt; strbuf name[3],manf_name,unit,sink[3],source[3]; size_t binsize,nboot,propdim[2],nt; char *cpt1,*cpt2; corr_no lastc; fit_model *fm_pt; opt = qcd_arg_parse(argc,argv,A_PLOT|A_SAVE_RS|A_LOAD_RG|A_PROP_NAME\ |A_PROP_LOAD|A_LATSPAC|A_QCOMP|A_FIT,2,0); cpt1 = strtok(opt->ss,"/"); printf("%s\n",cpt1); if (cpt1) { cpt2 = strchr(cpt1,':'); if (cpt2 == NULL) { fprintf(stderr,"error: sink/source option %s is invalid\n",\ cpt1); exit(EXIT_FAILURE); } strbufcpy(source[PP],cpt2+1); *cpt2 = '\0'; strbufcpy(sink[PP],cpt1); } else { fprintf(stderr,"error: sink/source option %s is invalid\n",\ opt->ss); exit(EXIT_FAILURE); } cpt1 = strtok(NULL,"/"); if (cpt1) { cpt2 = strchr(cpt1,':'); if (cpt2 == NULL) { fprintf(stderr,"error: sink/source option %s is invalid\n",\ cpt1); exit(EXIT_FAILURE); } strbufcpy(source[AP],cpt2+1); *cpt2 = '\0'; strbufcpy(sink[AP],cpt1); } else { strbufcpy(sink[AP],sink[PP]); strbufcpy(source[AP],source[PP]); } cpt1 = strtok(NULL,"/"); if (cpt1) { cpt2 = strchr(cpt1,':'); if (cpt2 == NULL) { fprintf(stderr,"error: sink/source option %s is invalid\n",\ cpt1); exit(EXIT_FAILURE); } strbufcpy(source[AA],cpt2+1); *cpt2 = '\0'; strbufcpy(sink[AA],cpt1); } else { strbufcpy(sink[AA],sink[PP]); strbufcpy(source[AA],source[PP]); } cpt1 = strtok(opt->channel[0],"/"); if (cpt1) { sprintf(name[PP],"%s_%s_%s_%s",cpt1,opt->quark[0],sink[PP],source[PP]); } else { fprintf(stderr,"error: channel option %s is invalid\n",\ opt->channel[0]); exit(EXIT_FAILURE); } cpt1 = strtok(NULL,"/"); if (cpt1) { sprintf(name[AP],"%s_%s_%s_%s",cpt1,opt->quark[0],sink[AP],source[AP]); } else { fprintf(stderr,"error: channel option %s is invalid\n",\ opt->channel[0]); exit(EXIT_FAILURE); } cpt1 = strtok(NULL,"/"); if (cpt1) { sprintf(name[AA],"%s_%s_%s_%s",cpt1,opt->quark[0],sink[AA],source[AA]); lastc = AA; fm_pt = &fm_pseudosc3; } else { lastc = AP; fm_pt = &fm_pseudosc2; } strbufcpy(manf_name,opt->manf_name); binsize = opt->binsize; nboot = opt->nboot; latspac_nu = opt->latspac_nu; if (opt->have_latspac) { strbufcpy(unit," (MeV)"); } else { strbufcpy(unit,""); } latan_set_verb(opt->latan_verb); minimizer_set_alg(opt->minimizer); mat_ar_loadbin(NULL,propdim,manf_name,name[PP],1); nt = propdim[0]; io_set_fmt(opt->latan_fmt); io_init(); /* loading datas */ /********************************************/ size_t ndat,nbdat; mat **prop[3]; corr_no c; ndat = (size_t)get_nfile(manf_name); nbdat = ndat/binsize + ((ndat%binsize == 0) ? 0 : 1); for (c=PP;c<=lastc;c++) { prop[c] = mat_ar_create(nbdat,propdim[0],propdim[1]); qcd_printf(opt,"-- loading %s datas from %s...\n",name[c],manf_name); mat_ar_loadbin(prop[c],NULL,manf_name,name[c],binsize); } /* propagator */ /********************************************/ rs_sample *s_mprop[3]; mat *mprop[3],*sigmprop[3]; for (c=PP;c<=lastc;c++) { s_mprop[c] = rs_sample_create(propdim[0],propdim[1],nboot); sigmprop[c] = mat_create(propdim[0],propdim[1]); qcd_printf(opt,"-- resampling %s mean propagator...\n",name[c]); randgen_set_state(opt->state); resample(s_mprop[c],prop[c],nbdat,&rs_mean,BOOT,NULL); mprop[c] = rs_sample_pt_cent_val(s_mprop[c]); rs_sample_varp(sigmprop[c],s_mprop[c]); mat_eqsqrt(sigmprop[c]); } /* effective mass */ /********************************************/ rs_sample *s_effmass[3]; mat *tem,*em[3],*sigem[3]; size_t emdim[2]; get_effmass_size(emdim,mprop[PP],1,EM_ACOSH); tem = mat_create(emdim[0],1); for (c=PP;c<=lastc;c++) { s_effmass[c] = rs_sample_create(emdim[0],emdim[1],nboot); sigem[c] = mat_create(emdim[0],emdim[1]); qcd_printf(opt,"-- resampling %s effective mass...\n",name[c]); rs_sample_effmass(s_effmass[c],tem,s_mprop[c],1,EM_ACOSH); em[c] = rs_sample_pt_cent_val(s_effmass[c]); rs_sample_varp(sigem[c],s_effmass[c]); mat_eqsqrt(sigem[c]); } /* fit mass */ /********************************************/ fit_data *d; rs_sample *s_fit; mat *fit,*limit,*sigfit,*scanres_t,*scanres_chi2,*scanres_mass,\ *scanres_masserr; size_t npar,nti,tibeg,range[2],ta; size_t i,j; strbuf buf,range_info,latan_path; double pref_i,mass_i; d = fit_data_create(nt,1,(size_t)(lastc+1)); tibeg = (size_t)(opt->range[0][0]); range[0] = 0; range[1] = 0; npar = fit_model_get_npar(fm_pt,&nt); s_fit = rs_sample_create(npar,1,nboot); fit = rs_sample_pt_cent_val(s_fit); limit = mat_create(npar,2); sigfit = mat_create(npar,1); /** print operation **/ if (!opt->do_range_scan) { qcd_printf(opt,"-- fitting and resampling...\n"); } else { qcd_printf(opt,"-- scanning ranges [ti,%u] from ti= %u\n",\ opt->range[0][1],opt->range[0][0]); opt->nmanrange = 1; } /** check ranges **/ strbufcpy(range_info,""); qcd_printf(opt,"%-20s: ","corrected range(s)"); fit_data_fit_all_points(d,false); for (i=0;i<opt->nmanrange;i++) { sprintf(buf,"_%u_%u",opt->range[i][0],opt->range[i][1]); strbufcat(range_info,buf); range[0] = (size_t)(opt->range[i][0]); range[1] = (size_t)(opt->range[i][1]); correct_range(range,mprop[PP],sigmprop[PP],opt); fit_data_fit_range(d,range[0],range[1],true); } qcd_printf(opt,"\n"); nti = MAX(range[1] - 1 - tibeg,1); scanres_t = mat_create(nti,1); scanres_chi2 = mat_create(nti,1); scanres_mass = mat_create(nti,1); scanres_masserr = mat_create(nti,1); /** set model **/ fit_data_set_model(d,fm_pt,&nt); /** set correlation filter **/ if (opt->corr == NO_COR) { for (i=0;i<nt;i++) for (j=0;j<nt;j++) { if (i != j) { fit_data_set_data_cor(d,i,j,false); } } } /** set initial parameter values **/ ta = nt/8-(size_t)(mat_get(tem,0,0)); mass_i = mat_get(em[PP],ta,0); if (latan_isnan(mass_i)) { mass_i = 0.3; } mat_set(fit,0,0,mass_i); pref_i = mat_get(mprop[PP],ta,0)/(exp(-mass_i*ta)+exp(-mass_i*(nt-ta))); if (latan_isnan(pref_i)) { pref_i = 1.0; } mat_set(fit,1,0,sqrt(pref_i)); mat_set(fit,2,0,sqrt(pref_i)); qcd_printf(opt,"%-22smass= %e -- prefactor_0= %e\n","initial parameters: ",\ mat_get(fit,0,0),pref_i); /** set parameter limits **/ mat_cst(limit,latan_nan()); mat_set(limit,0,0,0.0); mat_set(limit,1,0,0.0); mat_set(limit,2,0,0.0); /** positive AP correlator **/ rs_sample_eqabs(s_mprop[AP]); /** set x **/ for (i=0;i<nt;i++) { fit_data_set_x(d,i,0,(double)(i)-opt->tshift); } /** regular correlator fit... **/ if (!opt->do_range_scan) { latan_set_warn(false); rs_data_fit(s_fit,limit,NULL,s_mprop,d,NO_COR,NULL); latan_set_warn(true); rs_data_fit(s_fit,limit,NULL,s_mprop,d,opt->corr,NULL); rs_sample_varp(sigfit,s_fit); mat_eqsqrt(sigfit); if (fit_data_get_chi2pdof(d) > 2.0) { fprintf(stderr,"warning: bad final fit (chi^2/dof= %.2e)\n",\ fit_data_get_chi2pdof(d)); } qcd_printf(opt,"-- results:\n"); qcd_printf(opt,"%-10s= %.8f +/- %.8e %s\n","mass",\ mat_get(fit,0,0)/latspac_nu, \ mat_get(sigfit,0,0)/latspac_nu,unit); qcd_printf(opt,"%-10s= %.8f +/- %.8e %s\n","decay",\ mat_get(fit,1,0)/latspac_nu, \ mat_get(sigfit,1,0)/latspac_nu,unit); qcd_printf(opt,"%-10s= %.8f +/- %.8e %s\n","norm",\ mat_get(fit,2,0)/latspac_nu, \ mat_get(sigfit,2,0)/latspac_nu,unit); qcd_printf(opt,"%-10s= %d\n","dof",fit_data_get_dof(d)); qcd_printf(opt,"%-10s= %e\n","chi^2/dof",fit_data_get_chi2pdof(d)); if (opt->do_save_rs_sample) { sprintf(latan_path,"%s_pseudosc_fit%s_%s.boot:%s_pseudosc_fit%s_%s",\ opt->quark[0],range_info,manf_name,opt->quark[0],\ range_info,manf_name); rs_sample_save_subsamp(latan_path,'w',s_fit,0,0,1,0); } } /** ...or fit range scanning **/ else { qcd_printf(opt,"\n%-5s %-12s a*M_%-8s %-12s","ti/a","chi^2/dof",\ opt->quark[0],"error"); for (i=tibeg;i<range[1]-1;i++) { latan_set_warn(false); rs_data_fit(s_fit,limit,NULL,s_mprop,d,NO_COR,NULL); latan_set_warn(true); rs_data_fit(s_fit,limit,NULL,s_mprop,d,opt->corr,NULL); rs_sample_varp(sigfit,s_fit); mat_eqsqrt(sigfit); mat_set(scanres_t,i-tibeg,0,(double)(i)); mat_set(scanres_chi2,i-tibeg,0,fit_data_get_chi2pdof(d)); mat_set(scanres_mass,i-tibeg,0,mat_get(fit,0,0)); mat_set(scanres_masserr,i-tibeg,0,mat_get(sigfit,0,0)); qcd_printf(opt,"\n% -4d % -.5e % -.5e % -.5e",(int)(i),\ fit_data_get_chi2pdof(d),mat_get(fit,0,0), \ mat_get(sigfit,0,0)); fit_data_fit_point(d,i,false); } qcd_printf(opt,"\n\n"); } /* plot */ /********************************************/ if (opt->do_plot) { mat *mbuf,*em_i,*sigem_i,*par,*ft[3],*comp[3]; plot *p; strbuf key,dirname,color; size_t maxt,t,npoint; double dmaxt,nmass; mbuf = mat_create(1,1); em_i = mat_create(nrow(em[PP]),1); sigem_i = mat_create(nrow(em[PP]),1); par = mat_create(2,1); if (!opt->do_range_scan) { maxt = nt; dmaxt = (double)maxt; npoint = fit_data_fit_point_num(d); /** chi^2 plot **/ p = plot_create(); i = 0; for (c=PP;c<=lastc;c++) { ft[c] = mat_create(npoint,1); comp[c] = mat_create(npoint,1); for (t=0;t<nt;t++) { if (fit_data_is_fit_point(d,t)) { mat_set(ft[c],i%npoint,0,(double)(t)+0.33*(double)(c)); mat_set(comp[c],i%npoint,0,mat_get(d->chi2_comp,i,0)); i++; } } } plot_set_scale_manual(p,-1.0,dmaxt,-5.0,5.0); plot_add_plot(p,"0.0 lt -1 lc rgb 'black' notitle",""); plot_add_plot(p,"1.0 lt -1 lc rgb 'black' notitle",""); plot_add_plot(p,"-1.0 lt -1 lc rgb 'black' notitle",""); plot_add_plot(p,"2.0 lt -1 lc rgb 'dark-gray' notitle",""); plot_add_plot(p,"-2.0 lt -1 lc rgb 'dark-gray' notitle",""); plot_add_plot(p,"3.0 lt -1 lc rgb 'gray' notitle",""); plot_add_plot(p,"-3.0 lt -1 lc rgb 'gray' notitle",""); plot_add_plot(p,"4.0 lt -1 lc rgb 'light-gray' notitle",""); plot_add_plot(p,"-4.0 lt -1 lc rgb 'light-gray' notitle",""); plot_set_ylabel(p,"standard deviations"); for (c=PP;c<=lastc;c++) { sprintf(color,"%d",(int)(c)+1); plot_add_points(p,ft[c],comp[c],c_name[c],color,"impulses"); } plot_disp(p); if (opt->do_save_plot) { sprintf(dirname,"%s_dev",opt->save_plot_dir); plot_save(dirname,p); } plot_destroy(p); for (c=PP;c<=lastc;c++) { /** propagator plot **/ p = plot_create(); fit_data_fit_all_points(d,true); plot_set_scale_ylog(p); plot_set_scale_xmanual(p,0,dmaxt); sprintf(key,"%s %s propagator",opt->quark[0],c_name[c]); mat_eqabs(mprop[c]); plot_add_fit(p,d,c,mbuf,0,fit,0,dmaxt,1000,false,\ PF_FIT|PF_DATA,key,"","rgb 'red'","rgb 'red'"); plot_disp(p); if (opt->do_save_plot) { sprintf(dirname,"%s_prop_%s",opt->save_plot_dir,c_name[c]); plot_save(dirname,p); } plot_destroy(p); /** effective mass plot **/ p = plot_create(); mat_eqmuls(em[c],1.0/latspac_nu); mat_eqmuls(sigem[c],1.0/latspac_nu); nmass = mat_get(fit,0,0)/latspac_nu; plot_add_hlineerr(p,nmass, mat_get(sigfit,0,0)/latspac_nu,\ "rgb 'red'"); sprintf(key,"%s %s effective mass",opt->quark[0],c_name[c]); plot_add_dat(p,tem,em[c],NULL,sigem[c],key,"rgb 'blue'"); plot_disp(p); if (opt->do_save_plot) { sprintf(dirname,"%s_em_%s",opt->save_plot_dir,c_name[c]); plot_save(dirname,p); } plot_destroy(p); } } else { /* chi^2 plot */ p = plot_create(); plot_set_scale_manual(p,0,(double)(nt/2),0,5.0); plot_add_hline(p,1.0,"rgb 'black'"); plot_add_dat(p,scanres_t,scanres_chi2,NULL,NULL,"chi^2/dof",\ "rgb 'blue'"); plot_disp(p); if (opt->do_save_plot) { sprintf(dirname,"%s_chi2",opt->save_plot_dir); plot_save(dirname,p); } plot_destroy(p); /* mass plot */ p = plot_create(); plot_set_scale_xmanual(p,0,(double)(nt/2)); sprintf(key,"a*M_%s",opt->quark[0]); plot_add_dat(p,scanres_t,scanres_mass,NULL,scanres_masserr,key,\ "rgb 'red'"); plot_disp(p); if (opt->do_save_plot) { sprintf(dirname,"%s_mass",opt->save_plot_dir); plot_save(dirname,p); } plot_destroy(p); } mat_destroy(em_i); mat_destroy(sigem_i); mat_destroy(mbuf); mat_destroy(par); for (c=PP;c<=lastc;c++) { mat_destroy(ft[c]); mat_destroy(comp[c]); } } /* desallocation */ /********************************************/ free(opt); io_finish(); mat_ar_destroy(prop[0],nbdat); mat_ar_destroy(prop[1],nbdat); for (c=PP;c<=lastc;c++) { rs_sample_destroy(s_mprop[c]); mat_destroy(sigmprop[c]); rs_sample_destroy(s_effmass[c]); mat_destroy(sigem[c]); } mat_destroy(tem); fit_data_destroy(d); rs_sample_destroy(s_fit); mat_destroy(limit); mat_destroy(sigfit); mat_destroy(scanres_t); mat_destroy(scanres_chi2); mat_destroy(scanres_mass); mat_destroy(scanres_masserr); return EXIT_SUCCESS; }
void plot_fit(const rs_sample *s_fit, fit_data *d, fit_param *param, const plot_flag f) { plot *p[N_EX_VAR]; double *xb[N_EX_VAR] = {NULL,NULL,NULL,NULL,NULL,NULL,NULL}; double x_range[N_EX_VAR][2],b_int[2],dbind,a; size_t bind,vind,eind,k,phy_ind,s; strbuf color,gtitle,title,xlabel,ylabel; mat *phy_pt,*x_k,*fit,*cordat,**vol_av_corr,*yerrtmp; ens *ept; phy_pt = mat_create(N_EX_VAR,1); x_k = mat_create(param->nens,1); cordat = mat_create(param->nens,1); MALLOC(vol_av_corr,mat **,param->nbeta); for (bind=0;bind<param->nbeta;bind++) { vol_av_corr[bind] = mat_create(param->nvol[bind],1); } for (k=0;k<N_EX_VAR;k++) { p[k] = plot_create(); } param->scale_model = 1; fit = rs_sample_pt_cent_val(s_fit); if (IS_AN(param,AN_PHYPT)&&IS_AN(param,AN_SCALE)) { phy_ind = 1; s = fm_scaleset_taylor_npar(param); } else { phy_ind = 0; s = 0; } for (k=0;k<N_EX_VAR;k++) { if (k == i_vind) { fit_data_get_x_k(x_k,d,i_Linv); } else { fit_data_get_x_k(x_k,d,k); } if ((k == i_a)||(k == i_ud)||(k == i_Linv)||(k == i_alpha)\ ||(k == i_vind)) { x_range[k][0] = 0.0; } else { x_range[k][0] = mat_get_min(x_k)-0.15*fabs(mat_get_min(x_k)); } x_range[k][1] = mat_get_max(x_k)+0.15*fabs(mat_get_min(x_k)); plot_set_scale_xmanual(p[k],x_range[k][0],x_range[k][1]); } if (f == Q) { sprintf(gtitle,"quantity: %s -- scale: %s -- datasets: %s -- ensembles: %s",\ param->q_name,param->scale_part,param->dataset_cat,param->manifest); mat_set(phy_pt,i_ud,0,SQ(param->M_ud)); mat_set(phy_pt,i_s,0,SQ(param->M_s)); mat_set(phy_pt,i_umd,0,param->M_umd_val); mat_set(phy_pt,i_alpha,0,param->alpha); mat_set(phy_pt,i_bind,0,0.0); mat_set(phy_pt,i_vind,0,0.0); mat_set(phy_pt,i_a,0,0.0); mat_set(phy_pt,i_Linv,0,0.0); mat_set(phy_pt,i_fvM,0,param->qed_fvol_mass); /* regular plots */ PLOT_ADD_FIT(PF_FIT,i_ud,phy_ind,"","rgb 'black'"); PLOT_ADD_PB(i_ud,phy_ind,"rgb 'black'"); PLOT_ADD_FIT(PF_FIT,i_s,phy_ind,"","rgb 'black'"); PLOT_ADD_PB(i_s,phy_ind,"rgb 'black'"); PLOT_ADD_FIT(PF_FIT,i_umd,phy_ind,"","rgb 'black'"); PLOT_ADD_PB(i_umd,phy_ind,"rgb 'black'"); PLOT_ADD_FIT(PF_FIT,i_alpha,phy_ind,"","rgb 'black'"); PLOT_ADD_PB(i_alpha,phy_ind,"rgb 'black'"); PLOT_ADD_FIT(PF_FIT,i_a,phy_ind,"","rgb 'black'"); PLOT_ADD_PB(i_a,phy_ind,"rgb 'black'"); PLOT_ADD_FIT(PF_FIT,i_Linv,phy_ind,"","rgb 'black'"); PLOT_ADD_PB(i_Linv,phy_ind,"rgb 'black'"); for (bind=0;bind<param->nbeta;bind++) { dbind = (double)(bind); b_int[0] = dbind - 0.1; b_int[1] = dbind + 0.1; xb[i_bind] = b_int; fit_data_fit_region(d,xb); sprintf(color,"%d",1+(int)bind); sprintf(title,"beta = %s",param->beta[bind]); PLOT_ADD_FIT(PF_DATA,i_ud,phy_ind,title,color); PLOT_ADD_FIT(PF_DATA,i_s,phy_ind,title,color); PLOT_ADD_FIT(PF_DATA,i_umd,phy_ind,title,color); PLOT_ADD_FIT(PF_DATA,i_alpha,phy_ind,title,color); PLOT_ADD_FIT(PF_DATA,i_a,phy_ind,title,color); PLOT_ADD_FIT(PF_DATA,i_Linv,phy_ind,title,color); fit_data_fit_all_points(d,true); } /* volume averages plot */ plot_add_fit(p[i_vind],d,phy_ind,phy_pt,i_Linv,fit,x_range[i_Linv][0],\ x_range[i_Linv][1],MOD_PLOT_NPT,true,PF_FIT,"","", \ "rgb 'black'","rgb 'black'"); plot_add_fit_predband(p[i_vind],d,phy_ind,phy_pt,i_Linv,s_fit,\ x_range[i_Linv][0],x_range[i_Linv][1], \ MOD_PLOT_NPT/4,"rgb 'black'"); fit_partresidual(cordat,d,phy_ind,phy_pt,i_Linv,fit); for(bind=0;bind<param->nbeta;bind++) { mat_zero(vol_av_corr[bind]); } for(eind=0;eind<param->nens;eind++) { ept = param->point + eind; bind = (size_t)ind_beta(ept->beta,param); vind = (size_t)ind_volume((unsigned int)ept->L,(int)bind,param); mat_inc(vol_av_corr[bind],vind,0,mat_get(cordat,eind,0)); } for (bind=0;bind<param->nbeta;bind++) for (vind=0;vind<param->nvol[bind];vind++) { mat_set(vol_av_corr[bind],vind,0, \ mat_get(vol_av_corr[bind],vind,0) \ /((double)(param->nenspvol[bind][vind]))); } for(bind=0;bind<param->nbeta;bind++) { yerrtmp = mat_create(param->nvol[bind],1); rs_sample_varp(yerrtmp,param->s_vol_av[bind]); mat_eqsqrt(yerrtmp); sprintf(color,"%d",1+(int)bind); sprintf(title,"beta = %s",param->beta[bind]); plot_add_dat_yerr(p[i_vind], \ rs_sample_pt_cent_val(param->s_vol_Linv[bind]),\ vol_av_corr[bind],yerrtmp,title,color); mat_destroy(yerrtmp); } /* display plots */ switch (param->q_dim) { case 0: strbufcpy(ylabel,param->q_name); break; case 1: sprintf(ylabel,"%s (MeV)",param->q_name); break; default: sprintf(ylabel,"%s^%d (MeV^%d)",param->q_name,param->q_dim,\ param->q_dim); break; } sprintf(xlabel,"M_%s^2 (MeV^2)",param->ud_name); PLOT_ADD_EX(i_ud,s); PLOT_DISP(i_ud,"ud"); sprintf(xlabel,"M_%s^2 (MeV^2)",param->s_name); PLOT_ADD_EX(i_s,s); PLOT_DISP(i_s,"s"); strbufcpy(xlabel,"a (MeV^-1)"); PLOT_ADD_EX(i_a,s); PLOT_DISP(i_a,"a"); if (param->have_umd) { sprintf(xlabel,"%s (MeV^2)",param->umd_name); PLOT_ADD_EX(i_umd,s); PLOT_DISP(i_umd,"umd"); } if (param->have_alpha) { strbufcpy(xlabel,"alpha"); PLOT_ADD_EX(i_alpha,s); PLOT_DISP(i_alpha,"alpha"); } strbufcpy(xlabel,"1/L (MeV)"); PLOT_ADD_EX(i_Linv,s); PLOT_DISP(i_Linv,"Linv"); PLOT_ADD_EX(i_vind,s); PLOT_DISP(i_vind,"Linv_av"); } else if (f == SCALE) { sprintf(gtitle,"scale setting: %s -- datasets: %s -- ensembles: %s", param->scale_part,param->dataset_cat,param->manifest); for (bind=0;bind<param->nbeta;bind++) { dbind = (double)(bind); b_int[0] = dbind - 0.1; b_int[1] = dbind + 0.1; xb[i_bind] = b_int; a = mat_get(fit,bind,0); fit_data_fit_region(d,xb); mat_set(phy_pt,i_ud,0,SQ(a*param->M_ud)); mat_set(phy_pt,i_s,0,SQ(a*param->M_s)); mat_set(phy_pt,i_umd,0,SQ(a)*param->M_umd_val); mat_set(phy_pt,i_bind,0,bind); mat_set(phy_pt,i_a,0,a); mat_set(phy_pt,i_Linv,0,0.0); sprintf(color,"%d",1+(int)bind); sprintf(title,"beta = %s",param->beta[bind]); PLOT_ADD_FIT(PF_DATA|PF_FIT,i_ud,0,title,color); PLOT_ADD_FIT(PF_DATA|PF_FIT,i_s,0,title,color); PLOT_ADD_FIT(PF_DATA|PF_FIT,i_umd,0,title,color); PLOT_ADD_FIT(PF_DATA|PF_FIT,i_Linv,0,title,color); fit_data_fit_all_points(d,true); } sprintf(ylabel,"(a*M_%s)^2",param->scale_part); sprintf(xlabel,"(a*M_%s)^2",param->ud_name); PLOT_DISP(i_ud,"ud"); sprintf(xlabel,"(a*M_%s)^2",param->s_name); PLOT_DISP(i_s,"s"); if (param->have_umd) { sprintf(xlabel,"a^2*%s",param->umd_name); PLOT_DISP(i_umd,"umd"); } strbufcpy(xlabel,"a/L"); PLOT_DISP(i_Linv,"Linv"); } param->scale_model = 0; mat_destroy(phy_pt); mat_destroy(x_k); mat_destroy(cordat); for (bind=0;bind<param->nbeta;bind++) { mat_destroy(vol_av_corr[bind]); } free(vol_av_corr); for (k=0;k<N_EX_VAR;k++) { plot_destroy(p[k]); } }
int main(void) { int i, result; flint_rand_t state; printf("inv... "); fflush(stdout); _randinit(state); /* Check aliasing */ /* Managed element type (mpq_t) */ for (i = 0; i < 50; i++) { long n; ctx_t ctx; mat_t A, B, C; long ansB, ansC; n = n_randint(state, 20) + 1; ctx_init_mpq(ctx); mat_init(A, n, n, ctx); mat_init(B, n, n, ctx); mat_init(C, n, n, ctx); mat_randtest(A, state, ctx); mat_set(B, A, ctx); ansC = mat_inv(C, B, ctx); ansB = mat_inv(B, B, ctx); result = ((ansB == ansC) && (!ansB || mat_equal(B, C, ctx))); if (!result) { printf("FAIL:\n\n"); printf("A: \n"), mat_print(A, ctx), printf("\n"); printf("B: \n"), mat_print(B, ctx), printf("\n"); printf("C: \n"), mat_print(C, ctx), printf("\n"); printf("ansB = %ld\n", ansB); printf("ansC = %ld\n", ansC); } mat_clear(A, ctx); mat_clear(B, ctx); mat_clear(C, ctx); ctx_clear(ctx); } /* Check A * A^{-1} == A^{-1} * A == Id */ /* Managed element type (mpq_t) */ for (i = 0; i < 50; i++) { long n; ctx_t ctx; mat_t A, B, C, D, I; long ansB; n = n_randint(state, 20) + 1; ctx_init_mpq(ctx); mat_init(A, n, n, ctx); mat_init(B, n, n, ctx); mat_init(C, n, n, ctx); mat_init(D, n, n, ctx); mat_init(I, n, n, ctx); mat_randtest(A, state, ctx); ansB = mat_inv(B, A, ctx); if (!ansB) { mat_mul(C, A, B, ctx); mat_mul(D, B, A, ctx); } result = (ansB || (mat_equal(C, D, ctx) && mat_is_one(C, ctx))); if (!result) { printf("FAIL:\n\n"); printf("A: \n"), mat_print(A, ctx), printf("\n"); printf("B: \n"), mat_print(B, ctx), printf("\n"); printf("C: \n"), mat_print(C, ctx), printf("\n"); printf("D: \n"), mat_print(D, ctx), printf("\n"); printf("ansB = %ld\n", ansB); } mat_clear(A, ctx); mat_clear(B, ctx); mat_clear(C, ctx); mat_clear(D, ctx); ctx_clear(ctx); } _randclear(state); _fmpz_cleanup(); printf("PASS\n"); return EXIT_SUCCESS; }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { double M; double log_thr; int Xs,D,Ys,i,j,ep=0; int maxentep; int pxy_size; double mx; Matrix pxy,phi,psi,phi_tr,psi_tr,phi_exp_d,psi_exp_d,psi_exp_m,phi_exp_m,grad_phi,grad_psi; double *px0,*py0,*px,*py,*pa,*pb,*grad_a,*grad_b; double Z; int xi,yi,di; MatFromMLab(prhs[0],&phi); /* Size NX,D */ MatFromMLab(prhs[1],&psi); /* Size NX,D */ MatFromMLab(prhs[2],&phi_tr); /* Size D,NX */ MatFromMLab(prhs[3],&psi_tr); /* Size D,NY */ px0 = mxGetPr(prhs[4]); py0 = mxGetPr(prhs[5]); MatFromMLab(prhs[6],&phi_exp_d); MatFromMLab(prhs[7],&psi_exp_d); pa = mxGetPr(prhs[8]); pb = mxGetPr(prhs[9]); Xs = mxGetM(prhs[0]); D = mxGetN(prhs[0]); Ys = mxGetM(prhs[1]); pxy_size = Xs*Ys; plhs[0] = mxCreateDoubleMatrix(Xs,D,mxREAL); plhs[1] = mxCreateDoubleMatrix(D,Ys,mxREAL); plhs[2] = mxCreateDoubleMatrix(Xs,Ys,mxREAL); plhs[3] = mxCreateDoubleMatrix(1,Xs,mxREAL); plhs[4] = mxCreateDoubleMatrix(1,Ys,mxREAL); grad_phi.dat = mxGetPr(plhs[0]); grad_psi.dat = mxGetPr(plhs[1]); grad_phi.nx = Xs; grad_phi.ny = D; grad_phi.nall = Xs*D; grad_psi.nx = D; grad_psi.ny = Ys; grad_psi.nall = Ys*D; /* Will hold the models distribution */ pxy.dat = mxGetPr(plhs[2]); pxy.nx = Xs; pxy.ny = Ys; pxy.nall = Xs*Ys; grad_a = mxGetPr(plhs[3]); grad_b = mxGetPr(plhs[4]); AllocMat(D,Ys,&phi_exp_m); AllocMat(Xs,D,&psi_exp_m); px = malloc(Xs*sizeof(double)); py = malloc(Ys*sizeof(double)); mat_logemb_model(phi_tr,psi_tr,pa,pb,pxy); Z = mat_el_exp_sum(pxy.dat, pxy_size); mat_el_mul_scalar(pxy.dat, 1/Z, pxy_size); mat_sum_rows(pxy,px); mat_sum_cols(pxy,py); mat_mul_a_tr_b(phi,pxy,phi_exp_m); mat_mul(pxy,psi,psi_exp_m); /* Matlab line: diag(px)*phi-pxy*psi - (diag(px0)*phi-pxy0*psi); /* equivalent to diag(px-px0)*phi+psi_expect_d - psi_expect_m */ vec_subtract(px,px0,Xs); mat_set(grad_phi,phi); mat_mul_rows(grad_phi,px); mat_subtract(psi_exp_d.dat,psi_exp_m.dat,psi_exp_d.nall); mat_add(grad_phi.dat,psi_exp_d.dat,psi_exp_d.nall); vec_subtract(py,py0,Ys); mat_set(grad_psi,psi_tr); mat_mul_cols(grad_psi,py); mat_subtract(phi_exp_d.dat,phi_exp_m.dat,phi_exp_d.nall); mat_add(grad_psi.dat,phi_exp_d.dat,phi_exp_d.nall); mat_el_log(pxy.dat,pxy.nall); /* Generate gradient for a and b. Remember px already has px subtracted. Just need to flip sign */ memcpy(grad_a,px,Xs*sizeof(double)); vec_mul_scalar(grad_a,-1,Xs); memcpy(grad_b,py,Ys*sizeof(double)); vec_mul_scalar(grad_b,-1,Ys); free(psi_exp_m.dat); free(phi_exp_m.dat); free(px); free(py); }
void plot_add_fit(plot *p, const fit_data *d, const size_t ky, const mat *x_ex,\ const size_t kx, const mat *par, const double xmin, \ const double xmax, const size_t npt, const bool do_sub, \ const unsigned int obj, const strbuf dat_title, \ const strbuf fit_title, const strbuf dat_color, \ const strbuf fit_color) { mat *x,*x_err,*y,*y_err,*cor_data; bool have_x_err; size_t nfitpt,ndata; size_t i,j; nfitpt = fit_data_fit_point_num(d); ndata = fit_data_get_ndata(d); j = 0; have_x_err = fit_data_have_x_covar(d,kx); x = mat_create(nfitpt,1); y = mat_create(nfitpt,1); x_err = mat_create(nfitpt,1); y_err = mat_create(nfitpt,1); cor_data = mat_create(ndata,1); if (par != NULL) { if (obj & PF_FIT) { plot_add_model(p,d->model->func[ky],x_ex,kx,par,d->model_param,\ xmin,xmax,npt,fit_title,fit_color); } if ((obj & PF_DATA)&&(do_sub)) { fit_partresidual(cor_data,d,ky,x_ex,kx,par); } else { fit_data_get_y_k(cor_data,d,ky); } } else { fit_data_get_y_k(cor_data,d,ky); } if (obj & PF_DATA) { for (i=0;i<ndata;i++) { if (fit_data_is_fit_point(d,i)) { mat_set(x,j,0,fit_data_get_x(d,i,kx)); if (have_x_err) { mat_set(x_err,j,0, \ sqrt(mat_get(fit_data_pt_x_covar(d,kx,kx),i,i))); } mat_set(y,j,0,mat_get(cor_data,i,0)); mat_set(y_err,j,0, \ sqrt(mat_get(fit_data_pt_y_covar(d,ky,ky),i,i))); j++; } } if (have_x_err) { plot_add_dat(p,x,y,x_err,y_err,dat_title,dat_color); } else { plot_add_dat(p,x,y,NULL,y_err,dat_title,dat_color); } } mat_destroy(x); mat_destroy(x_err); mat_destroy(y); mat_destroy(y_err); mat_destroy(cor_data); }