def test_repr(): x = Point(1, 2, 3) assert str(x) == "(1.00, 2.00, 3.00)" y = Point(0, 4 + 3j, 7) assert str(y) == "(0.00, 4.00 + 3.00j, 7.00)"
def test_dimension(): x = Point(1, 2, 3) assert x.dimension == 3 y = Point(1, 2, z=3, x=4, n=5) assert y.dimension == 5
def test_point_initialization(): x = Point(1, 2, 3) assert (str(type(x))) == "<class 'geometry.base.3D Point'>" y = Point(4, 1, 2, 3) assert (str(type(y))) == "<class 'geometry.base.4D Point'>"
def test_point_key_lookup_set(): x = Point(x=1, y=2, z=3) x.x = 4 x.y = 5 x.z = 6 assert x.x == 4 assert x.y == 5 assert x.z == 6
def generatePoints(dim): """ generate base points for dodecaeder """ pm = (-1, 1) PHI=(1+math.sqrt(5))/2 phi = [0, 1/PHI, PHI] for i in range(8): yield Point(pm[(i>>j)&1] for j in range(3)) for j in range(3): for i in range(4): yield Point(pm[(i>>((k+j-1)%3))&1]*phi[(k+j)%3] for k in range(3))
def test_signature(): x = Point(1, 2, 3) y = Point(x=1, y=2, z=3) assert x.signature != y.signature z = Point(4, 5, 6) w = Point(x=4, y=5, z=6) assert x.signature == z.signature assert y.signature == w.signature
def generatePoints(dim): """ Generate base points for n-octaeder The points are the set of +1, -1 points on each axis. Points are yielded such that p[2*i] and p[2*i+1] are on the same axis. """ for i in range(dim): yield Point(1 if i==j else 0 for j in range(dim)) yield Point(-1 if i==j else 0 for j in range(dim))
def generatePoints(dim): """ generate base points for n-tetraeder """ CENTER = (1.0+dim+math.sqrt(1.0+dim))/((1.0+dim)*dim) p0= Point(CENTER for _ in range(dim)) # first points along axis for i in range(dim): yield Point(1 if i==j else 0 for j in range(dim))-p0 # and a single point at equal distance from others yield Point((1+math.sqrt(dim+1))/dim for j in range(dim))-p0
def generatePoints1(dim): assert(dim==4) def bit(x,i): return -1.0 if x&(1<<i) else 1.0 # (0,0,0,1) -> 4 * 2 for i in range(4): for b in range(2): yield Point(bit(b,0) if i==j else 0 for j in range(4)) # (0.5,0.5,0.5,0.5) -> 1 * 16 for i in range(16): yield Point(0.5*bit(i,j) for j in range(4))
def generatePoints(dim): """ generate base points for icosaeder """ pm = (-0.5, 0.5) PHI=(1+math.sqrt(5))/2 phi = [0, 1.0, PHI] for j in range(3): for i in range(4): yield Point(pm[(i>>((k+j-1)%3))&1]*phi[(k+j)%3] for k in range(3))
def doshape(self, cls, dim, npoints, nlines, edgelen): """ count nr points, lines, edge size for shape """ t = cls(Point(0 for x in range(dim))) self.assertEqual(len(t.points), npoints) count = 0 for a, b in t.generateLines(): count += 1 self.assertAlmostEqual(t.points[a].distance(t.points[b]), edgelen) self.assertEqual(count, nlines)
def test_point_mixed_addressing(): x = Point(1, y=2, z=3) assert x[2] == x.z == 3 x[1] = 4 assert x.y == 4 assert x[0] == 1
def test_vector_initialization(): x = Point(1, 2, 3) x_v = Vector(x) assert (str(type(x_v))) == "<class 'geometry.base.3D Vector'>" y = Vector(4, 1, 2, 3) assert (str(type(y))) == "<class 'geometry.base.4D Vector'>"
def test_point_indexing_set(): x = Point(1, 2, 3) x[0] = 4 x[1] = 5 x[2] = 6 assert x[0] == 4 assert x[1] == 5 assert x[2] == 6
def generatePoints(dim): """ Generate base points for n-cube. Each point is assigned a binary number, the bits form the coordinates. """ def bit(x,i): return -1.0 if x&(1<<i) else 1.0 for i in range(1<<dim): yield Point(bit(i,j)*0.5 for j in range(dim))
def dumppolar(dim, cls): """ dump named points and polar representations of shape """ center = Point(0 for _ in range(dim)) count = 0 for pt in cls.generatePoints(dim): center += pt count += 1 center /= count print("center = %.16f" % center.coord[0], namedpt(center), cls) for pt in cls.generatePoints(dim): pt -= center print("%-40s -- %-40s" % (namedpt(pt), namedpt(pt.toNSpherical())))
def generatePoints(dim): assert(dim==4) def bit(x,i): return -1.0 if x&(1<<i) else 1.0 # (1,1,0,0) -> 4 * 6 for a in range(1,4): for b in range(a): for i in range(4): p= [0 for _ in range(4)] p[a]= bit(i,0)/math.sqrt(2.0) p[b]= bit(i,1)/math.sqrt(2.0) yield Point(p)
def test_equality(): x = Point(1, 2, 3) y = Point(1, 2, 3) q = Point(4, 5, 6) z = Point(x=1, y=2, z=3) w = Point(x=1, y=2, z=3) assert x == y assert z == w assert x != q caught_exception = None try: assert x == z except TypeError as e: caught_exception = e finally: assert caught_exception is not None
def generatePoints(dim): assert(dim==4) SQ5 = math.sqrt(5.0) PHI = (1.0+SQ5)/2.0 def bit(x,i): return -1.0 if x&(1<<i) else 1.0 # (0.5,0.5,0.5,0.5) -> 1 * 16 for i in range(16): yield Point(0.5*bit(i,j) for j in range(4)) # (0,0,0,1) -> 4 * 2 for i in range(4): for b in range(2): yield Point(bit(b,0) if i==j else 0 for j in range(4)) # even perms of: # (PHI, 1, 1/PHI, 0)/2 -> 12 * 8 for a in range(8): p0= (0, bit(a,2)*PHI/2.0, bit(a,1)/2.0, bit(a,0)/2.0/PHI) for perm in ( (0,1,2,3), (0,2,3,1), (0,3,1,2), (1,0,3,2), (1,2,0,3), (1,3,2,0), (2,0,1,3), (2,1,3,0), (2,3,0,1), (3,0,2,1), (3,1,0,2), (3,2,1,0)): yield Point(p0[perm[0]], p0[perm[1]], p0[perm[2]], p0[perm[3]])
def generatePoints(dim): assert(dim==4) SQ5 = math.sqrt(5.0) PHI = (1.0+SQ5)/2.0 def bit(x,i): return -1.0 if x&(1<<i) else 1.0 # perms of: # 0022 0202 0220 2020 2200 2002 # (0,0,2, 2) -> 4*6 for a in range(1,4): for b in range(a): for i in range(4): p= [0 for _ in range(4)] p[a]= 2.0*bit(i,0) p[b]= 2.0*bit(i,1) yield Point(p) # 0001 0010 0100 1000 # (1,1,1,SQ5) -> 16*4 # (PHI**-2, PHI,PHI,PHI) -> 16*4 # (PHI**-1, PHI**-1,PHI**-1, PHI**2) -> 16*4 for a in range(16): for i in range(4): yield Point(bit(a,j)*(SQ5 if i==j else 1.0) for j in range(4)) yield Point(bit(a,j)*(PHI**-2 if i==j else PHI) for j in range(4)) yield Point(bit(a,j)*(PHI**2 if i==j else PHI**-1) for j in range(4)) # even perms of: # 0123 0231 0312 1032 1203 1320 2013 2130 2301 3021 3102 3210 # (0, PHI**-2, 1, PHI**2) -> 12*8 # (0, PHI**-1, PHI, SQ5) -> 12*8 # (PHI**-1, 1, PHI, 2) -> 12*16 for a in range(16): p0= (0, bit(a,2)*PHI**-2, bit(a,1), bit(a,0)*PHI**2) p1= (0, bit(a,2)*PHI**-1, bit(a,1)*PHI, bit(a,0)*SQ5) p2= (bit(a,3)*PHI**-1, bit(a,2), bit(a,1)*PHI, bit(a,0)*2.0) for perm in ( (0,1,2,3), (0,2,3,1), (0,3,1,2), (1,0,3,2), (1,2,0,3), (1,3,2,0), (2,0,1,3), (2,1,3,0), (2,3,0,1), (3,0,2,1), (3,1,0,2), (3,2,1,0)): if a<8: yield Point(p0[perm[0]], p0[perm[1]], p0[perm[2]], p0[perm[3]]) yield Point(p1[perm[0]], p1[perm[1]], p1[perm[2]], p1[perm[3]]) yield Point(p2[perm[0]], p2[perm[1]], p2[perm[2]], p2[perm[3]])
def test_point_key_lookup_get(): x = Point(x=1, y=2, z=3) assert x.x == 1 assert x.y == 2 assert x.z == 3
def test_hash(): x = Point(1, 2, 3) y = Point(1, 2, 3) w = Point(3, 4, 5, 6) z = Point(x=1, y=2, z=3) assert x.__hash__() == y.__hash__() assert x.__hash__() != z.__hash__() assert x.__hash__() != w.__hash__()
def test_point_indexing_get(): x = Point(1, 2, 3) assert x[0] == 1 assert x[1] == 2 assert x[2] == 3