numo-linalg-randsvd 0.1.0 → 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: 89e0d0d104cdc34ed4b941a4a6dd3460e72a477ea73c9cf3028a5074bd5234b1
4
- data.tar.gz: 621681cad42d247f99993a7db1a4c636f87b1902d2ea4d9d12dc349eb0013c16
3
+ metadata.gz: 71312ec59a5898236280d11b100f6b43c7e42c4851e69198dc105d71745aef67
4
+ data.tar.gz: d95416e7779f463e7017d119e701fd232280d2da98767b4c58940b0582174b9f
5
5
  SHA512:
6
- metadata.gz: 90fb17e4b7865c757e826ca2d8712f58f8e372a97acedd3eb45a03aa1775f4e23e20597218424669a8952350dc4e10504e2459cfdbf86f3d0a02d82d6a2687b0
7
- data.tar.gz: c7202a45e21ac88a52502b1b2af2b616466e452af922472db686b49e01ea1827114312c64a482568c2a9406986096edfc9acfac2a38e32f69ccaa1b77231b348
6
+ metadata.gz: 2c1b847a52683c315bbea34e4b685d03332719a1b9b198318c41d55990cb7502b67fcec1e33ac4d8ab13fbf34abf92721e0633e3fd06182603658bf39fd60b0e
7
+ data.tar.gz: be658a95443b24be18f82f2895ee49b07133c5ef12355d0d3fcffe49f5b5501af2bead44dde05701df51989e0cd01d41cb427921aec87cd53960b01ba23ac7c6
data/CHANGELOG.md CHANGED
@@ -1,3 +1,13 @@
1
+ ## [0.2.0] - 2022-11-12
2
+
3
+ **Breaking Changes**
4
+
5
+ - Change to use numo-random gem for generating random matrix.
6
+
7
+ ## [0.1.1] - 2022-10-23
8
+
9
+ - Fix documentation uri.
10
+
1
11
  ## [0.1.0] - 2022-10-09
2
12
 
3
- - Initial release
13
+ - Initial release.
data/README.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # Numo::Linalg.randsvd
2
2
 
3
+ [![Build Status](https://github.com/yoshoku/numo-linalg-randsvd/actions/workflows/main.yml/badge.svg?branch=main)](https://github.com/yoshoku/numo-linalg-randsvd/actions/workflows/main.yml)
4
+ [![Gem Version](https://badge.fury.io/rb/numo-linalg-randsvd.svg)](https://badge.fury.io/rb/numo-linalg-randsvd)
5
+ [![BSD 3-Clause License](https://img.shields.io/badge/License-BSD%203--Clause-orange.svg)](https://github.com/yoshoku/numo-linalg-randsvd/blob/main/LICENSE.txt)
6
+ [![Documentation](https://img.shields.io/badge/api-reference-blue.svg)](https://yoshoku.github.io/numo-linalg-randsvd/doc/)
7
+
3
8
  Numo::Linalg.randsvd is a module function on Numo::Linalg for truncated singular value decomposition with randomized algorithm.
4
9
  This gem re-implements [RandSVD](https://github.com/yoshoku/randsvd) using [Numo::NArray](https://github.com/ruby-numo/numo-narray) and
5
10
  [Numo::Linalg](https://github.com/ruby-numo/numo-linalg) instead of [NMatrix](https://github.com/SciRuby/nmatrix).
@@ -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.0
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-09 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.
@@ -59,7 +73,7 @@ metadata:
59
73
  homepage_uri: https://github.com/yoshoku/numo-linalg-randsvd
60
74
  source_code_uri: https://github.com/yoshoku/numo-linalg-randsvd
61
75
  changelog_uri: https://github.com/yoshoku/numo-linalg-randsvd/blob/main/CHANGELOG.md
62
- documentation_uri: https://rubydoc.info/gems/numo-linalg-randsvd
76
+ documentation_uri: https://yoshoku.github.io/numo-linalg-randsvd/doc/
63
77
  rubygems_mfa_required: 'true'
64
78
  post_install_message:
65
79
  rdoc_options: []
@@ -76,7 +90,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
76
90
  - !ruby/object:Gem::Version
77
91
  version: '0'
78
92
  requirements: []
79
- rubygems_version: 3.2.33
93
+ rubygems_version: 3.3.7
80
94
  signing_key:
81
95
  specification_version: 4
82
96
  summary: Numo::Linalg.randsvd is a module function on Numo::Linalg for truncated singular