예제 #1
0
 def compute_density(self, expr, **kwargs):
     z = Dummy('z', real=True, finite=True)
     rvs = random_symbols(expr)
     if any(pspace(rv).is_Continuous for rv in rvs):
         expr = self.compute_expectation(DiracDelta(expr - z), **kwargs)
     else:
         expr = self.compute_expectation(KroneckerDelta(expr, z), **kwargs)
     return Lambda(z, expr)
예제 #2
0
    def subtractBaseDiracDelta(self, point: Vector, particle: Particle):
        '''

        :param point:
        :param particle:
        :return:
        '''
        return DiracDelta(point.getDistanceFrom(particle.getState()))
예제 #3
0
 def doit(self, **kwargs):
     expr, condition = self.expr, self.condition
     if condition is not None:
         # Recompute on new conditional expr
         expr = given(expr, condition, **kwargs)
     if not random_symbols(expr):
         return Lambda(x, DiracDelta(x - expr))
     return pspace(expr).compute_density(expr, **kwargs)
예제 #4
0
def test_heaviside():
    assert Heaviside(0) == 0.5
    assert Heaviside(-5) == 0
    assert Heaviside(1) == 1
    assert Heaviside(nan) == nan

    assert Heaviside(x).diff(x) == DiracDelta(x)

    raises(ArgumentIndexError, 'Heaviside(x).fdiff(2)')
예제 #5
0
def test_inverse_laplace_transform_delta_cond():
    from sympy import DiracDelta, Eq, im, Heaviside
    ILT = inverse_laplace_transform
    t = symbols('t')
    r = Symbol('r', real=True)
    assert ILT(exp(r*s), s, t, noconds=False) == (DiracDelta(t + r), True)
    z = Symbol('z')
    assert ILT(exp(z*s), s, t, noconds=False) == \
        (DiracDelta(t + z), Eq(im(z), 0))
    # inversion does not exist: verify it doesn't evaluate to DiracDelta
    for z in (Symbol('z', extended_real=False),
              Symbol('z', imaginary=True, zero=False)):
        f = ILT(exp(z*s), s, t, noconds=False)
        f = f[0] if isinstance(f, tuple) else f
        assert f.func != DiracDelta
    # issue 15043
    assert ILT(1/s + exp(r*s)/s, s, t, noconds=False) == (
        Heaviside(t) + Heaviside(r + t), True)
    def test_parse_input_function(self):
        t = symbols('t')
        u = (1 + sin(t) + DiracDelta(2 - t) + 5 * DiracDelta(3 - t) +
             Piecewise((1, t <= 1), (2, True)))
        impulses, jump_times = parse_input_function(u, t)
        self.assertEqual(impulses, [{
            'time': 2,
            'intensity': 1
        }, {
            'time': 3,
            'intensity': 5
        }])
        self.assertEqual(jump_times, [1, 2, 3])

        u = 1
        impulses, jump_times = parse_input_function(u, t)
        self.assertEqual(impulses, [])
        self.assertEqual(jump_times, [])
예제 #7
0
def test_p():
    assert Px.hilbert_space == L2(Interval(S.NegativeInfinity, S.Infinity))
    assert apply_operators(Px*PxKet(px)) == px*PxKet(px)
    assert PxKet(px).dual_class == PxBra
    assert PxBra(x).dual_class == PxKet
    assert (Dagger(PxKet(py))*PxKet(px)).doit() == DiracDelta(px-py)
    assert (XBra(x)*PxKet(px)).doit() ==\
        exp(I*x*px/hbar)/sqrt(2*pi*hbar)
    assert represent(PxKet(px)) == px
