[python-arrayfire] 139/250: FEAT: Adding SVD

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Mon Mar 28 22:59:41 UTC 2016


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

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

commit 2c79746d7d165093f5c5e183ae5a3e98fe26d916
Author: Pavan Yalamanchili <pavan at arrayfire.com>
Date:   Wed Nov 11 10:26:09 2015 -0500

    FEAT: Adding SVD
---
 arrayfire/lapack.py    | 71 +++++++++++++++++++++++++++++++++++++++++++++++++-
 tests/simple/lapack.py |  7 +++++
 2 files changed, 77 insertions(+), 1 deletion(-)

diff --git a/arrayfire/lapack.py b/arrayfire/lapack.py
index 1b15456..ae62f3c 100644
--- a/arrayfire/lapack.py
+++ b/arrayfire/lapack.py
@@ -8,7 +8,7 @@
 ########################################################
 
 """
-dense linear algebra functions for arrayfire.
+Dense Linear Algebra functions for arrayfire.
 """
 
 from .library import *
@@ -339,3 +339,72 @@ def norm(A, norm_type=NORM.EUCLID, p=1.0, q=1.0):
     safe_call(backend.get().af_norm(ct.pointer(res), A.arr, norm_type.value,
                                     ct.c_double(p), ct.c_double(q)))
     return res.value
+
+def svd(A):
+    """
+    Singular Value Decomposition
+
+    Parameters
+    ----------
+    A: af.Array
+       A 2 dimensional arrayfire array.
+
+    Returns
+    -------
+    (U,S,Vt): tuple of af.Arrays
+           - U - A unitary matrix
+           - S - An array containing the elements of diagonal matrix
+           - Vt - A unitary matrix
+
+    Note
+    ----
+
+    - The original matrix `A` is preserved and additional storage space is required for decomposition.
+
+    - If the original matrix `A` need not be preserved, use `svd_inplace` instead.
+
+    - The original matrix `A` can be reconstructed using the outputs in the following manner.
+    >>> Smat = af.diag(S, 0, False)
+    >>> A_recon = af.matmul(af.matmul(U, Smat), Vt)
+
+    """
+    U = Array()
+    S = Array()
+    Vt = Array()
+    safe_call(backend.get().af_svd(ct.pointer(U.arr), ct.pointer(S.arr), ct.pointer(Vt.arr), A.arr))
+    return U, S, Vt
+
+def svd_inplace(A):
+    """
+    Singular Value Decomposition
+
+    Parameters
+    ----------
+    A: af.Array
+       A 2 dimensional arrayfire array.
+
+    Returns
+    -------
+    (U,S,Vt): tuple of af.Arrays
+           - U - A unitary matrix
+           - S - An array containing the elements of diagonal matrix
+           - Vt - A unitary matrix
+
+    Note
+    ----
+
+    - The original matrix `A` is not preserved.
+
+    - If the original matrix `A` needs to be preserved, use `svd` instead.
+
+    - The original matrix `A` can be reconstructed using the outputs in the following manner.
+    >>> Smat = af.diag(S, 0, False)
+    >>> A_recon = af.matmul(af.matmul(U, Smat), Vt)
+
+    """
+    U = Array()
+    S = Array()
+    Vt = Array()
+    safe_call(backend.get().af_svd_inplace(ct.pointer(U.arr), ct.pointer(S.arr), ct.pointer(Vt.arr),
+                                           A.arr))
+    return U, S, Vt
diff --git a/tests/simple/lapack.py b/tests/simple/lapack.py
index fa2731f..8ff59c2 100644
--- a/tests/simple/lapack.py
+++ b/tests/simple/lapack.py
@@ -74,4 +74,11 @@ def simple_lapack(verbose=False):
     print_func(af.norm(a, af.NORM.MATRIX_INF))
     print_func(af.norm(a, af.NORM.MATRIX_L_PQ, 1, 1))
 
+    a = af.randu(10,10)
+    display_func(a)
+    u,s,vt = af.svd(a)
+    display_func(af.matmul(af.matmul(u, af.diag(s, 0, False)), vt))
+    u,s,vt = af.svd_inplace(a)
+    display_func(af.matmul(af.matmul(u, af.diag(s, 0, False)), vt))
+
 _util.tests['lapack'] = simple_lapack

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



More information about the debian-science-commits mailing list