void tex::math_ac () { if (cur_cmd == ACCENT) { print_err("Please use "); print_esc("mathaccent"); print(" for accents in math mode"); help_math_accent(); error(); } tail_append(new_node(ACCENT_NOAD_SIZE)); type(tail) = ACCENT_NOAD; subtype(tail) = NORMAL; mzero(nucleus(tail)); mzero(subscr(tail)); mzero(supscr(tail)); math_type(accent_chr(tail)) = MATH_CHAR; scan_fifteen_bit_int(); character(accent_chr(tail)) = cur_val % 256; if (cur_val >= VAR_CODE && fam_in_range()) { fam(accent_chr(tail)) = cur_fam; } else { fam(accent_chr(tail)) = cur_val / 256 % 16; } scan_math(nucleus(tail)); }
void tex::math_radical () { tail_append(new_node(RADICAL_NOAD_SIZE)); type(tail) = RADICAL_NOAD; subtype(tail) = NORMAL; mzero(nucleus(tail)); mzero(supscr(tail)); mzero(subscr(tail)); scan_delimiter(left_delimiter(tail), TRUE); scan_math(nucleus(tail)); }
void tex::math_fraction () { int c; mcell garbage; c = cur_chr; if (incompleat_noad != null) { if (c >= DELIMITED_CODE) { scan_delimiter((ptr)&garbage, FALSE); scan_delimiter((ptr)&garbage, FALSE); } if (c % DELIMITED_CODE == ABOVE_CODE) scan_normal_dimen(); print_err("Ambiguous; you need another { and }"); help_fraction(); error(); } else { incompleat_noad = new_node(FRACTION_NOAD_SIZE); type(incompleat_noad) = FRACTION_NOAD; subtype(incompleat_noad) = NORMAL; math_type(numerator(incompleat_noad)) = SUB_MLIST; math_link(numerator(incompleat_noad)) = link(head); mzero(denominator(incompleat_noad)); mzero(left_delimiter(incompleat_noad)); mzero(right_delimiter(incompleat_noad)); link(head) = null; tail = head; if (c >= DELIMITED_CODE) { scan_delimiter(left_delimiter(incompleat_noad), FALSE); scan_delimiter(right_delimiter(incompleat_noad), FALSE); } switch (c % DELIMITED_CODE) { case ABOVE_CODE: scan_normal_dimen(); thickness(incompleat_noad) = cur_val; break; case OVER_CODE: thickness(incompleat_noad) = DEFAULT_CODE; break; case ATOP_CODE: thickness(incompleat_noad) = 0; break; } } }
void walsh(double in[N][N],double out[N][N]){ double tmp[N][N]; mzero(out); mzero(tmp); for(int y=0;y<N;y++) for(int x=0;x<N;x++) for(int iy=0;iy<N;iy++) tmp[y][x]+=(double)w_j[y][iy]*in[iy][x]; for(int y=0;y<N;y++) for(int x=0;x<N;x++) for(int iy=0;iy<N;iy++) out[y][x]+=tmp[y][iy]*(double)w_j[iy][x]/8.0; }
void dwt(double in[N][N],double out[N][N]){ double tmp[N][N]; mzero(out); mzero(tmp); for(int y=0;y<N;y++) for(int x=0;x<N;x++){ in[y][x]*=2.0; in[y][x]+=1.0; } for(int n=N;n>1;n/=2){ for(int y=0;y<n;y++) for(int x=0;x<n/2;x++){ tmp[y][x] =0; tmp[y][x+n/2]=0; for(int i=0;i<4;i++){ int idx=(2*x+i)%n; tmp[y][x] +=p4[i]*in[y][idx]/2.0; tmp[y][x+n/2]+=q4[i]*in[y][idx]; } } for(int x=0;x<n;x++) for(int y=0;y<n/2;y++){ out[y ][x]=0; out[y+n/2][x]=0; for(int i=0;i<4;i++){ int idx=(2*y+i)%n; out[y ][x]+=p4[i]*tmp[idx][x]/2.0; out[y+n/2][x]+=q4[i]*tmp[idx][x]; } } for(int x=0;x<n;x++) for(int y=0;y<n;y++) in[y][x]=out[y][x]; } }
void idwt(double in[N][N],double out[N][N]){ double tmp[N][N]; mzero(out); mzero(tmp); for(int n=2;n<=N;n*=2){ for(int x=0;x<n;x++) for(int y=0;y<n/2;y++){ tmp[y*2 ][x]=0; tmp[y*2+1][x]=0; for(int i=0;i<4/2;i++){ int idx=(y-i+(n/2)*128)%(n/2); tmp[y*2 ][x]+=(p4[i*2 ]*in[idx][x]+q4[i*2 ]*in[idx+n/2][x]/2.0); tmp[y*2+1][x]+=(p4[i*2+1]*in[idx][x]+q4[i*2+1]*in[idx+n/2][x]/2.0); } } for(int y=0;y<n;y++) for(int x=0;x<n/2;x++){ out[y][x*2 ]=0; out[y][x*2+1]=0; for(int i=0;i<4/2;i++){ int idx=(x-i+(n/2)*128)%(n/2); out[y][x*2 ]+=(p4[i*2 ]*tmp[y][idx]+q4[i*2 ]*tmp[y][idx+n/2]/2.0); out[y][x*2+1]+=(p4[i*2+1]*tmp[y][idx]+q4[i*2+1]*tmp[y][idx+n/2]/2.0); } } for(int x=0;x<n;x++) for(int y=0;y<n;y++) in[y][x]=out[y][x]; } for(int y=0;y<N;y++) for(int x=0;x<N;x++) out[y][x]/=2.0; }
void ihaar(double in[N][N],double out[N][N]){ double tmp[N][N]; mzero(out); mzero(tmp); for(int n=2;n<=N;n*=2){ for(int x=0;x<n;x++) for(int y=0;y<n/2;y++){ tmp[y*2 ][x]=(in[y][x]*2+in[y+n/2][x])/2; tmp[y*2+1][x]=(in[y][x]*2-in[y+n/2][x])/2; } for(int y=0;y<n;y++) for(int x=0;x<n/2;x++){ out[y][x*2 ]=(tmp[y][x]*2+tmp[y][x+n/2])/2; out[y][x*2+1]=(tmp[y][x]*2-tmp[y][x+n/2])/2; } for(int x=0;x<n;x++) for(int y=0;y<n;y++) in[y][x]=out[y][x]; } }
void haar(double in[N][N],double out[N][N]){ double tmp[N][N]; mzero(out); mzero(tmp); for(int n=N;n>1;n/=2){ for(int y=0;y<n;y++) for(int x=0;x<n/2;x++){ tmp[y][x] =(in[y][x*2]+in[y][x*2+1])/2; tmp[y][x+n/2]= in[y][x*2]-in[y][x*2+1]; } for(int x=0;x<n;x++) for(int y=0;y<n/2;y++){ out[y ][x]=(tmp[y*2][x]+tmp[y*2+1][x])/2; out[y+n/2][x]= tmp[y*2][x]-tmp[y*2+1][x]; } for(int x=0;x<n;x++) for(int y=0;y<n;y++) in[y][x]=out[y][x]; } }
void idct(double in[N][N],double out[N][N]){ mzero(out); for(int y=0;y<N;y++) for(int x=0;x<N;x++) for(int iy=0;iy<N;iy++) for(int ix=0;ix<N;ix++) out[y][x]+= (ix==0?1/(sqrt(2)):1)* (iy==0?1/(sqrt(2)):1)* cos((2*x+1)*ix*M_PI/(2*N))* cos((2*y+1)*iy*M_PI/(2*N))* in[iy][ix]* (2.0/N); }
int main(){ double out[8][8]; double out2[8][8]; double out3[8][8]; double in[8][8]; double in2[8][8]; for(int y=0;y<N;y++) for(int x=0;x<N;x++){ mzero(in); in[y][x]=1; idct(in,out); for(int iy=0;iy<N;iy++) for(int ix=0;ix<N;ix++){ if(out[iy][ix]>=0) in2[iy][ix]=255; else in2[iy][ix]=0; } dct(in2,out2); if(x==0&&y==0) out3[y][x]=out2[0][0]; else out3[y][x]=mmax(out2); } // mmap(out3,(1.0/4)*); mmap(out3,round); mprint(out3); return true; }
/******************************************************************* Subroutine to do the EM algorithm matrix *D: the pointer to the matrix data matrix *mean0_x: the pointer to a matrix containing the initial Means of clusters vector *w0: the pointer to a vector containing the initial mixing proportion of clusters double vv: the value for initializing the Covariance matrix of clusters double error: the error threshold vector *Zjk_up: the pointer to a vector containing Posterior probabilities of the up-level cluster samples matrix *mean1_x: the pointer to a matrix containing the Means of clusters in t-space vector *w0_t: the pointer to a vector containing the mixing proportions of the identified clusters in t-space matrix *cov_mat: the pointer to a group of matrixs containing the Covariance matrix of clusters in t-space matrix *Zjk: the pointer to a matrix containing Posterior probabilities of all samples belonging to all the sub-level clusters, each column is for one cluster. return value: '1' - successfully exit '0' - exit with waring/error *******************************************************************/ int veSubEM(matrix *D, matrix *mean0_x, vector *w0, double vv, double error, vector *Zjk_up, //input matrix *mean1_x, vector *w0_t, matrix *cov_mat, matrix *Zjk) //output { int k0, kc, n, p; int i, j, k, u, s; matrix *Var0; matrix Gxn; vector Fx; matrix MUK; matrix MU1; int zeroFx_num = 1; //double error = 0.01; double err = error + (double)1; vector Zjk_temp; n = D->m; p = D->n; k0 = mean0_x->m; kc = mean0_x->n; Var0 = new matrix[k0]; for(i=0; i<k0; i++) { mnew(Var0+i, p, p); } mnew(&Gxn, n, k0); vnew(&Fx, n); vnew(&Zjk_temp, n); mnew(&MUK, k0, p); mcopy(mean0_x, &MUK); mnew(&MU1, k0, p); vector D_j; vector Zjk_k; double sum_tmp = 0; matrix Ck; vector D_i; vector MUK_k; vector cen_D_i; matrix mtmp; vector vtmp; vnew(&D_j, n); vnew(&Zjk_k, n); mnew(&Ck, p, p); vnew(&D_i, p); vnew(&MUK_k, p); vnew(&cen_D_i, p); mnew(&mtmp, p, p); vnew(&vtmp, n); //Initializing the parameters of mixture of Gaussians //Initinalize the covariance matrix //Use EM algorithm to perform the local training. //Test intialization of covarinace matrix //printf("Testing covariance matrix initialization... \n"); while (zeroFx_num != 0) { for(i=0; i<k0; i++) { meye(Var0+i); for (j=0; j<p; j++) { *((Var0+i)->pr+j*p+j) = vv; } } veModel(D, mean0_x, Var0, w0, &Gxn, &Fx); //printf("\n Gxn = :\n"); //mprint(&Gxn); //printf("\n Fx = :\n"); //vprint(&Fx); zeroFx_num = 0; for (i=0; i<n; i++) { if (*(Fx.pr+i) == 0) { zeroFx_num++; } } vv *= 2; } vones(&Zjk_temp); //printf("\n EM in t-space starts ... \n"); //printf("\n Data = \n"); //mprint(D); int l = 0; while (err > error) { #ifdef _DEBUG printf(" \n...... in EM loop %d ......\n", ++l); printf("\n L%d: w0 = \n", l); vprint(w0); printf("\n L%d: MUK = \n", l); mprint(&MUK); printf("\n L%d: Var0 = \n", l); for(i=0; i<k0; i++) { mprint(Var0+i); printf("\n"); } printf("\n L%d: Zjk = \n", l); mprint(Zjk); #endif veModel(D, &MUK, Var0, w0, &Gxn, &Fx); #ifdef _DEBUG printf("\n L%d: Gxn = \n", l); mprint(&Gxn); printf("\n L%d: Fx = \n", l); vprint(&Fx); #endif for (k=0; k<k0; k++) { u = k*p; double zz = 0; double zz_up = 0; for (i=0; i<n; i++) { *(Zjk->pr+i*k0+k) = (*(w0->pr+k)) * Zjk_up->pr[i] * (*(Gxn.pr+i*k0+k)) / (*(Fx.pr+i)); zz += *(Zjk->pr+i*k0+k); zz_up += Zjk_up->pr[i]; } *(w0->pr+k) = zz/zz_up; for (j=0; j<p; j++) { getcolvec(D, j, &D_j); getcolvec(Zjk, k, &Zjk_k); sum_tmp = 0; for (i=0; i<n; i++) { sum_tmp += (*(Zjk_k.pr+i)) * (*(D_j.pr+i)); } *(MU1.pr+u+j) = sum_tmp / zz; } mzero(&Ck); for (i=0; i<n; i++) { getrowvec(D, i, &D_i); getrowvec(&MUK, k, &MUK_k); for (j=0; j<p; j++) { *(cen_D_i.pr+j) = *(D_i.pr+j) - *(MUK_k.pr+j); } vvMul(&cen_D_i, &cen_D_i, &mtmp); for (j=0; j<p; j++) { for (s=0; s<p; s++) { *(Ck.pr+j*p+s) += (*(Zjk->pr+i*k0+k)) * (*(mtmp.pr+j*p+s)); } } } for (j=0; j<p; j++) { for (s=0; s<p; s++) { *(Var0[k].pr+j*p+s) = (*(Ck.pr+j*p+s)) / zz; } } } // for (k... mcopy(&MU1, &MUK); for (i=0; i<n; i++) { *(vtmp.pr+i) = fabs(*(Zjk_k.pr+i) - *(Zjk_temp.pr+i)); } err = vmean(&vtmp); vcopy(&Zjk_k, &Zjk_temp); } // while vcopy(w0, w0_t); mcopy(&MUK, mean1_x); for(i=0; i<k0; i++) { mcopy(Var0+i, cov_mat+i); } for(i=0; i<k0; i++) { mdelete(Var0+i); } mdelete(&Gxn); vdelete(&Fx); vdelete(&Zjk_temp); mdelete(&MUK); mdelete(&MU1); vdelete(&D_j); vdelete(&Zjk_k); mdelete(&Ck); vdelete(&D_i); vdelete(&MUK_k); vdelete(&cen_D_i); mdelete(&mtmp); vdelete(&vtmp); return 1; }
int main(int argc, char *argv[]) { (void)argc; (void)argv; #ifdef __SSE__ printf("SSE "); #endif #ifdef __SSE2__ printf("SSE2 "); #endif #ifdef __SSE3__ printf("SSE3 "); #endif #ifdef __SSE4__ printf("SSE4 "); #endif #ifdef __SSE4_1__ printf("SSE4.1 "); #endif #ifdef __SSE4_2__ printf("SSE4.2 "); #endif #ifdef __AVX__ printf("AVX "); #endif #ifdef __FMA4__ printf("FMA4 "); #endif printf("\n"); printv(vec(1, 2, 3, 4)); printv(vzero()); printm(mzero()); printm(midentity()); vec4 a = { 1, 2, 3, 4 }, b = { 5, 6, 7, 8 }; printv(a); printv(b); printf("\nshuffles:\n"); printv(vshuffle(a, a, 0, 1, 2, 3)); printv(vshuffle(a, a, 3, 2, 1, 0)); printv(vshuffle(a, b, 0, 1, 0, 1)); printv(vshuffle(a, b, 2, 3, 2, 3)); printf("\ndot products:\n"); printv(vdot(a, b)); printv(vdot(b, a)); printv(vdot3(a, b)); printv(vdot3(b, a)); //vec4 blendmask = { 1, -1, 1, -1 }; //printv(vblend(x, y, blendmask)); vec4 x = { 1, 0, 0, 0 }, y = { 0, 1, 0, 0 }, z = { 0, 0, 1, 0 }, w = { 0, 0, 0, 1 }; printf("\ncross products:\n"); printv(vcross(x, y)); printv(vcross(y, x)); printv(vcross_scalar(x, y)); printv(vcross_scalar(y, x)); printf("\nquaternion products:\n"); printv(qprod(x, y)); printv(qprod(y, x)); printv(qprod_mad(x, y)); printv(qprod_mad(y, x)); printv(qprod_scalar(x, y)); printv(qprod_scalar(y, x)); printf("\nquaternion conjugates:\n"); printv(qconj(x)); printv(qconj(y)); printv(qconj(z)); printv(qconj(w)); printf("\nmat from quat:\n"); printm(quat_to_mat(w)); printm(quat_to_mat_mmul(w)); printm(quat_to_mat_scalar(w)); vec4 angles = { 0.0, 0.0, 0.0, 0.0 }; printf("\neuler to quaternion:\n"); printv(quat_euler(angles)); printv(quat_euler_scalar(angles)); printv(quat_euler_gems(angles)); printf("\neuler to matrix:\n"); printm(mat_euler(angles)); printm(mat_euler_scalar(angles)); printm(quat_to_mat(quat_euler(angles))); printf("\nperspective matrix:\n"); printm(mat_perspective_fovy(M_PI/4.0, 16.0/9.0, 0.1, 100.0)); printm(mat_perspective_fovy_inf_z(M_PI/4.0, 16.0/9.0, 0.1)); printm(mat_perspective_fovy_scalar(M_PI/4.0, 16.0/9.0, 0.1, 100.0)); printf("\northogonal matrix:\n"); printm(mat_ortho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0)); printm(mat_ortho(-1.0, 2.0, -1.0, 2.0, -1.0, 2.0)); printf("\ntranslate matrix:\n"); printm(mtranslate(a)); printf("\nscale matrix:\n"); printm(mscale(a)); return 0; }
// From List. // You can't insert an element within an empty list; fail. // insertWithin(Nil, ?x) == mzero() ListOfLists insertWithin(Element x) { return mzero(); }