Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

EDIT: I am not asking how to solve an equation in terms of a given variable (as in this supposed duplicated question), but how to represent an expression in terms of an other one, as specified in the question. I believe it is the "duplicated" question to have a misleading title.

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

C equation

A.rewrite(B)

error message

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

V equation

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

St equation

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

St = rS(t)+rV

But I am unable to do it in SymPy.

like image 910
Antonello Avatar asked Jan 16 '17 14:01

Antonello


People also ask

How do you substitute values in SymPy?

The subs() function in SymPy replaces all occurrences of first parameter with second. This function is useful if we want to evaluate a certain expression. For example, we want to calculate values of following expression by substituting a with 5.

How do you evaluate an expression in SymPy?

To evaluate a numerical expression into a floating point number, use evalf . SymPy can evaluate floating point expressions to arbitrary precision. By default, 15 digits of precision are used, but you can pass any number as the argument to evalf .


1 Answers

First note that when you do something like

a,b,c =  sp.symbols('a b c')
A = a+b+c
B = a+c

variables A, B are not new Sympy symbols that Sympy can understand and operate on, rather, they are aliases for the Sympy expressions a+b+c and a+c, respectively. Therefore, A.subs(a+c,B) is essentially the same as A.subs(a+c,a+c), which is, of course, meaningless. You get the idea of why A.rewrite(B) is also of no use.

I do not think that calls like expr.subs({complicated_mutlivariable_formula: new_variable}) work in Sympy. One way to do what you want is to first solve the equation complicated_mutlivariable_formula = new_variable with respect to one of the "old" variables, and, assuming a unique solution exist, use subs() to substitute this variable.

Applying this approach for the second example:

# sympy Symbol A will be used to represent expression V
A = sp.symbols('A') 

# Solve the equation V==A with respect to w, which has a unique solution as a function of A
w_A = sp.solve(sp.Eq(V,A), w)[0] 

# Now substitute w 
St.subs({w:w_A}).simplify()

enter image description here

like image 112
Stelios Avatar answered Oct 06 '22 07:10

Stelios