1
$\begingroup$

How do I find solution of an under-constrained linear system in $\mathbb{R}^d$ that is closest to given reference $w_0$ in Euclidean distance?

Below is a solution that uses QuadraticOptimization, however that requires writing out the formulas symbolically. For $d=1000$, there are 1 million symbolic variables, is there a more efficient approach?

norm2[mat_] := Total@Flatten[mat*mat];
modifiedLeastSquares[m_, b_, w0_] := Module[{xx, x, eqs, d},
   d = Last@Dimensions[b];
   x = Array[xx, {d, d}];
   eqs = Flatten@MapThread[#1 == #2 &, {m . x, b}, 2];
   x /. QuadraticOptimization[norm2[x - w0], eqs, Flatten@x]
   ];

SeedRandom[1];
bs = 4;
d = 8;
m = RandomReal[{-1, 1}, {bs, d}];
b = RandomReal[{-1, 1}, {bs, d}];
w0 = ConstantArray[0, {d, d}];
mySol = modifiedLeastSquares[m, b, w0];
refSol = LeastSquares[m, b];
Print["Difference from built-in LeastSquares: ", 
 mySol - refSol // Norm]
$\endgroup$
9
  • $\begingroup$ Interesting question. To clarify: (1) Is $w_{0}$ a point in $R^d$ or a point in $R^{d\times d}$? (The former case might be easier.) (2) LeastSquares[ ] tries to minimize $||mx-b||^{2}$, not $||x||^2$. Why do you use it as a reference? $\endgroup$
    – A. Kato
    Commented Sep 23 at 3:33
  • 1
    $\begingroup$ @A.Kato I believe you are considering overconstrained case. In underconstrained case, LeastSquares minimizes ||x||^2 subject to $mx-b=0$, the code above numerically checks that this formulation matches output of LeastSquares $\endgroup$ Commented Sep 23 at 5:00
  • $\begingroup$ Thank you for your answer. $\endgroup$
    – A. Kato
    Commented Sep 23 at 5:49
  • $\begingroup$ The explanation given in Details and Options of the documentation LeastSquares is misleading ..... $\endgroup$
    – A. Kato
    Commented Sep 23 at 6:01
  • 1
    $\begingroup$ Rather irrelevant to the main problem, but, FYI, Norm[mat, "Frobenius"] is built-in, but it's a tad slower (probably because it squares the absolute value of the entries). And norm2[mat_] := Total[mat*mat, 2] is a tad faster than your norm2[]. $\endgroup$
    – Michael E2
    Commented Sep 23 at 9:42

1 Answer 1

2
$\begingroup$

Your problem

  • Find $x$ such that $m.x=b$ and $||x-w_{0}||^2\to $ min

is equivalent to (by change of variables $y=x+w_{0}$)

  • Find $y$ such that $m.y=b-m.w_{0}$ and $||y||^2\to $ min

So, the simplest answer is:

LeastSquares[m, b - m . w0] + w0

This solves the case of $d=1000$ and $bs=500$ within 1.2 seconds on my PC.

$\endgroup$
2
  • $\begingroup$ aha, thanks, it was a simple re-arrangement after all $\endgroup$ Commented Sep 23 at 18:00
  • $\begingroup$ You are welcome :-) I also learned a lot. $\endgroup$
    – A. Kato
    Commented Sep 23 at 21:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.