Exemplo n.º 1
0
	def test_polySubtract2(self):
		poly1 = poly.Poly([])
		poly2 = poly.Poly([1,1])
		testDiff = poly1 - poly2
		
		correctDiff = poly.Poly([-1,-1])
		self.assertEqual(correctDiff,testDiff)
Exemplo n.º 2
0
	def test_normPolyB2(self):
		NF = poly.NumberField(poly.Poly([8,29,15,1]))
		a = -8
		b = 3
		NF = poly.NumberField(poly.Poly([8,29,15,1]))
		polyNormB = NF.getPolyNormB(b)
		self.assertEqual(polyNormB.evaluate(a),-5696)
Exemplo n.º 3
0
    def __init__(self):
        self.res = (960, 540)
        self.screen = pygame.display.set_mode(self.res)
        self.clock = pygame.time.Clock()

        self.cam = camera.Camera(self.screen, self.res)

        self.font_log = pygame.font.SysFont(None, 24)
        #textrect = text.get_rect()
        #textrect.centerx = screen.get_rect().centerx
        #textrect.centery = screen.get_rect().centery

        self.mouseLock = False

        self.polyList = [
            poly.Poly([
                vector.Vector3(10, 1, 0),
                vector.Vector3(10, 0, 1),
                vector.Vector3(10, 0, -1)
            ])
        ]

        for i in range(100):
            self.polyList.append(
                poly.Poly([
                    vector.Vector3(10 + 5 * math.sin(i / 20), 1 + i / 10, 0),
                    vector.Vector3(10 + 5 * math.sin(i / 20), 0, 1 + i / 10),
                    vector.Vector3(10 + 5 * math.sin(i / 20), 0, -1 - i / 10)
                ]))
Exemplo n.º 4
0
	def test_equal(self):
		poly1 = poly.Poly([1,2,3,4])
		poly2 = poly.Poly([1,2,3,4])
		poly3 = poly.Poly([1,4,2,3])
		self.assertEqual(poly1==poly2,True)
		self.assertEqual(poly1,poly2)
		self.assertNotEqual(poly1,poly3)
Exemplo n.º 5
0
	def test_reduceToNFp(self):
		p = 9923
		poly1 = poly.Poly([7301,1477,7726])
		poly2 = poly.Poly([8,29,15,1])
		NFp = poly.NumberFieldModP(poly1,p)
		testReduction = NFp(poly2)
		correctReduction = NFp(poly.Poly([9858, 7744]))
		self.assertEqual(testReduction,correctReduction)
Exemplo n.º 6
0
	def test_degree(self):
		polynomial = poly.Poly([1,2,3])
		self.assertEqual(polynomial.degree(), 2)
		polynomial = poly.Poly([0,2,3])
		self.assertEqual(polynomial.degree(), 2)
		polynomial = poly.Poly([1,2,0])
		self.assertEqual(polynomial.degree(), 1)
		polynomial = poly.Poly([1,2,3,4,5])
		self.assertEqual(polynomial.degree(), 4)
Exemplo n.º 7
0
	def test_numberfield_sqrt_equality(self):
		nfspoly = poly.Poly([8,29,15,1])
		prime = 9929
		NFp = poly.NumberFieldModP(nfspoly,prime)
		posSqrt = NFp(poly.Poly([3077, 1160, 3402]))
		negSqrt = NFp(poly.Poly([6852, 8769, 6527]))
		
		self.assertEqual(posSqrt,-negSqrt)
		self.assertEqual(-posSqrt,negSqrt)
Exemplo n.º 8
0
	def test_numberfield_square2(self):
		# Brigg's example
		nfspoly = poly.Poly([8,29,15,1])
		NF = poly.NumberField(nfspoly)
		
		nfspolyd = NF(nfspoly.derivative())
		sqrt = NF(poly.Poly([3889976768, 3686043120, 599923511]))
		correctSquare = NF(poly.Poly([22939402657683071224L, 54100105785512562427L, 22455983949710645412L]))
		
		square = sqrt * sqrt
		self.assertEqual(square,correctSquare)
