I was trying to understand an existing and working algorithm, where at one point they calculate the intercepts of a hyperplane which is defined by m points in a m dimensional space.
b = [1.0] * m
x = np.linalg.solve(A, b)
intercepts = [1.0 / i for i in x]
Where A is a list of the m points, which each are represented as a list with m values. So essentially, A is a matrix.
I get that linalg.solve calculates the x vector so that Ax=b is valid by solving a linear equation system.
But what I don't get:
1) Why is b hardcoded to be filled with 1's?
2) What exactly is x? How can I imagine those values? It must resemble the hyperplane, but I don't know how that relates to the values of b.
3) Why do you get the intercepts if you divide 1 by the x_i?
I believe instead of "1" you could use any other value other than 0 for both b and the division, but not 100% sure. Please try to keep the answer simple, I'm not a mathematician (as i'm sure you already figured from the simple question).
Edit: One thing that maybe matters: the points are guaranteed to all have values >=0 and the hyperplane through them is guaranteed to intersect all axes.
It might be easiest to understand this by working backwards toward it.
Given the Cartesian equation $ax+by+cz=d$ of a plane in $\mathbb R^3$, one finds its intercepts by setting two of the variables to $0$ and solving for the remaining one. E.g., to find the $x$-intercept, set $y=z=0$ to get $ax=d$, and if $a\ne0$, then $x=d/a$. If the plane doesn’t pass through the origin, then $d\ne0$ and we can without loss of generality assume that $d=1$. The condition that the plane intersects all of the coordinate axes means that none of the coefficients is zero, so the three intercepts are then simply $1/a$, $1/b$ and $1/c$. This obviously generalizes to hyperplanes in $n$-dimensional space: if the hyperplane equation is $$a_1x_1+\cdots+a_nx_n=1,\tag{*}$$ then its intercepts are $1/a_i$. As you suspect, we could’ve chosen any nonzero $b$ instead for the right-hand side of the equation, in which case the intercepts would be $b/a_i$ as we saw with the general plane equation.
Now we just need to find an equation in this form for your hyperplane, but that’s easy: swap the roles of the coefficients $a_i$ and the unknowns $x_i$. By substituting the coordinates of the $n$ points into this equation, you generate a system of $n$ linear equations in the unknowns $a_i$. If the defining points are in what’s usually called “general position,” then this system has a unique solution—the hyperplane is uniquely determined by the given points.
Note that the method in this code snippet fails if the hyperplane passes through the origin. In that case, its equation is homogeneous, i.e., the constant term on the right-hand side is $0$ and the intercepts aren’t the reciprocals of the coefficients. Instead, they’re all zero. Unfortunately, knowing that all of the coordinates of the defining points are positive isn’t enough to preclude this possibility. For example, in $\mathbb R^2$, the points $(1,1)$ and $(2,2)$ meet this criterion, but the line through them passes through the origin.