2

I am currently working on solving a system of equations.

A subset of the equations are:

eq1  = pi1  * q[0+1] == pi0 * r[0+1]
eq2  = pi2  * q[0+1] == pi0 * r[1+1]  + pi1 * r[1+1]
eq3  = pi3  * q[0+1] == pi0 * r[2+1]  + pi1 * r[2+1]  + pi2 * r[1+1]
eq4  = pi4  * q[0+1] == pi0 * r[3+1]  + pi1 * r[3+1]  + pi2 * r[2+1]  + pi3 * r[1+1]
eq5  = pi5  * q[0+1] == pi0 * r[4+1]  + pi1 * r[4+1]  + pi2 * r[3+1]  + pi3 * r[2+1]  + pi4 * r[1+1]
eq6  = pi6  * q[0+1] == pi0 * r[5+1]  + pi1 * r[5+1]  + pi2 * r[4+1]  + pi3 * r[3+1]  + pi4 * r[2+1]  + pi5 * r[1+1]
eq7  = pi7  * q[0+1] == pi0 * r[6+1]  + pi1 * r[6+1]  + pi2 * r[5+1]  + pi3 * r[4+1]  + pi4 * r[3+1]  + pi5 * r[2+1]  + pi6 * r[1+1]

Unfortunately, this is not working the way I want it to be working. I want it to be read as follows: the first equation has the name 'eq1' and has a certain equality equation. The other lines should be read similarly. In my code I have 14 more equations which are even longer. I want to give them a name to avoid really long expressions in "solve([], [])" .

Is this possible? And if so, how should it be done?

Student NL
  • 409
  • 6
  • 16

1 Answers1

0

You can store the equations in a dictionary - you will be mapping index of the equation (1, 2, etc) to a tuple, which will contain two items representing two sides of the equation.

equations = dict()
equations[1]  = (pi1  * q[0+1], pi0 * r[0+1])

You can then call solve(equations[1]) and in the solve() fuction do something like this:

def solve(equation):
    left_side_of_equation = equation[0]
    right_side_of_equation = equation[1]
    # Here do everything you need with this equation values.

Or call function like this: solve(equations[1][0], equations[1][1]) and have two parameters in the solve() function.

Edit to answer comment:

If you want to have more than equations with just == (greater, greater equal etc.) you need to save this information in the tuple as well. It can look like this:

equations[42] = (pi1, 0, "ge")

and implement solve() function like this:

def solve(equation):
    left_side_of_equation = equation[0]
    right_side_of_equation = equation[1]
    operator = equation[2]
    if operator == "ge": # Greater or equal
        # Do your comparison
    elif operator == "g": # Greater
        # Do your comparison
    # Add all remaining possibilities.
grael
  • 657
  • 2
  • 11
  • 26
  • Thanks a lot! Are the brackets () around the equation necessary? – Student NL Sep 28 '16 at 07:55
  • And how can I solve this with inequalities such as ineq0 = pi0 >= 0 and ineq1 = pi1 >= 0 ? – Student NL Sep 28 '16 at 08:11
  • No, they are not necessary - it just looks more readable to me. – grael Sep 28 '16 at 08:12
  • @StudentNL Added inequalities to my answer. – grael Sep 28 '16 at 08:17
  • Thanks for the addition. The solve function that I am using is an in-build function of sympy. All my inequalities simply state that the variables should have a value larger than or equal to 0. Is there a way to predefine the domain of the variables in advance? – Student NL Sep 28 '16 at 08:23
  • You can check the third element of target tuple and if it's inequality, just call `solve(equations[target_index][0])` and if it's equality call `solve(equations[target_index][0], equations[target_index][1])`. – grael Sep 28 '16 at 08:34
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/124389/discussion-between-grael-and-student-nl). – grael Sep 28 '16 at 09:43
  • You should use `sympy.Eq()` instead of a tuple to represent the equations. – asmeurer Sep 28 '16 at 16:26