Exemplo n.º 9
0
	def test_numberfield_square1(self):
		# Spaans's example
		nfspoly = poly.Poly([161, 134, 2, 1])
		NF = poly.NumberField(nfspoly)
		nfspolyd = NF(nfspoly.derivative())
		
		sqrt = NF(poly.Poly([-41757429265579073242,-34105727598423382475,1681812579256330563]))
		correctSquare = NF(poly.Poly([21124198049840950371210079793023892077432,18523314201045731615331644622444823801483,884477920457388669411401815623954662863]))
		
		square = sqrt * sqrt
		self.assertEqual(square,correctSquare)
Exemplo n.º 10
0
	def test_numberfield_power_modp2(self):
		#Briggs
		polynomial = poly.Poly([8,29,15,1])
		p = 9923
		NFp = poly.NumberFieldModP(polynomial,p)
		g = NFp(poly.Poly([0,1])) ** p
		g = g - NFp(poly.Poly([0,1]))
		g = g.getPoly() 
		
		correctG = poly.Poly([7301,1477,7726])
		self.assertEqual(g,correctG)
Exemplo n.º 11
0
	def test_polyRootModPFastDeg5_2(self):
		p = 157
		polynomial = poly.Poly([11,1])*poly.Poly([-23,1])*poly.Poly([-1,1])*poly.Poly([-1,1])*poly.Poly([-1,1])
		correctRoots = poly.getRootsModPSlow(polynomial,p)
		testRoots = poly.getRootsModPFast(polynomial,p)

		correctRoots.sort()
		testRoots.sort()
		
		self.assertEqual(len(correctRoots),len(testRoots))
		
		for i in range(len(correctRoots)):
			self.assertEqual(correctRoots[i], testRoots[i])
Exemplo n.º 12
0
	def test_numberfield_mult2(self):
		# Spaan's example
		nfspoly = poly.Poly([161, 134, 2, 1])
		NF = poly.NumberField(nfspoly)
		nfspolyd = NF(nfspoly.derivative())
		
		tomult = [[-92,-1],[-57,-1],[-23,-1],[-8,-1],[-7,-1],[2,-1],[10,-1],[17,-1],[29,-1],[35,-1],[84,-1],[115,-1],[139,-1],[-5,-2],[19,-2],[69,-2],[93,-2],[119,-2],[-542,-3],[-28,-3],[-23,-3],[-8,-3]]
		tomult = [NF(poly.Poly(x)) for x in tomult]
		
		correctProduct = NF(poly.Poly([21124198049840950371210079793023892077432,18523314201045731615331644622444823801483,884477920457388669411401815623954662863]))
		
		prod = multAllElements(NF(poly.Poly([1])),tomult)
		prod = prod * nfspolyd * nfspolyd
		self.assertEqual(prod,correctProduct)
Exemplo n.º 13
0
	def test_numberfield_mult1(self):
		# Briggs' example
		nfspoly = poly.Poly([8,29,15,1])
		NF = poly.NumberField(nfspoly)
		
		nfspolyd = NF(nfspoly.derivative())
		correctProduct = NF(poly.Poly([22939402657683071224L, 54100105785512562427L, 22455983949710645412L]))
		
		tomult = [[-1,1],[3,1],[13,1],[104,1],[3,2],[25,2],[-8,3],[48,5],[54,5],[-43,6],[-8,7],[11,7],[856,11]]
		tomult = [NF(poly.Poly(x)) for x in tomult]
		
		prod = multAllElements(NF(poly.Poly([1])),tomult)
		prod = prod * nfspolyd * nfspolyd
		self.assertEqual(prod,correctProduct)
Exemplo n.º 14
0
	def test_polyRootModPFastRandomZeroRoot(self):
		
		p = 503
		polynomial = poly.Poly([0,1])*poly.Poly([-11,1])*poly.Poly([-51,1])*poly.Poly([-231,1])
		correctRoots = poly.getRootsModPSlow(polynomial,p)
		testRoots = poly.getRootsModPFast(polynomial,p)

		correctRoots.sort()
		testRoots.sort()
		
		self.assertEqual(len(correctRoots),len(testRoots))
		
		for i in range(len(correctRoots)):
			self.assertEqual(correctRoots[i], testRoots[i])
