# Constraining some rows from LeastSquares to lines

For two matrices, A and B, with dimensions {n,p} and {n,2} respectively, LeastSquares[A,B] returns an matrix, R, with dimensions {p,2}.

But any row of R could be anywhere in the real plane. I want to constrain R[[1]] and R[[-1]] to two different lines. How should this be done?

The system is overdetermined, n>p

-
Seems like "random" cases will have them linearly independent. Trying to force this could be tricky, unless you have a criterion in mind for how far apart they must be. Determining what that might mean, for your purposes, would be the first step. –  Daniel Lichtblau Jan 31 at 16:23

Here is an example of one way to proceed.

n = 3; p = 4;
amat = Array[a, {n, p}];
bvec1 = Array[b1, n];
bvec2 = Array[b2, n];
rvec1 = Array[r1, p];
rvec2 = Array[r2, p];
sol = Solve[amat.Transpose[{rvec1, rvec2}] == Transpose[{bvec1, bvec2}],
Flatten[{rvec1, rvec2}]]


The solution you get from this comes with a warning that "Equations may not give solutions for all "solve" variables." This is precisely what you want to exploit. For the given n and p, you have free variables r1[1] and r2[1], which can be set to any desired value, thus giving the constraint that you specify. As you change n and p, you may have a larger or smaller number of free variables.

-