Linear Python equations - Gaussian elimination

purpose

Given a set of points, I try to find the coefficients of a linear equation that satisfy all the points indicated.

For example, if I wanted to find a linear equation (ax + by + c = z):

3x + 2y + 2 = z

I will need at least three, three-dimensional points:

(2, 2, 12)
(3, 4, 19)
(4, 5, 24)

Given enough points with coordinates (x, y, z), I have to find (a, b, c) using the Gauss exception.

However, I think that I am having problems solving the matrix in special cases. You can see my first hit in the python implementation here: https://gist.github.com/anonymous/8188272

Let's look at a few examples ...

Dataset 1

Use the following “manual” points (x, y, z):

(2, 2, 12)
(3, 4, 19)
(4, 5, 24)

Split LU into the following matrix:

[[  2.   2.   1.  12.]
 [  3.   4.   1.  19.]
 [  4.   5.   1.  24.]]

Back resolves matrix U:

[[  4.    5.    1.   24. ]
 [  0.   -0.5   0.5   0. ]
 [  0.    0.    0.5   1. ]]

Returned result (a, b, c):

[3.0, 2.0, 2.0]

! ...

2

"" (x, y, z):

(3, 4, 19)
(4, 5, 24)
(5, 6, 29)

LU :

[[  3.   4.   1.  19.]
 [  4.   5.   1.  24.]
 [  5.   6.   1.  29.]]

U:

[[  5.00000000e+00   6.00000000e+00   1.00000000e+00   2.90000000e+01]
 [  0.00000000e+00   4.00000000e-01   4.00000000e-01   1.60000000e+00]
 [  0.00000000e+00   0.00000000e+00   4.44089210e-16   0.00000000e+00]]

(a, b, c):

[1.0, 4.0, 0.0]

, , !

3

"" (x, y, z):

(5, 6, 29)
(6, 7, 34)
(7, 8, 39)

LU :

[[  5.   6.   1.  29.]
 [  6.   7.   1.  34.]
 [  7.   8.   1.  39.]]

U:

[[  7.00000000e+00   8.00000000e+00   1.00000000e+00   3.90000000e+01]
 [  0.00000000e+00   2.85714286e-01   2.85714286e-01   1.14285714e+00]
 [  0.00000000e+00   0.00000000e+00   0.00000000e+00   3.55271368e-15]]

...

2 3 - "". "b" "c" ( !). , , .

- , , - , ?

!

+4
4

, , -. 2 3 . , , .

, , determinant , , 3-.

>>> import numpy as np
>>> from scipy.linalg import det
>>> data1 = np.array([(2, 2, 12), (3, 4, 19), (4, 5, 24)])
>>> data2 = np.array([(3, 4, 19), (4, 5, 24), (5, 6, 29)])
>>> data3 = np.array([(5, 6, 29), (6, 7, 34), (7, 8, 39)])
>>> det(data1)
-1.9999999999999982
>>> det(data2)
5.551115123125788e-17
>>> det(data3)
8.881784197001213e-16

1 , , .

2 3 , , .

+5

numpy.linalg, scipy.linalg scipy.optimize.

, 1

(2, 2, 12)
(3, 4, 19)
(4, 5, 24)

3x + 2y + 2 = z numpy.linalg.solve

>>> a = np.array([[2, 2, 1],
                  [3, 4, 1],
                  [4, 5, 1]])
>>> b = np.array([12, 19, 24])
>>> np.linalg.solve(a, b)
array([ 3.,  2.,  2.])

, 2

(3, 4, 19)
(4, 5, 24)
(5, 6, 29)

...

>>> a = np.array([[3, 4, 1], [4, 5, 1], [5, 6, 1]])
>>> b = np.array([19, 24, 29])
>>> np.linalg.solve(a, b)
array([ 0.73333333,  4.26666667, -0.26666667])

, ? , a - 2, [2., 3., 1.] . . ...

, 3

(5, 6, 29)
(6, 7, 34)
(7, 8, 39)

...

>>> a = np.array([[5, 6, 1], [6, 7, 1], [7, 8, 1]])
>>> b = np.array([29, 34, 39])
>>> np.linalg.solve(a, b)
LinAlgError: Singular matrix

, , .

>>> np.linalg.det(a)
0.0
>>> 1. / np.linalg.det(a)
inf

, ax = b, , x = inv(a)b inv(a) . a , inv(a) .

>>> np.linalg.inv(a)
LinAlgError: Singular matrix

, , .

>>> np.linalg.lstsq(a,b)
(array([ 2.,  3.,  1.]),       # solution "x"
 array([], dtype=float64),     # residuals, empty if rank > a.shape[0] or < a.shape[1]
 2,                            # rank
 array([  1.61842911e+01,   2.62145599e-01,   2.17200830e-16]))    # singular values of "a"

, [2., 3., 1.], , ! , , @wim , a , , EG: a .

+4

, , , 3 . , 2 3, , , (.. , ). LU- "", 2- .

, , 3 , , 3- . , . 2, , , .

. 4 , , .

+2

3, ,

(6, 7, 34) = ((5, 6, 29) + (7, 8, 39))/2

, . . , (A'*A+lambda*I), A', . A' - A, lambda - , I - . LU, .

, . - . , , .

0
source

All Articles