Converts [U,d] from udfactor into the original matrix that the
factors represent, U * diag(d) * U.', with minimal operations.
This also works with multiple sets of U and d, stored along the 3rd
and 2nd dimension of each, respectively.
M = ud2mat(U, d)Finally, this works with the compact form of UDU factorization, where d
is not created explicitly and is stored on the diagonal of U.
U | Upper unitriangular part of UDU factorization |
|---|---|
d | Diagonals of UDU factorization |
M | Reconstructed matrix (or matrices) |
|---|
Let's generate a positive-definite matrix, factorize, and show that the factors reconstruct properly:
M = randcov(3);
[U, d] = udfactor(M);
U * diag(d) * U.'
ud2mat(U, d)ans =
0.5052 -0.2311 -0.2059
-0.2311 0.5124 -0.0714
-0.2059 -0.0714 0.7502
ans =
0.5052 -0.2311 -0.2059
-0.2311 0.5124 -0.0714
-0.2059 -0.0714 0.7502We can also use ud2mat with the compact UDU decomposition to save space
and time.
Z = udfactor(M);
ud2mat(Z)ans =
0.5052 -0.2311 -0.2059
-0.2311 0.5124 -0.0714
-0.2059 -0.0714 0.7502Let's generate a positive-definte matrix for each of 10 samples of each of 5 runs.
nx = 3;
ns = 10;
nr = 5;
M = zeros(nx, nx, ns, nr);
U = zeros(nx, nx, ns, nr);
d = zeros(nx, ns, nr);
for s = 1:ns
for r = 1:nr
M(:, :, s, r) = randcov(nx);
[U(:, :, s, r), d(:, s, r)] = udfactor(M(:, :, s, r));
end
endLet's reconstruct all of the matrices in M all at once and show the difference.
M2 = ud2mat(U, d);
max(abs(M(:) - M2(:)))ans =
1.1102e-16ldfactor, ld2mat, udfactor, sqrtpsdm
*kf v1.0.3