Beispiel #1
0
int main(void)
{
    int matrix[3][5] = {{ 1, 2, 3, 4, 5},
        { 11, 12, 13, 14,15},
        { 21, 22, 23, 24, 25}
    };

    void scalar_multiply(int matrix[3][5], int scalar);
    void display_matrix(int matrix[3][5]);

    printf("Original Matrix:\n");
    display_matrix(matrix);

    scalar_multiply(matrix, 2);

    printf("\nMultiplied by 2:\n");
    display_matrix(matrix);

    scalar_multiply(matrix, -1);

    printf("\nMultiplied by -1:\n");
    display_matrix(matrix);

    return 0;
}
Beispiel #2
0
void main()
{
	int row1, column1, row2, column2;
	int matrix1[100][100], matrix2[100][100], final_matrix[100][100];
	printf("Enter number of rows and columns of matrix 1\n");
	scanf_s("%d%d", &row1, &column1);
	printf("Enter elements of matrix 1\n");
	read_matrix(row1, column1, matrix1);
	printf("Enter number of rows and columns of matrix 2\n");
	scanf_s("%d%d", &row2, &column2);
	if (column1 != row2)
		printf("Matrices cannot be multiplied \n");
	else
	{
		printf("Enter elements of matrix 2\n");
		read_matrix(row2, column2, matrix2);
	}
	printf("Matrix 1  is as follows \n");
	display_matrix(row1, column1, matrix1);
	printf("Mtrix 2 is as follows \n");
	display_matrix(row2, column2, matrix2);
	multiply_matrix(row1, column1, matrix1, row2, column2, matrix2, final_matrix);
	printf("Resultant Matrix is as follows \n");
	display_matrix(row1, column2, final_matrix);
	getchar();
	getchar();
	getchar();
}
Beispiel #3
0
int main(void) {
  char cmd_buff[MAX_CMD_LENGTH];
  init();

  init_matrix(4, 4);
  print_help();
  display_matrix();
  wrefresh(display_wnd);
  while (true) {
    int wy, wx;
    getyx(work_wnd, wy, wx);
    wgetstr(work_wnd, cmd_buff);
    wclear(display_wnd);
    wborder(display_wnd,'|','|','=','=','*','*','*','*');
    if (wy > WORK_HEIGHT - 3)
      wclear(work_wnd);
    eval(cmd_buff);
    if (matrix != NULL)
      display_matrix();

    wrefresh(display_wnd);
    wrefresh(work_wnd);
  }
  finish();
  return 0;
}
Beispiel #4
0
int main(void){
	
	int choice;
	char done;
	printmenu();
	printf("play??\n");
	scanf("%d, &choice");
	if(choice == 2)
		return;
	done =  ' ';
	init_matrix();

	do{
        	display_matrix();
        	player_move();
  		/* see if player is winner */
    		done = check(); 
    		if(done != ' ') 
			break; /* player is winner!*/
    		computer_move();
    		done = check(); /* see if computer is winner */	
	} 
	while(done == ' ');
	if(done == 'X') 
   		printf("-----YOU WON!!!-----\n");
        else{
		printf("-----I WON!!!!-----\n");
		printf("-----OOPS!!! YOU LOSE...TRY NEXT TIME-----\n");
		printmenu();
	}
   		display_matrix(); /* show final positions */

        return 0;
}
Beispiel #5
0
void tispellb_state::prepare_display()
{
	// almost same as snspell
	UINT16 gridmask = (m_display_decay[15][16] != 0) ? 0xffff : 0x8000; // vfd filament on/off
	set_display_segmask(0xff, 0x3fff);
	display_matrix(16+1, 16, m_plate | 1<<16, m_grid & gridmask);
}
Beispiel #6
0
void tispellb_state::prepare_display()
{
	// almost same as snspell
	u16 gridmask = vfd_filament_on() ? 0xffff : 0x8000;
	set_display_segmask(0xff, 0x3fff);
	display_matrix(16+1, 16, m_plate | 1<<16, m_grid & gridmask);
}
Beispiel #7
0
void fidel68k_state::eag_prepare_display()
{
	// 8*7seg leds, (8+1)*8 chessboard leds
	UINT8 seg_data = BITSWAP8(m_7seg_data,0,1,3,2,7,5,6,4);
	set_display_segmask(0x1ef, 0x7f);
	display_matrix(16, 9, m_led_data << 8 | seg_data, m_inp_mux);
}
Beispiel #8
0
void fidel68k_state::eag_prepare_display()
{
	// Excel 68000: 4*7seg leds, 8*8 chessboard leds
	// EAG: 8*7seg leds(2 panels), (8+1)*8 chessboard leds
	uint8_t seg_data = BITSWAP8(m_7seg_data,0,1,3,2,7,5,6,4);
	set_display_segmask(0x1ff, 0x7f);
	display_matrix(16, 9, m_led_data << 8 | seg_data, m_inp_mux);
}
Beispiel #9
0
void Main_win::load_clicked()
{
  try
  {
    QString q_form = QFileDialog::getOpenFileName(this, tr("Load Matrix"));

    std::ifstream f;
    f.open(q_form.toStdString(), std::ios_base::in);

    if(f.is_open())
    {
      int row = 0;
      int col = 0;
      char c = 0;
      bool flag = true;

      while(f.get(c))
      {
        if(flag && (c == ' '))
          col++;
        if(c == '\n')
        {
          row++;
          flag = false;
        }
      }

      f.clear();
      f.seekg(0, f.beg);

      std::istream_iterator<double> ii{f};
      std::istream_iterator<double> eos{};
      std::vector<double> v{ii, eos};

      f.close();

      remove_matrix();

      build_matrix(row, col);

      mat_dim_.first = row;
      mat_dim_.second = col;

      mat_dim_tmp_.first = row;
      mat_dim_tmp_.second = col;

      display_matrix(row, col, v);
    }
    else throw std::ios_base::failure("cannot open file");
  }
  catch(std::exception& e)
  {
    e.what();
    to_display(e.what());
  }
}
Beispiel #10
0
/**
 * \brief perform a gauss experiment with n x n matrix
 *
 * \param n	dimension of the matrix the inverse
 */
