예제 #1
0
int main() {
    Vector *A = make_vector(4);
    consecutive_vector(A);
    printf("A\n");
    print_vector(A);

    Vector *B = make_vector(4);
    increment_vector(B, 1);
    printf("B\n");
    print_vector(B);

    Vector *C = add_vector_func(A, B);
    printf("A + B\n");
    print_vector(C);

    free_vector(A);
    free_vector(B);
    free_vector(C);

    return 0;
}
void to_matrix(Circuit* c,gsl_matrix** A_ptr,gsl_vector** b_ptr)
{
	int w=c->ccount+c->vcount;
	(*A_ptr)=gsl_matrix_calloc(w,w);
	(*b_ptr)=gsl_vector_calloc(w);
	gsl_matrix * A=*A_ptr;
	gsl_vector * b=*b_ptr;
	if(A==NULL||b==NULL){
		printf("ERROR, UNABLE TO ALLOCATE MEMORY FOR MATRIX CONVERSION\n");
		if(A!=NULL){
			gsl_matrix_free(A);
			A=NULL;
		}
		if(b!=NULL){
			gsl_vector_free(b);
			b=NULL;
		}
		return;
	}
	int i;
	int j;
	for(i=0;i<c->ccount;i++)
	{
		Component* edge=c->components+i;
		for(j=0;j<c->vcount-1;j++){
			if(edge->A==c->vertices[j].id){
				gsl_matrix_set(A,j,i,-1.0);
			}
			if(edge->B==c->vertices[j].id){
				gsl_matrix_set(A,j,i,1.0);
			}
		}
	}
	gsl_matrix_view voltage_view=gsl_matrix_submatrix(A,c->vcount-1,c->ccount,c->ccount,c->vcount);
	gsl_matrix* voltage=&voltage_view.matrix;
	for(i=0;i<c->ccount;i++){
		for(j=0;j<c->vcount;j++){
			if(c->vertices[j].id==c->components[i].A)gsl_matrix_set(voltage,i,j,-1);
			if(c->vertices[j].id==c->components[i].B)gsl_matrix_set(voltage,i,j,1);
		}
		if(c->components[i].type==RESISTOR)
		{
			gsl_matrix_set(A,c->vcount+i-1,i,*(double *)c->components[i].data);
		}
		if(c->components[i].type==BATTERY)
		{
			gsl_vector_set(b,c->vcount+i-1,*(double *)c->components[i].data);
		}
	}
	gsl_matrix_set(A,w-1,w-1,1);
	print_matrix(voltage);
	print_vector(b);
}
예제 #3
0
void swap(int vector[], int element_a, int element_b) {
	int place_holder;

	if (vector[element_a] > vector[element_b]) {
		print_vector(vector);
		print_left_right(element_a, element_b);

		place_holder = vector[element_a];
		vector[element_a] = vector[element_b];
		vector[element_b] = place_holder;
	}
}
int load_key_from_file(char *gkey){
	FILE *fp;
	fp = fopen(KEY_FILE, "rb");
	int read=fread(gkey,sizeof(char),GCRY_KEYLEN,fp);
	fclose(fp);
	if(read==GCRY_KEYLEN){
		printf("\n clave cargada\n");
		print_vector(gkey,GCRY_KEYLEN);
		return 0;
	}
	return -1;
}
예제 #5
0
int main()
{
    std::function<void()> foo = []() {};

    std::vector<int> vec = { 1,2,3,4,5,6,7,8,9,10 };
    std::transform(vec.begin(), vec.end(), vec.begin(), std::bind(std::multiplies<int>(), std::placeholders::_1, 2));

    std::vector< std::function<void()> > function_queue = { std::bind(print_vector, vec),[]() {std::cout << "\n"; }, [&vec]() {print_vector(vec); } };
    print_vector(vec);

    return 0;
}
예제 #6
0
파일: planet.c 프로젝트: dezgeg/PlanetSim
/* write a solar system state to file */
void print_system(FILE* file, const System* sys)
{
	int count = sys->nplanets;
	fprintf(file, "%d " FLOAT_PRINTF_FORMAT " ",
			count, sys->cur_step * sys->time_step);
	for(int i = 0; i < sys->nplanets; i++)
	{
		fprintf(file, " ");
		print_vector(file, sys->planets[i].position);
		fprintf(file, i == count - 1 ? "\n" : " ");
	}
}
예제 #7
0
int
main(void)
{
  complex v[N], v1[N], scratch[N];
  int k;

  /* Fill v[] with a function of known FFT: */
  for(k=0; k<N; k++) {
    v[k].Re = 0.125*cos(2*PI*k/(double)N);
    v[k].Im = 0.125*sin(2*PI*k/(double)N);
    v1[k].Re =  0.3*cos(2*PI*k/(double)N);
    v1[k].Im = -0.3*sin(2*PI*k/(double)N);
  }
    
  /* FFT, iFFT of v[]: */
  print_vector("Orig", v, N);
  fft( v, N, scratch );
  print_vector(" FFT", v, N);
  ifft( v, N, scratch );
  print_vector("iFFT", v, N);

  /* FFT, iFFT of v1[]: */
  print_vector("Orig", v1, N);
  fft( v1, N, scratch );
  print_vector(" FFT", v1, N);
  ifft( v1, N, scratch );
  print_vector("iFFT", v1, N);

  exit(EXIT_SUCCESS);
}
예제 #8
0
파일: ejercicio6.c 프로젝트: Miguelrn/MPI
int main(int argc, char *argv[]){
	
	srand (time(NULL));
	int i, j, z;
	int tamanio = 400;

	float* vectorA;
	float* vectorB;
	int resultado = 0;
	double startwtime, endwtime;
	int myrank, size;

	MPI_Init(&argc, &argv);
	MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
	MPI_Comm_size(MPI_COMM_WORLD, &size);

	vectorA = get_vector(tamanio);
	vectorB = get_vector(tamanio);

	if(tamanio <= 15) { //Mostramos hasta vectores de 15 elementos	
		print_vector ( vectorA, tamanio);
		print_vector ( vectorB, tamanio);
	}

	startwtime = MPI_Wtime();
	
	for(i = 0; i < tamanio; i++)//cada elemento del vector
    {
    	resultado += vectorA[i]*vectorB[i];     
   	}

	endwtime = MPI_Wtime();
	printf("Tiempo de ejecucion: %f, usando %d+1 maquinas\n", endwtime - startwtime, size - 1);	

	printf("\nEl resultado del producto escalar de los vectores es: %d\n\n", resultado);	  
	MPI_Finalize();	

	return 0;

}
예제 #9
0
int main ()
{
    typedef std::vector<int, std::allocator<int> > Vector;

    const Vector::value_type d1[] = { 1, 2, 3, 4 };
    const Vector::value_type d2[] = { 1, 3, 2, 4 };

    // Set up two vectors.
    Vector v1 (d1 + 0, d1 + sizeof d1 / sizeof *d1);
    Vector v2 (d2 + 0, d2 + sizeof d2 / sizeof *d2);

    // Make heaps.
    std::make_heap (v1.begin (), v1.end ());
    std::make_heap (v2.begin (), v2.end (), std::less<int>());

    // v1 = (4, x, y, z)  and  v2 = (4, x, y, z)

    // Note that x, y and z represent the remaining values in the
    // container (other than 4).  The definition of the heap and heap
    // operations  does not require any particular ordering
    // of these values.

    // Copy both vectors to cout.
    print_vector (std::cout, v1);
    print_vector (std::cout, v2);

    // Now let's pop.
    std::pop_heap (v1.begin (), v1.end ());
    std::pop_heap (v2.begin (), v2.end (), std::less<int>());

    print_vector (std::cout, v1);
    print_vector (std::cout, v2);

    // And push.
    std::push_heap (v1.begin (), v1.end ());
    std::push_heap (v2.begin (), v2.end (), std::less<int>());

    print_vector (std::cout, v1);
    print_vector (std::cout, v2);

    // Now sort those heaps.
    std::sort_heap (v1.begin (), v1.end ());
    std::sort_heap (v2.begin (), v2.end (), std::less<int>());

    print_vector (std::cout, v1);
    print_vector (std::cout, v2);

    return 0;
}
예제 #10
0
int main(void) {
	int vector[VECTOR_LENGTH], element;

	for (element = 0; element < VECTOR_LENGTH; ++element) {
		srandomdev();
		vector[element] = random() % 10;
	}

	quick_sort(vector, 0, VECTOR_LENGTH - 1);
	print_vector(vector);

	return 0;
}
예제 #11
0
static void vector_insert2(c_vector * p)
{
    c_vector vt;
    __c_vector(&vt, int_comparer);
    
    c_vector_insert2(&vt, 
                    c_vector_begin(&vt), 
                    c_vector_begin(p),
                    c_vector_end(p));
    printf("after insert2\n");
    print_vector(&vt);
    __c_rotcev(&vt);    
}
예제 #12
0
int main() {
    int size_vector = 0, maximun_element = 0, *my_vector_oficial, *my_vector_index, size_index = 0, element;
    int position_index = 0, position_oficial = 0;
   	size_vector = inserting_element();
   	size_index = sqrt(size_vector);
   	maximun_element = inserting_maximun_element();
   	
   	while(maximun_element < size_vector) {
   		maximun_element = inserting_maximun_element();
   	}
    my_vector_oficial = initializing_vector(size_vector, maximun_element);
    
    selection_sort(my_vector_oficial, size_vector);
    print_vector(my_vector_oficial, size_vector);
    my_vector_index = initializing_vector_index(my_vector_oficial, size_vector);

    printf("\nIndice\n");
    print_vector(my_vector_index, size_index);
    printf("Insira o elemento que deseja buscar: ");
    scanf("%d", &element);
    
    position_index = search_element_index(my_vector_index, size_index, element);

    if (presence_in_vector(my_vector_oficial, size_vector, element) == false) {
    	position_index = -1;
    }

    if (presence_in_vector(my_vector_oficial, size_vector, element) == true && 
    	element > *(my_vector_index+(size_index-1))) {
    	position_index = size_index-1;
    }

    position_oficial = indexed_search(my_vector_oficial, my_vector_index, size_vector, position_index, element);
    printf("Posição no vetor de indice: %d\n", position_index);
    printf("Posição no vetor principal: %d\n", position_oficial);

    printf("\n");
    return 0;
}
void c_FourierTransfrom::demo(void)
{
    Mat image = imread("../../data/lena.jpg", CV_LOAD_IMAGE_COLOR);
    vector<Mat> images;
    split(image, images);
    Mat data(Mat_<t_Real>(images[0]) / 255.0);
    t_Real *p_in = data.ptr<t_Real>(0), *p_out;
    size_t n_data = image.cols;
    c_FourierTransfrom ft;
    ft.fftw_complex_1d(p_in, n_data, p_out);
    print_vector(p_out, n_data);
    return;
}
int main()
{
    std::vector<std::vector<char>> board = {
        {'o','a','a','n'},
        {'e','t','a','e'},
        {'i','h','k','r'},
        {'i','f','l','v'}
    };

    std::vector<std::string> dictionary = {
        "oath","pea","eat","rain"
    };

    std::vector<std::string> result = find_words(board, dictionary);
    std::cout << "Board:" << std::endl;
    print_board(board);
    std::cout << "Dictionary:" << std::endl;
    print_vector(dictionary);
    std::cout << "Result:" << std::endl;
    print_vector(result);
    return 0;
}
예제 #15
0
파일: runtime.c 프로젝트: osa1/racket.rkt
void print_root_stack()
{
    printf("[\n");

    int64_t** rootstack_work_ptr = (int64_t**)rootstack_begin;
    while ((void*)rootstack_work_ptr < (void*)rootstack_ptr)
    {
        print_vector(*rootstack_work_ptr);
        printf("---\n");
        rootstack_work_ptr++;
    }
    printf("]\n");
}
예제 #16
0
static void
print_tsb_link(tsb_link_vector_el_t *tsb_linkp, void *data) {
  tsb_link_t *tsb_link = *tsb_linkp;
  gf_say(VERBOSE_DEBUG,
	 "TSB_LINK: name = '%s', lineno=%d\n", tsb_link->name, 
	 tsb_link->lineno);
  gf_say(VERBOSE_DEBUG, 
	 "     start_addr  = 0x%llx\n", tsb_link->start_addr);
  gf_say(VERBOSE_DEBUG, "\n");

  print_vector(&(tsb_link->entries));

}
예제 #17
0
int mipb_frac::run()
{
	bool targets=false;
	for (int user=0;user<lines;user++) {
		if (_rate_targets[user] != NOT_SET)
			targets=true;
	}	
	if (targets) {
		for (int user=0;user<lines;user++) {
			if (_rate_targets[user] == NOT_SET) 
				continue;
			_w_min[user]=0;
			_w_max[user]=1e-5;
			while(1) {
				reset_data();
				load();
				print_vector(_w,"_w");
                                print_vector(_b_total,"_b_total");
				//getchar();
				if (_b_total[user] > _rate_targets[user]) {
					_w_max[user] = _w[user];
				}
				else if (_b_total[user] < _rate_targets[user]) {
					_w_min[user] = _w[user];
				}
				if (abs(_b_total[user] - _rate_targets[user]) < _rate_tol) {
					printf("Converged on line %d\n",user);
					break;
				}
				_w[user] = (_w_max[user]+_w_min[user])/2;	
			}
		}
	}
	else
		load();
	init_lines();
	calculate_snr();
}
예제 #18
0
static void vector_less(c_vector * p)
{
    c_iterator iter;
    int * old;
    c_vector vt;
    __c_vector(&vt, int_comparer);
    
    print_vector(&vt);
    print_vector(p);
    printf(c_vector_less(&vt, p) ? "IS less\n" : "NOT less\n");
    
    create_with_push_back(&vt);
    print_vector(&vt);
    print_vector(p);
    printf(c_vector_less(&vt, p) ? "IS less\n" : "NOT less\n");
    
    iter = c_vector_begin(&vt);
    old = (int*)ITER_REF(iter);
    ITER_REF_ASSIGN(iter, &array[5]);
    print_vector(&vt);
    print_vector(p);
    printf(c_vector_less(&vt, p) ? "IS less\n" : "NOT less\n");
    ITER_REF_ASSIGN(iter, old);
    
    ITER_INC_N(iter, 3);
    old = (int*)ITER_REF(iter);
    ITER_REF_ASSIGN(iter, &array[0]);
    print_vector(&vt);
    print_vector(p);
    printf(c_vector_less(&vt, p) ? "IS less\n" : "NOT less\n");
        
    c_vector_clear(&vt);
    c_vector_clear(p);
    print_vector(&vt);
    print_vector(p);
    printf(c_vector_less(&vt, p) ? "IS less\n" : "NOT less\n");
    __c_rotcev(&vt);
}
예제 #19
0
void main(int argc, char **argv)
{
	int n = 3, iter = 0;
	if(argc > 1)
		n = atoi(argv[1]);

	double stop, *tmp, (*A)[n], *b, *x0, *x1;

	init(n, &A, &b, &x0, &x1);

	printf("Matrix A:\n");
	print_matrix(n, A);
	printf("\nVector b:\n");
	print_vector(n, b);
	printf("\nInitial Guess of Vector x:\n");
	print_vector(n, x1);

	do
	{
		stop = 0.0;
		for (int i = 0; i < n; i++)
		{
		  x1[i] = 0.0;
		  for (int j = 0; j < n; j++)
		  {
			  if(i != j)
				  x1[i] += A[i][j] * x0[j];
		  }
		  x1[i] = (b[i] - x1[i]) / A[i][i];
		  stop += pow(x1[i] - x0[i], 2.0);
		}
		tmp = x1; x1 = x0; x0 = tmp;
		iter++;
	} while((sqrt(stop) > EPSILON) && (iter < MAX_ITER));

	printf("\nSolution Vector x after %d Iterations:\n", iter);
	print_vector(n, x1);
}
예제 #20
0
static void mesh_generate(){
	// double ppwl = param->factor / param->freq;
	// double myfactor = param->factor;

	Octree tree = octor_newtree();
	tree.root = refine_tree(&tree.root, &toexpand, &query_model);

	vector<Element> elements;
	unordered_map<int , Node> nodes;
	elements = traverse_tree(&tree.root, elements);
	print_vector(elements, "/Users/kelicheng/Desktop/mymesh.txt");
	nodes = extract_mesh(elements);
	print_nodes(nodes, "/Users/kelicheng/Desktop/mynodes.txt");
}
예제 #21
0
파일: kalman.c 프로젝트: Colyette/P15230
void iter_ext_kalman_step( m_elem *z_in )
{
  int     iteration = 1;
  m_elem  est_change;
  m_elem  *prev_state;
  m_elem  *new_state;
  m_elem  *temp;

  generate_system_transfer( state_pre, sys_transfer );
  estimate_prob( cov_post, sys_transfer, sys_noise_cov, cov_pre );
  apply_system( state_post, state_pre );

  /*  Now iterate, updating the probability and the system model
      until no change is noticed between iteration steps      */

  prev_state = iter_state0;
  new_state  = iter_state1;

  generate_measurement_transfer( state_pre, mea_transfer );
  update_prob( cov_pre, mea_noise_cov, mea_transfer,
          cov_post, kalman_gain );
  update_system( z_in, state_pre, kalman_gain, prev_state );
  est_change = calc_state_change( state_pre, prev_state );

  while( (est_change < ITERATION_THRESHOLD) &&
    (iteration++ < ITERATION_DIVERGENCE) )
    {
#ifdef DEBUG_ITER      
      print_vector( "\titer state", prev_state, 10 );
#endif
      /*  Update the estimate  */

      generate_measurement_transfer( prev_state, mea_transfer );
      update_prob( cov_pre, mea_noise_cov, mea_transfer,
          cov_post, kalman_gain );
      update_system( z_in, prev_state, kalman_gain, new_state );
      est_change = calc_state_change( prev_state, new_state );

      temp = prev_state;
      prev_state = new_state;
      new_state = temp;
    }

  vec_copy( prev_state, state_post, state_size );

#ifdef PRINT_DEBUG
  printf( "iekf: step %3d, %2d iters\n", global_step, iteration );
#endif
  global_step++;
}
예제 #22
0
파일: planner.c 프로젝트: ADTL/TinyG
void mp_dump_runtime_state(void)
{
	fprintf_P(stderr, PSTR("***Runtime Singleton (mr)\n"));
	print_scalar(PSTR("line number:       "), mr.linenum);
	print_vector(PSTR("position:          "), mr.position, AXES);
	print_vector(PSTR("target:            "), mr.target, AXES);
	print_scalar(PSTR("length:            "), mr.length);

	print_scalar(PSTR("move_time:         "), mr.move_time);
//	print_scalar(PSTR("accel_time;        "), mr.accel_time);
//	print_scalar(PSTR("elapsed_accel_time:"), mr.elapsed_accel_time);
	print_scalar(PSTR("midpoint_velocity: "), mr.midpoint_velocity);
//	print_scalar(PSTR("midpoint_accel:    "), mr.midpoint_acceleration);
//	print_scalar(PSTR("jerk_div2:         "), mr.jerk_div2);

	print_scalar(PSTR("segments:          "), mr.segments);
	print_scalar(PSTR("segment_count:     "), mr.segment_count);
	print_scalar(PSTR("segment_move_time: "), mr.segment_move_time);
//	print_scalar(PSTR("segment_accel_time:"), mr.segment_accel_time);
	print_scalar(PSTR("microseconds:      "), mr.microseconds);
	print_scalar(PSTR("segment_length:	  "), mr.segment_length);
	print_scalar(PSTR("segment_velocity:  "), mr.segment_velocity);
}
예제 #23
0
int main() {
  
  igraph_t g;
  igraph_vector_ptr_t result;
  igraph_es_t es;
  igraph_integer_t omega;
  long int i, j, n;
  const int params[] = {4, -1, 2, 2, 0, 0, -1, -1};
 
  igraph_set_warning_handler(warning_handler_ignore);

  igraph_vector_ptr_init(&result, 0);
  igraph_full(&g, 6, 0, 0);
  igraph_es_pairs_small(&es, 0, 0, 1, 0, 2, 3, 5, -1);
  igraph_delete_edges(&g, es);
  igraph_es_destroy(&es);
  
  for (j=0; j<sizeof(params)/(2*sizeof(params[0])); j++) {
    if (params[2*j+1] != 0) {
      igraph_cliques(&g, &result, params[2*j], params[2*j+1]);  
    } else {
      igraph_largest_cliques(&g, &result);
    }
    n = igraph_vector_ptr_size(&result);
    printf("%ld cliques found\n", (long)n);
    canonicalize_list(&result);
    for (i=0; i<n; i++) {
      igraph_vector_t* v = (igraph_vector_t*) igraph_vector_ptr_e(&result,i);
      print_vector(v);
      igraph_vector_destroy(v);
      free(v);
    }
  }
   
  igraph_clique_number(&g, &omega);
  printf("omega=%ld\n", (long)omega);

  test_callback(&g);

  igraph_destroy(&g);

  igraph_tree(&g, 5, 2, IGRAPH_TREE_OUT);
  igraph_cliques(&g, &result, 5, 5);
  if (igraph_vector_ptr_size(&result) != 0) return 1;

  igraph_destroy(&g);
  igraph_vector_ptr_destroy(&result);

  return 0;
}
void SgListDlg::CalcTrafo()
{
	listTrafo->clear();

	const QListWidgetItem* pItem = listSGs->currentItem();
	if(!pItem)
		return;

	std::shared_ptr<const SpaceGroups<t_real>> sgs = SpaceGroups<t_real>::GetInstance();
	const SpaceGroups<t_real>::t_vecSpaceGroups* pvecSG = sgs->get_space_groups_vec();
	const unsigned int iSG = pItem->data(Qt::UserRole).toUInt();
	if(iSG >= pvecSG->size())
		return;
	const SpaceGroup<t_real>* psg = pvecSG->at(iSG);

	SpaceGroup<t_real>::t_vec vecIn =
		tl::make_vec({spinX->value(), spinY->value(), spinZ->value(), spinW->value()});

	const std::vector<SpaceGroup<t_real>::t_mat>& vecTrafos = psg->GetTrafos();
	std::vector<SpaceGroup<t_real>::t_vec> vecUnique;

	listTrafo->addItem(create_header_item("All Transformation Results"));
	for(const SpaceGroup<t_real>::t_mat& mat : vecTrafos)
	{
		SpaceGroup<t_real>::t_vec vec = ublas::prod(mat, vecIn);
		listTrafo->addItem(print_vector(vec).c_str());

		if(!is_vec_in_container<std::vector, SpaceGroup<t_real>::t_vec>(vecUnique, vec))
			vecUnique.push_back(vec);
	}

	listTrafo->addItem(create_header_item("Unique Transformation Results"));
	for(const SpaceGroup<t_real>::t_vec& vec : vecUnique)
	{
		listTrafo->addItem(print_vector(vec).c_str());
	}
}
예제 #25
0
int main() {

#   define ROWS 3
#   define COLS 5

    float a[] = {
        6, 2, 1, 4, 5,
        3, 9, 3, 8, 2,
        1, 3, 2, 2, 1
    };
    float x[] = { 5, 4, 3, 2, 1 };
    float b[] = { 54, 78, 28 };

    float bh[ROWS];

#   define LDA COLS
#   define INC 1
#   define F   (sizeof(float))

#   if 0
    cublasSgemv('t', COLS, ROWS,
            1, ad, LDA,
               xd, INC,
            0, bd, INC);
#   endif


    cblas_sgemv(CblasRowMajor, CblasNoTrans, ROWS, COLS,
                1.0, a, LDA,
                     x, INC,
                0.0, bh, INC);

    printf("Computed b:\t"); print_vector(bh, ROWS);
    printf("Expected b:\t"); print_vector(b , ROWS);

    return 0;
}
예제 #26
0
파일: test.c 프로젝트: st-gille/cl-ode
int impl()

