コード例 #1
0
def with_lagrange():
    ## This function tests the fact that non real solutions are discarded.

    echo_function("with_lagrange")

    x = var('x')
    mx = -6
    Mx = 2.7
    intersections = [
        Point(-5.5, 1.5),
        Point(-3.5, 1),
        Point(-1, 1),
        Point(1.5, -1)
    ]
    pts1 = intersections[:]
    pts1.extend([Point(-4.5, -1), Point(-2, 3), Point(1, -1), Point(2.5, 0)])
    lag1 = LagrangePolynomial(pts1)

    pts2 = intersections[:]
    pts2.extend([Point(-4.5, 2.5), Point(-2, 0), Point(0, 1.5), Point(2, -2)])
    lag2 = LagrangePolynomial(pts2)

    ans = []
    ans.append(Point(-6.381638163816381, -1.821966693100714))
    ans.append(Point(-11 / 2, 3 / 2))
    ans.append(Point(-7 / 2, 1))
    ans.append(Point(-1, 1))
    ans.append(Point(3 / 2, -1))

    pts = Intersection(lag1, lag2)

    for PP in zip(ans, pts):
        assert_equal(PP[0], PP[1])
コード例 #2
0
def lines_and_functions():
    """
    Test the intersection function
    """
    echo_function("lines_and_functions")

    x = var('x')
    fun = phyFunction(x**2 - 5 * x + 6)
    droite = phyFunction(2)
    pts = Intersection(fun, droite)

    echo_single_test("Function against horizontal line")
    assert_equal(pts[0], Point(1, 2))
    assert_equal(pts[1], Point(4, 2))

    echo_single_test("Two functions (sine and cosine)")
    f = phyFunction(sin(x))
    g = phyFunction(cos(x))
    pts = Intersection(f, g, -2 * pi, 2 * pi, numerical=True)

    # due to the default epsilon in `assert_almost_equal`,
    # in fact we do not test these points with the whole given precision.
    ans = []
    ans.append(Point(-5.497787143782138, 0.707106781186548))
    ans.append(Point(-2.3561944901923466, -0.707106781186546))
    ans.append(Point(0.7853981633974484, 0.707106781186548))
    ans.append(Point(3.926990816987241, -0.707106781186547))

    for t in zip(pts, ans):
        assert_almost_equal(t[0], t[1])
コード例 #3
0
def orthogonal_decompostion():
    echo_function("orthogonal_decomposition")
    v = Vector(2, 3)
    perp, parall = v.decomposition(Segment(Point(0, 0), Point(0, 1)))

    echo_single_test("In the Y direction")

    ans_parall = AffineVector(Point(0, 0), Point(0, 3))
    ans_perp = AffineVector(Point(0, 0), Point(2, 0))

    assert_true(parall + perp == v)
    assert_equal(parall, ans_parall)
    assert_equal(perp, ans_perp)

    P = Point(0, 2)
    seg = Segment(P, P.get_polar_point(2, -130))

    Q = seg.get_point_proportion(0.5)
    v = AffineVector(Q, Point(Q.x - 1, Q.y))
    perp, paral = v.decomposition(seg)

    echo_single_test("130 degree : sum")
    assert_equal(perp + paral, v)

    echo_single_test("130 degree : orthogonal")
    assert_true(perp.segment.is_almost_orthogonal(seg, epsilon=0.0001))

    ip = perp.inner_product(paral)
    echo_single_test("130 degree : inner product 1")
    assert_equal(perp.inner_product(paral), 0)
    echo_single_test("130 degree : inner product 2")
    assert_equal(paral.inner_product(perp), 0)
コード例 #4
0
def test_text_parenthesis():
    echo_function("test_text_parenthesis")
    text = "\subfigure[more points (5000)]{%"

    fd = TestRecall.TikzDecomposition(text)
    assert_true(fd.points_list == [])
    assert_true(len(fd.texts_list) == 2)
    assert_equal(fd.texts_list[0], "\subfigure[more points ")
    assert_equal(fd.texts_list[1], "5000)]{%")
コード例 #5
0
def test_zero():
    echo_function("test_zero")
    text = "\draw [e=d] plot [s,t=1] coordinates {(1.00,0)(1.05,0.0320))};"

    fd = TestRecall.TikzDecomposition(text)
    fdp = [str(p) for p in fd.points_list]

    ans_points = ["(1.0,0.0)", "(1.05,0.032)"]
    assert_equal(fdp, ans_points)
