Computes the singular value decomposition of a matrix. More...

Functions

AFAPI void svd (array &u, array &s, array &vt, const array &in)
 C++ Interface for SVD decomposition. More...
 
AFAPI void svdInPlace (array &u, array &s, array &vt, array &in)
 C++ Interface for SVD decomposition (in-place) More...
 
AFAPI af_err af_svd (af_array *u, af_array *s, af_array *vt, const af_array in)
 C Interface for SVD decomposition. More...
 
AFAPI af_err af_svd_inplace (af_array *u, af_array *s, af_array *vt, af_array in)
 C Interface for SVD decomposition (in-place) More...
 

Detailed Description

Computes the singular value decomposition of a matrix.

This function factorizes a matrix \(A\) into two unitary matrices, \(U\) and \(V^T\), and a diagonal matrix \(S\), such that \(A = USV^T\). If \(A\) has \(M\) rows and \(N\) columns ( \(M \times N\)), then \(U\) will be \(M \times M\), \(V\) will be \(N \times N\), and \(S\) will be \(M \times N\). However, for \(S\), this function only returns the non-zero diagonal elements as a sorted (in descending order) 1D array.

To reconstruct the original matrix \(A\) from the individual factors, the following code snippet can be used:

array U, S, Vt;
af::svd(U, S, Vt, A);
const int MN = std::min(M, N);
array UU = U(span, seq(MN));
array SS = diag(S, 0, false).as(ty);
array VV = Vt(seq(MN), span);
array AA = matmul(UU, SS, VV);

When memory is a concern, and \(A\) is dispensable, svdInPlace() can be used. However, this in-place version is currently limited to input arrays where \(M \geq N\).


Function Documentation

◆ af_svd()

AFAPI af_err af_svd ( af_array u,
af_array s,
af_array vt,
const af_array  in 
)

C Interface for SVD decomposition.

Parameters
[out]uis the output array containing U
[out]sis the output array containing the diagonal values of sigma, (singular values of the input matrix))
[out]vtis the output array containing V^H
[in]inis the input matrix

◆ af_svd_inplace()

AFAPI af_err af_svd_inplace ( af_array u,
af_array s,
af_array vt,
af_array  in 
)

C Interface for SVD decomposition (in-place)

Parameters
[out]uis the output array containing U
[out]sis the output array containing the diagonal values of sigma, (singular values of the input matrix))
[out]vtis the output array containing V^H
[in,out]inis the input matrix that will contain random data after this operation
Note
Currently, in is limited to arrays where dim0 \(\geq\) dim1
This is best used when minimizing memory usage and in is dispensable

◆ svd()

AFAPI void af::svd ( array u,
array s,
array vt,
const array in 
)

C++ Interface for SVD decomposition.

Parameters
[out]uis the output array containing U
[out]sis the output array containing the diagonal values of sigma, (singular values of the input matrix))
[out]vtis the output array containing V^H
[in]inis the input matrix
Examples
lin_algebra/svd.cpp.

◆ svdInPlace()

AFAPI void af::svdInPlace ( array u,
array s,
array vt,
array in 
)

C++ Interface for SVD decomposition (in-place)

Parameters
[out]uis the output array containing U
[out]sis the output array containing the diagonal values of sigma, (singular values of the input matrix))
[out]vtis the output array containing V^H
[in,out]inis the input matrix and will contain random data after this operation
Note
Currently, in is limited to arrays where dim0 \(\geq\) dim1
This is best used when minimizing memory usage and in is dispensable
af::matmul
AFAPI array matmul(const array &lhs, const array &rhs, const matProp optLhs=AF_MAT_NONE, const matProp optRhs=AF_MAT_NONE)
Matrix multiply of two arrays.
af::array::as
const array as(dtype type) const
Converts the array into another type.
af::diag
AFAPI array diag(const array &in, const int num=0, const bool extract=true)
afcl::array
static af::array array(af::dim4 idims, cl_mem buf, af::dtype type, bool retain=false)
Create an af::array object from an OpenCL cl_mem buffer.
Definition: opencl.h:327
af::span
AFAPI seq span
A special value representing the entire axis of an af::array.
af::svd
AFAPI void svd(array &u, array &s, array &vt, const array &in)
C++ Interface for SVD decomposition.
af::min
AFAPI array min(const array &in, const int dim=-1)
C++ Interface for minimum values in an array.