randsvd 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/randsvd.rb +24 -10
- data/lib/randsvd/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 42e2420ac7552a875700b488d8e4092099ab21d7
|
4
|
+
data.tar.gz: b40b493e5c098d4dadf55f8785341994d79a8c9c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 27547f5946df8b5de0a635b57539c5d6d8b10fb799f18033555c82e250888631e8e62e18a9fc893efed7342da48d59a5633076918704c1399b1439699669552a
|
7
|
+
data.tar.gz: bbfc6b7c32cef81528fa24827ebb841e65c00c92466c5aefd6f07754bb7d680daca68baa74217c15f4d39b86e2c07b53071b7b2c5c20da31b8ef246ab63db2a9
|
data/lib/randsvd.rb
CHANGED
@@ -5,33 +5,41 @@ require 'nmatrix/lapacke'
|
|
5
5
|
# using a randomized algorithm.
|
6
6
|
class RandSVD
|
7
7
|
class << self
|
8
|
+
attr_reader :seed
|
9
|
+
|
8
10
|
# Compute the randomized singular value decompostion
|
9
11
|
# using NMatrix::LAPACK.gesvd method.
|
10
12
|
#
|
11
|
-
# @param mat
|
12
|
-
# @param k
|
13
|
-
# @param t
|
13
|
+
# @param mat [NMatrix] The m-by-n input matrix to be decomposed.
|
14
|
+
# @param k [Integer] The number of singular values.
|
15
|
+
# @param t [Integer] The number of iterations for orthogonalization.
|
16
|
+
# @param seed [Integer] The random seed used to generate the random matrix.
|
14
17
|
#
|
15
18
|
# @return [Array<NMatrix>]
|
16
19
|
# Returns array containing the m-by-k matrix of left-singular vectors,
|
17
20
|
# the k-by-1 matrix containing the singular values in decreasing order,
|
18
21
|
# and the k-by-n transposed matrix of right-singular vectors.
|
19
|
-
def gesvd(mat, k, t = 0)
|
22
|
+
def gesvd(mat, k, t = 0, seed = nil)
|
23
|
+
seed ||= srand
|
24
|
+
@seed = seed
|
20
25
|
rsvd(mat, k, t, 0)
|
21
26
|
end
|
22
27
|
|
23
28
|
# Compute the randomized singular value decompostion
|
24
29
|
# using NMatrix::LAPACK.gesdd method.
|
25
30
|
#
|
26
|
-
# @param mat
|
27
|
-
# @param k
|
28
|
-
# @param t
|
31
|
+
# @param mat [NMatrix] The m-by-n input matrix to be decomposed.
|
32
|
+
# @param k [Integer] The number of singular values.
|
33
|
+
# @param t [Integer] The number of iterations for orthogonalization.
|
34
|
+
# @param seed [Integer] The random seed used to generate the random matrix.
|
29
35
|
#
|
30
36
|
# @return [Array<NMatrix>]
|
31
37
|
# Returns array containing the m-by-k matrix of left-singular vectors,
|
32
38
|
# the k-by-1 matrix containing the singular values in decreasing order,
|
33
39
|
# and the k-by-n transposed matrix of right-singular vectors.
|
34
|
-
def gesdd(mat, k, t = 0)
|
40
|
+
def gesdd(mat, k, t = 0, seed = nil)
|
41
|
+
seed ||= srand
|
42
|
+
@seed = seed
|
35
43
|
rsvd(mat, k, t, 1)
|
36
44
|
end
|
37
45
|
|
@@ -45,9 +53,15 @@ class RandSVD
|
|
45
53
|
truncate_svd_mats(mat_u, vec_s, mat_q.dot(mat_vt.transpose), k)
|
46
54
|
end
|
47
55
|
|
56
|
+
def rand_uniform(shape)
|
57
|
+
rng = Random.new(@seed)
|
58
|
+
rnd_vals = Array.new(NMatrix.size(shape)) { rng.rand }
|
59
|
+
NMatrix.new(shape, rnd_vals, dtype: :float64, stype: :dense)
|
60
|
+
end
|
61
|
+
|
48
62
|
def rand_normal(shape, mu = 0.0, sigma = 1.0)
|
49
|
-
a =
|
50
|
-
b =
|
63
|
+
a = rand_uniform(shape)
|
64
|
+
b = rand_uniform(shape)
|
51
65
|
((a.log * -2.0).sqrt * (b * 2.0 * Math::PI).sin) * sigma + mu
|
52
66
|
end
|
53
67
|
|
data/lib/randsvd/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: randsvd
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- yoshoku
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-09-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -122,7 +122,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
122
122
|
version: '0'
|
123
123
|
requirements: []
|
124
124
|
rubyforge_project:
|
125
|
-
rubygems_version: 2.6.
|
125
|
+
rubygems_version: 2.6.13
|
126
126
|
signing_key:
|
127
127
|
specification_version: 4
|
128
128
|
summary: RandSVD is a class that performs truncated singular value decomposition using
|