コード例 #1
0
ファイル: memory.c プロジェクト: JackHolland/Tap
/*! A helper function for freeExpr and freeExprNR
    @param expr     the expression to free from memory
    @param next     whether to free the next expression as well as the given one
    @return         0
*/
static bool freeExpr_ (expression* expr, bool next) {
    if (expr != NULL) { // if the expression isn't null
        exprvals ev = expr->ev;
        switch (expr->type) { // depending on the expression's type
            case TYPE_EXP:
                freeExpr(ev.expval); // recursively call this function with the expression's child expression
                break;
            case TYPE_LAZ:
                freeLaz(ev.lazval);
                break;
            case TYPE_STR:
                freeStr(ev.strval);
                break;
            case TYPE_ARR:
                freeArr(ev.arrval);
                break;
            case TYPE_OBJ:
            	freeObj(ev.objval);
            case TYPE_FUN:
                freeFun(ev.funval);
                break;
        }
        if (next) { // if the next expression should be freed
            freeExpr(expr->next); // recursively call this function with the expression's next expression
        }
        free(expr); // free the expression itself
    }
    
    return 0;
}
コード例 #2
0
ファイル: main.c プロジェクト: VVBondarenko/FDM-Solver
int main()
{
	double /* **U, **Unew,err=0.0,*/ sta = 1.0/8.0, stb=1.0/128.0;
	int sa = 9, sb = 129, i/*,j*/;
	FILE *outs;
	init(&U,sa,sa);
	init(&Unew,sa,sa);
	init(&Ub,sb,sb);
	
	init_cond(&U, sa, sa, sta,sta);
	
	memcpy(&Unew,&U,sizeof(Unew));
	for(i = 0; i<1000 /*&& (err>0.001 || err == 0.0)*/; i++)
	{
		//err = 0.0;
		iteration(&U,&Unew, sa,sa, sta,sta);
		memcpy(&U,&Unew,sizeof(Unew));
	}
	
	outs = fopen("../output/out1.dat", "w");
	output_line(U, sa,sa, sta,sta, outs);
	fclose(outs);
	
	//init_cond(&Ub, sb, sb, stb,stb);
	interpol(&Ub, sb, sb, stb, stb, 	&U, sa-1, sa-1, sta, sta);
	init_cond(&Ub, sb, sb, stb, stb);
	
	outs = fopen("../output/out2.dat", "w");
	output_line(Ub, sb,sb, stb,stb, outs);
	fclose(outs);
	
	freeArr(&Unew, sa);
	//output_line(U, sa,sa, sta,sta);
	//output_line(Ub, sb,sb, stb,stb);
	//freeArr(&U, sa);
	init(&Unew,sb,sb);
	init_cond(&Unew, sb, sb, stb, stb);
	memcpy(&Unew,&Ub,sizeof(Ub));
	//err = 0.0;
	for(i = 0; i<1000 /*&& (err>0.0005 || err == 0.0)*/ ; i++)
	{
		iteration(&Ub,&Unew, sb,sb, stb,stb);
		memcpy(&Ub,&Unew,sizeof(Unew));
	}
	
	outs = fopen("../output/out3.dat", "w");
	output_line(Ub, sb,sb, stb,stb, outs);
	fclose(outs);
	
	
	outs = fopen("../output/out3_mstk.dat", "w");
	opt_mstk_line(Ub, sb,sb, stb,stb, outs);
	fclose(outs);
	//output_line(U, size,size, step,step);

	//printf("%f %f %f\n", step*5, step*5, err);
	return 0;
}
コード例 #3
0
int main(void)
{
	char buf[255];
	int count;
	char **chbuffer;
	if((chbuffer=charArrAlloc(25,255))==NULL)
	{
		return 1;
	}
	while(fgets(buf,255,stdin)!=NULL)
	{
		count=stringtoarray(buf,255,chbuffer,25,255,'\n');
		printfstrarr(chbuffer,count);
	}
	freeArr(chbuffer,25);
	return 0;
}
コード例 #4
0
ファイル: single.c プロジェクト: thongbkvn/Project2
int main(int argc, char** argv)
{
    loadConfig(CONFIG_FILE);

    indexMovie();

    if (!hasSimMatrix)
    {
	readData(&A, nMovies, nUsers, trainSet);
    
#ifdef DEBUG
	readArr(A, nMovies, nUsers);
#endif



    
	/*        CHUAN HOA       */
	printf("\n\nChuan hoa: ");
	for (int i=0; i<nMovies; i++)
	{
	    float sum = 0; //Tong cac rating
	    int t = 0;     //So rating
	    
	    for (int j=0; j<nUsers; j++)
	    {
		if (A[i][j] != 0)
		{
		    sum += A[i][j];
		    t++;
		}
	    }

	    float rowMean = 0;
	    if (t!=0)
		rowMean = sum/t;
	    
	    for (int j=0; j<nUsers; j++)
	    {
		if (A[i][j] != 0)
		    A[i][j] -= rowMean;
	    }

	    printf("\r\%-10d/%10d", i, nMovies);
	}


#ifdef DEBUG
	puts("");
	printf("\nSTANDARDIZED MARTRIX: \n");
	readArr(A, nMovies, nUsers);
	puts("");
#endif



	/*         TINH SIMILARY MATRIX           */
	printf("\n\nTinh sim: ");    sim = calloc(nMovies, sizeof(float*));
	for (int i=0; i<nMovies; i++)
	    sim[i] = calloc(nMovies, sizeof(float*));

	for (int i=0; i<nMovies; i++)
	{
	    for (int j=i+1; j<nMovies; j++)
	    {
		sim[i][j] = cosine(A[i], A[j], nUsers);
	    }
	    printf("\r%-10d/%10d", i, nMovies);
	}

	for (int i=0; i<nMovies; i++)
	    for (int j=0; j<=i; j++)
	    {
		if (i == j)
		    sim[i][j] = 1;
		else
		    sim[i][j] = sim[j][i];
	    }

	writeDataToFile(sim, nMovies, nMovies, pathToSimMatrix);
	freeArr(A, nMovies);
    }
コード例 #5
0
ファイル: cf.c プロジェクト: thongbkvn/Project2
int main(int argc, char** argv)
{
    loadConfig(CONFIG_FILE);

    if (!hasSimMatrix)
    {
	readData(&A, nMovies, nUsers, trainSet);
    
#ifdef DEBUG
	readArr(A, nMovies, nUsers);
#endif

	omp_set_num_threads(MAX_THREAD);


    
	/*        CHUAN HOA       */
	printf("\n\nChuan hoa: ");
#pragma omp parallel for
	for (int i=0; i<nMovies; i++)
	{
	    float sum = 0; //Tong cac rating
	    int t = 0;     //So rating
	    
#pragma omp parallel for schedule(static, 3) reduction(+:sum)
	    for (int j=0; j<nUsers; j++)
	    {
		if (A[i][j] != 0)
		{
		    sum += A[i][j];
		    t++;
		}
	    }

	    float rowMean = sum/t;
	    
#pragma omp parallel for schedule(static, 3)
	    for (int j=0; j<nUsers; j++)
	    {
		if (A[i][j] != 0)
		    A[i][j] -= rowMean;
	    }

	    printf("\n%d/%d", i, nMovies);
	}


#ifdef DEBUG
	puts("");
	printf("\nSTANDARDIZED MARTRIX: \n");
	readArr(A, nMovies, nUsers);
	puts("");
#endif



	/*         TINH SIMILARY MATRIX           */
	printf("\n\nTinh sim: ");    sim = calloc(nMovies, sizeof(float*));
	for (int i=0; i<nMovies; i++)
	    sim[i] = calloc(nMovies, sizeof(float*));

#pragma omp parallel for schedule(static, 3)
	for (int i=0; i<nMovies; i++)
	{
#pragma omp parallel for schedule(static, 3)
	    for (int j=i+1; j<nMovies; j++)
	    {
		sim[i][j] = cosine(A[i], A[j], nUsers);
	    }
	    printf("\n%d/%d: ", i, nMovies);
	}

	for (int i=0; i<nMovies; i++)
	    for (int j=0; j<=i; j++)
	    {
		if (i == j)
		    sim[i][j] = 1;
		else
		    sim[i][j] = sim[j][i];
	    }

	writeDataToFile(sim, nMovies, nMovies, pathToSimMatrix);
	freeArr(A, nMovies);
    }
    else
    {
	int tmp;
	loadArrFromFile(&sim, &tmp, &tmp, pathToSimMatrix);
    }
    
#ifdef DEBUG
    puts("");
    printf("\nSIMILARY MATRIX: \n");
    readArr(sim, nMovies, nMovies);
    puts("");
#endif


    
    /*          TINH RATING            */
    Rating *B;
    int len;
    readTestData(&B, &len, testSet);
    readData(&A, nMovies, nUsers, trainSet);
    float *AVG;
    computeAvgRating(&AVG, A, nMovies, nUsers);

    
#pragma omp parallel for schedule(static, 3)
    for (int i=0; i<len; i++)
    {
	ratePredict(&B[i], A, sim);
	//Neu khong the du doan thi dua ve gia tri trung binh
	if (B[i].P == 0)
	    B[i].P = AVG[B[i].uID];
    }

    writeResultToFile(B, len, "data/result.txt");

    float T = 0;
    float temp;
#pragma omp parallel for schedule(static, 3) reduction(+:T) private(temp)
    for (int i=0; i<len; i++)
    {
	temp = B[i].R - B[i].P;
	T += temp * temp;
    }

    printf("\n %f", sqrt(T/len));
    
#ifdef DEBUG
    puts("");
    printf("RESULT: ");
    for (int i=0; i<len; i++)
    {
	printf("\nmID: %5d    uID: %5d     Real: %6.2fd      Predict: %6.2f", B[i].mID, B[i].uID, B[i].R, B[i].P);
	usleep(1000000);
    }
    puts("");
#endif

    


    free(AVG);
    freeArr(sim, nMovies);
    free(B);
    freeArr(A, nMovies);
    puts("");
    return 0;
}