コード例 #1
0
def _assert_mp(t, ydot, yout):
    from mpmath import odefun
    f = odefun(ydot, 0, [1, 0, 0])
    for tidx, tval in enumerate(t):
        mpvals = f(tval)
        for sidx in range(3):
            assert abs(mpvals[sidx] - yout[tidx, 0, sidx]) < 1e-8
コード例 #2
0
def test_odefun_harmonic():
    mp.dps = 15
    # Harmonic oscillator
    f = odefun(lambda x, y: [-y[1], y[0]], 0, [1, 0])
    for x in [0, 1, 2.5, 8, 3.7]:  #  we go back to 3.7 to check caching
        c, s = f(x)
        assert c.ae(cos(x))
        assert s.ae(sin(x))
コード例 #3
0
ファイル: test_ode.py プロジェクト: 1zinnur9/pymaclab
def test_odefun_harmonic():
    mp.dps = 15
    # Harmonic oscillator
    f = odefun(lambda x, y: [-y[1], y[0]], 0, [1, 0])
    for x in [0, 1, 2.5, 8, 3.7]:    #  we go back to 3.7 to check caching
        c, s = f(x)
        assert c.ae(cos(x))
        assert s.ae(sin(x))
コード例 #4
0
def test_odefun_sinc_large():
    mp.dps = 15
    # Sinc function; test for large x
    f = sinc
    g = odefun(lambda x, y: [(cos(x) - y[0]) / x],
               1, [f(1)],
               tol=0.01,
               degree=5)
    assert abs(f(100) - g(100)[0]) / f(100) < 0.01
コード例 #5
0
def createPhi_s(y_point, L, G):
    rside = createRsideOnePendulumsEquationMPMATH(L, G)
    q = mp.odefun(rside,
                  0,
                  mp.matrix([0, y_point]),
                  tol=0.001,
                  degree=4,
                  method="taylor")
    return lambda t: q(t)[0]
コード例 #6
0
ファイル: mpmathInt.py プロジェクト: tylerjackoliver/DA
def main():

    # Initialise initial conditions

    mpm.dps = 32  # Quadruple precision calculations

    #T = 2. * mpm.mpf(1.657935803684451)
    #x0 = mpm.matrix([1.006057199440406, 0.0, 0.0, 0.022036094262319])

    #x0 = mpm.matrix([1.009433860747752, 0.0, 0.0, 0.003760276142823])
    x0 = mpm.matrix([
        1.0060556387918027, 2.4281528205782993e-06, -8.1318569756237198e-06,
        0.022040604156795093
    ])  # Velocities have been inverted (+ -> -) to spoof reverse time
    # T = mpm.mpf(2 * 3.061660616555952)

    x_des = mpm.matrix([
        1.0039053449563018, 0.0024706864707973696, -0.012281740632575283,
        0.024133313701580495
    ])

    # Call the Taylor integrator

    f = mpm.odefun(PCR3BP, 0.0, x0, tol=1e-30, degree=100, verbose=True)

    #for i in range(4):

    #    print(f(T)[i] - f(0.0)[i])

    print(mpm.pi)

    min_dist = 10.

    with open('output.dat', 'w+') as fiyul:

        for time in mpm.linspace(0., 2 * mpm.pi, 1000):

            temp = f(time)
            distance = mpm.sqrt((temp[0] - x_des[0])**mpm.mpf(2.0) +
                                (temp[1] - x_des[1])**mpm.mpf(2.0))
            print("Distance between desired point: %s" % (distance))

            if distance < min_dist:

                min_dist = distance

            fiyul.write("%s, %s, %s, %s\n" %
                        (temp[0], temp[1], temp[2], temp[3]))

    print("Minimum distance was %s" % min_dist)

    return None
コード例 #7
0
def ode_solver(derv,
               xi,
               yi,
               x,
               args=(),
               solver_type='scipy.integrate.odeint',
               **kwargs):
    ''' ->> solver wrapper <<- 
        derv(p, x, y, *args, **kwargs):
    '''

    p = args[0]
    if solver_type == 'sympy.mpmath.odefun':
        return map(mt.odefun(lambda x, y: derv(p, x, y), xi, yi, **kwargs), x)

    elif solver_type == 'scipy.integrate.odeint':
        return integ.odeint(lambda y, x, p: derv(p, x, y),
                            yi,
                            x,
                            args=(p, ),
                            mxstep=_mx_step_)
コード例 #8
0
ファイル: symbolic.py プロジェクト: bjodah/pyodesys
    def _integrate_mpmath(self, xout, y0, params=()):
        """ Not working at the moment, need to fix
        (low priority - taylor series is a poor method)"""
        raise NotImplementedError
        xout, y0, self.internal_params = self.pre_process(xout, y0, params)
        from mpmath import odefun

        def rhs(x, y):
            rhs.ncall += 1
            return [
                e.subs(
                    ([(self.indep, x)] if self.indep is not None else []) +
                    list(zip(self.dep, y))
                ) for e in self.exprs
            ]
        rhs.ncall = 0

        cb = odefun(rhs, xout[0], y0)
        yout = []
        for x in xout:
            yout.append(cb(x))
        info = {'nrhs': rhs.ncall}
        return self.post_process(xout, yout, self.internal_params)[:2] + (info,)
コード例 #9
0
    def _integrate_mpmath(self, xout, y0, params=()):
        """ Not working at the moment, need to fix
        (low priority - taylor series is a poor method)"""
        raise NotImplementedError
        xout, y0, self.internal_params = self.pre_process(xout, y0, params)
        from mpmath import odefun

        def rhs(x, y):
            rhs.ncall += 1
            return [
                e.subs(
                    ([(self.indep, x)] if self.indep is not None else []) +
                    list(zip(self.dep, y))
                ) for e in self.exprs
            ]
        rhs.ncall = 0

        cb = odefun(rhs, xout[0], y0)
        yout = []
        for x in xout:
            yout.append(cb(x))
        info = {'nrhs': rhs.ncall}
        return self.post_process(
            xout, yout, self.internal_params)[:2] + (info,)
コード例 #10
0
def test_odefun_rational():
    mp.dps = 15
    # A rational function
    f = lambda t: 1 / (1 + mpf(t)**2)
    g = odefun(lambda x, y: [-2 * x * y[0]**2], 0, [f(0)])
    assert f(2).ae(g(2)[0])
コード例 #11
0
ファイル: test_ode.py プロジェクト: 1zinnur9/pymaclab
def test_odefun_sinc_large():
    mp.dps = 15
    # Sinc function; test for large x
    f = sinc
    g = odefun(lambda x, y: [(cos(x)-y[0])/x], 1, [f(1)], tol=0.01, degree=5)
    assert abs(f(100) - g(100)[0])/f(100) < 0.01
コード例 #12
0
ファイル: test_ode.py プロジェクト: 1zinnur9/pymaclab
def test_odefun_rational():
    mp.dps = 15
    # A rational function
    f = lambda t: 1/(1+mpf(t)**2)
    g = odefun(lambda x, y: [-2*x*y[0]**2], 0, [f(0)])
    assert f(2).ae(g(2)[0])
コード例 #13
0
def realSol(x):
    g = odefun(lambda x, y: 2 * y - 2 * x**2 + x - 3, 0.0, 1.2)
    return g(x)
コード例 #14
0
 def f1(Xi):  # производная по фи
     q0 = [0, Xi[0]]  # phase and speed
     rside = createRsideOnePendulumsEquationMPMATH(L, G)
     q = mp.odefun(rside, 0, q0, tol=h_iter, degree=deg, method="taylor")
     return q(Xi[1])[0] - 2 * np.pi