Example #1
0
    def update_index(self, rebuild=False):
        """Update index.

        Arguments:
        rebuild -- Rebuild the index from scratch

        Otherwise, it only updates count and last_prime
        """
        if rebuild:
            os.remove(self.index)
            self.connect()

            files = os.listdir(self.dir_)
            for f in files:
                if f == "index.db":
                    continue
                print "Adding", f
                self.add(polynomial.name_to_polynomial(f))

            self.update_index()
        else:
            for f in self.list():
                count = self.count(f, True)
                last_prime = self.last_prime(f, True)
                if count != self.count(f) or \
                last_prime != self.last_prime(f):
                    print "Updating", f
                    self.conn.execute("UPDATE polynomials \
                                      SET roots=?, last_prime=? \
                                      WHERE coefficients=?",
                                      (count, last_prime,
                                       polynomial.name_polynomial(f)))
            self.conn.commit()

        return None
Example #2
0
    def add(self, f):
        """Add a polynomial to the database."""
        coeffs = polynomial.name_polynomial(f)
        degree = int(f.degree())
        group = int(f.galois_group()._n)

        c = self.conn.execute("SELECT count(*) FROM polynomials \
                              WHERE coefficients=?", (coeffs, ))
        if c.fetchone()[0] > 0:
            print f, "is already in the database."
            return None

        fconn = self.connect(f)
        fconn.execute("""CREATE TABLE IF NOT EXISTS roots
                      (root integer,
                      prime integer,
                      normalized real,
                      th integer,
                      total integer)""")
        fconn.commit()
        self.conn.execute("INSERT INTO \
                          polynomials(coefficients, degree, \
                          galois_group, roots, last_prime) \
                          VALUES (?,?,?,?,?)",
                          (coeffs, degree, group, 0, 0))
        self.conn.commit()

        return None
Example #3
0
    def save_roots(self, f, p2):
        """Save roots between last prime upto p2."""
        p = next_prime(self.last_prime(f))

        if p > p2:
            print "Already calculated upto", p
            return None

        roots = polynomial.solve_roots_range(f, p, p2, extra=True)
        fconn = self.connect(f)
        for r in roots:
            fconn.execute("INSERT INTO roots VALUES(?,?,?,?,?)", r)
        fconn.commit()

        count = len(roots) + self.count(f)
        last_prime = r[1]

        self.conn.execute("UPDATE polynomials \
                          SET roots=?, last_prime=? \
                          WHERE coefficients=?",
                          (count, last_prime,
                           polynomial.name_polynomial(f)))
        self.conn.commit()

        return None
Example #4
0
    def last_prime(self, f, force=False):
        """Return the last prime in DB."""
        if force:
            fconn = self.connect(f)
            c = fconn.execute("SELECT MAX(PRIME) FROM roots")
        else:
            c = self.conn.execute("SELECT last_prime FROM polynomials \
                                  WHERE coefficients=?",
                                  (polynomial.name_polynomial(f), ))

        return c.fetchone()[0]
Example #5
0
    def count(self, f, force=False, type_=None):
        """Return count of roots for f."""
        if force:
            if type_:
                where = " WHERE total=" + str(type_)
            else:
                where = ""
            fconn = self.connect(f)
            c = fconn.execute("SELECT COUNT(*) FROM roots" + where)

        else:
            c = self.conn.execute("SELECT roots FROM polynomials \
                                  WHERE coefficients=?",
                                  (polynomial.name_polynomial(f),))

        return c.fetchone()[0]
Example #6
0
 def _filename(self, f):
     return polynomial.name_polynomial(f)