def setUp(self): unittest.TestCase.setUp(self) self.m1 = matrices.Matrix3D([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) self.m2 = matrices.Matrix3D([1, 2, 3], [4, 5, 6], [7, 8, 9]) self.m3 = matrices.Matrix3D(1, 2, 3, 4, 5, 6, 7, 8, 9) self.m4 = matrices.Matrix3D(2, 2, 2, 3, 3, 3, 4, 4, 4)
def testalmostequal(self): m = matrices.Matrix3D(1, 2, 3, 0, 1, 4, 5, 6, 1) self.assertFalse(matrices.almostequal(self.m1, m)) m = matrices.Matrix3D(1, 2, 3, 0, 1, 4, 5, 6, 0.0000000001) self.assertTrue(matrices.almostequal(self.m1, m)) self.assertTrue(matrices.almostequal(m, self.m1))
def testconstructor(self): # Example of a matrix with a determinant of 1, # but that the inverse is not equal to the transpose m = matrices.Matrix3D(3, -4, 1, 5, 3, -7, -9, 2, 6) self.assertEqual(matrices.det(m), 1) self.assertRaises(TypeError, matrices.SpecialOrthogonalMatrix3D, m.to_list())
def _calculate_cartesian_matrix(self): """ Calculate the matrix to change basis from the natural basis of a crystal :math:`(\\vec{a}, \\vec{b}, \\vec{c})` to the cartesian basis :math:`(\\vec{i}, \\vec{j}, \\vec{k})`. The cartesian basis is defined as follows: * :math:`\\vec{k}` is in the direction of :math:`\\vec{c}` * :math:`\\vec{j}` is in the direction of :math:`\\vec{c} \times \\vec{a}` * :math:`\\vec{i}` is in perpendicular to the plane of :math:`\\vec{k}` and :math:`\\vec{j}` **References** Equation 2.31 from Mathematical Crystallography """ g11 = self.a * sin(self.beta) g12 = -self.b * sin(self.alpha) * cos(self.gamma_) g13 = 0 g21 = 0 g22 = self.b * sin(self.alpha) * sin(self.gamma_) g23 = 0 g31 = self.a * cos(self.beta) g32 = self.b * cos(self.alpha) g33 = self.c self.cartesianmatrix = matrices.Matrix3D([g11, g12, g13], [g21, g22, g23], [g31, g32, g33])
def test__setitem__(self): m = matrices.Matrix3D(9, 2, 3, 4, 9, 6, 7, 8, 9) self.m1[0][0] = 9 self.m1[1][1] = 9 self.m1[2][2] = 9 for i in range(3): for j in range(3): self.assertEqual(self.m1[i][j], m[i][j])
def testmetricalmatrix(self): # Example from Mathematical Crystallography L = unitcell.create_hexagonal_unitcell(4.914, 5.409) metricalmatrix = L.metricalmatrix expected_metricalmatrix = matrices.Matrix3D([24.1474, -12.0737, 0.0], [-12.0737, 24.1474, 0.0], [0.0, 0.0, 29.2573]) self.assertTrue( matrices.almostequal(metricalmatrix, expected_metricalmatrix, 4))
def testdet(self): self.assertEqual(matrices.det(self.m1), 1.0) if numpy is not None: for _k in range(REPETITIONS): m = [] for _i in range(3): r = [] for _j in range(3): r.append(random.random()) m.append(r) m_ = numpy.array(m) self.assertAlmostEqual(numpy.linalg.det(m_), matrices.det(matrices.Matrix3D(m)))
def test__rmul__(self): mm = self.m2 * self.m1 self.assertTrue(isinstance(mm, matrices.SpecialOrthogonalMatrix3D)) mm = 4 * self.m1 self.assertFalse(isinstance(mm, matrices.SpecialOrthogonalMatrix3D)) self.assertTrue(isinstance(mm, matrices.Matrix3D)) mm = 1.0 * self.m1 self.assertTrue(isinstance(mm, matrices.SpecialOrthogonalMatrix3D)) # If a special orthogonal matrix is multiplied with a regular matrix, # the results is a regular Matrix3D object. m = matrices.Matrix3D(1, 2, 3, 4, 5, 6, 7, 8, 9) mm = m * self.m1 self.assertFalse(isinstance(mm, matrices.SpecialOrthogonalMatrix3D)) self.assertTrue(isinstance(mm, matrices.Matrix3D))
def testcartesianmatrix(self): alpha = 93.11 / 180.0 * pi beta = 115.91 / 180.0 * pi gamma = 91.26 / 180.0 * pi L = unitcell.create_triclinic_unitcell(8.173, 12.869, 14.165, alpha, beta, gamma) cartesianmatrix = L.cartesianmatrix expected_cartesianmatrix = matrices.Matrix3D( [7.3513, -0.65437, 0.0], [0.0, 12.8333, 0.0], [-3.5716, -0.69886, 14.165]) self.assertTrue( matrices.almostequal(cartesianmatrix, expected_cartesianmatrix, 2)) # Identity G = A^T A g = matrices.transpose(cartesianmatrix) * cartesianmatrix self.assertTrue(matrices.almostequal(g, L.metricalmatrix, 4))
def _calculate_metrical_matrix(self): """ Calculate the metrical matrix (G) of the unit cell. **References** Equation 1.12 and 2.14 from Mathematical Crystallography """ g11 = self.a * self.a g12 = self.a * self.b * cos(self.gamma) g13 = self.a * self.c * cos(self.beta) g21 = g12 g22 = self.b * self.b g23 = self.b * self.c * cos(self.alpha) g31 = g13 g32 = g23 g33 = self.c * self.c self.metricalmatrix = matrices.Matrix3D([g11, g12, g13], [g21, g22, g23], [g31, g32, g33])
def testinverse(self): expected_m1_inverse = matrices.Matrix3D(-24, 18, 5, 20, -15, -4, -5, 4, 1) m1_inverse = matrices.inverse(self.m1) self.assertEqual(expected_m1_inverse, m1_inverse)
def testtranspose(self): expected_m1_transpose = matrices.Matrix3D(1, 0, 5, 2, 1, 6, 3, 4, 0) m1_transpose = matrices.transpose(self.m1) self.assertEqual(expected_m1_transpose, m1_transpose)
def setUp(self): unittest.TestCase.setUp(self) self.m1 = matrices.Matrix3D(1, 2, 3, 0, 1, 4, 5, 6, 0) self.m_so3 = matrices.Matrix3D(0.36, 0.48, -0.8, -0.8, 0.6, 0, 0.48, 0.64, 0.6)