Exemple #1
0
def poly_E(eps, eps_R, F, P):
    r"""
    Function to obtain the polynomial E and its roots in the s-domain.
    
    :param eps:     Constant term associated with S21
    :param eps_R:   Constant term associated with S11
    :param F:       Polynomial F, i.e., numerator of S11 (in s-domain)
    :param P:       Polynomial P, i.e., numerator of S21 (in s-domain)
    
    For further explanation, see "filter theory notes" by the author.
                  
    :rtype:         [polynomial_E, roots_E] ... where polynomial E is the 
                    denominator of S11 and S21
    """
    N = len(F)
    nfz = len(P)
    if ((N + nfz) % 2 == 0) and (abs(eps.real) > 0):
        warnings.warn("'eps' value should be pure imaginary when (N+nfz) is an even number")
    elif ((N + nfz + 1) % 2 == 0) and (abs(eps.imag) > 0):
        warnings.warn("'eps' value should be pure real when (N+nfz) is an odd number")
    s = sp.Symbol("s")
    poly_P = sp.Poly(P.ravel().tolist(), s)
    poly_F = sp.Poly(F.ravel().tolist(), s)
    poly_E = eps_R * poly_P + eps * poly_F
    roots_E = I_to_i(sp.nroots(poly_E))
    roots_E = np.reshape(roots_E, (len(roots_E), -1))
    roots_E = -abs(roots_E.real) + 1j * roots_E.imag  # all roots to the LHS
    # create the polynomial from the obtained LHS roots
    poly_E = np.poly(roots_E.ravel().tolist())
    poly_E = np.reshape(poly_E, (len(poly_E), -1))
    return poly_E, roots_E
Exemple #2
0
    def risco_root_sanity(self):
        r, chi = spy.symbols("r, chi")
        p = (r * (r - 6))**2 - self.s_bh**2 * (2 * r *
                                               (3 * r + 14) - 9 * self.s_bh**2)
        p2 = (r * (r - 6))**2 - chi**2 * (2 * r * (3 * r + 14) - 9 * chi**2)

        return p2.as_poly(), spy.nroots(p)
Exemple #3
0
def solve_poly_inequality_numerically(expr, b, strict):
    poly = expr - b
    symX = get_poly_symbol(expr)
    # Obtain numerical roots.
    roots = sympy.nroots(poly)
    zeros = sorted([r for r in roots if r.is_real])
    if not zeros:
        return sympy.EmptySet
    # Construct intervals around roots.
    mk_intvl = lambda a, b: \
        Interval(a, b, left_open=strict, right_open=strict)
    intervals = list(
        chain([mk_intvl(-oo, zeros[0])],
              [mk_intvl(x, y) for x, y in zip(zeros, zeros[1:])],
              [mk_intvl(zeros[-1], oo)]))
    # Define probe points.
    xs_probe = list(
        chain([zeros[0] - 1 / 2],
              [(i.left + i.right) / 2
               for i in intervals[1:-1] if isinstance(i, Interval)],
              [zeros[-1] + 1 / 2]))
    # Evaluate poly at the probe points.
    f_xs_probe = [poly.subs(symX, x) for x in xs_probe]
    # Return intervals where poly is less than zero.
    idxs = [i for i, fx in enumerate(f_xs_probe) if fx < 0]
    return make_union(*[intervals[i] for i in idxs])
Exemple #4
0
def poly_E(eps, eps_R, F, P):
    r"""
    Function to obtain the polynomial E and its roots in the s-domain.
    
    :param eps:     Constant term associated with S21
    :param eps_R:   Constant term associated with S11
    :param F:       Polynomial F, i.e., numerator of S11 (in s-domain)
    :param P:       Polynomial P, i.e., numerator of S21 (in s-domain)
    
    For further explanation, see "filter theory notes" by the author.
                  
    :rtype:         [polynomial_E, roots_E] ... where polynomial E is the 
                    denominator of S11 and S21
    """
    N = len(F);
    nfz = len(P)
    if (((N + nfz) % 2 == 0) and (abs(eps.real) > 0)):
        warnings.warn("'eps' value should be pure imaginary when (N+nfz) is an even number")
    elif (((N + nfz + 1) % 2 == 0) and (abs(eps.imag) > 0)):
        warnings.warn("'eps' value should be pure real when (N+nfz) is an odd number")
    s = sp.Symbol('s')
    poly_P = sp.Poly(P.ravel().tolist(), s)
    poly_F = sp.Poly(F.ravel().tolist(), s)
    poly_E = eps_R * poly_P + eps * poly_F
    roots_E = I_to_i(sp.nroots(poly_E))
    roots_E = np.reshape(roots_E, (len(roots_E), -1))
    roots_E = -abs(roots_E.real) + 1j * roots_E.imag  # all roots to the LHS
    # create the polynomial from the obtained LHS roots
    poly_E = np.poly(roots_E.ravel().tolist())
    poly_E = np.reshape(poly_E, (len(poly_E), -1))
    return poly_E, roots_E
Exemple #5
0
def atten_coeff(disp, solve_mode=0):
    k = Symbol('k')
    if solve_mode == 0:
        # Solve mode for transverse dispersion relation
        ksol = list(solveset(disp, k))[0]
    else:
        # Solve mode for longitudinal dispersion relation
        ksol = list(nroots(disp, maxsteps=100))[2]
    alpha = abs(im(ksol))
    return alpha
Exemple #6
0
    def risso_polar_sanity(self):
        r, chi = spy.symbols("r, chi")
        p2 = r**3 * (r**2 * (r - 6) + chi**2 *
                     (3 * r + 4)) + chi**4 * (3 * r * (r - 2) + chi**2)
        print(p2.as_poly())

        p = r**3 * (r**2 * (r - 6) + self.s_bh**2 *
                    (3 * r + 4)) + self.s_bh**4 * (3 * r *
                                                   (r - 2) + self.s_bh**2)

        return spy.nroots(p)
Exemple #7
0
 def CiLP(self):
     x = sp.symbols('x')
     eq1 = self.legendreP(self.s, False, True)
     eq2 = self.legendreP(self.s - 1, False, True)
     eq3 = sp.Mul(sp.Integer(-1), eq2)
     eq4 = sp.Add(eq1, eq3)
     P = sp.Poly(eq4, x)
     Pc = P.all_coeffs()
     roots = list(np.roots(Pc))[::-1]
     listeq = sp.nroots(sp.Poly(eq4, x), self.nd)
     return roots, listeq
Exemple #8
0
def poly_E(eps, eps_R, F, P):
    """Function to obtain the polynomial E and its roots in the s-domain."""
    s = sp.Symbol('s')
    poly_P = sp.Poly(P.ravel().tolist(), s)
    poly_F = sp.Poly(F.ravel().tolist(), s)
    poly_E = eps_R * poly_P + eps * poly_F
    roots_E = I_to_i(sp.nroots(poly_E))
    roots_E = np.reshape(roots_E, (len(roots_E), -1))
    roots_E = -abs(roots_E.real) + 1j * roots_E.imag # all roots to the LHS
    # create the polynomial from the obtained LHS roots
    poly_E = np.poly(roots_E.ravel().tolist())
    poly_E = np.reshape(poly_E, (len(poly_E), -1))
    return poly_E, roots_E
Exemple #9
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
Exemple #10
0
def px_value(
    x_: float,
    pz_: float,
    px_poly_eqn: Eq,
    px_var_: Symbol = px,
    pz_var_: Symbol = pz,
) -> float:
    """
    TODO.

    Args:
        TODO
    """
    px_poly_eqn_: Poly = poly(px_poly_eqn.subs({rx: x_, x: x_, pz_var_: pz_}))
    px_poly_roots: List[float] = nroots(px_poly_eqn_)
    pxgen: float = [
        root_ for root_ in px_poly_roots
        if Abs(im(root_)) < 1e-10 and re(root_) > 0
    ][0]
    return solve(Eq(px_poly_eqn_.gens[0], pxgen), px_var_)[0]
Exemple #11
0
def max_rE_gains_3d(order, numeric=True):
    """max rE for a given order is the largest root of the order+1 Legendre
    polynomial"""

    x = sp.symbols('x')
    lp = sp.legendre_poly(order + 1, x)

    # there are more efficient methods to find the roots of the Legendre
    # polynomials, but this is good enough for our purposes
    # See discussion at:
    #   https://math.stackexchange.com/questions/12160/roots-of-legendre-polynomial
    if order < 5 and not numeric:
        roots = sp.roots(lp)
    else:
        roots = sp.nroots(lp)

    # the roots can be in the keys of a dictionary or in a list,
    # this works for either one
    max_rE = np.max([*roots])

    return [sp.legendre(n, max_rE) for n in range(order + 1)]
