randsvd 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/randsvd.rb +24 -10
  3. data/lib/randsvd/version.rb +1 -1
  4. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cdd512ffec6e4953cb3560c61965897afd59c0db
4
- data.tar.gz: 334b387b942d876147d86ccc4c1b8f43c98b57af
3
+ metadata.gz: 42e2420ac7552a875700b488d8e4092099ab21d7
4
+ data.tar.gz: b40b493e5c098d4dadf55f8785341994d79a8c9c
5
5
  SHA512:
6
- metadata.gz: f24beb52aad2e94e93020947ee3be599a056e218d3f5f7dd3c5bf955b47b1457846985b27d5020099013e810e3ed0908009925810da8b5eadc128715607547d3
7
- data.tar.gz: 489049a7c2eb0a9eb7e657c66c28a387e5f4ac1f1732e298a71946e2ebcd0770445449274b0fdca81438d05be9c6e92aed4dcc492288448c66dfcd2f7ba58004
6
+ metadata.gz: 27547f5946df8b5de0a635b57539c5d6d8b10fb799f18033555c82e250888631e8e62e18a9fc893efed7342da48d59a5633076918704c1399b1439699669552a
7
+ data.tar.gz: bbfc6b7c32cef81528fa24827ebb841e65c00c92466c5aefd6f07754bb7d680daca68baa74217c15f4d39b86e2c07b53071b7b2c5c20da31b8ef246ab63db2a9
@@ -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 [NMatrix] The m-by-n input matrix to be decomposed.
12
- # @param k [Integer] The number of singular values.
13
- # @param t [Integer] The number of iterations for orthogonalization.
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 [NMatrix] The m-by-n input matrix to be decomposed.
27
- # @param k [Integer] The number of singular values.
28
- # @param t [Integer] The number of iterations for orthogonalization.
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 = NMatrix.random shape
50
- b = NMatrix.random shape
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
 
@@ -2,7 +2,7 @@ class RandSVD
2
2
  module VERSION
3
3
  MAJOR = 0
4
4
  MINOR = 1
5
- TINY = 0
5
+ TINY = 1
6
6
  STRING = [MAJOR, MINOR, TINY].compact.join('.')
7
7
  end
8
8
  end
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.0
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-08-27 00:00:00.000000000 Z
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.11
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