0

Here’s a simple example of how i begin to attempt to solve a system of 2 equations in sympy. My goal is to first set equal to each other my equations:

import sympy as sp
x,y,z = sp.symbols("X, Y, X")
print(sp.Poly((x-y),sp.Derivative).coeffs())
print(sp.Poly(sp.Eq(x,y),sp.Derivative).coeffs())

However,I need to set equal to each other not just with two equations. Here’s what I tried that doesn’t work:

import sympy as sp
x,y,z = sp.symbols("X, Y, X")
print(sp.Poly(sp.Eq(x,y,z),sp.Derivative).coeffs())

TypeError: new() takes from 2 to 3 positional arguments but 4 were given

For the more algebraic way similar to x-y, i don't know how to implement with 3 or more equations.

How do I extend my example to three or more equations? Is this possible with sympy? If not, are there alternative libraries that do something similar? Any help would be appreciated.

EDIT: So when i had 2 polynomial(or factorial equations) equations that is very similar in structure other than their given beta constants, their first degree differential theoretically should equate to each other. This emulates the maclaurin series where my 2 factorial equations try to converge to a general unknown function.I would use sp.idiff() to implicitly differentiate both equations. sp.idiff() will implicit solve for dx0, where i still need to solve for dx1. I solve for dx1 as follows:

equation1 = sp.idiff(expr1,array-like,respective_beta)
equation2 = sp.idiff(expr2,array-like,respective_beta)
coeffs = sp.Poly((equation1-equation2),sp.Derivative).coeffs()
dx1 = coeffs[-1]/coeffs[0]

After getting dx1, i can back solve dx0 and finally substitute all the symbols which i get through my design of experiment to emulate something called a multivariate delta theorem.

The problem now is, i do not only have expr1, expr2, i also have expr3. In the future, i would also have expr4,expr5,expr6. I would need to extract the symbolic coefficients from that set of equations that equate to each other to back solve for dx5,dx4,dx3,dx2,dx1 simultaneously.

sp.Poly(sp.Eq(expr1,expr2,expr3,expr4,expr5,expr6),sp.Derivative).coeffs

The above way would obviously throw an error:

TypeError: new() takes from 2 to 3 positional arguments but "N" were given

So the question again is, would there be a mathematical solution or a method from sympy to "make equal" sets of expressions with out solving them. Does sympy support this?

pysolver33
  • 307
  • 1
  • 5
  • 13
  • 1
    `solve` takes a list of "equations". – hpaulj Jun 27 '21 at 18:04
  • My goal is to first set equal to each other my equations – pysolver33 Jun 28 '21 at 00:53
  • Does this answer your question? [How can I solve system of linear equations in SymPy?](https://stackoverflow.com/questions/31547657/how-can-i-solve-system-of-linear-equations-in-sympy) – ti7 Jun 29 '21 at 06:29
  • unfortunately, no. my first task is to equate a set of equations to each other so that i can concatenate a single big equation – pysolver33 Jun 30 '21 at 13:36

1 Answers1

0

Instead of declaring them equal, subtract one from the other and use that as a new equation in your solve() list!

>>> from sympy import *
>>> x, y, z = symbols("x y z")
>>> expr1 = x + y**2
>>> expr2 = z + y**3
>>> expr3 = expr1 - expr2
>>> expr3
x - y**3 + y**2 - z
>>> solve([expr3], x)
{x: y**3 - y**2 + z}
ti7
  • 16,375
  • 6
  • 40
  • 68
  • so i iteratively subtract each expr into their compilation. i mean, like first i do (x-y), then i do (x-y)-z and iteratively repeat this process? – pysolver33 Jun 29 '21 at 03:11
  • I think there's a misunderstanding here; `solve([expr1, expr2..])` expects every equation to be equal to zero, and so they are all equal to begin with! ..and if you have `Eq(x+y,3)`, you can also just write it `x+y-3`, which is implied to be `Eq(x+y-3,0)` ([NOTE gotchas on `=` and `==`](https://docs.sympy.org/latest/gotchas.html)) – ti7 Jun 29 '21 at 06:20
  • the regular [`solve()`](https://docs.sympy.org/latest/modules/solvers/solvers.html#algebraic-equations) will take a single equation or an iterable of 'em (ie. collected in a normal python list `[]`) .. the same page also offers other solvers, though you can almost-certainly pose your _system of equations_ to the regular method (rather than, for example, forming a matrix of the coefficients..) – ti7 Jun 29 '21 at 06:29
  • i think my main goal right now is not to solve the system but to set expr1,expr2,expr3 all equal to each other. i do not want to use solve() to solve anything – pysolver33 Jun 29 '21 at 12:12
  • ultimately, i want to have sp.Poly(("the large equation set equal to each other"),["list of all derivatives"]).coeffs() to extract coefficients of all the equations being set equal to each other. – pysolver33 Jun 29 '21 at 12:15
  • 1
    Tony, maybe you can say more about what your larger goal is here. What is the general problem you want to solve -- I mean, for what purpose are you setting polynomial1 = polynomial2 = polynomial3 = ... ? – Robert Dodier Jun 29 '21 at 21:52
  • ive updated my post with an "EDIT". not sure if that's how i make an edit – pysolver33 Jul 01 '21 at 14:22