Example #1
0
def find_dzdx_poly_root(
        dzdx_poly_: Poly,
        xhat_: float,
        xivhat0_: float,
        guess: float = 0.1,
        eta_: Optional[Rational] = None,
        method: str = "brentq",
        bracket: Tuple[float, float] = (0, 1e3),
) -> Any:
    """
    TODO.

    Args:
        TODO
    """
    if eta_ is not None and eta_ == Rational(1, 4):
        poly_eqn_ = dzdx_poly_.subs({xhat: xhat_, xivhat_0: xivhat0_})
        poly_lambda: Callable = lambdify([dzdx], poly_eqn_.as_expr())
        # return poly_eqn_
        dpoly_lambda: Optional[Callable] = lambdify(
            [dzdx],
            diff(poly_eqn_.as_expr(), dzdx) if method == "newton" else None,
        )
        bracket_: Optional[Tuple[float, float]] = (bracket if method
                                                   == "brentq" else None)
        for guess_ in [0, guess]:
            root_search = root_scalar(
                poly_lambda,
                fprime=dpoly_lambda,
                method=method,
                bracket=bracket_,
                x0=guess_,
            )
            if root_search.converged:
                break
        dzdx_poly_root: float = root_search.root
    else:
        dzdx_poly_roots = nroots(
            dzdx_poly_.subs({
                xhat: xhat_,
                xivhat_0: xivhat0_
            }))
        dzdx_poly_root = [
            root_ for root_ in dzdx_poly_roots
            if Abs(im(root_)) < 1e-10 and re(root_) > 0
        ][0]
    return dzdx_poly_root
Example #2
0
def horiz_line_bz_quadratic():
    x = Symbol('x')
    y = Symbol('y')
    t = Symbol('t')
    P = sympify('v_xx*x**2 + v_xy*x*y + v_yy*y**2 + v_x*x + v_y*y + v_0')
    X = sympify('x0')
    P = Poly(P.subs(x, X), y)
    print(P)
    return P
Example #3
0
def horiz_line_bz_line():
    x = Symbol('x')
    y = Symbol('y')
    t = Symbol('t')
    P = sympify('v_x*x + v_y*y + v_0')
    X = sympify('x0')
    P = Poly(P.subs(x, X), y)
    print(P)
    return P
Example #4
0
def circle_bz_cubic():
    x = Symbol('x')
    y = Symbol('y')
    t = Symbol('t')
    P = sympify('(x-cx)**2 + (y-cy)**2 - r**2')
    X = sympify('a3*t**3 + a2*t**2 + a1*t + a0')
    Y = sympify('b3*t**3 + b2*t**2 + b1*t + b0')
    P = Poly(P.subs(x, X).subs(y, Y), t)
    print(P)
Example #5
0
def circle_bz_line():
    x = Symbol('x')
    y = Symbol('y')
    t = Symbol('t')
    P = sympify('(x-cx)**2 + (y-cy)**2 - r**2')
    X = sympify('a1*t + a0')
    Y = sympify('b1*t + b0')
    P = Poly(P.subs(x, X).subs(y, Y), t)
    print(P)
def horiz_line_bz_line():
    x = Symbol('x')
    y = Symbol('y')
    t = Symbol('t')
    P = sympify('v_x*x + v_y*y + v_0')
    Y = sympify('y0')
    P = Poly(P.subs(y, Y), x)
    print(P)
    return P
def horiz_line_bz_cubic():
    x = Symbol('x')
    y = Symbol('y')
    t = Symbol('t')
    P = sympify(
        'v_xxx*x**3 + v_xxy*x**2*y + v_xyy*x*y**2 + v_yyy*y**3 + v_xx*x**2 + v_xy*x*y + v_yy*y**2 + v_x*x + v_y*y + v_0'
    )
    Y = sympify('y0')
    P = Poly(P.subs(y, Y), x)
    print(P)
    return P
