Beispiel #1
0
def test_re():
    a, b = symbols('a,b', extended_real=True)

    r = Symbol('r', extended_real=True)
    i = Symbol('i', imaginary=True)

    assert re(nan) == nan

    assert re(oo) == oo
    assert re(-oo) == -oo

    assert re(0) == 0

    assert re(1) == 1
    assert re(-1) == -1

    assert re(E) == E
    assert re(-E) == -E

    assert re(x) == re(x)
    assert re(x*I) == -im(x)
    assert re(r*I) == 0
    assert re(r) == r
    assert re(i*I) == I * i
    assert re(i) == 0

    assert re(x + y) == re(x + y)
    assert re(x + r) == re(x) + r

    assert re(re(x)) == re(x)

    assert re(2 + I) == 2
    assert re(x + I) == re(x)

    assert re(x + y*I) == re(x) - im(y)
    assert re(x + r*I) == re(x)

    assert re(log(2*I)) == log(2)

    assert re((2 + I)**2).expand(complex=True) == 3

    assert re(conjugate(x)) == re(x)
    assert conjugate(re(x)) == re(x)

    assert re(x).as_real_imag() == (re(x), 0)

    assert re(i*r*x).diff(r) == re(i*x)
    assert re(i*r*x).diff(i) == I*r*im(x)

    assert re(sqrt(a + b*I)) == root(a**2 + b**2, 4)*cos(arg(a + I*b)/2)
    assert re(a * (2 + b*I)) == 2*a

    assert re((1 + sqrt(a + b*I))/2) == root(a**2 + b**2, 4)*cos(arg(a + I*b)/2)/2 + Rational(1, 2)

    assert re(x).rewrite(im) == x - I*im(x)  # issue sympy/sympy#10897
    assert (x + re(y)).rewrite(re, im) == x + y - I*im(y)

    a = Symbol('a', algebraic=True)
    t = Symbol('t', transcendental=True)
    assert re(a).is_algebraic
    assert re(x).is_algebraic is None
    assert re(t).is_algebraic is False

    assert re(zoo) == nan
Beispiel #2
0
x = Symbol('x', extended_real=True)
y = Symbol('y', extended_real=True)
z = Symbol('z', extended_real=True)
t = Symbol('t', extended_real=True)
k = Symbol('k', extended_real=True)
x1 = Symbol('x1', extended_real=True)
x2 = Symbol('x2', extended_real=True)
x3 = Symbol('x3', extended_real=True)
y1 = Symbol('y1', extended_real=True)
y2 = Symbol('y2', extended_real=True)
y3 = Symbol('y3', extended_real=True)
z1 = Symbol('z1', extended_real=True)
z2 = Symbol('z2', extended_real=True)
z3 = Symbol('z3', extended_real=True)
half = Rational(1, 2)


def feq(a, b):
    """Test if two floating point values are 'equal'."""
    t = Float("1.0E-10")
    return -t < a - b < t


def test_curve():
    s = Symbol('s')
    z = Symbol('z')

    # this curve is independent of the indicated parameter
    c = Curve([2*s, s**2], (z, 0, 2))
