How to rewrite an expression in terms of an other expression in sympy

23 views
Skip to first unread message

Antonello L.

unread,
Jan 16, 2017, 3:07:43 PM1/16/17
to sympy

Hello,
I am very new with SymPy. I have an expression that, once expressed in terms to an other expression, should become very nice.
The problem is that I don't know how to "force" to express the original expression in terms of the other one.

This is a basic example:

    import sympy as sp
    sp.init_printing(use_unicode=True)
    a,b,c = sp.symbols('a b c')
    A = a+b+c
    B = a+c
    C = A.subs(a+c,B) # Expected/wanted: C = B+b
    C

a+b+c


    A.rewrite(B)





A and B could be rather complex expressions. For reference, this is my real-case scenario:

    import sympy as sp
    sp.init_printing(use_unicode=True)
    t, w, r = sp.symbols('t w r')
    S = sp.Function('S')(t)
    V = (S-w*(1+r)**t)/(((1+r)**t)-1)
    V


    St = -(r + 1)**t*(w - S)*sp.log(r + 1)/((r + 1)**t - 1)
    St




Once I can write St in terms of V, I should be able to simplify it to get just

St = rS(t)+rV

But I am unable to do it in SymPy. Any hint?

Kind regards,
  Antonello Lobianco


Reply all
Reply to author
Forward
0 new messages