Exemple #12
0
def poleres2pz(poles,residues,offset,slope, maxsteps=500):
    """
    Convert a pole-resiude representation of a TF -- 
     
        f(s) = Sum [r/(s-p)] + k + s * h 

    to a pole zero representation

        f(s) = Sum[s-z] / Sum[s-p]
    """
    from sympy import Symbol, together, expand, numer, denom, nroots

    x = Symbol('x')
    f = sum([r/(x-p) for p,r in zip(poles, residues)])
    f += offset + slope*x

    ft = together(f)
    nf = expand(numer(ft))

    zeros = nroots(nf,maxsteps=maxsteps)
    return np.array([complex(xx) for xx in poles]), np.array([complex(xx) for
                                                              xx in zeros])
Exemple #13
0
    def risso(self, tol=1e-5):
        r = spy.symbols("r")

        c = np.cos(self.s_bh_tilt)

        x1 = self.s_bh**2 * (3 * self.s_bh**2 + 4 * r *
                             (2 * r - 3)) + r**2 * (15 * r * (r - 4) + 28)
        x2 = 6 * r**4 * (r**2 - 4)
        x = self.s_bh**2 * x1 - x2

        y1 = self.s_bh**4 * (self.s_bh**4 + r**2 * (7 * r * (3 * r - 4) + 36))
        y2 = 6 * r * (r - 2) * (self.s_bh**6 + 2 * r**3 *
                                (self.s_bh**2 * (3 * r + 2) + 3 * r**2 *
                                 (r - 2)))
        y = y1 + y2

        z = (r * (r - 6))**2 - self.s_bh**2 * (2 * r *
                                               (3 * r + 14) - 9 * self.s_bh**2)

        p = r**8 * z + self.s_bh**2 * (1 -
                                       c**2) * (self.s_bh**2 *
                                                (1 - c**2) * y - 2 * r**4 * x)
        roots = np.array(spy.nroots(p, maxsteps=int(1e4)), dtype="complex128")

        a = self.risso_polar()
        b = self.risco()

        R = [
            r.real for r in roots if r.real - tol <= max(a, b) and r.real +
            tol >= min(a, b) and r.imag < tol
        ]
        try:
            assert len(R) == 1
        except AssertionError:
            print(R)

        return R[0]
