0001 function [x]=als_solve_rx(mat, rhs, tol, drx, nswp, addswp)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026 a1 = mat{1}; a2 = mat{2};
0027 n1 = size(a1,1); m1 = size(a1,2);
0028 n2 = size(a2,1); m2 = size(a2,2);
0029 ra = size(a1,3);
0030
0031
0032
0033 rhs = reshape(rhs, n1, n2);
0034
0035 if (nargin<3)||(isempty(tol))
0036 tol=1e-12;
0037 end;
0038 if (nargin<4)||(isempty(drx))
0039 drx=1;
0040 end;
0041 if (nargin<5)||(isempty(nswp))
0042 nswp=10;
0043 end;
0044 if (nargin<6)||(isempty(addswp))
0045 addswp=2;
0046 end;
0047
0048 if (drx>m1)||(drx>m2)
0049 drx = min(m1,m2);
0050 end;
0051
0052 rx=1;
0053
0054 curx = cell(2,1);
0055 curx{1}=rand(m1,rx);
0056 curx{2}=rand(m2,rx);
0057 x = curx{1}*(curx{2}.');
0058 derr = 2;
0059 sp = 0;
0060 resid_old = 1;
0061 for swp=1:nswp
0062
0063
0064
0065 [q,rv]=qr(curx{2},0);
0066 rx = size(q,2);
0067 curx{2}=q;
0068
0069
0070
0071 a2 = permute(mat{2}, [1 3 2]);
0072 a2 = reshape(a2, n2*ra, m2);
0073 phi = a2*curx{2};
0074 phi = reshape(phi, n2, ra*rx);
0075 phi = (phi')*phi;
0076 phi = reshape(phi, ra, rx, ra, rx);
0077 phi = reshape(permute(phi, [1 3 2 4]), ra*ra, rx*rx);
0078
0079
0080
0081
0082
0083
0084
0085 a1 = reshape(permute(mat{1}, [3 2 1]), ra*m1, n1);
0086 a1 = conj(a1)*reshape(mat{1}, n1, m1*ra);
0087 a1 = reshape(a1, ra, m1, m1, ra);
0088 a1 = reshape(permute(a1, [1 4 2 3]), ra*ra, m1*m1);
0089
0090 a1 = (phi.')*a1;
0091 a1 = reshape(a1, rx, rx, m1, m1);
0092 a1 = reshape(permute(a1, [2 4 1 3]), rx*m1, rx*m1);
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102 rhs1 = rhs*conj(reshape(mat{2}, n2, m2*ra));
0103 rhs1 = reshape(rhs1, n1, m2, ra);
0104 rhs1 = reshape(permute(rhs1, [1 3 2]), n1*ra, m2);
0105
0106 rhs1 = conj(reshape(permute(mat{1}, [2 1 3]), m1, n1*ra))*rhs1;
0107
0108 rhs1 = rhs1*conj(curx{2});
0109 rhs1 = reshape(rhs1.', rx*m1, 1);
0110
0111 curx{1}=a1 \ rhs1;
0112
0113 curx{1}=reshape(curx{1}, rx, m1).';
0114
0115
0116 if (mod(swp,addswp)==0)
0117
0118 curx{1}=[curx{1}, randn(m1,drx)];
0119
0120 end;
0121
0122
0123
0124 [q,rv]=qr(curx{1},0);
0125 rx = size(q,2);
0126 curx{1}=q;
0127
0128
0129 a1 = permute(mat{1}, [1 3 2]);
0130 a1 = reshape(a1, n1*ra, m1);
0131 phi = a1*q;
0132 phi = reshape(phi, n1, ra*rx);
0133 phi = (phi')*phi;
0134 phi = reshape(phi, ra, rx, ra, rx);
0135 phi = reshape(permute(phi, [1 3 2 4]), ra*ra, rx*rx);
0136
0137
0138
0139
0140
0141
0142 a2 = reshape(permute(mat{2}, [3 2 1]), ra*m2, n2);
0143 a2 = conj(a2)*reshape(mat{2}, n2, m2*ra);
0144 a2 = reshape(a2, ra, m2, m2, ra);
0145 a2 = reshape(permute(a2, [1 4 2 3]), ra*ra, m2*m2);
0146
0147 a2 = (phi.')*a2;
0148 a2 = reshape(a2, rx, rx, m2, m2);
0149 a2 = reshape(permute(a2, [2 4 1 3]), rx*m2, rx*m2);
0150
0151
0152 rhs2 = rhs*conj(reshape(mat{2}, n2, m2*ra));
0153 rhs2 = reshape(rhs2, n1, m2, ra);
0154 rhs2 = reshape(permute(rhs2, [1 3 2]), n1*ra, m2);
0155
0156 rhs2 = conj(reshape(permute(mat{1}, [2 1 3]), m1, n1*ra))*rhs2;
0157
0158 rhs2 = (curx{1}')*rhs2;
0159 rhs2 = reshape(rhs2, rx*m2, 1);
0160
0161 curx{2}=a2 \ rhs2;
0162 curx{2}=reshape(curx{2}, rx, m2).';
0163
0164 x_new = curx{1}*(curx{2}.');
0165 derr = norm(x_new(:)-x(:))/norm(x(:));
0166
0167 x = x_new;
0168
0169 resid = norm(tt_mat_full_vec(mat, x(:))-rhs(:))/norm(rhs(:));
0170 conv_fact = resid_old/resid;
0171 if (conv_fact-1<1e-4)
0172 sp=sp+1;
0173 end;
0174
0175 fprintf('als_solve: swp=%d, derr=%3.3e, rx=%d, resid=%3.3e, conv-1=%3.5e\n', swp, derr, rx, resid, conv_fact-1);
0176 if (derr<tol)
0177 break;
0178 end;
0179 resid_old = resid;
0180 end;
0181
0182 x = x(:);
0183
0184 end