Question Details

No question body available.

Tags

python sympy

Answers (2)

Accepted Answer Available
Accepted Answer
May 9, 2026 Score: 4 Rep: 15,223 Quality: Expert Completeness: 60%

The problem comes down to two things.

Firstly, SymPy considers that symbols with different assumptions but the same name are still different symbols:

In [19]: xr = Symbol('x', real=True)

In [20]: xv = Symbol('x')

In [21]: xr Out[21]: x

In [22]: xv Out[22]: x

In [23]: xr == xv Out[23]: False

This means that you cannot interchange those symbols when making a substitution:

In [24]: e = xr2 + 1

In [25]: e Out[25]: 2 x + 1

In [26]: e.subs(xr, 2) Out[26]: 5

In [27]: e.subs(xv, 2) Out[27]: 2 x + 1

Secondly you are building your substitution dictionary like this:

values = dict(L1=5m, L2=2m, P=16kN, q=-4kN/m, E=200000000kN/m
2)

That is equivalent to

values = {'L1':5
m, 'L2':2m, 'P':16kN, 'q':-4kN/m, 'E':200_000_000kN/m2}

What this means is that you are passing strings rather than symbols to subs. What subs will do with those strings is assume that they represent a symbol with no assumptions set (like xv rather than xr above).

The fix is that you should use the symbol objects as the keys rather than strings:

values = {L1:5m, L2:2m, P:16kN, q:-4kN/m, E:200000000kN/m
2}

The fixed code then is:

import sympy as s L1, L2, x = s.symbols('L1, L2, x', real=True, positive=True) P, q = s.symbols('P, q') m, kN = s.symbols('m, kN', real=True, positive=True) E, J = s.symbols('E, J', real=True, positive=True) ; EJ = E
J values = {L1:5m, L2:2m, P:16kN, q:-4kN/m, E:200000000kN/m2} Ra = (P/2 - qL22/2/L1).subs(values, simultaneous=True) print(Ra)

The output is not precisely what you asked for but is equivalent:

48*kN/5
May 9, 2026 Score: 3 Rep: 181,342 Quality: Medium Completeness: 60%

Note the difference between:

values = dict(L1=5m, L2=2m, P=16kN, q=-4kN/m, E=200000000kN/m2)

Result: {'L1': 5m, 'L2': 2m, 'P': 16kN, 'q': -4kN/m, 'E': 200000000kN/m2}

and:

values = {L1:5m, L2:2m, P:16kN, q:-4kN/m, E:200000000kN/m2}

Result: {L1: 5m, L2: 2m, P: 16kN, q: -4kN/m, E: 200000000kN/m2}

One has string keys, and the other has symbol keys.

The result of this fix:

import sympy as s
L1, L2, x = s.symbols('L1, L2, x', real=True, positive=True)
P, q = s.symbols('P, q')
m, kN = s.symbols('m, kN', real=True, positive=True)
E, J = s.symbols('E, J', real=True, positive=True) ; EJ = EJ
values = {L1:5m, L2:2m, P:16kN, q:-4kN/m, E:200_000_000kN/m2}
Ra = (P/2 - qL22/2/L1).subs(values, simultaneous=True)
print(Ra)

Output (Equivalent to 8kN + 8kN/5):

48
kN/5