예제 #8
0
def test_integrate_DiracDelta():
    # This is here to check that deltaintegrate is being called, but also
    # to test definite integrals. More tests are in test_deltafunctions.py
    assert integrate(DiracDelta(x) * f(x), (x, -oo, oo)) == f(0)
    assert integrate(DiracDelta(x)**2, (x, -oo, oo)) == DiracDelta(0)
    # issue 4522
    assert integrate(integrate((4 - 4*x + x*y - 4*y) * \
        DiracDelta(x)*DiracDelta(y - 1), (x, 0, 1)), (y, 0, 1)) == 0
    # issue 5729
    p = exp(-(x**2 + y**2)) / pi
    assert integrate(p*DiracDelta(x - 10*y), (x, -oo, oo), (y, -oo, oo)) == \
        integrate(p*DiracDelta(x - 10*y), (y, -oo, oo), (x, -oo, oo)) == \
        integrate(p*DiracDelta(10*x - y), (x, -oo, oo), (y, -oo, oo)) == \
        integrate(p*DiracDelta(10*x - y), (y, -oo, oo), (x, -oo, oo)) == \
        1/sqrt(101*pi)
예제 #9
0
def test_DiracDelta():
    assert DiracDelta(1) == 0
    assert DiracDelta(5.1) == 0
    assert DiracDelta(-pi) == 0
    assert DiracDelta(5, 7) == 0
    assert DiracDelta(0) == oo
    assert DiracDelta(0, 5) == oo
    assert DiracDelta(x).func == DiracDelta
예제 #10
0
def test_DiracDelta():
    assert DiracDelta(1) == 0
    assert DiracDelta(5.1) == 0
    assert DiracDelta(-pi) == 0
    assert DiracDelta(5,7) == 0
    assert DiracDelta(0) == oo
    assert DiracDelta(0,5) == oo
    assert isinstance(DiracDelta(x),DiracDelta)
def test_rewrite():
    x, y = Symbol('x', real=True), Symbol('y')
    assert Heaviside(x).rewrite(Piecewise) == (Piecewise(
        (0, x < 0), (Heaviside(0), Eq(x, 0)), (1, x > 0)))
    assert Heaviside(y).rewrite(Piecewise) == (Piecewise(
        (0, y < 0), (Heaviside(0), Eq(y, 0)), (1, y > 0)))
    assert Heaviside(x, y).rewrite(Piecewise) == (Piecewise(
        (0, x < 0), (y, Eq(x, 0)), (1, x > 0)))
    assert Heaviside(x, 0).rewrite(Piecewise) == (Piecewise((0, x <= 0),
                                                            (1, x > 0)))
    assert Heaviside(x, 1).rewrite(Piecewise) == (Piecewise((0, x < 0),
                                                            (1, x >= 0)))

    assert Heaviside(x).rewrite(sign) == (sign(x) + 1) / 2
    assert Heaviside(y).rewrite(sign) == Heaviside(y)
    assert Heaviside(x, S.Half).rewrite(sign) == (sign(x) + 1) / 2
    assert Heaviside(x, y).rewrite(sign) == Heaviside(x, y)

    assert DiracDelta(y).rewrite(Piecewise) == Piecewise(
        (DiracDelta(0), Eq(y, 0)), (0, True))
    assert DiracDelta(y, 1).rewrite(Piecewise) == DiracDelta(y, 1)
    assert DiracDelta(x - 5).rewrite(Piecewise) == (Piecewise(
        (DiracDelta(0), Eq(x - 5, 0)), (0, True)))

    assert (x * DiracDelta(x - 10)).rewrite(
        SingularityFunction) == x * SingularityFunction(x, 10, -1)
    assert 5 * x * y * DiracDelta(
        y, 1).rewrite(SingularityFunction) == 5 * x * y * SingularityFunction(
            y, 0, -2)
    assert DiracDelta(0).rewrite(SingularityFunction) == SingularityFunction(
        0, 0, -1)
    assert DiracDelta(0,
                      1).rewrite(SingularityFunction) == SingularityFunction(
                          0, 0, -2)

    assert Heaviside(x).rewrite(SingularityFunction) == SingularityFunction(
        x, 0, 0)
    assert 5 * x * y * Heaviside(y + 1).rewrite(
        SingularityFunction) == 5 * x * y * SingularityFunction(y, -1, 0)
    assert ((x - 3)**3 * Heaviside(x - 3)).rewrite(
        SingularityFunction) == (x - 3)**3 * SingularityFunction(x, 3, 0)
    assert Heaviside(0).rewrite(SingularityFunction) == SingularityFunction(
        0, 0, 0)
