Beispiel #1
0
/*
 * Macro definition
 */
const prog_macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt)
{
    keyevent_t event = record->event;
    tap_t tap = record->tap;

    switch (id) {
        case LSHIFT_PAREN:
            if (tap.count > 0 && !tap.interrupted) {
                return (event.pressed ?
                        MACRO( MD(LSHIFT), D(9), U(9), MU(LSHIFT), END ) : MACRO_NONE);
            } else {
                return (event.pressed ?
                        MACRO( MD(LSHIFT), END ) : MACRO( MU(LSHIFT), END ) );
            }
        case RSHIFT_PAREN:
            if (tap.count > 0 && !tap.interrupted) {
                return (event.pressed ?
                        MACRO( MD(RSHIFT), D(0), U(0), MU(RSHIFT), END ) : MACRO_NONE);
            } else {
                return (event.pressed ?
                        MACRO( MD(RSHIFT), END ) : MACRO( MU(RSHIFT), END ) );
            }
        case HELLO:
            return (event.pressed ?
                    MACRO( I(0), T(H), T(E), T(L), T(L), W(255), T(O), END ) :
                    MACRO_NONE );
    }
    return MACRO_NONE;
}
void splineM(int N,VEC &X,VEC &Y,VEC &M){
	M[0]=0;
	M[N]=0;
	VEC h(N);
	h[0] = 0;
	for(int i=1;i<N;i++){			// h is the length of the subinterval 
		h[i] = X[i] - X[i-1];
	}
	VEC MU(N+1);
	VEC LAMBDA(N);
	VEC d(N+1);
	MAT W(N+1);
	MAT L(N+1);
	LAMBDA[0] = 0;
	d[0] = 0;
	MU[N] = 0;
	d[N] = 0;
	for(int i=1;i<N;i++){			//prepare the variable for the tridiagonal matrix
		MU[i] = h[i]/(h[i]+h[i+1]);
		LAMBDA[i] = h[i+1]/(h[i]+h[i+1]);
		d[i] = (6/(h[i]+h[i+1]))*(((Y[i+1]-Y[i])/h[i+1])-((Y[i]-Y[i-1])/h[i]));
	}
	for(int i=0;i<N+1;i++){		//initialization
		for(int j=0;j<N+1;j++){
			W[i][j] = 0;
		}
	}
	for(int i=0;i<N+1;i++){		
		W[i][i] = 2;			//diagonal
		
	}
	for(int i=0;i<N;i++){
		W[i][i+1] = LAMBDA[i];	//above diagonal
	}
	for(int i=1;i<N+1;i++){
		W[i][i-1] = MU[i];	//below diagonal
		
	}
	
	L = cholesky(W);                        //in-place Cholesky Decomposition
    M = choSolve(L,d);                      //solve the linear system by forward and backward substitution
}
Beispiel #3
0
void gmm_fisher_save_soft_assgn(int n, const float *v, const gmm_t * g, int flags,
                                float *dp_dlambda,
                                float *word_total_soft_assignment) {
    long d=g->d, k=g->k;
    float *p = fvec_new(n * k);
    long i,j,l;
    long ii=0;

    float * vp = NULL; /* v*p */
    float * sum_pj = NULL; /* sum of p's for a given j */

    gmm_compute_p(n,v,g,p,flags | GMM_FLAGS_W);

#define P(j,i) p[(i)*k+(j)]
#define V(l,i) v[(i)*d+(l)]
#define MU(l,j) g->mu[(j)*d+(l)]
#define SIGMA(l,j) g->sigma[(j)*d+(l)]
#define VP(l,j) vp[(j)*d+(l)]

    // Save total soft assignment per centroid
    if (word_total_soft_assignment != NULL) {
        for (j=0; j<k; j++) {
            double sum=0;
            for (i=0; i<n; i++) {
                sum += P(j,i);
            }
            if (n != 0) {
                word_total_soft_assignment[j] = (float)(sum/n);
            } else {
                word_total_soft_assignment[j] = 0.0;
            }
        }
    }

    if(flags & GMM_FLAGS_W) {

        for(j=1; j<k; j++) {
            double accu=0;

            for(i=0; i<n; i++)
                accu+= P(j,i)/g->w[j] - P(0,i)/g->w[0];

            /* normalization */
            double f=n*(1/g->w[j]+1/g->w[0]);

            dp_dlambda[ii++]=accu/sqrt(f);
        }
    }

    if(flags & GMM_FLAGS_MU) {
        float *dp_dmu=dp_dlambda+ii;

#define DP_DMU(l,j) dp_dmu[(j)*d+(l)]

        if(0) { /* simple and slow */

            for(j=0; j<k; j++) {
                for(l=0; l<d; l++) {
                    double accu=0;

                    for(i=0; i<n; i++)
                        accu += P(j,i) * (V(l,i)-MU(l,j)) / SIGMA(l,j);

                    DP_DMU(l,j)=accu;
                }
            }

        } else { /* complicated and fast */

            /* precompute  tables that may be useful for sigma too */
            vp = fvec_new(k * d);
            fmat_mul_tr(v,p,d,k,n,vp);

            sum_pj = fvec_new(k);
            for(j=0; j<k; j++) {
                double sum=0;
                for(i=0; i<n; i++) sum += P(j,i);
                sum_pj[j] = sum;
            }

            for(j=0; j<k; j++) {
                for(l=0; l<d; l++)
                    DP_DMU(l,j) = (VP(l,j) - MU(l,j) * sum_pj[j]) / SIGMA(l,j);
            }

        }
        /* normalization */
        if(!(flags & GMM_FLAGS_NO_NORM)) {
            for(j=0; j<k; j++)
                for(l=0; l<d; l++) {
                    float nf = sqrt(n*g->w[j]/SIGMA(l,j));
                    if(nf > 0) DP_DMU(l,j) /= nf;
                }
        }
#undef DP_DMU
        ii+=d*k;
    }

    if(flags & (GMM_FLAGS_SIGMA | GMM_FLAGS_1SIGMA)) {


        if(flags & GMM_FLAGS_1SIGMA) { /* fast not implemented for 1 sigma */

            for(j=0; j<k; j++) {
                double accu2=0;
                for(l=0; l<d; l++) {
                    double accu=0;

                    for(i=0; i<n; i++)
                        accu += P(j,i) * (sqr(V(l,i)-MU(l,j)) / SIGMA(l,j) - 1) / sqrt(SIGMA(l,j));

                    if(flags & GMM_FLAGS_SIGMA) {

                        double f=flags & GMM_FLAGS_NO_NORM ? 1.0 : 2*n*g->w[j]/SIGMA(l,j);

                        dp_dlambda[ii++]=accu/sqrt(f);
                    }
                    accu2+=accu;
                }

                if(flags & GMM_FLAGS_1SIGMA) {
                    double f=flags & GMM_FLAGS_NO_NORM ? 1.0 : 2*d*n*g->w[j]/SIGMA(0,j);
                    dp_dlambda[ii++]=accu2/sqrt(f);
                }

            }

        } else { /* fast and complicated */
            assert(flags & GMM_FLAGS_SIGMA);
            float *dp_dsigma = dp_dlambda + ii;

            if(!vp) {
                vp = fvec_new(k * d);
                fmat_mul_tr(v,p,d,k,n,vp);
            }

            if(!sum_pj) {
                sum_pj = fvec_new(k);
                for(j=0; j<k; j++) {
                    double sum=0;
                    for(i=0; i<n; i++) sum += P(j,i);
                    sum_pj[j] = sum;
                }
            }
            float *v2 = fvec_new(n * d);
            for(i = n*d-1 ; i >= 0; i--) v2[i] = v[i] * v[i];
            float *v2p = fvec_new(k * d);
            fmat_mul_tr(v2,p,d,k,n,v2p);
            free(v2);

#define V2P(l,j) v2p[(j)*d+(l)]
#define DP_DSIGMA(i,j) dp_dsigma[(i)+(j)*d]
            for(j=0; j<k; j++) {

                for(l=0; l<d; l++) {
                    double accu;

                    accu = V2P(l, j);

                    accu += VP(l, j) * (- 2 * MU(l,j));

                    accu += sum_pj[j] * (sqr(MU(l,j))  - SIGMA(l,j));

                    /* normalization */

                    double f;

                    if(flags & GMM_FLAGS_NO_NORM) {
                        f = pow(SIGMA(l,j), -1.5);
                    } else {
                        f = 1 / (SIGMA(l,j) * sqrt(2*n*g->w[j]));
                    }

                    DP_DSIGMA(l,j) = accu * f;

                }

            }

            free(v2p);

#undef DP_DSIGMA
#undef V2P
            ii += d * k;
        }

    }

    assert(ii==gmm_fisher_sizeof(g,flags));
#undef P
#undef V
#undef MU
#undef SIGMA
    free(p);
    free(sum_pj);
    free(vp);
}