1
$\begingroup$

Given a matrix $L\in \mathbb{R}^{3 \times 3}$, I'm looking for a method to find the closest (in a least squares sense) product of a non-uniform scaling matrix and a rotation matrix:

$$ \min_{s\in\mathbb{R}^3,\ R\in SO(3) } \left\| L - \text{diag}(s)\,R \right\|_F^2$$

So far, the best method that I've come up with is to alternate between optimizing $R$ and $s$ in a block coordinate descent fashion. Given initial guess of $s$ (e.g., $s=\mathbb{1}$), repeating:

  1. Update $R$: holding $s$ constant, the optimal $R$ is found as the solution to the resulting orthogonal procrustes problem.
  2. Update $s$: holding $R$ constant, the optimal $s$ is found as via a linear system solve (and actually just extracting the diagonal: $s = \text{diag}(LR^T)$).

until convergence.

Trying this out on gazillions of random $L$s, I find it works pretty well in most cases, though occasionally convergence is very slow. I've also noticed that the initial guess doesn't appear to affect the resulting minimizers. Could it be proveable that there's only a single (global) minimum (under some mild assumptions of $L$)?

Is there a closed form solution?

If not, is there a superior or best-practice method for optimizing this constrained minimization problem?

A student found "Orthogonal, but not Orthonormal, Procrustes Problems", Everson 1997 which includes the algorithm above ("tandem" in their language) and some other descent approaches (I can't see if they show one's better than the other).

$\endgroup$
7
  • 1
    $\begingroup$ Interesting to see that you are still interested in Procrustes-like problems 5 years later. $\endgroup$ Commented Nov 15, 2022 at 9:24
  • 1
    $\begingroup$ Just to make sure that we are on the same page: you allow negative entries in $s$, don't you? $\endgroup$
    – fedja
    Commented Nov 16, 2022 at 15:39
  • $\begingroup$ Yes. Negative entries in $s$ are OK. (I believe that means non-uniqueness, e.g, could multiply any $s$,$R$ by $diag([1,-1,-1])$, so I guess(?) to make it unique we could say that $s$ is allowed to have at most one negative entry). $\endgroup$ Commented Nov 16, 2022 at 16:30
  • $\begingroup$ err, that wouldn't make it unique since you could still pick which negative entry in $s$ to keep. I guess it's just that we can always strip $s$ down to a single negative entry. $\endgroup$ Commented Nov 16, 2022 at 16:52
  • 2
    $\begingroup$ Then I have some good news for you: 1) Your algorithm is guaranteed to work unless you are doing one stupid thing that you shouldn't be doing, i.e., sticking to rotations ($\det=1$) rather than rotations+reflections (full orthogonal group) in solving Procrustes. 2) There might be a quicker algorithm though it is not theoretically guaranteed (it is an old MO problem whether it always works). Practically it merely means that you can run them in parallel and just choose the better iteration at each step to feed into both to have the best of two speeds at the expense of doubling the best time). $\endgroup$
    – fedja
    Commented Nov 16, 2022 at 21:08

3 Answers 3

3
$\begingroup$

OK, sorry for the delay: yesterday was hectic and then I got distracted by another question. Here is a small piece of theory promised.

First, let us notice that $\|L-SR\|_F=\|LR^*-S\|_F$ so, as you correctly noticed yourself, $S$ must be the diagonal of $LR^*$ or you can improve and also our task can be restated as maximizing the sum of squares of the diagonal entries of $LR^*$ (because the full sum of squares does not depend on $R$). Also, since we can throw the minus sign on any entries of $S$ and the corresponding row of $R$ any time without changing anything, we are free to use the full orthogonal group for $R$ instead of $SO_n(\mathbb R)$ initially requested. In this case the Proctustes step just maximizes $\rm{Tr}[L(SR)^*]=\rm{Tr}[(SL)R*]$ and results in the symmetric positive definite $T=SLR^*$ (by using one SVD). When your algorithm reaches the limit, it should have $\rm{diag T}\approx S$ (with required precision) or you can improve $S$ and the functional further by a noticeable amount.