Beispiel #3
0
def test_ellipse_geom():
    p1 = Point(0, 0)
    p2 = Point(1, 1)
    p4 = Point(0, 1)

    e1 = Ellipse(p1, 1, 1)
    e2 = Ellipse(p2, half, 1)
    e3 = Ellipse(p1, y1, y1)
    c1 = Circle(p1, 1)
    c2 = Circle(p2, 1)
    c3 = Circle(Point(sqrt(2), sqrt(2)), 1)

    pytest.raises(ValueError, lambda: Ellipse(Point3D(0, 0, 0), 1, 1))
    pytest.raises(ValueError, lambda: e3.arbitrary_point(y1))

    assert e1.ambient_dimension == 2

    # Test creation with three points
    cen, rad = Point(3*half, 2), 5*half
    assert Circle(Point(0, 0), Point(3, 0), Point(0, 4)) == Circle(cen, rad)
    pytest.raises(
        GeometryError, lambda: Circle(Point(0, 0), Point(1, 1), Point(2, 2)))

    pytest.raises(ValueError, lambda: Ellipse(None, None, None, 1))
    pytest.raises(GeometryError, lambda: Circle(Point(0, 0)))

    # Basic Stuff
    assert Ellipse(None, 1, 1).center == Point(0, 0)
    assert e1 == c1
    assert e1 != e2
    assert p4 in e1
    assert p2 not in e2
    assert e1.area == pi
    assert e2.area == pi/2
    assert e3.area == pi*y1*abs(y1)
    assert c1.area == e1.area
    assert c1.circumference == e1.circumference
    assert e3.circumference == 2*pi*y1
    assert e1.plot_interval() == e2.plot_interval() == [t, -pi, pi]
    assert e1.plot_interval(x) == e2.plot_interval(x) == [x, -pi, pi]
    assert Ellipse(None, 1, None, 1).circumference == 2*pi
    assert c1.minor == 1
    assert c1.major == 1
    assert c1.hradius == 1
    assert c1.vradius == 1

    # Private Functions
    assert hash(c1) == hash(Circle(Point(1, 0), Point(0, 1), Point(0, -1)))
    assert c1 in e1
    assert (Line(p1, p2) in e1) is False

    # Encloses
    assert e1.encloses(Segment(Point(-0.5, -0.5), Point(0.5, 0.5))) is True
    assert e1.encloses(Line(p1, p2)) is False
    assert e1.encloses(Ray(p1, p2)) is False
    assert e1.encloses(e1) is False
    assert e1.encloses(
        Polygon(Point(-0.5, -0.5), Point(-0.5, 0.5), Point(0.5, 0.5))) is True
    assert e1.encloses(RegularPolygon(p1, 0.5, 3)) is True
    assert e1.encloses(RegularPolygon(p1, 5, 3)) is False
    assert e1.encloses(RegularPolygon(p2, 5, 3)) is False

    # with generic symbols, the hradius is assumed to contain the major radius
    M = Symbol('M')
    m = Symbol('m')
    c = Ellipse(p1, M, m).circumference
    _x = c.atoms(Dummy).pop()
    assert c == 4*M*Integral(
        sqrt((1 - _x**2*(M**2 - m**2)/M**2)/(1 - _x**2)), (_x, 0, 1))

    assert e2.arbitrary_point() in e2

    # Foci
    f1, f2 = Point(sqrt(12), 0), Point(-sqrt(12), 0)
    ef = Ellipse(Point(0, 0), 4, 2)
    assert ef.foci in [(f1, f2), (f2, f1)]

    # Tangents
    v = sqrt(2) / 2
    p1_1 = Point(v, v)
    p1_2 = p2 + Point(half, 0)
    p1_3 = p2 + Point(0, 1)
    assert e1.tangent_lines(p4) == c1.tangent_lines(p4)
    assert e2.tangent_lines(p1_2) == [Line(Point(3/2, 1), Point(3/2, 1/2))]
    assert e2.tangent_lines(p1_3) == [Line(Point(1, 2), Point(5/4, 2))]
    assert c1.tangent_lines(p1_1) != [Line(p1_1, Point(0, sqrt(2)))]
    assert c1.tangent_lines(p1) == []
    assert e2.is_tangent(Line(p1_2, p2 + Point(half, 1)))
    assert e2.is_tangent(Line(p1_3, p2 + Point(half, 1)))
    assert c1.is_tangent(Line(p1_1, Point(0, sqrt(2))))
    assert e1.is_tangent(Line(Point(0, 0), Point(1, 1))) is False
    assert c1.is_tangent(e1) is False
    assert c1.is_tangent(Ellipse(Point(2, 0), 1, 1)) is True
    assert c1.is_tangent(
        Polygon(Point(1, 1), Point(1, -1), Point(2, 0))) is True
    assert c1.is_tangent(
        Polygon(Point(1, 1), Point(1, 0), Point(2, 0))) is False
    assert Circle(Point(5, 5), 3).is_tangent(Circle(Point(0, 5), 1)) is False

    assert Ellipse(Point(5, 5), 2, 1).tangent_lines(Point(0, 0)) == \
        [Line(Point(0, 0), Point(77/25, 132/25)),
         Line(Point(0, 0), Point(33/5, 22/5))]
    assert Ellipse(Point(5, 5), 2, 1).tangent_lines(Point(3, 4)) == \
        [Line(Point(3, 4), Point(4, 4)), Line(Point(3, 4), Point(3, 5))]
    assert Circle(Point(5, 5), 2).tangent_lines(Point(3, 3)) == \
        [Line(Point(3, 3), Point(4, 3)), Line(Point(3, 3), Point(3, 4))]
    assert Circle(Point(5, 5), 2).tangent_lines(Point(5 - 2*sqrt(2), 5)) == \
        [Line(Point(5 - 2*sqrt(2), 5), Point(5 - sqrt(2), 5 - sqrt(2))),
         Line(Point(5 - 2*sqrt(2), 5), Point(5 - sqrt(2), 5 + sqrt(2))), ]

    e = Ellipse(Point(0, 0), 2, 1)
    assert e.normal_lines(Point(0, 0)) == \
        [Line(Point(0, 0), Point(0, 1)), Line(Point(0, 0), Point(1, 0))]
    assert e.normal_lines(Point(1, 0)) == \
        [Line(Point(0, 0), Point(1, 0))]
    assert e.normal_lines((0, 1)) == \
        [Line(Point(0, 0), Point(0, 1))]
    assert e.normal_lines(Point(1, 1), 2) == [
        Line(Point(-51/26, -1/5), Point(-25/26, 17/83)),
        Line(Point(28/29, -7/8), Point(57/29, -9/2))]
    # test the failure of Poly.intervals and checks a point on the boundary
    p = Point(sqrt(3), Rational(1, 2))
    assert p in e
    assert e.normal_lines(p, 2) == [
        Line(Point(-341/171, -1/13), Point(-170/171, 5/64)),
        Line(Point(26/15, -1/2), Point(41/15, -43/26))]
    # be sure to use the slope that isn't undefined on boundary
    e = Ellipse((0, 0), 2, 2*sqrt(3)/3)
    assert e.normal_lines((1, 1), 2) == [
        Line(Point(-64/33, -20/71), Point(-31/33, 2/13)),
        Line(Point(1, -1), Point(2, -4))]
    # general ellipse fails except under certain conditions
    e = Ellipse((0, 0), x, 1)
    assert e.normal_lines((x + 1, 0)) == [Line(Point(0, 0), Point(1, 0))]
    pytest.raises(NotImplementedError, lambda: e.normal_lines((x + 1, 1)))

    # Properties
    major = 3
    minor = 1
    e4 = Ellipse(p2, minor, major)
    assert e4.focus_distance == sqrt(major**2 - minor**2)
    ecc = e4.focus_distance / major
    assert e4.eccentricity == ecc
    assert e4.periapsis == major*(1 - ecc)
    assert e4.apoapsis == major*(1 + ecc)
    # independent of orientation
    e4 = Ellipse(p2, major, minor)
    assert e4.focus_distance == sqrt(major**2 - minor**2)
    ecc = e4.focus_distance / major
    assert e4.eccentricity == ecc
    assert e4.periapsis == major*(1 - ecc)
    assert e4.apoapsis == major*(1 + ecc)

    # Intersection
    l1 = Line(Point(1, -5), Point(1, 5))
    l2 = Line(Point(-5, -1), Point(5, -1))
    l3 = Line(Point(-1, -1), Point(1, 1))
    l4 = Line(Point(-10, 0), Point(0, 10))
    pts_c1_l3 = [Point(sqrt(2)/2, sqrt(2)/2), Point(-sqrt(2)/2, -sqrt(2)/2)]

    assert intersection(e2, l4) == []
    assert intersection(c1, Point(1, 0)) == [Point(1, 0)]
    assert intersection(c1, l1) == [Point(1, 0)]
    assert intersection(c1, l2) == [Point(0, -1)]
    assert intersection(c1, l3) in [pts_c1_l3, [pts_c1_l3[1], pts_c1_l3[0]]]
    assert intersection(c1, c2) == [Point(0, 1), Point(1, 0)]
    assert intersection(c1, c3) == [Point(sqrt(2)/2, sqrt(2)/2)]
    assert e1.intersection(l1) == [Point(1, 0)]
    assert e2.intersection(l4) == []
    assert e1.intersection(Circle(Point(0, 2), 1)) == [Point(0, 1)]
    assert e1.intersection(Circle(Point(5, 0), 1)) == []
    assert e1.intersection(Ellipse(Point(2, 0), 1, 1)) == [Point(1, 0)]
    assert e1.intersection(Ellipse(Point(5, 0), 1, 1,)) == []
    assert e1.intersection(Point(2, 0)) == []
    assert e1.intersection(e1) == e1

    # some special case intersections
    csmall = Circle(p1, 3)
    cbig = Circle(p1, 5)
    cout = Circle(Point(5, 5), 1)
    # one circle inside of another
    assert csmall.intersection(cbig) == []
    # separate circles
    assert csmall.intersection(cout) == []
    # coincident circles
    assert csmall.intersection(csmall) == csmall

    v = sqrt(2)
    t1 = Triangle(Point(0, v), Point(0, -v), Point(v, 0))
    points = intersection(t1, c1)
    assert len(points) == 4
    assert Point(0, 1) in points
    assert Point(0, -1) in points
    assert Point(v/2, v/2) in points
    assert Point(v/2, -v/2) in points

    circ = Circle(Point(0, 0), 5)
    elip = Ellipse(Point(0, 0), 5, 20)
    assert intersection(circ, elip) in \
        [[Point(5, 0), Point(-5, 0)], [Point(-5, 0), Point(5, 0)]]
    assert elip.tangent_lines(Point(0, 0)) == []
    elip = Ellipse(Point(0, 0), 3, 2)
    assert elip.tangent_lines(Point(3, 0)) == \
        [Line(Point(3, 0), Point(3, -12))]

    e1 = Ellipse(Point(0, 0), 5, 10)
    e2 = Ellipse(Point(2, 1), 4, 8)
    a = 53/17
    c = 2*sqrt(3991)/17
    ans = [Point(a - c/8, a/2 + c), Point(a + c/8, a/2 - c)]
    assert e1.intersection(e2) == ans
    e2 = Ellipse(Point(x, y), 4, 8)
    c = sqrt(3991)
    ans = [Point(-c/68 + a, 2*c/17 + a/2), Point(c/68 + a, -2*c/17 + a/2)]
    assert [p.subs({x: 2, y: 1}) for p in e1.intersection(e2)] == ans

    # Combinations of above
    assert e3.is_tangent(e3.tangent_lines(p1 + Point(y1, 0))[0])

    e = Ellipse((1, 2), 3, 2)
    assert e.tangent_lines(Point(10, 0)) == \
        [Line(Point(10, 0), Point(1, 0)),
         Line(Point(10, 0), Point(14/5, 18/5))]

    # encloses_point
    e = Ellipse((0, 0), 1, 2)
    assert e.encloses_point(e.center)
    assert e.encloses_point(e.center + Point(0, e.vradius - Rational(1, 10)))
    assert e.encloses_point(e.center + Point(e.hradius - Rational(1, 10), 0))
    assert e.encloses_point(e.center + Point(e.hradius, 0)) is False
    assert e.encloses_point(
        e.center + Point(e.hradius + Rational(1, 10), 0)) is False
    e = Ellipse((0, 0), 2, 1)
    assert e.encloses_point(e.center)
    assert e.encloses_point(e.center + Point(0, e.vradius - Rational(1, 10)))
    assert e.encloses_point(e.center + Point(e.hradius - Rational(1, 10), 0))
    assert e.encloses_point(e.center + Point(e.hradius, 0)) is False
    assert e.encloses_point(
        e.center + Point(e.hradius + Rational(1, 10), 0)) is False
    assert c1.encloses_point(Point(1, 0)) is False
    assert c1.encloses_point(Point(0.3, 0.4)) is True

    assert e.scale(2, 3) == Ellipse((0, 0), 4, 3)
    assert e.scale(3, 6) == Ellipse((0, 0), 6, 6)
    assert e.rotate(pi) == e
    assert e.rotate(pi, (1, 2)) == Ellipse(Point(2, 4), 2, 1)
    pytest.raises(NotImplementedError, lambda: e.rotate(pi/3))

    # Circle rotation tests (issue sympy/sympy#11743)
    cir = Circle(Point(1, 0), 1)
    assert cir.rotate(pi/2) == Circle(Point(0, 1), 1)
    assert cir.rotate(pi/3) == Circle(Point(Rational(1, 2), sqrt(3)/2), 1)
    assert cir.rotate(pi/3, Point(1, 0)) == Circle(Point(1, 0), 1)
    assert cir.rotate(pi/3, Point(0, 1)) == Circle(Point(Rational(1, 2) + sqrt(3)/2,
                                                         Rational(1, 2) + sqrt(3)/2), 1)

    # transformations
    c = Circle((1, 1), 2)
    assert c.scale(-1) == Circle((-1, 1), 2)
    assert c.scale(y=-1) == Circle((1, -1), 2)
    assert c.scale(2) == Ellipse((2, 1), 4, 2)
