Пример #1
0
    def calculate_schmidttransformation(self):
        a1 = canonical_e0
        b1 = canonical_e1
        c1 = canonical_e2

        a2 = a1
        a3 = a2 * (1 / self.length(a2))

        b2 = b1 - a3 * self.dot(a3, b1)
        b3 = b2 * (1 / self.length(b2))

        c2 = c1 - a3 * self.dot(a3, c1) - b3 * self.dot(b3, c1)
        c3 = c2 * (1 / self.length(c2))

        M = nb.Matrix([[a3.x(), b3.x(), c3.x()], [a3.y(),
                                                  b3.y(),
                                                  c3.y()],
                       [a3.z(), b3.z(), c3.z()]]).inv()
        transformation = Transformation(
            nb.Matrix([[
                M.liste[0].liste[0], M.liste[0].liste[1], M.liste[0].liste[2],
                0
            ],
                       [
                           M.liste[1].liste[0], M.liste[1].liste[1],
                           M.liste[1].liste[2], 0
                       ],
                       [
                           M.liste[2].liste[0], M.liste[2].liste[1],
                           M.liste[2].liste[2], 0
                       ], [0, 0, 0, 1]]))

        return transformation
Пример #2
0
def test_Transgen():
    tg = geo.Transgen(geo.Dif(nb.Matrix([[1], [0], [0], [0]])),
                      geo.Dif(nb.Matrix([[0], [0], [2], [0]])),
                      geo.Dif(nb.Matrix([[0], [3], [0], [0]])))
    assert tg.__str__() == "Transgen /  1  \   /  0  \   /  0  \ \n"\
                           "        |   0   | |   0   | |   3   |\n"\
                           "         \  0  /   \  2  /   \  0  / "
Пример #3
0
def test_Pos():
    x = geo.Pos(nb.Matrix([[1], [2], [3], [1]]))
    assert x.__str__() == "Pos /  1  \ \n   |   2   |\n    \  3  / "
    assert x.x() == 1
    assert x.y() == 2
    assert x.z() == 3
    x = geo.origin
    assert x == geo.Pos(nb.Matrix([[0], [0], [0], [1]]))
Пример #4
0
def test_Dif():
    x = geo.Dif(nb.Matrix([[1], [2], [3], [0]]))
    assert x.__str__() == "Dif /  1  \ \n   |   2   |\n    \  3  / "
    assert x.x() == 1
    assert x.y() == 2
    assert x.z() == 3
    assert x.to_Symmetry() == geo.Symmetry(
        nb.Matrix([[1, 0, 0, 1], [0, 1, 0, 2], [0, 0, 1, 3], [0, 0, 0, 1]]))
Пример #5
0
def test_Rec():
    q = geo.Rec(nb.Matrix([[1, 2, 3, 0]]))
    assert q.__str__() == "Rec <  1  2  3  > "
    assert q.h() == 1
    assert q.k() == 2
    assert q.l() == 3
    x = geo.Rec(nb.Matrix([[0, 0, 1.00000000, 0]]))
    y = geo.Rec(nb.Matrix([[0, 0, 0.99999999, 0]]))
    assert hash(x) == hash(y)