Exemplo n.º 15
0
	def test_numberfield_modp3(self):
		# again, for a different prime
		nfspoly = poly.Poly([161, 134, 2, 1])
		NFp = poly.NumberFieldModP(nfspoly,2305843009213693973)
		nfspolyd = NFp(nfspoly.derivative())
		
		tomult = [[-92,-1],[-57,-1],[-23,-1],[-8,-1],[-7,-1],[2,-1],[10,-1],[17,-1],[29,-1],[35,-1],[84,-1],[115,-1],[139,-1],[-5,-2],[19,-2],[69,-2],[93,-2],[119,-2],[-542,-3],[-28,-3],[-23,-3],[-8,-3]]
		tomult = [NFp(poly.Poly(x)) for x in tomult]
		
		sqrt = NFp(poly.Poly([2053587909481112245, 481917539782027120, 1681812579256330563]))
		correctProduct = sqrt * sqrt
		
		prod = multAllElements(NFp(poly.Poly([1])),tomult)
		prod = prod * nfspolyd * nfspolyd
		self.assertEqual(prod,correctProduct)
Exemplo n.º 16
0
	def test_numberfield_modp1(self):
		# Briggs' example
		nfspoly = poly.Poly([8,29,15,1])
		NFp = poly.NumberFieldModP(nfspoly,9929)
		
		nfspolyd = NFp(nfspoly.derivative())
		correctProduct = NFp(poly.Poly([6659,3891,2027]))
		
		tomult = [[-1,1],[3,1],[13,1],[104,1],[3,2],[25,2],[-8,3],[48,5],[54,5],[-43,6],[-8,7],[11,7],[856,11]]
		tomult = [NFp(poly.Poly(x)) for x in tomult]
		prod = multAllElements(NFp(poly.Poly([1])),tomult)
		
		
		prod = prod * nfspolyd * nfspolyd
		self.assertEqual(prod,correctProduct)
Exemplo n.º 17
0
	def test_numberfield_sqrt_modp2(self):
		# Briggs
		nfspoly = poly.Poly([161, 134, 2, 1])
		primes = [2305843009213693951,2305843009213693967,2305843009213693973,2305843009213694381]
		tomult = [[-92,-1],[-57,-1],[-23,-1],[-8,-1],[-7,-1],[2,-1],[10,-1],[17,-1],[29,-1],[35,-1],[84,-1],[115,-1],[139,-1],[-5,-2],[19,-2],[69,-2],[93,-2],[119,-2],[-542,-3],[-28,-3],[-23,-3],[-8,-3]]
		
		for prime in primes:
			NFp = poly.NumberFieldModP(nfspoly,prime)
			tomultNFp = [NFp(poly.Poly(x)) for x in tomult]
			nfspolyd = NFp(nfspoly.derivative())
			
			prod = multAllElements(NFp(poly.Poly([1])),tomultNFp)
			prod = prod * nfspolyd * nfspolyd
			sqrt = prod.sqrt()
			self.assertEqual(sqrt*sqrt,prod)
Exemplo n.º 18
0
	def test_numberfield_sqrt_modp1(self):
		# Briggs
		nfspoly = poly.Poly([8,29,15,1])
		primes = [9851,9907,9929]
		tomult = [[-1,1],[3,1],[13,1],[104,1],[3,2],[25,2],[-8,3],[48,5],[54,5],[-43,6],[-8,7],[11,7],[856,11]]
		
		for prime in primes:
			NFp = poly.NumberFieldModP(nfspoly,prime)
			tomultNFp = [NFp(poly.Poly(x)) for x in tomult]
			nfspolyd = NFp(nfspoly.derivative())
			
			prod = multAllElements(NFp(poly.Poly([1])),tomultNFp)
			prod = prod * nfspolyd * nfspolyd
			sqrt = prod.sqrt()
			
			self.assertEqual(sqrt*sqrt,prod)