コード例 #6
0
def test_almost_equal():
    echo_function("test_almost_equal")
    s = Segment(Point(1, 1), Point(2, 2))
    v = s.get_normal_vector()
    assert_equal(v.I, Point(1.5, 1.5))
    assert_almost_equal(v.length, 1, epsilon=0.001)
    assert_almost_equal(v.F,
                        Point(1 / 2 * sqrt(2) + 1.5, -1 / 2 * sqrt(2) + 1.5),
                        epsilon=0.001)
コード例 #7
0
def test_vertical_horizontal():
    echo_function("test_vertical_horizontal")

    A = Point(1.50000000000000 * cos(0.111111111111111 * pi),
              -1.50000000000000 * sin(0.111111111111111 * pi))
    B = Point(3.00000000000000 * cos(0.111111111111111 * pi),
              -3.00000000000000 * sin(0.111111111111111 * pi))
    seg = Segment(A, B)
    assert_equal(seg.is_vertical, False)
    assert_equal(seg.is_horizontal, False)
コード例 #8
0
def test_math():
    echo_function("test_math")
    text = "#\setlength{\lengthOf}{\total{$f(x)$}}% (1,1)"

    fd = TestRecall.TikzDecomposition(text)
    fdp = [str(p) for p in fd.points_list]

    ans_texts = ['#\\setlength{\\lengthOf}{\total{$f', 'x)$}}% ', ')']
    ans_points = ["(1.0,1.0)"]

    assert_equal(fdp, ans_points)
    assert_equal(fd.texts_list, ans_texts)
コード例 #9
0
def with_box():
    # The "demonstration picture" BOVAooIlzgFQpG serves to test
    # how it works visually.
    echo_function("with_box")
    from phystricks.src.Utilities import point_to_box_intersection

    echo_single_test("one box ...")
    P = Point(1, 1)
    box = BoundingBox(xmin=2, ymin=3, xmax=6, ymax=4)
    ans = [Point(17 / 5, 3), Point(23 / 5, 4)]
    for t in zip(ans, point_to_box_intersection(P, box)):
        assert_equal(t[0], t[1])

    echo_single_test("an other box ...")
    P = Point(1, 1)
    box = BoundingBox(xmin=1, ymin=3, xmax=4, ymax=4)
    ans = [Point(11 / 5, 3), Point(14 / 5, 4)]
    for t in zip(ans, point_to_box_intersection(P, box)):
        assert_equal(t[0], t[1])

    echo_single_test("an other box ...")
    P = Point(0, 0)
    box = BoundingBox(xmin=cos(pi + 0.109334472936971) - 0.5,
                      xmax=cos(pi + 0.109334472936971) + 0.5,
                      ymin=sin(pi + 0.109334472936971) - 0.5,
                      ymax=sin(pi + 0.109334472936971) + 0.5)
    ans = [Point(11 / 5, 3), Point(14 / 5, 4)]

    ans=[Point(-22625191/45797299,-116397308741499/2146337772042166),\
            Point(-11397639/7628794,-58636168225371/357531318982996)]
    for t in zip(ans, point_to_box_intersection(P, box)):
        assert_equal(t[0], t[1])

    echo_single_test("point->center parallel to a edge")
    P = Point(0, 0)
    box = BoundingBox(xmin=-2.0719775,
                      xmax=-0.8437425000000001,
                      ymin=-0.1148125,
                      ymax=0.1148125)

    ans = [Point(-337497 / 400000, 0), Point(-828791 / 400000, 0)]
    for t in zip(ans, point_to_box_intersection(P, box)):
        assert_equal(t[0], t[1])

    echo_single_test("an other box (corner) ...")
    P = Point(0, 1)
    box = BoundingBox(xmin=-1, ymin=-3, xmax=-0.5, ymax=-1)
    ans = [Point(-0.5, -1), Point(-1, -3)]
    for t in zip(ans, point_to_box_intersection(P, box)):
        assert_equal(t[0], t[1])
コード例 #10
0
def test_angle_mark():
    # This test is replaced by the picture AEUYooWXYBuI.

    #echo_function("test_angle_mark")

    with SilentOutput() :
        angle,pspict = create_example(angleI=160,angleF=223,text="\( \int_A40mmmm\)",name="Six",dist=None)

    mark=angle.added_objects[pspict][0]
    code=mark.tikz_code()

    # this is not the correct final answer because
    # it depends on the position in the picture (size of the box)
    # I'm testing here if something in the processus is indeterministic.
    ans="\draw (-0.6170939622,-0.1216103571) node {\( \int_A40mmmm\)};"

    assert_equal(code,ans)
