A high-performance general-purpose compute library

Pseudo-invert (Moore-Penrose) a matrix. More...

Functions

AFAPI array pinverse (const array &in, const double tol=1E-6, const matProp options=AF_MAT_NONE)
 C++ Interface to pseudo-invert (Moore-Penrose) a matrix. More...
 
AFAPI af_err af_pinverse (af_array *out, const af_array in, const double tol, const af_mat_prop options)
 C Interface to pseudo-invert (Moore-Penrose) a matrix. More...
 

Detailed Description

Pseudo-invert (Moore-Penrose) a matrix.

This function calculates the Moore-Penrose pseudoinverse of a matrix \(A\), using af::svd at its core. If \(A\) is of size \(M \times N\), then its pseudoinverse \(A^+\) will be of size \(N \times M\).

This calculation can be batched if the input array is three or four-dimensional \((M \times N \times P \times Q\), with \(Q=1\) for only three dimensions \()\). Each \(M \times N\) slice along the third dimension will have its own pseudoinverse, for a total of \(P \times Q\) pseudoinverses in the output array \((N \times M \times P \times Q)\).

Below is an example snippet of its usage. In this example, we have a matrix \(A\) and compute its pseudoinverse \(A^+\). This condition must hold: \(AA^+A=A\), given that the two matrices are pseudoinverses of each other (in fact, this is one of the Moore-Penrose conditions):

float hA[] = {0, 1, 2, 3, 4, 5};
array A(3, 2, hA);
// 0.0000 3.0000
// 1.0000 4.0000
// 2.0000 5.0000
array Apinv = pinverse(A);
// -0.7778 -0.1111 0.5556
// 0.2778 0.1111 -0.0556
array MustBeA = matmul(A, Apinv, A);
// 0.0000 3.0000
// 1.0000 4.0000
// 2.0000 5.0000

Function Documentation

◆ af_pinverse()

AFAPI af_err af_pinverse ( af_array out,
const af_array  in,
const double  tol,
const af_mat_prop  options 
)

C Interface to pseudo-invert (Moore-Penrose) a matrix.

Currently uses the SVD-based approach.

Parameter tol is not the actual lower threshold, but it is passed in as a parameter to the calculation of the actual threshold relative to the shape and contents of in.

Suggested parameters for tol: 1e-6 for single precision and 1e-12 for double precision.

Parameters
[out]outpseudo-inverse matrix
[in]ininput matrix
[in]toldefines the lower threshold for singular values from SVD
[in]optionsmust be AF_MAT_NONE (more options might be supported in the future)
Returns
AF_SUCCESS, if function returns successfully, else an af_err code is given

◆ pinverse()

AFAPI array pinverse ( const array in,
const double  tol = 1E-6,
const matProp  options = AF_MAT_NONE 
)

C++ Interface to pseudo-invert (Moore-Penrose) a matrix.

Currently uses the SVD-based approach.

Parameter tol is not the actual lower threshold, but it is passed in as a parameter to the calculation of the actual threshold relative to the shape and contents of in.

This function is not supported in GFOR.

Parameters
[in]ininput matrix
[in]toldefines the lower threshold for singular values from SVD
[in]optionsmust be AF_MAT_NONE (more options might be supported in the future)
Returns
pseudo-inverse matrix