Beispiel #4
0
def test_Domain__contains__():
    assert (0 in EX) is True
    assert (0 in ZZ) is True
    assert (0 in QQ) is True
    assert (0 in RR) is True
    assert (0 in CC) is True
    assert (0 in ALG) is True
    assert (0 in ZZ.inject(x, y)) is True
    assert (0 in QQ.inject(x, y)) is True
    assert (0 in RR.inject(x, y)) is True

    assert (-7 in EX) is True
    assert (-7 in ZZ) is True
    assert (-7 in QQ) is True
    assert (-7 in RR) is True
    assert (-7 in CC) is True
    assert (-7 in ALG) is True
    assert (-7 in ZZ.inject(x, y)) is True
    assert (-7 in QQ.inject(x, y)) is True
    assert (-7 in RR.inject(x, y)) is True

    assert (17 in EX) is True
    assert (17 in ZZ) is True
    assert (17 in QQ) is True
    assert (17 in RR) is True
    assert (17 in CC) is True
    assert (17 in ALG) is True
    assert (17 in ZZ.inject(x, y)) is True
    assert (17 in QQ.inject(x, y)) is True
    assert (17 in RR.inject(x, y)) is True

    assert (-Rational(1, 7) in EX) is True
    assert (-Rational(1, 7) in ZZ) is False
    assert (-Rational(1, 7) in QQ) is True
    assert (-Rational(1, 7) in RR) is True
    assert (-Rational(1, 7) in CC) is True
    assert (-Rational(1, 7) in ALG) is True
    assert (-Rational(1, 7) in ZZ.inject(x, y)) is False
    assert (-Rational(1, 7) in QQ.inject(x, y)) is True
    assert (-Rational(1, 7) in RR.inject(x, y)) is True

    assert (Rational(3, 5) in EX) is True
    assert (Rational(3, 5) in ZZ) is False
    assert (Rational(3, 5) in QQ) is True
    assert (Rational(3, 5) in RR) is True
    assert (Rational(3, 5) in CC) is True
    assert (Rational(3, 5) in ALG) is True
    assert (Rational(3, 5) in ZZ.inject(x, y)) is False
    assert (Rational(3, 5) in QQ.inject(x, y)) is True
    assert (Rational(3, 5) in RR.inject(x, y)) is True

    assert (3.0 in EX) is True
    assert (3.0 in ZZ) is True
    assert (3.0 in QQ) is True
    assert (3.0 in RR) is True
    assert (3.0 in CC) is True
    assert (3.0 in ALG) is True
    assert (3.0 in ZZ.inject(x, y)) is True
    assert (3.0 in QQ.inject(x, y)) is True
    assert (3.0 in RR.inject(x, y)) is True

    assert (3.14 in EX) is True
    assert (3.14 in ZZ) is False
    assert (3.14 in QQ) is True
    assert (3.14 in RR) is True
    assert (3.14 in CC) is True
    assert (3.14 in ALG) is True
    assert (3.14 in ZZ.inject(x, y)) is False
    assert (3.14 in QQ.inject(x, y)) is True
    assert (3.14 in RR.inject(x, y)) is True

    assert (oo in EX) is True
    assert (oo in ZZ) is False
    assert (oo in QQ) is False
    assert (oo in RR) is True
    assert (oo in CC) is True
    assert (oo in ALG) is False
    assert (oo in ZZ.inject(x, y)) is False
    assert (oo in QQ.inject(x, y)) is False
    assert (oo in RR.inject(x, y)) is True

    assert (-oo in EX) is True
    assert (-oo in ZZ) is False
    assert (-oo in QQ) is False
    assert (-oo in RR) is True
    assert (-oo in CC) is True
    assert (-oo in ALG) is False
    assert (-oo in ZZ.inject(x, y)) is False
    assert (-oo in QQ.inject(x, y)) is False
    assert (-oo in RR.inject(x, y)) is True

    assert (sqrt(7) in EX) is True
    assert (sqrt(7) in ZZ) is False
    assert (sqrt(7) in QQ) is False
    assert (sqrt(7) in RR) is True
    assert (sqrt(7) in CC) is True
    assert (sqrt(7) in ALG) is False
    assert (sqrt(7) in ZZ.inject(x, y)) is False
    assert (sqrt(7) in QQ.inject(x, y)) is False
    assert (sqrt(7) in RR.inject(x, y)) is True

    assert (2 * sqrt(3) + 1 in EX) is True
    assert (2 * sqrt(3) + 1 in ZZ) is False
    assert (2 * sqrt(3) + 1 in QQ) is False
    assert (2 * sqrt(3) + 1 in RR) is True
    assert (2 * sqrt(3) + 1 in CC) is True
    assert (2 * sqrt(3) + 1 in ALG) is True
    assert (2 * sqrt(3) + 1 in ZZ.inject(x, y)) is False
    assert (2 * sqrt(3) + 1 in QQ.inject(x, y)) is False
    assert (2 * sqrt(3) + 1 in RR.inject(x, y)) is True

    assert (sin(1) in EX) is True
    assert (sin(1) in ZZ) is False
    assert (sin(1) in QQ) is False
    assert (sin(1) in RR) is True
    assert (sin(1) in CC) is True
    assert (sin(1) in ALG) is False
    assert (sin(1) in ZZ.inject(x, y)) is False
    assert (sin(1) in QQ.inject(x, y)) is False
    assert (sin(1) in RR.inject(x, y)) is True

    assert (x**2 + 1 in EX) is True
    assert (x**2 + 1 in ZZ) is False
    assert (x**2 + 1 in QQ) is False
    assert (x**2 + 1 in RR) is False
    assert (x**2 + 1 in CC) is False
    assert (x**2 + 1 in ALG) is False
    assert (x**2 + 1 in ZZ.inject(x)) is True
    assert (x**2 + 1 in QQ.inject(x)) is True
    assert (x**2 + 1 in RR.inject(x)) is True
    assert (x**2 + 1 in ZZ.inject(x, y)) is True
    assert (x**2 + 1 in QQ.inject(x, y)) is True
    assert (x**2 + 1 in RR.inject(x, y)) is True

    assert (x**2 + y**2 in EX) is True
    assert (x**2 + y**2 in ZZ) is False
    assert (x**2 + y**2 in QQ) is False
    assert (x**2 + y**2 in RR) is False
    assert (x**2 + y**2 in CC) is False
    assert (x**2 + y**2 in ALG) is False
    assert (x**2 + y**2 in ZZ.inject(x)) is False
    assert (x**2 + y**2 in QQ.inject(x)) is False
    assert (x**2 + y**2 in RR.inject(x)) is False
    assert (x**2 + y**2 in ZZ.inject(x, y)) is True
    assert (x**2 + y**2 in QQ.inject(x, y)) is True
    assert (x**2 + y**2 in RR.inject(x, y)) is True

    assert (Rational(3, 2) * x / (y + 1) - z in QQ.inject(x, y, z)) is False

    R = QQ.inject(x)

    assert R(1) in ZZ

    F = R.field

    assert F(1) in ZZ
Beispiel #5
0
def test_nan_inequality_raise_errors():
    # See discussion in pull request #7776.  We test inequalities with
    # a set including examples of various classes.
    for q in (x, Integer(0), Integer(10), Rational(1, 3), pi, Float(1.3), oo, -oo, nan):
        assert_all_ineq_raise_TypeError(q, nan)
Beispiel #6
0
def test_sympyissue_6782():
    assert sqrt(sin(x**3)).series(x, 0, 7) == x**Rational(3, 2) + O(x**7)
    assert sqrt(sin(x**4)).series(x, 0, 3) == x**2 + O(x**3)
Beispiel #7
0
def test_sympyissue_4362():
    neg = Symbol('neg', negative=True)
    nonneg = Symbol('nonneg', nonnegative=True)
    any = Symbol('any')
    num, den = sqrt(1 / neg).as_numer_denom()
    assert num == sqrt(-1)
    assert den == sqrt(-neg)
    num, den = sqrt(1 / nonneg).as_numer_denom()
    assert num == 1
    assert den == sqrt(nonneg)
    num, den = sqrt(1 / any).as_numer_denom()
    assert num == sqrt(1 / any)
    assert den == 1

    def eqn(num, den, pow):
        return (num / den)**pow

    npos = 1
    nneg = -1
    dpos = 2 - sqrt(3)
    dneg = 1 - sqrt(3)
    assert dpos > 0 > dneg and npos > 0 > nneg
    # pos or neg integer
    eq = eqn(npos, dpos, 2)
    assert eq.is_Pow and eq.as_numer_denom() == (1, dpos**2)
    eq = eqn(npos, dneg, 2)
    assert eq.is_Pow and eq.as_numer_denom() == (1, dneg**2)
    eq = eqn(nneg, dpos, 2)
    assert eq.is_Pow and eq.as_numer_denom() == (1, dpos**2)
    eq = eqn(nneg, dneg, 2)
    assert eq.is_Pow and eq.as_numer_denom() == (1, dneg**2)
    eq = eqn(npos, dpos, -2)
    assert eq.is_Pow and eq.as_numer_denom() == (dpos**2, 1)
    eq = eqn(npos, dneg, -2)
    assert eq.is_Pow and eq.as_numer_denom() == (dneg**2, 1)
    eq = eqn(nneg, dpos, -2)
    assert eq.is_Pow and eq.as_numer_denom() == (dpos**2, 1)
    eq = eqn(nneg, dneg, -2)
    assert eq.is_Pow and eq.as_numer_denom() == (dneg**2, 1)
    # pos or neg rational
    pow = Rational(1, 2)
    eq = eqn(npos, dpos, pow)
    assert eq.is_Pow and eq.as_numer_denom() == (npos**pow, dpos**pow)
    eq = eqn(npos, dneg, pow)
    assert eq.is_Pow is False and eq.as_numer_denom() == ((-npos)**pow,
                                                          (-dneg)**pow)
    eq = eqn(nneg, dpos, pow)
    assert not eq.is_Pow or eq.as_numer_denom() == (nneg**pow, dpos**pow)
    eq = eqn(nneg, dneg, pow)
    assert eq.is_Pow and eq.as_numer_denom() == ((-nneg)**pow, (-dneg)**pow)
    eq = eqn(npos, dpos, -pow)
    assert eq.is_Pow and eq.as_numer_denom() == (dpos**pow, npos**pow)
    eq = eqn(npos, dneg, -pow)
    assert eq.is_Pow is False and eq.as_numer_denom() == (-(-npos)**pow *
                                                          (-dneg)**pow, npos)
    eq = eqn(nneg, dpos, -pow)
    assert not eq.is_Pow or eq.as_numer_denom() == (dpos**pow, nneg**pow)
    eq = eqn(nneg, dneg, -pow)
    assert eq.is_Pow and eq.as_numer_denom() == ((-dneg)**pow, (-nneg)**pow)
    # unknown exponent
    pow = 2 * any
    eq = eqn(npos, dpos, pow)
    assert eq.is_Pow and eq.as_numer_denom() == (npos**pow, dpos**pow)
    eq = eqn(npos, dneg, pow)
    assert eq.is_Pow and eq.as_numer_denom() == ((-npos)**pow, (-dneg)**pow)
    eq = eqn(nneg, dpos, pow)
    assert eq.is_Pow and eq.as_numer_denom() == (nneg**pow, dpos**pow)
    eq = eqn(nneg, dneg, pow)
    assert eq.is_Pow and eq.as_numer_denom() == ((-nneg)**pow, (-dneg)**pow)
    eq = eqn(npos, dpos, -pow)
    assert eq.as_numer_denom() == (dpos**pow, npos**pow)
    eq = eqn(npos, dneg, -pow)
    assert eq.is_Pow and eq.as_numer_denom() == ((-dneg)**pow, (-npos)**pow)
    eq = eqn(nneg, dpos, -pow)
    assert eq.is_Pow and eq.as_numer_denom() == (dpos**pow, nneg**pow)
    eq = eqn(nneg, dneg, -pow)
    assert eq.is_Pow and eq.as_numer_denom() == ((-dneg)**pow, (-nneg)**pow)

    assert ((1 / (1 + x / 3))**-1).as_numer_denom() == (3 + x, 3)
    notp = Symbol('notp', positive=False)  # not positive does not imply real
    b = ((1 + x / notp)**-2)
    assert (b**(-y)).as_numer_denom() == (1, b**y)
    assert (b**-1).as_numer_denom() == ((notp + x)**2, notp**2)
    nonp = Symbol('nonp', nonpositive=True)
    assert (((1 + x / nonp)**-2)**-1).as_numer_denom() == ((-nonp - x)**2,
                                                           nonp**2)

    n = Symbol('n', negative=True)
    assert (x**n).as_numer_denom() == (1, x**-n)
    assert sqrt(1 / n).as_numer_denom() == (I, sqrt(-n))
    n = Symbol('0 or neg', nonpositive=True)
    # if x and n are split up without negating each term and n is negative
    # then the answer might be wrong; if n is 0 it won't matter since
    # 1/oo and 1/zoo are both zero as is sqrt(0)/sqrt(-x) unless x is also
    # zero (in which case the negative sign doesn't matter):
    # 1/sqrt(1/-1) = -I but sqrt(-1)/sqrt(1) = I
    assert (1 / sqrt(x / n)).as_numer_denom() == (sqrt(-n), sqrt(-x))
    c = Symbol('c', complex=True)
    e = sqrt(1 / c)
    assert e.as_numer_denom() == (e, 1)
    i = Symbol('i', integer=True)
    assert (((1 + x / y)**i)).as_numer_denom() == ((x + y)**i, y**i)
