Example #1
0
/* Makes a polynomial of degree degree with all terms		*
 * having coefficient 1.					*/
static struct polynomial make_full(unsigned int degree)
{
	unsigned int a1, a2, a3, a4;
	struct polynomial uit;
	struct term *uitterm;
	struct term **ptrterm;
	uitterm = NULL;
	uit.degree = degree;
	uit.leading = NULL;

	for (a1 = 0; (d1*a1 <= degree); a1++) {
	  for (a2 = 0; (d1*a1 + d2*a2 <= degree); a2++) {
	    for (a3 = 0; (d1*a1 + d2*a2 + d3*a3 <= degree); a3++) {
	      if ((degree - (a1*d1 + a2*d2 + a3*d3)) % d4 == 0) {
		a4 = (degree - (a1*d1 + a2*d2 + a3*d3))/d4;
		make_term(&uitterm);
		uitterm->n1 = a1;
		uitterm->n2 = a2;
		uitterm->n3 = a3;
		uitterm->n4 = a4;
		ito_sc(1, uitterm->c);
		ptrterm = &(uit.leading);
		while ((*ptrterm) && (kleiner(uitterm, *ptrterm) == KLEINER)) {
			ptrterm = &((*ptrterm)->next);
		}
		uitterm->next = *ptrterm;
		*ptrterm = uitterm;
		uitterm=NULL;
	      }
	    }
	  }
	}
	return(uit);
}
Example #2
0
poly_t* new_poly_from_string(const char* str)
{
	char	c;
	poly_t*	p = malloc(sizeof *p);
	{
		p->size		= 0;
		p->coeffs	= NULL;
	}

	while ((c = *str) != '\0') {

		if (isspace(c)) {
			str++;
			continue;
		}

		if (isplus(c) || isminus(c) || isdigit(c) || isvar(c)) {
			make_term(&str, p);
		}
	}

	printf("%zu\ncoeffs:", p->size);

	for (int i = 0; i < p->size; ++i)
		printf("%d ", p->coeffs[i]);
	putchar('\n');

//	printf("ALEXANDER\n");
//	print_poly(p);
//	printf("S**T\n");
//	putchar('\n');
	return p;
}
Example #3
0
term* make_implicit(variable* name) {
  term* ans = make_term();
  ans->tag = IMPLICIT;
  ans->right = make_var(name);

  return ans;
}
Example #4
0
int main ()
{
  typedef make_tree<std::string, std::string>::type tree;
  typedef std::list<tree> term_list;
  std::string const* tag_str=(std::string const*)0L;

  // a(b(), c)
  term_list terms;
  terms.push_back (make_term(std::string("b"), term_list (), tag_str));
  terms.push_back (make_var(std::string("c")));
  tree t = make_term(std::string("a"), terms, tag_str);

  std::cout << string_of_term (t) << std::endl;

  return 0;
}
Example #5
0
term* make_app(term* a, term* b) {
  term* ans = make_term();
  ans->tag = APP;
  ans->left = a;
  ans->right = b;
  
  return ans;
}
Example #6
0
term* make_lambda(variable* var, term* A, term* b) {
  term* ans = make_term();
  ans->tag = LAM;
  ans->var = var;
  ans->left = A;
  ans->right = b;

  return ans;
}
Example #7
0
term* make_pi(variable* x, term* A, term* B) {
  term* ans = make_term();
  ans->tag = PI;
  ans->var = x;
  ans->left = A;
  ans->right = B;

  return ans;
}
Example #8
0
term* make_datatype_term(variable* name, int num_params, int num_indices) {
  term* ans = make_term();
  ans->tag = DATATYPE;
  ans->var = name;
  ans->num_params = num_params;
  ans->params = calloc(num_params, sizeof(term*));
  ans->num_indices = num_indices;
  ans->indices = calloc(num_indices, sizeof(term*));
  return ans;
}
Example #9
0
term* make_elim(variable* name, int num_args, int num_params, int num_indices) {
  term* ans = make_term();
  ans->tag = ELIM;
  ans->var = name;
  ans->num_args = num_args;
  ans->args = calloc(num_args, sizeof(term*));
  ans->num_params = num_params;
  ans->params = calloc(num_params, sizeof(term*));
  ans->num_indices = num_indices;
  ans->indices = calloc(num_indices, sizeof(term*));
  return ans;
}
Example #10
0
term* make_intro(variable* name, term *type, int num_args, int num_params, int num_indices) {
  term* ans = make_term();
  ans->tag = INTRO;
  ans->var = name;
  ans->left = type;
  ans->num_args = num_args;
  ans->args = calloc(num_args, sizeof(term*));
  ans->num_params = num_params;
  ans->params = calloc(num_params, sizeof(term*));
  ans->num_indices = num_indices;
  ans->indices = calloc(num_indices, sizeof(term*));
  return ans;
}
Example #11
0
/* Frobenius operation by raising varaibles tot the	*
 * pth power.						*/