예제 #12
0
파일: cartesian.py 프로젝트: tuhina/sympy
    def _represent_PxKet(self, basis, **options):
        index = options.pop("index", 1)

        states = basis._enumerate_state(2, start_index=index)
        coord1 = states[0].momentum
        coord2 = states[1].momentum
        d = DifferentialOperator(coord1)
        delta = DiracDelta(coord1 - coord2)

        return I * hbar * (d * delta)
예제 #13
0
def test_p():
    assert Px.hilbert_space == L2(Interval(S.NegativeInfinity, S.Infinity))
    assert qapply(Px*PxKet(px)) == px*PxKet(px)
    assert PxKet(px).dual_class() == PxBra
    assert PxBra(x).dual_class() == PxKet
    assert (Dagger(PxKet(py))*PxKet(px)).doit() == DiracDelta(px-py)
    assert (XBra(x)*PxKet(px)).doit() ==\
        exp(I*x*px/hbar)/sqrt(2*pi*hbar)
    assert represent(PxKet(px)) == DiracDelta(px-px_1)

    rep_x = represent(PxOp(), basis = XOp)
    assert rep_x == -hbar*I*DiracDelta(x_1 - x_2)*DifferentialOperator(x_1)
    assert rep_x == represent(PxOp(), basis = XOp())
    assert rep_x == represent(PxOp(), basis = XKet)
    assert rep_x == represent(PxOp(), basis = XKet())

    assert represent(PxOp()*XKet(), basis=XKet) == \
           -hbar*I*DiracDelta(x - x_2)*DifferentialOperator(x)
    assert represent(XBra("y")*PxOp()*XKet(), basis=XKet) == \
           -hbar*I*DiracDelta(x-y)*DifferentialOperator(x)
예제 #14
0
 def delta_function(self, m):
     """
     The argument of Dirac is v_para - (w -m*Omega/k_parallel)
     @param m: The order
     @return: Dirac function of argument
     """
     vz = symbols('vz')
     k_para = symbols('k_para')
     w = symbols('w')
     argument = vz - (w - m * self.gyro_f.value) / k_para
     return DiracDelta(argument)
예제 #15
0
def test_x():
    assert X.hilbert_space == L2(Interval(S.NegativeInfinity, S.Infinity))
    assert Commutator(X, Px).doit() == I*hbar
    assert apply_operators(X*XKet(x)) == x*XKet(x)
    assert XKet(x).dual_class == XBra
    assert XBra(x).dual_class == XKet
    assert (Dagger(XKet(y))*XKet(x)).doit() == DiracDelta(x-y)
    assert (PxBra(px)*XKet(x)).doit() ==\
        exp(-I*x*px/hbar)/sqrt(2*pi*hbar)
    assert represent(XKet(x)) == x
    assert XBra(x).position == x
예제 #16
0
def test_as_independent():
    assert (2*x*sin(x)+y+x).as_independent(x) == (y, x + 2*x*sin(x))
    assert (2*x*sin(x)+y+x).as_independent(y) == (x + 2*x*sin(x), y)

    assert (2*x*sin(x)+y+x).as_independent(x, y) == (0, y + x + 2*x*sin(x))

    assert (x*sin(x)*cos(y)).as_independent(x) == (cos(y), x*sin(x))
    assert (x*sin(x)*cos(y)).as_independent(y) == (x*sin(x), cos(y))

    assert (x*sin(x)*cos(y)).as_independent(x, y) == (1, x*sin(x)*cos(y))

    assert (sin(x)).as_independent(x) == (1, sin(x))
    assert (sin(x)).as_independent(y) == (sin(x), 1)

    assert (2*sin(x)).as_independent(x) == (2, sin(x))
    assert (2*sin(x)).as_independent(y) == (2*sin(x), 1)

    # issue 1804 = 1766b
    n1, n2, n3 = symbols('n1 n2 n3', commutative=False)
    assert (n1 + n1*n2).as_independent(n2) == (n1, n1*n2)
    assert (n2*n1 + n1*n2).as_independent(n2) == (0, n1*n2 + n2*n1)
    assert (n1*n2*n1).as_independent(n2) == (n1, n2*n1)
    assert (n1*n2*n1).as_independent(n1) == (1, n1*n2*n1)

    assert (3*x).as_independent(x, as_Add=True) == (0, 3*x)
    assert (3*x).as_independent(x, as_Add=False) == (3, x)
    assert (3+x).as_independent(x, as_Add=True) == (3, x)
    assert (3+x).as_independent(x, as_Add=False) == (1, 3 + x)

    # issue 2380
    assert (3*x).as_independent(Symbol) == (3, x)

    # issue 2549
    assert (n1*x*y).as_independent(x) == (n1*y, x)
    assert ((x + n1)*(x - y)).as_independent(x) == (1, (x + n1)*(x - y))
    assert ((x + n1)*(x - y)).as_independent(y) == (x + n1, x - y)
    assert (DiracDelta(x - n1)*DiracDelta(x - y)).as_independent(x) == (1, DiracDelta(x - n1)*DiracDelta(x - y))
    assert (x*y*n1*n2*n3).as_independent(n2) == (x*y*n1, n2*n3)
    assert (x*y*n1*n2*n3).as_independent(n1) == (x*y, n1*n2*n3)
    assert (x*y*n1*n2*n3).as_independent(n3) == (x*y*n1*n2, n3)
    assert (DiracDelta(x - n1)*DiracDelta(y - n1)*DiracDelta(x - n2)).as_independent(y) == \
           (DiracDelta(x - n1), DiracDelta(y - n1)*DiracDelta(x - n2))

    # issue 2685
    assert (x + Integral(x, (x, 1, 2))).as_independent(x, strict=True) == \
           (Integral(x, (x, 1, 2)), x)