def test_loggamma():
    pytest.raises(TypeError, lambda: loggamma(2, 3))
    pytest.raises(ArgumentIndexError, lambda: loggamma(x).fdiff(2))

    assert loggamma(-1) == oo
    assert loggamma(-2) == oo
    assert loggamma(0) == oo
    assert loggamma(1) == 0
    assert loggamma(2) == 0
    assert loggamma(3) == log(2)
    assert loggamma(4) == log(6)

    n = Symbol("n", integer=True, positive=True)
    assert loggamma(n) == log(gamma(n))
    assert loggamma(-n) == oo
    assert loggamma(n / 2) == log(2**(-n + 1) * sqrt(pi) * gamma(n) /
                                  gamma(n / 2 + Rational(1, 2)))

    assert loggamma(oo) == oo
    assert loggamma(-oo) == zoo
    assert loggamma(I * oo) == zoo
    assert loggamma(-I * oo) == zoo
    assert loggamma(zoo) == zoo
    assert loggamma(nan) == nan

    L = loggamma(Rational(16, 3))
    E = -5 * log(3) + loggamma(Rational(
        1, 3)) + log(4) + log(7) + log(10) + log(13)
    assert expand_func(L).doit() == E
    assert L.evalf() == E.evalf()

    L = loggamma(19 / Integer(4))
    E = -4 * log(4) + loggamma(Rational(
        3, 4)) + log(3) + log(7) + log(11) + log(15)
    assert expand_func(L).doit() == E
    assert L.evalf() == E.evalf()

    L = loggamma(Rational(23, 7))
    E = -3 * log(7) + log(2) + loggamma(Rational(2, 7)) + log(9) + log(16)
    assert expand_func(L).doit() == E
    assert L.evalf() == E.evalf()

    L = loggamma(19 / Integer(4) - 7)
    E = -log(9) - log(5) + loggamma(Rational(3, 4)) + 3 * log(4) - 3 * I * pi
    assert expand_func(L).doit() == E
    assert L.evalf() == E.evalf()

    L = loggamma(23 / Integer(7) - 6)
    E = -log(19) - log(12) - log(5) + loggamma(Rational(
        2, 7)) + 3 * log(7) - 3 * I * pi
    assert expand_func(L).doit() == E
    assert L.evalf() == E.evalf()

    assert expand_func(loggamma(x)) == loggamma(x)
    assert expand_func(loggamma(1 / Integer(3))) == loggamma(1 / Integer(3))

    assert loggamma(x).diff(x) == polygamma(0, x)
    s1 = loggamma(1 / (x + sin(x)) + cos(x)).nseries(x, n=4)
    s2 = (-log(2*x) - 1)/(2*x) - log(x/pi)/2 + (4 - log(2*x))*x/24 + O(x**2) + \
        log(x)*x**2/2
    assert (s1 - s2).expand(force=True).removeO() == 0
    s1 = loggamma(1 / x).series(x)
    s2 = (1/x - Rational(1, 2))*log(1/x) - 1/x + log(2*pi)/2 + \
        x/12 - x**3/360 + x**5/1260 + O(x**7)
    assert ((s1 - s2).expand(force=True)).removeO() == 0

    assert loggamma(x).rewrite('intractable') == log(gamma(x))

    s1 = loggamma(x).series(x)
    assert s1 == -log(x) - EulerGamma*x + pi**2*x**2/12 + x**3*polygamma(2, 1)/6 + \
        pi**4*x**4/360 + x**5*polygamma(4, 1)/120 + O(x**6)
    assert s1 == loggamma(x).rewrite('intractable').series(x)

    assert conjugate(loggamma(x)) == conjugate(loggamma(x), evaluate=False)
    p = Symbol('p', positive=True)
    c = Symbol('c', complex=True, extended_real=False)
    assert conjugate(loggamma(p)) == loggamma(p)
    assert conjugate(loggamma(c)) == loggamma(conjugate(c))
    assert conjugate(loggamma(0)) == conjugate(loggamma(0))
    assert conjugate(loggamma(1)) == loggamma(conjugate(1))
    assert conjugate(loggamma(-oo)) == conjugate(loggamma(-oo))
    assert loggamma(x).is_extended_real is None
    y = Symbol('y', nonnegative=True)
    assert loggamma(y).is_extended_real
    assert loggamma(w).is_extended_real is None

    def tN(N, M):
        assert loggamma(1 / x)._eval_nseries(x, n=N).getn() == M

    tN(0, 0)
    tN(1, 1)
    tN(2, 3)
    tN(3, 3)
    tN(4, 5)
    tN(5, 5)
Beispiel #9
0
def test_issue_4136():
    assert cosh(asinh(Rational(3, 2))) == sqrt(Rational(13, 4))
def test_polygamma():
    assert polygamma(n, nan) == nan

    assert polygamma(0, oo) == oo
    assert polygamma(0, -oo) == oo
    assert polygamma(0, I * oo) == oo
    assert polygamma(0, -I * oo) == oo
    assert polygamma(1, oo) == 0
    assert polygamma(5, oo) == 0
    assert polygamma(n, oo) == polygamma(n, oo, evaluate=False)

    assert polygamma(0, -9) == zoo

    assert polygamma(0, -9) == zoo
    assert polygamma(0, -1) == zoo

    assert polygamma(0, 0) == zoo

    assert polygamma(0, 1) == -EulerGamma
    assert polygamma(0, 7) == Rational(49, 20) - EulerGamma
    assert polygamma(0, -Rational(3, 11)) == polygamma(0,
                                                       -Rational(3, 11),
                                                       evaluate=False)

    assert polygamma(1, 1) == pi**2 / 6
    assert polygamma(1, 2) == pi**2 / 6 - 1
    assert polygamma(1, 3) == pi**2 / 6 - Rational(5, 4)
    assert polygamma(3, 1) == pi**4 / 15
    assert polygamma(3, 5) == 6 * (Rational(-22369, 20736) + pi**4 / 90)
    assert polygamma(5, 1) == 8 * pi**6 / 63
    assert trigamma(x) == polygamma(1, x)

    def t(m, n):
        x = Integer(m) / n
        r = polygamma(0, x)
        if r.has(polygamma):
            return False
        return abs(polygamma(0, x.evalf()).evalf(strict=False) -
                   r.evalf()).evalf(strict=False) < 1e-10

    assert t(1, 2)
    assert t(3, 2)
    assert t(-1, 2)
    assert t(1, 4)
    assert t(-3, 4)
    assert t(1, 3)
    assert t(4, 3)
    assert t(3, 4)
    assert t(2, 3)

    assert polygamma(0, x).rewrite(zeta) == polygamma(0, x)
    assert polygamma(1, x).rewrite(zeta) == zeta(2, x)
    assert polygamma(2, x).rewrite(zeta) == -2 * zeta(3, x)

    assert polygamma(3, 7 * x).diff(x) == 7 * polygamma(4, 7 * x)

    assert polygamma(0, x).rewrite(harmonic) == harmonic(x - 1) - EulerGamma
    assert polygamma(
        2, x).rewrite(harmonic) == 2 * harmonic(x - 1, 3) - 2 * zeta(3)
    ni = Symbol("n", integer=True)
    assert polygamma(
        ni,
        x).rewrite(harmonic) == (-1)**(ni + 1) * (-harmonic(x - 1, ni + 1) +
                                                  zeta(ni + 1)) * factorial(ni)
    assert polygamma(x, y).rewrite(harmonic) == polygamma(x, y)

    # Polygamma of non-negative integer order is unbranched:
    k = Symbol('n', integer=True, nonnegative=True)
    assert polygamma(k, exp_polar(2 * I * pi) * x) == polygamma(k, x)

    # but negative integers are branched!
    k = Symbol('n', integer=True)
    assert polygamma(k,
                     exp_polar(2 * I * pi) *
                     x).args == (k, exp_polar(2 * I * pi) * x)

    # Polygamma of order -1 is loggamma:
    assert polygamma(-1, x) == loggamma(x)

    # But smaller orders are iterated integrals and don't have a special name
    assert isinstance(polygamma(-2, x), polygamma)

    # Test a bug
    assert polygamma(0, -x).expand(func=True) == polygamma(0, -x)

    assert polygamma(1, x).as_leading_term(x) == polygamma(1, x)
