-3

I want to solve a equation set like this in python

0 + x01 + 0 + x03 - 0 - 0 - 0 - 0 = 5
0 + 0 + 0 + 0 - x01 - 0 - 0 - 0 = -4
0 + 0 + 0 + x23 - 0 - 0 - 0 - 0 = 5
0 + 0 + 0 + 0 - x03 - 0 - x23 - 0 = -6

If there is a feasible solution, print all x If there is no solution, print 'no solution'

Thanks!

Ry-
  • 218,210
  • 55
  • 464
  • 476
wesley
  • 85
  • 1
  • 1
  • 6
  • What are you having problems with? – Blender Sep 14 '13 at 17:11
  • 1
    possible duplicate: http://stackoverflow.com/questions/6789927/is-there-a-python-module-to-solve-linear-equations unless you want to program an algorithm yourself? Then I'd start with something like the Gauss algorithm and show us what doesn't work – Nicolas78 Sep 14 '13 at 17:12
  • 1
    Take a look at `numpy.linalg`? – F.X. Sep 14 '13 at 17:12
  • Look up linear algebra and Gaussian reduction. It uses matrices, which are easy to code with. You have to parse your lines to create the matrices you need, but that should be the only major issue. – Spen-ZAR Sep 14 '13 at 17:13
  • numpy.linalg can just solve two equations. – wesley Sep 14 '13 at 17:30
  • Please answer your own question with whatever method you came up with. It's a nice way to help out other people who may have the same question. – Spen-ZAR Sep 14 '13 at 17:32

1 Answers1

1

You can use the scipy and numpy modules, scipy has the solve() method:

>>> import scipy
>>> import numpy as np
>>> a = np.array([[3,2,0],[1,-1,0],[0,5,1]])
>>> b = np.array([2,4,-1])
>>> x = linalg.solve(a,b)
>>> x
array([ 2., -2.,  9.])
pkacprzak
  • 5,537
  • 1
  • 17
  • 37