예제 #17
0
def algebraic_form(regex, what = None, threshold = 1e-3):
    _, (clusters, basis, partial_coefficients, bottom, (overflow, (top, bottom))) = \
        extract_coefficients_algebraically(regex, what, threshold)
    n = sympify('n')
    series = 0
    for i, (root, k) in enumerate(collate(clusters)):
        symbolic_root = inverse_symbolic(root)
        partial_coefficient = inverse_symbolic(partial_coefficients[i])
        series += partial_coefficient * binomial(n + k - 1, k - 1) * ((-1) ** k) * (symbolic_root ** (-n - k))
    for k, coefficient in overflow.items():
        series += coefficient * DiracDelta(n - k)
    return series
예제 #18
0
def test_integrate_DiracDelta():
    assert integrate(DiracDelta(x),x) == Heaviside(x)
    assert integrate(DiracDelta(x) * f(x),x) == f(0) * Heaviside(x)
    assert integrate(DiracDelta(x) * f(x),(x,-oo,oo)) == f(0)
    assert integrate(DiracDelta(x) * f(x),(x,0,oo)) == f(0)/2
    assert integrate(DiracDelta(x**2+x-2),x) - \
           (Heaviside(-1 + x)/3 + Heaviside(2 + x)/3) == 0
    assert integrate(cos(x)*(DiracDelta(x)+DiracDelta(x**2-1))*sin(x)*(x-pi),x) - \
           (-pi*(cos(1)*Heaviside(-1 + x)*sin(1)/2 - cos(1)*Heaviside(1 + x)*sin(1)/2) + \
           cos(1)*Heaviside(1 + x)*sin(1)/2 + cos(1)*Heaviside(-1 + x)*sin(1)/2) == 0
예제 #19
0
파일: test_utils.py 프로젝트: cni/nipy
def test_events():
    # test events utility function
    h = Function('hrf')
    evs = events([3,6,9])
    assert_equal(DiracDelta(-9 + t) + DiracDelta(-6 + t) +
                 DiracDelta(-3 + t), evs)
    evs = events([3,6,9], f=h)
    assert_equal(h(-3 + t) + h(-6 + t) + h(-9 + t), evs)
    # make some beta symbols
    b = [Dummy('b%d' % i) for i in range(3)]
    a = Symbol('a')
    p = b[0] + b[1]*a + b[2]*a**2
    evs = events([3,6,9], amplitudes=[2,1,-1], g=p)
    assert_equal((2*b[1] + 4*b[2] + b[0])*DiracDelta(-3 + t) +
                 (-b[1] + b[0] + b[2])*DiracDelta(-9 + t) +
                 (b[0] + b[1] + b[2])*DiracDelta(-6 + t),
                 evs)
    evs = events([3,6,9], amplitudes=[2,1,-1], g=p, f=h)
    assert_equal((2*b[1] + 4*b[2] + b[0])*h(-3 + t) +
                 (-b[1] + b[0] + b[2])*h(-9 + t) +
                 (b[0] + b[1] + b[2])*h(-6 + t),
                 evs)
    # test no error for numpy int arrays
    onsets = np.array([30, 70, 100], dtype=np.int64)
    evs = events(onsets, f=hrf.glover)
