mathi1651
mathi1651

Reputation: 215

Numpy: Solve linear equation system with one unknown + number

I would like to solve a linear equation system in numpy in order to check whether a point lines up with a vector or not.

Given are the following equations for a vector2:

point[x] = vector1[x] + λ * vector2[x]

point[y] = vector1[y] + λ * vector2[y]

Numpys linalg.solve() offers the option to solve two equations in the form:

ax + by = c

by defining the parameters a and b in a numpy.array().

But I can't seem to find a way to deal with equations with one fixed parameter like:

m*x + b = 0

Am I missing a point or do I have to deal with another solution?

Thanks in advance!

Upvotes: 0

Views: 2391

Answers (2)

prisa
prisa

Reputation: 21

You can solve your equation 2*x + 4 = 0 with sympy:

from sympy.abc import x
from sympy import Eq, solve

eq = Eq(2 * x + 4, 0)

print(solve(eq))

Upvotes: 1

samusa
samusa

Reputation: 483

Hi I will give it a try to help with this question.

The numpy.linagl.solve says:

Computes the “exact” solution, x, of the well-determined, i.e., full rank, linear matrix equation ax = b.

Note the assumptions made on the matrix!

Lambda the same

If your lambda for the point[x] and point[y] equation should be the same. Then just concatenate all the vectors.

x_new = np.concatenate([x,y])
vec1_new = np.concatenate([vec1_x,vec1_y])
...

Assuming that this will overdetermined your system and probably it will. Meaning you have too many equations and only one parameter to determine (well-determined assumption violated). My approach would be to go with least sqare.

The numpy.linagl.lstsq has a least square method too. Where the equation is y = mx + c is solved. For your case this is y = point[x], x = vector2[x] and c = vector1[x].

This is copied from the numpy.linagl.lstsq example:

x = np.array([0, 1, 2, 3])
y = np.array([-1, 0.2, 0.9, 2.1])
A = np.vstack([x, np.ones(len(x))]).T    # => horizontal stack
m, c = np.linalg.lstsq(A, y, rcond=None)[0]

Lambda different

If the lambdas are different. Stack the vector2[x] and vector2[y] horizontal and you have [lambda_1, lambda_2] to find. Probably also more equations then lambds and you will find a least square solution.

Note

Keep in mind that even if you construct your system from a staight line and a fixed lambda. You might need a least square approach due to rounding and numeric differences.

Upvotes: 1

Related Questions