Exemplo n.º 19
0
	def test_numberfield_modp2(self):
		# Spaans' examples
		nfspoly = poly.Poly([161, 134, 2, 1])
		NFp = poly.NumberFieldModP(nfspoly,2305843009213693951)
		nfspolyd = NFp(nfspoly.derivative())
		
		tomult = [[-92,-1],[-57,-1],[-23,-1],[-8,-1],[-7,-1],[2,-1],[10,-1],[17,-1],[29,-1],[35,-1],[84,-1],[115,-1],[139,-1],[-5,-2],[19,-2],[69,-2],[93,-2],[119,-2],[-542,-3],[-28,-3],[-23,-3],[-8,-3]]
		tomult = [NFp(poly.Poly(x)) for x in tomult]
		
		# Spaans provides the sqrt, so square that.
		sqrt = NFp(poly.Poly([2053587909481111827,481917539782026790,1681812579256330563]))
		correctProduct = sqrt * sqrt
		
		prod = multAllElements(NFp(poly.Poly([1])),tomult)
		prod = prod * nfspolyd * nfspolyd
		self.assertEqual(prod,correctProduct)
Exemplo n.º 20
0
def main():
    (n, nfsPoly, m, B, M, K) = files.loadParamsFile()

    afBase = files.loadFileArray("afbase.txt")
    rfBase = files.loadFileArray("rfbase.txt")
    smoothsFile = open("smooths.txt", "w")
    smoothsCount = 0
    q = B
    K += K / 10

    specialqFile = open("specialq.txt", "w")
    while (smoothsCount < K):
        while (True):
            q = primemath.nextPrime(q)
            roots = poly.getRootsModPFast(poly.Poly(nfsPoly), q)
            if (len(roots) > 0):
                break

        roots.sort()
        for s in roots:
            specialqFile.write(str([s, q]) + "\n")
            print "special_q: %s" % [q, s]

            I = 1024
            J = 512
            smooths = sieve_special_q(q, s, I, J, rfBase, afBase,
                                      (n, nfsPoly, m, B, M, K))
            smoothsCount += len(smooths)
            print "total smooths: %s/%s" % (smoothsCount, K)
            smoothsFile = open("smooths.txt", "a")
            for smooth in smooths:
                smoothsFile.write(str(smooth) + "\n")

            smoothsFile.close()
Exemplo n.º 21
0
def getAlgPrimeExponents(smooths, NF, base):
    primeExponents = [0] * len(base)
    for i in range(len(smooths)):
        smoothNorm = NF(poly.Poly(smooths[i])).norm()
        smoothPrimeExponents = getPrimeExponents(smoothNorm, base)
        sumExponents(primeExponents, smoothPrimeExponents)

    return primeExponents
Exemplo n.º 22
0
def generateNFSPoly(n, d):
    m = int(math.pow(n, 1.0 / d))
    nfsPoly = poly.Poly(expansionBaseM(n, m))
    shrinkNfsPoly(nfsPoly, m)
    while (reducible(nfsPoly)):
        m -= 1
        nfsPoly = Poly(expansionBaseM(n, m))
        shrinkNfsPoly(nfsPoly, m)

    return (m, nfsPoly)
