# How to solve these equations?

**URL:** <https://discourse.julialang.org/t/how-to-solve-these-equations/92788>\
**Category:** General Usage\
**Tags:** sympy\
**Created:** [January 11, 2023, 2:22am UTC](https://discourse.julialang.org/t/how-to-solve-these-equations/92788 "2023-01-11T02:22:02Z")\
**Posts on this page:** 14\
**Page:** 2

<div class="post-metadata">

**Author:** ![Raymond](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raymond/32/36557_2.png) [@Raymond](https://discourse.julialang.org/u/Raymond)\
**Post date:** [January 11, 2023, 4:40pm UTC](https://discourse.julialang.org/t/how-to-solve-these-equations/92788/21 "2023-01-11T16:40:30Z")

</div>

> [@rocco\_sprmnt21](#):
>
> `solve([eq1,eq2,eq3],[x,y,z], domain=SymPy.sympy.S.Reals)`

Can you try to run this code?

```julia
using SymPy
@syms x y z A B C D E F a b c d e f g

eq1 = x * (x - 1) * (D + E * y * z - E * y - E * z - A - B * y * z + B * y +
                    B * z - f * y - g * z - c * y)
eq2 = y * (y - 1) * (F + f * x - f + c * x)
eq3 = -z * (z - 1) * (-b + d + e)

solve([eq1, eq2, eq3], [x, y, z], domain=SymPy.sympy.S.Reals)

```

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [January 11, 2023, 4:43pm UTC](https://discourse.julialang.org/t/how-to-solve-these-equations/92788/22 "2023-01-11T16:43:31Z")

</div>

I try but I don’t know if the battery lasts long enough 😄

---

<div class="post-metadata">

**Author:** ![Raymond](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raymond/32/36557_2.png) [@Raymond](https://discourse.julialang.org/u/Raymond)\
**Post date:** [January 11, 2023, 4:45pm UTC](https://discourse.julialang.org/t/how-to-solve-these-equations/92788/23 "2023-01-11T16:45:09Z")

</div>

> [@rocco\_sprmnt21](#):
>
> `@time solve([eq1,eq2,eq3],[x,y,z], domain=SymPy.sympy.S.Reals)`

Why does it takes so long time? I have run for one day, but it still not finishes.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [January 11, 2023, 4:49pm UTC](https://discourse.julialang.org/t/how-to-solve-these-equations/92788/24 "2023-01-11T16:49:13Z")

</div>

I have no idea.  
This is my first time using these packages.  
I believe that the use of all these parameters commits (unnecessarily) the algorithm that “rearranges” the equations, for a long time.  
If you do it by hand (even the ncomplete system can be solced by hand) you save most of the effort.  
I wonder if with some suitable suggestions (via some kwarg) sympy is not able to do by itself the simplifications that I did.

---

<div class="post-metadata">

**Author:** ![Raymond](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raymond/32/36557_2.png) [@Raymond](https://discourse.julialang.org/u/Raymond)\
**Post date:** [January 11, 2023, 4:53pm UTC](https://discourse.julialang.org/t/how-to-solve-these-equations/92788/25 "2023-01-11T16:53:07Z")

</div>

> [@Raymond](#):
>
> ```julia
> eq2 = y * (y - 1) * (F + f * x - f + c * x)
> eq3 = -z * (z - 1) * (-b + d + e)
> 
> solve([eq1, eq2, eq3], [x, y, z], domain=SymPy.sympy.S.Reals)
> 
> ```

If I drop the last two terms in equation `fx`, that is, `- g₂*z - s₁*y`, then it runs fast.

 ![截屏2023-01-12 00.50.31](https://global.discourse-cdn.com/julialang/original/3X/6/c/6c10a4b6ed6b801bc32552197176876f84bc23e3.png)

---

<div class="post-metadata">

**Author:** ![Raymond](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raymond/32/36557_2.png) [@Raymond](https://discourse.julialang.org/u/Raymond)\
**Post date:** [January 11, 2023, 5:00pm UTC](https://discourse.julialang.org/t/how-to-solve-these-equations/92788/26 "2023-01-11T17:00:44Z")

</div>

Can you help me correct the codes? The solve can not finish in my computer, I can not get the results. 😂 🤣

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [January 11, 2023, 5:04pm UTC](https://discourse.julialang.org/t/how-to-solve-these-equations/92788/27 "2023-01-11T17:04:05Z")

</div>

you could define A, B, C etc as a function of the original parameters.  
Solve the systemq of equations and then do the inverse substitutions.

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [January 11, 2023, 5:16pm UTC](https://discourse.julialang.org/t/how-to-solve-these-equations/92788/28 "2023-01-11T17:16:30Z")

</div>

Since `eq1`, `eq2` are somewhat decoupled from `eq3`:

```julia
julia> solve([eq1,eq2], [x,y], domain=SymPy.sympy.S.Reals)
5-element Vector{Tuple{Sym, Sym}}:
 (0, 0)
 (0, 1)
 (1, 0)
 (1, 1)
 ((-F + f)/(c + f), (-A + B*z + D - E*z - g*z)/(B*z - B - E*z + E + c + f))

```

terminates within reasonable time. Adding the `eq3` consequences isn’t hard.

---

<div class="post-metadata">

**Author:** ![Raymond](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raymond/32/36557_2.png) [@Raymond](https://discourse.julialang.org/u/Raymond)\
**Post date:** [January 11, 2023, 6:22pm UTC](https://discourse.julialang.org/t/how-to-solve-these-equations/92788/29 "2023-01-11T18:22:38Z")

</div>

How to do the inverse substitutions?

```julia
using SymPy
@syms x y z e₁ e₂ c₁ c₂ c₃ c₅ s₁ s₂ s₃ g₁ g₂ A B C D E F G
eq1 = x*(x - 1)*(D - y*( A + z * B) - z * C)
eq2 = y*(y - 1)*( E + F * x)
eq3 = -z*(z - 1)*G
solve([eq1, eq2, eq3], [x, y, z])

10-element Vector{Tuple{Sym, Sym, Sym}}:
 (0, 0, 0)
 (0, 0, 1)
 (0, 1, 0)
 (0, 1, 1)
 (1, 0, 0)
 (1, 0, 1)
 (1, 1, 0)
 (1, 1, 1)
 (-E/F, D/A, 0)
 (-E/F, (-C + D)/(A + B), 1)

```

```julia
A => c₂ + g₁ + s₁ - e₂
B => e₂ - c₂
C => c₂ + g₂ - e₂
D => c₁ - e₁
E => c₃ - g₁
F => g₁ + s₁
G => -c₅ + s₂ + s₃

```

---

<div class="post-metadata">

**Author:** ![Raymond](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raymond/32/36557_2.png) [@Raymond](https://discourse.julialang.org/u/Raymond)\
**Post date:** [January 11, 2023, 6:55pm UTC](https://discourse.julialang.org/t/how-to-solve-these-equations/92788/30 "2023-01-11T18:55:55Z")

</div>

> [@Raymond](#):
>
> `A => c₂ + g₁ + s₁ - e₂`

I try to use `subs`, but it gets errors.

```julia
using SymPy
@syms x y z e₁ e₂ c₁ c₂ c₃ c₅ s₁ s₂ s₃ g₁ g₂ A B C D E F G
eq1 = x * (x - 1) * (D - y * (A + z * B) - z * C)
eq2 = y * (y - 1) * (E + F * x)
eq3 = -z * (z - 1) * G
answ = solve([eq1, eq2, eq3], [x, y, z])
subs(answ, A => c₂ + g₁ + s₁ - e₂, B => e₂ - c₂, C => c₂ + g₂ - e₂, D => c₁ - e₁, E => c₃ - g₁, F => g₁ + s₁)

ERROR: MethodError: no method matching subs(::Vector{Tuple{Sym, Sym, Sym}}, ::Pair{Sym, Sym}, ::Pair{Sym, Sym}, ::Pair{Sym, Sym}, ::Pair{Sym, Sym}, ::Pair{Sym, Sym}, ::Pair{Sym, Sym})
Closest candidates are:
  subs(::T, ::Pair...; kwargs...) where T<:SymPy.SymbolicObject at ~/.julia/packages/SymPy/7cg6m/src/utils.jl:126
  subs(::Tuple{T, N}, ::Any...; kwargs...) where {T<:SymPy.SymbolicObject, N} at ~/.julia/packages/SymPy/7cg6m/src/utils.jl:127
  subs(::Number, ::Any...; kwargs...) at ~/.julia/packages/SymPy/7cg6m/src/utils.jl:128
  ...
Stacktrace:
 [1] top-level scope
   @ ~/Desktop/sfby.jl:44

```

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [January 11, 2023, 7:04pm UTC](https://discourse.julialang.org/t/how-to-solve-these-equations/92788/31 "2023-01-11T19:04:00Z")

</div>

after some trial and error, this seems to work.  
But having absolutely no experience with these packages, I believe there are “easier” solutions

```julia
julia> sol=solve([eq1, eq2, eq3], [x, y, z])
10-element Vector{Tuple{Sym, Sym, Sym}}:
 (0, 0, 0)
 (0, 0, 1)
 (0, 1, 0)
 (0, 1, 1)
 (1, 0, 0)
 (1, 0, 1)
 (1, 1, 0)
 (1, 1, 1)
 (-E/F, D/A, 0)
 (-E/F, (-C + D)/(A + B), 1)

julia> repl=(A => c₂ + g₁ + s₁ - e₂,
       B => e₂ - c₂,
       C => c₂ + g₂ - e₂,
       D => c₁ - e₁,
       E => c₃ - g₁,
       F => g₁ + s₁,
       G => -c₅ + s₂ + s₃)
(A => c₂ - e₂ + g₁ + s₁, B => -c₂ + e₂, C => c₂ - e₂ + g₂, D => c₁ - e₁, E => c₃ - g₁, F => g₁ + s₁, G => -c₅ + s₂ + s₃)

julia> map(s->foldl((sc,c)->subs.(sc,c),repl, init=s), sol)
10-element Vector{Tuple{Sym, Sym, Sym}}:
 (0, 0, 0)
 (0, 0, 1)
 (0, 1, 0)
 (0, 1, 1)
 (1, 0, 0)
 (1, 0, 1)
 (1, 1, 0)
 (1, 1, 1)
 (-(c₃ - g₁)/(g₁ + s₁), (c₁ - e₁)/(c₂ - e₂ + g₁ + s₁), 0)
 (-(c₃ - g₁)/(g₁ + s₁), (c₁ - c₂ - e₁ + e₂ - g₂)/(g₁ + s₁), 1)

```

---

<div class="post-metadata">

**Author:** ![j\_verzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_verzani/32/8551_2.png) [@j\_verzani](https://discourse.julialang.org/u/j_verzani)\
**Post date:** [January 11, 2023, 8:29pm UTC](https://discourse.julialang.org/t/how-to-solve-these-equations/92788/32 "2023-01-11T20:29:03Z")

</div>

That works. The issue is the solution is a vector of tuples. Substitution must be applied to element of each tuple, as it is currently defined. Maybe this pattern seems more direct:

```julia
[subs.(s, repl...) for s ∈ sol]

```

As for speed, I found that by making the variables real (`@syms x::real ...` or `x, y,... = symbols("x, y...", real=true)`) the solving went fairly quickly.

---

<div class="post-metadata">

**Author:** ![Raymond](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raymond/32/36557_2.png) [@Raymond](https://discourse.julialang.org/u/Raymond)\
**Post date:** [January 12, 2023, 3:29am UTC](https://discourse.julialang.org/t/how-to-solve-these-equations/92788/33 "2023-01-12T03:29:48Z")

</div>

> [@Raymond](#):
>
> ```julia
> fx = x*(x - 1)*(c₁ + c₂*y*z - c₂*y - c₂*z - e₁ - e₂*y*z + e₂*y + 
> e₂*z - g₁*y - g₂*z - s₁*y)
> fy = y*(y - 1)*(c₃ + g₁*x - g₁ + s₁*x)
> fz = -z*(z - 1)*(-c₅ + s₂ + s₃)
> solve([fx, fy, fz], [x, y, z])
> 
> ```

Although I can do some substitutions and then finish the solve, but I think this is not a good method. I hope it can directly solve the problem.

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [January 12, 2023, 7:37am UTC](https://discourse.julialang.org/t/how-to-solve-these-equations/92788/34 "2023-01-12T07:37:53Z")

</div>

You ask in the wrong place. SymPy is not a Julia library, but a Python library. You got very good answers how to work around the issues with it, but if you suggest to improve it please create a ticket at [Issues · sympy/sympy · GitHub](https://github.com/sympy/sympy/issues) .

[Previous page](https://discourse.julialang.org/t/how-to-solve-these-equations/92788.md?page=1)
