MatX
MatX copied to clipboard
[FEA] diag() support 1D tensor
Is your feature request related to a problem? Please describe.
It is possible to decompose a matrix A into two matrices U and VT and a 1D vector S using various SVD implementations. It should be possible to reconstruct the original matrix A using MatX operations. This is useful, amongst other reasons, for writing a unit test to validate that the SVD was done correctly.
https://numpy.org/doc/stable/reference/generated/numpy.diag.html has the desired behavior for numpy.diag(v, k=0) for 1D array v.
Describe the solution you'd like
To reconstruct a matrix that has been SVD decomposed into two matrices U and VT and a 1D vector S, it would be useful to do matmul(matmul(U,diag(S)),VT)
Describe alternatives you've considered
Presently diag() only accepts a constant/0D tensor so one must manually create the diagonal matrix Smat using SetVals().
For this specific reconstruction problem it's also possible (and more computationally efficient) to use broadcasting and do A = matmul(U * S), VT)