Exemplo n.º 1
0
def poly_plot(poly, size=[5, 5], show_plot=True):
    """Automatically make a plot that shows the roots and critical points of the polynomial"""

    if type(poly) == ZPoly:
        if poly.M:
            pts = zpoly_plot(poly)
            return pts
        else:
            poly = QPoly(poly.coef)

    if type(poly) == QPoly:
        r = qpoly_roots(poly)
        s = stationary_points(poly)
        c = inflection_points(poly)

        interesting_points = r + s + c

    elif type(poly) == RFunc:
        r = rfunc_roots(poly)
        a = rfunc_asymptotes(poly)

        interesting_points = r + a

    else:
        raise TypeError("Input must be QPoly, RFunc, or ZPoly")

    interesting_values = poly.evaluate(interesting_points)

    # Pick the minimum possible axes for the graph
    xlimit = [min(interesting_points), max(interesting_points)]
    ylimit = [min(interesting_values), max(interesting_values)]

    # Choose how much to pad
    xmargin = max(abs(xlimit[0] - xlimit[1]) / 10, 1)
    ymargin = max(abs(ylimit[0] - ylimit[1]) / 10, 1)

    # create the actual axes
    xwidth = [float(xlimit[0] - xmargin), float(xlimit[1] + xmargin)]
    ywidth = [float(ylimit[0] - ymargin), float(ylimit[1] + ymargin)]

    step_size = abs(xwidth[0] - xwidth[1]) / 100
    x = rational_seq(xwidth[0], xwidth[1], step_size)
    y = [float(i) for i in poly.evaluate(x)]
    x = [float(i) for i in x]

    pts = [i for i in zip(x, y)]

    if show_plot:
        make_canvas(xwidth,
                    ywidth,
                    size=size,
                    show_axes=True,
                    title=poly.pretty_name)
        plot_points(pts)
        connect([xwidth[0], 0], [xwidth[1], 0], color="gray", zorder=-1)

    return [(a, b) for a, b in zip(x, y)]
Exemplo n.º 2
0
def zpoly_plot(poly,size=[5,5]):
    """Automatically make a plot that shows the roots and critical points of the polynomial"""
        
    M = poly.M
    xwidth = [-1,M+1]
    ywidth = [-1,M+1]

    pts = zpoly_points(poly)
    
    make_canvas(xwidth,ywidth,size=size,show_axes=True,title=poly.pretty_name)
    scatter_points(pts)
        
    return pts
Exemplo n.º 3
0
    b = b0

    P = [[0, 0]]
    old = [0, 0]
    for x, y in [(a * 2, 0), (b, b), (0, a * 2), (-b, b), (-a * 2, 0),
                 (-b, -b), (0, -2 * a)]:
        new = [old[0] + x, old[1] + y]
        P.append(new)
        old = new

    return P


if __name__ == '__main__':
    from Utility import make_canvas, plot_points, connect

    make_canvas([-10, 10], size=6, show_axes=False)
    for xy in range(-10, 11):
        connect([-10, xy], [10, xy], color='gray', linewidth=.5)
        connect([xy, -10], [xy, 10], color='gray', linewidth=.5)

    for i in range(3):
        pts = integer_octagon(i)

        # Shift to center
        x_mn = sum([i[0] for i in pts]) / 8
        y_mn = sum([i[1] for i in pts]) / 8
        pts = [[x - x_mn, y - y_mn] for x, y in pts]

        plot_points(pts, color='k')
        connect(pts[0], pts[-1], color='k')
Exemplo n.º 4
0
def prim_pythag_triples(N):
    """N is the limit of m and n in Euclid's formula"""

    for m in range(1, N):
        for n in range(m + 1, N):
            if gcd(n, m) == 1:  # must be coprime
                if not (n % 2 == 1 and m % 2 == 1):  # can't both be odd
                    a = n * n - m * m
                    b = 2 * m * n
                    c = m * m + n * n

                    yield sorted([a, b, c])


if __name__ == '__main__':
    from Utility import make_canvas, scatter_points

    for i in prim_pythag_triples(10):
        print(i)

    make_canvas([-1.2, 1.2], size=6, title="Rational Points on a Circle")
    pts = []
    for i in prim_pythag_triples(11):
        pts += [[i[0] / i[2], i[1] / i[2]]]
        pts += [[-i[0] / i[2], i[1] / i[2]]]
        pts += [[i[0] / i[2], -i[1] / i[2]]]
        pts += [[-i[0] / i[2], -i[1] / i[2]]]

    scatter_points(pts, s=2)
Exemplo n.º 5
0
        ctr += 1
        k *= ctr
        yield out


if __name__ == '__main__':
    from Utility import make_canvas, scatter_points, show_plot

    print("Farey Sequence F_12")
    F12 = [i for i in farey_sequence(12)]
    print(F12)

    print()
    xy = question_mark_func(25)
    make_canvas(
        [-.02, 1.02],
        size=6,
        title="Minkowski's Question-mark Function\nAt Rational Arguments")
    scatter_points(xy, s=1)
    show_plot()

    print("\n\nHarmonic Progressions")
    S1 = harmonic_progression(a=1, d=1)
    for pos, val in enumerate(S1):
        if pos > 10:
            break
        print(val)

    print("\n\nA Harmonic Progression")
    S2 = harmonic_progression(a="1/3", d="2/9")
    for pos, val in enumerate(S2):
        if pos > 10: