Is There a Python Module to Solve Linear Equations

Is there a python module to solve linear equations?

See http://sympy.org/ and http://numpy.scipy.org/.

Specifically, http://docs.scipy.org/doc/numpy/reference/routines.linalg.html

And http://docs.sympy.org/0.7.0/tutorial.html#algebra, http://docs.sympy.org/dev/modules/solvers/solvers.html

Edit: Added solvers link from the comment.

Is there a way to solve two linear equations in python without the use of any module?

you will need linear algebra module such as numpy.

#e.g. 
import numpy as np

X = np.array([[1,-1], [1,1]])
y = np.array([1,5])
np.linalg.inv(X).dot(y)

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

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.

Solving Linear Equation Using NumPy

Your b is a 1×3 array, so the dimensions of a and b do not match. Try

  1. b = np.array([[10], [11], [12]]) so that b is a 3×1 array, or

  2. b = np.array([10, 11, 12]) so that b is a vector with length 3 (which, as well as just b = [10, 11, 12], is also admissible by .solve(); see the doc).

The former will result in a 3×1 array as the solution, whereas the latter will result in a vector of length 3. Probably it is better to use the latter; usually we don't really care whether a vector is a column vector or a row vector. NumPy usually handles vectors in reasonable ways.

Solving linear equations w. three variables using numpy

You can use numpy.linalg.solve:

import numpy as np
a = np.array([[2, -4, 4], [34, 3, -1], [1, 1, 1]])
b = np.array([8, 30, 108])
x = np.linalg.solve(a, b)
print x # [ -2.17647059 53.54411765 56.63235294]

Solve a system of linear equations and linear inequalities

Yours is a problem in linear programming, where your equality and inequalities are the limitations and you want to minimize (then later maximize) the expression y. The equality, inequalities, and expression are all linear, so that makes it linear programming. The scipy package, using the scipy.optimize.linprog function, can do this kind of linear programming.

Here is commented code to do what you want. Note that all the inequalities were slightly changed to include equality, which is necessary to have a maximum or minimum value of y. To find the maximum value of y the code instead finds the minimum value of -y then prints the additive inverse of that, since linprog minimizes the objective function. Finally, the inequality restrictions must be "less than or equal to" in linprog, so I multiplied both sides of your inequality x + y > 180 by -1 to get one, namely -x + -y <= -180. Ask if you have any questions.

from scipy.optimize import linprog

# Set up values relating to both minimum and maximum values of y
coefficients_inequalities = [[-1, -1]] # require -1*x + -1*y <= -180
constants_inequalities = [-180]
coefficients_equalities = [[3, 12]] # require 3*x + 12*y = 1000
constants_equalities = [1000]
bounds_x = (30, 160) # require 30 <= x <= 160
bounds_y = (10, 60) # require 10 <= y <= 60

# Find and print the minimal value of y
coefficients_min_y = [0, 1] # minimize 0*x + 1*y
res = linprog(coefficients_min_y,
A_ub=coefficients_inequalities,
b_ub=constants_inequalities,
A_eq=coefficients_equalities,
b_eq=constants_equalities,
bounds=(bounds_x, bounds_y))
print('Minimum value of y =', res.fun)

# Find and print the maximal value of y = minimal value of -y
coefficients_max_y = [0, -1] # minimize 0*x + -1*y
res = linprog(coefficients_max_y,
A_ub=coefficients_inequalities,
b_ub=constants_inequalities,
A_eq=coefficients_equalities,
b_eq=constants_equalities,
bounds=(bounds_x, bounds_y))
print('Maximum value of y =', -res.fun) # opposite of value of -y

The printout from that code is

Minimum value of y = 43.3333333333
Maximum value of y = 51.1111111111

which is correct within the precision of floating point. If you need the corresponding values of x, see the value of res.x which is an array that gives the values of both x and y at the desired point--x is res.x[0] and y is res.x[1].



Related Topics



Leave a reply



Submit