def test_gamma():
    assert gamma(nan) == nan
    assert gamma(oo) == oo

    assert gamma(-100) == zoo
    assert gamma(0) == zoo

    assert gamma(1) == 1
    assert gamma(2) == 1
    assert gamma(3) == 2

    assert gamma(102) == factorial(101)

    assert gamma(Rational(1, 2)) == sqrt(pi)

    assert gamma(Rational(3, 2)) == Rational(1, 2) * sqrt(pi)
    assert gamma(Rational(5, 2)) == Rational(3, 4) * sqrt(pi)
    assert gamma(Rational(7, 2)) == Rational(15, 8) * sqrt(pi)

    assert gamma(Rational(-1, 2)) == -2 * sqrt(pi)
    assert gamma(Rational(-3, 2)) == Rational(4, 3) * sqrt(pi)
    assert gamma(Rational(-5, 2)) == -Rational(8, 15) * sqrt(pi)

    assert gamma(Rational(-15, 2)) == Rational(256, 2027025) * sqrt(pi)

    assert gamma(Rational(
        -11, 8)).expand(func=True) == Rational(64, 33) * gamma(Rational(5, 8))
    assert gamma(Rational(
        -10, 3)).expand(func=True) == Rational(81, 280) * gamma(Rational(2, 3))
    assert gamma(Rational(
        14, 3)).expand(func=True) == Rational(880, 81) * gamma(Rational(2, 3))
    assert gamma(Rational(
        17, 7)).expand(func=True) == Rational(30, 49) * gamma(Rational(3, 7))
    assert gamma(Rational(
        19, 8)).expand(func=True) == Rational(33, 64) * gamma(Rational(3, 8))

    assert gamma(x).diff(x) == gamma(x) * polygamma(0, x)
    pytest.raises(ArgumentIndexError, lambda: gamma(x).fdiff(2))

    assert gamma(x - 1).expand(func=True) == gamma(x) / (x - 1)
    assert gamma(x + 2).expand(func=True, mul=False) == x * (x + 1) * gamma(x)

    assert conjugate(gamma(x)) == gamma(conjugate(x))

    assert expand_func(gamma(x + Rational(3, 2))) == \
        (x + Rational(1, 2))*gamma(x + Rational(1, 2))

    assert expand_func(gamma(x - Rational(1, 2))) == \
        gamma(Rational(1, 2) + x)/(x - Rational(1, 2))

    # Test a bug:
    assert expand_func(gamma(x + Rational(3, 4))) == gamma(x + Rational(3, 4))

    assert gamma(3 * exp_polar(I * pi) / 4).is_nonnegative is False
    assert gamma(3 * exp_polar(I * pi) / 4).is_nonpositive is True

    # Issue sympy/sympy#8526
    k = Symbol('k', integer=True, nonnegative=True)
    assert isinstance(gamma(k), gamma)
    assert gamma(-k) == zoo
Beispiel #12
0
def test_diofantissue_453():
    x = Symbol('x', real=True)
    assert isolve(abs((x - 1)/(x - 5)) <= Rational(1, 3),
                  x) == And(Integer(-1) <= x, x <= 2)
    assert solve(abs((x - 1)/(x - 5)) - Rational(1, 3), x) == [{x: -1}, {x: 2}]
Beispiel #13
0
def test_sympyissue_6343():
    eq = -3*x**2/2 - 45*x/4 + Rational(33, 2) > 0
    assert reduce_inequalities(eq) == \
        And(x < -Rational(15, 4) + sqrt(401)/4, -sqrt(401)/4 - Rational(15, 4) < x)
Beispiel #14
0
def test_dice():
    # TODO: Make iid method!
    X, Y, Z = Die('X', 6), Die('Y', 6), Die('Z', 6)
    a, b = symbols('a b')

    assert E(X) == 3 + Rational(1, 2)
    assert variance(X) == Rational(35, 12)
    assert E(X + Y) == 7
    assert E(X + X) == 7
    assert E(a * X + b) == a * E(X) + b
    assert variance(X + Y) == variance(X) + variance(Y) == cmoment(X + Y, 2)
    assert variance(X + X) == 4 * variance(X) == cmoment(X + X, 2)
    assert cmoment(X, 0) == 1
    assert cmoment(4 * X, 3) == 64 * cmoment(X, 3)
    assert covariance(X, Y) == 0
    assert covariance(X, X + Y) == variance(X)
    assert density(Eq(cos(X * pi), 1))[True] == Rational(1, 2)
    assert correlation(X, Y) == 0
    assert correlation(X, Y) == correlation(Y, X)
    assert smoment(X + Y, 3) == skewness(X + Y)
    assert smoment(X, 0) == 1
    assert P(X > 3) == Rational(1, 2)
    assert P(2 * X > 6) == Rational(1, 2)
    assert P(X > Y) == Rational(5, 12)
    assert P(Eq(X, Y)) == P(Eq(X, 1))

    assert E(X, X > 3) == 5 == moment(X, 1, 0, X > 3)
    assert E(X, Y > 3) == E(X) == moment(X, 1, 0, Y > 3)
    assert E(X + Y, Eq(X, Y)) == E(2 * X)
    assert moment(X, 0) == 1
    assert moment(5 * X, 2) == 25 * moment(X, 2)

    assert P(X > 3, X > 3) == 1
    assert P(X > Y, Eq(Y, 6)) == 0
    assert P(Eq(X + Y, 12)) == Rational(1, 36)
    assert P(Eq(X + Y, 12), Eq(X, 6)) == Rational(1, 6)

    assert density(X + Y) == density(Y + Z) != density(X + X)
    d = density(2 * X + Y**Z)
    assert d[22] == Rational(1, 108) and d[4100] == Rational(
        1, 216) and 3130 not in d

    assert pspace(X).domain.as_boolean() == Or(
        *[Eq(X.symbol, i) for i in [1, 2, 3, 4, 5, 6]])

    assert where(X > 3).set == FiniteSet(4, 5, 6)

    X = Die('X', 2)
    x = X.symbol

    assert X.pspace.compute_cdf(X) == {1: Rational(1, 2), 2: 1}
    assert X.pspace.sorted_cdf(X) == [(1, Rational(1, 2)), (2, 1)]

    assert X.pspace.compute_density(X)(1) == Rational(1, 2)
    assert X.pspace.compute_density(X)(0) == 0
    assert X.pspace.compute_density(X)(8) == 0

    assert X.pspace.density == x
Beispiel #15
0
def test_large_rational():
    e = cbrt(Rational(123712**12 - 1, 7) + Rational(1, 7))
    assert e == 234232585392159195136 * cbrt(Rational(1, 7))
Beispiel #16
0
def test_Subs():
    assert Subs(x, (x, 0)) == Subs(y, (y, 0))
    assert Subs(x, (x, 0)).subs({x: 1}) == Subs(x, (x, 0))
    assert Subs(y, (x, 0)).subs({y: 1}) == Subs(1, (x, 0))
    assert Subs(f(x), (x, 0)).doit() == f(0)
    assert Subs(f(x**2), (x**2, 0)).doit() == f(0)
    assert Subs(f(x, y, z), (x, 0), (y, 1), (z, 1)) != \
        Subs(f(x, y, z), (x, 0), (y, 0), (z, 1))
    assert Subs(f(x, y), (x, 0), (y, 1), (z, 1)) == \
        Subs(f(x, y), (x, 0), (y, 1), (z, 2))
    assert Subs(f(x, y), (x, 0), (y, 1), (z, 1)) != \
        Subs(f(x, y) + z, (x, 0), (y, 1), (z, 0))
    assert Subs(f(x, y), (x, 0), (y, 1)).doit() == f(0, 1)
    assert Subs(Subs(f(x, y), (x, 0)), (y, 1)).doit() == f(0, 1)
    pytest.raises(ValueError, lambda: Subs(f(x, y), x))
    pytest.raises(ValueError, lambda: Subs(f(x, y), (x, 0), (x, 0), (y, 1)))

    assert len(Subs(f(x, y), (x, 0), (y, 1)).variables) == 2
    assert Subs(f(x, y), (x, 0), (y, 1)).point == Tuple(0, 1)

    assert Subs(f(x), (x, 0)) == Subs(f(y), (y, 0))
    assert Subs(f(x, y), (x, 0), (y, 1)) == Subs(f(x, y), (y, 1), (x, 0))
    assert Subs(f(x) * y, (x, 0), (y, 1)) == Subs(f(y) * x, (y, 0), (x, 1))
    assert Subs(f(x) * y, (x, 1), (y, 1)) == Subs(f(y) * x, (x, 1), (y, 1))

    assert Subs(f(x), (x, 0)).subs({x: 1}).doit() == f(0)
    assert Subs(f(x), (x, y)).subs({y: 0}) == Subs(f(x), (x, 0))
    assert Subs(y * f(x), (x, y)).subs({y: 2}) == Subs(2 * f(x), (x, 2))
    assert (2 * Subs(f(x), (x, 0))).subs({Subs(f(x), (x, 0)): y}) == 2 * y

    assert Subs(f(x), (x, 0)).free_symbols == set()
    assert Subs(f(x, y), (x, z)).free_symbols == {y, z}

    assert Subs(f(x).diff(x), (x, 0)).doit(), Subs(f(x).diff(x), (x, 0))
    assert Subs(1 + f(x).diff(x),
                (x, 0)).doit(), 1 + Subs(f(x).diff(x), (x, 0))
    assert Subs(y*f(x, y).diff(x), (x, 0), (y, 2)).doit() == \
        2*Subs(Derivative(f(x, 2), x), (x, 0))
    assert Subs(y**2 * f(x), (x, 0)).diff(y) == 2 * y * f(0)

    e = Subs(y**2 * f(x), (x, y))
    assert e.diff(y) == e.doit().diff(
        y) == y**2 * Derivative(f(y), y) + 2 * y * f(y)

    assert Subs(f(x), (x, 0)) + Subs(f(x), (x, 0)) == 2 * Subs(f(x), (x, 0))
    e1 = Subs(z * f(x), (x, 1))
    e2 = Subs(z * f(y), (y, 1))
    assert e1 + e2 == 2 * e1
    assert e1.__hash__() == e2.__hash__()
    assert Subs(z * f(x + 1), (x, 1)) not in (e1, e2)
    assert Derivative(f(x), x).subs({x: g(x)}) == Derivative(f(g(x)), g(x))
    assert Derivative(f(x), x).subs({x:
                                     x + y}) == Subs(Derivative(f(x), x),
                                                     (x, x + y))
    assert Subs(f(x)*cos(y) + z, (x, 0), (y, pi/3)).evalf(2, strict=False) == \
        Subs(f(x)*cos(y) + z, (x, 0), (y, pi/3)).evalf(2, strict=False) == \
        z + Rational('1/2').evalf(2)*f(0)

    assert f(x).diff(x).subs({x: 0}).subs({x: y}) == f(x).diff(x).subs({x: 0})
    assert (x * f(x).diff(x).subs({x: 0})).subs(
        {x: y}) == y * f(x).diff(x).subs({x: 0})
