-
Notifications
You must be signed in to change notification settings - Fork 14
/
pygsvd.py
151 lines (128 loc) · 5.09 KB
/
pygsvd.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import numpy as np
import _gsvd
def gsvd(A, B, full_matrices=False, extras='uv'):
'''Compute the generalized singular value decomposition of
a pair of matrices ``A`` of shape ``(m, n)`` and ``B`` of
shape ``(p, n)``
The GSVD is defined as a joint decomposition, as follows.
A = U*C*X.T
B = V*S*X.T
where
C.T*C + S.T*S = I
where ``U`` and ``V`` are unitary matrices.
Parameters
----------
A, B : ndarray
Input matrices on which to perform the decomposition. Must
be no more than 2D (and will be promoted if only 1D). The
matrices must also have the same number of columns.
full_matrices : bool, optional
If ``True``, the returned matrices ``U`` and ``V`` have
at most ``p`` columns and ``C`` and ``S`` are of length ``p``.
extras : str, optional
A string indicating which of the orthogonal transformation
matrices should be computed. By default, this only computes
the generalized singular values in ``C`` and ``S``, and the
right generalized singular vectors in ``X``. The string may
contain either 'u' or 'v' to indicate that the corresponding
matrix is to be computed.
Returns
-------
C : ndarray
The generalized singular values of ``A``. These are returned
in decreasing order.
S : ndarray
The generalized singular values of ``B``. These are returned
in increasing order.
X : ndarray
The right generalized singular vectors of ``A`` and ``B``.
U : ndarray
The left generalized singular vectors of ``A``, with
shape ``(m, m)``. This is only returned if
``'u' in extras`` is True.
V : ndarray
The left generalized singular vectors of ``B``, with
shape ``(p, p)``. This is only returned if
``'v' in extras`` is True.
Raises
------
A ValueError is raised if ``A`` and ``B`` do not have the same
number of columns, or if they are not both 2D (1D input arrays
will be promoted).
A RuntimeError is raised if the underlying LAPACK routine fails.
Notes
-----
This routine is intended to be as similar as possible to the
decomposition provided by Matlab and Octave. Note that this is slightly
different from the decomposition as put forth in Golub and Van Loan [1],
and that this routine is thus not directly a wrapper for the underlying
LAPACK routine.
One important difference between this routine and that provided by
Matlab is that this routine returns the singular values in decreasing
order, for consistency with NumPy's ``svd`` routine.
References
----------
[1] Golub, G., and C.F. Van Loan, 2013, Matrix Computations, 4th Ed.
'''
# The LAPACK routine stores R inside A and/or B, so we copy to
# avoid modifying the caller's arrays.
dtype = np.complex128 if any(map(np.iscomplexobj, (A, B))) else np.double
Ac = np.array(A, copy=True, dtype=dtype, order='C', ndmin=2)
Bc = np.array(B, copy=True, dtype=dtype, order='C', ndmin=2)
m, n = Ac.shape
p = Bc.shape[0]
if (n != Bc.shape[1]):
raise ValueError('A and B must have the same number of columns')
# Allocate input arrays to LAPACK routine
compute_uv = tuple(each in extras for each in 'uv')
sizes = (m, p)
U, V = (np.zeros((size, size), dtype=dtype) if compute
else np.zeros((1, 1), dtype=dtype)
for size, compute in zip(sizes, compute_uv))
Q = np.zeros((n, n), dtype=dtype)
C = np.zeros((n,), dtype=np.double)
S = np.zeros((n,), dtype=np.double)
iwork = np.zeros((n,), dtype=np.int32)
# Compute GSVD via LAPACK wrapper, returning the effective rank
k, l = _gsvd.gsvd(Ac, Bc, U, V, Q, C, S, iwork,
compute_uv[0], compute_uv[1])
# Compute X
R = _extract_R(Ac, Bc, k, l)
X = R.dot(Q.T).T
# Sort and sub-sample if needed
rank = k + l
ix = np.argsort(C[:rank])[::-1]
C = C[ix]
S = S[ix]
X[:, :rank] = X[:, ix]
if compute_uv[0]:
U[:, :rank] = U[:, ix]
if compute_uv[1]:
# Handle rank-deficient inputs
if k:
V = np.roll(V, k, axis=1)
V[:, :rank] = V[:, ix]
if not full_matrices:
X = X[:, :rank]
if compute_uv[0]:
U = U[:, :rank]
if compute_uv[1]:
V = V[:, :rank]
outputs = (C, S, X) + tuple(arr for arr, compute in
zip((U, V), compute_uv) if compute)
return outputs
def _extract_R(A, B, k, l):
'''Extract the diagonalized matrix R from A and/or B.
The indexing performed here is taken from the LAPACK routine
help, which can be found here:
``http://www.netlib.org/lapack/explore-html/d1/d7e/group__double_g_esing_gab6c743f531c1b87922eb811cbc3ef645.html#gab6c743f531c1b87922eb811cbc3ef645``
'''
m, n = A.shape
if (m - k - l) >= 0:
R = np.zeros((k+l, n), dtype=A.dtype)
R[:, (n-k-l):] = A[:k+l, n-k-l:n]
else:
R = np.zeros((k + l, k + l), dtype=A.dtype)
R[:m, :] = A
R[m:, m:] = B[(m-k):l, (n+m-k-l):]
return R