lin_algebra/svd.cpp
/*******************************************************
* Copyright (c) 2014, ArrayFire
* All rights reserved.
*
* This file is distributed under 3-clause BSD license.
* The complete license agreement can be obtained at:
* https://arrayfire.com/licenses/BSD-3-Clause
********************************************************/
#include <arrayfire.h>
#include <cstdio>
#include <cstdlib>
using namespace af;
int main(int argc, char* argv[]) {
try {
// Select a device and display arrayfire info
int device = argc > 1 ? atoi(argv[1]) : 0;
af::setDevice(device);
float h_buffer[] = {1, 4, 2, 5, 3, 6}; // host array
array in(2, 3, h_buffer); // copy host data to device
array u;
array s_vec;
array vt;
svd(u, s_vec, vt, in);
array s_mat = diag(s_vec, 0, false);
array in_recon = matmul(u, s_mat, vt(seq(2), span));
af_print(in);
af_print(s_vec);
af_print(s_mat);
af_print(vt);
af_print(in_recon);
} catch (af::exception& e) {
fprintf(stderr, "%s\n", e.what());
throw;
}
return 0;
}
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::seq
seq is used to create sequences for indexing af::array
Definition: seq.h:46
af::info
AFAPI void info()
af::setDevice
AFAPI void setDevice(const int device)
Sets the current device.
af::array
A multi dimensional data container.
Definition: array.h:35
af
Definition: algorithm.h:15
af_print
#define af_print(...)
Definition: util.h:148
af::diag
AFAPI array diag(const array &in, const int num=0, const bool extract=true)
af::exception
An ArrayFire exception class.
Definition: exception.h:29
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.
arrayfire.h
af::exception::what
virtual const char * what() const
Returns an error message for the exception in a string format.
Definition: exception.h:60