Beispiel #17
0
def test_sympyissue_6068():
    assert sqrt(sin(x)).series(x, 0, 8) == \
        sqrt(x) - x**Rational(5, 2)/12 + x**Rational(9, 2)/1440 - \
        x**Rational(13, 2)/24192 + O(x**8)
    assert sqrt(sin(x)).series(x, 0, 10) == \
        sqrt(x) - x**Rational(5, 2)/12 + x**Rational(9, 2)/1440 - \
        x**Rational(13, 2)/24192 - 67*x**Rational(17, 2)/29030400 + O(x**10)
    assert sqrt(sin(x**3)).series(x, 0, 19) == \
        x**Rational(3, 2) - x**Rational(15, 2)/12 + x**Rational(27, 2)/1440 + O(x**19)
    assert sqrt(sin(x**3)).series(x, 0, 20) == \
        x**Rational(3, 2) - x**Rational(15, 2)/12 + x**Rational(27, 2)/1440 - \
        x**Rational(39, 2)/24192 + O(x**20)
Beispiel #18
0
def test_gosper_normal():
    assert gosper_normal(4*n + 5, 2*(4*n + 1)*(2*n + 3), n) == \
        (Poly(Rational(1, 4), n), Poly(n + Rational(3, 2)), Poly(n + Rational(1, 4)))
    assert gosper_normal(4*n + 5, 2*(4*n + 1)*(2*n + 3), n, polys=False) == \
        (Rational(1, 4), n + Rational(3, 2), n + Rational(1, 4))
Beispiel #19
0
def test_expand():
    assert (2**(-1 - x)).expand() == Rational(1, 2) * 2**(-x)
Beispiel #20
0
def test_gosper_term():
    assert gosper_term((4 * k + 1) * factorial(k) / factorial(2 * k + 1),
                       k) == (-k - Rational(1, 2)) / (k + Rational(1, 4))
Beispiel #21
0
def test_expand_inverse():
    assert (1 / (1 + I)).expand(complex=True) == (1 - I) / 2
    assert ((1 + 2 * I)**(-2)).expand(complex=True) == (-3 - 4 * I) / 25
    assert ((1 + I)**(-8)).expand(complex=True) == Rational(1, 16)
Beispiel #22
0
def test_gosper_sum_parametric():
    assert gosper_sum(binomial(Rational(1, 2), m - j + 1)*binomial(Rational(1, 2), m + j), (j, 1, n)) == \
        n*(1 + m - n)*(-1 + 2*m + 2*n)*binomial(Rational(1, 2), 1 + m - n) * \
        binomial(Rational(1, 2), m + n)/(m*(1 + 2*m))
Beispiel #23
0
def test_Domain__algebraic_field():
    alg = QQ.algebraic_field(sqrt(3))
    assert alg.minpoly == Poly(x**2 - 3)
    assert alg.domain == QQ
    assert alg.from_expr(sqrt(3)).denominator == 1
    assert alg.from_expr(2 * sqrt(3)).denominator == 1
    assert alg.from_expr(sqrt(3) / 2).denominator == 2
    assert alg([QQ(3, 2), QQ(7, 38)]).denominator == 38

    alg = QQ.algebraic_field(sqrt(2))
    assert alg.minpoly == Poly(x**2 - 2)
    assert alg.domain == QQ

    alg = QQ.algebraic_field(sqrt(2), sqrt(3))
    assert alg.minpoly == Poly(x**4 - 10 * x**2 + 1)
    assert alg.domain == QQ

    assert alg(1).numerator == alg(1)
    assert alg.from_expr(sqrt(3) / 2).numerator == alg.from_expr(2 * sqrt(3))
    assert alg.from_expr(sqrt(3) / 2).denominator == 4

    pytest.raises(DomainError, lambda: AlgebraicField(ZZ, sqrt(2)))

    assert alg.characteristic == 0
    assert alg.inject(x).characteristic == 0
    assert alg.inject(x).field.characteristic == 0

    assert alg.is_RealAlgebraicField is True

    assert int(alg(2)) == 2
    assert int(alg.from_expr(Rational(3, 2))) == 1

    alg = QQ.algebraic_field(I)
    assert alg.algebraic_field(I) == alg
    assert alg.is_RealAlgebraicField is False
    pytest.raises(TypeError, lambda: int(alg([1, 1])))

    alg = QQ.algebraic_field(sqrt(2)).algebraic_field(sqrt(3))
    assert alg.minpoly == Poly(x**2 - 3, x, domain=QQ.algebraic_field(sqrt(2)))

    # issue sympy/sympy#14476
    assert QQ.algebraic_field(Rational(1, 7)) is QQ

    alg = QQ.algebraic_field(sqrt(2)).algebraic_field(I)
    assert alg.from_expr(2 * sqrt(2) + I / 3) == alg(
        [alg.domain([0, 2]), alg.domain([1]) / 3])
    alg2 = QQ.algebraic_field(sqrt(2))
    assert alg2.from_expr(sqrt(2)) == alg2.convert(alg.from_expr(sqrt(2)))

    eq = -x**3 + 2 * x**2 + 3 * x - 2
    rs = roots(eq, multiple=True)
    alg = QQ.algebraic_field(rs[0])
    assert alg.is_RealAlgebraicField

    alg1 = QQ.algebraic_field(I)
    alg2 = QQ.algebraic_field(sqrt(2)).algebraic_field(I)
    assert alg1 != alg2

    alg3 = QQ.algebraic_field(RootOf(4 * x**7 + x - 1, 0))
    assert alg3.is_RealAlgebraicField
    assert int(alg3.unit) == 2
    assert 2.772 > alg3.unit > 2.771
    assert int(alg3([2, -1, 11, 17, 3])) == 622
    assert int(
        alg3([
            QQ(2331359268715, 10459004949272),
            QQ(-16742151878022, 12894796053515),
            QQ(125326976730518, 44208605852241),
            QQ(-11, 4), 1
        ])) == 18

    alg4 = QQ.algebraic_field(sqrt(2) + I)
    assert alg4.convert(alg2.unit) == alg4.from_expr(I)
Beispiel #24
0
def test_construct_domain():
    assert construct_domain([1, 2, 3]) == (ZZ, [ZZ(1), ZZ(2), ZZ(3)])
    assert construct_domain([1, 2, 3],
                            field=True) == (QQ, [QQ(1), QQ(2),
                                                 QQ(3)])

    assert construct_domain([Integer(1), Integer(2),
                             Integer(3)]) == (ZZ, [ZZ(1), ZZ(2),
                                                   ZZ(3)])
    assert construct_domain(
        [Integer(1), Integer(2), Integer(3)],
        field=True) == (QQ, [QQ(1), QQ(2), QQ(3)])

    assert construct_domain([Rational(1, 2),
                             Integer(2)]) == (QQ, [QQ(1, 2), QQ(2)])
    assert construct_domain([3.14, 1, Rational(1, 2)
                             ]) == (RR, [RR(3.14), RR(1.0),
                                         RR(0.5)])

    assert construct_domain([3.14, sqrt(2)],
                            extension=False) == (EX, [EX(3.14),
                                                      EX(sqrt(2))])
    assert construct_domain([3.14, sqrt(2)]) == (EX, [EX(3.14), EX(sqrt(2))])
    assert construct_domain([sqrt(2), 3.14]) == (EX, [EX(sqrt(2)), EX(3.14)])

    assert construct_domain([1, sqrt(2)],
                            extension=False) == (EX, [EX(1),
                                                      EX(sqrt(2))])

    assert construct_domain([x, sqrt(x)]) == (EX, [EX(x), EX(sqrt(x))])
    assert construct_domain([x, sqrt(x), sqrt(y)
                             ]) == (EX, [EX(x),
                                         EX(sqrt(x)),
                                         EX(sqrt(y))])

    alg = QQ.algebraic_field(sqrt(2))

    assert (construct_domain(
        [7, Rational(1, 2),
         sqrt(2)]) == (alg, [alg([7]),
                             alg([Rational(1, 2)]),
                             alg([0, 1])]))

    alg = QQ.algebraic_field(sqrt(2) + sqrt(3))

    assert (construct_domain([7, sqrt(2), sqrt(3)]) == (alg, [
        alg([7]), alg.from_expr(sqrt(2)),
        alg.from_expr(sqrt(3))
    ]))

    dom = ZZ.inject(x)

    assert construct_domain([2 * x, 3]) == (dom, [dom(2 * x), dom(3)])

    dom = ZZ.inject(x, y)

    assert construct_domain([2 * x, 3 * y]) == (dom, [dom(2 * x), dom(3 * y)])

    dom = QQ.inject(x)

    assert construct_domain([x / 2, 3]) == (dom, [dom(x / 2), dom(3)])

    dom = QQ.inject(x, y)

    assert construct_domain([x / 2, 3 * y]) == (dom, [dom(x / 2), dom(3 * y)])

    dom = RR.inject(x)

    assert construct_domain([x / 2, 3.5]) == (dom, [dom(x / 2), dom(3.5)])

    dom = RR.inject(x, y)

    assert construct_domain([x / 2,
                             3.5 * y]) == (dom, [dom(x / 2),
                                                 dom(3.5 * y)])

    dom = ZZ.inject(x).field

    assert construct_domain([2 / x, 3]) == (dom, [dom(2 / x), dom(3)])

    dom = ZZ.inject(x, y).field

    assert construct_domain([2 / x, 3 * y]) == (dom, [dom(2 / x), dom(3 * y)])

    dom = RR.inject(x).field

    assert construct_domain([2 / x, 3.5]) == (dom, [dom(2 / x), dom(3.5)])

    dom = RR.inject(x, y).field

    assert construct_domain([2 / x,
                             3.5 * y]) == (dom, [dom(2 / x),
                                                 dom(3.5 * y)])

    assert construct_domain(2) == (ZZ, ZZ(2))
    assert construct_domain(Rational(2, 3)) == (QQ, QQ(2, 3))

    assert construct_domain({}) == (ZZ, {})

    assert construct_domain([-x * y + x * (y + 42) - 42 * x
                             ]) == (EX, [EX(-x * y + x * (y + 42) - 42 * x)])

    dom = ZZ.inject(E)

    assert construct_domain(E) == (dom, dom(E))

    dom = ZZ.inject(x, E)

    assert construct_domain(x**2 + 2 * x + E) == (dom, dom(x**2 + 2 * x + E))

    assert construct_domain(x + y + GoldenRatio) == (EX,
                                                     EX(x + y + GoldenRatio))
