[arrayfire] 124/408: Add SVD API

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Mon Sep 21 19:11:35 UTC 2015


This is an automated email from the git hooks/post-receive script.

ghisvail-guest pushed a commit to branch debian/sid
in repository arrayfire.

commit 12e4f9dfac921225abed7d50f815d77afa26ec37
Author: Richard Klemm <richy at coding-reality.de>
Date:   Sat Jul 4 22:33:38 2015 +0200

    Add SVD API
---
 include/af/lapack.h    |  47 +++++++++++++++++++
 src/api/c/svd.cpp      | 119 +++++++++++++++++++++++++++++++++++++++++++++++++
 src/api/cpp/lapack.cpp |  18 ++++++++
 3 files changed, 184 insertions(+)

diff --git a/include/af/lapack.h b/include/af/lapack.h
index ffedac1..7c8e6fe 100644
--- a/include/af/lapack.h
+++ b/include/af/lapack.h
@@ -14,6 +14,29 @@
 #ifdef __cplusplus
 namespace af
 {
+    /**
+       C++ Interface for SVD decomposition
+
+       \param[out] s is the output array containing the diagonal values of sigma, (singular values of the input matrix))
+       \param[out] u is the output array containing U
+       \param[out] vt is the output array containing V^H
+       \param[in] in is the input matrix
+
+       \ingroup lapack_factor_func_svd
+    */
+    AFAPI void svd(array &s, array &u, array &vt, const array &in);
+
+    /**
+       C++ Interface for SVD decomposition
+
+       \param[out] s is the output array containing the diagonal values of sigma, (singular values of the input matrix))
+       \param[out] u is the output array containing U
+       \param[out] vt is the output array containing V^H
+       \param[inout] in is the input matrix and will contain random data after this operation
+
+       \ingroup lapack_factor_func_svd
+    */
+    AFAPI void svdInPlace(array &s, array &u, array &vt, array &in);
 
     /**
        C++ Interface for LU decomposition in packed format
@@ -218,6 +241,30 @@ extern "C" {
 #endif
 
     /**
+       C Interface for SVD decomposition
+
+       \param[out] s is the output array containing the diagonal values of sigma, (singular values of the input matrix))
+       \param[out] u is the output array containing U
+       \param[out] vt is the output array containing V^H
+       \param[in] in is the input matrix
+
+       \ingroup lapack_factor_func_svd
+    */
+    AFAPI af_err af_svd(af_array *s, af_array *u, af_array *vt, const af_array in);
+
+    /**
+       C Interface for SVD decomposition
+
+       \param[out] s is the output array containing the diagonal values of sigma, (singular values of the input matrix))
+       \param[out] u is the output array containing U
+       \param[out] vt is the output array containing V^H
+       \param[inout] in is the input matrix that will contain random data after this operation
+
+       \ingroup lapack_factor_func_svd
+    */
+    AFAPI af_err af_svd_inplace(af_array *s, af_array *u, af_array *vt, af_array in);
+
+    /**
        C Interface for LU decomposition
 
        \param[out] lower will contain the lower triangular matrix of the LU decomposition
diff --git a/src/api/c/svd.cpp b/src/api/c/svd.cpp
new file mode 100644
index 0000000..d49c02b
--- /dev/null
+++ b/src/api/c/svd.cpp
@@ -0,0 +1,119 @@
+/*******************************************************
+ * Copyright (c) 2014, ArrayFire
+ * All rights reserved.
+ *
+ * This file is distributed under 3-clause BSD license.
+ * The complete license agreement can be obtained at:
+ * http://arrayfire.com/licenses/BSD-3-Clause
+ ********************************************************/
+
+#include <af/dim4.hpp>
+#include <af/array.h>
+#include <af/lapack.h>
+
+#include <af/util.h>
+
+#include <af/defines.h>
+
+#include <err_common.hpp>
+
+#include <backend.hpp>
+
+#include <Array.hpp>
+
+#include <handle.hpp>
+
+#include <svd.hpp>
+
+using namespace detail;
+
+template <typename T>
+static inline void svd(af_array *s, af_array *u, af_array *vt, const af_array in)
+{
+    ArrayInfo info = getInfo(in);  // ArrayInfo is the base class which
+    af::dim4 dims = info.dims();
+    int M = dims[0];
+    int N = dims[1];
+
+    //Allocate output arrays
+    Array<T> sA = createEmptyArray<T>(af::dim4(min(M, N)));
+    Array<T> uA = createEmptyArray<T>(af::dim4(M, M));
+    Array<T> vtA = createEmptyArray<T>(af::dim4(N, N));
+
+    svd<T>(sA, uA, vtA, getArray<T>(in));
+
+    *s = getHandle(sA);
+    *u = getHandle(uA);
+    *vt = getHandle(vtA);
+}
+
+template <typename T>
+static inline void svdInPlace(af_array *s, af_array *u, af_array *vt, af_array in){
+    ArrayInfo info = getInfo(in);  // ArrayInfo is the base class which
+    af::dim4 dims = info.dims();
+    int M = dims[0];
+    int N = dims[1];
+
+    //Allocate output arrays
+    Array<T> sA = createEmptyArray<T>(af::dim4(min(M, N)));
+    Array<T> uA = createEmptyArray<T>(af::dim4(M, M));
+    Array<T> vtA = createEmptyArray<T>(af::dim4(N, N));
+
+    svdInPlace<T>(sA, uA, vtA, getWritableArray<T>(in));
+
+    *s = getHandle(sA);
+    *u = getHandle(uA);
+    *vt = getHandle(vtA);
+}
+
+af_err af_svd(af_array *s, af_array *u, af_array *vt, const af_array in)
+{
+    try {
+        ArrayInfo info = getInfo(in);
+
+        af::dim4 dims = info.dims();
+
+        ARG_ASSERT(2, (dims.ndims() >= 0 && dims.ndims() <= 3));
+
+        af_dtype type = info.getType();
+
+        switch (type) {
+            case f64:
+                svd<double>(s, u, vt, in);
+                break;
+            case f32:
+                svd<float>(s, u, vt, in);
+                break;
+            default:
+                TYPE_ERROR(1, type);
+        }
+    }
+    CATCHALL;
+    return AF_SUCCESS;
+}
+
+af_err af_svd_inplace(af_array *s, af_array *u, af_array *vt, af_array in)
+{
+    try {
+        ArrayInfo info = getInfo(in);
+
+        af::dim4 dims = info.dims();
+
+        ARG_ASSERT(2, (dims.ndims() >= 0 && dims.ndims() <= 3));
+
+        af_dtype type = info.getType();
+
+        switch (type) {
+            case f64:
+                svdInPlace<double>(s, u, vt, in);
+                break;
+            case f32:
+                svdInPlace<float>(s, u, vt, in);
+                break;
+            default:
+                TYPE_ERROR(1, type);
+        }
+    }
+    CATCHALL;
+    return AF_SUCCESS;
+}
diff --git a/src/api/cpp/lapack.cpp b/src/api/cpp/lapack.cpp
index 139ae0b..2b03dba 100644
--- a/src/api/cpp/lapack.cpp
+++ b/src/api/cpp/lapack.cpp
@@ -13,6 +13,24 @@
 
 namespace af
 {
+    void svd(array &s, array &u, array &vt, const array &in)
+    {
+        af_array sl = 0, ul = 0, vtl = 0;
+        AF_THROW(af_svd(&sl, &ul, &vtl, in.get()));
+        s = array(sl);
+        u = array(ul);
+        vt = array(vtl);
+    }
+
+    void svdInPlace(array &s, array &u, array &vt, array &in)
+    {
+        af_array sl = 0, ul = 0, vtl = 0;
+        AF_THROW(af_svd_inplace(&sl, &ul, &vtl, in.get()));
+        s = array(sl);
+        u = array(ul);
+        vt = array(vtl);
+    }
+
     void lu(array &out, array &pivot, const array &in, const bool is_lapack_piv)
     {
         out = in.copy();

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/arrayfire.git



More information about the debian-science-commits mailing list