0

Note: I made this a while ago and have learned a lot more since then, enough to understand why what I was asking for was unrealistic. I also should have done more research into sympy before asking this question.

I want to create a program where you enter a linear equation or system of linear equations as a string and in return you get the value of the variable(s) you entered, like so:

equation = input('Expression: ')
# Code to solve your linear equation here
print(answer)

Just to be clear, I want to be able to solve things like x+5=10, or things like 2x+3y=29, and get an output that looks something like "5", or "4, 7" respectively.

I've tried searching multiple websites and some stack overflow questions, but all I've come up with are ways to solve systems of linear equations like 2x+3y=29 using numpy or ways to solve normal linear equations like x+5=10 but none that can do both.

Here's a list of 3 answers I found and why they didn't help:

  1. https://www.mybluelinux.com/how-solve-any-linear-equation-in-python/
    • Offers a solution to solve normal linear equations, but cannot solve systems of linear equations
  2. https://stackabuse.com/solving-systems-of-linear-equations-with-pythons-numpy/
    • Solves systems of linear equations, but can't solve linear equations like x+5=10
  3. Is there a python module to solve linear equations?
    • Same reason as 2, most answers suggested NumPy, and the other answers there didn't solve my problem either.

I also searched some other websites and stack overflow questions, but they didn't work either for the same reasons as the 3 examples I listed.

  • 1
    In your last link some answers mention sympy. Is that actually not enough for you? I have to agree with Prune's answer though. "x + 5 = 10", or "2x + 3y = 29" is not legitimate python syntax. You need to do work to convert such strings to whatever format your method will require (as is done in your first link). With sympy you'd save a lot of headaches, assuming you can get away with using `eval()`. – Reti43 Feb 24 '21 at 23:33
  • Why not use numpy?? – chess_lover_6 Feb 24 '21 at 23:38
  • @Reti43, I've looked some more into sympy, and I think that'll do! I thought it wouldn't work for some reason (that I don't remember anymore since I didn't put it into writing), but I was mistaken! Thanks for your comment. – SublimeCosmo Feb 25 '21 at 01:40
  • If you're looking for solutions which are only integers, an equation such as `2*x + 3*y = 29` is called a Diophantine equation, and there's a specialized approach for such equations. You might be interested to read up about that. I second the recommendation for sympy by the way. – Robert Dodier Feb 25 '21 at 18:31

1 Answers1

1

There are plenty of existing packages to solve linear equations. However, they won't do your normalization work. You have to first transform your existing equations into standard form: an augmented matrix.

If you want to accept arbitrary linear equations, then you have to write code to do the preprocessing, such as turning

3*x + 10 = y - 5

into the standard form

 v1  v2   c
(3, -1, -15)

Once you have every equation in canonical form, you pass the coefficients and constants to the existing package.

Prune
  • 76,765
  • 14
  • 60
  • 81