1

Here's an example of my problem. I define all the variables, except x which is automatic in the notebook session. Then I try to substitute an expression of previously defined variable omega_P.

In [2]: omega, omega_P, omega_C = symbols('omega omega_P omega_C')
        R = 1 - omega_P**2/(omega*(omega+omega_C))
        pprint(R)

           2   
        ω_P    
1 - ───────────
    ω⋅(ω + ω_C)

In [3]: x = omega_P**2/omega**2
        pprint(x)

   2
ω_P 
────
  2 
 ω  

In [4]: pprint(R.subs(omega_P**2, x*omega**2))

           2   
        ω_P    
1 - ───────────
    ω⋅(ω + ω_C)

omega_P was not changed by subs().

Tom Kuiper
  • 11
  • 2
  • I've since learned about `pprint`. – Tom Kuiper Apr 13 '20 at 14:06
  • Could you create a fully stand-alone example? The current code doesn't produce the output shown. Try creating an example in a freshly restarted environment. Also note that apart from `subs` sympy also has `replace` and [`xreplace`](https://docs.sympy.org/latest/modules/core.html#sympy.core.basic.Basic.xreplace) which might be helpful. Unfortunately, each of these functions sometimes don't produce the results that is hoped for. – JohanC Apr 13 '20 at 14:55
  • Your suggestion was good! I repeated the calculations in another notebook, stripped down to what I needed to get the result. The only difference that I can see is that this time `R` and `L` are just symbols instead of the results of expressions with other symbols. This time `n2.subs(theta, 0)` and `n2.subs(theta, pi/2)` gave the expected results. It seems to be that `R` and `L` have to be defined, not computed. – Tom Kuiper Apr 13 '20 at 16:21
  • Is there a way for me to attach a Jupyter notebook to this question? – Tom Kuiper Apr 13 '20 at 22:36
  • That's unfortunate. So where I am now is that substitutions for $\theta$ work, but if I then create expressions for R,L, and P, and also define $S = (R+L)/2$ and $D = (R-L)/2$, and then try to substitute for $R$, $L$, $P$ then noting happens. There isn't enough room in a comment to show that because the expressions are so big. But with simple examples I have no problem. – Tom Kuiper Apr 14 '20 at 00:22
  • 2
    Substituting expressions instead of single variables often doesn't work. [This](https://stackoverflow.com/questions/45382498/general-expression-substitution-in-sympy) and [this post](https://stackoverflow.com/questions/34633262/sympy-substitute-mathematical-expression) bring some tips from sympy's maintainers. – JohanC Apr 17 '20 at 11:11
  • In this particular case, the problem is that `x` also contains `omega_P**2`. So, probably sympy found a weird circular dependency and just returned the input instead of giving an error or a warning. Without the circular dependency, `subs` seems to work well here. (Note that `x` shouldn't be defined as symbol, `x` is defined through the assignment.) – JohanC Apr 17 '20 at 11:24
  • Yes. The first post you mentioned on Apr 17 at 11:11 works fine. It's a clever approach. Maybe it should be made into a `sympy` function. Thanks. – Tom Kuiper Apr 20 '20 at 17:50
  • 1
    The current version of the question applies a substitution that is the identity mapping on expressions, because `x*omega**2` is equal to `omega_P**2` when `x == omega_P**2/omega**2`. So `R.subs(omega_P**2, x*omega**2)` is equivalent to `R.subs(omega_P**2, omega_P**2)`, which is the same expression as `R`. – 0 _ Apr 17 '21 at 16:09

0 Answers0