static term * comterm(Static *S, int i) { int nlin; cexp *c; cexp1 *c1; linpart *L, *Le; expr_v ev, *vp; term *T; ASL_fg* asl = S->asl; if (i < ncom0) { c = cexps + i; T = ewalk(S, c->e); L = c->L; nlin = c->nlin; } else { c1 = cexps1 + (i - ncom0); T = ewalk(S, c1->e); L = c1->L; nlin = c1->nlin; } if (L && T) for(Le = L + nlin; L < Le; L++) { vp = (expr_v *)((char *)L->v.rp - ((char *)&ev.v - (char *)&ev)); T = termsum(S, T, new_term(S, new_og(S, 0, (int)(vp - var_e), L->fac))); } return T; }
/* returns a merge of p and q, or NULL if a merge cannot be made */ struct term * term_merge(struct term *p, struct term *q) { struct term *t; int i, m, diff = 0; if (p->bits != q->bits) return NULL; for (i = 0; i < p->bits; i++) { if (p->v[i] == DC && q->v[i] == DC) /* ignore matching DCs */ continue; if (!(p->v[i] == DC) ^ !(q->v[i] == DC)) /* non-match is fail */ return NULL; if (p->v[i] != q->v[i]) diff++; } if (diff != 1) return NULL; /* create the new term */ t = new_term(p->bits); for (i = 0; i < p->bits; i++) if (p->v[i] == q->v[i]) t->v[i] = p->v[i]; else t->v[i] = DC; for (i = 0; i < p->cover_len; i++) t->cover[t->cover_len++] = p->cover[i]; for (i = 0; i < q->cover_len; i++) t->cover[t->cover_len++] = q->cover[i]; return t; }
ring_elem SkewPolynomialRing::mult_by_term(const ring_elem f, const ring_elem c, const int *m) const // Computes c*m*f, BUT NOT doing normal form wrt a quotient ideal.. { Nterm head; Nterm *inresult = &head; exponents EXP1 = ALLOCATE_EXPONENTS(exp_size); exponents EXP2 = ALLOCATE_EXPONENTS(exp_size); M_->to_expvector(m, EXP1); for (Nterm *s = f; s != NULL; s = s->next) { M_->to_expvector(s->monom, EXP2); int sign = skew_.mult_sign(EXP1, EXP2); if (sign == 0) continue; Nterm *t = new_term(); t->next = 0; t->coeff = K_->mult(c, s->coeff); if (sign < 0) K_->negate_to(t->coeff); M_->mult(m, s->monom, t->monom); inresult->next = t; inresult = inresult->next; } inresult->next = 0; return head.next; }
res2term *res2_poly::new_term(ring_elem c, const int *m, res2_pair *x) const // Note: this does NOT copy 'c' { res2term *result = new_term(); result->coeff = c; M->copy(m, result->monom); result->comp = x; return result; }
/* ----------------------------------------------------------------------- * Adds a factor to the end of the term * ----------------------------------------------------------------------- */ void add_to_term(struct term_t **t, int mulop, struct factor_t *f) { if (*t == NULL) { *t = new_term(); (*t)->f = f; } else { struct term_t *temp; temp = *t; GOTO_END_OF_LIST(temp) temp->next = new_term(); temp->next->f = f; temp->mulop = mulop; } }
void Document::compute_freqs() { if (should_compute_freqs()) { for (string word : m_Words) { if (not_present(word)) m_TermFrequencies.insert(new_term(word)); else m_TermFrequencies.find(word)->second++; } } }
static Elem * new_term_string(Grammar *g, char *s, char *e, Rule *r) { Term *t = new_term(); Elem *elem; t->string = reinterpret_cast<char*>(MALLOC(e - s + 1)); memcpy(t->string, s, e - s); t->string[e - s] = 0; t->string_len = e - s; vec_add(&g->terminals, t); elem = new_elem_term(t, r); return elem; }
res2term *res2_poly::copy(const res2term *f) const { res2term head; res2term *result = &head; for (const res2term *tm = f; tm != NULL; tm = tm->next) { result->next = new_term(); result = result->next; result->comp = tm->comp; result->coeff = K->copy(tm->coeff); M->copy(tm->monom, result->monom); } result->next = NULL; return head.next; }
res2term *res2_poly::mult_by_coefficient(const res2term *f, const ring_elem c) const { res2term head; res2term *result = &head; for (const res2term *tm = f; tm != NULL; tm = tm->next) { result->next = new_term(); result = result->next; result->comp = tm->comp; result->coeff = K->mult(c,tm->coeff); M->copy(tm->monom, result->monom); } result->next = NULL; return head.next; }
res2term *res2_poly::strip(const res2term *f) const { res2term head; res2term *result = &head; for (const res2term *tm = f; tm != NULL; tm = tm->next) if (tm->comp->syz_type != SYZ2_NOT_MINIMAL) { result->next = new_term(); result = result->next; result->comp = tm->comp; result->coeff = K->copy(tm->coeff); M->copy(tm->monom, result->monom); } result->next = NULL; return head.next; }
res2term *res2_poly::ring_mult_by_term(const ring_elem f, ring_elem c, const int *m, res2_pair *x) const { res2term head; res2term *result = &head; for (const Nterm *tm = f; tm != NULL; tm = tm->next) { result->next = new_term(); result = result->next; result->comp = x; result->coeff = K->mult(c, tm->coeff); M->mult(tm->monom, m, result->monom); } result->next = NULL; return head.next; }
int minterms(struct truth *truth, struct term_list *minterms) { struct term *p; int i, j, pn = 0; for (i = 0; i < truth->entries; i++) if (truth->tab[i]) { p = new_term(truth->vars); for (j = 0; j < truth->vars; j++) p->v[j] = tt_bit(i, j); p->cover[p->cover_len++] = i; TAILQ_INSERT_TAIL(minterms, p, entry); pn++; } return pn; }
static term * termdup(Static *S, term *T) { term *rv; ograd *og, *oge; dyad *Q, *Q1; if ((og = oge = T->L)) og = ogdup(S, og, &oge); rv = new_term(S, og); rv->Le = oge; if (!(Q = T->Q)) return rv; Q1 = rv->Qe = new_dyad(S, 0, ogdup(S, Q->Lq,0), ogdup(S, Q->Rq,0), 1); while((Q = Q->next)) Q1 = new_dyad(S, Q1, ogdup(S, Q->Lq,0), ogdup(S, Q->Rq,0), 1); rv->Q = Q1; return rv; }
res2term *res2_poly::from_vector(const array<res2_pair *> &base, const vec v) const { res2term head; res2term *result = &head; for (vecterm *w = v; w != NULL; w = w->next) for (Nterm *t = w->coeff; t != 0; t = t->next) { result->next = new_term(); result = result->next; result->comp = base[w->comp]; result->coeff = t->coeff; M->copy(t->monom, result->monom); M->mult(result->monom, result->comp->syz->monom, result->monom); } result->next = NULL; // Now we must sort these sort(head.next); return head.next; }
void db::add(const string & term_c, int doc_id, int freq) { throw_if_reader(); string term = term_c; fancy_term(term); auto descriptor = m_descriptors->find(term); if (descriptor == m_descriptors->end()) { new_term(term); } descriptor = m_descriptors->find(term); uint cur = descriptor->second.cur; uint cur_in_clusters = cur - sizeof(file_header); //uint idx = cur_in_clusters % sizeof(cluster); cluster * added_cluster; cluster * cur_cluster = (cluster*)(((char*)m_cluster_space) + cur_in_clusters / sizeof(cluster) * sizeof(cluster)); if (cur_cluster->cur_pos == CLUSTER_SIZE_RECORDS - 1) { //cluster is complete added_cluster = new_cluster(); if (added_cluster == NULL) { add(term_c, doc_id, freq); return; //New file - new rules. } cur_cluster->next_cluster = ((char*)added_cluster) - m_work_file; //cur_in_clusters = ((char*)&(write_to->records)) - m_work_file; } term_record rec; rec.doc_id = doc_id; rec.freq = freq; record(cur, rec); descriptor->second.cur += sizeof(term_record); if (cur_cluster->next_cluster != (uint)(-1)) { descriptor->second.cur = ((char*)&added_cluster->records) - m_work_file; } cur_cluster->cur_pos++; //cout << "[" << m_worker_id << "] Term " << term << " in doc " << doc_id // << " with freq " << freq << "\n"; }
Structure::Structure() { POMAGMA_INFO("Initializing solver::Structure"); // Initialize vectors for 1-based indexing. term_arity_.resize(1); less_arg_.resize(1); // Initialize atoms. new_term(TermArity::TOP); new_term(TermArity::BOT); new_term(TermArity::I); new_term(TermArity::K); new_term(TermArity::B); new_term(TermArity::C); new_term(TermArity::S); if (POMAGMA_DEBUG_LEVEL) { assert_valid(); } }
void SchurRing::SM() { int lo, hi; if (_SMcurrent == _SMfinalwt) { // partition is to be output Nterm *f = new_term(); f->coeff = K_->from_int(1); // fprintf(stderr, "partition: "); // for (int i=1; i <= nvars_; i++) // fprintf(stderr, " %d", _SMtab.p[i]); // fprintf(stderr, "\n"); from_partition(_SMtab.p, f->monom); f->next = _SMresult; _SMresult = f; return; } _SMcurrent++; bounds(lo, hi); int this_one = LARGE_NUMBER; // larger than any entry of _SMtab int last_one; for (int i=lo; i<=hi; i++) { last_one = this_one; this_one = _SMtab.p[i]; if (last_one > this_one) { _SMtab.p[i]++; _SMtab.xloc[_SMcurrent] = i; _SMtab.yloc[_SMcurrent] = _SMtab.p[i]; SM(); _SMtab.p[i]--; } } _SMcurrent--; }
static term * ewalk(Static *S, expr *e) { term *L, *R, *T; ograd *o, *oR; expr **ep, **epe; int i; ASL_fg *asl; switch(Intcast e->op) { case OPNUM: return new_term(S, new_og(S, 0, -1 , ((expr_n *)e)->v)); case OPPLUS: return termsum(S, ewalk(S, e->L.e), ewalk(S, e->R.e)); case OPMINUS: return termsum(S, ewalk(S, e->L.e), scale(S, ewalk(S, e->R.e), -1.)); case OPUMINUS: return scale(S, ewalk(S, e->L.e), -1.); case OPMULT: if (!(L = ewalk(S, e->L.e)) || !(R = ewalk(S, e->R.e))) break; if (L->Q) { if (R->Q) break; qscale: o = R->L; if (o->next || o->varno >= 0) break; scale(S, L, o->coef); free_og(S, o); free_term(S, R); return L; } if (R->Q) { T = L; L = R; R = T; goto qscale; } o = L->L; oR = R->L; if (o->next || o->varno >= 0) { if (oR->next || oR->varno >= 0) { L->Q = L->Qe = new_dyad(S, 0,o,oR,1); L->L = L->Le = 0; } else { scale(S, L, oR->coef); free_og(S, oR); } free_term(S, R); return L; } scale(S, R, o->coef); free_og(S, o); free_term(S, L); return R; case OPDIV: /* only allow division by a constant */ if (!(R = ewalk(S, e->R.e))) break; o = R->L; if (R->Q || o->next || o->varno >= 0) break; if (!(L = ewalk(S, e->L.e))) break; if (!o->coef) { zerodiv++; L = 0; } else scale(S, L, 1./o->coef); free_og(S, o); free_term(S, R); return L; case OPSUMLIST: ep = e->L.ep; epe = e->R.ep; L = ewalk(S, *ep); while(L && ++ep < epe) L = termsum(S, L, ewalk(S, *ep)); return L; case OP2POW: L = ewalk(S, e->L.e); if (!L || L->Q) break; o = L->L; if (!o->next && o->varno < 0) { o->coef *= o->coef; return L; } L->Q = L->Qe = new_dyad(S, 0,o,o,1); L->L = L->Le = 0; return L; case OPVARVAL: asl = S->asl; if ((i = (expr_v *)e - var_e) < n_var) return new_term(S, new_og(S, 0, i, 1.)); i -= S->nvinc; if (!(L = cterms[i -= n_var]) && !(L = cterms[i] = comterm(S, i))) return 0; return termdup(S, L); } return 0; /* nonlinear */ }
int smain() { int n; string input; cin >> n >> input; if (check(input, n)) { term termb, nterm; terms.resize(n+1); terms_next.resize(sz(terms)); for (int l = 1; l <= sz(input); pows[l] = true, l *= 2); for (int i = 0; i < sz(input); i++) { if (input[i] == '1') { getterm(termb, i, n); terms[termb.second].insert(mp(termb.first, i)); } } bool f = true; while (f) { f = false; for (int i = 0; i < sz(terms); i++) { for (auto j = terms[i].begin(); j != terms[i].end(); j++) { bool prev = true, next = true; if (i < sz(terms)-1) { for (auto k = terms[i + 1].begin(); k != terms[i + 1].end(); k++) if (term_comp(*j, *k)) { f = true; next = false; int q = new_term(*j, *k, nterm); terms_next[q].insert(nterm); } } if (i > 0) { for (auto k = terms[i - 1].begin(); k != terms[i - 1].end(); k++) if (term_comp(*k, *j)) { f = true; prev = false; int q = new_term(*k, *j, nterm); terms_next[q].insert(nterm); } } if (prev && next) terms_next[i].insert(*j); } } terms.clear(); terms = terms_next; terms_next.clear(); terms_next.resize(sz(terms)); } set<string> _terms, _next, _nexta; for (int i = 0; i < sz(input); i++) { if (input[i] == '1') { getterm(termb, i, n); _terms.insert(termb.first); } } for (int i = 0; i < sz(terms); i++) for (auto j = terms[i].begin(); j != terms[i].end(); j++) _next.insert(j->first); _nexta = _next; string minterm, result; for (auto j = _terms.begin(); j != _terms.end();) { int count = 0; minterm.clear(); for (auto i = _next.begin(); i != _next.end(); i++) if (term_compa(*j, *i)) { count++; if (count == 1) minterm = *i; } if (count == 1) { _nexta.erase(minterm); for (auto z = _terms.begin(); z != _terms.end();) if (term_compa(*z, minterm)) { _terms.erase(z); z = _terms.begin(); } else z++; j = _terms.begin(); } else j++; } for (auto j = _nexta.begin(); j != _nexta.end(); j++) _next.erase(*j); for (auto j = _next.begin(); j != _next.end(); j++) { if (j != _next.begin()) result += " v "; for (int i = 0; i < sz((*j)); i++) { if ((*j)[i] == '0') result += i + 97; if ((*j)[i] == '1') result += i + 65; } } int min = (int)1e9; string minresult; if (!_terms.empty()) { findminresult(result, _terms, _nexta, _terms.begin(), &min, minresult); cout << minresult << endl; } else cout << result << endl; system("pause"); } else { system("pause"); } return 0; }