void	experiment(int n) {
	/* create a system to solve */
	a = random_matrix(n, 2 * n);

	/* display the matrix */
	if (n <= 10) {
		display_matrix(stdout, a, n, 2 * n);
	}

	/* perform the Gauss algorithm */
	gauss();

	/* display the matrix */
	if (n <= 10) {
		display_matrix(stdout, a, n, 2 * n);
	}

	free(a);
}
Beispiel #11
0
//convert the ray to the matrix visualisation
void ray_of_ints_to_matrix_picture(struct ray* r){
    int mtx_rows = r->length;
    // TODO: change mtx_cols to max int in array (within reason ...)
    int mtx_cols = r->length;
    printf("%i\n", r->length);
    char m[mtx_rows][mtx_cols];
    populate_matrix(mtx_rows,mtx_cols, m, r->array, UNIT, BLANK);
    display_matrix(mtx_rows,mtx_cols, m);
    
}
Beispiel #12
0
int main()
{
    int N;
    scanf("%d",&N);
    int *matrix_a = (int *)malloc(N * N * sizeof(int));
    int *matrix_b = (int *)malloc(N * N * sizeof(int));
    int *matrix_c = (int *)malloc(N * N * sizeof(int));
    read_matrix(matrix_a, N);
    read_matrix(matrix_b, N);
    multiply_matrix_3(matrix_a, matrix_b, matrix_c, N);
    display_matrix(matrix_c, N);
}
Beispiel #13
0
void Main_win::display_result(const Matrix& m)
{
  remove_matrix();
  build_matrix(m.rows(), m.cols());

  mat_dim_.first = m.rows();
  mat_dim_.second = m.cols();

  mat_dim_tmp_.first = m.rows();
  mat_dim_tmp_.second = m.cols();

  display_matrix(m);
}
void get_inputs_and_solve(int* p_sle_solver){
    Matrix *matrix, *initial_guess, *solution_matrix, *roots;
    int rows, columns, single_column = 1, iter, error, error_code = 0;

    get_row_col(&rows, &columns,
                "Enter no. of rows of coefficient matrix: ",
                "Enter no. of columns of coefficient matrix: ",
                "Error! Coefficient matrix is not a square matrix. Try again.\n",
                is_square_matrix);
    
    printf("\n");
    matrix = get_input_matrix(&rows, &columns, 1, 1, 'A');
    display_matrix(matrix, "Coefficient Matrix A: \n");
    
    printf("\nEnter solution matrix values: \n");
    solution_matrix = get_input_matrix(&rows, &single_column, 0, 0, 'C');
    display_matrix(solution_matrix, "\nSolution Matrix C: \n");
    
    printf("\nEnter unknown matrix initial guesses: \n");
    initial_guess = get_input_matrix(&rows, &single_column, 0, 0, 'X');
    display_matrix(initial_guess, "\nInitial Unknown Matrix X: \n");
    
    get_iter_error(&iter, &error);
    
    roots = iterative_method(p_sle_solver, matrix, initial_guess, 
                             solution_matrix, &iter, &error, display_iteration, &error_code);
    
    if(0 == roots && error_code == 1)
        printf("Division by zero.\n");
    else{
        printf("The unknowns of the system of equations are:\n\n");
        display_roots(roots, 'x');
        mtx_delete_matrix(roots);
    }
    
    mtx_delete_matrix(matrix);
    mtx_delete_matrix(solution_matrix);
    mtx_delete_matrix(initial_guess);
}
Beispiel #15
0
void display_print(uint8_t column) {
    if (column == 2)display_matrix(column, ~(pattern[encode(print_char)][0]));
    else if (column == 3)display_matrix(column, ~(pattern[encode(print_char)][1]));
    else if (column == 4)display_matrix(column, ~(pattern[encode(print_char)][2]));
    else if (column == 5)display_matrix(column, ~(pattern[encode(print_char)][3]));
    else if (column == 6)display_matrix(column, ~(pattern[encode(print_char)][4]));
    else display_matrix(column, 0x00);
}
Beispiel #16
0
void			solve(t_tet *lst)
{
	t_matrix	matrix;
	int			len;

	len = nk_sqrt(list_count(lst));
	matrix = create_matrix(len, len);
	while ((matrix.map = recurse(matrix, lst, return_pos(0, 0))) == NULL)
	{
		matrix = create_matrix(len, len);
		len++;
	}
	display_matrix(matrix);
	free_lst(lst);
	free_matrix(matrix.map, matrix.size_y);
}
Beispiel #17
0
int main(int argc, char** argv)
{
    if (argc < 3) {
        puts("Nombre de parametres insuffisants");
        return 1;
    }

    size_a = strlen(argv[1]);
    size_b = strlen(argv[2]);

//    int size_lcs = get_size_of_lcs(argv[1], argv[2], 0, 0);

//    printf("%d\n", size_lcs);
    char** matrix = build_path(argv[1], argv[2]);
    display_matrix(matrix, size_a+2, size_b+2);
    print_LCS(matrix, argv[1], argv[2], size_a, size_b);

    return 0;
}
Beispiel #18
0
void display_scroll(uint8_t column) {
    if (scroll_pointer + column < DISPLAY_WIDTH || scroll_pointer + column >= DISPLAY_WIDTH + scroll_char_pointer * 6 || (scroll_pointer + column - DISPLAY_WIDTH) % 6 == 5)display_matrix(column, 0x00);
    else display_matrix(column, ~(pattern[encode(scroll_char[(scroll_pointer + column - DISPLAY_WIDTH) / 6])][(scroll_pointer + column - DISPLAY_WIDTH) % 6]));
}
Beispiel #19
0
int main(int argc, char *argv[]){

    double mmvalues[] = {2.0,3.0,1.0,1.0,4.0,2.0,2.0,1.0,2.0,1.0,4.0,2.0,2.0,3.0,1.0,1.0,4.0,2.0,2.0,1.0,2.0,1.0,4.0,2.0,5.0,4.0,2.0,2.0,1.0,2.0,\
                         4.0,2.0,2.0,1.0,2.0,1.0,6.0,3.0,1.0,1.0,4.0,2.0,2.0,1.0,9.0,1.0,4.0,2.0,5.0,4.0,2.0,2.0,1.0,2.0,5.0,5.0,2.0,2.0,1.0,2.0};

    matrix_t mm, ma, mb, mc, md, mx, my, mz;
    init_matrix(&mm, 'm', 6, 6, mmvalues);
    init_matrix(&ma, 'a', 3, 3, NULL);
    init_matrix(&mb, 'b', 3, 3, NULL);
    init_matrix(&mc, 'c', 3, 3, NULL);

    display_matrix(&mm);
    display_matrix(&ma);
    display_matrix(&mb);
    display_matrix(&mc);

    free_matrix(&ma);
    ma = get_submatrix(&mm, 2, 1, 4, 4);
    display_matrix(&ma);

    free_matrix(&mb);
    mb = get_submatrix(&mm, 3, 2, 4, 4);
    display_matrix(&mb);

    mx = matrix_mul(&ma, &mb, 'X');
    my = square_matrix_mul_recursive(&ma, &mb, 'Y');
    mz = square_matrix_mul_strassen(&ma, &mb, 'Z');

    printf("the result is \n");

    display_matrix(&mx);
    display_matrix(&my);
    display_matrix(&mz);

    ma = get_submatrix(&mm, 1, 2, 6, 1);
    mb = get_submatrix(&mm, 2, 1, 1, 6);

    display_matrix(&ma);
    display_matrix(&mb);

    mc = matrix_mul(&mb, &ma, '1');
    display_matrix(&mc);

    md = matrix_mul(&ma, &mb, 'S');
    display_matrix(&md);

    free_matrix(&mc);
    mc = matrix_scale(&md, 2, '2');
    display_matrix(&mc);

    free_matrix(&ma);
    free_matrix(&mb);
    free_matrix(&mc);
    free_matrix(&md);
    free_matrix(&mx);
    free_matrix(&my);
    free_matrix(&mz);

    return 0;
}//main
Beispiel #20
0
/*
* PURPOSE: Runs various commands for the operations to be performed on the matrices
* INPUTS: command array, matrix array, size of matrix array
* RETURN: Nothing
**/
void run_commands (Commands_t* cmd, Matrix_t** mats, unsigned int num_mats) {
	if(cmd == NULL || mats == NULL || num_mats <= 0)
		return;


	/*Parsing and calling of commands*/
	if (strncmp(cmd->cmds[0],"display",strlen("display") + 1) == 0
		&& cmd->num_cmds == 2) {
			/*find the requested matrix*/
			int idx = find_matrix_given_name(mats,num_mats,cmd->cmds[1]);
			if (idx >= 0) {
				display_matrix (mats[idx]);
			}
			else {
				printf("Matrix (%s) doesn't exist\n", cmd->cmds[1]);
				return;
			}
	}
	else if (strncmp(cmd->cmds[0],"add",strlen("add") + 1) == 0
		&& cmd->num_cmds == 4) {
			int mat1_idx = find_matrix_given_name(mats,num_mats,cmd->cmds[1]);
			int mat2_idx = find_matrix_given_name(mats,num_mats,cmd->cmds[2]);
			if (mat1_idx >= 0 && mat2_idx >= 0) {
				Matrix_t* c = NULL;
				if( !create_matrix (&c,cmd->cmds[3], mats[mat1_idx]->rows,
						mats[mat1_idx]->cols)) {
					printf("Failure to create the result Matrix (%s)\n", cmd->cmds[3]);
					return;
				}

				if(add_matrix_to_array(mats,c, num_mats) == -1){
					printf("Failed to add matrix to the array");
					return;
				}


				if (! add_matrices(mats[mat1_idx], mats[mat2_idx],c) ) {
					printf("Failure to add %s with %s into %s\n", mats[mat1_idx]->name, mats[mat2_idx]->name,c->name);
					return;
				}
			}
	}
	else if (strncmp(cmd->cmds[0],"duplicate",strlen("duplicate") + 1) == 0
		&& cmd->num_cmds == 3 && strlen(cmd->cmds[1]) + 1 <= MATRIX_NAME_LEN) {
		int mat1_idx = find_matrix_given_name(mats,num_mats,cmd->cmds[1]);
		if (mat1_idx >= 0 ) {
				Matrix_t* dup_mat = NULL;
				if( !create_matrix (&dup_mat,cmd->cmds[2], mats[mat1_idx]->rows,
						mats[mat1_idx]->cols)) {
					return;
				}

				if(duplicate_matrix (mats[mat1_idx], dup_mat) == false){
				 	printf("Duplication Failed\n");
					return;
				}

				if(add_matrix_to_array(mats,dup_mat,num_mats) == -1){
					printf("Failed to add matrix to array\n");
					return;
				}
				printf ("Duplication of %s into %s finished\n", mats[mat1_idx]->name, cmd->cmds[2]);
		}
		else {
			printf("Duplication Failed\n");
			return;
		}
	}
	else if (strncmp(cmd->cmds[0],"equal",strlen("equal") + 1) == 0
		&& cmd->num_cmds == 3) {
			int mat1_idx = find_matrix_given_name(mats,num_mats,cmd->cmds[1]);
			int mat2_idx = find_matrix_given_name(mats,num_mats,cmd->cmds[2]);
			if (mat1_idx >= 0 && mat2_idx >= 0) {
				if ( equal_matrices(mats[mat1_idx],mats[mat2_idx]) ) {
					printf("SAME DATA IN BOTH\n");
				}
				else {
					printf("DIFFERENT DATA IN BOTH\n");
				}
			}
			else {
				printf("Equal Failed\n");
				return;
			}
	}
	else if (strncmp(cmd->cmds[0],"shift",strlen("shift") + 1) == 0
		&& cmd->num_cmds == 4) {
		int mat1_idx = find_matrix_given_name(mats,num_mats,cmd->cmds[1]);
		const int shift_value = atoi(cmd->cmds[3]);
		if (mat1_idx <= 0 ) {
			printf("Matrix shift failed\n");
			return;
				}

		if(bitwise_shift_matrix(mats[mat1_idx],cmd->cmds[2][0], shift_value) == false){
				printf("Matrix shift failed\n");
				return;
		}
		else
			printf("Matrix (%s) has been shifted by %d\n", mats[mat1_idx]->name, shift_value);

		}
	else if (strncmp(cmd->cmds[0],"read",strlen("read") + 1) == 0
		&& cmd->num_cmds == 2) {
		Matrix_t* new_matrix = NULL;
		if(! read_matrix(cmd->cmds[1],&new_matrix)) {
			printf("Read Failed\n");
			return;
		}

		if(add_matrix_to_array(mats,new_matrix, num_mats) == -1){
			printf("Failed to add matrix to the array");
			;
		}
		printf("Matrix (%s) is read from the filesystem\n", cmd->cmds[1]);
	}
	else if (strncmp(cmd->cmds[0],"write",strlen("write") + 1) == 0
		&& cmd->num_cmds == 2) {
		int mat1_idx = find_matrix_given_name(mats,num_mats,cmd->cmds[1]);
		if(! write_matrix(mats[mat1_idx]->name,mats[mat1_idx])) {
			printf("Write Failed\n");
			return;
		}
		else {
			printf("Matrix (%s) is wrote out to the filesystem\n", mats[mat1_idx]->name);
		}
	}
	else if (strncmp(cmd->cmds[0], "create", strlen("create") + 1) == 0
		&& strlen(cmd->cmds[1]) + 1 <= MATRIX_NAME_LEN && cmd->num_cmds == 4) {
		Matrix_t* new_mat = NULL;
		const unsigned int rows = atoi(cmd->cmds[2]);
		const unsigned int cols = atoi(cmd->cmds[3]);

		if(create_matrix(&new_mat,cmd->cmds[1],rows, cols) == false){
			printf("Failed to create the matrix");
			return;
		}

		if(add_matrix_to_array(mats,new_mat,num_mats) == -1){
			printf("Failed to add matrix to the array");
			return;
		}
		printf("Created Matrix (%s,%u,%u)\n", new_mat->name, new_mat->rows, new_mat->cols);
	}
	else if (strncmp(cmd->cmds[0], "random", strlen("random") + 1) == 0
		&& cmd->num_cmds == 4) {
		int mat1_idx = find_matrix_given_name(mats,num_mats,cmd->cmds[1]);
		const unsigned int start_range = atoi(cmd->cmds[2]);
		const unsigned int end_range = atoi(cmd->cmds[3]);
		if(random_matrix(mats[mat1_idx],start_range, end_range) == false){
		 	printf("Failed to randomize\n");
			return;//TODO ERROR CHECK NEEDED
		}

		printf("Matrix (%s) is randomized between %u %u\n", mats[mat1_idx]->name, start_range, end_range);
	}
	else {
		printf("Not a command in this application\n");
	}

}
void SyntopReducer::reduce(HadoopPipes::ReduceContext& context) { // NOLINT
  boost::split(temp_string_components_, context.getInputKey(),
               boost::is_any_of("_"));

  float sum = 0;
  while (context.nextValue()) {
    sum += HadoopUtils::toFloat(context.getInputValue());
  }



  if (boost::starts_with(temp_string_components_[0], "gamma")) {
    context.emit(context.getInputKey(), boost::lexical_cast<string>(sum));
  } else if (boost::starts_with(temp_string_components_[0], "lhood")) {
    // sum += GlobalLikelihoodTerm();
    sum += GlobalWeightTerm(vars_->nu_.get(),
                            vars_->beta_.get(),
                            params_->alpha_trans(),
                            params_->alpha_top(),
                            params_->finite());

    context.emit(context.getInputKey(), boost::lexical_cast<string>(sum));
  } else {
    if (boost::starts_with(temp_string_components_[2], "~")) {
      // cout << "optimizing" << endl;

      Optimize();
      Emit(&output);

      StringMap::const_iterator last = (output).end();
      for (StringMap::const_iterator itr = (output).begin();
           itr != last; itr++) {
        // cout << itr->first << "\t" << itr->second << endl;
        context.emit(itr->first, boost::lexical_cast<string>(itr->second));
      }

      output.clear();

      last = (output).end();
      for (StringMap::const_iterator itr = (output).begin();
           itr != last; itr++) {
        // cout << "output is\t" << itr->first << "\t" << itr->second << endl;
        // context.emit(itr->first, boost::lexical_cast<string>(itr->second));
      }

      index = boost::lexical_cast<int>(temp_string_components_[1]);
      vars_ = new VariationalParameters(*params_);

      display_matrix(vars_->tau_est_top_.get(), "tau_est_top is\n");
      display_vector(vars_->tau_est_bottom_.get(), "tau_est_bottom is\n");

      tau_coordinate_ = -1;
      nu_coordinate_ = -1;
    } else {
      ProcessKey(context.getInputKey(), sum);
      // cout << "processing\t" << context.getInputKey() << "\t" << sum << endl;
    }

    /*
    if (index == -1) {
      index = boost::lexical_cast<double>(temp_string_components_[1]);
      // reduceContext = context;
    } else {
      if (index != boost::lexical_cast<int>(temp_string_components_[1])) {
        Optimize();
        Emit(&output);

        StringMap::const_iterator last = (output).end();
        for (StringMap::const_iterator itr = (output).begin();
             itr != last; itr++) {
          // cout << itr->first << "\t" << itr->second << endl;
          context.emit(itr->first, boost::lexical_cast<string>(itr->second));
        }

        output.clear();

        index = boost::lexical_cast<int>(temp_string_components_[1]);
        vars_ = new VariationalParameters(*params_);

        tau_coordinate_ = -1;
        nu_coordinate_ = -1;
      }
    }
    */
  }
  // }
}
Beispiel #22
0
/* 
 * PURPOSE: run the commands which user entered 
 * INPUTS: 
 * cmd double pointer that holds all commands
 * mats the matrix list
 * num_mats the number of matrix in the list
 * RETURN: void
 * If no errors occurred during process then return nothing
 * else print error message
 **/