Exemple #14
0
def forecast_typhoon(userlocation, forecast, wind_7_r, wind_10_r):
    nearlocation = np.empty([2, 2])
    time_goin_10 = 0
    time_goout_10 = 0
    time_goin_10_first = 0
    time_goout_10_first = 0
    time_goin_10_second = 0
    time_goout_10_second = 0
    time_goin_7 = np.zeros([2])
    time_goout_7 = np.zeros([2])
    time_goin_7_first = 0
    time_goout_7_first = 0
    time_goin_7_second = 0
    time_goout_7_second = 0
    nearlat = 0
    nearlon = 0
    neartm = 0
    dist = np.sqrt((forecast[1, :] - userlocation[0]) ** 2 + (forecast[2, :] - userlocation[1]) ** 2)
    idx_near = np.argmin(dist)
    ###
    ###
    if idx_near == 0:
        idx_near_after = idx_near + 1
        a2 = (forecast[2, idx_near] - forecast[2, idx_near_after]) / (
            forecast[1, idx_near] - forecast[1, idx_near_after]
        )
        b2 = forecast[2, idx_near] - a2 * forecast[1, idx_near]
        dist2 = abs(a2 * userlocation[0] + b2 - userlocation[1]) * np.sqrt(a2 ** 2 + 1)
        nearlocation[0, 0] = -(a2 * b2 - a2 * userlocation[1] - userlocation[0]) / (a2 * a2 + 1)
        nearlocation[0, 1] = a2 * nearlocation[0, 0] + b2
        neardist = dist2
        if nearlocation[1, 0] < min(forecast[1, idx_near], forecast[1, idx_near_after]) or nearlocation[1, 0] > max(
            forecast[1, idx_near], forecast[1, idx_near_after]
        ):
            time_near = (forecast[0, idx_near_after] - forecast[0, idx_near]) * (dist2 - dist[idx_near]) / (
                dist[idx_near_after] - dist[idx_near]
            ) + forecast[0, idx_near]
        else:
            time_near = (forecast[0, idx_near_after] - forecast[0, idx_near]) * (dist[idx_near] - dist2) / (
                dist[idx_near_after] - dist[idx_near]
            ) + forecast[0, idx_near]
        if dist2 <= wind_10_r / 111:
            if time_near < forecast[0, idx_near] and dist[idx_near] <= wind_10_r / 111:
                # print('10 level wind influence me11')
                time_goout_10 = (forecast[0, idx_near_after] - forecast[0, idx_near]) * (
                    wind_10_r / 111 - dist[idx_near]
                ) / (dist[idx_near_after] - dist[idx_near]) + forecast[0, idx_near]
                time_goin_10 = -(time_goout_10 - time_near) + time_near
                nearlat = forecast[1, idx_near]
                nearlon = forecast[2, idx_near]
                neartm = forecast[0, idx_near]
            if time_near < forecast[0, idx_near] and dist[idx_near] > wind_10_r / 111:
                pass
                # print('10 level wind not influence me11')
            if time_near >= forecast[0, idx_near] and dist[idx_near] <= wind_10_r / 111:
                pass
                # print('10 level wind influence me12')
                time_goout_10 = (forecast[0, idx_near_after] - forecast[0, idx_near]) * (
                    wind_10_r / 111 - dist[idx_near]
                ) / (dist[idx_near_after] - dist[idx_near]) + forecast[0, idx_near]
                time_goin_10 = -(time_goout_10 - time_near) + time_near
                nearlat = nearlocation[0, 0]
                nearlon = nearlocation[0, 1]
                neartm = time_near
            if time_near >= forecast[0, idx_near] and dist[idx_near] > wind_10_r / 111:
                # print('10 level wind influence me13')
                time_goin_10 = (forecast[0, idx_near_after] - forecast[0, idx_near]) * (
                    -wind_10_r / 111 + dist[idx_near]
                ) / (dist[idx_near_after] - dist[idx_near]) + forecast[0, idx_near]
                time_goout_10 = (time_near - time_goin_10) + time_near
                nearlat = nearlocation[0, 0]
                nearlon = nearlocation[0, 1]
                neartm = time_near
        else:
            pass
            # print('10 level wind not influence me12')
        if dist2 <= wind_7_r / 111:
            if time_near < forecast[0, idx_near] and dist[idx_near] <= wind_7_r / 111:
                # print('7 level wind influence me11')
                time_goout_7 = (forecast[0, idx_near_after] - forecast[0, idx_near]) * (
                    wind_7_r / 111 - dist[idx_near]
                ) / (dist[idx_near_after] - dist[idx_near]) + forecast[0, idx_near]
                time_goin_7 = (time_goout_7 - time_near) + time_near
                nearlat = forecast[1, idx_near]
                nearlon = forecast[2, idx_near]
                neartm = forecast[0, idx_near]
            if time_near < forecast[0, idx_near] and dist[idx_near] > wind_7_r / 111:
                pass
                # print('7 level wind not influence me11')
            if time_near >= forecast[0, idx_near] and dist[idx_near] <= wind_7_r / 111:
                # print('7 level wind influence me12')
                time_goout_7 = (forecast[0, idx_near_after] - forecast[0, idx_near]) * (
                    wind_7_r / 111 - dist[idx_near]
                ) / (dist[idx_near_after] - dist[idx_near]) + forecast[0, idx_near]
                time_goin_7 = (time_goout_7 - time_near) + time_near
                nearlat = nearlocation[0, 0]
                nearlon = nearlocation[0, 1]
                neartm = time_near
            if time_near >= forecast[0, idx_near] and dist[idx_near] > wind_7_r / 111:
                # print('7 level wind influence me13')
                time_goin_7 = (forecast[0, idx_near_after] - forecast[0, idx_near]) * (
                    -wind_7_r / 111 + dist[idx_near]
                ) / (dist[idx_near_after] - dist[idx_near]) + forecast[0, idx_near]
                time_goout_7 = (time_near - time_goin_7) + time_near
                nearlat = nearlocation[0, 0]
                nearlon = nearlocation[0, 1]
                neartm = time_near
        else:
            pass
            # print('7 level wind not influence me12')
        time_goin_10 = time_goin_10_first
        time_goout_10 = time_goout_10_first
        time_goin_7 = time_goin_7_first
        time_goout_7 = time_goout_7_first
    ###
    ###
    if idx_near == np.size(dist) - 1:
        idx_near_before = idx_near - 1
        a1 = (forecast[2, idx_near] - forecast[2, idx_near_before]) / (
            forecast[1, idx_near] - forecast[1, idx_near_before]
        )
        b1 = forecast[2, idx_near] - a1 * forecast[1, idx_near]
        dist1 = abs(a1 * userlocation[0] + b1 - userlocation[1]) * np.sqrt(a1 ** 2 + 1)
        nearlocation[1, 0] = -(a1 * b1 - a1 * userlocation[1] - userlocation[0]) / (a1 * a1 + 1)
        nearlocation[1, 1] = a1 * nearlocation[1, 0] + b1
        neardist = dist1
        if nearlocation[1, 0] < min(forecast[1, idx_near], forecast[1, idx_near_before]) or nearlocation[1, 0] > max(
            forecast[1, idx_near], forecast[1, idx_near_before]
        ):
            time_near = (forecast[0, idx_near] - forecast[0, idx_near_before]) * (dist[idx_near] - dist1) / (
                dist[idx_near_before] - dist[idx_near]
            ) + forecast[0, idx_near]
        else:
            time_near = (forecast[0, idx_near] - forecast[0, idx_near_before]) * (dist1 - dist[idx_near]) / (
                dist[idx_near_before] - dist[idx_near]
            ) + forecast[0, idx_near]
        if dist1 <= wind_10_r / 111:
            if time_near < forecast[0, idx_near] and dist[idx_near] <= wind_10_r / 111:
                # print('10 level wind influence me21')
                time_goout_10_first = (forecast[0, idx_near] - forecast[0, idx_near_before]) * (
                    wind_10_r / 111 - dist[idx_near]
                ) / (dist[idx_near_before] - dist[idx_near]) + forecast[0, idx_near]
                time_goin_10_first = (time_goout_10_first - time_near) + time_near
                nearlat = nearlocation[1, 0]
                nearlon = nearlocation[1, 1]
                neartm = time_near
            if time_near < forecast[0, idx_near] and dist[idx_near] > wind_10_r / 111:
                # print('10 level wind influence me22')
                time_goout_10_first = (forecast[0, idx_near] - forecast[0, idx_near_before]) * (
                    dist[idx_near] - wind_10_r / 111
                ) / (dist[idx_near_before] - dist[idx_near]) + forecast[0, idx_near]
                time_goin_10_first = (time_goout_10_first - time_near) + time_near
                nearlat = nearlocation[1, 0]
                nearlon = nearlocation[1, 1]
                neartm = time_near
            if time_near >= forecast[0, idx_near] and dist[idx_near] <= wind_10_r / 111:
                # print('10 level wind influence me23')
                time_goin_10_first = (forecast[0, idx_near] - forecast[0, idx_near_before]) * (
                    dist[idx_near] - wind_10_r / 111
                ) / (dist[idx_near_before] - dist[idx_near]) + forecast[0, idx_near]
                time_goout_10_first = (time_near - time_goin_10_first) + time_near
                nearlat = nearlocation[1, 0]
                nearlon = nearlocation[1, 1]
                neartm = time_near
            if time_near >= forecast[0, idx_near] and dist[idx_near] > wind_10_r / 111:
                # print('10 level wind influence me24')
                time_goin_10_first = (forecast[0, idx_near] - forecast[0, idx_near_before]) * (
                    dist[idx_near] - wind_10_r / 111
                ) / (dist[idx_near_before] - dist[idx_near]) + forecast[0, idx_near]
                time_goout_10_first = (time_near - time_goin_10_first) + time_near
                nearlat = nearlocation[1, 0]
                nearlon = nearlocation[1, 1]
                neartm = time_near
        else:
            pass
            # print('10 level wind not influence me21')
        if dist1 <= wind_7_r / 111:
            if time_near < forecast[0, idx_near] and dist[idx_near] <= wind_7_r / 111:
                # print('7 level wind influence me21')
                time_goout_7_first = (forecast[0, idx_near] - forecast[0, idx_near_before]) * (
                    wind_7_r / 111 - dist[idx_near]
                ) / (dist[idx_near_before] - dist[idx_near]) + forecast[0, idx_near]
                time_goin_7_first = (time_goout_7_first - time_near) + time_near
                nearlat = nearlocation[1, 0]
                nearlon = nearlocation[1, 1]
                neartm = time_near
            if time_near < forecast[0, idx_near] and dist[idx_near] > wind_7_r / 111:
                # print('7 level wind influence me22')
                time_goout_7_first = (forecast[0, idx_near] - forecast[0, idx_near_before]) * (
                    dist[idx_near] - wind_7_r / 111
                ) / (dist[idx_near_before] - dist[idx_near]) + forecast[0, idx_near]
                time_goin_7_first = (time_goout_7_first - time_near) + time_near
                nearlat = nearlocation[1, 0]
                nearlon = nearlocation[1, 1]
                neartm = time_near
            if time_near >= forecast[0, idx_near] and dist[idx_near] <= wind_7_r / 111:
                # print('7 level wind influence me23')
                time_goin_7_first = (forecast[0, idx_near] - forecast[0, idx_near_before]) * (
                    dist[idx_near] - wind_7_r / 111
                ) / (dist[idx_near_before] - dist[idx_near]) + forecast[0, idx_near]
                time_goout_7_first = (time_near - time_goin_7_first) + time_near
                nearlat = nearlocation[1, 0]
                nearlon = nearlocation[1, 1]
                neartm = time_near
            if time_near >= forecast[0, idx_near] and dist[idx_near] > wind_7_r / 111:
                # print('7 level wind influence me24')
                time_goin_7_first = (forecast[0, idx_near] - forecast[0, idx_near_before]) * (
                    dist[idx_near] - wind_7_r / 111
                ) / (dist[idx_near_before] - dist[idx_near]) + forecast[0, idx_near]
                time_goout_7_first = (time_near - time_goin_7_first) + time_near
                nearlat = nearlocation[1, 0]
                nearlon = nearlocation[1, 1]
                neartm = time_near
        else:
            pass
            # print('7 level wind not influence me22')
        time_goin_10 = time_goin_10_first
        time_goout_10 = time_goout_10_first
        time_goin_7 = time_goin_7_first
        time_goout_7 = time_goout_7_first
        nearlat = nearlocation[1, 0]
        nearlon = nearlocation[1, 1]
        neartm = time_near
    ###
    ###
    if idx_near < np.size(dist) - 1 and idx_near > 0:
        idx_near_before = idx_near - 1
        idx_near_after = idx_near + 1
        xf = np.empty([3, 3])
        xf[0, 0] = forecast[1, idx_near] ** 2
        xf[1, 0] = forecast[1, idx_near_before] ** 2
        xf[2, 0] = forecast[1, idx_near_after] ** 2
        xf[0, 1] = forecast[1, idx_near]
        xf[1, 1] = forecast[1, idx_near_before]
        xf[2, 1] = forecast[1, idx_near_after]
        xf[0, 2] = 1
        xf[1, 2] = 1
        xf[2, 2] = 1
        y = [forecast[2, idx_near], forecast[2, idx_near_before], forecast[2, idx_near_after]]
        bounds_lat_min = min(forecast[1, idx_near_before : idx_near_after + 1])
        bounds_lat_max = max(forecast[1, idx_near_before : idx_near_after + 1])
        fac = np.linalg.solve(xf, y)
        from scipy.optimize import minimize_scalar

        f = lambda x: np.sqrt(
            (x - userlocation[0]) ** 2 + (fac[0] * x ** 2 + fac[1] * x + fac[2] - userlocation[1]) ** 2
        )
        rslt = minimize_scalar(f, bounds=(bounds_lat_min, bounds_lat_max))
        nearlat = rslt.x
        nearlon = fac[0] * nearlat ** 2 + fac[1] * nearlat + fac[2]
        neardist = f(nearlat)
        if nearlat < max(forecast[1, idx_near], forecast[1, idx_near_before]) and nearlat > min(
            forecast[1, idx_near], forecast[1, idx_near_before]
        ):
            neartm = (forecast[0, idx_near] - forecast[0, idx_near_before]) * (neardist - dist[idx_near]) / (
                dist[idx_near_before] - dist[idx_near]
            ) + forecast[0, idx_near]
        else:
            neartm = (forecast[0, idx_near_after] - forecast[0, idx_near]) * (dist[idx_near] - neardist) / (
                dist[idx_near_after] - dist[idx_near]
            ) + forecast[0, idx_near]
        lat = smy.Symbol("lat")
        lat_goinout_10raw = smy.nroots(
            (lat - userlocation[0]) ** 2
            + (fac[0] * lat ** 2 + fac[1] * lat + fac[1] - userlocation[1]) ** 2
            - (wind_10_r / 111) ** 2,
            n=6,
        )
        lat_goinout_7raw = smy.nroots(
            (lat - userlocation[0]) ** 2
            + (fac[0] * lat ** 2 + fac[1] * lat + fac[1] - userlocation[1]) ** 2
            - (wind_10_r / 111) ** 2,
            n=6,
        )
        lat_goinout_10 = [complex(flag).real for flag in lat_goinout_10raw]
        lat_goinout_7 = [complex(flag).real for flag in lat_goinout_7raw]
        lon_goinout_10 = [
            fac[0] * complex(flag).real ** 2 + fac[1] * complex(flag).real + fac[2] for flag in lat_goinout_10raw
        ]
        lon_goinout_7 = [
            fac[0] * complex(flag).real ** 2 + fac[1] * complex(flag).real + fac[2] for flag in lat_goinout_7raw
        ]
        if dist[idx_near] <= wind_10_r / 111:
            # print('10 level wind influence me31')
            if dist[idx_near_before] >= wind_10_r / 111:
                idx_in = idx_near_before
            else:
                idx_in = 0
            if dist[idx_near_after] >= wind_10_r / 111:
                idx_out = idx_near_after
            else:
                idx_out = np.size(dist) - 1
            a2 = (forecast[2, idx_near] - forecast[2, idx_out]) / (forecast[1, idx_near] - forecast[1, idx_out])
            b2 = forecast[2, idx_near] - a2 * forecast[1, idx_near]
            dist2 = abs(a2 * userlocation[0] + b2 - userlocation[1]) * np.sqrt(a2 ** 2 + 1)
            nearlocation[1, 0] = -(a2 * b2 - a2 * userlocation[1] - userlocation[0]) / (a2 * a2 + 1)
            nearlocation[1, 1] = a2 * nearlocation[1, 0] + b2
            a1 = (forecast[2, idx_near] - forecast[2, idx_in]) / (forecast[1, idx_near] - forecast[1, idx_in])
            b1 = forecast[2, idx_near] - a1 * forecast[1, idx_near]
            dist1 = abs(a1 * userlocation[0] + b1 - userlocation[1]) * np.sqrt(a1 ** 2 + 1)
            nearlocation[0, 0] = -(a1 * b1 - a1 * userlocation[1] - userlocation[0]) / (a1 * a1 + 1)
            nearlocation[0, 1] = a1 * nearlocation[0, 0] + b1
            neardist = min(dist1, dist2)
            if dist[idx_in] > wind_10_r / 111 and dist[idx_in + 1] <= wind_10_r / 111:
                time_goin_10_first = (forecast[0, idx_near] - forecast[0, idx_in]) * (
                    dist[idx_in] - (wind_10_r / 111)
                ) / (dist[idx_in] - dist[idx_near]) + forecast[0, idx_in]
            else:
                time_goin_10_first = (forecast[0, idx_near] - forecast[0, idx_in]) * (
                    dist[idx_in] - (wind_10_r / 111)
                ) / (dist[idx_in] - dist[idx_near]) + forecast[0, idx_in]
            if dist[idx_out] < wind_10_r / 111 and dist[idx_out + 1] >= wind_10_r / 111:
                time_goout_10_first = (-forecast[0, idx_near] + forecast[0, idx_out]) * (
                    -dist[idx_out] + (wind_10_r / 111)
                ) / (dist[idx_out] - dist[idx_near]) + forecast[0, idx_out]
                # print "311"
            else:
                time_goout_10_first = (-forecast[0, idx_near] + forecast[0, idx_out]) * (
                    -dist[idx_out] + (wind_10_r / 111)
                ) / (dist[idx_out] - dist[idx_near]) + forecast[0, idx_out]
                # print "312"
                time_goin_10_second = 0
                time_goout_10_second = 0
        else:
            if neardist > wind_10_r / 111:
                pass
                # print('10 level wind not influence me31')
            else:
                # print('10 level wind influence me32')
                dist_from_before = min(
                    np.sqrt(
                        (lat_goinout_10 - forecast[1, idx_near_before]) ** 2
                        + (lon_goinout_10 - forecast[2, idx_near_before]) ** 2
                    )
                )
                dist_from_after = min(
                    np.sqrt(
                        (lat_goinout_10 - forecast[1, idx_near_after]) ** 2
                        + (lon_goinout_10 - forecast[2, idx_near_after]) ** 2
                    )
                )
                dist_near_from_before = np.sqrt(
                    (nearlat - forecast[1, idx_near_before]) ** 2 + (nearlon - forecast[2, idx_near_before]) ** 2
                )
                dist_near_from_after = np.sqrt(
                    (nearlat - forecast[1, idx_near_after]) ** 2 + (nearlon - forecast[2, idx_near_after]) ** 2
                )
                time_goin_10_first = (
                    neartm - forecast[0, idx_near_before]
                ) * dist_from_before / dist_near_from_before + forecast[0, idx_near_before]
                time_goout_10_first = neartm - time_goin_10_first + neartm
                time_goout_10_second = (
                    neartm - forecast[0, idx_near_after]
                ) * dist_from_after / dist_near_from_after + forecast[0, idx_near_after]
                time_goin_10_second = -time_goout_10_second + neartm + neartm
        if time_goin_10_first != 0 and time_goin_10_second != 0:
            time_goin_10 = min(time_goin_10_first, time_goin_10_second)
        else:
            if time_goin_10_first == 0:
                time_goin_10 = time_goin_10_second
            if time_goin_10_second == 0:
                time_goin_10 = time_goin_10_first
        if time_goout_10_first != 0 and time_goout_10_second != 0:
            time_goin_10 = max(time_goout_10_first, time_goout_10_second)
        else:
            if time_goout_10_first == 0:
                time_goout_10 = time_goout_10_second
            if time_goout_10_second == 0:
                time_goout_10 = time_goout_10_first
        if dist[idx_near] <= wind_7_r / 111:
            # print('7 level wind influence me31')
            if dist[idx_near_before] >= wind_7_r / 111:
                idx_in = idx_near_before
            else:
                idx_in = 0
            if dist[idx_near_after] >= wind_7_r / 111:
                idx_out = idx_near_after
            else:
                idx_out = np.size(dist) - 1
            a2 = (forecast[2, idx_near] - forecast[2, idx_out]) / (forecast[1, idx_near] - forecast[1, idx_out])
            b2 = forecast[2, idx_near] - a2 * forecast[1, idx_near]
            dist2 = abs(a2 * userlocation[0] + b2 - userlocation[1]) * np.sqrt(a2 ** 2 + 1)
            nearlocation[1, 0] = -(a2 * b2 - a2 * userlocation[1] - userlocation[0]) / (a2 * a2 + 1)
            nearlocation[1, 1] = a2 * nearlocation[1, 0] + b2
            a1 = (forecast[2, idx_near] - forecast[2, idx_in]) / (forecast[1, idx_near] - forecast[1, idx_in])
            b1 = forecast[2, idx_near] - a1 * forecast[1, idx_near]
            dist1 = abs(a1 * userlocation[0] + b1 - userlocation[1]) * np.sqrt(a1 ** 2 + 1)
            nearlocation[0, 0] = -(a1 * b1 - a1 * userlocation[1] - userlocation[0]) / (a1 * a1 + 1)
            nearlocation[0, 1] = a1 * nearlocation[0, 0] + b1
            neardist = min(dist1, dist2)
            if dist[idx_in] > wind_7_r / 111 and dist[idx_in + 1] <= wind_7_r / 111:
                time_goin_7_first = (forecast[0, idx_near] - forecast[0, idx_in]) * (
                    dist[idx_in] - (wind_7_r / 111)
                ) / (dist[idx_in] - dist[idx_near]) + forecast[0, idx_in]
            else:
                time_goin_7_first = (forecast[0, idx_near] - forecast[0, idx_in]) * (
                    dist[idx_in] - (wind_7_r / 111)
                ) / (dist[idx_in] - dist[idx_near]) + forecast[0, idx_in]
            if dist[idx_out] < wind_7_r / 111 and dist[idx_out + 1] >= wind_7_r / 111:
                time_goout_7_first = (-forecast[0, idx_near] + forecast[0, idx_out]) * (
                    -dist[idx_out] + (wind_7_r / 111)
                ) / (dist[idx_out] - dist[idx_near]) + forecast[0, idx_out]
            else:
                time_goout_7_first = (-forecast[0, idx_near] + forecast[0, idx_out]) * (
                    -dist[idx_out] + (wind_7_r / 111)
                ) / (dist[idx_out] - dist[idx_near]) + forecast[0, idx_out]
                time_goin_7_second = 0
                time_goout_7_second = 0
        else:
            if neardist > wind_7_r / 111:
                pass
                # print('7 level wind not influence me31')
            else:
                # print('7 level wind influence me32')
                dist_from_before = min(
                    np.sqrt(
                        (lat_goinout_7 - forecast[1, idx_near_before]) ** 2
                        + (lon_goinout_7 - forecast[2, idx_near_before]) ** 2
                    )
                )
                dist_from_after = min(
                    np.sqrt(
                        (lat_goinout_7 - forecast[1, idx_near_after]) ** 2
                        + (lon_goinout_7 - forecast[2, idx_near_after]) ** 2
                    )
                )
                dist_near_from_before = np.sqrt(
                    (nearlat - forecast[1, idx_near_before]) ** 2 + (nearlon - forecast[2, idx_near_before]) ** 2
                )
                dist_near_from_after = np.sqrt(
                    (nearlat - forecast[1, idx_near_after]) ** 2 + (nearlon - forecast[2, idx_near_after]) ** 2
                )
                time_goin_7_first = (
                    neartm - forecast[0, idx_near_before]
                ) * dist_from_before / dist_near_from_before + forecast[0, idx_near_before]
                time_goout_7_first = neartm - time_goin_7_first + neartm
                time_goout_7_second = (
                    neartm - forecast[0, idx_near_after]
                ) * dist_from_after / dist_near_from_after + forecast[0, idx_near_after]
                time_goin_7_second = -time_goout_7_second + neartm + neartm
        if time_goin_7_first != 0 and time_goin_7_second != 0:
            time_goin_7 = min(time_goin_7_first, time_goin_7_second)
        else:
            if time_goin_7_first == 0:
                time_goin_7 = time_goin_7_second
            if time_goin_7_second == 0:
                time_goin_7 = time_goin_7_first
        if time_goout_7_first != 0 and time_goout_7_second != 0:
            time_goin_7 = max(time_goout_7_first, time_goout_7_second)
        else:
            if time_goout_7_first == 0:
                time_goout_7 = time_goout_7_second
            if time_goout_7_second == 0:
                time_goout_7 = time_goout_7_first
        # print time_goin_7_first,time_goin_10_first,time_goin_7_second,time_goin_10_second
        # print time_goout_7_first,time_goout_10_first,time_goout_7_second,time_goout_10_second
    return (
        time_goin_10,
        time_goout_10,
        time_goin_7,
        time_goout_7,
        nearlat,
        nearlon,
        neartm,
        neardist * 111,
        neardist * 111 - wind_10_r,
        neardist * 111 - wind_7_r,
    )
