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)
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]]))
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)
def recfromstr(string): string = string.replace('\n', ' ') string = re.sub("\\\\| \/|\/ |[|<>]", " ", string) # string = string.replace('\\', ' ') # string = string.replace('/ ', ' ') # string = string.replace(' /', ' ') # string = string.replace('|', ' ') # string = string.replace('<', ' ') # string = string.replace('>', ' ') string = removeletters(string) words = string.split() string = ' '.join(words) string += " 0" return geo.Rec(matrixfromstr(string))
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
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
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]]))
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"))
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]]))
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