Beispiel #25
0
def test_radsimp():
    r2 = sqrt(2)
    r3 = sqrt(3)
    r5 = sqrt(5)
    r7 = sqrt(7)
    assert fraction(radsimp(1 / r2)) == (sqrt(2), 2)
    assert radsimp(1/(1 + r2)) == \
        -1 + sqrt(2)
    assert radsimp(1/(r2 + r3)) == \
        -sqrt(2) + sqrt(3)
    assert fraction(radsimp(1/(1 + r2 + r3))) == \
        (-sqrt(6) + sqrt(2) + 2, 4)
    assert fraction(radsimp(1/(r2 + r3 + r5))) == \
        (-sqrt(30) + 2*sqrt(3) + 3*sqrt(2), 12)
    assert fraction(radsimp(
        1 /
        (1 + r2 + r3 + r5))) == ((-34 * sqrt(10) - 26 * sqrt(15) -
                                  55 * sqrt(3) - 61 * sqrt(2) + 14 * sqrt(30) +
                                  93 + 46 * sqrt(6) + 53 * sqrt(5), 71))
    assert fraction(radsimp(
        1 / (r2 + r3 + r5 + r7))) == ((-50 * sqrt(42) - 133 * sqrt(5) -
                                       34 * sqrt(70) - 145 * sqrt(3) +
                                       22 * sqrt(105) + 185 * sqrt(2) +
                                       62 * sqrt(30) + 135 * sqrt(7), 215))
    z = radsimp(1 / (1 + r2 / 3 + r3 / 5 + r5 + r7))
    assert len((3616791619821680643598 * z).args) == 16
    assert radsimp(1 / z) == 1 / z
    assert radsimp(1 / z,
                   max_terms=20).expand() == 1 + r2 / 3 + r3 / 5 + r5 + r7
    assert radsimp(1/(r2*3)) == \
        sqrt(2)/6
    assert radsimp(1 / (r2 * a + r3 + r5 + r7)) == (
        (8 * sqrt(2) * a**7 - 8 * sqrt(7) * a**6 - 8 * sqrt(5) * a**6 -
         8 * sqrt(3) * a**6 - 180 * sqrt(2) * a**5 + 8 * sqrt(30) * a**5 +
         8 * sqrt(42) * a**5 + 8 * sqrt(70) * a**5 - 24 * sqrt(105) * a**4 +
         84 * sqrt(3) * a**4 + 100 * sqrt(5) * a**4 + 116 * sqrt(7) * a**4 -
         72 * sqrt(70) * a**3 - 40 * sqrt(42) * a**3 - 8 * sqrt(30) * a**3 +
         782 * sqrt(2) * a**3 - 462 * sqrt(3) * a**2 - 302 * sqrt(7) * a**2 -
         254 * sqrt(5) * a**2 + 120 * sqrt(105) * a**2 - 795 * sqrt(2) * a -
         62 * sqrt(30) * a + 82 * sqrt(42) * a + 98 * sqrt(70) * a -
         118 * sqrt(105) + 59 * sqrt(7) + 295 * sqrt(5) + 531 * sqrt(3)) /
        (16 * a**8 - 480 * a**6 + 3128 * a**4 - 6360 * a**2 + 3481))
    assert radsimp(1 / (r2 * a + r2 * b + r3 + r7)) == (
        (sqrt(2) * a *
         (a + b)**2 - 5 * sqrt(2) * a + sqrt(42) * a + sqrt(2) * b *
         (a + b)**2 - 5 * sqrt(2) * b + sqrt(42) * b - sqrt(7) *
         (a + b)**2 - sqrt(3) * (a + b)**2 - 2 * sqrt(3) + 2 * sqrt(7)) /
        (2 * a**4 + 8 * a**3 * b + 12 * a**2 * b**2 - 20 * a**2 +
         8 * a * b**3 - 40 * a * b + 2 * b**4 - 20 * b**2 + 8))
    assert radsimp(1/(r2*a + r2*b + r2*c + r2*d)) == \
        sqrt(2)/(2*a + 2*b + 2*c + 2*d)
    assert radsimp(1 / (1 + r2 * a + r2 * b + r2 * c + r2 * d)) == (
        (sqrt(2) * a + sqrt(2) * b + sqrt(2) * c + sqrt(2) * d - 1) /
        (2 * a**2 + 4 * a * b + 4 * a * c + 4 * a * d + 2 * b**2 + 4 * b * c +
         4 * b * d + 2 * c**2 + 4 * c * d + 2 * d**2 - 1))
    assert radsimp((y**2 - x)/(y - sqrt(x))) == \
        sqrt(x) + y
    assert radsimp(-(y**2 - x)/(y - sqrt(x))) == \
        -(sqrt(x) + y)
    assert radsimp(1/(1 - I + a*I)) == \
        (-I*a + 1 + I)/(a**2 - 2*a + 2)
    assert radsimp(1/((-x + y)*(x - sqrt(y)))) == \
        (-x - sqrt(y))/((x - y)*(x**2 - y))
    e = (3 + 3 * sqrt(2)) * x * (3 * x - 3 * sqrt(y))
    assert radsimp(e) == x * (3 + 3 * sqrt(2)) * (3 * x - 3 * sqrt(y))
    assert radsimp(1 / e) == (
        (-9 * x + 9 * sqrt(2) * x - 9 * sqrt(y) + 9 * sqrt(2) * sqrt(y)) /
        (9 * x * (9 * x**2 - 9 * y)))
    assert radsimp(1 + 1/(1 + sqrt(3))) == \
        Mul(Rational(1, 2), -1 + sqrt(3), evaluate=False) + 1
    A = symbols("A", commutative=False)
    assert radsimp(x**2 + sqrt(2)*x**2 - sqrt(2)*x*A) == \
        x**2 + sqrt(2)*x**2 - sqrt(2)*x*A
    assert radsimp(1 / sqrt(5 + 2 * sqrt(6))) == -sqrt(2) + sqrt(3)
    assert radsimp(1 / sqrt(5 + 2 * sqrt(6))**3) == -(-sqrt(3) + sqrt(2))**3

    # issue sympy/sympy#6532
    assert fraction(radsimp(1 / sqrt(x))) == (sqrt(x), x)
    assert fraction(radsimp(1 / sqrt(2 * x + 3))) == (sqrt(2 * x + 3),
                                                      2 * x + 3)
    assert fraction(radsimp(1 / sqrt(2 * (x + 3)))) == (sqrt(2 * x + 6),
                                                        2 * x + 6)

    # issue sympy/sympy#5994
    e = -(2 + 2 * sqrt(2) + 4 * root(2, 4)) / (1 + 2**Rational(3, 4) +
                                               3 * root(2, 4) + 3 * sqrt(2))
    assert radsimp(e).expand(
    ) == -2 * 2**Rational(3, 4) - 2 * root(2, 4) + 2 + 2 * sqrt(2)

    # issue sympy/sympy#5986 (modifications to radimp didn't initially recognize this so
    # the test is included here)
    assert radsimp(1 / (-sqrt(5) / 2 - Rational(1, 2) +
                        (-sqrt(5) / 2 - Rational(1, 2))**2)) == 1

    # from issue sympy/sympy#5934
    eq = ((-240 * sqrt(2) * sqrt(sqrt(5) + 5) * sqrt(8 * sqrt(5) + 40) -
           360 * sqrt(2) * sqrt(-8 * sqrt(5) + 40) * sqrt(-sqrt(5) + 5) -
           120 * sqrt(10) * sqrt(-8 * sqrt(5) + 40) * sqrt(-sqrt(5) + 5) +
           120 * sqrt(2) * sqrt(-sqrt(5) + 5) * sqrt(8 * sqrt(5) + 40) +
           120 * sqrt(2) * sqrt(-8 * sqrt(5) + 40) * sqrt(sqrt(5) + 5) +
           120 * sqrt(10) * sqrt(-sqrt(5) + 5) * sqrt(8 * sqrt(5) + 40) +
           120 * sqrt(10) * sqrt(-8 * sqrt(5) + 40) * sqrt(sqrt(5) + 5)) /
          (-36000 - 7200 * sqrt(5) + (12 * sqrt(10) * sqrt(sqrt(5) + 5) +
                                      24 * sqrt(10) * sqrt(-sqrt(5) + 5))**2))
    assert radsimp(eq) is nan  # it's 0/0

    # work with normal form
    e = 1 / sqrt(sqrt(7) / 7 + 2 * sqrt(2) + 3 * sqrt(3) + 5 * sqrt(5)) + 3
    assert radsimp(e) == (
        -sqrt(sqrt(7) + 14 * sqrt(2) + 21 * sqrt(3) + 35 * sqrt(5)) *
        (-11654899 * sqrt(35) - 1577436 * sqrt(210) - 1278438 * sqrt(15) -
         1346996 * sqrt(10) + 1635060 * sqrt(6) + 5709765 +
         7539830 * sqrt(14) + 8291415 * sqrt(21)) / 1300423175 + 3)

    # obey power rules
    base = sqrt(3) - sqrt(2)
    assert radsimp(1 / base**3) == (sqrt(3) + sqrt(2))**3
    assert radsimp(1 / (-base)**3) == -(sqrt(2) + sqrt(3))**3
    assert radsimp(1 / (-base)**x) == (-base)**(-x)
    assert radsimp(1 / base**x) == (sqrt(2) + sqrt(3))**x
    assert radsimp(root(1 / (-1 - sqrt(2)),
                        -x)) == (-1)**(-1 / x) * (1 + sqrt(2))**(1 / x)

    # recurse
    e = cos(1 / (1 + sqrt(2)))
    assert radsimp(e) == cos(-sqrt(2) + 1)
    assert radsimp(e / 2) == cos(-sqrt(2) + 1) / 2
    assert radsimp(1 / e) == 1 / cos(-sqrt(2) + 1)
    assert radsimp(2 / e) == 2 / cos(-sqrt(2) + 1)
    assert fraction(radsimp(e / sqrt(x))) == (sqrt(x) * cos(-sqrt(2) + 1), x)

    # test that symbolic denominators are not processed
    r = 1 + sqrt(2)
    assert radsimp(x / r, symbolic=False) == -x * (-sqrt(2) + 1)
    assert radsimp(x / (y + r), symbolic=False) == x / (y + 1 + sqrt(2))
    assert radsimp(x/(y + r)/r, symbolic=False) == \
        -x*(-sqrt(2) + 1)/(y + 1 + sqrt(2))

    # issue sympy/sympy#7408
    eq = sqrt(x) / sqrt(y)
    assert radsimp(eq) == UMul(sqrt(x), sqrt(y), 1 / y)
    assert radsimp(eq, symbolic=False) == eq

    # issue sympy/sympy#7498
    assert radsimp(sqrt(x) / sqrt(y)**3) == UMul(sqrt(x), sqrt(y**3), 1 / y**3)

    # for coverage
    eq = sqrt(x) / y**2
    assert radsimp(eq) == eq

    assert (radsimp(1 / (1 + r2 / 3 + r3 / 5 + r5 + sqrt(r7))) == 15 /
            (3 * sqrt(3) + 5 * sqrt(2) + 15 + 15 * root(7, 4) + 15 * sqrt(5)))
