0
votes

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!

2
maybe I'm missing something, but isn't that like mx+0y=-b? - Jeremy Kahan
Hi, if you have a vector plus a vector times a scalar, then you get a vector as result. So if lambda is a scalar, then you equation should look like this: vector3[x] = vector1[x] + λ * vector2[x] - samusa
@Jeremy: I do not think so because in the vector example there is the equation: c = mx + b .. two fixed parameters - mathi1651
@samusa: I do not really understand where the difference to my stated equation system is? Point[x,y] is indeed a vector. I need to calculate lamda to see whether the point is on the line or not :) If that's what zur reply is explaining then I do not seem to get it and maybe need further explanation.. thanks:) - mathi1651

2 Answers

1
votes

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.

1
votes

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))