# Inverse of non-square matrix

Hello,

Is it possible to calculate the pseudo-inverse of a non-square matrix in Stan?

``````generated quantities {
...
Dt = inverse(Z) * Zt * D * (Zt)' * inverse((Z)');
...
}
``````

It returns “Exception: inverse: Expecting a square matrix; rows of m () and columns of m () must match in size”. Same for matrix division \ and / .

I also tried to use SVD to compute inverse(A), based on A = USV^{T}, A^{+} = VS^{+}U^{T} . However,

``````vector singular_values(matrix A)
``````

it only gives singular values of A, not V and U.

Thanks!

V consists of (normalized) eigenvectors of A^TA.

I wonder why V or U is not provided through `singular_values`.

Inability to return an array of matrices (or vectors) with different dimensions.

One option is to return concatenated \Sigma, U^T, V, ugly but it avoids put users into brute forcing eigenvector of A^TA.

Thanks for your replies. However, I found stan can only compute eigenvector for symmetric matrix.

``````matrix eigenvectors_sym(matrix A)
``````

Thanks for your suggestion! I had tried. I used `qr_thin_Q`, but it exceeds the memory of my computer. :/