struct polynomial frobenius(struct polynomial f)
{
	struct polynomial uit;
	struct term *fterm;
	struct term **ptrterm;
	uit.leading = NULL;
	ptrterm = NULL;

	uit.degree = p * f.degree;
	ptrterm = &(uit.leading);
	fterm = f.leading;
	while (fterm) {
		make_term(ptrterm);
		sc_copy(fterm->c, (*ptrterm)->c);
		(*ptrterm)->n1 = p*fterm->n1;
		(*ptrterm)->n2 = p*fterm->n2;
		(*ptrterm)->n3 = p*fterm->n3;
		(*ptrterm)->n4 = p*fterm->n4;
		ptrterm = &((*ptrterm)->next);
		fterm = fterm->next;
	}
	return(uit);
}
Example #12
0
term* term_dup(term* t) {
  if (t == NULL) return NULL;

  term* ans = make_term();
  ans->tag = t->tag;
  ans->var = variable_dup(t->var);
  ans->left = term_dup(t->left);
  ans->right = term_dup(t->right);

  DUP_VEC(ans->num_args, ans->args,
          t->num_args, t->args,
          term_dup, struct term*);

  DUP_VEC(ans->num_params, ans->params,
          t->num_params, t->params,
          term_dup, struct term*);

  DUP_VEC(ans->num_indices, ans->indices,
          t->num_indices, t->indices,
          term_dup, struct term*);


  return ans;
}
Example #13
0
term* make_hole() {
  term* ans = make_term();
  ans->tag = HOLE;

  return ans;
}
Example #14
0
term* make_type() {
  term* ans = make_term();
  ans->tag = TYPE;

  return ans;
}
Example #15
0
/* Ouputs the derivative. 					*
 * The result is nonsense if the degree of f is too low.	*/
struct polynomial deriv(struct polynomial f, unsigned int i)
{
	mscalar c;
	struct polynomial uit;
	struct term *fterm;
	struct term **ptrterm;
	uit.leading = NULL;
	ptrterm = NULL;

	fterm = f.leading;
	switch (i) {
		case 1: 
		uit.degree = (f.degree > d1) ? (f.degree - d1) : 0;
		ptrterm = &(uit.leading);
		while (fterm) {
			sc_imult(fterm->n1, fterm->c, c);
			if (!sc_is_zero(c)) {
				make_term(ptrterm);
				sc_copy(c, (*ptrterm)->c);
				(*ptrterm)->n1 = fterm->n1 - 1;
				(*ptrterm)->n2 = fterm->n2;
				(*ptrterm)->n3 = fterm->n3;
				(*ptrterm)->n4 = fterm->n4;
				ptrterm = &((*ptrterm)->next);
			}
			fterm = fterm->next;
		}
		return(uit);

		case 2: 
		uit.degree = (f.degree > d2) ? (f.degree - d2) : 0;
		ptrterm = &(uit.leading);
		while (fterm) {
			sc_imult(fterm->n2, fterm->c, c);
			if (!sc_is_zero(c)) {
				make_term(ptrterm);
				sc_copy(c, (*ptrterm)->c);
				(*ptrterm)->n1 = fterm->n1;
				(*ptrterm)->n2 = fterm->n2 - 1;
				(*ptrterm)->n3 = fterm->n3;
				(*ptrterm)->n4 = fterm->n4;
				ptrterm = &((*ptrterm)->next);
			}
			fterm = fterm->next;
		}
		return(uit);

		case 3:
		uit.degree = (f.degree > d3) ? (f.degree - d3) : 0;
		ptrterm = &(uit.leading);
		while (fterm) {
			sc_imult(fterm->n3, fterm->c, c);
			if (!sc_is_zero(c)) {
				make_term(ptrterm);
				sc_copy(c, (*ptrterm)->c);
				(*ptrterm)->n1 = fterm->n1;
				(*ptrterm)->n2 = fterm->n2;
				(*ptrterm)->n3 = fterm->n3 - 1;
				(*ptrterm)->n4 = fterm->n4;
				ptrterm = &((*ptrterm)->next);
			}
			fterm = fterm->next;
		}
		return(uit);
		
		case 4:
		uit.degree = (f.degree > d4) ? (f.degree - d4) : 0;
		ptrterm = &(uit.leading);
		while (fterm) {
			sc_imult(fterm->n4, fterm->c, c);
			if (!sc_is_zero(c)) {
				make_term(ptrterm);
				sc_copy(c, (*ptrterm)->c);
				(*ptrterm)->n1 = fterm->n1;
				(*ptrterm)->n2 = fterm->n2;
				(*ptrterm)->n3 = fterm->n3;
				(*ptrterm)->n4 = fterm->n4 - 1;
				ptrterm = &((*ptrterm)->next);
			}
			fterm = fterm->next;
		}
		return(uit);
		
		default:
		printf("Wrong again honey!");
		exit(1);
	}
	exit(1);
}
Example #16
0
term *make_var(variable *var) {
  term *ans = make_term();
  ans->tag = VAR;
  ans->var = var;
  return ans;
}
Example #17
0
/* Makes a random polynomial of degree degree.		*
 * The result may be the zero polynomial!		*/