{
    size_t dim = 2;
    double x[] = {0.5, 1.0};
    double r[] = {1.5, 0.5};
    if(newtons_method(dim, x, f, df) == FAILURE)
        printf("failed!\n");
    else
    {
        f(x, r);
        printf("f(x) = ");
        print_vector(dim, r);
    }
}
예제 #27
0
void loop(char *s,int r,int m) {
    vector *v = to_int_vector(s,strlen(s));
	vector *e = encode(v,r,m);
    vector *d = decode(e,r,m);
	printf("original:\n");
    print_vector(v, stdout);
    printf("encoded:\n");
    print_vector(e, stdout);
    printf("decoded:\n");
	if (d == NULL) {
		printf("Error: 0s and 1s tied in majority logic.\n");
	} else {
		print_vector(d, stdout);
		if (compare_vectors(v,d) == 0) {
			printf("SAME\n");
		} else {
			printf("DIFFERENT\n");
		}
		destroy_vector(d);
	}
    destroy_vector(e);
	destroy_vector(v);
    printf("\n\n");
}
예제 #28
0
void gen_perm(std::vector<char> v, int l) {
    if (v.size() == 0)
        return;
    if (v.size() < l)
        return;

    gen_perm(v, l+1);

    for (int i = l + 1; i < v.size(); i++) {
        swap(v[l], v[i]);
        print_vector(v);
        gen_perm(v, l+1);
        swap(v[i], v[l]);
    }
}
예제 #29
0
int main(int argc, char *argv[]){
    int rank, size;
    long int i,j;
    long int *A, *local_A, *b, *c, *local_c;
    MPI_Init(&argc, &argv);	
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);	
    MPI_Comm_size(MPI_COMM_WORLD, &size);	

    b = (long int *) malloc(N * sizeof(long int));
    c = (long int *) malloc(M * sizeof(long int));
    
    // Process 0 creates the Matrix and the Vectors.
    if (rank == 0) {
        A = (long int *) malloc(M * N * sizeof(long int));
        for (i = 0; i < M * N; i++) A[i] = i;
        for (i = 0; i < N; i++) b[i] = i;

        // print_matrix(A, M, N);
        // print_vector(b, N);
    }

    // Broadcast the Vector to everyone. 
    MPI_Bcast(b, N, MPI_LONG, 0, MPI_COMM_WORLD);
    // printf("Process %d.\n", rank);
    // print_vector(b, N);     

    // Scatter the Matrix.
    local_A = (long int *) malloc(((M * N) / size) * sizeof(long int));
    MPI_Scatter(A, (M * N) / size, MPI_LONG, local_A, (M * N) / size, MPI_LONG, 0, MPI_COMM_WORLD);
 
    // printf("Process %d:\n", rank);
    // print_matrix(local_A, M / size, N);
    // MPI_Barrier(MPI_COMM_WORLD);

    // Computes the local portion of c.
    local_c = (long int *) malloc((M / size) * sizeof(long int));
    for(i = 0; i < M / size; i++) local_c[i] = 0;
    
    for(i = 0; i < M / size; i++)
        for(j = 0; j < N; j++)
            local_c[i] += local_A[i * N + j] * b[j]; 

    // Multi-broadcast the final result to everyone.
    MPI_Allgather(local_c, M / size, MPI_LONG, c, M / size, MPI_LONG, MPI_COMM_WORLD);
    
    print_vector(c, M);
    MPI_Finalize();
}
예제 #30
0
static void vector_insert(c_vector * p)
{
    c_iterator iter;
    c_vector_insert(p, c_vector_end(p), &array[0]);
    c_vector_insert(p, c_vector_begin(p), &array[2]);
    c_vector_insert(p, c_vector_end(p), &array[4]);
    c_vector_insert(p, c_vector_end(p), &array[6]);
    c_vector_insert(p, c_vector_begin(p), &array[8]);
    
    iter = c_vector_begin(p);
    iter = ITER_POSITIVE_N(iter, 3);
    c_vector_insert(p, iter, &array[5]);   
    
    printf("vector after insertion\n");
    print_vector(p);
}