numo-linalg-randsvd 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 13231de1b8ebda4466ffc207054984fb294ee73653dcd7c954089e2739d75ea0
4
- data.tar.gz: f09bf78c3de2598053be56ce84083fdd293f3d4609c277ac6642c18a0ff3843f
3
+ metadata.gz: 71312ec59a5898236280d11b100f6b43c7e42c4851e69198dc105d71745aef67
4
+ data.tar.gz: d95416e7779f463e7017d119e701fd232280d2da98767b4c58940b0582174b9f
5
5
  SHA512:
6
- metadata.gz: 5349bfc846c6ac5a52139747db1c8261a2d0d3ba2ac1b2e25b958deaf8cc3283f2786401ba9def1aa8d2210fb39483bc5d1d0d2a90ef6cfd2b1f3e36d58efb87
7
- data.tar.gz: 5add7d6b98a45739ab2c35f2c57e6d92bdcb210fa43327c913b59a3761816a0fca12090adc9fbea795fbad151fb58434c2a80a7b2674ac9ba4c99959efd787d9
6
+ metadata.gz: 2c1b847a52683c315bbea34e4b685d03332719a1b9b198318c41d55990cb7502b67fcec1e33ac4d8ab13fbf34abf92721e0633e3fd06182603658bf39fd60b0e
7
+ data.tar.gz: be658a95443b24be18f82f2895ee49b07133c5ef12355d0d3fcffe49f5b5501af2bead44dde05701df51989e0cd01d41cb427921aec87cd53960b01ba23ac7c6
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ ## [0.2.0] - 2022-11-12
2
+
3
+ **Breaking Changes**
4
+
5
+ - Change to use numo-random gem for generating random matrix.
6
+
1
7
  ## [0.1.1] - 2022-10-23
2
8
 
3
9
  - Fix documentation uri.
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'numo/narray'
4
+ require 'numo/random'
4
5
 
5
6
  # Ruby/Numo (NUmerical MOdules)
6
7
  module Numo
@@ -15,24 +16,25 @@ module Numo
15
16
  # @param t [Integer] The number of iterations for orthogonalization.
16
17
  # @param driver [String] The driver parameter of Numo::Linalg.svd.
17
18
  # @param job [String] The job parameter of Numo::Linalg.svd.
18
- def randsvd(a, k, t = 0, driver: 'svd', job: 'A')
19
+ # @param seed [Integer] The seed of random number generator.
20
+ def randsvd(a, k, t = 0, driver: 'svd', job: 'A', seed: nil)
19
21
  n = a.shape[1]
20
- q = _orthonormal_mat(a, [k + 10, n].min, t)
22
+ q = _orthonormal_mat(a, [k + 10, n].min, t, seed)
21
23
  b = a.dot(q)
22
24
  s, u, vt = Numo::Linalg.svd(b, driver: driver, job: job)
23
25
  vtqt = vt.dot(q.transpose)
24
26
  _truncated_mat(s, u, vtqt, k)
25
27
  end
26
28
 
27
- def _rand_normal(shape, dtype = Numo::DFloat, mu = 0.0, sigma = 1.0)
28
- a = dtype.new(shape).rand
29
- b = dtype.new(shape).rand
30
- ((Numo::NMath.sqrt(Numo::NMath.log(a) * -2.0) * Numo::NMath.sin(b * 2.0 * Math::PI)) * sigma) + mu
29
+ def _rand_normal(shape, dtype = Numo::DFloat, seed = nil, mu = 0.0, sigma = 1.0)
30
+ Numo::Random::Generator.new(seed: seed).normal(
31
+ shape: shape, dtype: dtype.name.split('::')[-1].downcase.to_sym, loc: mu, scale: sigma
32
+ )
31
33
  end
32
34
 
33
- def _orthonormal_mat(a, l, t)
35
+ def _orthonormal_mat(a, l, t, seed)
34
36
  m = a.shape[0]
35
- r = _rand_normal([m, l], a.class)
37
+ r = _rand_normal([m, l], a.class, seed)
36
38
  q, = Numo::Linalg.qr(a.transpose.dot(r), mode: 'economic')
37
39
  t.times do
38
40
  r, = Numo::Linalg.qr(a.dot(q), mode: 'economic')
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: numo-linalg-randsvd
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - yoshoku
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-10-23 00:00:00.000000000 Z
11
+ date: 2022-11-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: numo-linalg
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: 0.9.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: numo-random
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.4.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.4.0
41
55
  description: |
42
56
  Numo::Linalg.randsvd is a module function on Numo::Linalg for
43
57
  truncated singular value decomposition with randomized algorithm.