1

Is there a way to solve a linear system of equations in python where the unknown is on both sides of the equality. So for instance if I have:

x_1 = 0.7 + 0.5 * x_1 - 0.4 * x_2
x_2 = 0.7 * x_1 + 0.5 - 0.4 * x_1
x_3 = 0.7 * x_2 + 0.5 * x_1 - 0.4

where this can be expressed as:

|x_1|   | 1   x_1  x_2|| 0.7|
|x_2| = |x_1   1   x_1|| 0.5|
|x_3|   |x_2  x_1   1 ||-0.4|

where the we have a Toeplitz Matrix.

I could easily solve such an expression by hand, but it gets laborious as I have large sets. I was looking at

Is there a python module to solve linear equations?

How to solve a pair of nonlinear equations using Python?

and the SymPy Solvers modules, but I can't seem to find a way of going about this.

Community
  • 1
  • 1
Lukasz
  • 2,476
  • 10
  • 41
  • 51
  • If you're trying to solve matrix multiplication or system of linear equasions you can do it like here http://stackoverflow.com/questions/6789927/is-there-a-python-module-to-solve-linear-equations or use scipy http://docs.scipy.org/doc/scipy/reference/tutorial/linalg.html – dmitryro Jul 04 '16 at 03:02
  • I'm a little bit confused about what comes after "where this can be expressed as", multiplying the matrix and vector gives for example `x_1 = 0.5 + 0.7 * x_1 - 0.4 * x_2` for the first row which does not correspond to the original equations. – J.J. Hakala Jul 04 '16 at 03:02
  • @dmitryro, I've had a look at that question and those packages, it doesn't seem to apply since in those cases the matrix is composed entirely of values. – Lukasz Jul 04 '16 at 03:18
  • You can give a try Theano to create your initial notation and then solve it using one of the packages. http://deeplearning.net/software/theano/library/tensor/basic.html – dmitryro Jul 04 '16 at 03:23
  • 1
    For SymPy you should look at `linsolve`. – asmeurer Jul 05 '16 at 21:46

2 Answers2

2

You need to get your equations to form (x_3 omitted for obvious reasons)

0.5 * x1 + 0.4 * x2 = 0.7
0.7 * x1 - 1.4 * x2 = -0.5

before you can use numpy.linalg.solve, like in this case

import numpy as np

a = np.array([[0.5, 0.4],    # first row  x_i factors
              [0.7, -1.4]])  # second row x_i factors
b = np.array([0.7, -0.5])

sol = np.linalg.solve(a, b)

print sol

which gives

[ 0.79591837  0.75510204]

If the question is about how to transform the input

x_1 = 0.7 + 0.5 * x_1 - 0.4 * x_2
x_2 = 0.7 * x_1 + 0.5 - 0.4 * x_2

automatically to the form where constants are on the right side of the equations, it can be archieved by noting first that

0 + 1 * x_1 + 0 * x_2 = 0.7 + 0.5 * x_1 - 0.4 * x_2
0 + 0 * x_1 + 1 * x_2 = 0.5 + 0.7 * x_1 - 0.4 * x_2

and then placing those values in matrixes

import numpy as np

left = np.matrix([[0, 1, 0],    # [cons., x_1, x_2]
                  [0, 0, 1]])
right = np.matrix([[0.7, 0.5, -0.4],
                   [0.5, 0.7, -0.4]])

tmp = left - right
constants = - tmp[:, 0]
factors = tmp[:, [1, 2]]

sol = np.linalg.solve(factors, constants)

print factors
print constants
print sol
J.J. Hakala
  • 6,136
  • 6
  • 27
  • 61
0

Looks like that can be easily cast as a linear equation problem:

-0.7 = (0.5 - 1) * x_1 - 0.4 * x_2
-0.5 = 0.7 * x_1 - (0.4 + 1) * x_2
0.4 = 0.7 * x_2 + 0.5 * x_1 - x_3

A = np.array([[0.5-1, -0.4, 0], [0.7, -.4-1, 0], [.5, .7, -1]])
F = np.array([-.7, -.5, .4])

F = A * X
X = F/A

which can be solved, in most cases, with

np.linalg.solve(A, F)

If my guess is right, what is the problem?

hpaulj
  • 221,503
  • 14
  • 230
  • 353
  • That solves this particular example. I'm looking for something that can be done an a more general scale, and not just a 3 system equation. Also the number of equation varies from problem to problem. – Lukasz Jul 04 '16 at 04:30