Exemplo n.º 23
0
def main():
    nfspoly = poly.Poly([8, 29, 15, 1])
    NF = poly.NumberField(nfspoly)
    while (True):
        q = primemath.generateLargePrime(10)
        s = poly.getRootsModPFast(nfspoly, q)
        if (len(s) > 0):
            break
    s = s[0]

    print "special_q: f(%s) = 0 mod %s" % (s, q)

    while (True):
        p = primemath.generateLargePrime(7)
        r = poly.getRootsModPFast(nfspoly, p)
        if (len(r) > 0):
            break
    r = r[0]

    print "p: f(%s) = 0 mod %s" % (r, p)

    basis = find_sublattice_basis(q, s, p, r)
    print "basis: %s" % basis
    basis_r = reduce_basis(basis[0], basis[1])
    a0 = basis_r[0][0]
    b0 = basis_r[0][1]
    a1 = basis_r[1][0]
    b1 = basis_r[1][1]
    print "reduced basis: %s" % [[a0, b0], [a1, b1]]

    print "generating lattice points..."
    for i in range(-20, 20):
        for j in range(-20, 20):
            a = a0 * i + a1 * j
            b = b0 * i + b1 * j
            if (a == 0 or b == 0):
                continue
            NFelement = NF(poly.Poly([a, b]))
            if (NFelement.norm() % q == 0 and NFelement.norm() % p == 0):
                print "%s is q-divisible and p-divisible" % [a, b]
            else:
                raise AssertionError("%s is not q-divisible or p-divisible" %
                                     [a, b])
Exemplo n.º 24
0
def calcAlgMatrixRowsSum(afBase, smooths, m, NF):
    matrixRowSum = [0] * (len(afBase))

    for smoothPair in smooths:
        smoothPoly = poly.Poly(smoothPair)
        matrixRow = matrixmath.getMatrixRowAlg(smoothPoly, NF, afBase)
        for i in range(len(matrixRow)):
            matrixRowSum[i] += matrixRow[i]

    return matrixRowSum
Exemplo n.º 25
0
	def test_numberfield_positivesquareroot_modp1(self):
		# Briggs' CRT example
		n = 45113
		m = 31
		nfspoly = poly.Poly([8,29,15,1])
		tomult = [[-1,1],[3,1],[13,1],[104,1],[3,2],[25,2],[-8,3],[48,5],[54,5],[-43,6],[-8,7],[11,7],[856,11]]
		primeSizeEst = etcmath.calcRequiredPrimeLength(n, m, nfspoly, tomult)
		#primes = randomPrimes(primeSizeEst,32)
		prime = 9929
		
		NFp = poly.NumberFieldModP(nfspoly,prime)
		nfspolyd = NFp(nfspoly.derivative())
		tomultNFp = [NFp(poly.Poly(x)) for x in tomult]
		prod = multAllElements(NFp(poly.Poly([1])),tomultNFp)
		prod = prod * nfspolyd * nfspolyd
			
		sqrt = prod.sqrt()
		posSqrt = NFp(poly.Poly([3077, 1160, 3402]))
		negSqrt = NFp(poly.Poly([6852, 8769, 6527]))
		
		self.assertTrue(sqrt == posSqrt or sqrt == negSqrt)
Exemplo n.º 26
0
	def test_polyRootModPSlow(self):
		nfspoly = poly.Poly([8,29,15,1])
		correctRoots = [2,44,6]
		testRoots = poly.getRootsModPSlow(nfspoly,67)
		
		self.assertEqual(len(correctRoots), len(testRoots))
		
		correctRoots.sort()
		testRoots.sort()
		
		for i in range(len(correctRoots)):
			self.assertEqual(correctRoots[i], testRoots[i])
Exemplo n.º 27
0
	def test_polyRootModPFast(self):
		# Briggs
		polynomial = poly.Poly([8,29,15,1])
		testRoots = poly.getRootsModPFast(polynomial,67)
		correctRoots = [2,44,6]
		correctRoots.sort()
		testRoots.sort()
		
		self.assertEqual(len(correctRoots),len(testRoots))
		
		for i in range(len(correctRoots)):
			self.assertEqual(correctRoots[i], testRoots[i])