예제 #20
0
def test_heaviside():
    assert Heaviside(0) == 0.5
    assert Heaviside(-5) == 0
    assert Heaviside(1) == 1
    assert Heaviside(nan) == nan

    assert Heaviside(x).diff(x) == DiracDelta(x)
    assert Heaviside(x + I).is_Function
    assert Heaviside(I * x).is_Function

    raises(ArgumentIndexError, lambda: Heaviside(x).fdiff(2))
    raises(ValueError, lambda: Heaviside(I))
    raises(ValueError, lambda: Heaviside(2 + 3 * I))
예제 #21
0
def get_values():
    """
    Get the special values as a dict where the keys are the Symi expressions
    ans the values are the corresponding SymPy values.

    Returns
    -------
    values : tuple of dict
        Correspondences between Symi and Sympy.

        Values[0] gives the basic function replacements,
        values[1] gives the operator replacements,
        values[2] gives the constants replacements
        values[3] gives the advances function replacements
    """
    fcts = {
        "arccos": "acos",
        "arcsin": "asin",
        "arctan": "atan",
        "conj": "conjugate",
        "abs": "Abs",
        "int": "integrate",
        "des": "apart"
    }

    operators = {}

    constants = {"i": "I", "j": "J", "inf": "oo", "ipi": "I*pi", "e": "E"}

    advanced = {
        "Laplace":
        lambda __wild_sym__: laplace_transform(parse_expr(str(__wild_sym__)),
                                               parse_expr("t"),
                                               parse_expr("s"),
                                               noconds=True),
        "Linv":
        lambda __wild_sym__: inverse_laplace_transform(parse_expr(
            str(__wild_sym__)),
                                                       parse_expr("s"),
                                                       parse_expr("t"),
                                                       noconds=True),
        "step":
        lambda __wild_sym__: Heaviside(__wild_sym__),
        "dirac":
        lambda __wild_sym__: DiracDelta(__wild_sym__),
        "sym":
        lambda __wild_sym__: Symbol(str(__wild_sym__)),
    }
    advanced["L"] = advanced["Laplace"]

    return fcts, operators, constants, advanced
예제 #22
0
    def test_init(self):
        C_0, C_1 = symbols('C_0 C_1')
        state_vector = [C_0, C_1]
        t = Symbol('t')
        input_fluxes = {
            0:
            Piecewise((1, t <= 1), (0, True)) + 1 * DiracDelta(3 - t),
            1:
            Piecewise(
                (2, t <= 2),
                (0, True)) + 2 * DiracDelta(3 - t) + 4 * DiracDelta(4 - t)
        }
        output_fluxes = {0: 1 * C_0, 1: 1 * C_1}
        internal_fluxes = {}
        rm = ReservoirModel(state_vector, t, input_fluxes, output_fluxes,
                            internal_fluxes)

        times = np.linspace(0, 5, 11)
        start_values = [5, 3]
        mr = ModelRun(rm, start_values=start_values, times=times)

        ref_split_data = [{
            'intensity': [0, 0],
            'times': [0.0, 0.5, 1.0]
        }, {
            'intensity': [0, 0],
            'times': [1.0, 1.5, 2.0]
        }, {
            'intensity': [0, 0],
            'times': [2.0, 2.5, 3.0]
        }, {
            'intensity': [1, 2],
            'times': [3.0, 3.5, 4.0]
        }, {
            'intensity': [0, 4],
            'times': [4.0, 4.5, 5.0]
        }]
        self.assertTrue(mr.split_data, ref_split_data)
