Example #1
0
void main( )
{
	struct polynode *first, *second, *mult ;
	int i = 1 ;

	first = second = mult = NULL ;  /* empty linked lists */

	poly_append ( &first, 3, 5 ) ;
	poly_append ( &first, 2, 4 ) ;
	poly_append ( &first, 1, 2 ) ;

	display_poly ( first ) ;

	poly_append ( &second, 1, 6 ) ;
	poly_append ( &second, 2, 5 ) ;
	poly_append ( &second, 3, 4 ) ;

	printf ( "\n\n" ) ;
	display_poly ( second ) ;

	printf ( "\n" );
	while ( i++ < 79 )
		printf ( "-" ) ;

	poly_multiply ( first, second, &mult ) ;

	printf ( "\n\n" ) ;
	display_poly ( mult ) ;
}
/* Calculate the values of the constants V(J,R) as
 * described in BFN section 3.3.
 *
 *   px = appropriate irreducible polynomial for current dimension
 *   pb = polynomial defined in section 2.3 of BFN.
 * pb is modified
 */
static void calculate_v(
  const int px[], int px_degree,
  int pb[], int * pb_degree,
  int v[], int maxv
  )
{
  const int nonzero_element = 1;    /* nonzero element of Z_2  */
  const int arbitrary_element = 1;  /* arbitray element of Z_2 */

  /* The polynomial ph is px**(J-1), which is the value of B on arrival.
   * In section 3.3, the values of Hi are defined with a minus sign:
   * don't forget this if you use them later !
   */
  int ph[NIED2_MAX_DEGREE+1];
  /* int ph_degree = *pb_degree; */
  int bigm = *pb_degree;      /* m from section 3.3 */
  int m;                      /* m from section 2.3 */
  int r, k, kj;

  for(k=0; k<=NIED2_MAX_DEGREE; k++) {
    ph[k] = pb[k];
  }

  /* Now multiply B by PX so B becomes PX**J.
   * In section 2.3, the values of Bi are defined with a minus sign :
   * don't forget this if you use them later !
   */
   poly_multiply(px, px_degree, pb, *pb_degree, pb, pb_degree);
   m = *pb_degree;

  /* Now choose a value of Kj as defined in section 3.3.
   * We must have 0 <= Kj < E*J = M.
   * The limit condition on Kj does not seem very relevant
   * in this program.
   */
  /* Quoting from BFN: "Our program currently sets each K_q
   * equal to eq. This has the effect of setting all unrestricted
   * values of v to 1."
   * Actually, it sets them to the arbitrary chosen value.
   * Whatever.
   */
  kj = bigm;

  /* Now choose values of V in accordance with
   * the conditions in section 3.3.
   */
  for(r=0; r<kj; r++) {
    v[r] = 0;
  }
  v[kj] = 1;


  if(kj >= bigm) {
    for(r=kj+1; r<m; r++) {
      v[r] = arbitrary_element;
    }
  }
  else {
    /* This block is never reached. */

    int term = NIED2_SUB(0, ph[kj]);

    for(r=kj+1; r<bigm; r++) {
      v[r] = arbitrary_element;

      /* Check the condition of section 3.3,
       * remembering that the H's have the opposite sign.  [????????]
       */
      term = NIED2_SUB(term, NIED2_MUL(ph[r], v[r]));
    }

    /* Now v[bigm] != term. */
    v[bigm] = NIED2_ADD(nonzero_element, term);

    for(r=bigm+1; r<m; r++) {
      v[r] = arbitrary_element;
    }
  }

  /* Calculate the remaining V's using the recursion of section 2.3,
   * remembering that the B's have the opposite sign.
   */
  for(r=0; r<=maxv-m; r++) {
    int term = 0;
    for(k=0; k<m; k++) {
      term = NIED2_SUB(term, NIED2_MUL(pb[k], v[r+k]));
    }
    v[r+m] = term;
  }
}
Example #3
0
int main (int numb, char *argv[] ) {

    // Read in file through the command line and close program is file is invalid.
    FILE *cmdFile = fopen(argv[1], "r");
    if (cmdFile == NULL) {
        printf("Error opening file: %s\nThis file may not exst or is empty.\nExiting program now...\n", argv[1]);
        exit(-1);
    }

    //printf("Reading this file: %s\n", argv[1]);

    char command[MAXCOMMANDLENGTH];
    int index;
    int index1;
    int index2;
    int coeffient;
    int exponent;
    int scalar;
    char string[MAXSTRINGLENGTH];

    // Read each line until white space
    while ( fscanf(cmdFile, "%s", command) != EOF ) {

        // If the command == ADDTERM
        if ( strcmp(command, addterm) == 0) {

            // Take in the next 3 values
            fscanf(cmdFile, "%d", &index );
            fscanf(cmdFile, "%d", &coeffient );
            fscanf(cmdFile, "%d", &exponent );

            // Print what the command line wants to perform
            printf("CMD: %s, Poly: %d, Coeff: %d, EXP: %d\n", command, index, coeffient, exponent );

            // Addterm to this array of at location array index
            polynomials[index] = poly_addterm(polynomials[index], coeffient, exponent);

            // construct the polynomial string
            polyString(polynomials[index], string);
            printf("Poly%d =%s\n\n", index, string);
        }
        else if ( strcmp(command, multiply) == 0 ) {

            // Take in the next 2 values
            fscanf(cmdFile, "%d", &index );
            fscanf(cmdFile, "%d", &scalar );

            // Print what the command line wants to excute
            printf("CMD: %s, Poly: %d, Multiplier: %d\n", command, index, scalar );

            // Stores the polynomial struct to this location in the array
            polynomials[index] = poly_multiply(polynomials[index], scalar);

            // construct the polynomial string and prints it
            polyString(polynomials[index], string);
            printf("Poly%d =%s\n\n", index, string);
        }
        else if ( strcmp(command, adding) == 0 ) {

            // Take in the next 3 values
            fscanf(cmdFile, "%d", &index );
            fscanf(cmdFile, "%d", &index1 );
            fscanf(cmdFile, "%d", &index2 );

            // Print what the command line wants to excute
            printf("CMD: %s, Sum: %d, Op1: %d, Op2: %d\n", command, index, index1, index2 );

            polynomials[index] = poly_adding(polynomials[index], polynomials[index1], polynomials[index2]);

            // construct the polynomial string and prints it
            polyString(polynomials[index], string);
            printf("Poly%d =%s\n\n", index, string);
        }
        else if ( strcmp(command, subtract) == 0 ) {

            // Take in the next 3 values
            fscanf(cmdFile, "%d", &index );
            fscanf(cmdFile, "%d", &index1 );
            fscanf(cmdFile, "%d", &index2 );

            // Print what the command line wants to excute
            printf("CMD: %s, Diff: %d, Op1: %d, Op2: %d\n", command, index, index1, index2 );

            polynomials[index] = poly_subtract(polynomials[index], polynomials[index1], polynomials[index2]);

            // construct the polynomial string and prints it
            polyString(polynomials[index], string);
            printf("Poly%d =%s\n\n", index, string);
        }
    }

    return 0;
}