What we will show now that if $\rm{diag T}=S$, indeed, then the sum of squares of $LR^*$ cannot be raised any further. To this end, notice that the condition that $T=SLR^*$ is symmetric positive definite and $\rm{diag(T)}=S$ can be restated as $LR^*=AS$ where $A$ is a symmetric positive definite matrix with all $1$s on the diagonal. Morally, $A=S^{-1}TS^{-1}$ though $S$ may have zeroes, in which case the corresponding row of $T$ is identically $0$ and so, by symmetry, is the corresponding column, so that block of zeroes can be treated separately as $I\cdot 0$ and in the remaining block the division is legitimate. Our aim is to show that for every orthigonal $U$, the sum $\sum_i (ASU)_{ii}^2\le \sum_i S_{ii}^2$.

Write $A=\sum_k x^{(k)}\otimes x^{(k)}$ with $x^{(k)}=[x^{(k)}_1,\dots,x^{(k)}_n]$. All $1$'s on the diagonal of $A$ mean that $$ \sum_k (x^{(k)}_i)^2=1\tag{$*$} $$ for all $i$. If $u_i$ are the columns of $u$, then $$ (ASU)_{ii}^2=\left[\sum_k x^{(k)}_i (x^{(k)}Su_i)\right]^2 \\ \le \left[\sum_k (x^{(k)}_i)^2\right]\left[\sum_k (x^{(k)}Su_i)^2\right]=\sum_k (x^{(k)}Su_i)^2 $$ by Cauchy-Schwarz. Summing over $i$ and using that $u_i$ form an orthonormal basis, wi get $$ \sum_i (ASU)_{ii}^2\le \sum_k\|x^{(k)}S\|^2=\sum_i S_{ii}^2 $$ as required (here we used $(*)$ once again).

Thus, our task can be reduced to finding a decomposition $$ L=ASR $$ where $A$ is positive definite with ones on the diagonal, $S$ is diagonal (say, with non-negative entries, because we can throw all minuses on $R$), and $R$ is orthogonal.

It is not immediately clear to me how to approach it using SVD or some other standard machinery, but one may notice that if such a decomposition holds (and we just showed that your algorithm yields one in the limit, though we haven't discussed the convergence speed yet; I still have to think here), then $$ AS^2A=LL^*\Longleftrightarrow SAS=\sqrt{SLL^*S} $$ in the sense of positive definite matrices. Since we don't care too much about the off-diagonal entries of $A$ but the correct $S$ leads to the full solution in one Procrustes step, we can just as well rewrite it as $$ S=\sqrt{\rm{diag}\sqrt{SLL^*S}} $$ which is exactly the problem that was posted on MO a few years ago with the question whether we can just use Picard iteration method with the initial guess $S_0=\sqrt{\rm{diag}LL^*}$ to solve it fairly efficiently (the $\sqrt{}$ on positive definite matrices is also one SVD, and you use it for Procrustes anyway, so a single iteration takes the same time here but the convergence speed may be much faster). The answer is still unknown but it is, probably, time to revisit that old question (I don't remember the link now but I'll add it in the comments when I find it). Still, as I said, you may run your theoretically guaranteed method in parallel with this unproved one and have the best of the two worlds (like it is normally done with sure but slow bisection and fast but unreliable Newton for finding a root on an interval).

I hope to be able to say more later but this should give you some food for both thinking and experimenting for a while now. Apologies for the delay again :-)