Exemple #15
0
r2 = lambdify(t, e_2, 'numpy')

print [i for i in r2(1)]

mpm.mp.dps = 15

x1, x2, x3, x4, x5, x6, x7, x8 = symbols('x1, x2, x3, x4, x5, x6, x7, x8')
                         
e_3 = Poly(x1*x2 + x3*(x1**2) + x4*(x1**3) + x5*(x1**4) + x6*(x1**5) + 
          x7*(x1**6), (x1, x2, x3, x4, x5, x6, x7))


#Bien, un punto singular!
#r3 = nsolve((e_3 - 863, e_3 - 538, e_3 - 778, e_3 - 638, e_3 - 430, e_3 - 644, 
#             e_3 - 740), (x1, x2, x3, x4, x5, x6, x7), (-0.23, 110, 230, 440, 
#             330, 50, -10))


p2 = Poly(q**5 +2*q +1, q)

r4 = nroots(Poly(p2, q), 15)

r5 = newton_method(0, w, 5)

r6 = newton_method(0, m, 12)

r7 = chi2(p_reader)

r8 = integrate(L(p_reader), (j, -100, 100))

print L(p_reader, 0.00000000001)
Exemple #16
0
    f10= (x**3)*y**4 + 4*x**2*y**2 + 2*x**3*y - 1

    f = f9
    n = sympy.degree(f,y)
    coeffs = coeff_functions(sympy.poly(f,y),x)

    # compute the path parameterization around a given branch point
    bpt = 3
    G = monodromy_graph(f,x,y)
    path_segments = path_around_branch_point(G,bpt,1)
    nseg = len(path_segments)


    # select a root at the base_point
    a = G.nodes[0]['basepoint']
    fibre = map(numpy.complex,sympy.nroots(f.subs(x,a),n=15))

    # analytically continue
    ypath = []
    for k in range(nseg):