Exemplo n.º 28
0
def orth_pcd(order, dist, eps=1.e-16, normed=False, **kws):
    """
Create orthogonal polynomial expansion from pivoted Cholesky
decompostion.

Parameters
----------
order : int
    Order of polynomial expansion
dist : Dist
    Distribution space where polynomials are orthogonal
normed : bool
    If True orthonormal polynomials will be used instead of monic.
**kws : optional
    Extra keywords passed to dist.mom

Examples
--------
#  >>> Z = cp.Normal()
#  >>> print cp.orth_pcd(2, Z)
#  [1.0, q0^2-1.0, q0]
    """
    raise DeprecationWarning("Obsolete. Use orth_chol instead.")

    dim = len(dist)
    basis = po.basis(1, order, dim)
    C = Cov(basis, dist)
    N = len(basis)

    L, P = pcd(C, approx=1, pivot=1, tol=eps)
    Li = np.dot(P, np.linalg.inv(L.T))

    if normed:
        for i in xrange(N):
            Li[:, i] /= np.sum(Li[:, i] * P[:, i])
    E_ = -po.sum(E(basis, dist, **kws) * Li.T, -1)

    coefs = np.zeros((N + 1, N + 1))
    coefs[1:, 1:] = Li
    coefs[0, 0] = 1
    coefs[0, 1:] = E_

    out = {}
    out[(0, ) * dim] = coefs[0]
    basis = list(basis)
    for i in xrange(N):
        I = basis[i].keys[0]
        out[I] = coefs[i + 1]

    P = po.Poly(out, dim, coefs.shape[1:], float)
    return P
Exemplo n.º 29
0
def orth_chol(order, dist, normed=True, sort="GR", **kws):
    """
Create orthogonal polynomial expansion from Cholesky decomposition

Parameters
----------
order : int
    Order of polynomial expansion
dist : Dist
    Distribution space where polynomials are orthogonal
normed : bool
    If True orthonormal polynomials will be used instead of monic.
sort : str
    Ordering argument passed to poly.basis.
    If custom basis is used, argument is ignored.
kws : optional
    Keyword argument passed to dist.mom.

Examples
--------
>>> Z = cp.Normal()
>>> print cp.orth_chol(3, Z)
[1.0, q0, 0.707106781187q0^2-0.707106781187, 0.408248290464q0^3-1.22474487139q0]
    """

    dim = len(dist)
    basis = po.basis(1, order, dim, sort)
    C = Cov(basis, dist)
    N = len(basis)

    L, e = chol_gko(C)
    Li = np.linalg.inv(L.T).T
    if not normed:
        Li /= np.repeat(np.diag(Li), len(Li)).reshape(Li.shape)
    E_ = -np.sum(Li * E(basis, dist, **kws), -1)
    coefs = np.empty((N + 1, N + 1))
    coefs[1:, 1:] = Li
    coefs[0, 0] = 1
    coefs[0, 1:] = 0
    coefs[1:, 0] = E_
    coefs = coefs.T

    out = {}
    out[(0, ) * dim] = coefs[0]
    for i in xrange(N):
        I = basis[i].keys[0]
        out[I] = coefs[i + 1]

    P = po.Poly(out, dim, coefs.shape[1:], float)

    return P
Exemplo n.º 30
0
def main():
    nfspoly = poly.Poly([8, 29, 15, 1])
    NF = poly.NumberField(nfspoly)
    while (True):
        q = primemath.generateLargePrime(20)
        r = poly.getRootsModPFast(nfspoly, q)
        if (len(r) > 0):
            break
    r = r[0]

    print "special_q: f(%s) = 0 mod %s" % (r, q)

    basis = []
    b = 1
    for i in range(2):
        a = -b * r + i * q
        basis.append([a, b])

    print "basis: %s" % basis
    basis_r = reduce_basis(basis[0], basis[1])
    a0 = basis_r[0][0]
    b0 = basis_r[0][1]
    a1 = basis_r[1][0]
    b1 = basis_r[1][1]
    print "reduced basis: %s" % [[a0, b0], [a1, b1]]

    print "generating lattice points..."
    for i in range(-2, 2):
        for j in range(-2, 2):
            a = a0 * i + a1 * j
            b = b0 * i + b1 * j
            if (a == 0 or b == 0):
                continue
            NFelement = NF(poly.Poly([a, b]))
            if (NFelement.norm() % q == 0):
                print "%s is q-divisible" % [a, b]
            else:
                raise AssertionError("%s is not q-divisible" % [a, b])