def test_normalization(self): """Test Vector normalization.""" v1 = Vector([2, 0, 0]) self.assertTrue( numpy.array_equal(v1.normalized().get_array(), numpy.array([1, 0, 0])) ) # State of v1 should not be affected by `normalized` self.assertTrue(numpy.array_equal(v1.get_array(), numpy.array([2, 0, 0]))) v1.normalize() # State of v1 should be affected by `normalize` self.assertTrue(numpy.array_equal(v1.get_array(), numpy.array([1, 0, 0])))
def test_rotmat_0(self): """Test rotmat when the rotation is 0 deg (singularity).""" v1 = Vector([1.0, 0.8, 0]) v2 = Vector([1.0, 0.8, 0]) rot = rotmat(v1, v2) v3 = v1.left_multiply(rot) self.assertTrue(numpy.allclose(v1.get_array(), v3.get_array()))
def test_Vector(self): """Test Vector object.""" v1 = Vector(0, 0, 1) v2 = Vector(0, 0, 0) v3 = Vector(0, 1, 0) v4 = Vector(1, 1, 0) self.assertEqual(calc_angle(v1, v2, v3), 1.5707963267948966) self.assertEqual(calc_dihedral(v1, v2, v3, v4), 1.5707963267948966) self.assertTrue( numpy.array_equal((v1 - v2).get_array(), numpy.array([0.0, 0.0, 1.0]))) self.assertTrue( numpy.array_equal((v1 - 1).get_array(), numpy.array([-1.0, -1.0, 0.0]))) self.assertTrue( numpy.array_equal((v1 - (1, 2, 3)).get_array(), numpy.array([-1.0, -2.0, -2.0]))) self.assertTrue( numpy.array_equal((v1 + v2).get_array(), numpy.array([0.0, 0.0, 1.0]))) self.assertTrue( numpy.array_equal((v1 + 3).get_array(), numpy.array([3.0, 3.0, 4.0]))) self.assertTrue( numpy.array_equal((v1 + (1, 2, 3)).get_array(), numpy.array([1.0, 2.0, 4.0]))) self.assertTrue( numpy.array_equal(v1.get_array() / 2, numpy.array([0, 0, 0.5]))) self.assertTrue( numpy.array_equal(v1.get_array() / 2, numpy.array([0, 0, 0.5]))) self.assertEqual(v1 * v2, 0.0) self.assertTrue( numpy.array_equal((v1**v2).get_array(), numpy.array([0.0, -0.0, 0.0]))) self.assertTrue( numpy.array_equal((v1**2).get_array(), numpy.array([0.0, 0.0, 2.0]))) self.assertTrue( numpy.array_equal((v1**(1, 2, 3)).get_array(), numpy.array([0.0, 0.0, 3.0]))) self.assertEqual(v1.norm(), 1.0) self.assertEqual(v1.normsq(), 1.0) v1[2] = 10 self.assertEqual(v1.__getitem__(2), 10)
def test_Vector_angles(self): """Test Vector angles.""" angle = random() * numpy.pi axis = Vector(random(3) - random(3)) axis.normalize() m = rotaxis(angle, axis) cangle, caxis = m2rotaxis(m) self.assertAlmostEqual(angle, cangle, places=3) self.assertTrue( numpy.allclose(list(map(int, (axis - caxis).get_array())), [0, 0, 0]), "Want %r and %r to be almost equal" % (axis.get_array(), caxis.get_array()))
def calculateCoordinates(refA: Residue, refB: Residue, refC: Residue, L: float, ang: float, di: float) -> np.ndarray: AV = refA.get_vector() BV = refB.get_vector() CV = refC.get_vector() CA = AV - CV CB = BV - CV ##CA vector AX = CA[0] AY = CA[1] AZ = CA[2] ##CB vector BX = CB[0] BY = CB[1] BZ = CB[2] ##Plane Parameters A = (AY * BZ) - (AZ * BY) B = (AZ * BX) - (AX * BZ) G = (AX * BY) - (AY * BX) ##Dot Product Constant F = math.sqrt(BX * BX + BY * BY + BZ * BZ) * L * math.cos( ang * (math.pi / 180.0)) ##Constants const = math.sqrt( math.pow((B * BZ - BY * G), 2) * (-(F * F) * (A * A + B * B + G * G) + (B * B * (BX * BX + BZ * BZ) + A * A * (BY * BY + BZ * BZ) - (2 * A * BX * BZ * G) + (BX * BX + BY * BY) * G * G - (2 * B * BY) * (A * BX + BZ * G)) * L * L)) denom = ((B * B) * (BX * BX + BZ * BZ) + (A * A) * (BY * BY + BZ * BZ) - (2 * A * BX * BZ * G) + (BX * BX + BY * BY) * (G * G) - (2 * B * BY) * (A * BX + BZ * G)) X = ((B * B * BX * F) - (A * B * BY * F) + (F * G) * (-A * BZ + BX * G) + const) / denom if (B == 0 or BZ == 0) and (BY == 0 or G == 0): const1 = math.sqrt(G * G * (-A * A * X * X + (B * B + G * G) * (L - X) * (L + X))) Y = ((-A * B * X) + const1) / (B * B + G * G) Z = -(A * G * G * X + B * const1) / (G * (B * B + G * G)) else: Y = ((A * A * BY * F) * (B * BZ - BY * G) + G * (-F * math.pow(B * BZ - BY * G, 2) + BX * const) - A * (B * B * BX * BZ * F - B * BX * BY * F * G + BZ * const)) / ( (B * BZ - BY * G) * denom) Z = ((A * A * BZ * F) * (B * BZ - BY * G) + (B * F) * math.pow(B * BZ - BY * G, 2) + (A * BX * F * G) * (-B * BZ + BY * G) - B * BX * const + A * BY * const) / ( (B * BZ - BY * G) * denom) # Get the new Vector from the origin D = Vector(X, Y, Z) + CV with warnings.catch_warnings(): # ignore inconsequential warning warnings.simplefilter("ignore") temp = calc_dihedral(AV, BV, CV, D) * (180.0 / math.pi) di = di - temp rot = rotaxis(math.pi * (di / 180.0), CV - BV) D = (D - BV).left_multiply(rot) + BV return D.get_array()