コード例 #11
0
def test_visual_length():
    echo_function("test_visual_length")

    with SilentOutput():
        pspict, fig = SinglePicture("ZZTHooTeGyMT")

    v = AffineVector(Point(0, 0), Point(0, sin(0.5 * pi)))
    w = visual_length(v, 0.1, pspict=pspict)
    ans = AffineVector(Point(0, 0), Point(0, 0.1))

    echo_single_test("positive")
    assert_equal(w, ans)

    echo_single_test("negative")
    v = AffineVector(Point(0, 0), Point(0, -sin(0.5 * pi)))
    w = visual_length(v, 0.1, pspict=pspict)
    ans = AffineVector(Point(0, 0), Point(0, -0.1))
    assert_equal(w, ans)
コード例 #12
0
def point_translation():
    echo_function("translation")
    v = Vector(2, 1)
    P = Point(-1, -1)
    assert_equal(P.translate(v), Point(1, 0))

    w = AffineVector(Point(1, 1), Point(2, 3))
    assert_equal(P.translate(w), Point(0, 1))

    assert_equal(P.translate(10, -9), Point(9, -10))

    x, y = var('x,y')
    P = Point(x, y)
    assert_equal(P.translate(Vector(-P)), Point(0, 0))
コード例 #13
0
def segment_translation():
    echo_function("segment_translation")
    segment = Segment(Point(-1, 3), Point(pi, 0))

    # One tuple and 3 ways to describe the same vector.
    t = (1, 7)
    v1 = AffineVector(Point(0, 0), Point(1, 7))
    v2 = AffineVector(Point(5, 6), Point(6, 13))
    v3 = Vector(1, 7)

    s1 = segment.translate(1, 7)
    s2 = segment.translate(t)
    s3 = segment.translate(v1)
    s4 = segment.translate(v2)
    s5 = segment.translate(v3)

    ans = Segment(Point(0, 10), Point(pi + 1, 7))
    assert_equal(s1, ans)
    assert_equal(s2, ans)
    assert_equal(s3, ans)
    assert_equal(s4, ans)
    assert_equal(s5, ans)
コード例 #14
0
def testEnsureUnicode():
    echo_function("testEnsureUnicode")
    from phystricks.src.NoMathUtilities import ensure_unicode
    from phystricks.src.NoMathUtilities import ensure_str

    u1 = u"éà"
    s1 = "éà"

    uni_u1 = ensure_unicode(u1)
    str_u1 = ensure_str(u1)

    assert_equal(uni_u1, u1)
    assert_equal(str_u1, s1)

    s2 = "éàù"
    double_s2 = ensure_str(ensure_unicode(s2))
    assert_equal(double_s2, s2)

    u2 = u"éàù"
    double_u2 = ensure_unicode(ensure_str(u2))
    assert_equal(double_u2, u2)
コード例 #15
0
def test_vector_equality():
    echo_function("test_vector_equality")

    A = Point(1, 1)
    B = Point(2, -3)

    vv = AffineVector(A, B)
    ww = AffineVector(A, B)

    echo_single_test("Two trivial equalities")
    assert_equal(vv, vv)
    assert_equal(ww, ww)

    echo_single_test("One less trivial equalities")
    assert_equal(vv, ww)
コード例 #16
0
def test_second_derivative_vector():
    echo_function("test_second_derivative_vector")

    F = ParametricCurve(x, x**3)

    echo_single_test("normalize=true")
    v = F.get_second_derivative_vector(0, normalize=True)
    ans = AffineVector(Point(0, 0), Point(0, 0))
    assert_equal(v, ans)

    echo_single_test("normalize=false")
    v = F.get_second_derivative_vector(0, normalize=False)
    ans = AffineVector(Point(0, 0), Point(0, 0))
    assert_equal(v, ans)

    echo_single_test("On an other point")
    v = F.get_second_derivative_vector(1)
    ans = AffineVector(Point(1, 1), Point(1, 2))
    assert_equal(v, ans)
コード例 #17
0
def test_split():
    echo_function("test_split")
    text = "aaaZbbZcc"

    echo_single_test("only internal")
    it = TestRecall.split_for_positions(text, [3, 6])
    lit = list(it)
    ans = ['aaa', 'bb', 'cc']
    assert_equal(lit, ans)

    echo_single_test("left border")
    text = "stuZbbZcc"
    it = TestRecall.split_for_positions(text, [0, 3, 6])
    lit = list(it)
    ans = ['', 'stu', 'bb', 'cc']
    assert_equal(lit, ans)

    echo_single_test("right border")
    text = "stuZbbZccZ"
    it = TestRecall.split_for_positions(text, [0, 3, 6, 9])
    lit = list(it)
    ans = ['', 'stu', 'bb', 'cc', '']
    assert_equal(lit, ans)
