symmetric solutions of linear matrix equations
4 views (last 30 days)
Show older comments
I have X symmetric matrix , X is similar to X=[X1, X2, X3; X2, X4, X5; X3, X5, X6 ] size is changeable. and want to compute the unkown values so I vectorize the X.
I vectorize X matrix to solve XA=b but when converting a matrix in one column/row vector there are repeating unkowns but I do not want them in my solution matrix. How can I solve it?
Vec(X)*A=b
Vec(X)=b*A^-1
I want to write a matlab function to solve the problem.
3 Comments
Dyuman Joshi
on 6 Apr 2024
"X is a symmetric matrix including unkonwn values"
How many values are unknown?
What are the values in A and B?
"I have A and B matrices. I need to solve X
(AX = B )"
(Assuming compatible dimensions) If X is a vector, A*X will be a vector, which will not be equal to be B, a matrix.
Please share the code you have written yet and the values for A and B.
Accepted Answer
Bruno Luong
on 7 Apr 2024
Edited: Bruno Luong
on 7 Apr 2024
If you have tthe optimization toolbox
% Generate test matrices
n = 3;
A = randn(n);
X=rand(n); X = X+X.';
B = X*A;
% Add small noise to B
B = B + 1e-1*randn(size(B))
ij = nchoosek(1:n,2)-1;
m = size(ij,1);
r = (1:m)';
C = zeros([m n^2]);
C(r + ij * [n; 1] * m) = 1;
C(r + ij * [1; n] * m) = -1;
M = kron(A',eye(n));
X = lsqlin(M,B(:),[],[],C,zeros(m,1));
X = reshape(X,n,n)
XA = X*A
norm(XA-B,'fro')/norm(B,'fro') % error
2 Comments
Bruno Luong
on 7 Apr 2024
Edited: Bruno Luong
on 7 Apr 2024
Yes, the problem is
given A, B two (n x n) matrices
minimizing norm(X*A - B, 'fro')
such that X = X.';
More Answers (3)
Bruno Luong
on 8 Apr 2024
Edited: Bruno Luong
on 8 Apr 2024
This is a method that use the small "original" linear system. I use pcg since it a linear solver that can accept function handle instead of matrix coefficients.
No toolbox is required. Note the convergence of pcg seems to be fragile without preconditioning.
% Generate test matrices
n = 5;
A = randn(n);
X=rand(n); X = X+X.';
B = X*A;
% Add small noise to B
B = B + 1e-1*randn(size(B));
% Result from expanded kron system
ij = nchoosek(0:n-1,2);
m = size(ij,1);
r = (1:m)';
C = sparse(r, ij * [n; 1] +1, 1, m, n^2) - ...
sparse(r, ij * [1; n] +1, 1, m, n^2);
M = kron(A, speye(n));
Y=[M*B(:); zeros(m,1)]';
M=[M*M', C';
C, zeros(m)];
X=Y/M;
X = reshape(X(1:n^2),n,n) % symmetric
norm(X - X', 'fro');
XA = X*A; % should be close to B
norm(XA-B,'fro')/norm(B,'fro') % error
% New method starts here tor
% Solve
% X = E*Upper, E is a linear operator that expands upper to symmetric matrix
% X*A = B, minimize "fro" norm sense
tu=triu(true(n)); % only UPPER coefficients of that positions is considered
AtB = adj_symmlprod(tu, A, B); % multiply RHS by (E*A)'
upper = pcg(@(upper) normalsymmlprod(upper, tu, A), AtB);
XX = uexpand(upper, tu)
norm(XX*A-B,'fro')/norm(B,'fro') % error
% E operator: Expand triangular part to symmetric matrix
% Note the diagonal is double
function X = uexpand(upper, tu)
X = zeros(size(tu));
X(tu) = upper;
X = X + X.';
end
% Adjoint of uexpand (E')
function upper = adj_uexpand(tu, X)
X = X + X.';
upper = X(tu);
end
% Model E*A
function Y = symmlprod(upper, tu, A)
Y = uexpand(upper, tu)*A;
end
% Adkoint of symmlprod, A'*E'
function upper = adj_symmlprod(tu, A, Y)
upper = adj_uexpand(tu, Y*A');
end
% Model followed by the adjoint
function res = normalsymmlprod(upper, tu, A)
Y = symmlprod(upper, tu, A);
res = adj_symmlprod(tu, A, Y);
end
0 Comments
Bruno Luong
on 6 Apr 2024
Edited: Bruno Luong
on 6 Apr 2024
% Generate test matrices
n = 5;
A = randn(n);
X=rand(n); X = X+X.';
B = A*X;
% Add small noise to B
B = B + 1e-1*randn(size(B))
[i,j] = find(triu(ones(n),1));
k = sub2ind([n,n],i,j);
l = sub2ind([n,n],j,i);
m = numel(i);
r = (1:m)';
s = [m n^2];
C=accumarray([r k(:)],1,s)-accumarray([r l(:)],1,s);
M = kron(eye(n),A);
Y=[M'*B(:); zeros(m,1)];
M=[M'*M, C';
C zeros(m)];
X=M\Y;
X = reshape(X(1:n^2),n,n) % symmetric
norm(X - X', 'fro')
AX = A*X % should be close to B
norm(AX-B,'fro')/norm(B,'fro') % error
% It should be better than solving original matrix then symmetrizeing it
XX = A\B;
XX = 1/2*(XX+XX');
norm(A*XX-B,'fro')/norm(B,'fro')
11 Comments
Bruno Luong
on 7 Apr 2024
"Since x0 is ignored, one could remove its use."
Oh you are completely right.
Paul
on 7 Apr 2024
Moved: Bruno Luong
on 7 Apr 2024
Here's an alternative using mldivide, \ that arrives at the same result for this particular problem. Is it effectively the same solution as yours using lsqlin?
rng(100)
% Generate test matrices
n = 3;
A = randn(n);
X=rand(n); X = X+X.';
B = X*A;
% Add small noise to B
B = B + 1e-1*randn(size(B));
ij = nchoosek(1:n,2)-1;
m = size(ij,1);
r = (1:m)';
C = zeros([m n^2]);
C(r + ij * [n; 1] * m) = 1;
C(r + ij * [1; n] * m) = -1;
M = kron(A',eye(n));
X = lsqlin(M,B(:),[],[],C,zeros(m,1));
X = reshape(X,n,n)
XA = X*A;
norm(XA-B,'fro')/norm(B,'fro') % error
XX = (kron(eye(3),A')*insmat(3)) \ reshape(B',[],1)
XX = reshape(insmat(3)*XX,3,3)
function Is = insmat(s)
% reference: Vetter, W.J., "Vector Structures and Solutions of Linear
% Matrix Equations," Linear Algebra and Its Applications, 10, 181-188,
% 1975
% first form the index matrix m
m = tril(ones(s,s));
m(m>0) = 1:(s*(s+1)/2);
m = m + tril(m,-1)';
I = eye(s*(s+1)/2,s*(s+1)/2);
Is = I(m(:),:);
end
1 Comment
Bruno Luong
on 7 Apr 2024
Moved: Bruno Luong
on 7 Apr 2024
Not necessary your method parametrizes the subspace of matrices that meet the constraints X = X.', then solve the leastsqure in term of parametrization.
This can also be done by find the (orthogonal) basis null C, which can be carried out by QR decomosition on C'.
Other alternative is forming a KKT system and solve it.
Not sure lsqlin uses which method.
The KKT method is used by my my second answer.
See Also
Categories
Find more on Linear Algebra in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!