numo-linalg-randsvd 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 89e0d0d104cdc34ed4b941a4a6dd3460e72a477ea73c9cf3028a5074bd5234b1
4
+ data.tar.gz: 621681cad42d247f99993a7db1a4c636f87b1902d2ea4d9d12dc349eb0013c16
5
+ SHA512:
6
+ metadata.gz: 90fb17e4b7865c757e826ca2d8712f58f8e372a97acedd3eb45a03aa1775f4e23e20597218424669a8952350dc4e10504e2459cfdbf86f3d0a02d82d6a2687b0
7
+ data.tar.gz: c7202a45e21ac88a52502b1b2af2b616466e452af922472db686b49e01ea1827114312c64a482568c2a9406986096edfc9acfac2a38e32f69ccaa1b77231b348
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ## [0.1.0] - 2022-10-09
2
+
3
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at yoshoku@outlook.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/LICENSE.txt ADDED
@@ -0,0 +1,27 @@
1
+ Copyright (c) 2022 Atsushi Tatsuma
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are met:
6
+
7
+ * Redistributions of source code must retain the above copyright notice, this
8
+ list of conditions and the following disclaimer.
9
+
10
+ * Redistributions in binary form must reproduce the above copyright notice,
11
+ this list of conditions and the following disclaimer in the documentation
12
+ and/or other materials provided with the distribution.
13
+
14
+ * Neither the name of the copyright holder nor the names of its
15
+ contributors may be used to endorse or promote products derived from
16
+ this software without specific prior written permission.
17
+
18
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/README.md ADDED
@@ -0,0 +1,72 @@
1
+ # Numo::Linalg.randsvd
2
+
3
+ Numo::Linalg.randsvd is a module function on Numo::Linalg for truncated singular value decomposition with randomized algorithm.
4
+ This gem re-implements [RandSVD](https://github.com/yoshoku/randsvd) using [Numo::NArray](https://github.com/ruby-numo/numo-narray) and
5
+ [Numo::Linalg](https://github.com/ruby-numo/numo-linalg) instead of [NMatrix](https://github.com/SciRuby/nmatrix).
6
+
7
+ References:
8
+
9
+ - P.-G. Martinsson, A. Szlam, M. Tygert, "Normalized power iterations for the computation of SVD," Proc. of NIPS Workshop on Low-Rank Methods for Large-Scale Machine Learning, 2011.
10
+ - P.-G. Martinsson, V. Rokhlin, M. Tygert, "A randomized algorithm for the approximation of matrices," Tech. Rep., 1361, Yale University Department of Computer Science, 2006.
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ ```ruby
17
+ gem 'numo-linalg-randsvd'
18
+ ```
19
+
20
+ And then execute:
21
+
22
+ $ bundle install
23
+
24
+ Or install it yourself as:
25
+
26
+ $ gem install numo-linalg-randsvd
27
+
28
+ ## Usage
29
+
30
+ ```ruby
31
+ require 'numo/linalg/autoloader'
32
+ require 'numo/linalg/randsvd'
33
+
34
+ # An example of matrix decomposition is as follows:
35
+ x = Numo::DFloat.new(100, 20).rand
36
+ y = Numo::DFloat.new(20, 50).rand
37
+ z = x.dot(y)
38
+ p z
39
+ # Numo::DFloat#shape=[100,50]
40
+ # ...
41
+
42
+ # Performing the randomized singular value decomposition with specified the number of singular values.
43
+ n_singular_values = 20
44
+ s, u, vt = Numo::Linalg.randsvd(z, n_singular_values)
45
+ p s
46
+ # Numo::DFloat#shape=[20]
47
+ # ...
48
+ p u
49
+ # Numo::DFloat#shape=[100,20]
50
+ # ...
51
+ p vt
52
+ # Numo::DFloat#shape=[20,50]
53
+ # ...
54
+
55
+ # Reconstructing the matrix with the singular values and singular vectors.
56
+ zz = u.dot(s.diag).dot(vt)
57
+ p zz
58
+ # Numo::DFloat#shape=[100,50]
59
+ # ...
60
+ p (z - zz).abs.max
61
+ # 5.5067062021407764e-14
62
+ ```
63
+
64
+ ## Contributing
65
+
66
+ Bug reports and pull requests are welcome on GitHub at https://github.com/yoshoku/numo-linalg-randsvd.
67
+ This project is intended to be a safe, welcoming space for collaboration,
68
+ and contributors are expected to adhere to the [code of conduct](https://github.com/yoshoku/numo-linalg-randsvd/blob/main/CODE_OF_CONDUCT.md).
69
+
70
+ ## License
71
+
72
+ The gem is available as open source under the terms of the [BSD-3-Clause License](https://opensource.org/licenses/BSD-3-Clause)
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'numo/narray'
4
+
5
+ # Ruby/Numo (NUmerical MOdules)
6
+ module Numo
7
+ # Numo::Linalg : Linear Algebra library with BLAS/LAPACK binding to Numo::NArray
8
+ module Linalg
9
+ module_function
10
+
11
+ # Compute the randomized singular value decompostion.
12
+ #
13
+ # @param a [Numo::NArray] 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 driver [String] The driver parameter of Numo::Linalg.svd.
17
+ # @param job [String] The job parameter of Numo::Linalg.svd.
18
+ def randsvd(a, k, t = 0, driver: 'svd', job: 'A')
19
+ n = a.shape[1]
20
+ q = _orthonormal_mat(a, [k + 10, n].min, t)
21
+ b = a.dot(q)
22
+ s, u, vt = Numo::Linalg.svd(b, driver: driver, job: job)
23
+ vtqt = vt.dot(q.transpose)
24
+ _truncated_mat(s, u, vtqt, k)
25
+ end
26
+
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
31
+ end
32
+
33
+ def _orthonormal_mat(a, l, t)
34
+ m = a.shape[0]
35
+ r = _rand_normal([m, l], a.class)
36
+ q, = Numo::Linalg.qr(a.transpose.dot(r), mode: 'economic')
37
+ t.times do
38
+ r, = Numo::Linalg.qr(a.dot(q), mode: 'economic')
39
+ q, = Numo::Linalg.qr(a.transpose.dot(r), mode: 'economic')
40
+ end
41
+ q
42
+ end
43
+
44
+ def _truncated_mat(s, u, vt, k)
45
+ m = u.shape[0]
46
+ n = vt.shape[1]
47
+ [s[0...k].dup, u[0...m, 0...k].dup, vt[0...k, 0...n].dup]
48
+ end
49
+
50
+ private_class_method :_rand_normal, :_orthonormal_mat, :_truncated_mat
51
+ end
52
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: numo-linalg-randsvd
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - yoshoku
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-10-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: numo-linalg
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 0.1.4
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 0.1.4
27
+ - !ruby/object:Gem::Dependency
28
+ name: numo-narray
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.9.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 0.9.1
41
+ description: |
42
+ Numo::Linalg.randsvd is a module function on Numo::Linalg for
43
+ truncated singular value decomposition with randomized algorithm.
44
+ email:
45
+ - yoshoku@outlook.com
46
+ executables: []
47
+ extensions: []
48
+ extra_rdoc_files: []
49
+ files:
50
+ - CHANGELOG.md
51
+ - CODE_OF_CONDUCT.md
52
+ - LICENSE.txt
53
+ - README.md
54
+ - lib/numo/linalg/randsvd.rb
55
+ homepage: https://github.com/yoshoku/numo-linalg-randsvd
56
+ licenses:
57
+ - BSD-3-Clause
58
+ metadata:
59
+ homepage_uri: https://github.com/yoshoku/numo-linalg-randsvd
60
+ source_code_uri: https://github.com/yoshoku/numo-linalg-randsvd
61
+ changelog_uri: https://github.com/yoshoku/numo-linalg-randsvd/blob/main/CHANGELOG.md
62
+ documentation_uri: https://rubydoc.info/gems/numo-linalg-randsvd
63
+ rubygems_mfa_required: 'true'
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubygems_version: 3.2.33
80
+ signing_key:
81
+ specification_version: 4
82
+ summary: Numo::Linalg.randsvd is a module function on Numo::Linalg for truncated singular
83
+ value decomposition with randomized algorithm.
84
+ test_files: []