float fuzzy_system(float inputs[], fuzzy_system_rec fz) {
	int i, j;
	short variable_index, fuzzy_set;
	float sum1 = 0.0, sum2 = 0.0, weight;
	float m_values[MAX_NO_OF_INPUTS];

	for (i = 0; i < fz.no_of_rules; i++) {
		for (j = 0; j < fz.no_of_inputs; j++) {
			variable_index = fz.rules[i].inp_index[j];
			fuzzy_set = fz.rules[i].inp_fuzzy_set[j];
			m_values[j] = trapz(inputs[variable_index],
				fz.inp_mem_fns[variable_index][fuzzy_set]);
		} /* end j  */
		weight = min_of(m_values, fz.no_of_inputs);
		sum1 += weight * fz.output_values[fz.rules[i].out_fuzzy_set];
		sum2 += weight;
	} /* end i  */

	if (fabs(sum2) < TOO_SMALL) {
		cout << "\r\nFLPRCS Error: Sum2 in fuzzy_system is 0.  Press key: " << endl;
		//~ getch();
		exit(1);
		return 0.0;
	}

	return (sum1 / sum2);
}  /* end fuzzy_system  */
Esempio n. 2
0
void simpson(int *m1, int *n1, double *x, double *y, double *out) {
    int k, j;
    double *yptr;
    double dx1, dx2;
    double alpha, a0, a1, a2;
    int n = *n1;
    int m = *m1;

    if (m<3) {
        trapz(m1,n1,x,y,out);
    }
    else {
        for (k=0; k<n; k++)
        out[k] = 0.0;

        for (j=0; j<m-2; j=j+2) {
            dx1 = x[j+1] - x[j];
            dx2 = x[j+2] - x[j+1];

            alpha = (dx1+dx2)/dx1/6.0;
            a0 = alpha*(2.0*dx1-dx2);
            a1 = alpha*(dx1+dx2)*(dx1+dx2)/dx2;
            a2 = alpha*dx1/dx2*(2.0*dx2-dx1);

            yptr = y;
            for (k=0; k<n; k++) {
                out[k] += a0*yptr[j] + a1*yptr[j+1] +a2*yptr[j+2];
                yptr += m;
            }
        }

        if (m%2==0) {
            yptr = y;
            for (k=0; k<n; k++) {
                alpha = x[m-3]*x[m-2]*(x[m-3]-x[m-2]) -
                    x[m-3]*x[m-1]*(x[m-3]-x[m-1]) +
                    x[m-2]*x[m-1]*(x[m-2]-x[m-1]);
                a0 = yptr[m-3]*(x[m-2]-x[m-1]) - yptr[m-2]*(x[m-3]-x[m-1]) +
                    yptr[m-1]*(x[m-3]-x[m-2]);
                a1 = yptr[m-3]*(x[m-1]*x[m-1]-x[m-2]*x[m-2]) -
                    yptr[m-2]*(x[m-1]*x[m-1]-x[m-3]*x[m-3]) +
                    yptr[m-1]*(x[m-2]*x[m-2]-x[m-3]*x[m-3]);
                a2 = x[m-3]*x[m-2]*yptr[m-1]*(x[m-3]-x[m-2]) -
                    x[m-3]*yptr[m-2]*x[m-1]*(x[m-3]-x[m-1]) +
                    yptr[m-3]*x[m-2]*x[m-1]*(x[m-2]-x[m-1]);
                a0 /= alpha; a1 /= alpha; a2 /= alpha;

                out[k] += a0*(x[m-1]*x[m-1]*x[m-1]-x[m-2]*x[m-2]*x[m-2])/3 +
                    a1*(x[m-1]*x[m-1]-x[m-2]*x[m-2])/2 + a2*(x[m-1]-x[m-2]);
                yptr += m;
            }
        }
    }
}
Esempio n. 3
0
/* SRSF Inner Product */
void innerprod_q(int *m1, double *t, double *q1, double *q2, double *out) {
    int k;
    double *q;
    int m = *m1;
    int n1 = 1;

    q = (double *) malloc(m*sizeof(double));
    for (k=0; k<m; k++)
        q[k] = q1[k]*q2[k];

    trapz(m1, &n1, t, q, out);

    free(q);
}
Esempio n. 4
0
void norm(double * function_1, double left_endpoint, double right_endpoint, int num_points, double * integral){
	//cast ii as int so use in for loop. 
	int ii; 
	//creating the pointer psi_hat and dynamically allocating memory for it. 
	double * psi_hat;
	psi_hat = (double*)malloc(num_points*sizeof(double));
	//multiplying the two vectors(functions). 
	for(ii=0;ii<num_points;ii++){
		psi_hat[ii] = function_1[ii] * function_1[ii];
	}
	//calling trapz function to integrate the new psi_hat function. 
	trapz(psi_hat, left_endpoint, right_endpoint, num_points, integral);

	//computes the norm by taking the square root of the computed integral. 
	double norm = pow(*integral,0.5); 

	/*this section would normalize the vector, if you wanted to do something like that. 
	for(ii=0;ii<num_points;ii++){
		psi_hat[ii] = function_1[ii] / norm;
	}/*/

	free(psi_hat);

}
/* R interface for trapz function. */
void trapz_R(int *N, double *x, double *y, double *result){
	result[0] = trapz(N[0], x, y);
}
Esempio n. 6
0
void SqrtMeanInverse(int *T1, int *n1, double *ti, double *gami, double *out){
    int T = *T1, n = *n1;
    double psi[T*n], gam[T*n], mu[T], vec[T*n], v[T], y[T], tmpi, len, vm[T], mnpsi[T], dqq[n];
    double eps = DBL_EPSILON, tmpv[T], min = 0.0, binsize, tmp, gam_mu[T];
    int k, iter, l, n2 = 1, min_ind = 0;
    int maxiter = 30, tt = 1;
    double lvm[maxiter];
    double *x = malloc(sizeof(double)*(T));

    for (k=0; k<maxiter; k++)
        lvm[k] = 0;

    for (k=0; k<T; k++)
        gam_mu[k] = 0;

    
    for (k=0; k<T; k++)
        x[k] = (double)k/((double)(T-1));

    
    double *psi_ptr, *gam_ptr, *tmp1_ptr, *y_ptr, *gam_mu_ptr;

    binsize = 0;
    for (k=0; k<T-1; k++)
        binsize += ti[k+1]-ti[k];
    binsize = binsize/(T-1);

    for (k=0; k<T*n; k++){
        gam[k] = gami[k];
    }
    psi_ptr = psi; gam_ptr = gam;
    gradient(&T,&n,gam_ptr,&binsize,psi_ptr);
    for (k=0; k<T*n; k++)
        psi[k] = sqrt(fabs(psi[k])+eps);

    
    for (k=0; k<T; k++){
        tmp = 0;
        for(l=0; l<n; l++)
            tmp += psi[k+l*T];

        mnpsi[k] = tmp/n;
    }
    for (k=0; k<n; k++){
        for(l=0; l<T; l++)
            tmpv[l] = psi[k*T+l]-mnpsi[l];
        tmp = 0;
        for(l=0; l<T; l++)
            tmp += tmpv[l];
        dqq[k] = tmp;
    }

    for (k=0; k<n; k++) {
        if (k==0) {
            min_ind = 0;
            min = dqq[k];
        }
        else{
            if (dqq[k]<min){
                min = dqq[k];
                min_ind = k;
            }
        }
    }

    for (k=0; k<T; k++)
        mu[k] = psi[(min_ind-1)*T+k];


    
    for (iter=1; iter<maxiter; iter++){
        for (k=0; k<n; k++){
            for(l=0; l<T; l++)
                v[l] = psi[k*T+l] - mu[l];

            for(l=0; l<T; l++)
                y[l] = psi[k*T+l]*mu[l];
            y_ptr = y;
            trapz(T1, &n2, ti, y_ptr, &tmpi);

            if (tmpi > 1){
                tmpi = 1;
            }
            else if (tmpi < (-1)){
                tmpi = 1;
            }

            len = acos(tmpi);
            if (len > 0.0001){
                for(l=0; l<T; l++)
                    vec[k*T+l] = (len/sin(len))*(psi[k*T+l]-cos(len)*mu[l]);
            }
            else {
                for(l=0; l<T; l++)
                    vec[k*T+l] = 0;
            }

        }

        for (k=0; k<T; k++){
            tmp = 0;
            for(l=0; l<n; l++){
                tmp += vec[k+l*T];
            }
            vm[k] = tmp/n;
        }

        for (k=0; k<T; k++)
            tmpv[k] = vm[k]*vm[k];

        tmp = 0;
        for (k=0; k<T; k++)
            tmp += tmpv[k];

        lvm[iter] = sqrt(tmp*binsize);

        if (lvm[iter] == 0){
            break;
        }

        for (k=0; k<T; k++)
            mu[k] = cos(tt*lvm[iter])*mu[k]+(sin(tt*lvm[iter])/lvm[iter])*vm[k];

        if (lvm[iter] < 1e-6 || iter >= maxiter)
            break;
    }

    for (k=0; k<T; k++)
        tmpv[k] = mu[k]*mu[k];

    tmp1_ptr = tmpv;
    gam_mu_ptr = gam_mu;
    cumtrapz(T1,ti,tmp1_ptr,gam_mu_ptr);
    for (k=0; k<T; k++)
        gam_mu_ptr[k] = (gam_mu_ptr[k] - gam_mu_ptr[0])/(gam_mu_ptr[T-1]-gam_mu_ptr[0]); 

    invertGamma(T, gam_mu_ptr, out);

    free(x);
    return;
}