Beispiel #26
0
def test_gruntz_evaluation():
    # Gruntz' thesis pp. 122 to 123
    # 8.1
    assert gruntz(exp(x) * (exp(1 / x - exp(-x)) - exp(1 / x)), x) == -1
    # 8.2
    assert gruntz(
        exp(x) *
        (exp(1 / x + exp(-x) + exp(-x**2)) - exp(1 / x - exp(-exp(x)))),
        x) == 1
    # 8.3
    assert gruntz(exp(exp(x - exp(-x)) / (1 - 1 / x)) - exp(exp(x)), x) == oo
    # 8.4
    assert gruntz(
        exp(exp(exp(x) / (1 - 1 / x))) -
        exp(exp(exp(x) / (1 - 1 / x - log(x)**(-log(x))))), x) == -oo
    # 8.5
    assert gruntz(exp(exp(exp(x + exp(-x)))) / exp(exp(exp(x))), x) == oo
    # 8.6
    assert gruntz(exp(exp(exp(x))) / exp(exp(exp(x - exp(-exp(x))))), x) == oo
    # 8.7
    assert gruntz(exp(exp(exp(x))) / exp(exp(exp(x - exp(-exp(exp(x)))))),
                  x) == 1
    # 8.8
    assert gruntz(exp(exp(x)) / exp(exp(x - exp(-exp(exp(x))))), x) == 1
    # 8.9
    assert gruntz(
        log(x)**2 * exp(
            sqrt(log(x)) *
            (log(log(x)))**2 * exp(sqrt(log(log(x))) *
                                   (log(log(log(x))))**3)) / sqrt(x), x) == 0
    # 8.10
    assert gruntz((x * log(x) * (log(x * exp(x) - x**2))**2) /
                  (log(log(x**2 + 2 * exp(exp(3 * x**3 * log(x)))))),
                  x) == Rational(1, 3)
    # 8.11
    assert gruntz(
        (exp(x * exp(-x) / (exp(-x) + exp(-2 * x**2 / (x + 1)))) - exp(x)) / x,
        x) == -exp(2)
    # 8.12
    assert gruntz((3**x + 5**x)**(1 / x), x) == 5
    # 8.13
    assert gruntz(x / log(x**(log(x**(log(2) / log(x))))), x) == oo
    # 8.14
    assert gruntz(
        exp(exp(2 * log(x**5 + x) * log(log(x)))) /
        exp(exp(10 * log(x) * log(log(x)))), x) == oo
    # 8.15
    assert gruntz(
        exp(
            exp(
                Rational(5, 2) * x**(-Rational(5, 7)) +
                Rational(21, 8) * x**Rational(6, 11) + 2 * x**(-8) +
                Rational(54, 17) * x**Rational(49, 45)))**8 /
        log(log(-log(Rational(4, 3) * x**(-Rational(5, 14)))))**Rational(7, 6),
        x) == oo
    # 8.16
    assert gruntz(
        (exp(4 * x * exp(-x) /
             (1 / exp(x) + 1 / exp(2 * x**2 / (x + 1)))) - exp(x)) / exp(x)**4,
        x) == 1
    # 8.17
    assert gruntz(exp(x*exp(-x)/(exp(-x) + exp(-2*x**2/(x + 1))))/exp(x), x) \
        == 1
    # 8.18
    assert gruntz(
        (exp(exp(-x / (1 + exp(-x)))) * exp(-x / (1 + exp(-x /
                                                          (1 + exp(-x))))) *
         exp(exp(-x + exp(-x / (1 + exp(-x)))))) /
        (exp(-x / (1 + exp(-x))))**2 - exp(x) + x, x) == 2
    # 8.19
    assert gruntz(
        log(x) * (log(log(x) + log(log(x))) - log(log(x))) /
        (log(log(x) + log(log(log(x))))), x) == 1
    # 8.20
    assert gruntz(
        exp((log(log(x + exp(log(x) * log(log(x)))))) /
            (log(log(log(exp(x) + x + log(x)))))), x) == E
    # Another
    assert gruntz(exp(exp(exp(x + exp(-x)))) / exp(exp(x)), x) == oo
Beispiel #27
0
def test_polygon():
    a, b, c = Point(0, 0), Point(2, 0), Point(3, 3)
    t = Triangle(a, b, c)
    assert Polygon(a) == a
    assert Polygon(a, 1, 1, n=4) == RegularPolygon(a, 1, 4, 1)
    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
    pytest.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
    assert p1.encloses(Circle(Point(2.5, 2.5), 5)) is False
    assert p1.encloses(Ellipse(Point(2.5, 2), 5, 6)) 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")
    pytest.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', extended_real=True): 0}) == \
        Point(0, 0)
    pytest.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)
    pytest.raises(GeometryError, lambda: RegularPolygon(Point(0, 0), Point(0,
                                                                           1), Point(1, 1)))
    pytest.raises(GeometryError, lambda: RegularPolygon(Point(0, 0), 1, 2))
    pytest.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)
    pytest.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 = Triangle(Point(Rational(100080156402737, 5000000000000),
                       Rational(79782624633431, 500000000000)),
                 Point(Rational(39223884078253, 2000000000000),
                       Rational(156345163124289, 1000000000000)),
                 Point(Rational(31241359188437, 1250000000000),
                       Rational(338338270939941, 1000000000000000)))
    assert t.orthocenter == \
        Point(Rational(-78066086905059984021699779471538701955848721853,
                       80368430960602242240789074233100000000000000),
              Rational(20151573611150265741278060334545897615974257,
                       160736861921204484481578148466200000000000))

    # 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")
    pytest.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) == 2*sqrt(2)/5
Beispiel #28
0
def test_sympyissue_6990():
    assert (sqrt(a + b*x + x**2)).series(x, 0, 3).removeO() == \
        b*x/(2*sqrt(a)) + x**2*(1/(2*sqrt(a)) -
                                b**2/(8*a**Rational(3, 2))) + sqrt(a)
Beispiel #29
0
def test_match_bug6():
    p = Wild('p')
    e = x
    assert e.match(3 * p * x) == {p: Rational(1) / 3}
Beispiel #30
0
def test_minpoly_fraction_field_slow():
    assert minimal_polynomial(
        minimal_polynomial(sqrt(x**Rational(1, 5) - 1),
                           y).subs(y, sqrt(x**Rational(1, 5) - 1)), z) == z