0

Function f (assume n=3 for simplicity): enter image description here

There are 3 symbols related to entities, corresponding to x[j](j=1,2,3) respectively. R and c is other symbols, which can be treated like constant for now. I try to diff f w.r.t x[j], and solve the results equations together and get x[j]=g(R,c). However, sympy cannot rearrange or split x[j] from the equation.

Derivatives: enter image description here

Expected Results: enter image description here

from sympy import *
import sympy as sym
real_n = 3
x = IndexedBase('x')
j, k, n = symbols('j,k n', cls=Idx)
f = x[j]*Symbol("R")/Sum(x[k],(k,1,real_n))-Symbol("c")*x[j]
equ = diff(f,x[j])

ee = solve([equ.subs(j,1),equ.subs(j,2),equ.subs(j,3)], (x[1],x[2],x[3]))
simplify(ee)

Sympy's result:

{x[1]: (R*Sum(x[k], (k, 1, 3)) - c*Sum(x[k], (k, 1, 3))**2)/(R*Sum(KroneckerDelta(1, k), (k, 1, 3))),
 x[2]: (R*Sum(x[k], (k, 1, 3)) - c*Sum(x[k], (k, 1, 3))**2)/(R*Sum(KroneckerDelta(2, k), (k, 1, 3))),
 x[3]: (R*Sum(x[k], (k, 1, 3)) - c*Sum(x[k], (k, 1, 3))**2)/(R*Sum(KroneckerDelta(3, k), (k, 1, 3)))}

enter image description here

I tried to check if the indexed symbol caused the error, and wrote x[i] as 3 different symbols, but it still didn't work.

from sympy import *

a, b, c = symbols('a b c', cls=Idx)
R = symbols("R")
eq1 = diff(a/(a+b+c)-a*R,a)
eq2 = diff(b/(a+b+c)-b*R,b)
eq3 = diff(c/(a+b+c)-c*R,c)
print(eq1,"\n",eq2,"\n",eq3)
solve([eq1,eq2,eq3], [a,b,c])

Output:

-R + 1/(a + b + c) - a/(a + b + c)**2 
 -R + 1/(a + b + c) - b/(a + b + c)**2 
 -R + 1/(a + b + c) - c/(a + b + c)**2
[]

Is there something wrong with my approach? Is it possible to approach this problem in SymPy from another angle?

Any suggestions for the solution of equations are also most welcome.

vio1etus
  • 21
  • 1
  • 6

1 Answers1

1

You can use doit to expand the summation and then solve:

In [6]: solve([equ.subs(j,1).doit(),equ.subs(j,2).doit(),equ.subs(j,3).doit()], (x[1],x[2],x[3]))
Out[6]: 
⎡⎛         ____          ⎞⎤
⎢⎜        ╱  2           ⎟⎥
⎢⎜R + 3⋅╲╱  R    2⋅R  2⋅R⎟⎥
⎢⎜─────────────, ───, ───⎟⎥
⎣⎝     18⋅c      9⋅c  9⋅c⎠⎦

Oscar Benjamin
  • 12,649
  • 1
  • 12
  • 14
  • Thank you for your generous help. It's very inspirational to see the right result. However, there still empty result for my machine , after I tried `doit` by myself. My runtime infos: sympy version:1.11.1, 'macOS-13.0.1-arm64', python: '3.8.15'. And I try the code in another machine with Windows 10 python3.8, it still cannot get the result. I wonder what platform or runtime are you using? Thanks. [tried_results_link](https://imgur.com/a/3Pnr53d) – vio1etus Dec 08 '22 at 03:54
  • I think it's because I'm using SymPy from git. A bug was fixed but the fix has not been released yet. – Oscar Benjamin Dec 08 '22 at 10:42
  • I tried the the latest sympy version in git, sympy==1.12.dev0 for now. However, it's still empty results. – vio1etus Dec 09 '22 at 01:44
  • In order to help others with the same problem, here is my final solution: I turned to Matlab, it gave me the right and expected result for the code that does exactly the same thing. But Oscar Benjamin's `doit` function really helped me – vio1etus Apr 20 '23 at 07:42
  • Actually there was no bug. The code works with older versions of sympy. I just tested 1.10 and 1.11 as well as sympy master. I just used the first 7 lines of your code and then copied the line that I showed and it gave the same output that I showed before. If you were getting an empty result then you did not run the same code as shown so I can't say what the problem was. – Oscar Benjamin Apr 20 '23 at 15:03