$\endgroup$
4
  • $\begingroup$ Here is the promised link: mathoverflow.net/questions/413633/… As you can see, there seems to be no progress yet as far as the convergence is concerned :-( $\endgroup$
    – fedja
    Commented Nov 17, 2022 at 22:43
  • $\begingroup$ I'm a little confused how I would go about running these in parallel. It seems if I have to do SVD each iteration then the block-descent approach will always be strictly better. I'm probably misunderstanding. Here's what I'm trying gist.github.com/alecjacobson/70556ef7ed90a86f148404d167a2016a (gist so I can use newlines 😅). I think I'm confused about where I would be making the choice to use this $S=\sqrt{}$… versus $S = diag(diag(LR^T))$. $\endgroup$ Commented Nov 18, 2022 at 21:21
  • $\begingroup$ @AlecJacobson You iterate the square root until you arrive at the correct $S$ and then do one Procrustes to get $R$. The point is not that single iterations will get faster; they won't. However the square root method may need much fewer iterations than your current approach. $\endgroup$
    – fedja
    Commented Nov 21, 2022 at 15:07
  • $\begingroup$ Thanks. Don't I need to do svd each time I iterate on the sqrt? That seems to be the bulk of the Procrustes work so why not just update r? $\endgroup$ Commented Nov 21, 2022 at 17:53
1
$\begingroup$

Here I'll present a few remarks and one reasonably efficient (IMHO) algorithm. Unlike my previous answer the conclusions here are experimental more than theoretical.

  1. You are absolutely right that the convergence speed of your iterations is just as fast as that of the square root iterations. However, both are frustratingly slow on the matrix $$ \begin{bmatrix} 1&2&3\\ 1&2.001&3\\ 1&2&3.001 \end{bmatrix} $$ or any other nearly degenerate matrix. This forced me to think of some way to speed it up and for a while I was playing with various convergence acceleration techniques, but could find nothing that was both fast and stable, so I'll skip the description of that story.

  2. As I said, the problem is equivalent to maximizing the sum of the squares of the diagonal elements of $LR^*$ where $R$ is orthogonal. $S$ can that be read as the diagonal of that maximizing matrix (up to entry signs). The key idea that finally occurred to me was that one can solve the 2D problem exactly in one simple step. Indeed, the sum of squares of the diagonal elements of $$ \begin{bmatrix} x&y\\ z&t \end{bmatrix} \begin{bmatrix} \cos a&-\sin a\\ \sin a&\cos a \end{bmatrix} $$ can be written as $$ \frac{x^2+y^2+z^2+t^2}2+\frac{x^2+t^2-y^2-z^2}2\cos(2a)+[xy-zt]\sin(2a) $$ so the maximum is achieved when $[\cos(2a), \sin(2a)]$ is just the unit vector in the direction $[\frac{x^2+t^2-y^2-z^2}2, xy-zt]$ (if this latter vector is $[0,0]$, then all $a$ give the same result so you can just choose your favorite one, otherwise the optimal angle can be found using a couple trigonometric functions).

That leads to the idea to make one cycle through three $2D$ coordinate planes optimizing at each step. Let's call the resulting mapping $L\mapsto F(L)$.

Now the natural thing to do would be just to iterate the assignment $L=F(L)$. The iterations indeed, converge reasonably fast (10-15 of them are enough to get to the situation when you have the sum of the squares of the diagonal elements stabilizing within the standard $10^{-15}$ machine precision (note that the matrix itself is not obliged to stabilize that precisely because at the stationary point any deviation by $\sqrt\delta$ from the optimizer results just in the deviation $\delta$ in the value of the functional, so, technically, the fluctuations of the size up to $10^{-8}$ in the matrix elements may continue to occur). Unfortunately, the limit is only guaranteed to be a maximizer with respect to each 2D coordinate rotation but not with respect to the full orthogonal group. It is clear for the matrix $$ \begin{bmatrix} 1& 0.9& -0.9\\ 0.9 & 1 & 0.9\\ -0.9 & 0.9 & 1 \end{bmatrix} $$ where no 2D coordinate rotation can improve anything but the 3D can. However, there are more interesting examples where you arrive at a stable limit (this one was unstable, really) that is far from the global maximum. The computer found, for example, $$ \begin{bmatrix} -0.01 & 0.37 & 0.27\\ -0.18 & 0.28 & -0.24\\ -0.2 & -0.14 & -0.45 \end{bmatrix} $$ If you only iterate $F$ here, you get the limiting value for the sum of squares only of $0.392442207640134$ while the truth is $0.446704013852864$.

To get unstuck, just apply one iteration of your SVD map now and then. Note that while I do not know how many times one can hit the wrong limit, we do know how to recognize that we are at the true $\max$: the matrix $(\rm{diag} L)L$ should be symmetric non-negative definite, so you can just check that property and shake only if needed. In practice, I prefer to shake 4 times with 6 iterations of $F$ in between. Out of a million random examples, none failed to yield a true maximum.

The pseudo-code is below:

real[][] F(real[][] L)
{
real[][] LL=copy(L);
for(int k=0;k<3;++k)
for(int j=k+1;j<3;++j)
{
real A=(LL[k][k]^2+LL[j][j]^2-LL[k][j]^2-LL[j][k]^2)/2, 
B=LL[k][k]*LL[k][j]-LL[j][j]*LL[j][k];
complex P=A+Bi; if(P==0) P=1; else P/=abs(P);
P=sqrt(P);
real[][] U=copy(IdentityMatrix);
U[k][k]=P.x; U[k][j]=-P.y;
U[j][j]=P.x; U[j][k]=P.y;
LL=LL*U;
}
return LL;
}



real[][] MAP(real[][] L)
{
real[][] d=diag(L); 
for(int k=0;k<n;++k)
{
struct T {real[][] U; real[][] D; real[][] V}
T f=SVD(d*L); // so d*L=U*D*V is the singular value decomposition
return L*transpose(f.V)*transpose(f.U);
}

int M=6, K=5;
for(int m=0;m<M;++m)
{
for(int k=0;k<K;++k) L=F(L);
L=MAP1(L);
}

I leave writing the accurate check of the non-negativity of a quadratic form to you. For non-degenerate $L$, it suffices to check that $(\rm{diag}L)L$ has positive determinant but degenerate matrices require more care. Or we can think a bit of why such a check is not really necessary. That gives you $S$. As to $R$, you can either trace it through the process or, better, just compute it once at the end where you know $S$ (just store the original copy of $L$ to use it at that point).

You may want to replace the for loop over m by the while loop until you reach the desired precision. I guess $K=5$ is a fair equivalent of one SVD step just because bringing a 3 by 3 symmetric matrix to the diagonal form by 2D coordinate rotations usually takes 4-5 cycles over coordinate pairs, but even $K=1$ looks better than two pure MAP1 steps.

Try this and let me know if that gives you improved performance. My experiments show that the combination that I used is 2-3 times more efficient than pure SVD iterations in any reasonable metric, but I don't know if it is sufficient for your purposes. :-)

$\endgroup$
0
$\begingroup$

I'm still not sure whether a closed form solution exists. But I recognized that most implementations of SVD will be iterative (the simplest being power iterations), so the block-coordinate descent in the original question can be understood as

while not converged
    update s ← diag(LRᵀ)
    while not converged
       improve svd [U,∑,V] = diag(s) L
    update R ← UVᵀ

The idea here (presented without proof of convergence but with convincing randomized testing) is to merge these while-loops and update $s$ during the iterative singular value decomposition of $\text{diag}(s) L$:

s ← √diag(LLᵀ), initial guess
V ← L, initial guess of eigen vectors of Lᵀ diag(s)² L
while not converged
   V ← Lᵀ diag(s)² L V, power iteration
   V ← gram-schmidt on columns so that Vᵀ V = I
   d ← diag(Vᵀ Lᵀ diag(s)² L V), eigen values
   z ← √d,  adjust so that U diag(z) V = diag(s) L
   U ← diag(s) L V diag(z)⁻¹
   R ← U Vᵀ, update R
   s ← diag(L Rᵀ), update s

There's no need to track $R$ if we can decide to stop based on $s$ alone, so it might be possible to avoid some multiplications in there.

My experiments indicate that this converges just as quickly with respect to (outer) iteration count as the block-descent. It's my understanding Jacobi method is superior to power iterations, so I wonder if the same idea could be applied there.

$\endgroup$
3
  • $\begingroup$ I'm not terribly happy with my initial guess $V \leftarrow L$. Looking around for how this can be improved. $\endgroup$ Commented Nov 19, 2022 at 0:24
  • $\begingroup$ I'll try both methods when I have time and see if I agree with you on the speeds (I'm especially concerned about the scenario when $L$ is nearly singular). Unfortunately, it seems like it'll have to wait until the Thanksgiving is over :-) $\endgroup$
    – fedja
    Commented Nov 22, 2022 at 3:41
  • $\begingroup$ OK, I believe I figured out how it should really be done. Now I'm testing it on a few millions of random Gaussian matrices plus on some degenerate cases. If it works out to my satisfaction, I'll post the code with which (I hope) you will be happy :) $\endgroup$
    – fedja
    Commented Nov 25, 2022 at 16:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .