SublimeCosmo
SublimeCosmo

Reputation: 3

Is there a way to solve any linear equation and system of linear equations all in one?

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.

Upvotes: 0

Views: 296

Answers (1)

Prune
Prune

Reputation: 77847

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.

Upvotes: 1

Related Questions