Example #8
0
def ratint_ratpart(f, g, x):
    """Horowitz-Ostrogradsky algorithm.

       Given a field K and polynomials f and g in K[x], such that f and g
       are coprime and deg(f) < deg(g), returns fractions A and B in K(x),
       such that f/g = A' + B and B has square-free denominator.

    """
    f, g = Poly(f, x), Poly(g, x)

    u = poly_gcd(g, g.diff())
    v = poly_div(g, u)[0]

    n = u.degree - 1
    m = v.degree - 1
    d = g.degree

    A_coeff = [ Symbol('a' + str(n-i), dummy=True) for i in xrange(0, n+1) ]
    B_coeff = [ Symbol('b' + str(m-i), dummy=True) for i in xrange(0, m+1) ]

    symbols = A_coeff + B_coeff

    A = Poly(zip(A_coeff, xrange(n, -1, -1)), x)
    B = Poly(zip(B_coeff, xrange(m, -1, -1)), x)

    H = f - A.diff()*v + A*poly_div(u.diff()*v, u)[0] - B*u

    result = solve(H.coeffs, symbols)

    A = A.subs(result)
    B = B.subs(result)

    rat_part = Poly.cancel((A, u), x)
    log_part = Poly.cancel((B, v), x)

    return rat_part, log_part
def ratint_ratpart(f, g, x):
    """Horowitz-Ostrogradsky algorithm.

       Given a field K and polynomials f and g in K[x], such that f and g
       are coprime and deg(f) < deg(g), returns fractions A and B in K(x),
       such that f/g = A' + B and B has square-free denominator.

    """
    f, g = Poly(f, x), Poly(g, x)

    u = poly_gcd(g, g.diff())
    v = poly_div(g, u)[0]

    n = u.degree - 1
    m = v.degree - 1
    d = g.degree

    A_coeff = [ Symbol('a' + str(n-i), dummy=True) for i in xrange(0, n+1) ]
    B_coeff = [ Symbol('b' + str(m-i), dummy=True) for i in xrange(0, m+1) ]

    symbols = A_coeff + B_coeff

    A = Poly(zip(A_coeff, xrange(n, -1, -1)), x)
    B = Poly(zip(B_coeff, xrange(m, -1, -1)), x)

    H = f - A.diff()*v + A*poly_div(u.diff()*v, u)[0] - B*u

    result = solve(H.coeffs, symbols)

    A = A.subs(result)
    B = B.subs(result)

    rat_part = Poly.cancel((A, u), x)
    log_part = Poly.cancel((B, v), x)

    return rat_part, log_part
Example #10
0
    def mean_cooperation(self):
        expr = self.__get_replicator_rhs()
        if expr == 0:
            return 0.0
        p = Poly(expr, domain='RR')
        roots = sorted([float(r) for r in p.nroots(n=15, maxsteps=300) if r.is_real and r >= 0.00 and r <= 1.00])
        roots = [0.0] + roots + [1.0]

        coop = 0
        p = Poly(x * (1 - x) * expr, domain='RR')
        for i in range(1, len(roots)):
            difference = roots[i] - roots[i - 1]
            poly_val = p.subs(x, roots[i - i] + difference / 2)
            coop += roots[i - 1] * difference if poly_val < 0 else roots[i] * difference
        return coop
Example #11
0
def test_subs():
    p = Poly(t*x*y**2 + x*y + t**2, x, y)

    assert p.subs(x, 2) == \
        Poly(((2*t, 2, t**2), ((2,), (1,), (0,))), y)
    assert p.subs(y, 2) == \
        Poly(((2 + 4*t, t**2), ((1,), (0,))), x)

    assert p.subs(x, y) == \
        Poly(((t, 1, t**2), ((3,), (2,), (0,))), y)
    assert p.subs(y, x) == \
        Poly(((t, 1, t**2), ((3,), (2,), (0,))), x)

    assert p.subs(x, z) == \
        Poly(((t, 1, t**2), ((1, 2), (1, 1), (0, 0))), z, y)
    assert p.subs(y, z) == \
        Poly(((t, 1, t**2), ((1, 2), (1, 1), (0, 0))), x, z)
    assert p.subs(t, z) == \
        Poly(((z, 1, z**2), ((1, 2), (1, 1), (0, 0))), x, y)

    assert p.subs(z, t) == \
        Poly(((t, 1, t**2), ((1, 2), (1, 1), (0, 0))), x, y)

    assert p.subs(t, x) == \
        Poly(((1, 1, 1), ((2, 2), (2, 0), (1, 1))), x, y)
    assert p.subs(t, y) == \
        Poly(((1, 1, 1), ((1, 3), (1, 1), (0, 2))), x, y)

    assert p.subs(t, sin(1)) == \
        Poly(((sin(1), 1, sin(1)**2), ((1, 2), (1, 1), (0, 0))), x, y)

    assert p.subs(t, sin(x)) == \
        x*y**2*sin(x) + x*y + sin(x)**2

    assert p.subs(x, sin(x)) == \
        t*y**2*sin(x) + y*sin(x) + t**2
