2

Is there a way to provide get dsolve() to use arbitrary constants named by a different sequence than C1, C2, etc?

cse() allows a symbols parameter that accepts an infinite generator of names, but I don't see a similar parameter to dsolve().

I'm solving circuit equations full of symbolic capacitances, and the dsolve constants are getting confusing.

Failing that, is there a quick way to replace the arbitrary constants with others such as k_1, k_2, etc? It looks like dsolve() is using C1, C2, etc, for its constants while my code uses C_1, C_2, etc for capacitances. I could change my naming everywhere to use a non-standard capacitance symbol, but I'd prefer not to.


With thanks to @Marshmallow, I've started using this routine which wraps dsolve lets me change the symbols after the fact. There's still the risk of collision, but it's good enough for now:

def newdsolve(eq,*args,**kwds):
    ss=kwds.get('symbols')
    sln=dsolve(eq,*args,**kwds)
    psyms=((newdsolve.csre.match(n.name),n) for n in eqVc.free_symbols)
    if ss and isinstance(ss,str):
        subsdict=dict([(n[1],'k_'+n[0].group(1)) for n in psyms if n[0]])
    elif ss:
        subsdict=dict([(n[1],next(ss)) for n in psyms if n[0]])
    else: subsdict={}
    return sln.subs(subsdict)
newdsolve.csre=re.compile(r'C(\d+)')
Omegaman
  • 2,189
  • 2
  • 18
  • 30

1 Answers1

2

I don't see such an option in dsolve either, but you can use subs to replace the symbols C1,C2 with your own. The function sub_const replaces them with, e.g., k_1, k_2, and so on, until the expression stops changing. Another character can be used in place of k: it's a parameter.

from sympy import *

def sub_const(expr, char):
    i = 1
    while True:
        old = symbols('C{}'.format(i))
        new = symbols(char + '_{}'.format(i))
        new_expr = expr.subs(old, new)
        if new_expr == expr:
            return expr
        expr = new_expr
        i += 1

x, f = symbols('x, f')
print(sub_const(dsolve(Derivative(f(x), x, x) + 9*f(x), f(x)), 'k'))

Output:

Eq(f(x), k_1*sin(3*x) + k_2*cos(3*x))
  • Thanks. Is there a way to do that substitution without knowing how many constants `dsolve` has inserted? I'm looking for a general solution in part because C1 and C_1 both show up the same in latex and it's not immediately obvious how many constants dsolve inserted. (I updated my question a bit to clarify that I'm already using C_1 for other purposes and want to replace C1 with something like k_1-- I see where you're going though.) – Omegaman Apr 15 '16 at 23:33
  • I included a function that subs regardless of the number of constants. –  Apr 15 '16 at 23:50