def test_manualintegrate_trigonometry(): assert manualintegrate(sin(x), x) == -cos(x) assert manualintegrate(tan(x), x) == -log(cos(x)) assert manualintegrate(sec(x), x) == log(sec(x) + tan(x)) assert manualintegrate(csc(x), x) == -log(csc(x) + cot(x)) assert manualintegrate(sin(x) * cos(x), x) in [sin(x)**2 / 2, -cos(x)**2 / 2] assert manualintegrate(-sec(x) * tan(x), x) == -sec(x) assert manualintegrate(csc(x) * cot(x), x) == -csc(x) assert manualintegrate(sec(x)**2, x) == tan(x) assert manualintegrate(csc(x)**2, x) == -cot(x) assert manualintegrate(x * sec(x**2), x) == log(tan(x**2) + sec(x**2)) / 2 assert manualintegrate(cos(x) * csc(sin(x)), x) == -log(cot(sin(x)) + csc(sin(x))) assert manualintegrate(cos(3 * x) * sec(x), x) == -x + sin(2 * x) assert manualintegrate(sin(3*x)*sec(x), x) == \ -3*log(cos(x)) + 2*log(cos(x)**2) - 2*cos(x)**2 assert_is_integral_of(sinh(2 * x), cosh(2 * x) / 2) assert_is_integral_of(x * cosh(x**2), sinh(x**2) / 2) assert_is_integral_of(tanh(x), log(cosh(x))) assert_is_integral_of(coth(x), log(sinh(x))) f, F = sech(x), 2 * atan(tanh(x / 2)) assert manualintegrate(f, x) == F assert (F.diff(x) - f).rewrite(exp).simplify() == 0 # todo: equals returns None f, F = csch(x), log(tanh(x / 2)) assert manualintegrate(f, x) == F assert (F.diff(x) - f).rewrite(exp).simplify() == 0
def eval(cls, n, m, z=None): if z is not None: n, z, m = n, m, z k = 2 * z / pi if n == S.Zero: return elliptic_f(z, m) elif n == S.One: return elliptic_f(z, m) + (sqrt(1 - m * sin(z) ** 2) * tan(z) - elliptic_e(z, m)) / (1 - m) elif k.is_integer: return k * elliptic_pi(n, m) elif m == S.Zero: return atanh(sqrt(n - 1) * tan(z)) / sqrt(n - 1) elif n == m: return elliptic_f(z, n) - elliptic_pi(1, z, n) + tan(z) / sqrt(1 - n * sin(z) ** 2) elif n in (S.Infinity, S.NegativeInfinity): return S.Zero elif m in (S.Infinity, S.NegativeInfinity): return S.Zero elif z.could_extract_minus_sign(): return -elliptic_pi(n, -z, m) else: if n == S.Zero: return elliptic_k(m) elif n == S.One: return S.ComplexInfinity elif m == S.Zero: return pi / (2 * sqrt(1 - n)) elif m == S.One: return -S.Infinity / sign(n - 1) elif n == m: return elliptic_e(n) / (1 - n) elif n in (S.Infinity, S.NegativeInfinity): return S.Zero elif m in (S.Infinity, S.NegativeInfinity): return S.Zero
def test_heurisch_trigonometric(): assert heurisch(sin(x), x) == -cos(x) assert heurisch(pi * sin(x) + 1, x) == x - pi * cos(x) assert heurisch(cos(x), x) == sin(x) assert heurisch(tan(x), x) in [ log(1 + tan(x)**2) / 2, log(tan(x) + I) + I * x, log(tan(x) - I) - I * x, ] assert heurisch(sin(x) * sin(y), x) == -cos(x) * sin(y) assert heurisch(sin(x) * sin(y), y) == -cos(y) * sin(x) # gives sin(x) in answer when run via setup.py and cos(x) when run via py.test assert heurisch(sin(x) * cos(x), x) in [sin(x)**2 / 2, -cos(x)**2 / 2] assert heurisch(cos(x) / sin(x), x) == log(sin(x)) assert heurisch(x * sin(7 * x), x) == sin(7 * x) / 49 - x * cos(7 * x) / 7 assert heurisch( 1 / pi / 4 * x**2 * cos(x), x) == 1 / pi / 4 * (x**2 * sin(x) - 2 * sin(x) + 2 * x * cos(x)) assert heurisch(acos(x/4) * asin(x/4), x) == 2*x - (sqrt(16 - x**2))*asin(x/4) \ + (sqrt(16 - x**2))*acos(x/4) + x*asin(x/4)*acos(x/4) assert heurisch(sin(x) / (cos(x)**2 + 1), x) == -atan(cos(x)) #fixes issue 13723 assert heurisch(1 / (cos(x) + 2), x) == 2 * sqrt(3) * atan(sqrt(3) * tan(x / 2) / 3) / 3 assert heurisch( 2 * sin(x) * cos(x) / (sin(x)**4 + 1), x) == atan(sqrt(2) * sin(x) - 1) - atan(sqrt(2) * sin(x) + 1) assert heurisch(1 / cosh(x), x) == 2 * atan(tanh(x / 2))
def f(rv): if not rv.is_Mul: return rv args = {tan: [], cot: [], None: []} for a in ordered(Mul.make_args(rv)): if a.func in (tan, cot): args[a.func].append(a.args[0]) else: args[None].append(a) t = args[tan] c = args[cot] if len(t) < 2 and len(c) < 2: return rv args = args[None] while len(t) > 1: t1 = t.pop() t2 = t.pop() args.append(1 - (tan(t1) / tan(t1 + t2) + tan(t2) / tan(t1 + t2))) if t: args.append(tan(t.pop())) while len(c) > 1: t1 = c.pop() t2 = c.pop() args.append(1 + cot(t1) * cot(t1 + t2) + cot(t2) * cot(t1 + t2)) if c: args.append(cot(c.pop())) return Mul(*args)
def test_maximum(): x, y = symbols('x y') assert maximum(sin(x), x) is S.One assert maximum(sin(x), x, Interval(0, 1)) == sin(1) assert maximum(tan(x), x) is oo assert maximum(tan(x), x, Interval(-pi/4, pi/4)) is S.One assert maximum(sin(x)*cos(x), x, S.Reals) == S.Half assert simplify(maximum(sin(x)*cos(x), x, Interval(pi*Rational(3, 8), pi*Rational(5, 8))) ) == sqrt(2)/4 assert maximum((x+3)*(x-2), x) is oo assert maximum((x+3)*(x-2), x, Interval(-5, 0)) == S(14) assert maximum((x+3)/(x-2), x, Interval(-5, 0)) == Rational(2, 7) assert simplify(maximum(-x**4-x**3+x**2+10, x) ) == 41*sqrt(41)/512 + Rational(5419, 512) assert maximum(exp(x), x, Interval(-oo, 2)) == exp(2) assert maximum(log(x) - x, x, S.Reals) is S.NegativeOne assert maximum(cos(x), x, Union(Interval(0, 5), Interval(-6, -3)) ) is S.One assert maximum(cos(x)-sin(x), x, S.Reals) == sqrt(2) assert maximum(y, x, S.Reals) == y assert maximum(abs(a**3 + a), a, Interval(0, 2)) == 10 assert maximum(abs(60*a**3 + 24*a), a, Interval(0, 2)) == 528 assert maximum(abs(12*a*(5*a**2 + 2)), a, Interval(0, 2)) == 528 assert maximum(x/sqrt(x**2+1), x, S.Reals) == 1 raises(ValueError, lambda : maximum(sin(x), x, S.EmptySet)) raises(ValueError, lambda : maximum(log(cos(x)), x, S.EmptySet)) raises(ValueError, lambda : maximum(1/(x**2 + y**2 + 1), x, S.EmptySet)) raises(ValueError, lambda : maximum(sin(x), sin(x))) raises(ValueError, lambda : maximum(sin(x), x*y, S.EmptySet)) raises(ValueError, lambda : maximum(sin(x), S.One))
def test_minimum(): x, y = symbols('x y') assert minimum(sin(x), x) is S.NegativeOne assert minimum(sin(x), x, Interval(1, 4)) == sin(4) assert minimum(tan(x), x) is -oo assert minimum(tan(x), x, Interval(-pi/4, pi/4)) is S.NegativeOne assert minimum(sin(x)*cos(x), x, S.Reals) == Rational(-1, 2) assert simplify(minimum(sin(x)*cos(x), x, Interval(pi*Rational(3, 8), pi*Rational(5, 8))) ) == -sqrt(2)/4 assert minimum((x+3)*(x-2), x) == Rational(-25, 4) assert minimum((x+3)/(x-2), x, Interval(-5, 0)) == Rational(-3, 2) assert minimum(x**4-x**3+x**2+10, x) == S(10) assert minimum(exp(x), x, Interval(-2, oo)) == exp(-2) assert minimum(log(x) - x, x, S.Reals) is -oo assert minimum(cos(x), x, Union(Interval(0, 5), Interval(-6, -3)) ) is S.NegativeOne assert minimum(cos(x)-sin(x), x, S.Reals) == -sqrt(2) assert minimum(y, x, S.Reals) == y assert minimum(x/sqrt(x**2+1), x, S.Reals) == -1 raises(ValueError, lambda : minimum(sin(x), x, S.EmptySet)) raises(ValueError, lambda : minimum(log(cos(x)), x, S.EmptySet)) raises(ValueError, lambda : minimum(1/(x**2 + y**2 + 1), x, S.EmptySet)) raises(ValueError, lambda : minimum(sin(x), sin(x))) raises(ValueError, lambda : minimum(sin(x), x*y, S.EmptySet)) raises(ValueError, lambda : minimum(sin(x), S.One))
def test_ray_generation(): assert Ray((1, 1), angle=pi / 4) == Ray((1, 1), (2, 2)) assert Ray((1, 1), angle=pi / 2) == Ray((1, 1), (1, 2)) assert Ray((1, 1), angle=-pi / 2) == Ray((1, 1), (1, 0)) assert Ray((1, 1), angle=-3 * pi / 2) == Ray((1, 1), (1, 2)) assert Ray((1, 1), angle=5 * pi / 2) == Ray((1, 1), (1, 2)) assert Ray((1, 1), angle=5.0 * pi / 2) == Ray((1, 1), (1, 2)) assert Ray((1, 1), angle=pi) == Ray((1, 1), (0, 1)) assert Ray((1, 1), angle=3.0 * pi) == Ray((1, 1), (0, 1)) assert Ray((1, 1), angle=4.0 * pi) == Ray((1, 1), (2, 1)) assert Ray((1, 1), angle=0) == Ray((1, 1), (2, 1)) assert Ray((1, 1), angle=4.05 * pi) == Ray( Point(1, 1), Point( 2, -sqrt(5) * sqrt(2 * sqrt(5) + 10) / 4 - sqrt(2 * sqrt(5) + 10) / 4 + 2 + sqrt(5), ), ) assert Ray((1, 1), angle=4.02 * pi) == Ray(Point(1, 1), Point(2, 1 + tan(4.02 * pi))) assert Ray((1, 1), angle=5) == Ray((1, 1), (2, 1 + tan(5))) assert Ray3D((1, 1, 1), direction_ratio=[4, 4, 4]) == Ray3D(Point3D(1, 1, 1), Point3D(5, 5, 5)) assert Ray3D((1, 1, 1), direction_ratio=[1, 2, 3]) == Ray3D(Point3D(1, 1, 1), Point3D(2, 3, 4)) assert Ray3D((1, 1, 1), direction_ratio=[1, 1, 1]) == Ray3D(Point3D(1, 1, 1), Point3D(2, 2, 2))
def test_hyper_as_trig(): from sympy.simplify.fu import _osborne, _osbornei eq = sinh(x)**2 + cosh(x)**2 t, f = hyper_as_trig(eq) assert f(fu(t)) == cosh(2 * x) e, f = hyper_as_trig(tanh(x + y)) assert f(TR12(e)) == (tanh(x) + tanh(y)) / (tanh(x) * tanh(y) + 1) d = Dummy() assert _osborne(sinh(x), d) == I * sin(x * d) assert _osborne(tanh(x), d) == I * tan(x * d) assert _osborne(coth(x), d) == cot(x * d) / I assert _osborne(cosh(x), d) == cos(x * d) assert _osborne(sech(x), d) == sec(x * d) assert _osborne(csch(x), d) == csc(x * d) / I for func in (sinh, cosh, tanh, coth, sech, csch): h = func(pi) assert _osbornei(_osborne(h, d), d) == h # /!\ the _osborne functions are not meant to work # in the o(i(trig, d), d) direction so we just check # that they work as they are supposed to work assert _osbornei(cos(x * y + z), y) == cosh(x + z * I) assert _osbornei(sin(x * y + z), y) == sinh(x + z * I) / I assert _osbornei(tan(x * y + z), y) == tanh(x + z * I) / I assert _osbornei(cot(x * y + z), y) == coth(x + z * I) * I assert _osbornei(sec(x * y + z), y) == sech(x + z * I) assert _osbornei(csc(x * y + z), y) == csch(x + z * I) * I
def test_hyperbolic_simp(): x, y = symbols('x,y') assert trigsimp(sinh(x)**2 + 1) == cosh(x)**2 assert trigsimp(cosh(x)**2 - 1) == sinh(x)**2 assert trigsimp(cosh(x)**2 - sinh(x)**2) == 1 assert trigsimp(1 - tanh(x)**2) == 1 / cosh(x)**2 assert trigsimp(1 - 1 / cosh(x)**2) == tanh(x)**2 assert trigsimp(tanh(x)**2 + 1 / cosh(x)**2) == 1 assert trigsimp(coth(x)**2 - 1) == 1 / sinh(x)**2 assert trigsimp(1 / sinh(x)**2 + 1) == 1 / tanh(x)**2 assert trigsimp(coth(x)**2 - 1 / sinh(x)**2) == 1 assert trigsimp(5 * cosh(x)**2 - 5 * sinh(x)**2) == 5 assert trigsimp(5 * cosh(x / 2)**2 - 2 * sinh(x / 2)**2) == 3 * cosh(x) / 2 + Rational(7, 2) assert trigsimp(sinh(x) / cosh(x)) == tanh(x) assert trigsimp(tanh(x)) == trigsimp(sinh(x) / cosh(x)) assert trigsimp(cosh(x) / sinh(x)) == 1 / tanh(x) assert trigsimp(2 * tanh(x) * cosh(x)) == 2 * sinh(x) assert trigsimp(coth(x)**3 * sinh(x)**3) == cosh(x)**3 assert trigsimp(y * tanh(x)**2 / sinh(x)**2) == y / cosh(x)**2 assert trigsimp(coth(x) / cosh(x)) == 1 / sinh(x) for a in (pi / 6 * I, pi / 4 * I, pi / 3 * I): assert trigsimp(sinh(a) * cosh(x) + cosh(a) * sinh(x)) == sinh(x + a) assert trigsimp(-sinh(a) * cosh(x) + cosh(a) * sinh(x)) == sinh(x - a) e = 2 * cosh(x)**2 - 2 * sinh(x)**2 assert trigsimp(log(e)) == log(2) # issue 19535: assert trigsimp(sqrt(cosh(x)**2 - 1)) == sqrt(sinh(x)**2) assert trigsimp(cosh(x)**2 * cosh(y)**2 - cosh(x)**2 * sinh(y)**2 - sinh(x)**2, recursive=True) == 1 assert trigsimp(sinh(x)**2 * sinh(y)**2 - sinh(x)**2 * cosh(y)**2 + cosh(x)**2, recursive=True) == 1 assert abs(trigsimp(2.0 * cosh(x)**2 - 2.0 * sinh(x)**2) - 2.0) < 1e-10 assert trigsimp(sinh(x)**2 / cosh(x)**2) == tanh(x)**2 assert trigsimp(sinh(x)**3 / cosh(x)**3) == tanh(x)**3 assert trigsimp(sinh(x)**10 / cosh(x)**10) == tanh(x)**10 assert trigsimp(cosh(x)**3 / sinh(x)**3) == 1 / tanh(x)**3 assert trigsimp(cosh(x) / sinh(x)) == 1 / tanh(x) assert trigsimp(cosh(x)**2 / sinh(x)**2) == 1 / tanh(x)**2 assert trigsimp(cosh(x)**10 / sinh(x)**10) == 1 / tanh(x)**10 assert trigsimp(x * cosh(x) * tanh(x)) == x * sinh(x) assert trigsimp(-sinh(x) + cosh(x) * tanh(x)) == 0 assert tan(x) != 1 / cot(x) # cot doesn't auto-simplify assert trigsimp(tan(x) - 1 / cot(x)) == 0 assert trigsimp(3 * tanh(x)**7 - 2 / coth(x)**7) == tanh(x)**7
def test_find_substitutions(): assert find_substitutions((cot(x)**2 + 1)**2*csc(x)**2*cot(x)**2, x, u) == \ [(cot(x), 1, -u**6 - 2*u**4 - u**2)] assert find_substitutions( (sec(x)**2 + tan(x) * sec(x)) / (sec(x) + tan(x)), x, u) == [(sec(x) + tan(x), 1, 1 / u)] assert (-x**2, Rational(-1, 2), exp(u)) in find_substitutions(x * exp(-x**2), x, u)
def test_issue_19113(): eq = y**3 - y + 1 # generator is a canonical x in RootOf assert str(Poly(eq).real_roots()) == '[CRootOf(x**3 - x + 1, 0)]' assert str(Poly(eq.subs( y, tan(y))).real_roots()) == '[CRootOf(x**3 - x + 1, 0)]' assert str(Poly(eq.subs( y, tan(x))).real_roots()) == '[CRootOf(x**3 - x + 1, 0)]'
def test_issue_18795(): r = Symbol('r', real=True) a = B(-1, 1) c = B(7, oo) b = B(-oo, oo) assert c - tan(r) == B(7 - tan(r), oo) assert b + tan(r) == B(-oo, oo) assert (a + r) / a == B(-oo, oo) * B(r - 1, r + 1) assert (b + a) / a == B(-oo, oo)
def test_RootSum_independent(): f = (x**3 - a)**2 * (x**4 - b)**3 g = Lambda(x, 5 * tan(x) + 7) h = Lambda(x, tan(x)) r0 = RootSum(x**3 - a, h, x) r1 = RootSum(x**4 - b, h, x) assert RootSum(f, g, x).as_ordered_terms() == [10 * r0, 15 * r1, 126]
def test_issue_18473(): assert limit(sin(x)**(1/x), x, oo) == Limit(sin(x)**(1/x), x, oo, dir='-') assert limit(cos(x)**(1/x), x, oo) == Limit(cos(x)**(1/x), x, oo, dir='-') assert limit(tan(x)**(1/x), x, oo) == Limit(tan(x)**(1/x), x, oo, dir='-') assert limit((cos(x) + 2)**(1/x), x, oo) == 1 assert limit((sin(x) + 10)**(1/x), x, oo) == 1 assert limit((cos(x) - 2)**(1/x), x, oo) == Limit((cos(x) - 2)**(1/x), x, oo, dir='-') assert limit((cos(x) + 1)**(1/x), x, oo) == AccumBounds(0, 1) assert limit((tan(x)**2)**(2/x) , x, oo) == AccumBounds(0, oo) assert limit((sin(x)**2)**(1/x), x, oo) == AccumBounds(0, 1)
def test_P(): assert P(0, z, m) == F(z, m) assert P(1, z, m) == F(z, m) + \ (sqrt(1 - m*sin(z)**2)*tan(z) - E(z, m))/(1 - m) assert P(n, i*pi/2, m) == i*P(n, m) assert P(n, z, 0) == atanh(sqrt(n - 1)*tan(z))/sqrt(n - 1) assert P(n, z, n) == F(z, n) - P(1, z, n) + tan(z)/sqrt(1 - n*sin(z)**2) assert P(oo, z, m) == 0 assert P(-oo, z, m) == 0 assert P(n, z, oo) == 0 assert P(n, z, -oo) == 0 assert P(0, m) == K(m) assert P(1, m) is zoo assert P(n, 0) == pi/(2*sqrt(1 - n)) assert P(2, 1) is -oo assert P(-1, 1) is oo assert P(n, n) == E(n)/(1 - n) assert P(n, -z, m) == -P(n, z, m) ni, mi = Symbol('n', real=False), Symbol('m', real=False) assert P(ni, z, mi).conjugate() == \ P(ni.conjugate(), z.conjugate(), mi.conjugate()) nr, mr = Symbol('n', negative=True), \ Symbol('m', negative=True) assert P(nr, z, mr).conjugate() == P(nr, z.conjugate(), mr) assert P(n, m).conjugate() == P(n.conjugate(), m.conjugate()) assert P(n, z, m).diff(n) == (E(z, m) + (m - n)*F(z, m)/n + (n**2 - m)*P(n, z, m)/n - n*sqrt(1 - m*sin(z)**2)*sin(2*z)/(2*(1 - n*sin(z)**2)))/(2*(m - n)*(n - 1)) assert P(n, z, m).diff(z) == 1/(sqrt(1 - m*sin(z)**2)*(1 - n*sin(z)**2)) assert P(n, z, m).diff(m) == (E(z, m)/(m - 1) + P(n, z, m) - m*sin(2*z)/(2*(m - 1)*sqrt(1 - m*sin(z)**2)))/(2*(n - m)) assert P(n, m).diff(n) == (E(m) + (m - n)*K(m)/n + (n**2 - m)*P(n, m)/n)/(2*(m - n)*(n - 1)) assert P(n, m).diff(m) == (E(m)/(m - 1) + P(n, m))/(2*(n - m)) # These tests fail due to # https://github.com/fredrik-johansson/mpmath/issues/571#issuecomment-777201962 # https://github.com/sympy/sympy/issues/20933#issuecomment-777080385 # # rx, ry = randcplx(), randcplx() # assert td(P(n, rx, ry), n) # assert td(P(rx, z, ry), z) # assert td(P(rx, ry, m), m) assert P(n, z, m).series(z) == z + z**3*(m/6 + n/3) + \ z**5*(3*m**2/40 + m*n/10 - m/30 + n**2/5 - n/15) + O(z**6) assert P(n, z, m).rewrite(Integral).dummy_eq( Integral(1/((1 - n*sin(t)**2)*sqrt(1 - m*sin(t)**2)), (t, 0, z))) assert P(n, m).rewrite(Integral).dummy_eq( Integral(1/((1 - n*sin(t)**2)*sqrt(1 - m*sin(t)**2)), (t, 0, pi/2)))
def test_exptrigsimp(): def valid(a, b): from sympy.core.random import verify_numerically as tn if not (tn(a, b) and a == b): return False return True assert exptrigsimp(exp(x) + exp(-x)) == 2 * cosh(x) assert exptrigsimp(exp(x) - exp(-x)) == 2 * sinh(x) assert exptrigsimp( (2 * exp(x) - 2 * exp(-x)) / (exp(x) + exp(-x))) == 2 * tanh(x) assert exptrigsimp((2 * exp(2 * x) - 2) / (exp(2 * x) + 1)) == 2 * tanh(x) e = [ cos(x) + I * sin(x), cos(x) - I * sin(x), cosh(x) - sinh(x), cosh(x) + sinh(x) ] ok = [exp(I * x), exp(-I * x), exp(-x), exp(x)] assert all(valid(i, j) for i, j in zip([exptrigsimp(ei) for ei in e], ok)) ue = [ cos(x) + sin(x), cos(x) - sin(x), cosh(x) + I * sinh(x), cosh(x) - I * sinh(x) ] assert [exptrigsimp(ei) == ei for ei in ue] res = [] ok = [ y * tanh(1), 1 / (y * tanh(1)), I * y * tan(1), -I / (y * tan(1)), y * tanh(x), 1 / (y * tanh(x)), I * y * tan(x), -I / (y * tan(x)), y * tanh(1 + I), 1 / (y * tanh(1 + I)) ] for a in (1, I, x, I * x, 1 + I): w = exp(a) eq = y * (w - 1 / w) / (w + 1 / w) res.append(simplify(eq)) res.append(simplify(1 / eq)) assert all(valid(i, j) for i, j in zip(res, ok)) for a in range(1, 3): w = exp(a) e = w + 1 / w s = simplify(e) assert s == exptrigsimp(e) assert valid(s, 2 * cosh(a)) e = w - 1 / w s = simplify(e) assert s == exptrigsimp(e) assert valid(s, 2 * sinh(a))
def test_manualintegrate_trigpowers(): assert manualintegrate(sin(x)**2 * cos(x), x) == sin(x)**3 / 3 assert manualintegrate(sin(x)**2 * cos(x) **2, x) == \ x / 8 - sin(4*x) / 32 assert manualintegrate(sin(x) * cos(x)**3, x) == -cos(x)**4 / 4 assert manualintegrate(sin(x)**3 * cos(x)**2, x) == \ cos(x)**5 / 5 - cos(x)**3 / 3 assert manualintegrate(tan(x)**3 * sec(x), x) == sec(x)**3 / 3 - sec(x) assert manualintegrate(tan(x) * sec(x)**2, x) == sec(x)**2 / 2 assert manualintegrate(cot(x)**5 * csc(x), x) == \ -csc(x)**5/5 + 2*csc(x)**3/3 - csc(x) assert manualintegrate(cot(x)**2 * csc(x)**6, x) == \ -cot(x)**7/7 - 2*cot(x)**5/5 - cot(x)**3/3
def test_RootSum___new__(): f = x**3 + x + 3 g = Lambda(r, log(r * x)) s = RootSum(f, g) assert isinstance(s, RootSum) is True assert RootSum(f**2, g) == 2 * RootSum(f, g) assert RootSum((x - 7) * f**3, g) == log(7 * x) + 3 * RootSum(f, g) # issue 5571 assert hash(RootSum((x - 7) * f**3, g)) == hash(log(7 * x) + 3 * RootSum(f, g)) raises(MultivariatePolynomialError, lambda: RootSum(x**3 + x + y)) raises(ValueError, lambda: RootSum(x**2 + 3, lambda x: x)) assert RootSum(f, exp) == RootSum(f, Lambda(x, exp(x))) assert RootSum(f, log) == RootSum(f, Lambda(x, log(x))) assert isinstance(RootSum(f, auto=False), RootSum) is True assert RootSum(f) == 0 assert RootSum(f, Lambda(x, x)) == 0 assert RootSum(f, Lambda(x, x**2)) == -2 assert RootSum(f, Lambda(x, 1)) == 3 assert RootSum(f, Lambda(x, 2)) == 6 assert RootSum(f, auto=False).is_commutative is True assert RootSum(f, Lambda(x, 1 / (x + x**2))) == Rational(11, 3) assert RootSum(f, Lambda(x, y / (x + x**2))) == Rational(11, 3) * y assert RootSum(x**2 - 1, Lambda(x, 3 * x**2), x) == 6 assert RootSum(x**2 - y, Lambda(x, 3 * x**2), x) == 6 * y assert RootSum(x**2 - 1, Lambda(x, z * x**2), x) == 2 * z assert RootSum(x**2 - y, Lambda(x, z * x**2), x) == 2 * z * y assert RootSum(x**2 - 1, Lambda(x, exp(x)), quadratic=True) == exp(-1) + exp(1) assert RootSum(x**3 + a*x + a**3, tan, x) == \ RootSum(x**3 + x + 1, Lambda(x, tan(a*x))) assert RootSum(a**3*x**3 + a*x + 1, tan, x) == \ RootSum(x**3 + x + 1, Lambda(x, tan(x/a)))
def eval(cls, n, m, z=None): if z is not None: n, z, m = n, m, z if n.is_zero: return elliptic_f(z, m) elif n is S.One: return (elliptic_f(z, m) + (sqrt(1 - m*sin(z)**2)*tan(z) - elliptic_e(z, m))/(1 - m)) k = 2*z/pi if k.is_integer: return k*elliptic_pi(n, m) elif m.is_zero: return atanh(sqrt(n - 1)*tan(z))/sqrt(n - 1) elif n == m: return (elliptic_f(z, n) - elliptic_pi(1, z, n) + tan(z)/sqrt(1 - n*sin(z)**2)) elif n in (S.Infinity, S.NegativeInfinity): return S.Zero elif m in (S.Infinity, S.NegativeInfinity): return S.Zero elif z.could_extract_minus_sign(): return -elliptic_pi(n, -z, m) if n.is_zero: return elliptic_f(z, m) if m.is_extended_real and m.is_infinite or \ n.is_extended_real and n.is_infinite: return S.Zero else: if n.is_zero: return elliptic_k(m) elif n is S.One: return S.ComplexInfinity elif m.is_zero: return pi/(2*sqrt(1 - n)) elif m == S.One: return S.NegativeInfinity/sign(n - 1) elif n == m: return elliptic_e(n)/(1 - n) elif n in (S.Infinity, S.NegativeInfinity): return S.Zero elif m in (S.Infinity, S.NegativeInfinity): return S.Zero if n.is_zero: return elliptic_k(m) if m.is_extended_real and m.is_infinite or \ n.is_extended_real and n.is_infinite: return S.Zero
def test_series3(): w = Symbol("w", real=True) e = w**(-6) * (w**3 * tan(w) - w**3 * sin(w)) assert e.nseries( w, n=8 ) == Integer(1) / 2 + w**2 / 8 + 13 * w**4 / 240 + 529 * w**6 / 24192 + O( w**8)
def test_issue_4511(): # This works, but gives a complicated answer. The correct answer is x - cos(x). # If current answer is simplified, 1 - cos(x) + x is obtained. # The last one is what Maple gives. It is also quite slow. assert integrate(cos(x)**2 / (1 - sin(x))) in [ x - cos(x), 1 - cos(x) + x, -2 / (tan((S.Half) * x)**2 + 1) + x ]
def test_trigsimp_groebner(): from sympy.simplify.trigsimp import trigsimp_groebner c = cos(x) s = sin(x) ex = (4 * s * c + 12 * s + 5 * c**3 + 21 * c**2 + 23 * c + 15) / ( -s * c**2 + 2 * s * c + 15 * s + 7 * c**3 + 31 * c**2 + 37 * c + 21) resnum = (5 * s - 5 * c + 1) resdenom = (8 * s - 6 * c) results = [resnum / resdenom, (-resnum) / (-resdenom)] assert trigsimp_groebner(ex) in results assert trigsimp_groebner(s / c, hints=[tan]) == tan(x) assert trigsimp_groebner(c * s) == c * s assert trigsimp((-s + 1) / c + c / (-s + 1), method='groebner') == 2 / c assert trigsimp((-s + 1) / c + c / (-s + 1), method='groebner', polynomial=True) == 2 / c # Test quick=False works assert trigsimp_groebner(ex, hints=[2]) in results assert trigsimp_groebner(ex, hints=[int(2)]) in results # test "I" assert trigsimp_groebner(sin(I * x) / cos(I * x), hints=[tanh]) == I * tanh(x) # test hyperbolic / sums assert trigsimp_groebner((tanh(x) + tanh(y)) / (1 + tanh(x) * tanh(y)), hints=[(tanh, x, y)]) == tanh(x + y)
def test_TR14(): eq = (cos(x) - 1) * (cos(x) + 1) ans = -sin(x)**2 assert TR14(eq) == ans assert TR14(1 / eq) == 1 / ans assert TR14((cos(x) - 1)**2 * (cos(x) + 1)**2) == ans**2 assert TR14((cos(x) - 1)**2 * (cos(x) + 1)**3) == ans**2 * (cos(x) + 1) assert TR14((cos(x) - 1)**3 * (cos(x) + 1)**2) == ans**2 * (cos(x) - 1) eq = (cos(x) - 1)**y * (cos(x) + 1)**y assert TR14(eq) == eq eq = (cos(x) - 2)**y * (cos(x) + 1) assert TR14(eq) == eq eq = (tan(x) - 2)**2 * (cos(x) + 1) assert TR14(eq) == eq i = symbols('i', integer=True) assert TR14((cos(x) - 1)**i * (cos(x) + 1)**i) == ans**i assert TR14((sin(x) - 1)**i * (sin(x) + 1)**i) == (-cos(x)**2)**i # could use extraction in this case eq = (cos(x) - 1)**(i + 1) * (cos(x) + 1)**i assert TR14(eq) in [(cos(x) - 1) * ans**i, eq] assert TR14((sin(x) - 1) * (sin(x) + 1)) == -cos(x)**2 p1 = (cos(x) + 1) * (cos(x) - 1) p2 = (cos(y) - 1) * 2 * (cos(y) + 1) p3 = (3 * (cos(y) - 1)) * (3 * (cos(y) + 1)) assert TR14(p1 * p2 * p3 * (x - 1)) == -18 * ((x - 1) * sin(x)**2 * sin(y)**4)
def test_line_intersection(): # see also test_issue_11238 in test_matrices.py x0 = tan(pi * Rational(13, 45)) x1 = sqrt(3) x2 = x0**2 x, y = [8 * x0 / (x0 + x1), (24 * x0 - 8 * x1 * x2) / (x2 - 3)] assert Line(Point(0, 0), Point(1, -sqrt(3))).contains(Point(x, y)) is True
def plot_implicit_tests(name): temp_dir = mkdtemp() TmpFileManager.tmp_folder(temp_dir) x = Symbol('x') y = Symbol('y') #implicit plot tests plot_and_save(Eq(y, cos(x)), (x, -5, 5), (y, -2, 2), name=name, dir=temp_dir) plot_and_save(Eq(y**2, x**3 - x), (x, -5, 5), (y, -4, 4), name=name, dir=temp_dir) plot_and_save(y > 1 / x, (x, -5, 5), (y, -2, 2), name=name, dir=temp_dir) plot_and_save(y < 1 / tan(x), (x, -5, 5), (y, -2, 2), name=name, dir=temp_dir) plot_and_save(y >= 2 * sin(x) * cos(x), (x, -5, 5), (y, -2, 2), name=name, dir=temp_dir) plot_and_save(y <= x**2, (x, -3, 3), (y, -1, 5), name=name, dir=temp_dir) #Test all input args for plot_implicit plot_and_save(Eq(y**2, x**3 - x), dir=temp_dir) plot_and_save(Eq(y**2, x**3 - x), adaptive=False, dir=temp_dir) plot_and_save(Eq(y**2, x**3 - x), adaptive=False, points=500, dir=temp_dir) plot_and_save(y > x, (x, -5, 5), dir=temp_dir) plot_and_save(And(y > exp(x), y > x + 2), dir=temp_dir) plot_and_save(Or(y > x, y > -x), dir=temp_dir) plot_and_save(x**2 - 1, (x, -5, 5), dir=temp_dir) plot_and_save(x**2 - 1, dir=temp_dir) plot_and_save(y > x, depth=-5, dir=temp_dir) plot_and_save(y > x, depth=5, dir=temp_dir) plot_and_save(y > cos(x), adaptive=False, dir=temp_dir) plot_and_save(y < cos(x), adaptive=False, dir=temp_dir) plot_and_save(And(y > cos(x), Or(y > x, Eq(y, x))), dir=temp_dir) plot_and_save(y - cos(pi / x), dir=temp_dir) plot_and_save(x**2 - 1, title='An implicit plot', dir=temp_dir)
def test_line_intersection(): assert asa(120, 8, 52) == \ Triangle( Point(0, 0), Point(8, 0), Point(-4 * cos(19 * pi / 90) / sin(2 * pi / 45), 4 * sqrt(3) * cos(19 * pi / 90) / sin(2 * pi / 45))) assert Line((0, 0), (1, 1)).intersection(Ray((1, 0), (1, 2))) == [Point(1, 1)] assert Line((0, 0), (1, 1)).intersection(Segment((1, 0), (1, 2))) == [Point(1, 1)] assert Ray((0, 0), (1, 1)).intersection(Ray((1, 0), (1, 2))) == [Point(1, 1)] assert Ray((0, 0), (1, 1)).intersection(Segment((1, 0), (1, 2))) == [Point(1, 1)] assert Ray((0, 0), (10, 10)).contains(Segment((1, 1), (2, 2))) is True assert Segment((1, 1), (2, 2)) in Line((0, 0), (10, 10)) x = 8 * tan(13 * pi / 45) / (tan(13 * pi / 45) + sqrt(3)) y = (-8 * sqrt(3) * tan(13 * pi / 45) ** 2 + 24 * tan(13 * pi / 45)) / (-3 + tan(13 * pi / 45) ** 2) assert Line(Point(0, 0), Point(1, -sqrt(3))).contains(Point(x, y)) is True
def test_stationary_points(): x, y = symbols('x y') assert stationary_points(sin(x), x, Interval(-pi/2, pi/2) ) == {-pi/2, pi/2} assert stationary_points(sin(x), x, Interval.Ropen(0, pi/4) ) is S.EmptySet assert stationary_points(tan(x), x, ) is S.EmptySet assert stationary_points(sin(x)*cos(x), x, Interval(0, pi) ) == {pi/4, pi*Rational(3, 4)} assert stationary_points(sec(x), x, Interval(0, pi) ) == {0, pi} assert stationary_points((x+3)*(x-2), x ) == FiniteSet(Rational(-1, 2)) assert stationary_points((x + 3)/(x - 2), x, Interval(-5, 5) ) is S.EmptySet assert stationary_points((x**2+3)/(x-2), x ) == {2 - sqrt(7), 2 + sqrt(7)} assert stationary_points((x**2+3)/(x-2), x, Interval(0, 5) ) == {2 + sqrt(7)} assert stationary_points(x**4 + x**3 - 5*x**2, x, S.Reals ) == FiniteSet(-2, 0, Rational(5, 4)) assert stationary_points(exp(x), x ) is S.EmptySet assert stationary_points(log(x) - x, x, S.Reals ) == {1} assert stationary_points(cos(x), x, Union(Interval(0, 5), Interval(-6, -3)) ) == {0, -pi, pi} assert stationary_points(y, x, S.Reals ) == S.Reals assert stationary_points(y, x, S.EmptySet) == S.EmptySet
def test_homogeneous_order(): assert homogeneous_order(exp(y/x) + tan(y/x), x, y) == 0 assert homogeneous_order(x**2 + sin(x)*cos(y), x, y) is None assert homogeneous_order(x - y - x*sin(y/x), x, y) == 1 assert homogeneous_order((x*y + sqrt(x**4 + y**4) + x**2*(log(x) - log(y)))/ (pi*x**Rational(2, 3)*sqrt(y)**3), x, y) == Rational(-1, 6) assert homogeneous_order(y/x*cos(y/x) - x/y*sin(y/x) + cos(y/x), x, y) == 0 assert homogeneous_order(f(x), x, f(x)) == 1 assert homogeneous_order(f(x)**2, x, f(x)) == 2 assert homogeneous_order(x*y*z, x, y) == 2 assert homogeneous_order(x*y*z, x, y, z) == 3 assert homogeneous_order(x**2*f(x)/sqrt(x**2 + f(x)**2), f(x)) is None assert homogeneous_order(f(x, y)**2, x, f(x, y), y) == 2 assert homogeneous_order(f(x, y)**2, x, f(x), y) is None assert homogeneous_order(f(x, y)**2, x, f(x, y)) is None assert homogeneous_order(f(y, x)**2, x, y, f(x, y)) is None assert homogeneous_order(f(y), f(x), x) is None assert homogeneous_order(-f(x)/x + 1/sin(f(x)/ x), f(x), x) == 0 assert homogeneous_order(log(1/y) + log(x**2), x, y) is None assert homogeneous_order(log(1/y) + log(x), x, y) == 0 assert homogeneous_order(log(x/y), x, y) == 0 assert homogeneous_order(2*log(1/y) + 2*log(x), x, y) == 0 a = Symbol('a') assert homogeneous_order(a*log(1/y) + a*log(x), x, y) == 0 assert homogeneous_order(f(x).diff(x), x, y) is None assert homogeneous_order(-f(x).diff(x) + x, x, y) is None assert homogeneous_order(O(x), x, y) is None assert homogeneous_order(x + O(x**2), x, y) is None assert homogeneous_order(x**pi, x) == pi assert homogeneous_order(x**x, x) is None raises(ValueError, lambda: homogeneous_order(x*y))
def test_issue_18473(): assert exp(x * log(cos(1 / x))).as_leading_term(x) == S.NaN assert exp(x * log(tan(1 / x))).as_leading_term(x) == S.NaN assert log(cos(1 / x)).as_leading_term(x) == S.NaN assert log(tan(1 / x)).as_leading_term(x) == S.NaN assert log(cos(1 / x) + 2).as_leading_term(x) == AccumBounds(0, log(3)) assert exp(x * log(cos(1 / x) + 2)).as_leading_term(x) == 1 assert log(cos(1 / x) - 2).as_leading_term(x) == S.NaN assert exp(x * log(cos(1 / x) - 2)).as_leading_term(x) == S.NaN assert log(cos(1 / x) + 1).as_leading_term(x) == AccumBounds(-oo, log(2)) assert exp(x * log(cos(1 / x) + 1)).as_leading_term(x) == AccumBounds(0, 1) assert log(sin(1 / x)**2).as_leading_term(x) == AccumBounds(-oo, 0) assert exp(x * log(sin(1 / x)**2)).as_leading_term(x) == AccumBounds(0, 1) assert log(tan(1 / x)**2).as_leading_term(x) == AccumBounds(-oo, oo) assert exp(2 * x * (log(tan(1 / x)**2))).as_leading_term(x) == AccumBounds( 0, oo)
def __new__(cls, p1, pt=None, angle=None, **kwargs): p1 = Point(p1) if pt is not None and angle is None: try: p2 = Point(pt) except NotImplementedError: from sympy.utilities.misc import filldedent raise ValueError( filldedent(''' The 2nd argument was not a valid Point; if it was meant to be an angle it should be given with keyword "angle".''')) if p1 == p2: raise ValueError('A Ray requires two distinct points.') elif angle is not None and pt is None: # we need to know if the angle is an odd multiple of pi/2 c = pi_coeff(sympify(angle)) p2 = None if c is not None: if c.is_Rational: if c.q == 2: if c.p == 1: p2 = p1 + Point(0, 1) elif c.p == 3: p2 = p1 + Point(0, -1) elif c.q == 1: if c.p == 0: p2 = p1 + Point(1, 0) elif c.p == 1: p2 = p1 + Point(-1, 0) if p2 is None: c *= S.Pi else: c = angle % (2 * S.Pi) if not p2: m = 2 * c / S.Pi left = And(1 < m, m < 3) # is it in quadrant 2 or 3? x = Piecewise((-1, left), (Piecewise((0, Eq(m % 1, 0)), (1, True)), True)) y = Piecewise((-tan(c), left), (Piecewise( (1, Eq(m, 1)), (-1, Eq(m, 3)), (tan(c), True)), True)) p2 = p1 + Point(x, y) else: raise ValueError('A 2nd point or keyword "angle" must be used.') return LinearEntity.__new__(cls, p1, p2, **kwargs)
def test_diff3(): p = Rational(5) e = a*b + sin(b**p) assert e == a*b + sin(b**5) assert e.diff(a) == b assert e.diff(b) == a + 5*b**4*cos(b**5) e = tan(c) assert e == tan(c) assert e.diff(c) in [cos(c)**(-2), 1 + sin(c)**2/cos(c)**2, 1 + tan(c)**2] e = c*log(c) - c assert e == -c + c*log(c) assert e.diff(c) == log(c) e = log(sin(c)) assert e == log(sin(c)) assert e.diff(c) in [sin(c)**(-1)*cos(c), cot(c)] e = (Rational(2)**a/log(Rational(2))) assert e == 2**a*log(Rational(2))**(-1) assert e.diff(a) == 2**a
def test_ray_generation(): assert Ray((1, 1), angle=pi / 4) == Ray((1, 1), (2, 2)) assert Ray((1, 1), angle=pi / 2) == Ray((1, 1), (1, 2)) assert Ray((1, 1), angle=-pi / 2) == Ray((1, 1), (1, 0)) assert Ray((1, 1), angle=-3 * pi / 2) == Ray((1, 1), (1, 2)) assert Ray((1, 1), angle=5 * pi / 2) == Ray((1, 1), (1, 2)) assert Ray((1, 1), angle=5.0 * pi / 2) == Ray((1, 1), (1, 2)) assert Ray((1, 1), angle=pi) == Ray((1, 1), (0, 1)) assert Ray((1, 1), angle=3.0 * pi) == Ray((1, 1), (0, 1)) assert Ray((1, 1), angle=4.0 * pi) == Ray((1, 1), (2, 1)) assert Ray((1, 1), angle=0) == Ray((1, 1), (2, 1)) assert Ray((1, 1), angle=4.05 * pi) == Ray(Point(1, 1), Point(2, -sqrt(5) * sqrt(2 * sqrt(5) + 10) / 4 - sqrt( 2 * sqrt(5) + 10) / 4 + 2 + sqrt(5))) assert Ray((1, 1), angle=4.02 * pi) == Ray(Point(1, 1), Point(2, 1 + tan(4.02 * pi))) assert Ray((1, 1), angle=5) == Ray((1, 1), (2, 1 + tan(5))) assert Ray3D((1, 1, 1), direction_ratio=[4, 4, 4]) == Ray3D(Point3D(1, 1, 1), Point3D(5, 5, 5)) assert Ray3D((1, 1, 1), direction_ratio=[1, 2, 3]) == Ray3D(Point3D(1, 1, 1), Point3D(2, 3, 4)) assert Ray3D((1, 1, 1), direction_ratio=[1, 1, 1]) == Ray3D(Point3D(1, 1, 1), Point3D(2, 2, 2))
def __new__(cls, p1, pt=None, angle=None, **kwargs): p1 = Point(p1) if pt is not None and angle is None: try: p2 = Point(pt) except NotImplementedError: from sympy.utilities.misc import filldedent raise ValueError(filldedent(''' The 2nd argument was not a valid Point; if it was meant to be an angle it should be given with keyword "angle".''')) if p1 == p2: raise ValueError('A Ray requires two distinct points.') elif angle is not None and pt is None: # we need to know if the angle is an odd multiple of pi/2 c = pi_coeff(sympify(angle)) p2 = None if c is not None: if c.is_Rational: if c.q == 2: if c.p == 1: p2 = p1 + Point(0, 1) elif c.p == 3: p2 = p1 + Point(0, -1) elif c.q == 1: if c.p == 0: p2 = p1 + Point(1, 0) elif c.p == 1: p2 = p1 + Point(-1, 0) if p2 is None: c *= S.Pi else: c = angle % (2*S.Pi) if not p2: m = 2*c/S.Pi left = And(1 < m, m < 3) # is it in quadrant 2 or 3? x = Piecewise((-1, left), (Piecewise((0, Eq(m % 1, 0)), (1, True)), True)) y = Piecewise((-tan(c), left), (Piecewise((1, Eq(m, 1)), (-1, Eq(m, 3)), (tan(c), True)), True)) p2 = p1 + Point(x, y) else: raise ValueError('A 2nd point or keyword "angle" must be used.') return LinearEntity.__new__(cls, p1, p2, **kwargs)
def test_line_intersection(): x0 = tan(13*pi/45) x1 = sqrt(3) x2 = x0**2 x, y = [8*x0/(x0 + x1), (24*x0 - 8*x1*x2)/(x2 - 3)] assert Line(Point(0, 0), Point(1, -sqrt(3))).contains(Point(x, y)) is True
def easyR(): #=================================================================================================================================================== # Range and Domain 0-100 , interval 5. Keep coordinates in this format Xa2=20. Ya2=80. Xb2=20. Yb2=20. Xc2=80. Yc2=50. eT= radians(60./60.) #=== Error in directions threshold=0.02 #==threshold to control danger circle uncertainty, and adjust depth of figure as wanted #=================================================================================================================================================== #=================================================================================================================================================== #================== Error Equations E and F Calculated using diff() function prior to running to lighten resources ======================= print "Calculating..." tA,tB = sy.symbols('tA,tB') dtA=eT**2 + eT**2 # check() also calculates error propagation by means of matrix multiplication, used as a check def check(): Xa,Ya,Xb,Yb,Xc,Yc, = sy.symbols('xa,ya,xb,yb,xc,yc') # dtA,dx2,dx3,dx4 = sy.symbols('dtA,dx2,dx3,dx4') tNBtop= -(Xc-Xa)+(Yc-Yb)*sy.cot(tB) +(Ya-Yb)*sy.cot(tA) tNBbot= (Yc-Ya)+(Xc-Xb)*sy.cot(tB)+(Xa-Xb)*sy.cot(tA) dtA=eT**2 + eT**2 TNB=[[sy.atan(tNBtop/tNBbot)]] # print TNB params = [tA,tB] Cov = sy.Matrix([[dtA, 0], [0, dtA]]) A = sy.Matrix(TNB) J = A.jacobian(params) eProp = J*Cov*J.T return eProp.subs(Xa,Xa2).subs(Xb,Xb2).subs(Xc,Xc2).subs(Ya,Ya2).subs(Yb,Yb2).subs(Yc,Yc2) #partial differentials squared multiply by error squared #======== PARTIAL DERIVATIVES^2 * Variance for E:alpha and F:beta E=((-(Xa2 - Xb2)*(-tan(tA)**2 - 1)*(Xa2 - Xc2 + (Ya2 - Yb2)/tan(tA) + (-Yb2 + Yc2)/tan(tB))/((-Ya2 + Yc2 + (Xa2 - Xb2)/tan(tA) + (-Xb2 + Xc2)/tan(tB))**2*tan(tA)**2) + (Ya2 - Yb2)*(-tan(tA)**2 - 1)/((-Ya2 + Yc2 + (Xa2 - Xb2)/tan(tA) + (-Xb2 + Xc2)/tan(tB))*tan(tA)**2))/((Xa2 - Xc2 + (Ya2 - Yb2)/tan(tA) + (-Yb2 + Yc2)/tan(tB))**2/(-Ya2 + Yc2 + (Xa2 - Xb2)/tan(tA) + (-Xb2 + Xc2)/tan(tB))**2 + 1))**2*dtA F=((-(-Xb2 + Xc2)*(-tan(tB)**2 - 1)*(Xa2 - Xc2 + (Ya2 - Yb2)/tan(tA) + (-Yb2 + Yc2)/tan(tB))/((-Ya2 + Yc2 + (Xa2 - Xb2)/tan(tA) + (-Xb2 + Xc2)/tan(tB))**2*tan(tB)**2) + (-Yb2 + Yc2)*(-tan(tB)**2 - 1)/((-Ya2 + Yc2 + (Xa2 - Xb2)/tan(tA) + (-Xb2 + Xc2)/tan(tB))*tan(tB)**2))/((Xa2 - Xc2 + (Ya2 - Yb2)/tan(tA) + (-Yb2 + Yc2)/tan(tB))**2/(-Ya2 + Yc2 + (Xa2 - Xb2)/tan(tA) + (-Xb2 + Xc2)/tan(tB))**2 + 1))**2*dtA z7=E+F #==================================================================================================================================================== # check() was used as a check for the error calculated in the partial derivatives above. Both methods give the same answer. # z7 = check() #==================================================================================================================================================== def joinT(yb,ya,xb,xa): dya=yb-ya+0. dxa=xb-xa+0. if (dxa==0 and dya>0): tAn=math.pi/4. return tAn elif (dxa==0 and dya<=0): tAn=(3./2.)*math.pi return tAn elif (dya==0 and dxa>=0): tAn = 0. return tAn elif (dya==0 and dxa<0): tAn = math.pi return tAn else : tAn= arctan(((dya)/dxa)) # get correct quadrant if(dya<0 and dxa>0): tAn= tAn + 2*math.pi elif(dya<0 and dxa<0): tAn= tAn + math.pi elif(dya>0 and dxa<0): tAn= tAn + math.pi return tAn #TBN def joinS(yb2,ya2,xb2,xa2): dya=yb2-ya2 dxa=xb2-xa2 sAB=sqrt(dya**2+dxa**2) return sAB #TBN def intersection(Nx,Ny,Xa,Ya,Xb,Yb,tNa,Z): tAN= tNa+math.pi sAN= joinS(Ny,Ya,Nx,Xa) eAn=Z eX= (sAN*sin(tAN))**2*(eT)**2 + (cos(tAN)**2)*eAn**2 eY= (sAN*cos(tAN))**2*(eT)**2 + (sin(tAN)**2)*eAn**2 return sqrt(eX + eY) def AddValues(): for Nx in xa: for Ny in ya: tNa=joinT(Ny,Ya2,Nx,Xa2) #join from N to A tNb=joinT(Ny,Yb2,Nx,Xb2) #join from N to B tNc=joinT(Ny,Yc2,Nx,Xc2) #join from N to C alpha=tNb-tNa beta= tNc-tNb while (alpha<0): alpha=alpha+2*math.pi while (beta<0): beta=beta+2*math.pi x.append(Nx) y.append(Ny) if ((Nx==Xa2 and Ny==Ya2) or (Nx==Xb2 and Ny==Yb2) or (Nx==Xc2 and Ny==Yc2)): z2.append(10) else : z2.append(0) Z=((z7.subs(tA,alpha).subs(tB,beta))) + 0.0 if (math.isnan(Z)): # print "N : " + str(Nx) # print "Y : " + str(Ny) # print "Z : " + str(Z) # print "Math Error" Z=threshold if (Z>threshold): Z=threshold # check for the best geometry for intersection i=intersection(Nx,Ny,Xa2,Ya2,Xb2,Yb2,tNa,Z**1.0/2) j=intersection(Nx,Ny,Xb2,Yb2,Xc2,Yc2,tNb,Z**1.0/2) k=intersection(Nx,Ny,Xc2,Yc2,Xa2,Ya2,tNc,Z**1.0/2) if (k<j and k<=i): z3.append(k ) elif (j<k and j<=i): z3.append(j ) elif (i<k and i<=j): z3.append(i) else : z3.append(k) z.append(Z**1.0/2) def Display(): del z[0] del x[0] del y[0] del z2[0] del z3[0] title1='Error Figure for Directions Accuracy to N from resection:\n threshold= ' + str(threshold) title2='Total RMS for Point N' title0='Control Points...\nClose window to continue' print "Control Points Locations" window3(x,y,z2,threshold,title0) print "===================================================================================================================================================" print "===================================================================================================================================================" print "RMS error values for direction to N Y location Pairs:" window3(x,y,z,threshold,title1) print "===================================================================================================================================================" print "===================================================================================================================================================" print "Total RMS error values for N Y location Pairs after intersection:" window3(x,y,z3,threshold,title2) xa = np.arange(0,100,5.0) ya = np.arange(0,100,5.0) z = [''] x = [''] y = [''] z2 = [''] z3 = [''] AddValues() Display()
def test_sympy__functions__elementary__trigonometric__tan(): from sympy.functions.elementary.trigonometric import tan assert _test_args(tan(2))
def test_line_geom(): x = Symbol('x', real=True) y = Symbol('y', real=True) x1 = Symbol('x1', real=True) y1 = Symbol('y1', real=True) half = Rational(1, 2) p1 = Point(0, 0) p2 = Point(1, 1) p3 = Point(x1, x1) p4 = Point(y1, y1) p5 = Point(x1, 1 + x1) p6 = Point(1, 0) p7 = Point(0, 1) p8 = Point(2, 0) p9 = Point(2, 1) l1 = Line(p1, p2) l2 = Line(p3, p4) l3 = Line(p3, p5) l4 = Line(p1, p6) l5 = Line(p1, p7) l6 = Line(p8, p9) l7 = Line(p2, p9) raises(ValueError, lambda: Line(Point(0, 0), Point(0, 0))) # Basic stuff assert Line((1, 1), slope=1) == Line((1, 1), (2, 2)) assert Line((1, 1), slope=oo) == Line((1, 1), (1, 2)) assert Line((1, 1), slope=-oo) == Line((1, 1), (1, 2)) raises(ValueError, lambda: Line((1, 1), 1)) assert Line(p1, p2) == Line(p1, p2) assert Line(p1, p2) != Line(p2, p1) assert l1 != l2 assert l1 != l3 assert l1.slope == 1 assert l1.length == oo assert l3.slope == oo assert l4.slope == 0 assert l4.coefficients == (0, 1, 0) assert l4.equation(x=x, y=y) == y assert l5.slope == oo assert l5.coefficients == (1, 0, 0) assert l5.equation() == x assert l6.equation() == x - 2 assert l7.equation() == y - 1 assert p1 in l1 # is p1 on the line l1? assert p1 not in l3 assert Line((-x, x), (-x + 1, x - 1)).coefficients == (1, 1, 0) assert simplify(l1.equation()) in (x - y, y - x) assert simplify(l3.equation()) in (x - x1, x1 - x) assert Line(p1, p2).scale(2, 1) == Line(p1, p9) assert l2.arbitrary_point() in l2 for ind in range(0, 5): assert l3.random_point() in l3 # Orthogonality p1_1 = Point(-x1, x1) l1_1 = Line(p1, p1_1) assert l1.perpendicular_line(p1.args) == Line(Point(0, 0), Point(1, -1)) assert l1.perpendicular_line(p1) == Line(Point(0, 0), Point(1, -1)) assert Line.is_perpendicular(l1, l1_1) assert Line.is_perpendicular(l1, l2) is False p = l1.random_point() assert l1.perpendicular_segment(p) == p # Parallelity l2_1 = Line(p3, p5) assert l2.parallel_line(p1_1) == Line(Point(-x1, x1), Point(-y1, 2*x1 - y1)) assert l2_1.parallel_line(p1.args) == Line(Point(0, 0), Point(0, -1)) assert l2_1.parallel_line(p1) == Line(Point(0, 0), Point(0, -1)) assert Line.is_parallel(l1, l2) assert Line.is_parallel(l2, l3) is False assert Line.is_parallel(l2, l2.parallel_line(p1_1)) assert Line.is_parallel(l2_1, l2_1.parallel_line(p1)) # Intersection assert intersection(l1, p1) == [p1] assert intersection(l1, p5) == [] assert intersection(l1, l2) in [[l1], [l2]] assert intersection(l1, l1.parallel_line(p5)) == [] # Concurrency l3_1 = Line(Point(5, x1), Point(-Rational(3, 5), x1)) assert Line.are_concurrent(l1) is False assert Line.are_concurrent(l1, l3) assert Line.are_concurrent(l1, l1, l1, l3) assert Line.are_concurrent(l1, l3, l3_1) assert Line.are_concurrent(l1, l1_1, l3) is False # Projection assert l2.projection(p4) == p4 assert l1.projection(p1_1) == p1 assert l3.projection(p2) == Point(x1, 1) raises(GeometryError, lambda: Line(Point(0, 0), Point(1, 0)) .projection(Circle(Point(0, 0), 1))) # Finding angles l1_1 = Line(p1, Point(5, 0)) assert feq(Line.angle_between(l1, l1_1).evalf(), pi.evalf()/4) # Testing Rays and Segments (very similar to Lines) assert Ray((1, 1), angle=pi/4) == Ray((1, 1), (2, 2)) assert Ray((1, 1), angle=pi/2) == Ray((1, 1), (1, 2)) assert Ray((1, 1), angle=-pi/2) == Ray((1, 1), (1, 0)) assert Ray((1, 1), angle=-3*pi/2) == Ray((1, 1), (1, 2)) assert Ray((1, 1), angle=5*pi/2) == Ray((1, 1), (1, 2)) assert Ray((1, 1), angle=5.0*pi/2) == Ray((1, 1), (1, 2)) assert Ray((1, 1), angle=pi) == Ray((1, 1), (0, 1)) assert Ray((1, 1), angle=3.0*pi) == Ray((1, 1), (0, 1)) assert Ray((1, 1), angle=4.0*pi) == Ray((1, 1), (2, 1)) assert Ray((1, 1), angle=0) == Ray((1, 1), (2, 1)) assert Ray((1, 1), angle=4.05*pi) == Ray(Point(1, 1), Point(2, -sqrt(5)*sqrt(2*sqrt(5) + 10)/4 - sqrt(2*sqrt(5) + 10)/4 + 2 + sqrt(5))) assert Ray((1, 1), angle=4.02*pi) == Ray(Point(1, 1), Point(2, 1 + tan(4.02*pi))) assert Ray((1, 1), angle=5) == Ray((1, 1), (2, 1 + tan(5))) raises(ValueError, lambda: Ray((1, 1), 1)) # issue 7963 r = Ray((0, 0), angle=x) assert r.subs(x, 3*pi/4) == Ray((0, 0), (-1, 1)) assert r.subs(x, 5*pi/4) == Ray((0, 0), (-1, -1)) assert r.subs(x, -pi/4) == Ray((0, 0), (1, -1)) assert r.subs(x, pi/2) == Ray((0, 0), (0, 1)) assert r.subs(x, -pi/2) == Ray((0, 0), (0, -1)) r1 = Ray(p1, Point(-1, 5)) r2 = Ray(p1, Point(-1, 1)) r3 = Ray(p3, p5) r4 = Ray(p1, p2) r5 = Ray(p2, p1) r6 = Ray(Point(0, 1), Point(1, 2)) r7 = Ray(Point(0.5, 0.5), Point(1, 1)) assert l1.projection(r1) == Ray(Point(0, 0), Point(2, 2)) assert l1.projection(r2) == p1 assert r3 != r1 t = Symbol('t', real=True) assert Ray((1, 1), angle=pi/4).arbitrary_point() == \ Point(t + 1, t + 1) r8 = Ray(Point(0, 0), Point(0, 4)) r9 = Ray(Point(0, 1), Point(0, -1)) assert r8.intersection(r9) == [Segment(Point(0, 0), Point(0, 1))] s1 = Segment(p1, p2) s2 = Segment(p1, p1_1) assert s1.midpoint == Point(Rational(1, 2), Rational(1, 2)) assert s2.length == sqrt( 2*(x1**2) ) assert Segment((1, 1), (2, 3)).arbitrary_point() == Point(1 + t, 1 + 2*t) assert s1.perpendicular_bisector() == \ Line(Point(1/2, 1/2), Point(3/2, -1/2)) # intersections assert s1.intersection(Line(p6, p9)) == [] s3 = Segment(Point(0.25, 0.25), Point(0.5, 0.5)) assert s1.intersection(s3) == [s1] assert s3.intersection(s1) == [s3] assert r4.intersection(s3) == [s3] assert r4.intersection(Segment(Point(2, 3), Point(3, 4))) == [] assert r4.intersection(Segment(Point(-1, -1), Point(0.5, 0.5))) == \ [Segment(p1, Point(0.5, 0.5))] s3 = Segment(Point(1, 1), Point(2, 2)) assert s1.intersection(s3) == [Point(1, 1)] s3 = Segment(Point(0.5, 0.5), Point(1.5, 1.5)) assert s1.intersection(s3) == [Segment(Point(0.5, 0.5), p2)] assert s1.intersection(Segment(Point(4, 4), Point(5, 5))) == [] assert s1.intersection(Segment(Point(-1, -1), p1)) == [p1] assert s1.intersection(Segment(Point(-1, -1), Point(0.5, 0.5))) == \ [Segment(p1, Point(0.5, 0.5))] assert r4.intersection(r5) == [s1] assert r5.intersection(r6) == [] assert r4.intersection(r7) == r7.intersection(r4) == [r7] # Segment contains a, b = symbols('a,b') s = Segment((0, a), (0, b)) assert Point(0, (a + b)/2) in s s = Segment((a, 0), (b, 0)) assert Point((a + b)/2, 0) in s raises(Undecidable, lambda: Point(2*a, 0) in s) # Testing distance from a Segment to an object s1 = Segment(Point(0, 0), Point(1, 1)) s2 = Segment(Point(half, half), Point(1, 0)) pt1 = Point(0, 0) pt2 = Point(Rational(3)/2, Rational(3)/2) assert s1.distance(pt1) == 0 assert s1.distance((0, 0)) == 0 assert s2.distance(pt1) == 2**(half)/2 assert s2.distance(pt2) == 2**(half) # Line to point p1, p2 = Point(0, 0), Point(1, 1) s = Line(p1, p2) assert s.distance(Point(-1, 1)) == sqrt(2) assert s.distance(Point(1, -1)) == sqrt(2) assert s.distance(Point(2, 2)) == 0 assert s.distance((-1, 1)) == sqrt(2) assert Line((0, 0), (0, 1)).distance(p1) == 0 assert Line((0, 0), (0, 1)).distance(p2) == 1 assert Line((0, 0), (1, 0)).distance(p1) == 0 assert Line((0, 0), (1, 0)).distance(p2) == 1 m = symbols('m') l = Line((0, 5), slope=m) p = Point(2, 3) assert l.distance(p) == 2*abs(m + 1)/sqrt(m**2 + 1) # Ray to point r = Ray(p1, p2) assert r.distance(Point(-1, -1)) == sqrt(2) assert r.distance(Point(1, 1)) == 0 assert r.distance(Point(-1, 1)) == sqrt(2) assert Ray((1, 1), (2, 2)).distance(Point(1.5, 3)) == 3*sqrt(2)/4 assert r.distance((1, 1)) == 0 #Line contains p1, p2 = Point(0, 1), Point(3, 4) l = Line(p1, p2) assert l.contains(p1) is True assert l.contains((0, 1)) is True assert l.contains((0, 0)) is False #Ray contains p1, p2 = Point(0, 0), Point(4, 4) r = Ray(p1, p2) assert r.contains(p1) is True assert r.contains((1, 1)) is True assert r.contains((1, 3)) is False s = Segment((1, 1), (2, 2)) assert r.contains(s) is True s = Segment((1, 2), (2, 5)) assert r.contains(s) is False r1 = Ray((2, 2), (3, 3)) assert r.contains(r1) is True r1 = Ray((2, 2), (3, 5)) assert r.contains(r1) is False # Special cases of projection and intersection r1 = Ray(Point(1, 1), Point(2, 2)) r2 = Ray(Point(2, 2), Point(0, 0)) r3 = Ray(Point(1, 1), Point(-1, -1)) r4 = Ray(Point(0, 4), Point(-1, -5)) r5 = Ray(Point(2, 2), Point(3, 3)) assert intersection(r1, r2) == [Segment(Point(1, 1), Point(2, 2))] assert intersection(r1, r3) == [Point(1, 1)] assert r1.projection(r3) == Point(1, 1) assert r1.projection(r4) == Segment(Point(1, 1), Point(2, 2)) r5 = Ray(Point(0, 0), Point(0, 1)) r6 = Ray(Point(0, 0), Point(0, 2)) assert r5 in r6 assert r6 in r5 s1 = Segment(Point(0, 0), Point(2, 2)) s2 = Segment(Point(-1, 5), Point(-5, -10)) s3 = Segment(Point(0, 4), Point(-2, 2)) assert intersection(r1, s1) == [Segment(Point(1, 1), Point(2, 2))] assert r1.projection(s2) == Segment(Point(1, 1), Point(2, 2)) assert s3.projection(r1) == Segment(Point(0, 4), Point(-1, 3)) l1 = Line(Point(0, 0), Point(3, 4)) r1 = Ray(Point(0, 0), Point(3, 4)) s1 = Segment(Point(0, 0), Point(3, 4)) assert intersection(l1, l1) == [l1] assert intersection(l1, r1) == [r1] assert intersection(l1, s1) == [s1] assert intersection(r1, l1) == [r1] assert intersection(s1, l1) == [s1] entity1 = Segment(Point(-10, 10), Point(10, 10)) entity2 = Segment(Point(-5, -5), Point(-5, 5)) assert intersection(entity1, entity2) == [] r1 = Ray(p1, Point(0, 1)) r2 = Ray(Point(0, 1), p1) r3 = Ray(p1, p2) r4 = Ray(p2, p1) s1 = Segment(p1, Point(0, 1)) assert Line(r1.source, r1.random_point()).slope == r1.slope assert Line(r2.source, r2.random_point()).slope == r2.slope assert Segment(Point(0, -1), s1.random_point()).slope == s1.slope p_r3 = r3.random_point() p_r4 = r4.random_point() assert p_r3.x >= p1.x and p_r3.y >= p1.y assert p_r4.x <= p2.x and p_r4.y <= p2.y p10 = Point(2000, 2000) s1 = Segment(p1, p10) p_s1 = s1.random_point() assert p1.x <= p_s1.x and p_s1.x <= p10.x and \ p1.y <= p_s1.y and p_s1.y <= p10.y s2 = Segment(p10, p1) assert hash(s1) == hash(s2) p11 = p10.scale(2, 2) assert s1.is_similar(Segment(p10, p11)) assert s1.is_similar(r1) is False assert (r1 in s1) is False assert Segment(p1, p2) in s1 assert s1.plot_interval() == [t, 0, 1] assert s1 in Line(p1, p10) assert Line(p1, p10) != Line(p10, p1) assert Line(p1, p10) != p1 assert Line(p1, p10).plot_interval() == [t, -5, 5] assert Ray((0, 0), angle=pi/4).plot_interval() == \ [t, 0, 10]
def test_polygon(): a, b, c = Point(0, 0), Point(2, 0), Point(3, 3) t = Triangle(a, b, c) assert Polygon(a, Point(1, 0), b, c) == t assert Polygon(Point(1, 0), b, c, a) == t assert Polygon(b, c, a, Point(1, 0)) == t # 2 "remove folded" tests assert Polygon(a, Point(3, 0), b, c) == t assert Polygon(a, b, Point(3, -1), b, c) == t raises(GeometryError, lambda: Polygon((0, 0), (1, 0), (0, 1), (1, 1))) # remove multiple collinear points assert Polygon( Point(-4, 15), Point(-11, 15), Point(-15, 15), Point(-15, 33 / 5), Point(-15, -87 / 10), Point(-15, -15), Point(-42 / 5, -15), Point(-2, -15), Point(7, -15), Point(15, -15), Point(15, -3), Point(15, 10), Point(15, 15), ) == Polygon(Point(-15, -15), Point(15, -15), Point(15, 15), Point(-15, 15)) p1 = Polygon(Point(0, 0), Point(3, -1), Point(6, 0), Point(4, 5), Point(2, 3), Point(0, 3)) p2 = Polygon(Point(6, 0), Point(3, -1), Point(0, 0), Point(0, 3), Point(2, 3), Point(4, 5)) p3 = Polygon(Point(0, 0), Point(3, 0), Point(5, 2), Point(4, 4)) p4 = Polygon(Point(0, 0), Point(4, 4), Point(5, 2), Point(3, 0)) p5 = Polygon(Point(0, 0), Point(4, 4), Point(0, 4)) p6 = Polygon(Point(-11, 1), Point(-9, 6.6), Point(-4, -3), Point(-8.4, -8.7)) r = Ray(Point(-9, 6.6), Point(-9, 5.5)) # # General polygon # assert p1 == p2 assert len(p1.args) == 6 assert len(p1.sides) == 6 assert p1.perimeter == 5 + 2 * sqrt(10) + sqrt(29) + sqrt(8) assert p1.area == 22 assert not p1.is_convex() # ensure convex for both CW and CCW point specification assert p3.is_convex() assert p4.is_convex() dict5 = p5.angles assert dict5[Point(0, 0)] == pi / 4 assert dict5[Point(0, 4)] == pi / 2 assert p5.encloses_point(Point(x, y)) is None assert p5.encloses_point(Point(1, 3)) assert p5.encloses_point(Point(0, 0)) is False assert p5.encloses_point(Point(4, 0)) is False p5.plot_interval("x") == [x, 0, 1] assert p5.distance(Polygon(Point(10, 10), Point(14, 14), Point(10, 14))) == 6 * sqrt(2) assert p5.distance(Polygon(Point(1, 8), Point(5, 8), Point(8, 12), Point(1, 12))) == 4 warnings.filterwarnings("error", message="Polygons may intersect producing erroneous output") raises( UserWarning, lambda: Polygon(Point(0, 0), Point(1, 0), Point(1, 1)).distance(Polygon(Point(0, 0), Point(0, 1), Point(1, 1))), ) warnings.filterwarnings("ignore", message="Polygons may intersect producing erroneous output") assert hash(p5) == hash(Polygon(Point(0, 0), Point(4, 4), Point(0, 4))) assert p5 == Polygon(Point(4, 4), Point(0, 4), Point(0, 0)) assert Polygon(Point(4, 4), Point(0, 4), Point(0, 0)) in p5 assert p5 != Point(0, 4) assert Point(0, 1) in p5 assert p5.arbitrary_point("t").subs(Symbol("t", real=True), 0) == Point(0, 0) raises(ValueError, lambda: Polygon(Point(x, 0), Point(0, y), Point(x, y)).arbitrary_point("x")) assert p6.intersection(r) == [Point(-9, 33 / 5), Point(-9, -84 / 13)] # # Regular polygon # p1 = RegularPolygon(Point(0, 0), 10, 5) p2 = RegularPolygon(Point(0, 0), 5, 5) raises(GeometryError, lambda: RegularPolygon(Point(0, 0), Point(0, 1), Point(1, 1))) raises(GeometryError, lambda: RegularPolygon(Point(0, 0), 1, 2)) raises(ValueError, lambda: RegularPolygon(Point(0, 0), 1, 2.5)) assert p1 != p2 assert p1.interior_angle == 3 * pi / 5 assert p1.exterior_angle == 2 * pi / 5 assert p2.apothem == 5 * cos(pi / 5) assert p2.circumcenter == p1.circumcenter == Point(0, 0) assert p1.circumradius == p1.radius == 10 assert p2.circumcircle == Circle(Point(0, 0), 5) assert p2.incircle == Circle(Point(0, 0), p2.apothem) assert p2.inradius == p2.apothem == (5 * (1 + sqrt(5)) / 4) p2.spin(pi / 10) dict1 = p2.angles assert dict1[Point(0, 5)] == 3 * pi / 5 assert p1.is_convex() assert p1.rotation == 0 assert p1.encloses_point(Point(0, 0)) assert p1.encloses_point(Point(11, 0)) is False assert p2.encloses_point(Point(0, 4.9)) p1.spin(pi / 3) assert p1.rotation == pi / 3 assert p1.vertices[0] == Point(5, 5 * sqrt(3)) for var in p1.args: if isinstance(var, Point): assert var == Point(0, 0) else: assert var == 5 or var == 10 or var == pi / 3 assert p1 != Point(0, 0) assert p1 != p5 # while spin works in place (notice that rotation is 2pi/3 below) # rotate returns a new object p1_old = p1 assert p1.rotate(pi / 3) == RegularPolygon(Point(0, 0), 10, 5, 2 * pi / 3) assert p1 == p1_old assert p1.area == (-250 * sqrt(5) + 1250) / (4 * tan(pi / 5)) assert p1.length == 20 * sqrt(-sqrt(5) / 8 + 5 / 8) assert p1.scale(2, 2) == RegularPolygon(p1.center, p1.radius * 2, p1._n, p1.rotation) assert RegularPolygon((0, 0), 1, 4).scale(2, 3) == Polygon(Point(2, 0), Point(0, 3), Point(-2, 0), Point(0, -3)) assert repr(p1) == str(p1) # # Angles # angles = p4.angles assert feq(angles[Point(0, 0)].evalf(), Float("0.7853981633974483")) assert feq(angles[Point(4, 4)].evalf(), Float("1.2490457723982544")) assert feq(angles[Point(5, 2)].evalf(), Float("1.8925468811915388")) assert feq(angles[Point(3, 0)].evalf(), Float("2.3561944901923449")) angles = p3.angles assert feq(angles[Point(0, 0)].evalf(), Float("0.7853981633974483")) assert feq(angles[Point(4, 4)].evalf(), Float("1.2490457723982544")) assert feq(angles[Point(5, 2)].evalf(), Float("1.8925468811915388")) assert feq(angles[Point(3, 0)].evalf(), Float("2.3561944901923449")) # # Triangle # p1 = Point(0, 0) p2 = Point(5, 0) p3 = Point(0, 5) t1 = Triangle(p1, p2, p3) t2 = Triangle(p1, p2, Point(Rational(5, 2), sqrt(Rational(75, 4)))) t3 = Triangle(p1, Point(x1, 0), Point(0, x1)) s1 = t1.sides assert Triangle(p1, p2, p1) == Polygon(p1, p2, p1) == Segment(p1, p2) raises(GeometryError, lambda: Triangle(Point(0, 0))) # Basic stuff assert Triangle(p1, p1, p1) == p1 assert Triangle(p2, p2 * 2, p2 * 3) == Segment(p2, p2 * 3) assert t1.area == Rational(25, 2) assert t1.is_right() assert t2.is_right() is False assert t3.is_right() assert p1 in t1 assert t1.sides[0] in t1 assert Segment((0, 0), (1, 0)) in t1 assert Point(5, 5) not in t2 assert t1.is_convex() assert feq(t1.angles[p1].evalf(), pi.evalf() / 2) assert t1.is_equilateral() is False assert t2.is_equilateral() assert t3.is_equilateral() is False assert are_similar(t1, t2) is False assert are_similar(t1, t3) assert are_similar(t2, t3) is False assert t1.is_similar(Point(0, 0)) is False # Bisectors bisectors = t1.bisectors() assert bisectors[p1] == Segment(p1, Point(Rational(5, 2), Rational(5, 2))) ic = (250 - 125 * sqrt(2)) / 50 assert t1.incenter == Point(ic, ic) # Inradius assert t1.inradius == t1.incircle.radius == 5 - 5 * sqrt(2) / 2 assert t2.inradius == t2.incircle.radius == 5 * sqrt(3) / 6 assert t3.inradius == t3.incircle.radius == x1 ** 2 / ((2 + sqrt(2)) * Abs(x1)) # Circumcircle assert t1.circumcircle.center == Point(2.5, 2.5) # Medians + Centroid m = t1.medians assert t1.centroid == Point(Rational(5, 3), Rational(5, 3)) assert m[p1] == Segment(p1, Point(Rational(5, 2), Rational(5, 2))) assert t3.medians[p1] == Segment(p1, Point(x1 / 2, x1 / 2)) assert intersection(m[p1], m[p2], m[p3]) == [t1.centroid] assert t1.medial == Triangle(Point(2.5, 0), Point(0, 2.5), Point(2.5, 2.5)) # Perpendicular altitudes = t1.altitudes assert altitudes[p1] == Segment(p1, Point(Rational(5, 2), Rational(5, 2))) assert altitudes[p2] == s1[0] assert altitudes[p3] == s1[2] assert t1.orthocenter == p1 t = S( """Triangle( Point(100080156402737/5000000000000, 79782624633431/500000000000), Point(39223884078253/2000000000000, 156345163124289/1000000000000), Point(31241359188437/1250000000000, 338338270939941/1000000000000000))""" ) assert t.orthocenter == S( """Point(-780660869050599840216997""" """79471538701955848721853/80368430960602242240789074233100000000000000,""" """20151573611150265741278060334545897615974257/16073686192120448448157""" """8148466200000000000)""" ) # Ensure assert len(intersection(*bisectors.values())) == 1 assert len(intersection(*altitudes.values())) == 1 assert len(intersection(*m.values())) == 1 # Distance p1 = Polygon(Point(0, 0), Point(1, 0), Point(1, 1), Point(0, 1)) p2 = Polygon( Point(0, Rational(5) / 4), Point(1, Rational(5) / 4), Point(1, Rational(9) / 4), Point(0, Rational(9) / 4) ) p3 = Polygon(Point(1, 2), Point(2, 2), Point(2, 1)) p4 = Polygon(Point(1, 1), Point(Rational(6) / 5, 1), Point(1, Rational(6) / 5)) pt1 = Point(half, half) pt2 = Point(1, 1) """Polygon to Point""" assert p1.distance(pt1) == half assert p1.distance(pt2) == 0 assert p2.distance(pt1) == Rational(3) / 4 assert p3.distance(pt2) == sqrt(2) / 2 """Polygon to Polygon""" # p1.distance(p2) emits a warning # First, test the warning warnings.filterwarnings("error", message="Polygons may intersect producing erroneous output") raises(UserWarning, lambda: p1.distance(p2)) # now test the actual output warnings.filterwarnings("ignore", message="Polygons may intersect producing erroneous output") assert p1.distance(p2) == half / 2 assert p1.distance(p3) == sqrt(2) / 2 assert p3.distance(p4) == (sqrt(2) / 2 - sqrt(Rational(2) / 25) / 2)