Пример #6
0
def test_Spacegroup():
    transgen = geo.canonical
    c1 = geo.Coset(geo.Symmetry(nb.Matrix.onematrix(4)), transgen)
    c2 = geo.Coset(
        geo.Symmetry(
            nb.Matrix([[-1, 0, 0, 0], [0, -1, 0, 0], [0, 0, -1, 0],
                       [0, 0, 0, 1]])), transgen)
    sg = geo.Spacegroup(transgen, [c1, c2])
    assert sg.__str__() == \
        "Spacegroup        \n"\
        "----------        \n"\
        " canonical        \n"\
        "             x,y,z\n"\
        "          -x,-y,-z"
    transgen = geo.Transgen(geo.Dif(nb.Matrix([[1], [0], [0], [0]])),
                            geo.Dif(nb.Matrix([[0], [1], [0], [0]])),
                            geo.Dif(nb.Matrix([[0], [0], [2], [0]])))
    c1 = geo.Coset(geo.Symmetry(nb.Matrix.onematrix(4)), transgen)
    c2 = geo.Coset(
        geo.Symmetry(
            nb.Matrix([[-1, 0, 0, 0], [0, -1, 0, 0], [0, 0, -1, 0],
                       [0, 0, 0, 1]])), transgen)
    sg = geo.Spacegroup(transgen, [c1, c2])
    assert sg.__str__() == \
        "                           Spacegroup        \n"\
        "                           ----------        \n"\
        "Transgen /  1  \   /  0  \   /  0  \         \n"\
        "        |   0   | |   1   | |   0   |        \n"\
        "         \  0  /   \  0  /   \  2  /         \n"\
        "                                        x,y,z\n"\
        "                                     -x,-y,-z"

    sg = geo.Spacegroup(geo.canonical, [
        geo.Coset(geo.Symmetry(nb.Matrix.onematrix(4)), geo.canonical),
        geo.Coset(
            geo.Symmetry(
                nb.Matrix([[1, 0, 0, 0], [0, -1, 0, 0], [0, 0, 1, 0],
                           [0, 0, 0, 1]])), geo.canonical)
    ])

    transformation = geo.Transformation(
        nb.Matrix([[0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]))
    sg1 = transformation**sg
    sg2 = geo.Spacegroup(transformation**geo.canonical, [
        geo.Coset(geo.Symmetry(nb.Matrix.onematrix(4)), geo.canonical),
        geo.Coset(
            geo.Symmetry(
                nb.Matrix([[-1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0],
                           [0, 0, 0, 1]])), geo.canonical)
    ])

    assert sg.is_really_a_spacegroup() == True
    sg_error = geo.Spacegroup(geo.canonical, [
        geo.Coset(geo.Symmetry(nb.Matrix.onematrix(4)), geo.canonical),
        geo.Coset(
            geo.Symmetry(
                nb.Matrix([[1, 0, 0, fr.Fraction(1, 4)], [0, 1, 0, 0],
                           [0, 0, 1, 0], [0, 0, 0, 1]])), geo.canonical)
    ])
    assert sg_error.is_really_a_spacegroup() == False
Пример #7
0
def test_Operator():
    id = geo.Operator(
        nb.Matrix([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]))
    g = geo.Operator(
        nb.Matrix([[-1, 0, 0, 0], [0, -1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]))
    g1 = geo.Operator(
        nb.Matrix([[-1, 0, 0, 0], [0, -1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]))
    assert g.__str__() == "Operator /  -1   0  0  0  \ \n"\
                          "        |    0  -1  0  0   |\n"\
                          "        |    0   0  1  0   |\n"\
                          "         \   0   0  0  1  / "
    assert g == g1
Пример #8
0
 def __mul__(self, right):
     if isinstance(right, nb.Mixed):
         return Dif(
             nb.Matrix([[right * self.x()], [right * self.y()],
                        [right * self.z()], [0]]))
     else:
         return NotImplemented
Пример #9
0
def test_Symmetry():
    g = geo.Symmetry(
        nb.Matrix([[1, 0, 2, -1], [0, -2, 0, 0], [0, 0, 1,
                                                  fr.Fraction(1, 3)],
                   [0, 0, 0, 1]]))
    assert g.__str__() == "x+2z-1,-2y,z+1/3"

    assert isinstance(g.inv(), geo.Symmetry)
    assert g * g.inv() == geo.Symmetry(nb.Matrix.onematrix(4))
Пример #10
0
    def sort(self):
        # This is the sorting of algorithm A of:
        #     H. D. Flack
        #     "The Derivation of Twin Laws for
        #      (Pseudo-)Merohedry by Coset
        #      Decomposition"
        #     Acta Cryst. (1987) A43, 564-568
        #
        identity = []
        twofold_rotations = []
        non_twofold_rotations = []
        inversion = []
        mirror_reflections = []
        non_twofold_second_kind = []

        id = Symmetry(
            nb.Matrix([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0,
                                                                  1]]))
        I = Symmetry(
            nb.Matrix([[-1, 0, 0, 0], [0, -1, 0, 0], [0, 0, -1, 0],
                       [0, 0, 0, 1]]))

        for symmetry in self.liste:
            if symmetry == id:
                identity.append(symmetry)
            elif symmetry.value.det() == 1:
                if symmetry * symmetry == id:
                    twofold_rotations.append(symmetry)
                else:
                    non_twofold_rotations.append(symmetry)
            else:
                if symmetry == I:
                    inversion.append(symmetry)
                elif symmetry * symmetry == id:
                    mirror_reflections.append(symmetry)
                else:
                    non_twofold_second_kind.append(symmetry)

        return Pointgroup(identity + twofold_rotations +
                          non_twofold_rotations + inversion +
                          mirror_reflections + non_twofold_second_kind)
Пример #11
0
def test_Symmetry():
    g = geo.Symmetry(nb.Matrix([[1,  0, 2, -1],
                                [0, -2, 0,  0],
                                [0,  0, 1,  fr.Fraction(1, 3)],
                                [0,  0, 0,  1]]))
    assert g.__str__() == "x+2z-1,-2y,z+1/3"

    assert isinstance(g.inv(), geo.Symmetry)
    assert g * g.inv() == geo.Symmetry(nb.Matrix.onematrix(4))

    g = geo.Symmetry(nb.Matrix([[0,-1, 0, 0],
                                [1, -1,  0, 0],
                                [0,  0,  1, 0],
                                [0,  0,  0, 1]]))
    print(g)
    assert g.__str__() == "-y,x-y,z"
    x = uc.ufloat(0.4, 0.1)
    p = geo.Pos(nb.Matrix([[x], [x], [0], [1]]))
    print(p)
    print((g*g*g)**p)
    assert (g*g*g)**p == p
Пример #12
0
def centre_of_gravity(liste):
    # Calculates the centre of gravity of a list of positions
    # of type geo.Pos.
    assert isinstance(liste, list), \
        "Argument of geo.centre_of_gravity(...) must be of type list."
    for item in liste:
        assert isinstance(item, Pos), \
            "Argument of geo.centre_of_gravity(...) must be a list " \
            "of objects of type geo.Pos."
    summe = Dif(nb.Matrix([[0], [0], [0], [0]]))
    for item in liste:
        summe += item - origin
    return origin + Dif((summe.value * (1 / nb.Mixed(len(liste)))))
Пример #13
0
 def __str__(self):
     result = 'Transformation '
     m = self.value.inv()
     Ox = m.liste[0].liste[3]
     Oy = m.liste[1].liste[3]
     Oz = m.liste[2].liste[3]
     matrix = nb.Matrix([[1, 0, 0, Ox], [0, 1, 0, Oy], [0, 0, 1, Oz],
                         [0, 0, 0, 1]])
     result = "Transformation O -> (%s, %s, %s)\n" \
              "               then\n" % \
     (Ox.__str__(), Oy.__str__(), Oz.__str__())
     matrix = nb.Matrix([
         nb.Row([
             m.liste[0].liste[0], m.liste[1].liste[0], m.liste[2].liste[0],
             0
         ]),
         nb.Row([
             m.liste[0].liste[1], m.liste[1].liste[1], m.liste[2].liste[1],
             0
         ]),
         nb.Row([
             m.liste[0].liste[2], m.liste[1].liste[2], m.liste[2].liste[2],
             0
         ]),
         nb.Row([
             m.liste[3].liste[0], m.liste[3].liste[1], m.liste[3].liste[2],
             1
         ])
     ])
     terms = []
     for i in range(3):
         print(matrix.liste[i].liste[0], matrix.liste[i].liste[1],
               matrix.liste[i].liste[2], matrix.liste[i].liste[3])
         terms.append(
             linearterm2str(matrix.liste[i].liste, ["a", "b", "c", '']))
     return result + "               a' = " + terms[0] + "\n" \
                   + "               b' = " + terms[1] + "\n" \
                   + "               c' = " + terms[2]
Пример #14
0
def symmetryfromstr(string):
    words = string.split(',')
    assert len(words) == 3, \
        "The following string looks like a Symmetry, but it has not "\
        "three comma-separated terms: %s" % (string)
    liste = []
    for word in words:
        row = nb.Row(str2linearterm(word, ['x', 'y', 'z']))
        liste.append(row)
    liste.append(
        nb.Row([fromstr("0"),
                fromstr("0"),
                fromstr("0"),
                fromstr("1")]))
    return geo.Symmetry(nb.Matrix(liste))
Пример #15
0
def test_eq():
    r1 = geo.Pos(nb.Matrix([[1], [2], [3], [1]]))
    r2 = geo.Pos(nb.Matrix([[1], [2], [3], [1]]))
    r3 = geo.Pos(nb.Matrix([[1], [2], [4], [1]]))
    d1 = geo.Dif(nb.Matrix([[4], [5], [6], [0]]))
    d2 = geo.Dif(nb.Matrix([[4], [5], [6], [0]]))
    d3 = geo.Dif(nb.Matrix([[3], [5], [6], [0]]))
    q1 = geo.Rec(nb.Matrix([[1, 2, 3, 0]]))
    q2 = geo.Rec(nb.Matrix([[1, 2, 3, 0]]))
    q3 = geo.Rec(nb.Matrix([[2, 3, 4, 0]]))
    assert not (r1 == d1)
    assert     (r1 == r1)
    assert     (r1 == r2)
    assert not (r1 == r3)
    assert     (d1 == d1)
    assert     (d1 == d2)
    assert not (d1 == d3)
    assert not (r1 == q1)
    assert not (d1 == q1)
    assert     (q1 == q1)
    assert     (q1 == q2)
    assert not (q1 == q3)
Пример #16
0
 def to_Metric(self):
     a = self.a
     b = self.b
     c = self.c
     alpha = self.alpha
     beta = self.beta
     gamma = self.gamma
     aa = a * a
     bb = b * b
     cc = c * c
     ab = a * b * nb.cos(nb.deg2rad(gamma))
     ac = a * c * nb.cos(nb.deg2rad(beta))
     bc = b * c * nb.cos(nb.deg2rad(alpha))
     return Metric(
         nb.Matrix([
             nb.Row([aa, ab, ac, 0]),
             nb.Row([ab, bb, bc, 0]),
             nb.Row([ac, bc, cc, 0]),
             nb.Row([0, 0, 0, 1])
         ]))
Пример #17
0
def matrixfromstr(string):
    string = string.replace('|', '\\')
    string = re.sub('\/ | \/|[<>]', ' ', string)
    #    string = string.replace('/ ', ' ')
    #    string = string.replace('<', ' ')
    #    string = string.replace('>', ' ')
    #    string = string.replace(' /', ' ')
    string = string.replace('\n', '\\')
    #    string = re.sub('\\\\ +\\\\', '\\', string)
    for i in range(4):
        string = string.replace('\\ \\', '\\')
        string = string.replace('\\  \\', '\\')
        string = string.replace('\\   \\', '\\')
    rowwords = string.split('\\')
    rowliste = []
    for rowword in rowwords:
        words = rowword.split()
        liste = []
        for word in words:
            liste.append(mixedfromstr(word))
        rowliste.append(nb.Row(liste))
    return nb.Matrix(rowliste)
Пример #18
0
 def __init__(self, b1, b2, b3):
     assert  isinstance(b1, Dif) \
         and isinstance(b2, Dif) \
         and isinstance(b3, Dif), \
         "Arguments must be of type Dif."
     self.liste = [b1, b2, b3]
     m00 = b1.value.liste[0].liste[0]
     m01 = b2.value.liste[0].liste[0]
     m02 = b3.value.liste[0].liste[0]
     m10 = b1.value.liste[1].liste[0]
     m11 = b2.value.liste[1].liste[0]
     m12 = b3.value.liste[1].liste[0]
     m20 = b1.value.liste[2].liste[0]
     m21 = b2.value.liste[2].liste[0]
     m22 = b3.value.liste[2].liste[0]
     self.transformation = Transformation(
         nb.Matrix([
             nb.Row([m00, m01, m02, 0]),
             nb.Row([m10, m11, m12, 0]),
             nb.Row([m20, m21, m22, 0]),
             nb.Row([0, 0, 0, 1])
         ]))
     self.transformationinv = self.transformation.inv()
Пример #19
0
def test_Metric():
    M = nb.Matrix([[1, 0, 0, 0],
                   [0, 1, 0, 0],
                   [0, 0, 1, 0],
                   [0, 0, 0, 1]])
    metric = geo.Metric(M)
    t = metric.schmidttransformation

    assert t ** geo.canonical_e0 == geo.Dif(nb.Matrix([[1], [0], [0], [0]]))
    assert t ** geo.canonical_e1 == geo.Dif(nb.Matrix([[0], [1], [0], [0]]))
    assert t ** geo.canonical_e2 == geo.Dif(nb.Matrix([[0], [0], [1], [0]]))

    metric = geo.Cellparameters(1, 1, 1, 90, 90, 45).to_Metric()
    t = metric.schmidttransformation
    assert t ** geo.canonical_e0 == geo.Dif(nb.Matrix([[1], [0], [0], [0]]))
    e1 = t ** geo.canonical_e1
    assert abs(e1.x() - 0.70710678).value < 0.000001
    assert abs(e1.y() - 0.70710678).value < 0.000001
    assert abs(e1.z() - 0).value < 0.000001
    e2 = t ** geo.canonical_e2
    assert abs(e2.x() - 0).value < 0.000001
    assert abs(e2.y() - 0).value < 0.000001
    assert abs(e2.z() - 1).value < 0.000001

    t = metric.schmidttransformation.inv()

    M = nb.Matrix([[9, 0, 0, 0],
                   [0, 4, 0, 0],
                   [0, 0, 1, 0],
                   [0, 0, 0, 1]])
    metric = geo.Metric(M)
    assert metric.__str__() == "Metric /  9  0  0  0  \ \n" \
                               "      |   0  4  0  0   |\n" \
                               "      |   0  0  1  0   |\n" \
                               "       \  0  0  0  1  / "
    o = geo.Pos(nb.Matrix([[0], [0], [0], [1]]))
    p1 = geo.Pos(nb.Matrix([[1], [0], [0], [1]]))
    p2 = geo.Pos(nb.Matrix([[0], [1], [0], [1]]))
    q1 = geo.Rec(nb.Matrix([[1, 0, 0, 0]]))
    q2 = geo.Rec(nb.Matrix([[0, 1, 0, 0]]))
    q3 = geo.Rec(nb.Matrix([[1, 1, 0, 0]]))
    assert metric.dot(p1 - o, p1 - o).__str__() == "9"
    assert metric.dot(p1 - o, p2 - o).__str__() == "0"
    assert metric.dot(q1, q1).__str__() == "1/9"
    assert metric.dot(q2, q2).__str__() == "1/4"
    assert metric.dot(q3, q3).__str__() == "13/36"
    assert metric.length(p1 - o) == 3
    assert np.abs(float(metric.angle(p1 - o, p2 - o)) - 1.5707963) < 0.0001
    assert metric.dangle(p1 - o, p2 - o) == 90
    assert metric.length(q1).__str__() == "1/3"
    assert np.abs(float(metric.angle(q1, q2)) - 1.5707963) < 0.0001
    assert metric.dangle(q1, q2) == 90
    assert metric.angle(p1 - o, p1 - o).__str__() == "0.0"
    assert metric.dangle(p1 - o, p1 - o).__str__() == "0"
    assert metric.angle(q1, q1).__str__() == "0.0"
    assert metric.dangle(q1, q1).__str__() == "0"

    metric = geo.Cellparameters(4.15, 4.15, 28.64, 90, 90, 120).to_Metric()

    M = nb.Matrix([[9, 0, 0, 0],
                   [0, 4, 0, 0],
                   [0, 0, 1, 0],
                   [0, 0, 0, 1]])
    metric = geo.Metric(M)

    cell = metric.to_Cellparameters()
    assert cell.__str__() == \
        geo.Cellparameters(3, 2, 1, 90, 90, 90).__str__()

    t = geo.Transformation(nb.Matrix([[0, 1, 0, 0],
                                      [1, 0, 0, 0],
                                      [0, 0, 1, 0.5],
                                      [0, 0, 0, 1]]))

    assert t ** metric == geo.Metric(nb.Matrix([[4, 0, 0, 0],
                                                [0, 9, 0, 0],
                                                [0, 0, 1, 0],
                                                [0, 0, 0, 1]]))

    metric = geo.Cellparameters(fs("8.534(5)"), fs("8.556(5)"), fs("7.015(5)"),
                                fs("101.53(8)"), fs("114.97(8)"), 
                                fs("103.38(8)")).to_Metric()
    assert np.abs(metric.cellvolume().value.n - 425.24239) < 0.0001

    metric = geo.Cellparameters(fs("1.0(1)"), 1, 1, 90, 90, 90).to_Metric()
    print(metric.cellvolume())
    assert np.abs(metric.cellvolume().value.n - 1) < 0.00000001
    assert np.abs(metric.cellvolume().value.s - 0.1) < 0.00000001

    a = fs("1.0(1)")
    metric = geo.Cellparameters(a, a, a, 90, 90, 90).to_Metric()
    m00 = metric.value.liste[0].liste[0]
    m11 = metric.value.liste[1].liste[1]
    assert (m00 - m11).value.n == 0.0
    assert (m00 - m11).value.s == 0.0
Пример #20
0
def test_Skalarprodukt():
    d1 = geo.Dif(nb.Matrix([[1], [2], [3], [0]]))
    q1 = geo.Rec(nb.Matrix([[4, 5, 6, 0]]))
    assert q1 * d1 == 32
Пример #21
0
def test_centre_of_gravity():
    p1 = geo.Pos(nb.Matrix([[1], [0], [0], [1]]))
    p2 = geo.Pos(nb.Matrix([[3], [0], [0], [1]]))
    assert geo.centre_of_gravity([p1, p2]) == geo.Pos(nb.Matrix([[2], [0], [0], [1]]))
Пример #22
0
def test_add_and_sub():
    x1 = geo.Pos(nb.Matrix([[1], [2], [3], [1]]))
    x2 = geo.Pos(nb.Matrix([[2], [3], [4], [1]]))
    d1 = geo.Dif(nb.Matrix([[4], [5], [6], [0]]))
    d2 = geo.Dif(nb.Matrix([[5], [5], [5], [0]]))
    assert (x1 + d1) == geo.Pos(nb.Matrix([[5], [7], [9], [1]]))
    assert (d1 + d2) == geo.Dif(nb.Matrix([[9], [10], [11], [0]]))
    assert (d1 - d2) == geo.Dif(nb.Matrix([[-1], [0], [1], [0]]))
    assert (x1 - d1) == geo.Pos(nb.Matrix([[-3], [-3], [-3], [1]]))
    assert (x1 - x2) == geo.Dif(nb.Matrix([[-1], [-1], [-1], [0]]))
    q1 = geo.Rec(nb.Matrix([[1, 2, 3, 0]]))
    q2 = geo.Rec(nb.Matrix([[4, 5, 6, 0]]))
    assert (q1 + q2) == geo.Rec(nb.Matrix([[5, 7, 9, 0]]))
    assert (q1 - q2) == geo.Rec(nb.Matrix([[-3, -3, -3, 0]]))
    assert -d1 == geo.Dif(nb.Matrix([[-4], [-5], [-6], [0]]))
    assert -q1 == geo.Rec(nb.Matrix([[-1, -2, -3, 0]]))
Пример #23
0
def test_operations():
    # Here I want to test all operations between equal and different types.
    # It shall be as follows:
    #
    #         *         | Symmetry    Transformation   Transgen   Coset  Pos Dif Metric  Spacegroup
    # ----------------------------------------------------------------------------------------------
    #   Symmetry        | Symmetry         -              -         -     -   -    -          -
    #   Transformation  |    -        Transformation      -         -     -   -    -          -
    #   Transgen        |    -             -              -         -     -   -    -          -
    #   Coset           |    -             -              -       Coset   -   -    -          -
    #   Pos             |    -             -              -         -     -   -    -          -
    #   Dif             |    -             -              -         -     -   -    -          -
    #   Metric          |    -             -              -         -     -   -    -          -
    #   Spacegroup      |    -             -              -         -     -   -    -          -
    #
    #
    #         **        | Symmetry  Transformation  Transgen  Coset    Pos   Dif Metric  Spacegroup
    # -----------------------------------------------------------------------------------------------
    #   Symmetry        |    -             -           -        -      Pos   Dif   -         -
    #   Transformation  | Symmetry         -        Transgen  Coset    Pos   Dif Metric  Spacegroup
    #   Transgen        |    -             -           -        -       -     -    -         -
    #   Coset           |    -             -           -        -      Pos   Dif   -         -
    #   Pos             |    -             -           -        -       -     -    -         -
    #   Dif             |    -             -           -        -       -     -    -         -
    #   Metric          |    -             -           -        -       -     -    -         -
    #   Spacegroup      |    -             -           -        -       -     -    -         -
    #
    #
    #         %         | Symmetry  Transformation   Transgen  Coset   Pos   Dif Metric  Spacegroup
    #  ---------------------------------------------------------------------------------------------
    #   Symmetry        |    -            -          Symmetry    -      -     -    -         -
    #   Transformation  |    -            -             -        -      -     -    -         -
    #   Transgen        |    -            -             -        -      -     -    -         -
    #   Coset           |    -            -           Coset      -      -     -    -         -
    #   Pos             |    -            -            Pos       -      -     -    -         -
    #   Dif             |    -            -            Dif       -      -     -    -         -
    #   Metric          |    -            -             -        -      -     -    -         -
    #   Spacegroup      |    -            -          Spacegroup  -      -     -    -         -

    # Coset needs some pow()-declarations, so first I test everything else:
    symmetry = geo.Symmetry(nb.Matrix([[-1,  0, 0, 0],
                                       [0, -1, 0, 0],
                                       [0,  0, 1, 0],
                                       [0,  0, 0, 1]]))
    transformation = geo.Transformation(nb.Matrix([[0, 1, 0, 0],
                                                   [1, 0, 0, 0],
                                                   [0, 0, 2, 0],
                                                   [0, 0, 0, 1]]))
    transgen = geo.canonical
    pos = geo.Pos(nb.Matrix([[1], [2], [3], [1]]))
    dif = geo.Dif(nb.Matrix([[4], [5], [6], [0]]))
    metric = geo.Metric(nb.Matrix([[4, 0, 0, 0],
                                   [0, 9, 0, 0],
                                   [0, 0, 1, 0],
                                   [0, 0, 0, 1]]))
    spacegroup = geo.Spacegroup(geo.canonical,
        [geo.Coset(geo.Symmetry(nb.Matrix.onematrix(4)), geo.canonical),
         geo.Coset(geo.Symmetry(nb.Matrix([[-1,  0,  0, 0],
                                           [0, -1,  0, 0],
                                           [0,  0, -1, 0],
                                           [0,  0,  0, 1]])), geo.canonical)])

    # * :
    #====

    # symmetry * symmetry:
    assert isinstance(symmetry * symmetry, geo.Symmetry)

    # transformation * transformation
    assert isinstance(transformation * transformation, geo.Transformation)

    # **:
    #====

    # symmetry ** pos:
    assert isinstance(symmetry ** pos, geo.Pos)

    # symmetry ** dif:
    assert isinstance(symmetry ** dif, geo.Dif)

    # transformation ** symmetry:
    assert isinstance(transformation ** symmetry, geo.Symmetry)

    # transformation ** transgen:
    assert isinstance(transformation ** transgen, geo.Transgen)

    # transformation ** pos:
    assert isinstance(transformation ** pos, geo.Pos)

    # transformation ** dif:
    assert isinstance(transformation ** dif, geo.Dif)

    # transformation ** metric:
    assert isinstance(transformation ** metric, geo.Metric)

    # transformation ** spacegroup:
    assert isinstance(transformation ** spacegroup, geo.Spacegroup)

    # %:
    #===

    # symmetry % transgen:
    assert isinstance(symmetry % transgen, geo.Symmetry)

    # pos % transgen:
    assert isinstance(pos % transgen, geo.Pos)
    transgen1 = geo.Transgen(geo.Dif(nb.Matrix([[1], [0], [0], [0]])),
                             geo.Dif(nb.Matrix([[0], [1], [0], [0]])),
                             geo.Dif(nb.Matrix([[0], [0], [2], [0]])))
    pos1 = geo.Pos(nb.Matrix([[0], [0], [fr.Fraction(3, 2)], [1]]))
    pos1_ = geo.Pos(nb.Matrix([[0], [0], [fr.Fraction(3, 2)], [1]]))
    assert pos1 % transgen1 == pos1_

    transgen1 = geo.Transgen(geo.Dif(nb.Matrix([[0], [0], [2], [0]])),
                             geo.Dif(nb.Matrix([[0], [1], [0], [0]])),
                             geo.Dif(nb.Matrix([[1], [0], [0], [0]])))
    pos1 = geo.Pos(nb.Matrix([[0], [0], [fr.Fraction(3, 2)], [1]]))
    pos1_ = geo.Pos(nb.Matrix([[0], [0], [fr.Fraction(3, 2)], [1]]))
    assert pos1 % transgen1 == pos1_

    # dif % transgen:
    assert isinstance(dif % transgen, geo.Dif)

    transgen1 = geo.Transgen(geo.Dif(nb.Matrix([[0], [0], [2], [0]])),
                             geo.Dif(nb.Matrix([[0], [1], [0], [0]])),
                             geo.Dif(nb.Matrix([[1], [0], [0], [0]])))
    dif1 = geo.Dif(nb.Matrix([[0], [0], [fr.Fraction(3, 2)], [0]]))
    dif1_ = geo.Dif(nb.Matrix([[0], [0], [fr.Fraction(3, 2)], [0]]))
    assert dif1 % transgen1 == dif1_

    # spacegroup % transgen:
    assert isinstance(spacegroup % transgen, geo.Spacegroup)

    # Here comes Coset:
    #==================

    coset = geo.Coset(symmetry, transgen)

    # coset * coset:
    assert isinstance(coset * coset, geo.Coset)

    # coset ** pos:
    assert isinstance(coset ** pos, geo.Pos)

    # coset ** dif:
    assert isinstance(coset ** dif, geo.Dif)

    # coset % transgen:
    assert isinstance(coset % transgen, geo.Coset)

    transgen1 = geo.Transgen(geo.Dif(nb.Matrix([[0], [0], [2], [0]])),
                             geo.Dif(nb.Matrix([[0], [1], [0], [0]])),
                             geo.Dif(nb.Matrix([[1], [0], [0], [0]])))
    coset1 = geo.Coset(geo.Symmetry(nb.Matrix([[1, 0, 0, fr.Fraction(3, 2)],
                                               [0, 1, 0, fr.Fraction(3, 2)],
                                               [0, 0, 1, fr.Fraction(7, 2)],
                                               [0, 0, 0, 1]])), transgen1)
    coset1_ = geo.Coset(geo.Symmetry(nb.Matrix([[1, 0, 0, fr.Fraction(1, 2)],
                                                [0, 1, 0, fr.Fraction(1, 2)],
                                                [0, 0, 1, fr.Fraction(3, 2)],
                                                [0, 0, 0, 1]])), transgen1)
    assert coset1 % transgen1 == coset1_
Пример #24
0
def test_Coset():
    g = geo.Symmetry(nb.Matrix([[1,  0, 2, -1],
                                [0, -2, 0,  0],
                                [0,  0, 1,  1],
                                [0,  0, 0,  1]]))
    tg = geo.Transgen(geo.Dif(nb.Matrix([[1], [0], [0], [0]])),
                      geo.Dif(nb.Matrix([[0], [1], [0], [0]])),
                      geo.Dif(nb.Matrix([[0], [0], [2], [0]])))
    tg1 = geo.Transgen(geo.Dif(nb.Matrix([[1], [0], [0], [0]])),
                       geo.Dif(nb.Matrix([[0], [1], [0], [0]])),
                       geo.Dif(nb.Matrix([[0], [0], [2], [0]])))
    assert tg == tg1
    c = geo.Coset(g, tg)
    assert c.__str__() == \
        "{x+2z,-2y,z+1}\n"\
        "     1  0  0  \n"\
        "     0  1  0  \n"\
        "     0  0  2  "
    c = geo.Coset(g, geo.canonical)
    assert c.__str__() == "{x+2z,-2y,z}"
    g = geo.Symmetry(nb.Matrix([[ 1, 0, 0, 0.5],
                                [ 0, 1, 0, 0],
                                [ 0, 0, 1, 0],
                                [ 0, 0, 0, 1]]))
    c = geo.Coset(g, geo.canonical)
    g2 = geo.Symmetry(nb.Matrix([[ 1, 0, 0, -0.5],
                                 [ 0, 1, 0, 0],
                                 [ 0, 0, 1, 0],
                                 [ 0, 0, 0, 1]]))
    p1 = geo.Pos(nb.Matrix([[0.3], [0], [0], [1]]))
    p2 = geo.Pos(nb.Matrix([[0.8], [0], [0], [1]]))
    assert c.coset_representant_for_pos(p1) == g
    assert c.coset_representant_for_pos(p2) == g2
Пример #25
0
def test_fromstr():
    string = "1/2"
    assert isinstance(fs(string), nb.Mixed)
    assert fs(string) == nb.Mixed(fr.Fraction(1, 2))
    string = "1.2+/-0.1"
    assert fs(string).value.n == nb.Mixed(uc.ufloat(1.2, 0.1)).value.n
    assert fs(string).value.s == nb.Mixed(uc.ufloat(1.2, 0.1)).value.s
    string = "1.2(1)"
    assert fs(string).value.n == nb.Mixed(uc.ufloat(1.2, 0.1)).value.n
    assert fs(string).value.s == nb.Mixed(uc.ufloat(1.2, 0.1)).value.s
    string = "4"
    assert fs(string) == nb.Mixed(fr.Fraction(4, 1))
    string = "4.5"
    assert fs(string) == nb.Mixed(4.5)

    string = "1 2 3"
    assert fromstr.typefromstr(string) == nb.Matrix
    assert fs(string) == nb.Matrix([[1, 2, 3]])

    string = "/ 1 2 \ \n \ 3 4 /"
    assert fs(string) == \
        nb.Matrix([nb.Row([nb.Mixed(1), nb.Mixed(2)]),
                   nb.Row([nb.Mixed(3), nb.Mixed(4)])])
    string = "1 2 \n 3 4"
    assert fs(string) == \
        nb.Matrix([nb.Row([nb.Mixed(1), nb.Mixed(2)]),
                   nb.Row([nb.Mixed(3), nb.Mixed(4)])])
    string = "x+y,y - x +1/3,2z"
    g = fs(string)
    assert g == geo.Symmetry(
        fs("/ 1 1 0 0 \n"
           " -1 1 0 1/3 \n"
           "  0 0 2 0 \n"
           "  0 0 0 1"))
    string = "O->(0,0,0)\n" \
             "then\n" \
             "a' = a-b \n" \
             "b' = b+a \n" \
             "c' = 2c"
    g = fs(string)
    assert g == geo.Transformation(
        fs(" 1 1 0 0 \n"
           "-1 1 0 0 \n"
           " 0 0 2 0 \n"
           " 0 0 0 1").inv())

    string = "O->(0,0,0) \n" \
             "then\n" \
             "a' = c \n" \
             "b' = a \n" \
             "c' = b"
    g = fs(string)
    assert g == geo.Transformation(
        fs("0 1 0 0 \n"
           "0 0 1 0 \n"
           "1 0 0 0 \n"
           "0 0 0 1").inv())
    string = "O -> (1/2, 0, 0) \n" \
             "then\n" \
             "a' = a \n" \
             "b' = b \n" \
             "c' = c"
    g = fs(string)
    assert g == geo.Transformation(
        fs("1 0 0 -1/2 \n"
           "0 1 0    0 \n"
           "0 0 1    0 \n"
           "0 0 0    1"))

    string1 = "O -> (1/2, 0, 0) \n" \
              "then\n" \
              "a' = a \n" \
              "b' = b \n" \
              "c' = c"

    string2 = "O -> (0,0,0) \n" \
              "then\n" \
              "a' = b\n" \
              "b' = a\n" \
              "c' = c"
    string = "O -> (1/2, 0, 0) \n" \
             "then\n"\
             "a' = b\n"\
             "b' = a\n"\
             "c' = c"

    g1 = fs(string1)
    g2 = fs(string2)
    g = fs(string)
    assert g == g2 * g1

    string = "O -> (1/2, 0, 0) \n" \
             "then\n" \
             "a' = a + b\n" \
             "b' = b\n" \
             "c' = c"
    print("--------")
    g = fs(string)
    print(g.value)
    assert g**fs("p 1/2 0 0") == fs("p 0 0 0")
    assert g**fs("p 1/2 1/3 0") == fs("p 0 1/3 0")
    assert g**fs("p 0 0 0") == fs("p -1/2 1/2 0")

    assert g * g.inv() == geo.Transformation(nb.Matrix.onematrix(4))

    string = "p0 0 0"
    p = fs(string)
    assert p == geo.Pos(fs("0 \n 0 \n 0 \n 1"))
    string = "P0 0 0"
    p = fs(string)
    assert p == geo.Pos(fs("0 \n 0 \n 0 \n 1"))
    string = "r0 0 0"
    p = fs(string)
    assert p == geo.Pos(fs("0 \n 0 \n 0 \n 1"))
    string = "R0 0 0"
    p = fs(string)
    assert p == geo.Pos(fs("0 \n 0 \n 0 \n 1"))
    string = p.__str__()
    assert string == "Pos /  0  \ \n" \
                     "   |   0   |\n" \
                     "    \  0  / "
    p1 = fs(string)
    assert p == p1
    string = "R1/2 1/2 1/2"
    p = fs(string)
    assert p == geo.Pos(fs("1/2 \n 1/2 \n 1/2 \n 1"))

    q = fs("k1 2 3")
    assert q == geo.Rec(fs("1 2 3 0"))
    q = fs("K 1 2 3")
    assert q == geo.Rec(fs("1 2 3 0"))
    q = fs("q 1/2 1/2 0")
    assert q == geo.Rec(fs("1/2 1/2 0 0"))
    q = fs("Q0 0 0")
    assert q == geo.Rec(fs("0 0 0 0"))
    q = fs("Rec <  1  2  3  > ")
    assert q == geo.Rec(fs("1 2 3 0"))
Пример #26
0
def test_Matrix():

    # create and shape

    M1 = nb.Matrix([nb.Row([1, 2, 3]), nb.Row([4, 5, 6])])
    M2 = nb.Matrix([[1, 2, 3], [4, 5, 6]])
    assert M1.shape() == (2, 3)
    assert M2.shape() == (2, 3)

    # Equal

    M1 = nb.Matrix([nb.Row([1, 2, 3]), nb.Row([4, 5, 6])])
    M2 = nb.Matrix([[1, 2, 3], [4, 5, 6]])
    M3 = nb.Matrix([[1, 2, 3], [4, 5.1, 6]])
    M4 = nb.Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
    M5 = nb.Matrix([[1, 2], [4, 5]])
    M6 = nb.Matrix([[1, 2, 3], [4, 5.0, 6]])
    M7 = nb.Matrix([[1, 2, 3], [4, fr.Fraction(5, 1), 6]])
    assert M1 == M2
    assert (M1 == M3) == False
    assert (M1 == M4) == False
    assert (M1 == M5) == False
    assert (M1 == M6) == False
    assert M1 == M7

    M1 = nb.Matrix([nb.Row([1, 2, 3.0]), nb.Row([4, 5, 6])])
    M2 = nb.Matrix([nb.Row([1, 2, 2.999999999999]), nb.Row([4, 5, 6])])
    print(hash(M1))
    print(hash(M2))
    assert hash(M1) == hash(M2)

    e = uc.ufloat(1.2, 0.1)
    M1 = nb.Matrix([nb.Row([1, 2, e]), nb.Row([4, 5, 6])])
    M2 = nb.Matrix([nb.Row([1, 2, e]), nb.Row([4, 5, 6])])
    M3 = nb.Matrix([nb.Row([1, 2, uc.ufloat(1.2, 0.1)]), nb.Row([4, 5, 6])])
    assert M1 == M2
    assert (M1 == M3) == False
    assert hash(M1) == hash(M2)
    assert (hash(M1) == hash(M3)) == False

    # print

    assert nb.Matrix([[1, 2]]).__str__() == " <  1  2  > "
    assert nb.Matrix([[1, 2], [3, 4]]).__str__() == " /  1  2  \ \n" \
                                                    " \  3  4  / "
    assert nb.Matrix([[1, 2, 3], [4, 5, 6], [7, 800, 9]]).__str__() == \
        " /  1    2  3  \ \n" \
        "|   4    5  6   |\n" \
        " \  7  800  9  / "

    # Addition

    assert nb.Matrix([[1, 2], [3, 4]]) + nb.Matrix([[5, 6], [7, 8]]) == \
        nb.Matrix([[6, 8], [10, 12]])

    assert nb.Matrix([[1, 2], [3, 4], [5, 6]]) + \
           nb.Matrix([[1, 1], [1, 1], [1, 1]]) == \
           nb.Matrix([[2, 3], [4, 5], [6, 7]])

    # Subtraction

    assert nb.Matrix([[1, 2], [3, 4]]) - nb.Matrix([[5, 6], [7, 8]]) == \
        nb.Matrix([[-4, -4], [-4, -4]])

    assert nb.Matrix([[1, 2], [3, 4], [5, 6]]) - \
           nb.Matrix([[1, 1], [1, 1], [1, 1]]) == \
           nb.Matrix([[0, 1], [2, 3], [4, 5]])

    # neg

    M1 = nb.Matrix([[1, 2, 3], [4, 5, 6]])
    assert -M1 == nb.Matrix([[-1, -2, -3], [-4, -5, -6]])

    # Multiplication "Matrix * Matrix"

    M1 = nb.Matrix([[1, 2], [3, 4]])
    M2 = nb.Matrix([[5, 6], [7, 8]])
    assert isinstance(M1 * M2, nb.Matrix)
    assert M1 * M2 == nb.Matrix([[19, 22], [43, 50]])

    M1 = nb.Matrix([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
    M2 = nb.Matrix([[uc.ufloat(1.2, 0.1)], [1], [2]])
    print(M2)
    print(M1*M2)
    assert (M1 * M2).liste[1].liste[0] == M2.liste[1].liste[0]
    assert M1 * M2 == M2


    # Multiplication "Scalar * Matrix"

    M1 = nb.Matrix([[1, 2], [3, 4]])
    assert M1 * nb.Mixed(fr.Fraction(1, 2)) == \
        nb.Matrix([[fr.Fraction(1, 2), 1], [fr.Fraction(3, 2), 2]])
    assert M1 * fr.Fraction(1, 2) == \
        nb.Matrix([[fr.Fraction(1, 2), 1], [fr.Fraction(3, 2), 2]])
    assert nb.Mixed(fr.Fraction(1, 2)) * M1 == \
        nb.Matrix([[fr.Fraction(1, 2), 1], [fr.Fraction(3, 2), 2]])
    assert fr.Fraction(1, 2) * M1 == \
        nb.Matrix([[fr.Fraction(1, 2), 1], [fr.Fraction(3, 2), 2]])

    M = M1 * nb.Mixed(uc.ufloat(1.2, 0.1))
    assert approx(M.liste[0].liste[0].value.n, 1.2)
    assert approx(M.liste[0].liste[0].value.s, 0.1)
    assert approx(M.liste[0].liste[1].value.n, 2.4)
    assert approx(M.liste[0].liste[1].value.s, 0.2)
    assert approx(M.liste[1].liste[0].value.n, 3.6)
    assert approx(M.liste[1].liste[0].value.s, 0.3)
    assert approx(M.liste[1].liste[1].value.n, 4.8)
    assert approx(M.liste[1].liste[1].value.s, 0.4)
    M = M1 * uc.ufloat(1.2, 0.1)
    assert approx(M.liste[0].liste[0].value.n, 1.2)
    assert approx(M.liste[0].liste[0].value.s, 0.1)
    assert approx(M.liste[0].liste[1].value.n, 2.4)
    assert approx(M.liste[0].liste[1].value.s, 0.2)
    assert approx(M.liste[1].liste[0].value.n, 3.6)
    assert approx(M.liste[1].liste[0].value.s, 0.3)
    assert approx(M.liste[1].liste[1].value.n, 4.8)
    assert approx(M.liste[1].liste[1].value.s, 0.4)
    M = nb.Mixed(uc.ufloat(1.2, 0.1)) * M1
    assert approx(M.liste[0].liste[0].value.n, 1.2)
    assert approx(M.liste[0].liste[0].value.s, 0.1)
    assert approx(M.liste[0].liste[1].value.n, 2.4)
    assert approx(M.liste[0].liste[1].value.s, 0.2)
    assert approx(M.liste[1].liste[0].value.n, 3.6)
    assert approx(M.liste[1].liste[0].value.s, 0.3)
    assert approx(M.liste[1].liste[1].value.n, 4.8)
    assert approx(M.liste[1].liste[1].value.s, 0.4)
    M = uc.ufloat(1.2, 0.1) * M1
    assert approx(M.liste[0].liste[0].value.n, 1.2)
    assert approx(M.liste[0].liste[0].value.s, 0.1)
    assert approx(M.liste[0].liste[1].value.n, 2.4)
    assert approx(M.liste[0].liste[1].value.s, 0.2)
    assert approx(M.liste[1].liste[0].value.n, 3.6)
    assert approx(M.liste[1].liste[0].value.s, 0.3)
    assert approx(M.liste[1].liste[1].value.n, 4.8)
    assert approx(M.liste[1].liste[1].value.s, 0.4)

    assert M1 * nb.Mixed(2) == nb.Matrix([[2, 4], [6, 8]])
    assert M1 * 2 == nb.Matrix([[2, 4], [6, 8]])
    assert nb.Mixed(2) * M1 == nb.Matrix([[2, 4], [6, 8]])
    assert 2 * M1 == nb.Matrix([[2, 4], [6, 8]])

    M = M1 * nb.Mixed(2.5)
    assert approx(M.liste[0].liste[0].value, 2.5)
    assert approx(M.liste[0].liste[1].value, 5.0)
    assert approx(M.liste[1].liste[0].value, 7.5)
    assert approx(M.liste[1].liste[1].value, 10.0)
    M = M1 * 2.5
    assert approx(M.liste[0].liste[0].value, 2.5)
    assert approx(M.liste[0].liste[1].value, 5.0)
    assert approx(M.liste[1].liste[0].value, 7.5)
    assert approx(M.liste[1].liste[1].value, 10.0)
    M = nb.Mixed(2.5) * M1
    assert approx(M.liste[0].liste[0].value, 2.5)
    assert approx(M.liste[0].liste[1].value, 5.0)
    assert approx(M.liste[1].liste[0].value, 7.5)
    assert approx(M.liste[1].liste[1].value, 10.0)
    M = 2.5 * M1
    assert approx(M.liste[0].liste[0].value, 2.5)
    assert approx(M.liste[0].liste[1].value, 5.0)
    assert approx(M.liste[1].liste[0].value, 7.5)
    assert approx(M.liste[1].liste[1].value, 10.0)

    # onematrix

    assert nb.Matrix.onematrix(2) == nb.Matrix([[1, 0], [0, 1]])
    assert nb.Matrix.onematrix(3) == nb.Matrix([[1, 0, 0], [0, 1, 0], [0, 0, 1]])

    # block

    M = nb.Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
    assert M.block(0, 2, 0, 2) == nb.Matrix([[1, 2], [4, 5]])
    assert M.block(1, 2, 0, 3) == nb.Matrix([[4, 5, 6]])
    M = nb.Matrix([[1, 2, 3, 4]])
    assert M.block(0, 1, 0, 3) == nb.Matrix([[1, 2, 3]])

    # swap_rows

    M = nb.Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
    assert M.swap_rows(0, 1) == nb.Matrix([[4, 5, 6], [1, 2, 3], [7, 8, 9]])

    # vglue

    M1 = nb.Matrix([[1, 2, 3]])
    M2 = nb.Matrix([[4, 5, 6]])
    assert nb.Matrix.vglue(M1, M2) == nb.Matrix([[1, 2, 3], [4, 5, 6]])

    # subtract_x_times_rowj_from_rowi

    M = nb.Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
    assert M.subtract_x_times_rowj_from_rowi(2, 2, 0) == \
        nb.Matrix([[1, 2, 3], [4, 5, 6], [5, 4, 3]])

    # inv

    assert nb.Matrix([[2, 3],
                      [4, 5]]).inv() == \
        nb.Matrix([[-fr.Fraction(5, 2), fr.Fraction(3, 2)],
                   [2,                -1]])
    assert nb.Matrix([[0, 1,  1],
                      [1, 0,  0],
                      [0, 0, -1]]).inv() == \
           nb.Matrix([[0, 1,  0],
                      [1, 0,  1],
                      [0, 0, -1]])

    assert nb.Matrix([[0, 0, 1, 0],
                      [1, 0, 0, 0],
                      [0, 1, 0, 0],
                      [0, 0, 0, 1]]).inv() == \
           nb.Matrix([[0, 1, 0, 0],
                      [0, 0, 1, 0],
                      [1, 0, 0, 0],
                      [0, 0, 0, 1]])

    # transpose

    assert nb.Matrix([[1, 2, 3], [4, 5, 6]]).transpose() == \
           nb.Matrix([[1, 4], [2, 5], [3, 6]])

    # delete_ith_row_and_first_column
    M = nb.Matrix([[1, 2, 3, 4],
                   [5, 6, 7, 8],
                   [9, 10, 11, 12]])
    assert M.delete_ith_row_and_first_column(1) == nb.Matrix([[2, 3, 4],
                                                              [10, 11, 12]])

    # det
    M = nb.Matrix([[3]])
    assert M.det() == 3
    M = nb.Matrix([[1, 2],
                   [3, 4]])
    assert M.det() == -2
    M = nb.Matrix([[1, 2, 3],
                   [4, 5, 6],
                   [7, 8, 9]])
    assert M.det() == 0
    M = nb.Matrix([[1, 2, 3, 4],
                   [5, 6, 7, 8],
                   [9, 8, 11, 12],
                   [13, 14, 15, 17]])
    assert M.det() == -16

    # delete_translation

    M = nb.Matrix([[1,  2,  3,  4],
                   [5,  6,  7,  8],
                   [9, 10, 11, 12],
                   [0,  0,  0,  1]])

    assert M.delete_translation() == nb.Matrix([[1,  2,  3, 0],
                                              [5,  6,  7, 0],
                                              [9, 10, 11, 0],
                                              [0,  0,  0, 1]])
Пример #27
0
def test_Pointgroup():
    pg = geo.Pointgroup([geo.Symmetry(nb.Matrix([[1, 0, 0, 0],
                                                 [0, 1, 0, 0],
                                                 [0, 0, 1, 0],
                                                 [0, 0, 0, 1]])),
                         geo.Symmetry(nb.Matrix([[-1, 0, 0, 0],
                                                 [0, -1, 0, 0],
                                                 [0, 0, -1, 0],
                                                 [0, 0, 0, 1]]))])

    assert isinstance(pg, geo.Pointgroup)
    assert pg.__str__() == "Pointgroup\n" \
                           "==========\n" \
                           "x,y,z\n" \
                           "-x,-y,-z\n"

    assert pg.is_really_a_pointgroup()

    pg1 = geo.Pointgroup([geo.Symmetry(nb.Matrix([[1, 0, 0, 0],
                                                 [0, 1, 0, 0],
                                                 [0, 0, 1, 0],
                                                 [0, 0, 0, 1]])),
                         geo.Symmetry(nb.Matrix([[-1, 0, 0, 0],
                                                 [0, -1, 0, 0],
                                                 [0, 0, -1, 0],
                                                 [0, 0, 0, 1]]))])

    pg2 = geo.Pointgroup([geo.Symmetry(nb.Matrix([[-1, 0, 0, 0],
                                                  [0, -1, 0, 0],
                                                  [0, 0, -1, 0],
                                                  [0, 0, 0, 1]]))])

    assert pg == pg1
    assert not (pg == pg2)
    assert not (pg == 2)

    assert not pg2.is_really_a_pointgroup()

    G = geo.Pointgroup([geo.Symmetry(nb.Matrix([[1, 0, 0, 0],
                                                [0, 1, 0, 0],
                                                [0, 0, 1, 0],
                                                [0, 0, 0, 1]])),
                        geo.Symmetry(nb.Matrix([[-1, 0, 0, 0],
                                                [0, -1, 0, 0],
                                                [0, 0, 1, 0],
                                                [0, 0, 0, 1]])),
                        geo.Symmetry(nb.Matrix([[-1, 0, 0, 0],
                                                [0, 1, 0, 0],
                                                [0, 0, -1, 0],
                                                [0, 0, 0, 1]])),
                        geo.Symmetry(nb.Matrix([[1, 0, 0, 0],
                                                [0, -1, 0, 0],
                                                [0, 0, -1, 0],
                                                [0, 0, 0, 1]])),
                        geo.Symmetry(nb.Matrix([[0, 1, 0, 0],
                                                [1, 0, 0, 0],
                                                [0, 0, -1, 0],
                                                [0, 0, 0, 1]])),
                        geo.Symmetry(nb.Matrix([[0, -1, 0, 0],
                                                [-1, 0, 0, 0],
                                                [0, 0, -1, 0],
                                                [0, 0, 0, 1]])),
                        geo.Symmetry(nb.Matrix([[0, -1, 0, 0],
                                                [1, 0, 0, 0],
                                                [0, 0, 1, 0],
                                                [0, 0, 0, 1]])),
                        geo.Symmetry(nb.Matrix([[0, 1, 0, 0],
                                                [-1, 0, 0, 0],
                                                [0, 0, 1, 0],
                                                [0, 0, 0, 1]])),
                        geo.Symmetry(nb.Matrix([[-1, 0, 0, 0],
                                                [0, -1, 0, 0],
                                                [0, 0, -1, 0],
                                                [0, 0, 0, 1]])),
                        geo.Symmetry(nb.Matrix([[1, 0, 0, 0],
                                                [0, 1, 0, 0],
                                                [0, 0, -1, 0],
                                                [0, 0, 0, 1]])),
                        geo.Symmetry(nb.Matrix([[1, 0, 0, 0],
                                                [0, -1, 0, 0],
                                                [0, 0, 1, 0],
                                                [0, 0, 0, 1]])),
                        geo.Symmetry(nb.Matrix([[-1, 0, 0, 0],
                                                [0, 1, 0, 0],
                                                [0, 0, 1, 0],
                                                [0, 0, 0, 1]])),
                        geo.Symmetry(nb.Matrix([[0, -1, 0, 0],
                                                [-1, 0, 0, 0],
                                                [0, 0, 1, 0],
                                                [0, 0, 0, 1]])),
                        geo.Symmetry(nb.Matrix([[0, 1, 0, 0],
                                                [1, 0, 0, 0],
                                                [0, 0, 1, 0],
                                                [0, 0, 0, 1]])),
                        geo.Symmetry(nb.Matrix([[0, 1, 0, 0],
                                                [-1, 0, 0, 0],
                                                [0, 0, -1, 0],
                                                [0, 0, 0, 1]])),
                        geo.Symmetry(nb.Matrix([[0, -1, 0, 0],
                                                [1, 0, 0, 0],
                                                [0, 0, -1, 0],
                                                [0, 0, 0, 1]]))])

    assert G.is_really_a_pointgroup()

    g = G.liste
    # For this test, the order of the symmetry elements must not be
    # completely arbitrary, because there are several valid possibilities
    # to sort the symmetryelements.
    G1 = geo.Pointgroup([g[6], g[7], g[0], g[14], g[15], g[9], g[10], g[11],
                         g[12], g[13], g[8], g[1], g[2], g[3], g[4], g[5]])

    print(G1.sort())
    print(G)
    assert G1.sort() == G

    H = geo.Pointgroup([geo.Symmetry(nb.Matrix([[1, 0, 0, 0],
                                                [0, 1, 0, 0],
                                                [0, 0, 1, 0],
                                                [0, 0, 0, 1]])),
                        geo.Symmetry(nb.Matrix([[-1, 0, 0, 0],
                                                [0, -1, 0, 0],
                                                [0, 0, 1, 0],
                                                [0, 0, 0, 1]])),
                        geo.Symmetry(nb.Matrix([[0, -1, 0, 0],
                                                [1, 0, 0, 0],
                                                [0, 0, 1, 0],
                                                [0, 0, 0, 1]])),
                        geo.Symmetry(nb.Matrix([[0, 1, 0, 0],
                                                [-1, 0, 0, 0],
                                                [0, 0, 1, 0],
                                                [0, 0, 0, 1]])),
                        geo.Symmetry(nb.Matrix([[-1, 0, 0, 0],
                                                [0, -1, 0, 0],
                                                [0, 0, -1, 0],
                                                [0, 0, 0, 1]])),
                        geo.Symmetry(nb.Matrix([[1, 0, 0, 0],
                                                [0, 1, 0, 0],
                                                [0, 0, -1, 0],
                                                [0, 0, 0, 1]])),
                        geo.Symmetry(nb.Matrix([[0, 1, 0, 0],
                                                [-1, 0, 0, 0],
                                                [0, 0, -1, 0],
                                                [0, 0, 0, 1]])),
                        geo.Symmetry(nb.Matrix([[0, -1, 0, 0],
                                                [1, 0, 0, 0],
                                                [0, 0, -1, 0],
                                                [0, 0, 0, 1]]))])

    assert H.is_really_a_pointgroup()
    assert G1.coset_decomposition_with_respect_to(H) \
        == "x,y,z\n" \
           "-x,y,-z\n"
Пример #28
0
def test_Transformation():
    t1 = geo.Transformation(nb.Matrix([[0, 1, 0, 0],
                                       [1, 0, 0, 0],
                                       [0, 0, 1, 0],
                                       [0, 0, 0, 1]]))

    assert t1.__str__() == "Transformation O -> (0, 0, 0)\n" \
                           "               then\n" \
                           "               a' = b\n" \
                           "               b' = a\n" \
                           "               c' = c"

    t2 = geo.Transformation(nb.Matrix([[1, 0, 0, fr.Fraction(1, 2)],
                                       [0, 1, 0, fr.Fraction(1, 4)],
                                       [0, 0, 1, fr.Fraction(1, 3)],
                                       [0, 0, 0, 1]]))

    assert t2.__str__() == "Transformation O -> (-1/2, -1/4, -1/3)\n" \
                           "               then\n" \
                           "               a' = a\n" \
                           "               b' = b\n" \
                           "               c' = c"

    assert isinstance(t2.inv(), geo.Transformation)
    assert t2 * t2.inv() == geo.Transformation(nb.Matrix.onematrix(4))

    t3 = geo.Transformation(nb.Matrix([[1.0, 0, 0, 0],
                                       [0, 1, 0, 0],
                                       [0, 0, 1, 0],
                                       [0, 0, 0, 1]]))
    print(t3.value)
    assert t3.__str__() == "Transformation O -> (0, 0, 0)\n" \
                           "               then\n" \
                           "               a' = 1.0a\n" \
                           "               b' = b\n" \
                           "               c' = c"

    q1 = geo.Rec(nb.Matrix([[1, 0, 0, 0]]))
    t = geo.Transformation(nb.Matrix([[0, 1, 0, 0],
                                      [0, 0, 1, 0],
                                      [1, 0, 0, 0],
                                      [0, 0, 0, 1]]))
    assert isinstance(t ** q1, geo.Rec)
    assert (t ** q1) == geo.Rec(nb.Matrix([[0, 0, 1, 0]]))

    q1 = geo.Rec(nb.Matrix([[1, 0, 0, 0]]))
    t = geo.Transformation(nb.Matrix([[0, 1, 0, 0.3],
                                      [0, 0, 1, 0.7],
                                      [1, 0, 0, 100],
                                      [0, 0, 0, 1]]))
    assert isinstance(t ** q1, geo.Rec)
    assert (t ** q1) == geo.Rec(nb.Matrix([[0, 0, 1, 0]]))

    q1 = geo.Rec(nb.Matrix([[0, 0, 1, 0]]))
    t = geo.Transformation(nb.Matrix([[1, 0, 0, 0],
                                      [0, 1, 0, 0],
                                      [0, 0, 2, 0],
                                      [0, 0, 0, 1]]))
    assert t.__str__()  == "Transformation O -> (0, 0, 0)\n" \
                           "               then\n" \
                           "               a' = a\n" \
                           "               b' = b\n" \
                           "               c' = 1/2c"
    assert isinstance(t ** q1, geo.Rec)
    assert (t ** q1) == \
        geo.Rec(nb.Matrix([[0, 0, nb.Mixed(fr.Fraction(1, 2)), 0]]))

    q1 = geo.Rec(nb.Matrix([[0, 0, 1, 0]]))
    t = geo.Transformation(nb.Matrix([[1, 0, 0, 0],
                                      [0, 0, 2, 0],
                                      [0, -1, 0, 0],
                                      [0, 0, 0, 1]]))
    assert t.__str__()  == "Transformation O -> (0, 0, 0)\n" \
                           "               then\n" \
                           "               a' = a\n" \
                           "               b' = 1/2c\n" \
                           "               c' = -b"
    assert isinstance(t ** q1, geo.Rec)
    print(t)
    assert (t ** q1) == \
        geo.Rec(nb.Matrix([[0, nb.Mixed(fr.Fraction(1, 2)), 0, 0]]))
Пример #29
0
def transformationfromstr(string):
    if len(string.split("then")) > 1:
        # The string respresents not an elementar transformation,
        # i.e. the string represents a transformation which is
        # a composition of elementar transformations.
        result = geo.Transformation(nb.Matrix.onematrix(4))
        for word in string.split("\nthen\n"):
            result = transformationfromstr(word) * result
        return result
    else:
        # The string represents an elementar transformation,
        # i.e. either a pure translation of the origin or a
        # pure change of the axes.
        if ('O' in string) or ("->" in string):
            # The string represents a pure translation of the origin.
            words = string.split("->")
            word = words[1]
            word = word.replace('\n', ' ')
            word = word.replace('(', ' ').replace(')', ' ').replace(',', ' ')
            threenumbers = fromstr(word)
            matrix = nb.Matrix([[1, 0, 0, -threenumbers.liste[0].liste[0]],
                                [0, 1, 0, -threenumbers.liste[0].liste[1]],
                                [0, 0, 1, -threenumbers.liste[0].liste[2]],
                                [0, 0, 0, 1]])
            return geo.Transformation(matrix)
        elif ('a' in string) or ('b' in string) or ('c' in string):
            # The string represents a pure change of the axes
            lines = string.split('\n')
            assert len(lines) == 3, \
                "The following string looks like a Transformation, " \
                "but it has not exactly three lines: %s" % (string)
            liste = []
            i = 0
            for line in lines:
                if len(line.split(' ')) > 0:
                    i += 1
                    words = line.split(' ')
                    assert  (    ((i == 1) and (words[0] == "a'"))
                                 or ((i == 2) and (words[0] == "b'"))
                                 or ((i == 3) and (words[0] == "c'"))  )\
                        and (words[1] == '='), \
                        "The Transformation must have the following form: \n" \
                        "a' = ... \n" \
                        "b' = ... \n" \
                        "c' = ... \n" \
                        "in this Order!"
                    words = line.split('=')
                    row = nb.Row(str2linearterm(words[1], ['a', 'b', 'c']))
                    liste.append(row)
            liste.append(
                nb.Row(
                    [fromstr("0"),
                     fromstr("0"),
                     fromstr("0"),
                     fromstr("1")]))
            m = nb.Matrix(liste)
            matrix = nb.Matrix([
                nb.Row([
                    m.liste[0].liste[0], m.liste[1].liste[0],
                    m.liste[2].liste[0], 0
                ]),
                nb.Row([
                    m.liste[0].liste[1], m.liste[1].liste[1],
                    m.liste[2].liste[1], 0
                ]),
                nb.Row([
                    m.liste[0].liste[2], m.liste[1].liste[2],
                    m.liste[2].liste[2], 0
                ]),
                nb.Row([0, 0, 0, 1])
            ])

        return geo.Transformation(matrix.inv())
Пример #30
0
def test_Metric():
    M = nb.Matrix([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]])
    metric = geo.Metric(M)
    t = metric.schmidttransformation

    assert t**geo.canonical_e0 == geo.Dif(nb.Matrix([[1], [0], [0], [0]]))
    assert t**geo.canonical_e1 == geo.Dif(nb.Matrix([[0], [1], [0], [0]]))
    assert t**geo.canonical_e2 == geo.Dif(nb.Matrix([[0], [0], [1], [0]]))

    metric = geo.Cellparameters(1, 1, 1, 90, 90, 45).to_Metric()
    t = metric.schmidttransformation
    assert t**geo.canonical_e0 == geo.Dif(nb.Matrix([[1], [0], [0], [0]]))
    e1 = t**geo.canonical_e1
    assert abs(e1.x() - 0.70710678).value < 0.000001
    assert abs(e1.y() - 0.70710678).value < 0.000001
    assert abs(e1.z() - 0).value < 0.000001
    e2 = t**geo.canonical_e2
    assert abs(e2.x() - 0).value < 0.000001
    assert abs(e2.y() - 0).value < 0.000001
    assert abs(e2.z() - 1).value < 0.000001

    t = metric.schmidttransformation.inv()

    M = nb.Matrix([[9, 0, 0, 0], [0, 4, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]])
    metric = geo.Metric(M)
    assert metric.__str__() == "Metric /  9  0  0  0  \ \n" \
                               "      |   0  4  0  0   |\n" \
                               "      |   0  0  1  0   |\n" \
                               "       \  0  0  0  1  / "
    o = geo.Pos(nb.Matrix([[0], [0], [0], [1]]))
    p1 = geo.Pos(nb.Matrix([[1], [0], [0], [1]]))
    p2 = geo.Pos(nb.Matrix([[0], [1], [0], [1]]))
    q1 = geo.Rec(nb.Matrix([[1, 0, 0, 0]]))
    q2 = geo.Rec(nb.Matrix([[0, 1, 0, 0]]))
    q3 = geo.Rec(nb.Matrix([[1, 1, 0, 0]]))
    assert metric.dot(p1 - o, p1 - o).__str__() == "9"
    assert metric.dot(p1 - o, p2 - o).__str__() == "0"
    assert metric.dot(q1, q1).__str__() == "1/9"
    assert metric.dot(q2, q2).__str__() == "1/4"
    assert metric.dot(q3, q3).__str__() == "13/36"
    assert metric.length(p1 - o) == 3
    assert abs(float(
        (metric.angle(p1 - o, p2 - o) - nb.deg2rad(90)))) < 0.00001
    assert metric.length(q1).__str__() == "1/3"
    assert abs(float((metric.angle(q1, q2) - nb.deg2rad(90)))) < 0.00001
    assert metric.angle(p1 - o, p1 - o).__str__() == "0.0"
    assert metric.angle(q1, q1).__str__() == "0.0"

    metric = geo.Cellparameters(4.15, 4.15, 28.64, 90, 90, 120).to_Metric()

    M = nb.Matrix([[9, 0, 0, 0], [0, 4, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]])
    metric = geo.Metric(M)

    cell = metric.to_Cellparameters()
    assert cell.__str__() == \
        geo.Cellparameters(3, 2, 1, 90.0, 90.0, 90.0).__str__()

    t = geo.Transformation(
        nb.Matrix([[0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0.5], [0, 0, 0, 1]]))

    assert t**metric == geo.Metric(
        nb.Matrix([[4, 0, 0, 0], [0, 9, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]))