#        print("=== segment %k ===")
#        print("fibre (start) =")
#        for yj in fibre: print(yj)

        ypath += analytically_continue(df,coeffs,n,fibre,1,path_segments[k][0])
        fibre = [yij for yij in ypath[-1]]

#        print("fibre (end)   =")
#        for yj in fibre: print(yj)

    # parse out yroots data: right now it's of the form
Exemple #17
0
# Choose non-wetting material
# 0 - Air
# 1 - Oil
# 2 - Gas
material_mode = 2

S0 = np.linspace(epsilon, 0.9, N)
w_array = [10, 100, 500, 1000, 5000]
w_label = [str(i) for i in w_array]
k = Symbol('k')

for i in range(len(w_array)):
    w = w_array[i]
    print('Progress: ' + str(int(100 * (i + 1) / 5)) + '%')
    disp_rel = longitudinal_disp(S0, w, material_mode)
    k_array = [list(nroots(d, maxsteps=100))[0:3] for d in disp_rel]
    for j in range(3):
        speed_array = [w / abs(re(e[j])) for e in k_array]
        attenuation_array = [abs(im(e[j])) for e in k_array]

        plt.figure(j)
        if j == 2:
            plt.plot(S0, speed_array, label=w_label[i])
        else:
            plt.semilogy(S0, speed_array, label=w_label[i])

        plt.figure(j + 3)
        plt.semilogy(S0, attenuation_array, label=w_label[i])

for j in range(3):
    plt.figure(j)
Exemple #18
0
from sympy.abc import x
from sympy import roots, nroots

f = 4 * x**3 + 2 * x - 3
roots(f)
nroots(f)
Exemple #19
0
def solve_poly_equality_numerically(expr, b):
    roots = sympy.nroots(expr - b)
    zeros = [r for r in roots if r.is_real]
    return FiniteReal(*zeros)
Exemple #20
0
def pade(A):
    X = np.subtract.outer(A[1:, 0], A[:, 0]).T
    column_size = np.size(A, 0)
    M = (A[0, 1] / (A[1:, 1])) - 1

    Z = np.zeros((column_size, column_size - 1))
    Z[0, 0] = M[0] / X[0, 0]
    Z[0, 1:] = (Z[0, 0] * X[0, 1:] / M[1:]) - 1

    for i in range(1, column_size - 1):
        Z[i, i] = Z[i - 1, i] / X[i, i]
        Z[i, i + 1:] = (Z[i, i] * X[i, i + 1:] / Z[i - 1, i + 1:]) - 1

    diag_z = Z.diagonal()

    def build_frac(a, z, size):
        plus_one = Rational(str(
            z[size - 2])) * (t - Rational(str(a[size - 2, 0]))) + Rational(1)
        frac = None
        for i in reversed(range(size - 1)):
            if i == 0:
                frac = Rational(str(a[0, 1])) / plus_one
            else:
                frac = (Rational(str(z[i - 1])) *
                        (t - Rational(str(a[i - 1, 0])))) / plus_one
            frac = frac.ratsimp()
            plus_one = Rational(1) + frac
        return frac

    eqa = build_frac(A, diag_z, column_size)

    diag_z = list(diag_z)
    diag_z[-1] = 0
    eqa_minus1 = build_frac(A, diag_z, column_size)

    eqa_der = eqa.diff(t)
    eqa_der = eqa_der.ratsimp()

    numerator, denominator = fraction(eqa_der)
    try:
        sol = nroots(numerator, n=15, maxsteps=100)
    except:
        try:
            sol = nroots(numerator, n=15, maxsteps=150)
        except:
            return None

    sol = np.array(sol, dtype=complex)
    num = len(sol)

    eqa_solve = np.empty(num, dtype=complex)
    eqa_der_solve = np.empty(num, dtype=complex)
    eqa_minus1_solve = np.empty(num, dtype=complex)

    for i in range(num):
        eqa_solve[i] = eqa.evalf(subs={t: sol[i]})
        eqa_der_solve[i] = eqa_der.evalf(subs={t: sol[i]})
        eqa_minus1_solve[i] = eqa_minus1.evalf(subs={t: sol[i]})

    df = pd.DataFrame()
    df['alpha'] = np.abs(sol)
    df['theta'] = np.angle(sol)

    df['real'] = eqa_solve.real
    df['imag'] = eqa_solve.imag

    df['real_der'] = eqa_der_solve.real
    df['imag_der'] = eqa_der_solve.imag
    df['abs_der'] = np.abs(eqa_der_solve)

    pade_error = eqa_solve - eqa_minus1_solve
    df['real_err'] = pade_error.real
    df['imag_err'] = pade_error.imag

    df = df.loc[df['theta'] > 0].reset_index()

    return df