Example #12
0
def inversionCubicBezierForm():
    x3 = Symbol('x3')
    y3 = Symbol('y3')
    x2 = Symbol('x2')
    y2 = Symbol('y2')
    x1 = Symbol('x1')
    y1 = Symbol('y1')
    x0 = Symbol('x0')
    y0 = Symbol('y0')

    xi = Symbol('xi')
    yi = Symbol('yi')
    xj = Symbol('xj')
    yj = Symbol('yj')

    x = Symbol('x')
    y = Symbol('y')
    t = Symbol('t')

    def lij():
        M = Matrix([[x, y, 1], [xi, yi, 1], [xj, yj, 1]])
        D = det(M)
        return Poly(D)

    def bin3(i, j):
        return binomial(3, i) * binomial(3, j)

    ps = [[0.5, 0.25], [1, 1], [2, -1], [3, 0]]

    P32 = bin3(3, 2) * lij()
    l32 = Poly(P32.subs(xi, x3).subs(xj, x2).subs(yi, y3).subs(yj, y2), x, y)
    l32_ = l32.subs(x3,
                    ps[3][0]).subs(x2,
                                   ps[2][0]).subs(y3,
                                                  ps[3][1]).subs(y2, ps[2][1])

    P31 = bin3(3, 1) * lij()
    l31 = Poly(P31.subs(xi, x3).subs(xj, x1).subs(yi, y3).subs(yj, y1), x, y)
    l31_ = l31.subs(x3,
                    ps[3][0]).subs(x1,
                                   ps[1][0]).subs(y3,
                                                  ps[3][1]).subs(y1, ps[1][1])

    P30 = bin3(3, 0) * lij()
    l30 = Poly(P30.subs(xi, x3).subs(xj, x0).subs(yi, y3).subs(yj, y0), x, y)
    l30_ = l30.subs(x3,
                    ps[3][0]).subs(x0,
                                   ps[0][0]).subs(y3,
                                                  ps[3][1]).subs(y0, ps[0][1])

    P21 = bin3(2, 1) * lij()
    l21 = Poly(P21.subs(xi, x2).subs(xj, x1).subs(yi, y2).subs(yj, y1), x, y)
    l21_ = l21.subs(x2,
                    ps[2][0]).subs(x1,
                                   ps[1][0]).subs(y2,
                                                  ps[2][1]).subs(y1, ps[1][1])

    P20 = bin3(2, 0) * lij()
    l20 = Poly(P20.subs(xi, x2).subs(xj, x0).subs(yi, y2).subs(yj, y0), x, y)
    l20_ = l20.subs(x2,
                    ps[2][0]).subs(x0,
                                   ps[0][0]).subs(y2,
                                                  ps[2][1]).subs(y0, ps[0][1])

    P10 = bin3(1, 0) * lij()
    l10 = Poly(P10.subs(xi, x1).subs(xj, x0).subs(yi, y1).subs(yj, y0), x, y)
    l10_ = l10.subs(x1,
                    ps[1][0]).subs(x0,
                                   ps[0][0]).subs(y1,
                                                  ps[1][1]).subs(y0, ps[0][1])

    M = Matrix([[l32, l31, l30], [l31, l30 + l21, l20], [l30, l20, l10]])
    M_ = Matrix([[l32_, l31_, l30_], [l31_, l30_ + l21_, l20_],
                 [l30_, l20_, l10_]])

    T2 = sympify('t**2')
    T1 = sympify('t*(1-t)')
    T0 = sympify('(1-t)**2')
    T = Matrix([[T2], [T1], [T0]])

    INV1_ = expand(M_[0] * T2 + M_[1] * T1 + M_[2] * T0)
    INV2_ = expand(M_[3] * T2 + M_[4] * T1 + M_[5] * T0)
    INV3_ = expand(M_[6] * T2 + M_[7] * T1 + M_[8] * T0)

    INV1 = Poly(expand(M[0] * T2 + M[1] * T1 + M[2] * T0), t)
    INV2 = Poly(expand(M[3] * T2 + M[4] * T1 + M[5] * T0), t)
    INV3 = Poly(expand(M[6] * T2 + M[7] * T1 + M[8] * T0), t)

    p = [3, 0]

    Pt1 = INV1_.subs(x, p[0]).subs(y, p[1])
    Pt2 = INV2_.subs(x, p[0]).subs(y, p[1])
    Pt3 = INV3_.subs(x, p[0]).subs(y, p[1])
    t1 = solve(Pt1)
    t2 = solve(Pt2)
    t3 = solve(Pt3)

    print('INV1', INV1)
    print()
    print('INV2', INV2)
    print()
    print('INV3', INV3)
    print()

    print('Pt1: ', Pt1)
    print('Pt2: ', Pt2)
    print('Pt3: ', Pt3)
    print('t1: ', t1)
    print('t2: ', t2)
    print('t3: ', t3)
