Example #1
0
// test
void test(Net* net){
    FILE *inf = fopen(TRAIN_PATH1, "rb");
    FILE *outf = fopen(TRAIN_PATH2, "rb");
    
    byte input[INPUT_SIZE];
    byte output[OUTPUT_SIZE];
    fread(input, sizeof(byte), 16, inf);	// trash
    fread(output, sizeof(byte), 8, outf);	// trash
    
    double input_double[INPUT_SIZE];
    double output_double[OUTPUT_SIZE];
    
    double* result;
    
    puts("Testing...");
    for(int i=0; i<TEST_SIZE; i++){
        fread(input, sizeof(byte), INPUT_SIZE, inf);
        fread(output, sizeof(byte), 1, outf);
        for(int l=0; l<INPUT_SIZE; l++)
            input_double[l] = (double)input[l]!=0?1:0;
        for(int l=0; l<OUTPUT_SIZE; l++)
            output_double[l] = (double)((1 << output[0]) & (1 << l))!=0?1:0;
        
        result = net->test(input_double);
        
        error_rate(result, output_double);
    }
}// test
Example #2
0
std::string DHT22::message() 
{
	std::stringstream ss;
	ss << "Hum: "<< humidity()<<" Temp: "<<temperature()
	   << " Total Errors: "<<errors() 
	   << " Error Rate: "<<error_rate()
	   << " Cycle: "<<cycles()<<std::endl;
	return ss.str();
}
Example #3
0
int main(int argc, char *argv[]){
    int mode=0;
	int n = 1;
	int t = 3;
	int max = 10;
	double *A;
    double *b;
	double t1;
	FILE* input;
	int ret = 0;
	opterr = 0;
	while ((ret = getopt( argc, argv, "f::g:N:t:")) != -1) {
		switch (ret) {
		case 'N':
			if (optarg == NULL) return -1;
			max = atoi(optarg);
			printf("максимальный выходной размер: %d\n", max);
			break;
		case 't':
			if (optarg == NULL) return -1;
			t = atoi(optarg);
			break;		
		case 'f':
			mode = 1;
			if (optarg != NULL){
				printf ("ввод из файла %s\n", optarg);
				if ((input = fopen (optarg, "r")) == NULL) {
					perror ("невозможно открыть файл\n");
					return -1;
				}
			} else {
				printf ("ввод из файла input.txt\n");
				if ((input = fopen ("input.txt", "r")) == NULL) {
					perror ("невозможно открыть файл\n");
					return -1;
				}
			}
			break;
		case 'g':
			if (mode == 1) break;
			if (optarg == NULL) return -1;
			printf("ввод из функции\n");
			mode = 2;
			n = atoi(optarg);
			printf("матрица размера: %d\n", n);
			break;
		case '?':
			printf("неизвестная опция -%c\n", optopt);
			printf("поддерживаемые опции:\n");
			printf("f - ввод матрицы из файла, являющегося аргументом, или, если аргумента нет, ввод матрицы из файла input.txt;\n");
			printf("g - ввод матрицы из функции. Аргумент функции (обязателен) является размером матрицы;\n");
			printf("t - количество нитей;\n");
			printf("N - максимальный выходной размер.\n");
			return -1;
		}
	}
	printf("Используется %d нитей.\n", t); 	
	if (mode == 1){
		if(fscanf (input, "%d", &n) == 0){
			printf ("не получилось сосканировать размер матрицы из файла\n");	
			return -1;
		}
		A = new double [n*(n+1)];
		if (A == NULL){
			printf("не удалось выделить память под матрицу А\n");
			return -1;
		}
		if (file_input(input, n, A) != 0){
			return -1;
		}			
		fclose(input);
	} else if (mode == 2){
		A = new double [n*(n+1)];
		if (func_input(A, n) != 0){
			return -1;
		}
	} else {
		printf("Требуется запустить программу с какими-либо параметрами\n");
		printf("Поддерживаемые параметры запуска:\n");
		printf("f - ввод матрицы из файла, являющегося аргументом, или, если аргумента нет, ввод матрицы из файла input.txt;\n");
		printf("g - ввод матрицы из функции. Аргумент функции (обязателен) является размером матрицы;\n");
		printf("t - количество нитей;\n");
		printf("N - опция, аргумент которой (обязателен) задает максимальный выходной размер.\n");
		return -1;
	}
	pthread_t *threads = new pthread_t [t];                     //инициализируем нити, выделяем под них память
	if (threads == NULL){
		printf("не удалось выделить память под массив нитей\n");
		return -1;
	}
	b = new double [n];
	if (b == NULL){
		printf("не удалось выделить память под вектор b\n");
		return -1;
	}
    for (int z=0;z<n;z++){
        b[z] = -A[z*(n+1)+n];
    }
   	int MAX_OUTPUT_SIZE = 10;

   	PrintMatrix(n, A, b, MAX_OUTPUT_SIZE);
	delete []b;
	printf("\n");	
	
	FILE *out;
	out = fopen("output.txt", "wr");
	if(out == NULL){
		printf("не могу открыть выходной файл\n");
		return -1;
	}
	
	double *x = new double [n];                    //сюда пишем решение
	if (x == NULL){
		printf("не удалось выделить память под вектор x\n");
		return -1;
	}
	double *E = new double [(n+1)*(n+1)];          //здесь будут храниться базисы
	if (E == NULL){
		printf("не удалось выделить память под матрицу Е\n");
		return -1;
	}
	solve *args = new solve [t];                   //массив структур для нитей
	if (args == NULL){
		printf("не удалось выделить память под аргументы нитей\n");
		return -1;
	}
	double *v = new double [n+1];                  //сюда запоминается вектор для master
	if (v == NULL){
		printf("не удалось выделить память под вектор v\n");
		return -1;
	}
	printf("\nрешение системы...\n");
	t1 = get_full_time();                         //get_full_time для счета астрономического времени
	Solve(n,A,x,t,threads,E,args,v);
	t1 = get_full_time() - t1;
	
	Nevyaska * arg = new Nevyaska [t];
	if (args == NULL){
		printf("не удалось выделить память под структуру значений для невязки\n");
		return -1;
	}
	printf("\n");
	file_output(out,n,max,x);
	printf("время решения системы = %f\n", t1);
	printf("\nподсчет невязки...\n");
	printf("невязка = %e\n", nev(n,A,x,t,threads,arg));
	if (mode == 2){
		printf("норма погрешности = %e\n", error_rate(n,x));
	}	
	delete []A;
	delete []x;
	delete []E;
	delete []v;
	delete []args;
	delete []threads;
	delete []arg;
	fclose(out); 
	return 0;
}
Example #4
0
int main(int argc, char *argv[]) {
    int q = 0;
    int n = 1;
    int max = 10;
    double t1,t2;
    int max_matrix_size = 10;
    double *A;
    double *b;
    double *A1;
    FILE* f;
    int ret = 0;
    opterr = 0;
    while ((ret = getopt( argc, argv, "f::g:N:")) != -1) {
        switch (ret) {
        case 'N':
            max = atoi(optarg);
            printf("Max output size: %d\n", max);
            break;
        case 'f':
            q = 1;
            if (optarg != NULL) {
                printf ("Input from file %s\n", optarg);
                if ((f = fopen (optarg, "r")) == NULL) {
                    perror ("Can't open file.\n");
                    return -1;
                }
            } else {
                printf ("Input from file input.txt\n");
                if ((f = fopen ("input.txt", "r")) == NULL) {
                    perror ("Can't open file.\n");
                    return -1;
                }
            }
            break;
        case 'g':
            printf("Input from function in file input_function.cpp\n");
            q = 2;
            n = atoi(optarg);
            printf("Size of matrix: %d\n", n);
            break;
        case '?':
            printf("Unknown option -%c\n", optopt);
            printf("Supported options:\n");
            printf("-f -- Input matrix from the argument file, or, if there is no argument, input matrix from the file input.txt;\n");
            printf("-g -- Input matrix from the function. Argument of this option means size of matrix and it is required;\n");
            printf("-N -- Maximum size of the solution, displayed on the screen. Argument is required.\n");
            return -1;
        }
    }
    if (q == 1) {
        if(fscanf (f, "%d", &n) == 0) {
            printf ("Problem with size of matrix.\n");
            return -1;
        }
        A = new double [n*(n+1)];
        b = new double [n];
        A1 = new double [n*n];
        if (A == NULL || b == NULL || A1 == NULL) {
            printf ("Problem with memory allocation.\n");
            return -1;
        }
        if(file_input(f, n, A, A1, b) != 0) {
            return -1;
        }
        fclose(f);
    } else if (q == 2) {
        A = new double [n*(n+1)];
        b = new double [n];
        if (A == NULL || b == NULL) {
            printf ("Problem with memory allocation.\n");
            return -1;
        }
        if (func_input(A, b, n) != 0) {
            return -1;
        }
    } else {
        printf("It's recommended to run the program with boot options\n");
        printf("Supported options:\n");
        printf("-f -- Input matrix from the argument file, or, if there is no argument, input matrix from the file input.txt;\n");
        printf("-g -- Input matrix from the function. Argument of this option means size of the matrix and it's required;\n");
        printf("-N -- Maximum size of the solution, displayed on the screen. Argument is required.\n");
        printf ("\n\n");
        printf("Choose, where to take the matrix from:\n");
        printf("1 -- from file input.txt\n");
        printf("2 -- from input function in file input_function.cpp\n");
        scanf("%d", &q);
        if (q == 1) {
            f = fopen("input.txt","r");
            if(f == NULL) {
                printf("Can't open input file.\n");
                return -1;
            }
            if(fscanf (f, "%d", &n) == 0) {
                printf("Problem with size of matrix.\n");
                return -1;
            }
            A = new double [n*(n+1)/2];
            b = new double [n];
            A1 = new double [n*n];
            if (A == NULL || b == NULL || A1 == NULL) {
                printf ("Problem with memory allocation.\n");
                return -1;
            }
            if(file_input(f, n, A,A1, b) != 0) {
                return -1;
            }
            fclose(f);
        } else if (q == 2) {
            printf("Size of matrix: ");
            scanf("%d", &n);
            A = new double [n*(n+1)/2];
            b = new double [n];
            if (A == NULL || b == NULL) {
                printf ("Problem with memory allocation.\n");
                return -1;
            }
            if (func_input(A, b, n) != 0) {
                return -1;
            }
        } else {
            printf("Incorrect input way.\n");
            return -1;
        }
    }
    FILE *g;
    g = fopen("output.txt", "wr");
    if(g == NULL) {
        printf("Can't open output file.\n");
        return -1;
    }

    PrintMatrix(n,A,b,max_matrix_size);

    double *Y = new double [n];
    t1 = get_time();

    Solve(A,b,n,Y);

    t2 = get_time();

    file_output(g,n,max,Y);
    printf("Time = %f\n", t2-t1);
    if (q == 1) {
        printf("Residual norm = %e\n", residual_norm1(n,A1,b,Y));
        delete []A1;
    } else if(q == 2) {
        printf("Residual norm = %e\n", residual_norm(n,b,Y));
        printf("Error rate = %e\n", error_rate(n,Y));
    }
    delete []A;
    delete []b;
    delete []Y;
    fclose(g);
    return 0;
}