Example #1
0
std::map<int, Matrix > get_coefficient(LinOp &lin){
	std::map<int, Matrix > coeffs;
	if (lin.type == VARIABLE){
		std::map<int, Matrix> new_coeffs = get_variable_coeffs(lin);
		typedef std::map<int, Matrix >::iterator it_type;
		for(it_type it = new_coeffs.begin(); it != new_coeffs.end(); ++it){
			if(coeffs.count(it->first) == 0)
				coeffs[it->first] = it->second ;
			else
				coeffs[it->first] += it->second;
		}
	}
	else if (lin.has_constant_type()){
		/* ID will be CONSTANT_TYPE */
		std::map<int, Matrix> new_coeffs = get_const_coeffs(lin);
		typedef std::map<int, Matrix >::iterator it_type;
		for(it_type it = new_coeffs.begin(); it != new_coeffs.end(); ++it){
			if(coeffs.count(it->first) == 0)
				coeffs[it->first] = it->second;
			else
				coeffs[it->first] += it->second;
		}
	}
	else {
		/* Multiply the arguments of the function coefficient in order */
		std::vector<Matrix> coeff_mat = get_func_coeffs(lin); 
		for (unsigned i = 0; i < lin.args.size(); i++){
			Matrix coeff = coeff_mat[i];
			std::map<int, Matrix > rh_coeffs = get_coefficient(*lin.args[i]);
			std::map<int,  Matrix > new_coeffs;
			mul_by_const(coeff, rh_coeffs, new_coeffs);

			typedef std::map<int, Matrix>::iterator it_type;
			for (it_type it = new_coeffs.begin(); it != new_coeffs.end(); ++it){
				if(coeffs.count(it->first) == 0)
					coeffs[it->first] = it->second;
				else
					coeffs[it->first] += it->second;		
			}
		}
	}
	return coeffs;
}
Example #2
0
void process_constraint(LinOp & lin, std::vector<double> &V,
                        std::vector<int> &I, std::vector<int> &J,
                        std::vector<double> &constant_vec, int &vert_offset,
                        std::map<int, int> &id_to_col, int & horiz_offset){
	/* Get the coefficient for the current constraint */
	std::map<int, Matrix > coeffs = get_coefficient(lin);	

	typedef std::map<int, Matrix >::iterator it_type;
	for(it_type it = coeffs.begin(); it != coeffs.end(); ++it){
		int id = it->first;	// Horiz offset determined by the id
		Matrix block = it->second;
		if (id == CONSTANT_ID) { // Add to CONSTANT_VEC if linop is constant
			extend_constant_vec(constant_vec, vert_offset, block);	
		}
		else {
			int offset = get_horiz_offset(id, id_to_col, horiz_offset, lin);
			add_matrix_to_vectors(block, V, I, J, vert_offset, offset);
		}
	}
}
Example #3
0
int main(){
  CGAL::set_pretty_mode(std::cout);
  typedef CGAL::Polynomial_type_generator<int,2>::Type Poly_2;
  typedef CGAL::Polynomial_traits_d<Poly_2>            PT_2;
  
  //construction using shift 
  Poly_2 x = PT_2::Shift()(Poly_2(1),1,0); // = x^1
  Poly_2 y = PT_2::Shift()(Poly_2(1),1,1); // = y^1
  
  Poly_2 F // = (11*x^2 + 5*x)*y^4 + (7*x^2)*y^3
    = 11 * CGAL::ipower(y,4) * CGAL::ipower(x,2) 
    + 5 * CGAL::ipower(y,4)  * CGAL::ipower(x,1) 
    + 7 * CGAL::ipower(y,3)  * CGAL::ipower(x,2);  
  std::cout << "The bivariate polynomial F: " << F <<"\n"<< std::endl;
  
  PT_2::Get_coefficient get_coefficient;
  std::cout << "Coefficient of y^0: "<< get_coefficient(F,0) << std::endl;
  std::cout << "Coefficient of y^1: "<< get_coefficient(F,1) << std::endl;
  std::cout << "Coefficient of y^2: "<< get_coefficient(F,2) << std::endl;
  std::cout << "Coefficient of y^3: "<< get_coefficient(F,3) << std::endl;
  std::cout << "Coefficient of y^4: "<< get_coefficient(F,4) << std::endl;
  std::cout << "Coefficient of y^5: "<< get_coefficient(F,5) << std::endl; 
  std::cout << std::endl;
  
  PT_2::Leading_coefficient lcoeff;
  std::cout << "Leading coefficient with respect to y:           "
            << lcoeff(F)   // = 11*x^2 + 5*x
            << std::endl;

  PT_2::Get_innermost_coefficient get_icoeff;
  std::cout << "Innermost coefficient of monomial x^1y^4:        "
            << get_icoeff(F,CGAL::Exponent_vector(1,4)) // = 5
            << std::endl;
  
  PT_2::Innermost_leading_coefficient ilcoeff;
  std::cout << "Innermost leading coefficient with respect to y: "
            << ilcoeff(F) // = 11 
            << std::endl; 
}
Example #4
0
//test raid6 algorithm
int main(int argc, char *argv[])
{

	char disk_data[DISKNUM][BLOCKSIZE];
	char read_data[DISKNUM-2][BLOCKSIZE];
	int disk_id;
	char temp_char;
	int i, j;
	int code_disk_id, parity_disk_id;
	int data_disk_coeff;

	int disk_failed_no = DISK_FAILED_NUM;
	int disk_first_id = FIRST_FAIL_ID;
	int disk_second_id = SECOND_FAIL_ID;
	int disk_another_failed_id;	

	int g1, g2, g12;
	char P_temp[BLOCKSIZE], Q_temp[BLOCKSIZE];

	gen_tables(8);

	printf("\nPrint gflog(x)\n");
	for (i=0; i<256; i++){
		printf("%d(%d) ",i,gflog[i]);
	}
	printf("\n");

        printf("\nPrint gfilog(x)\n");
        for (i=0; i<256; i++){
                printf("%d(%d) ",i,gfilog[i]);
        }
        printf("\n");


	for (i=0; i<DISKNUM; i++){
		if (i < DISKNUM-2){
			temp_char = TEST_DATA_BEGIN + i;
		}
		else{
			temp_char = 0;
		}

		for (j=0; j<BLOCKSIZE; j++){
			disk_data[i][j] = temp_char;
		}
	}

	printf("\n****** PRINT DISK DATA ******\n");
        for (i=0; i<DISKNUM; i++){
                printf("Disk id: %d\n",i);
                for (j=0; j<BLOCKSIZE; j++){
                        printf("%c(%d) ",disk_data[i][j],disk_data[i][j]);
                }
                printf("\n");
        }


	//write P and Q

	code_disk_id = DISKNUM -1;
	parity_disk_id = DISKNUM - 2;

	for (i=0; i<DISKNUM; i++){
		if ( (i != parity_disk_id) && (i != code_disk_id) ){
			
			for (j=0; j<BLOCKSIZE; j++){

				//calculate P
				disk_data[parity_disk_id][j] = 
					disk_data[parity_disk_id][j] ^ disk_data[i][j];
			

                                //calculate the coefficient of the data block
                                data_disk_coeff = i;

                                if (i > code_disk_id){   
                                        (data_disk_coeff)--;                  
                                }
                                if (i > parity_disk_id){
                                        (data_disk_coeff)--;
                                }        
                                data_disk_coeff = DISKNUM - 3 - data_disk_coeff;
                                data_disk_coeff = get_coefficient(data_disk_coeff);

                                printf("\ndata disk coefficient = %d\n",data_disk_coeff);

				//calculate Q
				temp_char = disk_data[code_disk_id][j];
				disk_data[code_disk_id][j] = temp_char ^ 
					(char)gf_mul((unsigned char)disk_data[i][j],data_disk_coeff,FIELDSIZE);

				printf(" newQ=%c(%d) \n",disk_data[code_disk_id][j],
					(unsigned int)disk_data[code_disk_id][j]);
			}

		}
	}


        printf("\n****** PRINT DISK DATA ******\n");
        for (i=0; i<DISKNUM; i++){
                printf("Disk id: %d\n",i);
                for (j=0; j<BLOCKSIZE; j++){
                        printf("%c(%d) ",disk_data[i][j],disk_data[i][j]);
                }
                printf("\n");
        }


	//read data

	for (i=0; i<DISKNUM-2; i++){
		for (j=0; j<BLOCKSIZE; j++){
			read_data[i][j] = 0;
		}
	}


	for (disk_id=0; disk_id<DISKNUM-2; disk_id++){

		disk_another_failed_id = disk_second_id;

		if (disk_id == disk_first_id){
			disk_another_failed_id = disk_second_id;
		}
		else if (disk_id == disk_second_id){
			disk_another_failed_id = disk_first_id;
		}

		printf("\nDisk ID: %d, another failed disk ID: %d\n",disk_id, disk_another_failed_id);

		//case of the disk is not failed
		if ( (disk_failed_no == 0) || 
				((disk_id != disk_first_id) && (disk_id != disk_second_id)) ){
			printf("NO DISK FAIL\n");
			for (j=0; j<BLOCKSIZE; j++){
				read_data[disk_id][j] = disk_data[disk_id][j];
			}
		}
		//cases of single disk fail
		else if ( (disk_failed_no == 1) ||
		((disk_failed_no == 2) && (disk_another_failed_id == code_disk_id)) ){
			printf("One disk fail, or 1 data plus Q disk fail\n");
			for (i = 0; i < DISKNUM; i++){
				if ( (i != disk_id) && (i != code_disk_id) ){
					for (j=0; j<BLOCKSIZE; j++){
						read_data[disk_id][j] =
							read_data[disk_id][j] ^ disk_data[i][j];
					}
				}
			}
		}
		else if (disk_failed_no == 2){
			if (disk_another_failed_id == parity_disk_id){
				printf("1 data plus P disk fail\n");

				//calculate Q'
				for (i=0; i<DISKNUM; i++){
                                        if ( ((i != disk_first_id) && (i != disk_second_id)) 
					&& (i != parity_disk_id) && (i != code_disk_id) ){
                                                for (j=0; j < BLOCKSIZE; j++){

                                			//calculate the coefficient of the data block
                                			data_disk_coeff = i;

                                			if (i > code_disk_id){
                                        			(data_disk_coeff)--;
                                			}
                                			if (i > parity_disk_id){
                                        			(data_disk_coeff)--;
                                			}
                                			data_disk_coeff = DISKNUM - 3 - data_disk_coeff;              
                                			data_disk_coeff = get_coefficient(data_disk_coeff);

                                			printf("\ndata disk coefficient = %d\n",data_disk_coeff);       

                                                        temp_char = read_data[disk_id][j];
                                                        read_data[disk_id][j] = read_data[disk_id][j] ^
								(char)gf_mul((unsigned char)disk_data[i][j],data_disk_coeff,FIELDSIZE);
                                                }
                                        }

                                	for (j=0; j<BLOCKSIZE; j++){
                                        	printf("Q'=%c(%d) ",read_data[disk_id][j],read_data[disk_id][j]);
                                	}

				}


                                //calculate Q xor Q'
                                for (j=0; j < BLOCKSIZE; j++){
                                	read_data[disk_id][j] = read_data[disk_id][j] ^ disk_data[code_disk_id][j];
                                }
        
                                for (j=0; j<BLOCKSIZE; j++){
                                        printf("Q'+Q=%c(%d) ",read_data[disk_id][j],read_data[disk_id][j]);
                                }

                                //calculate the coefficient of the data block
                                data_disk_coeff = disk_id;

                                if (disk_id > code_disk_id){
                                        (data_disk_coeff)--;
                                }
                                if (disk_id > parity_disk_id){
                                        (data_disk_coeff)--;
                                }
                                data_disk_coeff = DISKNUM - 3 - data_disk_coeff;
				data_disk_coeff = get_coefficient(data_disk_coeff);

				printf("\ndata disk coefficient = %d\n",data_disk_coeff);

                                //decode the origianl data block
                                for (j=0; j < BLOCKSIZE; j++){
					temp_char = read_data[disk_id][j];
                                        read_data[disk_id][j] = (char)gf_div((unsigned char)temp_char,data_disk_coeff,FIELDSIZE);
                                }
			}
			else{
			//case of two data disk fail
				printf("2 data disk fail\n");

				//calculate g1
				g1 = disk_id;
                                if (g1 > code_disk_id){
                                        (g1)--;
                                }
                                if (g1 > parity_disk_id){
                                        (g1)--;
                                }
                                g1 = DISKNUM - 3 - g1;
                                g1 = get_coefficient(g1);
				printf("g1=%d \n",g1);

				//calculate g2
                                g2 = disk_another_failed_id;
                                if (g2 > code_disk_id){     
                                        (g2)--;             
                                }
                                if (g2 > parity_disk_id){     
                                        (g2)--;             
                                }
                                g2 = DISKNUM - 3 - g2;                          
                                g2 = get_coefficient(g2);   
				printf("g2=%d \n",g2);

				//calculate g12
				g12 = g1 ^ g2;

				//calculate P'
				for (j=0; j<BLOCKSIZE; j++){
					P_temp[j] = 0;
				}

				for (i=0; i<DISKNUM; i++){
					if ( (i != disk_first_id) && (i != disk_second_id) &&
						(i != parity_disk_id) && (i != code_disk_id) )
					{
						for (j=0; j<BLOCKSIZE; j++){
							P_temp[j] = P_temp[j] ^ disk_data[i][j];
						}
					}
				}

				for (j=0; j<BLOCKSIZE; j++){
					P_temp[j] = P_temp[j] ^ disk_data[parity_disk_id][j];
					//P_temp = P' xor P
				}


				//calculate Q'
				for (j=0; j<BLOCKSIZE; j++){
					Q_temp[j] = 0;
				}

                                for (i=0; i<DISKNUM; i++){
                                        if ( ((i != disk_first_id) && (i != disk_second_id)) 
                                        && (i != parity_disk_id) && (i != code_disk_id) ){

                                                //calculate the coefficient of the data block
                                                data_disk_coeff = i;

                                                if (i > code_disk_id){
                                                        (data_disk_coeff)--;
                                                }
                                                if (i > parity_disk_id){
                                                        (data_disk_coeff)--;
                                                }
                                                data_disk_coeff = DISKNUM - 3 - data_disk_coeff;              
                                                data_disk_coeff = get_coefficient(data_disk_coeff);

                                                printf("\ndata disk coefficient = %d\n",data_disk_coeff);       

                                                for (j=0; j < BLOCKSIZE; j++){
                                                        temp_char = Q_temp[j];
                                                        Q_temp[j] = temp_char ^ 
							(char)gf_mul((unsigned char)disk_data[i][j],data_disk_coeff,FIELDSIZE);
                                                }
                                        }

                                        for (j=0; j<BLOCKSIZE; j++){
                                                printf("Q'=%c(%d) ",Q_temp[j],Q_temp[j]);
                                        }
                                }

				//calculate D
				for (j=0; j<BLOCKSIZE; j++){
					temp_char = (char)(gf_mul(g2,(unsigned char)P_temp[j],FIELDSIZE) 
								^ Q_temp[j] ^ disk_data[code_disk_id][j]);
					read_data[disk_id][j] = (char)gf_div((unsigned char)temp_char, g12, FIELDSIZE);
				}
			}
		}
	}


	//print data read
	printf("\n****** PRINT READ DATA ******\n");
        for (i=0; i<DISKNUM-2; i++){
                printf("Read disk id: %d\n",i);
                for (j=0; j<BLOCKSIZE; j++){
                        printf("%c(%d) ",read_data[i][j],read_data[i][j]);
                }
                printf("\n");
        }

	return 0;
}
Example #5
0
 /**
  * Just an alias for get_coefficient() method.
  */
 inline double get_coef(int i, int j)
 {
   return get_coefficient(i, j);
 }