Example #13
0
    a = Symbol('a')

    symbols = []
    for symbol in polynom.free_symbols:
        symbols.append(str(symbol))

    symbols.sort()

    replace_symbols = {}
    j = 0
    for symbol in symbols:
        replace_symbols[symbol] = a**(d**j)
        j += 1

    for symbol in replace_symbols:
        polynom = polynom.subs(symbol, replace_symbols[symbol])

    p_new = (polynom._sorted_args)[0]._sorted_args
    f = 0
    for i in range(len(p_new) - 1, -1, -1):
        if i == 0 and str(p_new[i]).find('a') == -1:
            f += int(p_new[i])
        else:
            f += Poly(str(p_new[i]))
    p_coef = f.all_coeffs()
    for i in range(len(p_coef)):
        p_coef[i] = int(p_coef[i])

    kronecker(p_coef)

    ans.sort(key=len)
Example #14
0
def test_subs():
    p = Poly(t*x*y**2 + x*y + t**2, x, y)

    assert p.subs(x, 2) == \
        Poly(((2*t, 2, t**2), ((2,), (1,), (0,))), y)
    assert p.subs(y, 2) == \
        Poly(((2 + 4*t, t**2), ((1,), (0,))), x)

    assert p.subs(x, y) == \
        Poly(((t, 1, t**2), ((3,), (2,), (0,))), y)
    assert p.subs(y, x) == \
        Poly(((t, 1, t**2), ((3,), (2,), (0,))), x)

    assert p.subs(x, z) == \
        Poly(((t, 1, t**2), ((1, 2), (1, 1), (0, 0))), z, y)
    assert p.subs(y, z) == \
        Poly(((t, 1, t**2), ((1, 2), (1, 1), (0, 0))), x, z)
    assert p.subs(t, z) == \
        Poly(((z, 1, z**2), ((1, 2), (1, 1), (0, 0))), x, y)

    assert p.subs(z, t) == \
        Poly(((t, 1, t**2), ((1, 2), (1, 1), (0, 0))), x, y)

    assert p.subs(t, x) == \
        Poly(((1, 1, 1), ((2, 2), (2, 0), (1, 1))), x, y)
    assert p.subs(t, y) == \
        Poly(((1, 1, 1), ((1, 3), (1, 1), (0, 2))), x, y)

    assert p.subs(t, sin(1)) == \
        Poly(((sin(1), 1, sin(1)**2), ((1, 2), (1, 1), (0, 0))), x, y)

    assert p.subs(t, sin(x)) == \
        x*y**2*sin(x) + x*y + sin(x)**2

    assert p.subs(x, sin(x)) == \
        t*y**2*sin(x) + y*sin(x) + t**2

    coeffs = [ Symbol('a' + str(i)) for i in [3,2,1,0] ]
    values = [ 4, 0, 0, 1 ]

    f = Poly(zip(coeffs, [3,2,1,0]), x)

    assert f.subs(dict(zip(coeffs, values))) == Poly(4*x**3+1, x)
Example #15
0
    R = -R if R.degree() % 2 else R

    return resultant(G, R, res)


anss = 1
for gs in g_zeros:
    anss *= f_pol.eval(x, -gs)