static struct polynomial make_initial_pol(unsigned int degree, int print)
{
	unsigned int a1, a2, a3, a4;
	int c;
	struct polynomial uit;
	struct term *uitterm;
	struct term **ptrterm;
	uitterm = NULL;
	uit.degree = degree;
	uit.leading = NULL;

	if (!count_sum(degree)) {
		printf("No monomials of degree %d! Stop.\n", degree);
		exit(1);
	}

	for (a1 = 0; (d1*a1 <= degree);a1++) {
	  for (a2 = 0; (d1*a1 + d2*a2 <= degree);a2++) {
	    for (a3 = 0; (d1*a1 + d2*a2 + d3*a3 <= degree);a3++) {
	      if ((degree - (a1*d1 + a2*d2 + a3*d3)) % d4 == 0) {
		a4 = (degree - (a1*d1 + a2*d2 + a3*d3))/d4;
		/* Dummy input at first. */
		c = 1;
		/* Create the new term to be put in. */
		make_term(&uitterm);
		uitterm->n1 = a1;
		uitterm->n2 = a2;
		uitterm->n3 = a3;
		uitterm->n4 = a4;
		uitterm->c = c;
		ptrterm = &(uit.leading);
		while ((*ptrterm) && (kleiner(uitterm, *ptrterm) == KLEINER)) {
			ptrterm = &((*ptrterm)->next);
		}
		uitterm->next = *ptrterm;
		*ptrterm = uitterm;
		uitterm = NULL;
	      }
	    }
	  }
	}
	if (print) {
		uitterm = uit.leading;
		while (uitterm) {
			a1 = uitterm->n1;
			a2 = uitterm->n2;
			a3 = uitterm->n3;
			a4 = uitterm->n4;
			c = 0;
			printf("Coefficient of   ");
			if (a1) {
				printf("x^%d", a1);
				c++;
			}
			if ((a1) && (a2 + a3 + a4)) {
				printf(" * ");
				c++;
			}
			if (a2) {
				printf("y^%d", a2);
				c++;
			}
			if ((a2) && (a3 + a4)) {
				printf(" * ");
				c++;
			}
			if (a3) {
				printf("z^%d", a3);
				c++;
			}
			if ((a3) && (a4)) {
				printf(" * ");
				c++;
			}
			if (a4) {
				printf("w^%d", a4);
				c++;
			}
			while (8 - c) {
				printf("   ");
				c++;
			}
			printf("= ");
			print_scalar(uitterm->c);
			printf("\n");
			uitterm = uitterm->next;
		}
	}
	return(uit);
}
Example #18
0
int main(int argc, char *argv[])
{
  tt = make_term(3, 10);

  is_termlog("Termlog initially",
      NULL);
  is_display_text("Display initially",
      "          ",
      "          ",
      "          ");

  tickit_term_goto(tt, 1, 5);
  is_termlog("Termlog after goto",
      GOTO(1,5),
      NULL);

  tickit_term_print(tt, "foo");

  is_termlog("Termlog after print",
      PRINT("foo"),
      NULL);
  is_display_text("Display after print",
      "          ",
      "     foo  ",
      "          ");

  char c = 'l';
  tickit_term_printn(tt, &c, 1);

  is_termlog("Termlog after printn 1",
      PRINT("l"),
      NULL);
  is_display_text("Display after printn 1",
      "          ",
      "     fool ",
      "          ");

  tickit_term_goto(tt, 2, 0);
  tickit_term_print(tt, "Ĉu vi?");

  is_termlog("Termlog after print UTF-8",
      GOTO(2,0), PRINT("Ĉu vi?"),
      NULL);
  is_display_text("Display after print UTF-8",
      "          ",
      "     fool ",
      "Ĉu vi?    ");

  // U+FF10 = Fullwidth digit zero = EF BC 90
  tickit_term_print(tt, "\xef\xbc\x90");

  is_termlog("Termlog after print UTF-8 fullwidth",
      PRINT("0"),
      NULL);
  is_display_text("Display after print UTF-8 fullwidth",
      "          ",
      "     fool ",
      "Ĉu vi?0  ");

  tickit_term_clear(tt);

  is_termlog("Termlog after clear",
      CLEAR(),
      NULL);
  is_display_text("Display after clear",
      "          ",
      "          ",
      "          ");

  TickitPen *fg_pen = tickit_pen_new_attrs(TICKIT_PEN_FG, 3, -1);

  tickit_term_setpen(tt, fg_pen);

  is_termlog("Termlog after setpen",
      SETPEN(.fg=3),
      NULL);

  TickitPen *bg_pen = tickit_pen_new_attrs(TICKIT_PEN_BG, 6, -1);

  tickit_term_chpen(tt, bg_pen);

  is_termlog("Termlog after chpen",
      SETPEN(.fg=3, .bg=6),
      NULL);

  // Now some test content for scrolling
  fillterm(tt);
  tickit_mockterm_clearlog((TickitMockTerm *)tt);

  is_display_text("Display after scroll fill",
      "0000000000",
      "1111111111",
      "2222222222");

  ok(tickit_term_scrollrect(tt, 0,0,3,10, +1,0), "Scrollrect down OK");
  is_termlog("Termlog after scroll 1 down",
      SCROLLRECT(0,0,3,10, +1,0),
      NULL);
  is_display_text("Display after scroll 1 down",
      "1111111111",
      "2222222222",
      "          ");

  ok(tickit_term_scrollrect(tt, 0,0,3,10, -1,0), "Scrollrect up OK");
  is_termlog("Termlog after scroll 1 up",
      SCROLLRECT(0,0,3,10, -1,0),
      NULL);
  is_display_text("Display after scroll 1 up",
      "          ",
      "1111111111",
      "2222222222");

  fillterm(tt);
  tickit_mockterm_clearlog((TickitMockTerm *)tt);

  tickit_term_scrollrect(tt, 0,0,2,10, +1,0);
  is_termlog("Termlog after scroll partial 1 down",
      SCROLLRECT(0,0,2,10, +1,0),
      NULL);
  is_display_text("Display after scroll partial 1 down",
      "1111111111",
      "          ",
      "2222222222");

  tickit_term_scrollrect(tt, 0,0,2,10, -1,0);
  is_termlog("Termlog after scroll partial 1 up",
      SCROLLRECT(0,0,2,10, -1,0),
      NULL);
  is_display_text("Display after scroll partial 1 up",
      "          ",
      "1111111111",
      "2222222222");

  for(int line = 0; line < 3; line++)
    tickit_term_goto(tt, line, 0), tickit_term_print(tt, "ABCDEFGHIJ");
  tickit_mockterm_clearlog((TickitMockTerm *)tt);

  tickit_term_scrollrect(tt, 0,5,1,5, 0,2);
  is_termlog("Termlog after scroll right",
      SCROLLRECT(0,5,1,5, 0,+2),
      NULL);
  is_display_text("Display after scroll right",
      "ABCDEHIJ  ",
      "ABCDEFGHIJ",
      "ABCDEFGHIJ");

  tickit_term_scrollrect(tt, 1,5,1,5, 0,-3);
  is_termlog("Termlog after scroll left",
      SCROLLRECT(1,5,1,5, 0,-3),
      NULL);
  is_display_text("Display after scroll left",
      "ABCDEHIJ  ",
      "ABCDE   FG",
      "ABCDEFGHIJ");

  tickit_term_goto(tt, 2, 3);
  tickit_term_erasech(tt, 5, -1);

  is_termlog("Termlog after erasech",
      GOTO(2,3),
      ERASECH(5, -1),
      NULL);
  is_display_text("Display after erasech",
      "ABCDEHIJ  ",
      "ABCDE   FG",
      "ABC     IJ");

  return exit_status();
}