linalg
linalg copied to clipboard
Ruby Linear Algebra Library
= Linalg - Ruby Linear Algebra Library
A Fortran-based linear algebra package.
=== Features
Major features:
- Cholesky decomposition
- LU decomposition
- QR decomposition
- Schur decomposition
- Singular value decomposition
- Eigenvalues and eigenvectors of a general matrix
- Minimization by least squares
- Linear equation solving
- Stand-alone LAPACK bindings: call any LAPACK routine from directly from ruby.
Minor features:
- Convenient iterators
- Condition numbers and condition number estimates
- Nullspace, rank, nullity
- Inverse
- Pseudo-inverse
- Determinant
- 2-norm, 1-norm, infinity-norm, Frobenius norm
=== Getting Started
Everything you need to know is in:
- Linalg::DMatrix
- Linalg::Iterators
and this README.
=== Tutorial
$ irb irb(main):000:0> require 'linalg' => true irb(main):000:0> include Linalg => Object
==== Construction
irb(main):000:0> DMatrix[[1,2,3], [4,5,6]] => 1.000000 2.000000 3.000000 4.000000 5.000000 6.000000
irb(main):000:0> DMatrix.rows [[1,2,3], [4,5,6]] => 1.000000 2.000000 3.000000 4.000000 5.000000 6.000000
irb(main):000:0> DMatrix.columns [[1,2,3], [4,5,6]] => 1.000000 4.000000 2.000000 5.000000 3.000000 6.000000
irb(main):000:0> a = DMatrix.new(3, 3) { |i, j| 10*i + j } => 0.000000 1.000000 2.000000 10.000000 11.000000 12.000000 20.000000 21.000000 22.000000
irb(main):000:0> DMatrix.new(3, 3, 99)
=>
99.000000 99.000000 99.000000
99.000000 99.000000 99.000000
99.000000 99.000000 99.000000
irb(main):000:0> DMatrix.diagonal [3,4,5] => 3.000000 0.000000 0.000000 0.000000 4.000000 0.000000 0.000000 0.000000 5.000000
irb(main):000:0> DMatrix.diagonal(4) { |i| i*i } => 0.000000 0.000000 0.000000 0.000000 0.000000 1.000000 0.000000 0.000000 0.000000 0.000000 4.000000 0.000000 0.000000 0.000000 0.000000 9.000000
irb(main):000:0> DMatrix.diagonal(4, 99) => 99.000000 0.000000 0.000000 0.000000 0.000000 99.000000 0.000000 0.000000 0.000000 0.000000 99.000000 0.000000 0.000000 0.000000 0.000000 99.000000
==== Indexing
Indexing is in (row, column) order. This is the convention for Mathematics and Fortran. It is opposite from C convention.
irb(main):000:0> a => 0.000000 1.000000 2.000000 10.000000 11.000000 12.000000 20.000000 21.000000 22.000000
irb(main):000:0> a[1,0] => 10.0 irb(main):000:0> a[2,0] => 20.0 irb(main):000:0> a[0,1] => 1.0 irb(main):000:0> a[0,2] => 2.0
Index boundaries are strongly enforced
irb(main):000:0> a[-1,0]
IndexError: out of range
from (irb):27:in `[]'
from (irb):27
==== Enumerables
There are several abstract Enumerables which you may obtain from a matrix: columns, rows, elements, and diagonal elements.
irb(main):000:0> a => 0.000000 1.000000 2.000000 10.000000 11.000000 12.000000 20.000000 21.000000 22.000000
irb(main):000:0> a.columns.class => Linalg::Iterators::ColumnEnum irb(main):000:0> cols = a.columns.map { |x| x } => [ 0.000000 10.000000 20.000000 , 1.000000 11.000000 21.000000 , 2.000000 12.000000 22.000000 ] irb(main):000:0> rows = a.rows.map { |x| x } => [ 0.000000 1.000000 2.000000 , 10.000000 11.000000 12.000000 , 20.000000 21.000000 22.000000 ] irb(main):000:0> a.elems.map { |x| x } => [0.0, 10.0, 20.0, 1.0, 11.0, 21.0, 2.0, 12.0, 22.0] irb(main):003:0> a.elems.find_all { |x| x > 10 } => [20.0, 11.0, 21.0, 12.0, 22.0] irb(main):008:0> a.diags.map { |x| x } => [0.0, 11.0, 22.0]
Another method of constructing a matrix is to join rows or columns,
irb(main):000:0> DMatrix.join_columns [cols[0], cols[2]] => 0.000000 2.000000 10.000000 12.000000 20.000000 22.000000
irb(main):000:0> DMatrix.join_rows [rows[0], rows[2]] => 0.000000 1.000000 2.000000 20.000000 21.000000 22.000000
==== Enumerable-like Iterators with Index Pairs
A matrix itself is not +Enumerable+, but a select number of +Enumerable+-like methods are provided.
irb(main):000:0> a => 0.000000 1.000000 2.000000 10.000000 11.000000 12.000000 20.000000 21.000000 22.000000
irb(main):000:0> a.each_with_index { |e, i, j| puts "row #{i} column #{j} : #{e}" } ; nil row 0 column 0 : 0.0 row 1 column 0 : 10.0 row 2 column 0 : 20.0 row 0 column 1 : 1.0 row 1 column 1 : 11.0 row 2 column 1 : 21.0 row 0 column 2 : 2.0 row 1 column 2 : 12.0 row 2 column 2 : 22.0 => nil irb(main):000:0> a.map_with_index { |e, i, j| eij } => 0.000000 0.000000 0.000000 0.000000 11.000000 24.000000 0.000000 42.000000 88.000000
irb(main):000:0> a.each_upper_with_index { |e, i, j| puts "a[#{i}, #{j}] : #{e}" } ; nil a[0, 1] : 1.0 a[0, 2] : 2.0 a[1, 2] : 12.0 => nil irb(main):000:0> a.each_lower_with_index { |e, i, j| puts "a[#{i}, #{j}] : #{e}" } ; nil a[1, 0] : 10.0 a[2, 0] : 20.0 a[2, 1] : 21.0 => nil
==== Epsilon Comparison
For good and bad, a default epsilon of 1e-8 is provided for comparison, nullspace identification, and symmetric testing.
You can change +default_epsilon+ class-wide or on a per-object basis, or simply pass an explicit epsilon to any of these methods.
irb(main):000:0> a = DMatrix.rand(3, 3) => 0.824730 0.305527 0.044433 -0.582865 -0.351364 -0.752941 0.103417 -0.254290 0.216312
irb(main):000:0> b = a.map { |e| e + 0.000001 } => 0.824731 0.305528 0.044434 -0.582864 -0.351363 -0.752940 0.103418 -0.254289 0.216313
irb(main):000:0> a.within(1e-4, b) => true irb(main):000:0> a.class.default_epsilon => 1.0e-08 irb(main):000:0> a =~ b => false irb(main):000:0> a.singleton_class.default_epsilon => nil irb(main):000:0> a.singleton_class.default_epsilon = 0.0001 => 0.0001 irb(main):000:0> a =~ b => true irb(main):000:0> b =~ a => false
singleton_class.epsilon has first preference over class.epsilon.
==== Singular Value Decomposition
irb(main):000:0> a = DMatrix.rand(4, 7) ; irb(main):000:0* u, s, vt = a.singular_value_decomposition => [ -0.747003 0.304315 -0.144972 -0.573029 -0.435034 -0.814506 0.381951 0.037926 0.207010 -0.490811 -0.777727 -0.333753 -0.458125 0.055467 -0.477741 0.747535 , 2.186983 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 1.719562 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 1.474243 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.676138 0.000000 0.000000 0.000000 , 0.276463 -0.345917 0.573929 -0.416910 -0.139648 0.526564 0.062694 -0.838456 -0.430716 0.199266 0.170144 0.087954 0.077829 0.170372 -0.022382 -0.403705 -0.054483 -0.077530 -0.042506 -0.160232 -0.894461 -0.079171 0.476099 0.396614 0.253131 0.573392 0.315335 -0.342737 0.034285 -0.212892 -0.265934 -0.537325 0.749204 -0.111692 0.142407 -0.377150 0.344083 -0.443538 -0.422834 -0.235075 0.530130 -0.165989 -0.265280 0.376113 0.450755 -0.509553 -0.160031 -0.545940 -0.041002 ] irb(main):000:0> usvt => -0.854950 0.241548 -0.975366 0.688628 0.061092 -0.907441 0.310691 0.896671 0.717254 -0.845643 0.121186 0.000445 -0.692125 -0.810721 0.876331 0.562343 0.064623 -0.300574 -0.218113 0.285261 0.987489 -0.381214 0.830467 -0.317184 0.616481 0.468055 -0.247913 0.410178
irb(main):000:0> a => -0.854950 0.241548 -0.975366 0.688628 0.061092 -0.907441 0.310691 0.896671 0.717254 -0.845643 0.121186 0.000445 -0.692125 -0.810721 0.876331 0.562343 0.064623 -0.300574 -0.218113 0.285261 0.987489 -0.381214 0.830467 -0.317184 0.616481 0.468055 -0.247913 0.410178
irb(main):000:0> u*u.t => 1.000000 0.000000 -0.000000 0.000000 0.000000 1.000000 0.000000 0.000000 -0.000000 0.000000 1.000000 0.000000 0.000000 0.000000 0.000000 1.000000
irb(main):000:0> vt.t*vt => 1.000000 0.000000 0.000000 0.000000 0.000000 0.000000 -0.000000 0.000000 1.000000 -0.000000 -0.000000 0.000000 -0.000000 0.000000 0.000000 -0.000000 1.000000 -0.000000 -0.000000 0.000000 0.000000 0.000000 -0.000000 -0.000000 1.000000 -0.000000 -0.000000 0.000000 0.000000 0.000000 -0.000000 -0.000000 1.000000 -0.000000 0.000000 0.000000 -0.000000 0.000000 -0.000000 -0.000000 1.000000 -0.000000 -0.000000 0.000000 0.000000 0.000000 0.000000 -0.000000 1.000000
==== Eigenvectors and Eigenvalues
irb(main):000:0> a = DMatrix.rand(5, 5) => -0.319566 0.633985 0.335298 -0.150403 0.758559 -0.633389 0.444269 0.375873 0.521107 0.247966 0.757654 0.504831 0.160970 -0.241885 -0.949746 -0.174517 0.351239 -0.600079 -0.533921 0.851118 -0.736717 0.006612 -0.941311 -0.417801 0.555841
irb(main):000:0> eigs, re, im = a.eigensystem
=> [
-0.232169 -0.540550 -0.215201 -0.216959 0.036677
-0.449048 -0.031420 -0.114366 -0.228818 0.062841
0.682632 0.283575 0.019432 0.540907 0.073854
0.364843 -0.597905 0.000000 -0.592036 0.000000
0.381257 -0.207285 -0.407649 -0.490737 -0.076896
,
-1.088525
-0.093563
-0.093563
0.791621
0.791621
,
0.000000
0.604163
-0.604163
0.158934
-0.158934
]
irb(main):000:0> a*eigs.column(0)
=>
0.252722
0.488799
-0.743062
-0.397141
-0.415008
irb(main):000:0> re[0]*eigs.column(0) => 0.252722 0.488799 -0.743062 -0.397141 -0.415008
==== QR factorization
irb(main):000:0> a = DMatrix.rand(4, 7) ; irb(main):000:0* q, r = a.qr => [ -0.593983 0.263367 -0.572195 -0.500414 -0.486715 -0.780258 0.331047 -0.211458 -0.184137 -0.328403 -0.586889 0.716803 -0.613503 0.462588 0.467507 0.437109 , -1.180464 -0.295897 -0.478813 -0.300434 0.360094 0.738799 -0.571687 0.000000 0.873800 -0.167873 0.612545 0.462280 0.097813 -0.733398 0.000000 0.000000 1.659791 -0.439355 -0.234388 -0.063566 0.149709 0.000000 0.000000 0.000000 -0.047795 0.805122 -0.158261 0.754104 ] irb(main):000:0> q*r => 0.701176 0.405888 -0.709530 0.615091 -0.360919 -0.297505 -0.316607 0.574550 -0.537772 0.913498 -0.467057 -0.783803 -0.423482 0.740588 0.217367 -0.232473 -0.830816 0.077752 0.496553 -0.244298 0.798800 0.724218 0.585743 0.992061 0.241379 0.235274 -0.506903 0.411086
irb(main):000:0> a => 0.701176 0.405888 -0.709530 0.615091 -0.360919 -0.297505 -0.316607 0.574550 -0.537772 0.913498 -0.467057 -0.783803 -0.423482 0.740588 0.217367 -0.232473 -0.830816 0.077752 0.496553 -0.244298 0.798800 0.724218 0.585743 0.992061 0.241379 0.235274 -0.506903 0.411086
irb(main):000:0> q.t*q => 1.000000 0.000000 -0.000000 -0.000000 0.000000 1.000000 0.000000 -0.000000 -0.000000 0.000000 1.000000 0.000000 -0.000000 -0.000000 0.000000 1.000000
See the Linalg::DMatrix documentation for more info. The various tests in test/ are also instructive.
=== Download
- http://rubyforge.org/frs/?group_id=273
=== Repository
- http://github.com/quix/linalg
=== Notes
There are four matrix types: +SMatrix+, +DMatrix+, +CMatrix+, and +ZMatrix+ -- single precision, double precision, single precision complex, and double precision complex, respectively. They are all available with basic functionality, however the more complex routines you see here currently lie only in +DMatrix+.
If you have used +narray+, note that +linalg+ uses the mathematical definition of rank, which is equal to the number of columns only in the case of a nonsingular square matrix.
=== Details
Author:: James M. Lawrence [email protected] Requires:: Ruby 1.8.1 or later License:: Copyright (c) 2004-2008 James M. Lawrence. Released under the MIT license.
=== License
Copyright (c) 2004-2008 James M. Lawrence
If +linalg+ begins to smoke, get away immediately. Seek shelter and cover head.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.