anss = 1
for fs in f_zeros:
    anss *= g_pol.eval(x, -fs)

# 나눠:

_, r_pol = reduction(f_pol, Poly(g_pol.subs(x, -x), x))
anss = 1
print(r_pol)
for gs in g_zeros:
    anss *= r_pol.eval(-gs)

print(anss)

f, g, res = resultant(f_pol, g_pol, 1)
'''
f = invs(f.coeffs()[0])*f
f = Poly(f + pp, x, modulus = p)
g = invs(g.coeffs()[0])*g
g = Poly(g + pp, x, modulus = p)
'''
print(f, g, res)
Example #16
0
def test_subs():
    p = Poly(t*x*y**2 + x*y + t**2, x, y)

    assert p.subs(x, 2) == \
        Poly(((2*t, 2, t**2), ((2,), (1,), (0,))), y)
    assert p.subs(y, 2) == \
        Poly(((2 + 4*t, t**2), ((1,), (0,))), x)

    assert p.subs(x, y) == \
        Poly(((t, 1, t**2), ((3,), (2,), (0,))), y)
    assert p.subs(y, x) == \
        Poly(((t, 1, t**2), ((3,), (2,), (0,))), x)

    assert p.subs(x, z) == \
        Poly(((t, 1, t**2), ((1, 2), (1, 1), (0, 0))), z, y)
    assert p.subs(y, z) == \
        Poly(((t, 1, t**2), ((1, 2), (1, 1), (0, 0))), x, z)
    assert p.subs(t, z) == \
        Poly(((z, 1, z**2), ((1, 2), (1, 1), (0, 0))), x, y)

    assert p.subs(z, t) == \
        Poly(((t, 1, t**2), ((1, 2), (1, 1), (0, 0))), x, y)

    assert p.subs(t, x) == \
        Poly(((1, 1, 1), ((2, 2), (2, 0), (1, 1))), x, y)
    assert p.subs(t, y) == \
        Poly(((1, 1, 1), ((1, 3), (1, 1), (0, 2))), x, y)

    assert p.subs(t, sin(1)) == \
        Poly(((sin(1), 1, sin(1)**2), ((1, 2), (1, 1), (0, 0))), x, y)

    assert p.subs(t, sin(x)) == \
        x*y**2*sin(x) + x*y + sin(x)**2

    assert p.subs(x, sin(x)) == \
        t*y**2*sin(x) + y*sin(x) + t**2

    coeffs = [ Symbol('a' + str(i)) for i in [3,2,1,0] ]
    values = [ 4, 0, 0, 1 ]

    f = Poly(zip(coeffs, [3,2,1,0]), x)

    assert f.subs(dict(zip(coeffs, values))) == Poly(4*x**3+1, x)
Example #17
0
def test_subs():
    p = Poly(t * x * y**2 + x * y + t**2, x, y)

    assert p.subs(x, 2) == \
        Poly(((2*t, 2, t**2), ((2,), (1,), (0,))), y)
    assert p.subs(y, 2) == \
        Poly(((2 + 4*t, t**2), ((1,), (0,))), x)

    assert p.subs(x, y) == \
        Poly(((t, 1, t**2), ((3,), (2,), (0,))), y)
    assert p.subs(y, x) == \
        Poly(((t, 1, t**2), ((3,), (2,), (0,))), x)

    assert p.subs(x, z) == \
        Poly(((t, 1, t**2), ((1, 2), (1, 1), (0, 0))), z, y)
    assert p.subs(y, z) == \
        Poly(((t, 1, t**2), ((1, 2), (1, 1), (0, 0))), x, z)
    assert p.subs(t, z) == \
        Poly(((z, 1, z**2), ((1, 2), (1, 1), (0, 0))), x, y)

    assert p.subs(z, t) == \
        Poly(((t, 1, t**2), ((1, 2), (1, 1), (0, 0))), x, y)

    assert p.subs(t, x) == \
        Poly(((1, 1, 1), ((2, 2), (2, 0), (1, 1))), x, y)
    assert p.subs(t, y) == \
        Poly(((1, 1, 1), ((1, 3), (1, 1), (0, 2))), x, y)

    assert p.subs(t, sin(1)) == \
        Poly(((sin(1), 1, sin(1)**2), ((1, 2), (1, 1), (0, 0))), x, y)

    assert p.subs(t, sin(x)) == \
        x*y**2*sin(x) + x*y + sin(x)**2

    assert p.subs(x, sin(x)) == \
        t*y**2*sin(x) + y*sin(x) + t**2
