https://github.com/yoshoku/numo-linalg-randsvd
Numo::Linalg.randsvd is a module function on Numo::Linalg for truncated singular value decomposition with randomized algorithm.
https://github.com/yoshoku/numo-linalg-randsvd
gem linear-algebra machine-learning matrix ruby singular-value-decomposition
Last synced: 12 days ago
JSON representation
Numo::Linalg.randsvd is a module function on Numo::Linalg for truncated singular value decomposition with randomized algorithm.
- Host: GitHub
- URL: https://github.com/yoshoku/numo-linalg-randsvd
- Owner: yoshoku
- License: bsd-3-clause
- Created: 2022-09-09T22:55:56.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2025-01-01T12:03:08.000Z (10 months ago)
- Last Synced: 2025-04-18T02:21:43.554Z (6 months ago)
- Topics: gem, linear-algebra, machine-learning, matrix, ruby, singular-value-decomposition
- Language: Ruby
- Homepage: https://rubygems.org/gems/numo-linalg-randsvd
- Size: 85.9 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Numo::Linalg.randsvd
[](https://github.com/yoshoku/numo-linalg-randsvd/actions/workflows/main.yml)
[](https://badge.fury.io/rb/numo-linalg-randsvd)
[](https://github.com/yoshoku/numo-linalg-randsvd/blob/main/LICENSE.txt)
[](https://gemdocs.org/gems/numo-linalg-randsvd/)Numo::Linalg.randsvd is a module function on Numo::Linalg for truncated singular value decomposition with randomized algorithm.
This gem re-implements [RandSVD](https://github.com/yoshoku/randsvd) using [Numo::NArray](https://github.com/ruby-numo/numo-narray) and
[Numo::Linalg](https://github.com/ruby-numo/numo-linalg) instead of [NMatrix](https://github.com/SciRuby/nmatrix).Note: Since v0.4.0, this gem uses [Numo::NArray Alternative](https://github.com/yoshoku/numo-narray-alt) instead of Numo::NArray as a dependency.
References:
- 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.
- 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.## Installation
Add this line to your application's Gemfile:
```ruby
gem 'numo-linalg-randsvd'
```And then execute:
$ bundle install
Or install it yourself as:
$ gem install numo-linalg-randsvd
## Usage
```ruby
require 'numo/linalg/randsvd'# An example of matrix decomposition is as follows:
x = Numo::DFloat.new(100, 20).rand
y = Numo::DFloat.new(20, 50).rand
z = x.dot(y)
p z
# Numo::DFloat#shape=[100,50]
# ...# Performing the randomized singular value decomposition with specified the number of singular values.
n_singular_values = 20
s, u, vt = Numo::Linalg.randsvd(z, n_singular_values)
p s
# Numo::DFloat#shape=[20]
# ...
p u
# Numo::DFloat#shape=[100,20]
# ...
p vt
# Numo::DFloat#shape=[20,50]
# ...# Reconstructing the matrix with the singular values and singular vectors.
zz = u.dot(s.diag).dot(vt)
p zz
# Numo::DFloat#shape=[100,50]
# ...
p (z - zz).abs.max
# 5.5067062021407764e-14
```## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/yoshoku/numo-linalg-randsvd.
This project is intended to be a safe, welcoming space for collaboration,
and contributors are expected to adhere to the [code of conduct](https://github.com/yoshoku/numo-linalg-randsvd/blob/main/CODE_OF_CONDUCT.md).## License
The gem is available as open source under the terms of the [BSD-3-Clause License](https://opensource.org/licenses/BSD-3-Clause)