Esempio n. 1
0
void ShowMainMenu()
{
    int choice;
    DisplayInfo *mainMenu=(DisplayInfo *)malloc(sizeof(DisplayInfo));
    mainMenu->curPos = 0;
    mainMenu->size = 4;
    init2DArray(&(mainMenu->words), MainStr, mainMenu->size);
    InitDisplay(mainMenu);
    for(;;)
    {
        switch(keyValue)
        {
        case Up:
        {
            keyValue = Null;
            CursorMoveUp(mainMenu);
            break;
        }
        case Down:
        {
            keyValue = Null;
            CursorMoveDown(mainMenu);
            break;
        }
        	
        case Enter:
        {
        	keyValue = Null;
            choice = mainMenu->curPos;        
            switch(choice)
            {
            
            case 0:ParaMenu();break;
            case 1:ComMenu();break;
            case 2:ConstMenu();break;
            case 3:BacklistMenu();break;
            }
            ResumeDisplay(mainMenu);
            break;
        }
        default:
        {
            break;
        }
        }
    }
    free2DArray(&(mainMenu->words),mainMenu->size);
	free(mainMenu);
}
Esempio n. 2
0
int main(int arg, char* argv[])
{
	int p = 2;																	//	our basis order
	double U[] = {0.0, 0.0, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0,5.0, 5.0};		//	knot vector
	int m = sizeof(U)/sizeof(double) - 1;										//	number of knot points
	int n = m - p - 1;															//	number of basis functions
	
	double u; 
	int c, span;
	
	/*printf("Number of knot points: %d\n", m);
	
	//
	//	Test our knot span index code
	//
	
	for(c = 0; c < m+1; ++c)
	{
		span = FindSpan(n, p, U[c], U);
		printf("For Xi=%2.1f, the knot span index is i=%d\n\n",U[c], span);
	}*/
	
	//
	//	Now let's test our routines which calculate the basis functions and derivatives
	//
	
	int numNonZeroBsFns = p + 1;
	
	/*
	double *N = (double*)malloc(sizeof(double) * numNonZeroBsFns);
	
	double **ders = init2DArray(n+1, p+1);
	
	for(c = 0; c < numNonZeroBsFns; ++c) N[c] = 0.0;
	
	double point[] = {0.0, 1.0, 2,0, 3.0, 4.0, 5.0};	//	some sample points to evaluate at
	int k = sizeof(point) / sizeof(double);				//	number of points (for our loop)
	int j,d;
	
	for(j = 0; j < k; ++j)
	{
		span = FindSpan(n, p, point[j], U);				//	get the span
		BasisFuns( span, point[j], p, U, N);			//	get the non-zero basis functions
		dersBasisFuns(span, point[j], p, n, U, ders);	//	get the derivatives ders[k][j]

		//	print out basis functions
		
		printf("The non-zero basis functions in the range %2.2f and %2.2f and u=%2.2f are:\n",U[span], U[span+1], point[j]);
		for(c = 0; c < numNonZeroBsFns; ++c) printf("\n %2.2f\n", N[c]);
		
		
		// 	print out derivatives
		
		for(d = 0; d < (n+1); d++)
		{
			printf("For k=%d, derivatives are: \n", d);
			for(c = 0; c < numNonZeroBsFns; c++) printf("%2.2f\t", ders[d][c]);
			printf("\n");
		}
		printf("\n\n");

	}
	
	free(N);
	free2Darray(ders, n+1);
	*/
	
	double *dN = (double*)malloc(sizeof(double) * (n+1));
	double **ders = init2DArray(n+1, p+1);
	
	double xi, Nip;
	int i=2;
	
	printf("xi\tN\tdNdxi\n");
	for(xi=0.0; xi <= 5.0; xi+=0.1)
	{
		span = FindSpan(n, p, xi, U);
		Nip = OneBasisFun(p, m, U, i, xi);
		dersOneBasisFuns(p, m, U, i, xi, n, dN);

	}
	
	xi = 2.1;
	span = FindSpan(n, p, xi, U);
	dersBasisFuns(span, xi, p, n, U, ders);	//	get the derivatives ders[k][j]
	printf("\n\n%2.2f\t%2.20f\t%2.20f\t%2.20f\n",xi, ders[1][0], ders[1][1], ders[1][2]);
		
	free(dN); 
	free2Darray(ders, n+1);
	return 0;
}
Esempio n. 3
0
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    /*	Return the NURBS basis function to matlab

    //
    // We expect the function to be called as [Nip] = NURBSBasis(i, p, xi, knot, weights)
    //
    // p = order of basis (0,1,2 etc)
    // knot = knot vector
    // i = basis function we want (1,2,3 ie. Hughes' notation)
    // xi = the coordinate we wish to evaluate at
    // weights = the weightings for the NURNS function (length(weights) = m - p -1) */

    if(nrhs != 5) mexErrMsgTxt("You fool! You haven't passed in 5 arguments to the function."
                                   "We expect it to be in the form [Nip] = NURBSBasis(i, p, xi, knot, weights)\n");

    int c, k, p, m, i, numKnot, numWeights;

    double *p_in, *m_in, *i_in;
    double *knot, *xi, *weight;

    double tol=100*DBL_EPSILON;

    for(c = 0; c < nrhs; c++)
    {
        switch(c)
        {
        case 0:
            i_in = mxGetPr(prhs[c]);
            i = (int)*i_in -1;
        /*mexPrintf("\n\ni=%d\n", i);*/

        case 1:
            p_in = mxGetPr(prhs[c]);
            p = (int)*p_in;
        /*mexPrintf("\n\np=%d\n", p);*/

        case 2:
            xi = mxGetPr(prhs[c]);
        /*mexPrintf("\nxi=%2.20f\n", *xi);											*/

        case 3:
            knot = mxGetPr(prhs[c]);
            numKnot = mxGetN(prhs[c]);
            m = numKnot - 1;
        /*mexPrintf("\nWe have a knot vector with %d components\n", numKnot);
        for(k = 0; k < numKnot; k++) mexPrintf("%2.2f\t", *(knot+k));
        mexPrintf("\n");*/

        case 4:
            weight = mxGetPr(prhs[c]);
            numWeights = mxGetM(prhs[c]);

        }
    }

    if(fabs(*xi-knot[numKnot-1]) < tol)
        *xi = knot[numKnot-1] - tol;


    /* and call the basis function routine*/

    double Rip, Nip, dRip_dxi;
    double w_interp, dw_interp_dxi ;

    int n = m - p -1;

    double *N = (double *)malloc(sizeof(double)*(p+1));
    double *dN = (double *)malloc(sizeof(double)*(p+1));
    double **ders = init2DArray(p+1, p+1);

    int span = FindSpan(n, p, *xi, knot);
    BasisFuns(span, *xi, p, knot, N);
    dersBasisFuns(span, *xi, p, p, knot, ders);

    w_interp = 0.0;
    dw_interp_dxi = 0.0;

    for(c = 0; c <= p; c++)
    {
        w_interp += N[c] * weight[span-p+c];
        dw_interp_dxi += ders[1][c] * weight[span-p+c];
    }

    dersOneBasisFuns(p, m, knot, i, *xi, p, dN);

    Nip = OneBasisFun(p, m, knot, i, *xi);

    Rip = Nip * weight[i] / w_interp;

    dRip_dxi = weight[i] * ( w_interp * dN[1] - dw_interp_dxi * Nip ) / (w_interp * w_interp);


    free2Darray(ders, (p+1));
    free(N);
    free(dN);

    plhs[0] = mxCreateDoubleScalar(Rip);
    plhs[1] = mxCreateDoubleScalar(dRip_dxi);

}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
	/*	Return the NURBS basis function to matlab
	
	//
	// We expect the function to be called as [interp interp_deriv] = NURBSinterpolation(xi, p, knot, points, weights)
	//
	//	xi = point where we want to interpolate
	//	knot = knot vector
	//	vector of points in format [pt1 pt2 pt3 pt4 .... ptn] */
	
	if(nrhs != 5) mexErrMsgTxt("You fool! You haven't passed in 3 arguments to the function."
								"We expect it to be in the form [interp interp_deriv] = NURBSinterpolation(xi, knot, points)\n");
			
	/* First get the inputs */
	
	double *xi = mxGetPr(prhs[0]);	

	double *p_in = (mxGetPr(prhs[1]));
	int p = (int) *p_in;
	
	double *knot = mxGetPr(prhs[2]);
	int numKnot = mxGetN(prhs[2]);
	int m = numKnot - 1;
	int n = m - p -1;
	
	double *points = mxGetPr(prhs[3]);
	int numPoints = mxGetN(prhs[3]);
	
	double *weight = mxGetPr(prhs[4]);
	int numWeights = mxGetN(prhs[4]);
	
	double tol=100*DBL_EPSILON;
	
	if(fabs(*xi-knot[numKnot-1]) < tol) 
		*xi = knot[numKnot-1] - tol;
	
	/* and evaluate the non-zero basis functions*/
	
	double *N = (double *)malloc(sizeof(double)*(p+1));
	double *NURBS = (double *)malloc(sizeof(double)*(p+1));
	double *NURBS_deriv = (double *)malloc(sizeof(double)*(p+1));
	double **ders = init2DArray(n+1, p+1);
	
	int span = FindSpan(n, p, *xi, knot); 
	BasisFuns(span, *xi, p, knot, N);
	dersBasisFuns(span, *xi, p, n, knot, ders);	
	
	/* and create NURBS approximation */
	int k, c;
	
	for(k = 0; k <=p; k++)
	{
		double w_interp = 0.0, dw_interp_dxi= 0.0;
		for(c = 0; c <= p; c++)
		{
			w_interp += N[c] * weight[span-p+c];
			dw_interp_dxi += ders[1][c] * weight[span-p+c];
		}
		NURBS[k] = N[k] * weight[span-p+k] / w_interp;
		NURBS_deriv[k] =  weight[span-p+k] * ( w_interp * ders[1][k] - dw_interp_dxi * N[k] ) / (w_interp * w_interp);
	}

	double interp = 0.0;
	double interp_deriv = 0.0;
	
	for(c = 0; c <= p; c++)
	{
		interp += NURBS[c] * points[span-p+c];
		interp_deriv += NURBS_deriv[c] * points[span-p+c];
	}
	
	free(N);
	free(NURBS);
	free(NURBS_deriv);
	free2Darray(ders, (n+1));
	
	plhs[0] = mxCreateDoubleScalar(interp); plhs[1] = mxCreateDoubleScalar(interp_deriv);


}
Esempio n. 5
0
// Simple row decomposition version;
int LUDecomp_RD(int* argc, char*** argv){

	
	// Declarations
	int i, j, k, p, rank, procs, n, size;
	double **l, **data, *ptrRow;
	double *ldata, *ll, *wdata;

	// Initialization
	MPI_Init(argc, argv);
	MPI_Comm_rank(MPI_COMM_WORLD, &rank);
	MPI_Comm_size(MPI_COMM_WORLD, &procs);


	if(*argc <= 1){
		if(rank == 0)
			printf("Not enough input you stupid ass!\n");
		MPI_Finalize();
		return -1;
	}
	size = atoi((*argv)[1]);
	
	// Initialized a random matrix
	n = size / procs; // suppose divisible;

	init2DArray(&data, n, size);
	randMatrixGene(&data, n, size, rank);

	init2DArray(&l, n, size);
	initL(&l, n, size, rank);
	
    wdata = (double*) malloc(sizeof(double) * size * size);
    ldata = (double*) malloc(sizeof(double) * n * size);
   	MD2SD(&data, &ldata, n, size);
   	
   	MPI_Gather(ldata, n*size, MPI_DOUBLE, wdata, n*size, MPI_DOUBLE, 0, MPI_COMM_WORLD);
   	
	// Parallelized version
	for(k = 0; k < size - 1; k++){
		ptrRow = (double*) malloc(sizeof(double) * (size - k) );
		
		if( k/n == rank){
			p = k%n;
			for(i = k; i < size; i++){
				ptrRow[i-k] = data[p][i]; // spread this information 
			}
		}
		MPI_Bcast(ptrRow, size-k, MPI_DOUBLE, k/n, MPI_COMM_WORLD); // collective call;
		if( fabs((ptrRow[0])) < 0.0001 ){
			MPI_Finalize();
			return -1;
		}
		
		if(k >= n * (rank+1)) continue;

		int cc = (k/n == rank)? (k%n)+1 : 0;

		#pragma omp parallel for
		for(i = cc; i < n; i++){
			l[i][k] = data[i][k] / ptrRow[0];

			for(j = k; j < size; j++){
				data[i][j] -= l[i][k] * ptrRow[j-k];
			}
		}

		free(ptrRow);
	}
	
	// send data back to process 0;
	// MPI_Gather()
	double *U, *L;
	if(rank == 0){
		U = (double*) malloc(sizeof(double) * size * size);
		L = (double*) malloc(sizeof(double) * size * size);
	}

	
	MD2SD(&data, &ldata, n, size);
	ll = (double*) malloc(sizeof(double) * n * size);
	MD2SD(&l, &ll, n, size);

	MPI_Gather(ldata, size*n, MPI_DOUBLE, U, size*n, MPI_DOUBLE, 0, MPI_COMM_WORLD);
	MPI_Gather(ll, size*n, MPI_DOUBLE, L, size*n, MPI_DOUBLE, 0, MPI_COMM_WORLD);

	//free2DArr(data, n, size);
	//free2DArr(l, n, size);
	
	if(rank == 0){
		printf("Whole data:\n");
		for(i = 0; i < size; i++){
			for(j = 0; j< size; j++){
				printf("%lf ", wdata[i*size+j]);
			}
			printf("\n");
		}
		printf("U:\n");
		for(i = 0; i < size; i++){
			for(j = 0; j< size; j++){
				printf("%lf ", U[i*size+j]);
			}
			printf("\n");
		}
		
		printf("L:\n");
		for(i = 0; i < size; i++){
			for(j = 0; j< size; j++){
				printf("%lf ", L[i*size+j]);
			}
			printf("\n");
		}

		matrixPro(L, U, size, size);

		free(U); free(L);
	}

	free(ll); free(ldata);
	
	MPI_Finalize();
	
	return 1;
}