Example #18
0
def inversionQuadraticBezierForm():
    x2 = Symbol('x2')
    y2 = Symbol('y2')
    x1 = Symbol('x1')
    y1 = Symbol('y1')
    x0 = Symbol('x0')
    y0 = Symbol('y0')

    xi = Symbol('xi')
    yi = Symbol('yi')
    xj = Symbol('xj')
    yj = Symbol('yj')

    x = Symbol('x')
    y = Symbol('y')
    t = Symbol('t')

    def lij():
        M = Matrix([[x, y, 1], [xi, yi, 1], [xj, yj, 1]])
        D = det(M)
        return Poly(D)

    def bin2(i, j):
        return binomial(2, i) * binomial(2, j)

    ps = [[0, -0.5], [0.25, 3], [2, -1]]

    P21 = bin2(2, 1) * lij()
    l21 = Poly(P21.subs(xi, x2).subs(xj, x1).subs(yi, y2).subs(yj, y1), x, y)
    l21_ = l21.subs(x2,
                    ps[2][0]).subs(x1,
                                   ps[1][0]).subs(y2,
                                                  ps[2][1]).subs(y1, ps[1][1])

    P20 = bin2(2, 0) * lij()
    l20 = Poly(P20.subs(xi, x2).subs(xj, x0).subs(yi, y2).subs(yj, y0), x, y)
    l20_ = l20.subs(x2,
                    ps[2][0]).subs(x0,
                                   ps[0][0]).subs(y2,
                                                  ps[2][1]).subs(y0, ps[0][1])

    P10 = bin2(1, 0) * lij()
    l10 = Poly(P10.subs(xi, x1).subs(xj, x0).subs(yi, y1).subs(yj, y0), x, y)
    l10_ = l10.subs(x1,
                    ps[1][0]).subs(x0,
                                   ps[0][0]).subs(y1,
                                                  ps[1][1]).subs(y0, ps[0][1])

    M = Matrix([[l21, l20], [l20, l10]])
    M_ = Matrix([[l21_, l20_], [l20_, l10_]])

    T1 = sympify('t')
    T0 = sympify('(1-t)')
    T = Matrix([[T1], [T0]])

    INV1_ = expand(M_[0] * T1 + M_[1] * T0)
    INV2_ = expand(M_[2] * T1 + M_[3] * T0)

    INV1 = Poly(expand(M[0] * T1 + M[1] * T0), t)
    INV2 = Poly(expand(M[2] * T1 + M[3] * T0), t)

    p = [2, -1]

    Pt1 = INV1_.subs(x, p[0]).subs(y, p[1])
    Pt2 = INV2_.subs(x, p[0]).subs(y, p[1])
    t1 = solve(Pt1)
    t2 = solve(Pt2)

    print('INV1', INV1)
    print()
    print('INV2', INV2)
    print()

    print('Pt1: ', Pt1)
    print('Pt2: ', Pt2)
    print('t1: ', t1)
    print('t2: ', t2)
Example #19
0
            list_grados.append(grado)
        if np.sum(list_grados) <= order:
            f_4 = f_4 + monos * coef1_2[k]
        k = k + 1
    return f_4


#%%

order = 4
K_x, coef = K_var_no_central([y, a], order)
eq1 = Poly(xpunto, x, y).subs(x, K_x)
eq1_filtrada = filtro_f(eq1, [x, y, a], order)

eq2 = Poly(diff(K_x, y) * ypunto, x, y, a)
eq2 = eq2.subs(x, K_x)
eq2_filtrada = filtro_f(eq2, [x, y, a], order)

eq_final = Poly(eq1_filtrada - eq2_filtrada, x, y, a)
eq_final_info = eq_final.as_expr().as_coefficients_dict()
monomios = eq_final_info.keys()
monomios2 = []
for monos in monomios:
    monomios2.append(monos)

list_res = {}
for j in range(len(monomios2)):
    monos = monomios2[j]
    for k in range(len(coef)):
        coeff = coef[k]
        res = solve(monos, coeff)