예제 #23
0
    def __init__(self,
                 state_vector,
                 time_symbol,
                 input_fluxes,
                 output_fluxes,
                 internal_fluxes,
                 units=None,
                 time_unit=None):

        self.state_vector = state_vector
        self.state_variables = [sv.name for sv in state_vector]
        self.time_symbol = time_symbol
        #self.output_fluxes = output_fluxes
        #self.internal_fluxes = internal_fluxes
        self.units = units
        self.time_unit = time_unit

        #self.input_fluxes = input_fluxes

        clean_input_fluxes = {}
        impulsive_input_fluxes = {}
        jump_times = []

        impulsive_fluxes = {}

        for (input_pool, flux) in input_fluxes.items():
            impulses, jumps = parse_input_function(flux, time_symbol)
            jump_times += jumps

            clean_flux = flux
            for impulse in impulses:
                impulse_time = impulse['time']
                intensity = impulse['intensity']
                clean_flux -= intensity * DiracDelta(-time_symbol +
                                                     impulse_time)

                if not impulse_time in impulsive_fluxes.keys():
                    impulsive_fluxes[impulse_time] = {}

                impulsive_fluxes[impulse_time][input_pool] = intensity

            clean_input_fluxes[input_pool] = clean_flux

        self.clean_model = SmoothReservoirModel(state_vector, time_symbol,
                                                clean_input_fluxes,
                                                output_fluxes, internal_fluxes,
                                                units, time_unit)
        self.jump_times = sorted(set(jump_times))
        self.impulsive_fluxes = impulsive_fluxes
예제 #24
0
    def test_plot_sols(self):
        C_0, C_1 = symbols('C_0 C_1')
        state_vector = [C_0, C_1]
        t = Symbol('t')
        input_fluxes = {
            0:
            Piecewise((1, t <= 1), (0, True)) + 1 * DiracDelta(3 - t),
            1:
            Piecewise(
                (2, t <= 2),
                (0, True)) + 2 * DiracDelta(3 - t) + 4 * DiracDelta(4 - t)
        }
        output_fluxes = {0: 1 * C_0, 1: 1 * C_1 / (C_0 + 1)}
        internal_fluxes = {(0, 1): 1 * C_1}
        rm = ReservoirModel(state_vector, t, input_fluxes, output_fluxes,
                            internal_fluxes)

        times = np.linspace(0, 10, 1001)
        start_values = [5, 0]
        mr = ModelRun(rm, start_values=start_values, times=times)

        fig = plt.figure()
        mr.plot_sols(fig)
        fig.savefig('testfig.pdf')
예제 #25
0
    def doit(self, evaluate=True, **kwargs):
        expr, condition = self.expr, self.condition
        if condition is not None:
            # Recompute on new conditional expr
            expr = given(expr, condition, **kwargs)
        if not random_symbols(expr):
            return Lambda(x, DiracDelta(x - expr))
        if (isinstance(expr, RandomSymbol)
                and hasattr(expr.pspace, 'distribution')
                and isinstance(pspace(expr), SinglePSpace)):
            return expr.pspace.distribution
        result = pspace(expr).compute_density(expr, **kwargs)

        if evaluate and hasattr(result, 'doit'):
            return result.doit()
        else:
            return result
예제 #26
0
def test_inverse_laplace_transform_delta():
    from sympy import DiracDelta
    ILT = inverse_laplace_transform
    t = symbols('t')
    assert ILT(2, s, t) == 2*DiracDelta(t)
    assert ILT(2*exp(3*s) - 5*exp(-7*s), s, t) == \
        2*DiracDelta(t + 3) - 5*DiracDelta(t - 7)
    a = cos(sin(7)/2)
    assert ILT(a*exp(-3*s), s, t) == a*DiracDelta(t - 3)
    assert ILT(exp(2*s), s, t) == DiracDelta(t + 2)
    r = Symbol('r', real=True)
    assert ILT(exp(r*s), s, t) == DiracDelta(t + r)
예제 #27
0
파일: boson.py 프로젝트: ajgpitch/sympsi
    def _eval_commutator_MultiBosonOp(self, other, **hints):
        if (self.name == other.name
                and self.normalization_type == other.normalization_type):
            if not self.is_annihilation and other.is_annihilation:
                if self.normalization_type == 'discrete':
                    return -KroneckerDelta(self.mode, other.mode)
                elif self.normalization_type == 'continuous':
                    return -DiracDelta(self.mode - other.mode)
            elif not self.is_annihilation and not other.is_annihilation:
                return Integer(0)
            elif self.is_annihilation and other.is_annihilation:
                return Integer(0)

        elif 'independent' in hints and hints['independent']:
            # [a, b] = 0
            return Integer(0)

        return None