void run_commands (Commands_t* cmd, Matrix_t** mats, unsigned int num_mats) {
	//TODO ERROR CHECK INCOMING PARAMETERS
	if(!cmd){
		printf("commands array is null\n");
		return;
	}
	if(!(*mats)){
		printf("matrix list is null\n");
		return;
	}

	/*Parsing and calling of commands*/
	if (strncmp(cmd->cmds[0],"display",strlen("display") + 1) == 0
		&& cmd->num_cmds == 2) {
			/*find the requested matrix*/
			int idx = find_matrix_given_name(mats,num_mats,cmd->cmds[1]);
			if (idx >= 0) {
				display_matrix (mats[idx]);
			}
			else {
				printf("Matrix (%s) doesn't exist\n", cmd->cmds[1]);
				return;
			}
	}
	else if (strncmp(cmd->cmds[0],"add",strlen("add") + 1) == 0
		&& cmd->num_cmds == 4) {
			int mat1_idx = find_matrix_given_name(mats,num_mats,cmd->cmds[1]);
			int mat2_idx = find_matrix_given_name(mats,num_mats,cmd->cmds[2]);
			if (mat1_idx >= 0 && mat2_idx >= 0) {
				Matrix_t* c = NULL;
				if( !create_matrix (&c,cmd->cmds[3], mats[mat1_idx]->rows, 
						mats[mat1_idx]->cols)) {
					printf("Failure to create the result Matrix (%s)\n", cmd->cmds[3]);
					return;
				}
			
				if(add_matrix_to_array(mats,c, num_mats) == 999){
					perror("PROGRAM FAILED TO ADD MATRIX TO ARRAY\n");
					return;
				} //TODO ERROR CHECK NEEDED


				if (! add_matrices(mats[mat1_idx], mats[mat2_idx],c) ) {
					printf("Failure to add %s with %s into %s\n", mats[mat1_idx]->name, mats[mat2_idx]->name,c->name);
					return;	
				}
			}
	}
	else if (strncmp(cmd->cmds[0],"duplicate",strlen("duplicate") + 1) == 0
		&& cmd->num_cmds == 3 && strlen(cmd->cmds[1]) + 1 <= MATRIX_NAME_LEN) {
		int mat1_idx = find_matrix_given_name(mats,num_mats,cmd->cmds[1]);
		if (mat1_idx >= 0 ) {
				Matrix_t* dup_mat = NULL;
				if( !create_matrix (&dup_mat,cmd->cmds[2], mats[mat1_idx]->rows, 
						mats[mat1_idx]->cols)) {
					return;
				}
				if(!duplicate_matrix (mats[mat1_idx], dup_mat)){
					perror("PROGRAM FAILED TO DUPLICATE MATRIX\n");
					return;
				} //TODO ERROR CHECK NEEDED
				if(add_matrix_to_array(mats,dup_mat,num_mats) == 999){
					perror("PROGRAM FAILED TO ADD MATRIX TO ARRAY\n");
					return;
				} //TODO ERROR CHECK NEEDED
				printf ("Duplication of %s into %s finished\n", mats[mat1_idx]->name, cmd->cmds[2]);
		}
		else {
			printf("Duplication Failed\n");
			return;
		}
	}
	else if (strncmp(cmd->cmds[0],"equal",strlen("equal") + 1) == 0
		&& cmd->num_cmds == 2) {
			int mat1_idx = find_matrix_given_name(mats,num_mats,cmd->cmds[1]);
			int mat2_idx = find_matrix_given_name(mats,num_mats,cmd->cmds[2]);
			if (mat1_idx >= 0 && mat2_idx >= 0) {
				if ( equal_matrices(mats[mat1_idx],mats[mat2_idx]) ) {
					printf("SAME DATA IN BOTH\n");
				}
				else {
					printf("DIFFERENT DATA IN BOTH\n");
				}
			}
			else {
				printf("Equal Failed\n");
				return;
			}
	}
	else if (strncmp(cmd->cmds[0],"shift",strlen("shift") + 1) == 0
		&& cmd->num_cmds == 4) {
		int mat1_idx = find_matrix_given_name(mats,num_mats,cmd->cmds[1]);
		const int shift_value = atoi(cmd->cmds[3]);
		if (mat1_idx >= 0 ) {
			if(!bitwise_shift_matrix(mats[mat1_idx],cmd->cmds[2][0], shift_value)){
				perror("PROGRAM FAILED TO SHIFT MATRIX\n");
				return;
			} //TODO ERROR CHECK NEEDED
			printf("Matrix (%s) has been shifted by %d\n", mats[mat1_idx]->name, shift_value);
		}
		else {
			printf("Matrix shift failed\n");
			return;
		}

	}
	else if (strncmp(cmd->cmds[0],"read",strlen("read") + 1) == 0
		&& cmd->num_cmds == 2) {
		Matrix_t* new_matrix = NULL;
		if(! read_matrix(cmd->cmds[1],&new_matrix)) {
			printf("Read Failed\n");
			return;
		}	
		
		if(add_matrix_to_array(mats,new_matrix, num_mats) == 999){
			perror("PROGRAM FAILED TO ADD MATRIX TO ARRAY\n");
			return;
		} //TODO ERROR CHECK NEEDED
		printf("Matrix (%s) is read from the filesystem\n", cmd->cmds[1]);	
	}
	else if (strncmp(cmd->cmds[0],"write",strlen("write") + 1) == 0
		&& cmd->num_cmds == 2) {
		int mat1_idx = find_matrix_given_name(mats,num_mats,cmd->cmds[1]);
		if(! write_matrix(mats[mat1_idx]->name,mats[mat1_idx])) {
			printf("Write Failed\n");
			return;
		}
		else {
			printf("Matrix (%s) is wrote out to the filesystem\n", mats[mat1_idx]->name);
		}
	}
	else if (strncmp(cmd->cmds[0], "create", strlen("create") + 1) == 0
		&& strlen(cmd->cmds[1]) + 1 <= MATRIX_NAME_LEN && cmd->num_cmds == 4) {
		Matrix_t* new_mat = NULL;
		const unsigned int rows = atoi(cmd->cmds[2]);
		const unsigned int cols = atoi(cmd->cmds[3]);

		if(!create_matrix(&new_mat,cmd->cmds[1],rows, cols)){
			perror("PROGRAM FAILED TO ADD CREATE TO ARRAY\n");
			return;
		} //TODO ERROR CHECK NEEDED
		if(add_matrix_to_array(mats,new_mat,num_mats) == 999){
			perror("PROGRAM FAILED TO ADD MATRIX TO ARRAY\n");
			return;
		} // TODO ERROR CHECK NEEDED
		printf("Created Matrix (%s,%u,%u)\n", new_mat->name, new_mat->rows, new_mat->cols);
	}
	else if (strncmp(cmd->cmds[0], "random", strlen("random") + 1) == 0
		&& cmd->num_cmds == 4) {
		int mat1_idx = find_matrix_given_name(mats,num_mats,cmd->cmds[1]);
		const unsigned int start_range = atoi(cmd->cmds[2]);
		const unsigned int end_range = atoi(cmd->cmds[3]);
		if(!random_matrix(mats[mat1_idx],start_range, end_range)) {
			perror("PROGRAM FAILED TO RANDOMIZE MATRIX\n");
			return;
		} //TODO ERROR CHECK NEEDED

		printf("Matrix (%s) is randomized between %u %u\n", mats[mat1_idx]->name, start_range, end_range);
	}
	else {
		printf("Not a command in this application\n");
	}

}
Beispiel #23
0
template<class T> void SGMatrix<T>::display_matrix(const char* name) const
{
	display_matrix(matrix, num_rows, num_cols, name);
}
Beispiel #24
0
int main(int arc, char**argv) {


	if (arc != 2) {
		print_trace(TRACE_ERROR, "Veuillez indiquer un fichier où stocker la matrice de calibration.\n");
		return EXIT_FAILURE;
	} 
	
	struct Matrix *matrice = create_matrix(NB_LIGNES,NB_COLONNES, NUM_CAPTORS);
	pthread_mutex_init(&mutexMatrice, NULL);

	for (uint8_t i = 0; i < NUM_CAPTORS; i++) {
		mesures[i] = calloc(200, sizeof(char));
	}

	bdaddr_t controllerAdd, server1Mac, server2Mac, server3Mac, sensorMac;
	str2ba(btControllerAdd, &controllerAdd); 
	str2ba(server1Add, &server1Mac); 
	str2ba(server2Add, &server2Mac); 
	str2ba(server3Add, &server3Mac); 
	str2ba(sensorAdd, &sensorMac);

	hci_controller_t hci_controller = hci_open_controller(&controllerAdd, "MAIN_SERVER");
	sensor = bt_device_create(sensorMac, PUBLIC_DEVICE_ADDRESS, NULL, "SENSOR_TAG");
	server1 = bt_device_create(server1Mac, PUBLIC_DEVICE_ADDRESS, NULL, "SERVER_1");
	server2 = bt_device_create(server2Mac, PUBLIC_DEVICE_ADDRESS, NULL, "SERVER_2");
	server3 = bt_device_create(server3Mac, PUBLIC_DEVICE_ADDRESS, NULL, "SERVER_3");

	hci_LE_clear_white_list(NULL, &hci_controller);
	hci_LE_add_white_list(NULL, &hci_controller, sensor);
	
	// Création des trois clients :
	l2cap_client_t clients[NUM_CAPTORS-1] = {0};
	l2cap_client_create(&clients[0], &server1Mac, 0x1001, 500, NULL, &(send_req_func));
	l2cap_client_create(&clients[1], &server2Mac, 0x1001, 500, NULL, &(send_req_func)); 
	l2cap_client_create(&clients[2], &server3Mac, 0x1001, 500, NULL, &(send_req_func));


	if (l2cap_client_connect(&clients[0]) != 0) {	
		perror("client_connect : unable to connect client 1");
		return EXIT_FAILURE;
	}
	if (l2cap_client_connect(&clients[1]) != 0) {	
		perror("client_connect : unable to connect client 2");
		return EXIT_FAILURE;
	}
	if (l2cap_client_connect(&clients[2]) != 0) {	
		perror("client_connect : unable to connect client 3");
		return EXIT_FAILURE;
	}

	fprintf(stderr, "\n-------------------\n");
	fprintf(stderr, "----Calibration----\n");
	fprintf(stderr, "-------------------\n");

	pthread_t clients_threads[NUM_CAPTORS];
	struct routine_data_t routine_data[NUM_CAPTORS];
	for (uint8_t k = 0; k < NUM_CAPTORS; k++) {
		routine_data[k].timeout = 4500;
		routine_data[k].num_captor = k;
		routine_data[k].hci_controller = &hci_controller;
		routine_data[k].sensor = sensor;
		if (k < NUM_CAPTORS-1) {
			routine_data[k].client = &(clients[k]);
		} else {
			routine_data[k].client = NULL;
		}
		routine_data[k].matrice = matrice;
	}
	char command[20] = {0};
	char *status;
	char retry = 0;
	for (uint8_t i = 0; i < NB_LIGNES; i++) {
		for (uint8_t j = 0; j < NB_COLONNES; j++) {
			scan:
			retry = 0;
			fprintf(stdout, "Position courante : %i, %i\n", i, j); 
			scanf("%s", command);
			for (uint8_t k = 0; k < NUM_CAPTORS; k++) {
				routine_data[k].num_row = i;
				routine_data[k].num_col = j;
				pthread_create(&(clients_threads[k]), NULL, 
					       &(get_rssi_thread_routine),
					       (void *)&routine_data[k]);
			}
			for (uint8_t k = 0; k < 4; k ++) {
				pthread_join(clients_threads[k], (void **)&status);
				retry = retry || *status;
			}
			if (retry) {
				print_trace(TRACE_WARNING, "Attention : impossible d'acquérir les mesures pour cette case, veuillez réessayer.\n");
				goto scan;
			}
		}
	}

	// Fermeture des clients :
	// Envoie des demandes de fin de connexion :
	l2cap_client_send(&clients[0], 3000, CLIENT_CLOSE_CONNECTION);
	l2cap_client_send(&clients[1], 3000, CLIENT_CLOSE_CONNECTION);
	l2cap_client_send(&clients[2], 3000, CLIENT_CLOSE_CONNECTION);

	// Destruction des clients :
	l2cap_client_close(&clients[0]);
	l2cap_client_close(&clients[1]);
	l2cap_client_close(&clients[2]);

	display_matrix(matrice);
	save_matrix(argv[1], *matrice);
	pthread_mutex_destroy(&mutexMatrice);

	hci_close_controller(&hci_controller);
	display_hci_socket_list(hci_controller.sockets_list);
	bt_destroy_device_table();

} 
Beispiel #25
0
void
Rank(int n)
{
	int			N, i, k, r;
	double		p_value, product, chi_squared, arg1, p_32, p_31, p_30, R, F_32, F_31, F_30;
	BitSequence	**matrix = create_matrix(32, 32);
	
	N = n/(32*32);
	if ( isZero(N) ) {
		fprintf(stats[TEST_RANK], "\t\t\t\tRANK TEST\n");
		fprintf(stats[TEST_RANK], "\t\tError: Insuffucient # Of Bits To Define An 32x32 (%dx%d) Matrix\n", 32, 32);
		p_value = 0.00;
	}
	else {
		r = 32;					/* COMPUTE PROBABILITIES */
		product = 1;
		for ( i=0; i<=r-1; i++ )
			product *= ((1.e0-pow(2, i-32))*(1.e0-pow(2, i-32)))/(1.e0-pow(2, i-r));
		p_32 = pow(2, r*(32+32-r)-32*32) * product;
		
		r = 31;
		product = 1;
		for ( i=0; i<=r-1; i++ )
			product *= ((1.e0-pow(2, i-32))*(1.e0-pow(2, i-32)))/(1.e0-pow(2, i-r));
		p_31 = pow(2, r*(32+32-r)-32*32) * product;
		
		p_30 = 1 - (p_32+p_31);
		
		F_32 = 0;
		F_31 = 0;
		for ( k=0; k<N; k++ ) {			/* FOR EACH 32x32 MATRIX   */
			def_matrix(32, 32, matrix, k);
#if (DISPLAY_MATRICES == 1)
			display_matrix(32, 32, matrix);
#endif
			R = computeRank(32, 32, matrix);
			if ( R == 32 )
				F_32++;			/* DETERMINE FREQUENCIES */
			if ( R == 31 )
				F_31++;
		}
		F_30 = (double)N - (F_32+F_31);
		
		chi_squared =(pow(F_32 - N*p_32, 2)/(double)(N*p_32) +
					  pow(F_31 - N*p_31, 2)/(double)(N*p_31) +
					  pow(F_30 - N*p_30, 2)/(double)(N*p_30));
		
		arg1 = -chi_squared/2.e0;

		fprintf(stats[TEST_RANK], "\t\t\t\tRANK TEST\n");
		fprintf(stats[TEST_RANK], "\t\t---------------------------------------------\n");
		fprintf(stats[TEST_RANK], "\t\tCOMPUTATIONAL INFORMATION:\n");
		fprintf(stats[TEST_RANK], "\t\t---------------------------------------------\n");
		fprintf(stats[TEST_RANK], "\t\t(a) Probability P_%d = %f\n", 32,p_32);
		fprintf(stats[TEST_RANK], "\t\t(b)             P_%d = %f\n", 31,p_31);
		fprintf(stats[TEST_RANK], "\t\t(c)             P_%d = %f\n", 30,p_30);
		fprintf(stats[TEST_RANK], "\t\t(d) Frequency   F_%d = %d\n", 32,(int)F_32);
		fprintf(stats[TEST_RANK], "\t\t(e)             F_%d = %d\n", 31,(int)F_31);
		fprintf(stats[TEST_RANK], "\t\t(f)             F_%d = %d\n", 30,(int)F_30);
		fprintf(stats[TEST_RANK], "\t\t(g) # of matrices    = %d\n", N);
		fprintf(stats[TEST_RANK], "\t\t(h) Chi^2            = %f\n", chi_squared);
		fprintf(stats[TEST_RANK], "\t\t(i) NOTE: %d BITS WERE DISCARDED.\n", n%(32*32));
		fprintf(stats[TEST_RANK], "\t\t---------------------------------------------\n");

		p_value = exp(arg1);
		if ( isNegative(p_value) || isGreaterThanOne(p_value) )
			fprintf(stats[TEST_RANK], "WARNING:  P_VALUE IS OUT OF RANGE.\n");

		for ( i=0; i<32; i++ )				/* DEALLOCATE MATRIX  */
			free(matrix[i]);
		free(matrix);
	}
	fprintf(stats[TEST_RANK], "%s\t\tp_value = %f\n\n", p_value < ALPHA ? "FAILURE" : "SUCCESS", p_value); fflush(stats[TEST_RANK]);
	fprintf(results[TEST_RANK], "%f\n", p_value); fflush(results[TEST_RANK]);
}