Exemple #21
0
def forecast_typhoon(userlocation, forecast, wind_7_r, wind_10_r):
    nearlocation = np.empty([2, 2])
    time_goin_10 = 0
    time_goout_10 = 0
    time_goin_10_first = 0
    time_goout_10_first = 0
    time_goin_10_second = 0
    time_goout_10_second = 0
    time_goin_7 = np.zeros([2])
    time_goout_7 = np.zeros([2])
    time_goin_7_first = 0
    time_goout_7_first = 0
    time_goin_7_second = 0
    time_goout_7_second = 0
    nearlat = 0
    nearlon = 0
    neartm = 0
    dist = np.sqrt((forecast[1, :] - userlocation[0])**2 +
                   (forecast[2, :] - userlocation[1])**2)
    idx_near = np.argmin(dist)
    ###
    ###
    if idx_near == 0:
        idx_near_after = idx_near + 1
        a2 = (forecast[2, idx_near] - forecast[2, idx_near_after]) / (
            forecast[1, idx_near] - forecast[1, idx_near_after])
        b2 = forecast[2, idx_near] - a2 * forecast[1, idx_near]
        dist2 = abs(a2 * userlocation[0] + b2 -
                    userlocation[1]) * np.sqrt(a2**2 + 1)
        nearlocation[0, 0] = -(a2 * b2 - a2 * userlocation[1] -
                               userlocation[0]) / (a2 * a2 + 1)
        nearlocation[0, 1] = a2 * nearlocation[0, 0] + b2
        neardist = dist2
        if nearlocation[1, 0] < min(
                forecast[1, idx_near],
                forecast[1, idx_near_after]) or nearlocation[1, 0] > max(
                    forecast[1, idx_near], forecast[1, idx_near_after]):
            time_near = (forecast[0, idx_near_after] -
                         forecast[0, idx_near]) * (dist2 - dist[idx_near]) / (
                             dist[idx_near_after] -
                             dist[idx_near]) + forecast[0, idx_near]
        else:
            time_near = (forecast[0, idx_near_after] -
                         forecast[0, idx_near]) * (dist[idx_near] - dist2) / (
                             dist[idx_near_after] -
                             dist[idx_near]) + forecast[0, idx_near]
        if dist2 <= wind_10_r / 111:
            if time_near < forecast[
                    0, idx_near] and dist[idx_near] <= wind_10_r / 111:
                #print('10 level wind influence me11')
                time_goout_10 = (forecast[0, idx_near_after] - forecast[
                    0, idx_near]) * (wind_10_r / 111 - dist[idx_near]) / (
                        dist[idx_near_after] -
                        dist[idx_near]) + forecast[0, idx_near]
                time_goin_10 = -(time_goout_10 - time_near) + time_near
                nearlat = forecast[1, idx_near]
                nearlon = forecast[2, idx_near]
                neartm = forecast[0, idx_near]
            if time_near < forecast[
                    0, idx_near] and dist[idx_near] > wind_10_r / 111:
                pass
                #print('10 level wind not influence me11')
            if time_near >= forecast[
                    0, idx_near] and dist[idx_near] <= wind_10_r / 111:
                pass
                #print('10 level wind influence me12')
                time_goout_10 = (forecast[0, idx_near_after] - forecast[
                    0, idx_near]) * (wind_10_r / 111 - dist[idx_near]) / (
                        dist[idx_near_after] -
                        dist[idx_near]) + forecast[0, idx_near]
                time_goin_10 = -(time_goout_10 - time_near) + time_near
                nearlat = nearlocation[0, 0]
                nearlon = nearlocation[0, 1]
                neartm = time_near
            if time_near >= forecast[
                    0, idx_near] and dist[idx_near] > wind_10_r / 111:
                #print('10 level wind influence me13')
                time_goin_10 = (forecast[0, idx_near_after] - forecast[
                    0, idx_near]) * (-wind_10_r / 111 + dist[idx_near]) / (
                        dist[idx_near_after] -
                        dist[idx_near]) + forecast[0, idx_near]
                time_goout_10 = (time_near - time_goin_10) + time_near
                nearlat = nearlocation[0, 0]
                nearlon = nearlocation[0, 1]
                neartm = time_near
        else:
            pass
            #print('10 level wind not influence me12')
        if dist2 <= wind_7_r / 111:
            if time_near < forecast[
                    0, idx_near] and dist[idx_near] <= wind_7_r / 111:
                #print('7 level wind influence me11')
                time_goout_7 = (forecast[0, idx_near_after] - forecast[
                    0, idx_near]) * (wind_7_r / 111 - dist[idx_near]) / (
                        dist[idx_near_after] -
                        dist[idx_near]) + forecast[0, idx_near]
                time_goin_7 = (time_goout_7 - time_near) + time_near
                nearlat = forecast[1, idx_near]
                nearlon = forecast[2, idx_near]
                neartm = forecast[0, idx_near]
            if time_near < forecast[
                    0, idx_near] and dist[idx_near] > wind_7_r / 111:
                pass
                #print('7 level wind not influence me11')
            if time_near >= forecast[
                    0, idx_near] and dist[idx_near] <= wind_7_r / 111:
                #print('7 level wind influence me12')
                time_goout_7 = (forecast[0, idx_near_after] - forecast[
                    0, idx_near]) * (wind_7_r / 111 - dist[idx_near]) / (
                        dist[idx_near_after] -
                        dist[idx_near]) + forecast[0, idx_near]
                time_goin_7 = (time_goout_7 - time_near) + time_near
                nearlat = nearlocation[0, 0]
                nearlon = nearlocation[0, 1]
                neartm = time_near
            if time_near >= forecast[
                    0, idx_near] and dist[idx_near] > wind_7_r / 111:
                #print('7 level wind influence me13')
                time_goin_7 = (forecast[0, idx_near_after] - forecast[
                    0, idx_near]) * (-wind_7_r / 111 + dist[idx_near]) / (
                        dist[idx_near_after] -
                        dist[idx_near]) + forecast[0, idx_near]
                time_goout_7 = (time_near - time_goin_7) + time_near
                nearlat = nearlocation[0, 0]
                nearlon = nearlocation[0, 1]
                neartm = time_near
        else:
            pass
            #print('7 level wind not influence me12')
        time_goin_10 = time_goin_10_first
        time_goout_10 = time_goout_10_first
        time_goin_7 = time_goin_7_first
        time_goout_7 = time_goout_7_first
    ###
    ###
    if idx_near == np.size(dist) - 1:
        idx_near_before = idx_near - 1
        a1 = (forecast[2, idx_near] - forecast[2, idx_near_before]) / (
            forecast[1, idx_near] - forecast[1, idx_near_before])
        b1 = forecast[2, idx_near] - a1 * forecast[1, idx_near]
        dist1 = abs(a1 * userlocation[0] + b1 -
                    userlocation[1]) * np.sqrt(a1**2 + 1)
        nearlocation[1, 0] = -(a1 * b1 - a1 * userlocation[1] -
                               userlocation[0]) / (a1 * a1 + 1)
        nearlocation[1, 1] = a1 * nearlocation[1, 0] + b1
        neardist = dist1
        if nearlocation[1, 0] < min(
                forecast[1, idx_near],
                forecast[1, idx_near_before]) or nearlocation[1, 0] > max(
                    forecast[1, idx_near], forecast[1, idx_near_before]):
            time_near = (forecast[0, idx_near] - forecast[0, idx_near_before]
                         ) * (dist[idx_near] -
                              dist1) / (dist[idx_near_before] -
                                        dist[idx_near]) + forecast[0, idx_near]
        else:
            time_near = (forecast[0, idx_near] - forecast[0, idx_near_before]
                         ) * (dist1 - dist[idx_near]) / (
                             dist[idx_near_before] -
                             dist[idx_near]) + forecast[0, idx_near]
        if dist1 <= wind_10_r / 111:
            if time_near < forecast[
                    0, idx_near] and dist[idx_near] <= wind_10_r / 111:
                #print('10 level wind influence me21')
                time_goout_10_first = (
                    forecast[0, idx_near] - forecast[0, idx_near_before]
                ) * (wind_10_r / 111 -
                     dist[idx_near]) / (dist[idx_near_before] -
                                        dist[idx_near]) + forecast[0, idx_near]
                time_goin_10_first = (time_goout_10_first -
                                      time_near) + time_near
                nearlat = nearlocation[1, 0]
                nearlon = nearlocation[1, 1]
                neartm = time_near
            if time_near < forecast[
                    0, idx_near] and dist[idx_near] > wind_10_r / 111:
                #print('10 level wind influence me22')
                time_goout_10_first = (
                    forecast[0, idx_near] - forecast[0, idx_near_before]) * (
                        dist[idx_near] - wind_10_r / 111) / (
                            dist[idx_near_before] -
                            dist[idx_near]) + forecast[0, idx_near]
                time_goin_10_first = (time_goout_10_first -
                                      time_near) + time_near
                nearlat = nearlocation[1, 0]
                nearlon = nearlocation[1, 1]
                neartm = time_near
            if time_near >= forecast[
                    0, idx_near] and dist[idx_near] <= wind_10_r / 111:
                #print('10 level wind influence me23')
                time_goin_10_first = (
                    forecast[0, idx_near] - forecast[0, idx_near_before]) * (
                        dist[idx_near] - wind_10_r / 111) / (
                            dist[idx_near_before] -
                            dist[idx_near]) + forecast[0, idx_near]
                time_goout_10_first = (time_near -
                                       time_goin_10_first) + time_near
                nearlat = nearlocation[1, 0]
                nearlon = nearlocation[1, 1]
                neartm = time_near
            if time_near >= forecast[
                    0, idx_near] and dist[idx_near] > wind_10_r / 111:
                #print('10 level wind influence me24')
                time_goin_10_first = (
                    forecast[0, idx_near] - forecast[0, idx_near_before]) * (
                        dist[idx_near] - wind_10_r / 111) / (
                            dist[idx_near_before] -
                            dist[idx_near]) + forecast[0, idx_near]
                time_goout_10_first = (time_near -
                                       time_goin_10_first) + time_near
                nearlat = nearlocation[1, 0]
                nearlon = nearlocation[1, 1]
                neartm = time_near
        else:
            pass
            #print('10 level wind not influence me21')
        if dist1 <= wind_7_r / 111:
            if time_near < forecast[
                    0, idx_near] and dist[idx_near] <= wind_7_r / 111:
                #print('7 level wind influence me21')
                time_goout_7_first = (
                    forecast[0, idx_near] - forecast[0, idx_near_before]
                ) * (wind_7_r / 111 -
                     dist[idx_near]) / (dist[idx_near_before] -
                                        dist[idx_near]) + forecast[0, idx_near]
                time_goin_7_first = (time_goout_7_first -
                                     time_near) + time_near
                nearlat = nearlocation[1, 0]
                nearlon = nearlocation[1, 1]
                neartm = time_near
            if time_near < forecast[
                    0, idx_near] and dist[idx_near] > wind_7_r / 111:
                #print('7 level wind influence me22')
                time_goout_7_first = (
                    forecast[0, idx_near] - forecast[0, idx_near_before]
                ) * (dist[idx_near] -
                     wind_7_r / 111) / (dist[idx_near_before] -
                                        dist[idx_near]) + forecast[0, idx_near]
                time_goin_7_first = (time_goout_7_first -
                                     time_near) + time_near
                nearlat = nearlocation[1, 0]
                nearlon = nearlocation[1, 1]
                neartm = time_near
            if time_near >= forecast[
                    0, idx_near] and dist[idx_near] <= wind_7_r / 111:
                #print('7 level wind influence me23')
                time_goin_7_first = (
                    forecast[0, idx_near] - forecast[0, idx_near_before]
                ) * (dist[idx_near] -
                     wind_7_r / 111) / (dist[idx_near_before] -
                                        dist[idx_near]) + forecast[0, idx_near]
                time_goout_7_first = (time_near -
                                      time_goin_7_first) + time_near
                nearlat = nearlocation[1, 0]
                nearlon = nearlocation[1, 1]
                neartm = time_near
            if time_near >= forecast[
                    0, idx_near] and dist[idx_near] > wind_7_r / 111:
                #print('7 level wind influence me24')
                time_goin_7_first = (
                    forecast[0, idx_near] - forecast[0, idx_near_before]
                ) * (dist[idx_near] -
                     wind_7_r / 111) / (dist[idx_near_before] -
                                        dist[idx_near]) + forecast[0, idx_near]
                time_goout_7_first = (time_near -
                                      time_goin_7_first) + time_near
                nearlat = nearlocation[1, 0]
                nearlon = nearlocation[1, 1]
                neartm = time_near
        else:
            pass
            #print('7 level wind not influence me22')
        time_goin_10 = time_goin_10_first
        time_goout_10 = time_goout_10_first
        time_goin_7 = time_goin_7_first
        time_goout_7 = time_goout_7_first
        nearlat = nearlocation[1, 0]
        nearlon = nearlocation[1, 1]
        neartm = time_near
    ###
    ###
    if idx_near < np.size(dist) - 1 and idx_near > 0:
        idx_near_before = idx_near - 1
        idx_near_after = idx_near + 1
        xf = np.empty([3, 3])
        xf[0, 0] = forecast[1, idx_near]**2
        xf[1, 0] = forecast[1, idx_near_before]**2
        xf[2, 0] = forecast[1, idx_near_after]**2
        xf[0, 1] = forecast[1, idx_near]
        xf[1, 1] = forecast[1, idx_near_before]
        xf[2, 1] = forecast[1, idx_near_after]
        xf[0, 2] = 1
        xf[1, 2] = 1
        xf[2, 2] = 1
        y = [
            forecast[2, idx_near], forecast[2, idx_near_before],
            forecast[2, idx_near_after]
        ]
        bounds_lat_min = min(forecast[1, idx_near_before:idx_near_after + 1])
        bounds_lat_max = max(forecast[1, idx_near_before:idx_near_after + 1])
        fac = np.linalg.solve(xf, y)
        from scipy.optimize import minimize_scalar
        f = lambda x: np.sqrt(
            (x - userlocation[0])**2 +
            (fac[0] * x**2 + fac[1] * x + fac[2] - userlocation[1])**2)
        rslt = minimize_scalar(f, bounds=(bounds_lat_min, bounds_lat_max))
        nearlat = rslt.x
        nearlon = fac[0] * nearlat**2 + fac[1] * nearlat + fac[2]
        neardist = f(nearlat)
        if nearlat < max(forecast[1, idx_near],
                         forecast[1, idx_near_before]) and nearlat > min(
                             forecast[1, idx_near], forecast[1,
                                                             idx_near_before]):
            neartm = (forecast[0, idx_near] - forecast[0, idx_near_before]) * (
                neardist - dist[idx_near]
            ) / (dist[idx_near_before] - dist[idx_near]) + forecast[0,
                                                                    idx_near]
        else:
            neartm = (forecast[0, idx_near_after] - forecast[0, idx_near]) * (
                dist[idx_near] - neardist
            ) / (dist[idx_near_after] - dist[idx_near]) + forecast[0, idx_near]
        lat = smy.Symbol('lat')
        lat_goinout_10raw = smy.nroots(
            (lat - userlocation[0])**2 +
            (fac[0] * lat**2 + fac[1] * lat + fac[1] - userlocation[1])**2 -
            (wind_10_r / 111)**2,
            n=6)
        lat_goinout_7raw = smy.nroots(
            (lat - userlocation[0])**2 +
            (fac[0] * lat**2 + fac[1] * lat + fac[1] - userlocation[1])**2 -
            (wind_10_r / 111)**2,
            n=6)
        lat_goinout_10 = [complex(flag).real for flag in lat_goinout_10raw]
        lat_goinout_7 = [complex(flag).real for flag in lat_goinout_7raw]
        lon_goinout_10 = [
            fac[0] * complex(flag).real**2 + fac[1] * complex(flag).real +
            fac[2] for flag in lat_goinout_10raw
        ]
        lon_goinout_7 = [
            fac[0] * complex(flag).real**2 + fac[1] * complex(flag).real +
            fac[2] for flag in lat_goinout_7raw
        ]
        if dist[idx_near] <= wind_10_r / 111:
            #print('10 level wind influence me31')
            if dist[idx_near_before] >= wind_10_r / 111:
                idx_in = idx_near_before
            else:
                idx_in = 0
            if dist[idx_near_after] >= wind_10_r / 111:
                idx_out = idx_near_after
            else:
                idx_out = np.size(dist) - 1
            a2 = (forecast[2, idx_near] - forecast[2, idx_out]) / (
                forecast[1, idx_near] - forecast[1, idx_out])
            b2 = forecast[2, idx_near] - a2 * forecast[1, idx_near]
            dist2 = abs(a2 * userlocation[0] + b2 -
                        userlocation[1]) * np.sqrt(a2**2 + 1)
            nearlocation[1, 0] = -(a2 * b2 - a2 * userlocation[1] -
                                   userlocation[0]) / (a2 * a2 + 1)
            nearlocation[1, 1] = a2 * nearlocation[1, 0] + b2
            a1 = (forecast[2, idx_near] - forecast[2, idx_in]) / (
                forecast[1, idx_near] - forecast[1, idx_in])
            b1 = forecast[2, idx_near] - a1 * forecast[1, idx_near]
            dist1 = abs(a1 * userlocation[0] + b1 -
                        userlocation[1]) * np.sqrt(a1**2 + 1)
            nearlocation[0, 0] = -(a1 * b1 - a1 * userlocation[1] -
                                   userlocation[0]) / (a1 * a1 + 1)
            nearlocation[0, 1] = a1 * nearlocation[0, 0] + b1
            neardist = min(dist1, dist2)
            if dist[idx_in] > wind_10_r / 111 and dist[idx_in +
                                                       1] <= wind_10_r / 111:
                time_goin_10_first = (forecast[0, idx_near] - forecast[
                    0, idx_in]) * (dist[idx_in] - (wind_10_r / 111)) / (
                        dist[idx_in] - dist[idx_near]) + forecast[0, idx_in]
            else:
                time_goin_10_first = (forecast[0, idx_near] - forecast[
                    0, idx_in]) * (dist[idx_in] - (wind_10_r / 111)) / (
                        dist[idx_in] - dist[idx_near]) + forecast[0, idx_in]
            if dist[idx_out] < wind_10_r / 111 and dist[idx_out +
                                                        1] >= wind_10_r / 111:
                time_goout_10_first = (-forecast[0, idx_near] + forecast[
                    0, idx_out]) * (-dist[idx_out] + (wind_10_r / 111)) / (
                        dist[idx_out] - dist[idx_near]) + forecast[0, idx_out]
                #print "311"
            else:
                time_goout_10_first = (-forecast[0, idx_near] + forecast[
                    0, idx_out]) * (-dist[idx_out] + (wind_10_r / 111)) / (
                        dist[idx_out] - dist[idx_near]) + forecast[0, idx_out]
                #print "312"
                time_goin_10_second = 0
                time_goout_10_second = 0
        else:
            if neardist > wind_10_r / 111:
                pass
                #print('10 level wind not influence me31')
            else:
                #print('10 level wind influence me32')
                dist_from_before = min(
                    np.sqrt(
                        (lat_goinout_10 - forecast[1, idx_near_before])**2 +
                        (lon_goinout_10 - forecast[2, idx_near_before])**2))
                dist_from_after = min(
                    np.sqrt((lat_goinout_10 - forecast[1, idx_near_after])**2 +
                            (lon_goinout_10 - forecast[2, idx_near_after])**2))
                dist_near_from_before = np.sqrt(
                    (nearlat - forecast[1, idx_near_before])**2 +
                    (nearlon - forecast[2, idx_near_before])**2)
                dist_near_from_after = np.sqrt(
                    (nearlat - forecast[1, idx_near_after])**2 +
                    (nearlon - forecast[2, idx_near_after])**2)
                time_goin_10_first = (
                    neartm - forecast[0, idx_near_before]
                ) * dist_from_before / dist_near_from_before + forecast[
                    0, idx_near_before]
                time_goout_10_first = neartm - time_goin_10_first + neartm
                time_goout_10_second = (
                    neartm - forecast[0, idx_near_after]
                ) * dist_from_after / dist_near_from_after + forecast[
                    0, idx_near_after]
                time_goin_10_second = -time_goout_10_second + neartm + neartm
        if time_goin_10_first != 0 and time_goin_10_second != 0:
            time_goin_10 = min(time_goin_10_first, time_goin_10_second)
        else:
            if time_goin_10_first == 0:
                time_goin_10 = time_goin_10_second
            if time_goin_10_second == 0:
                time_goin_10 = time_goin_10_first
        if time_goout_10_first != 0 and time_goout_10_second != 0:
            time_goin_10 = max(time_goout_10_first, time_goout_10_second)
        else:
            if time_goout_10_first == 0:
                time_goout_10 = time_goout_10_second
            if time_goout_10_second == 0:
                time_goout_10 = time_goout_10_first
        if dist[idx_near] <= wind_7_r / 111:
            #print('7 level wind influence me31')
            if dist[idx_near_before] >= wind_7_r / 111:
                idx_in = idx_near_before
            else:
                idx_in = 0
            if dist[idx_near_after] >= wind_7_r / 111:
                idx_out = idx_near_after
            else:
                idx_out = np.size(dist) - 1
            a2 = (forecast[2, idx_near] - forecast[2, idx_out]) / (
                forecast[1, idx_near] - forecast[1, idx_out])
            b2 = forecast[2, idx_near] - a2 * forecast[1, idx_near]
            dist2 = abs(a2 * userlocation[0] + b2 -
                        userlocation[1]) * np.sqrt(a2**2 + 1)
            nearlocation[1, 0] = -(a2 * b2 - a2 * userlocation[1] -
                                   userlocation[0]) / (a2 * a2 + 1)
            nearlocation[1, 1] = a2 * nearlocation[1, 0] + b2
            a1 = (forecast[2, idx_near] - forecast[2, idx_in]) / (
                forecast[1, idx_near] - forecast[1, idx_in])
            b1 = forecast[2, idx_near] - a1 * forecast[1, idx_near]
            dist1 = abs(a1 * userlocation[0] + b1 -
                        userlocation[1]) * np.sqrt(a1**2 + 1)
            nearlocation[0, 0] = -(a1 * b1 - a1 * userlocation[1] -
                                   userlocation[0]) / (a1 * a1 + 1)
            nearlocation[0, 1] = a1 * nearlocation[0, 0] + b1
            neardist = min(dist1, dist2)
            if dist[idx_in] > wind_7_r / 111 and dist[idx_in +
                                                      1] <= wind_7_r / 111:
                time_goin_7_first = (forecast[0, idx_near] - forecast[
                    0, idx_in]) * (dist[idx_in] - (wind_7_r / 111)) / (
                        dist[idx_in] - dist[idx_near]) + forecast[0, idx_in]
            else:
                time_goin_7_first = (forecast[0, idx_near] - forecast[
                    0, idx_in]) * (dist[idx_in] - (wind_7_r / 111)) / (
                        dist[idx_in] - dist[idx_near]) + forecast[0, idx_in]
            if dist[idx_out] < wind_7_r / 111 and dist[idx_out +
                                                       1] >= wind_7_r / 111:
                time_goout_7_first = (-forecast[0, idx_near] + forecast[
                    0, idx_out]) * (-dist[idx_out] + (wind_7_r / 111)) / (
                        dist[idx_out] - dist[idx_near]) + forecast[0, idx_out]
            else:
                time_goout_7_first = (-forecast[0, idx_near] + forecast[
                    0, idx_out]) * (-dist[idx_out] + (wind_7_r / 111)) / (
                        dist[idx_out] - dist[idx_near]) + forecast[0, idx_out]
                time_goin_7_second = 0
                time_goout_7_second = 0
        else:
            if neardist > wind_7_r / 111:
                pass
                #print('7 level wind not influence me31')
            else:
                #print('7 level wind influence me32')
                dist_from_before = min(
                    np.sqrt((lat_goinout_7 - forecast[1, idx_near_before])**2 +
                            (lon_goinout_7 - forecast[2, idx_near_before])**2))
                dist_from_after = min(
                    np.sqrt((lat_goinout_7 - forecast[1, idx_near_after])**2 +
                            (lon_goinout_7 - forecast[2, idx_near_after])**2))
                dist_near_from_before = np.sqrt(
                    (nearlat - forecast[1, idx_near_before])**2 +
                    (nearlon - forecast[2, idx_near_before])**2)
                dist_near_from_after = np.sqrt(
                    (nearlat - forecast[1, idx_near_after])**2 +
                    (nearlon - forecast[2, idx_near_after])**2)
                time_goin_7_first = (
                    neartm - forecast[0, idx_near_before]
                ) * dist_from_before / dist_near_from_before + forecast[
                    0, idx_near_before]
                time_goout_7_first = neartm - time_goin_7_first + neartm
                time_goout_7_second = (
                    neartm - forecast[0, idx_near_after]
                ) * dist_from_after / dist_near_from_after + forecast[
                    0, idx_near_after]
                time_goin_7_second = -time_goout_7_second + neartm + neartm
        if time_goin_7_first != 0 and time_goin_7_second != 0:
            time_goin_7 = min(time_goin_7_first, time_goin_7_second)
        else:
            if time_goin_7_first == 0:
                time_goin_7 = time_goin_7_second
            if time_goin_7_second == 0:
                time_goin_7 = time_goin_7_first
        if time_goout_7_first != 0 and time_goout_7_second != 0:
            time_goin_7 = max(time_goout_7_first, time_goout_7_second)
        else:
            if time_goout_7_first == 0:
                time_goout_7 = time_goout_7_second
            if time_goout_7_second == 0:
                time_goout_7 = time_goout_7_first
        #print time_goin_7_first,time_goin_10_first,time_goin_7_second,time_goin_10_second
        #print time_goout_7_first,time_goout_10_first,time_goout_7_second,time_goout_10_second
    return (time_goin_10, time_goout_10, time_goin_7, time_goout_7, nearlat,
            nearlon, neartm, neardist * 111, neardist * 111 - wind_10_r,
            neardist * 111 - wind_7_r)
    f10= (x**3)*y**4 + 4*x**2*y**2 + 2*x**3*y - 1

    f = f9
    n = sympy.degree(f,y)
    coeffs = coeff_functions(sympy.poly(f,y),x)

    # compute the path parameterization around a given branch point
    bpt = 3
    G = monodromy_graph(f,x,y)
    path_segments = path_around_branch_point(G,bpt,1)
    nseg = len(path_segments)


    # select a root at the base_point
    a = G.node[0]['basepoint']
    fibre = map(numpy.complex,sympy.nroots(f.subs(x,a),n=15))

    # analytically continue
    ypath = []
    for k in xrange(nseg):
#        print "=== segment %k ==="
#        print "fibre (start) ="
#        for yj in fibre: print yj

        ypath += analytically_continue(df,coeffs,n,fibre,1,path_segments[k][0])
        fibre = [yij for yij in ypath[-1]]

#        print "fibre (end)   ="
#        for yj in fibre: print yj

    # parse out yroots data: right now it's of the form