예제 #28
0
def JacM(t, y, *args):
    σ, β, γ, α, Λ, μ, ξ, κ, κ_old, τ_ξ, τ_σ, N, N_old, time, Is, Ss, Rs, pre, i, Es, Ds = args
    xx, yy = sp.symbols('xx yy')
    S = y[0]
    E = y[1]
    I = y[2]
    R = y[3]
    D = y[4]
    Dv = mv(sp.integrate(DiracDelta(xx), (xx, 0.0, R - τ_ξ)), pre)
    return [[
        I * β * σ / N - I * β / N - μ, 0, S * β * σ / N - S * β / N, ξ * Dv, 0
    ], [-I * β * σ / N + I * β / N, -α - μ, -S * β * σ / N + S * β / N, 0, 0],
            [
                0, α + μ,
                -γ * (N_old * (1 - κ_old) + (1 - N_old) * (1 - κ)) - γ - μ, 0,
                0
            ], [0, 0, γ + μ, -μ - ξ * Dv, 0],
            [0, 0, γ * (N_old * (1 - κ_old) + (1 - N_old) * (1 - κ)), 0, 0]]
예제 #29
0
def replace_dirac_delta(e, _n=0):
    """
    Look for Integral of the form ∫ exp(I*k*x) dx
    and replace with 2*pi*DiracDelta(k)
    """
    if _n > 20:
        warnings.warn("Too high level or recursion, aborting")
        return e
    if isinstance(e, Add):
        return Add(*[replace_dirac_delta(arg, _n=_n + 1) for arg in e.args])
    if isinstance(e, Mul):
        return Mul(*[replace_dirac_delta(arg, _n=_n + 1) for arg in e.args])
    if isinstance(e, Sum):
        nargs = [replace_dirac_delta(e.function, _n=_n + 1)]
        for lim in e.limits:
            nargs.append(lim)
        return Sum(*nargs)
    if isinstance(e, Integral):
        func = simplify(e.function)
        lims = e.limits
        if isinstance(func, exp) and len(
                lims[0]) == 3:  # works only for definite integrals
            ex_s = simplify(func.exp)
            dvar, xa, xb = lims[0]
            if (isinstance(ex_s, Mul)
                    and all([x in ex_s.args for x in [I, dvar]])
                    and (xa, xb) == (-oo, oo)):
                nvar = ex_s / (I * dvar)
                new_func = 2 * pi * DiracDelta(nvar)
                if len(lims) == 1:
                    return new_func
                else:
                    nargs = [new_func]
                    for i in range(1, len(lims)):
                        nargs.append(lims[i])
                    return Integral(*nargs)
        else:
            nargs = [replace_dirac_delta(e.function, _n=_n + 1)]
            for lim in e.limits:
                nargs.append(lim)
            return Integral(*nargs)
    return e
예제 #30
0
def test_heaviside():
    assert Heaviside(0) == 0.5
    assert Heaviside(-5) == 0
    assert Heaviside(1) == 1
    assert Heaviside(nan) == nan

    assert adjoint(Heaviside(x)) == Heaviside(x)
    assert adjoint(Heaviside(x - y)) == Heaviside(x - y)
    assert conjugate(Heaviside(x)) == Heaviside(x)
    assert conjugate(Heaviside(x - y)) == Heaviside(x - y)
    assert transpose(Heaviside(x)) == Heaviside(x)
    assert transpose(Heaviside(x - y)) == Heaviside(x - y)

    assert Heaviside(x).diff(x) == DiracDelta(x)
    assert Heaviside(x + I).is_Function is True
    assert Heaviside(I * x).is_Function is True

    raises(ArgumentIndexError, lambda: Heaviside(x).fdiff(2))
    raises(ValueError, lambda: Heaviside(I))
    raises(ValueError, lambda: Heaviside(2 + 3 * I))