Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Here is a more generic code, making use of wildcards. It should work in many more cases than just linear combinations. It also makes it easier to add new rules.

def simplify_dirac_delta(E, t):
"""
Apply some rules on expression E as long as it changes. The rules depend on a parameter t.
"""
    w = SR.wild(0)

    rules = {w*dirac_delta(t): lambda f: f.subs({t: 0})*dirac_delta(t),
             w*diff(dirac_delta(t), t): lambda f: diff(f, t).subs({t: 0})*dirac_delta(t)}

    def apply_rules(E):
        for r in rules:
            for e in E.find(r):
                f = e.match(r)[w]
                E = E.substitute({e: rules[r](f)})
        return E

    E2 = 0
    while(E2!=E):
        E2 = E
        E = apply_rules(E)

    return E

it can be used like this:

sage: simplify_dirac_delta(k**2*G + diff(G, x, x), x)
3*dirac_delta(x)

Simplifications rules are given as a dictionary. The function will try to apply these rules as long as the result changes, so you have to make sure that your rules can't loop back (alternatively you can delete the loop, and only apply the rules a fixed number of times).

Unfortunately, diff is not a symbolic function and does not accept wildcards, so I don't know how to match a derivative, and it is necessary to pass x as an argument.

Here is a more generic code, making use of wildcards. It should work in many more cases than just linear combinations. It also makes it easier to add new rules.

def simplify_dirac_delta(E, t):
"""
Apply some rules on expression E as long as it changes. The rules depend on a parameter t.
"""
    w = SR.wild(0)

    rules = {w*dirac_delta(t): lambda f: f.subs({t: 0})*dirac_delta(t),
             w*diff(dirac_delta(t), t): lambda f: diff(f, t).subs({t: 0})*dirac_delta(t)}

    def apply_rules(E):
apply_rules():
        nonlocal E
        for r in rules:
            for e in E.find(r):
                f = e.match(r)[w]
                E = E.substitute({e: rules[r](f)})
        return E

    E2 = 0
    while(E2!=E):
        E2 = E
        E = apply_rules(E)
while(E!=apply_rules()):
        pass

    return E

it can be used like this:

sage: simplify_dirac_delta(k**2*G + diff(G, x, x), x)
3*dirac_delta(x)

Simplifications rules are given as a dictionary. The function will try to apply these rules as long as the result changes, so you have to make sure that your rules can't loop back (alternatively you can delete the loop, and only apply the rules a fixed number of times).

Unfortunately, diff is not a symbolic function and does not accept wildcards, so I don't know how to match a derivative, and it is necessary to pass x as an argument.

Here is a more generic code, making use of wildcards. It should work in many more cases than just linear combinations. It also makes it easier to add new rules.

def simplify_dirac_delta(E, t):
"""
Apply some rules on expression E as long as it changes. The rules depend on a parameter t.
"""
    w = SR.wild(0)

    rules = {w*dirac_delta(t): lambda f: f.subs({t: 0})*dirac_delta(t),
             w*diff(dirac_delta(t), t): lambda f: diff(f, t).subs({t: 0})*dirac_delta(t)}
t)*dirac_delta(t)}

    def apply_rules():
        nonlocal E
        for r in rules:
            for e in E.find(r):
                f = e.match(r)[w]
                E = E.substitute({e: rules[r](f)})
        return E

    while(E!=apply_rules()):
        pass

    return E

it can be used like this:

sage: simplify_dirac_delta(k**2*G + diff(G, x, x), x)
3*dirac_delta(x)

Simplifications rules are given as a dictionary. The function will try to apply these rules as long as the result changes, so you have to make sure that your rules can't loop back (alternatively you can delete the loop, and only apply the rules a fixed number of times).

Unfortunately, diff is not a symbolic function and does not accept wildcards, so I don't know how to match a derivative, and it is necessary to pass x as an argument.