コード例 #18
0
def test_constructors():

    echo_single_test("Usual constructor")
    seg = Segment(Point(0, 0), Point(2, 10))
    assert_equal(seg.I, Point(0, 0))
    assert_equal(seg.F, Point(2, 10))

    echo_single_test("Construct with a vector")
    seg = Segment(Point(-3, 4), vector=Vector(1, 2))
    assert_equal(seg.I, Point(-3, 4))
    assert_equal(seg.F, Point(-2, 6))

    echo_single_test("Construct with an affine vector")
    v = AffineVector(Point(1, 2), Point(-2, 5))
    seg = Segment(Point(-3, 4), vector=v)
    assert_equal(seg.I, Point(-3, 4))
    assert_equal(seg.F, Point(-6, 7))
コード例 #19
0
def projection():
    echo_function("projection")
    s1 = Segment(Point(0, 0), Point(2, 1))
    assert_equal(Point(3, -1).projection(s1), Point(2, 1))
    assert_equal(Point(5, 0).projection(s1), Point(4, 2))
    assert_equal(Point(5, 0).projection(Vector(2, 1)), Point(4, 2))
コード例 #20
0
def testAngleMeasure():
    echo_function("testAngleMeasure")

    comparison()

    alpha=AngleMeasure(value_degree=360)
    assert_equal(alpha.__repr__(),
                    "AngleMeasure, degree=360.000000000000,radian=2*pi")

    alpha=AngleMeasure(value_degree=30)
    assert_equal(cos(alpha.radian),1/2*sqrt(3))


    alpha=AngleMeasure(value_degree=180)
    beta=AngleMeasure(alpha)
    assert_equal(beta.degree,180)

    alpha=AngleMeasure(value_degree=-(3.47548077273962e-14)/pi + 360)
    assert_equal(alpha.degree,360)
    assert_equal(alpha.radian,2*pi)

    alpha=AngleMeasure(value_degree=-30)
    assert_equal(alpha.positive().degree,330)


    alpha=AngleMeasure(value_degree=45)
    beta=AngleMeasure(value_radian=pi/3)
    assert_equal(alpha.degree,45)
    assert_equal(alpha.radian,1/4*pi)
    assert_equal(beta.degree,60)
    assert_equal(beta.radian,1/3*pi)

    a_sum_b = alpha+beta
    assert_equal(a_sum_b.degree,105)
    assert_equal(a_sum_b.radian,7/12*pi)
コード例 #21
0
def vector_constructor():
    """
    Test different ways of building a vector.
    """
    echo_function("vector_constructor")
    P = Point(4, 2)
    O = Point(0, 0)
    t = (4, 2)
    v1 = Vector(P)
    v2 = Vector(t)
    v3 = Vector(4, 2)

    assert_equal(v1.I, O)
    assert_equal(v2.I, O)
    assert_equal(v3.I, O)

    assert_equal(v1.F.x, 4)
    assert_equal(v2.F.x, 4)
    assert_equal(v3.F.x, 4)

    assert_equal(v1.F.y, 2)
    assert_equal(v2.F.y, 2)
    assert_equal(v3.F.y, 2)
コード例 #22
0
def test_number_to_string():
    echo_function("test_number_to_string")
    from phystricks.src.Utilities import number_to_string

    a = 7.73542889062775 * cos(11 / 9 * pi +
                               1.30951587282752) - 7.55775391156456 * cos(
                                   5 / 18 * pi) + 2.5 * cos(2 / 9 * pi)
    assert_equal(number_to_string(a, digits=7), "0.329851")

    assert_equal(number_to_string(0, digits=15), "0.00000000000000")
    assert_equal(number_to_string(120, digits=3), "120")
    assert_equal(number_to_string(120, digits=5), "120.00")
    assert_equal(number_to_string(120.67, digits=3), "120")
    assert_equal(number_to_string(120.67, digits=4), "120.6")
    assert_equal(number_to_string(120.67, digits=14), "120.67000000000")
    assert_equal(number_to_string(-1, digits=3), "-1.00")
    assert_equal(number_to_string(-12, digits=2), "-12")
    assert_equal(number_to_string(-0.1234, digits=6), "-0.12340")
    assert_equal(number_to_string(-0.12, digits=3), "-0.12")