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)
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])
def test_is_negative(): echo_function("test_is_negative") echo_single_test("some values") a = -sin(0.5 * pi) assert_true(numerical_is_negative(a)) a = -pi assert_true(numerical_is_negative(a)) a = pi assert_false(numerical_is_negative(a)) echo_single_test("zero is not negative") assert_false(numerical_is_negative(0))
def test_is_negative(): from phystricks.src.Numerical import numerical_is_negative echo_function("test_is_negative") echo_single_test("some values") a = -sin(0.5 * pi) assert_true(numerical_is_negative(a)) a = -pi assert_true(numerical_is_negative(a)) a = pi assert_false(numerical_is_negative(a)) echo_single_test("zero is not negative") assert_false(numerical_is_negative(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])
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)
def test_decorator(): echo_function("test_decorator") v = AffineVector(Point(1, 1), Point(2, 2)) echo_single_test("initial value : None") assert_true(v.parameters.color == None) assert_true(v.parameters.style == None) v.parameters.color = "foo" v.parameters.style = "bar" echo_single_test("after fix_origin") w = v.fix_origin(Point(3, 4)) assert_true(w.parameters.color == "foo") assert_true(w.parameters.style == "bar")
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)
def test_non_equalities(): echo_function("test_non_equalities") A = Point(0, 0) B = Point(4.00000000000000 * cos(0.111111111111111 * pi), 0) echo_single_test("difficult not 'almost equal'") assert_false(A.is_almost_equal(B)) echo_single_test("difficult not 'really equal'") assert_false(A == B) v = AffineVector(A, B) u = AffineVector(B, A) w = AffineVector(A, -B) echo_single_test("difficult not 'almost equal' for affine vector") assert_false(v.is_almost_equal(w)) echo_single_test("difficult 'almost equal' for affine vector") assert_true(w.is_almost_equal(-v))
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))
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)
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)