[Matrix] eigenvalues, eigenvectors in Ruby?

is there algo allready implemented to find eigenvalues, eigenvectors in
Ruby ???

for symetrical matrices ?

···

--
Une Bévue

Une Bév
ue wrote:

is there algo allready implemented to find eigenvalues, eigenvectors in
Ruby ???

for symetrical matrices ?

You can use rb-gsl to do that. http://rb-gsl.rubyforge.org/eigen.html

It is a binding to the gsl library (written in c).

Edwin

···

--
Posted via http://www.ruby-forum.com/\.

Take a look at RNum: http://rnum.rubyforge.org/

Ruby Numerical Library is a linear algebra package using Blas and
Lapack (the highly regarded scientific libraries originally written in
FORTRAN.)

···

On Nov 20, 12:49 pm, unbewusst.s...@weltanschauung.com.invalid (Une Bévue) wrote:

is there algo allready implemented to find eigenvalues, eigenvectors in
Ruby ???

for symetrical matrices ?

--
Alex

-------- Original-Nachricht --------

Datum: Tue, 20 Nov 2007 19:50:14 +0900
Von: unbewusst.sein@weltanschauung.com.invalid
An: ruby-talk@ruby-lang.org
Betreff: [Matrix] eigenvalues, eigenvectors in Ruby ???

is there algo allready implemented to find eigenvalues, eigenvectors in
Ruby ???

for symetrical matrices ?
--
Une Bévue

As far as I know, there is nothing like that in the "matrix.rb" Matrix
class, but you can find this information by using Ruby bindings to GSL

http://rb-gsl.rubyforge.org/

or, alternatively, via the bindings to the R statistical language,

http://raa.ruby-lang.org/project/rsruby/

Best regards,

Axel

···

--
Psssst! Schon vom neuen GMX MultiMessenger gehört?
Der kann`s mit allen: http://www.gmx.net/de/go/multimessenger

Like several have already said, if you're doing numerical work - you
should use something besides the matrix lib in the stdlib.

For yet another working suggestion, NArray has an extension to do
eigenvalues and eigenvectors based on lapack.

  http://narray.rubyforge.org/
  (ext is called na_geev, it's also available on that page)

It's worked for me.

Cameron

···

On 11/20/07, Une Bévue <unbewusst.sein@weltanschauung.com.invalid> wrote:

is there algo allready implemented to find eigenvalues, eigenvectors in
Ruby ???

for symetrical matrices ?

Axel Etzold wrote:

-------- Original-Nachricht --------

Datum: Tue, 20 Nov 2007 19:50:14 +0900
Von: unbewusst.sein@weltanschauung.com.invalid
An: ruby-talk@ruby-lang.org
Betreff: [Matrix] eigenvalues, eigenvectors in Ruby ???

is there algo allready implemented to find eigenvalues, eigenvectors in
Ruby ???

for symetrical matrices ?
--
Une Bévue

As far as I know, there is nothing like that in the "matrix.rb" Matrix
class, but you can find this information by using Ruby bindings to GSL

http://rb-gsl.rubyforge.org/

or, alternatively, via the bindings to the R statistical language,

http://raa.ruby-lang.org/project/rsruby/

Best regards,

Axel

One of the Summer of Code projects extends Matrix to do most of the common matrix operations. Try

http://rubyforge.org/projects/matrix

ok, fine thanks, to both !

···

Alex Shulgin <alex.shulgin@gmail.com> wrote:

On Nov 20, 12:49 pm, unbewusst.s...@weltanschauung.com.invalid (Une > Bévue) wrote:
> is there algo allready implemented to find eigenvalues, eigenvectors in
> Ruby ???
>
> for symetrical matrices ?

Take a look at RNum: http://rnum.rubyforge.org/

Ruby Numerical Library is a linear algebra package using Blas and
Lapack (the highly regarded scientific libraries originally written in
FORTRAN.)

--
Alex

--
Une Bévue

OK, fine thanks, i've a lot a orking solution right now...

···

Cameron McBride <cameron.mcbride@gmail.com> wrote:

For yet another working suggestion, NArray has an extension to do
eigenvalues and eigenvectors based on lapack.

--
Une Bévue

That's kind of neat, I missed it. Thanks.

Do you know right off how its accuracy compares to GSL/LAPACK routines?

Cameron

···

On 11/20/07, M. Edward (Ed) Borasky <znmeb@cesmail.net> wrote:

One of the Summer of Code projects extends Matrix to do most of the
common matrix operations. Try

http://rubyforge.org/projects/matrix

I've downloadd it, how do I install it ?
I've found this only file (except docs) :
/opt/local/lib/ruby/1.8/matrix.rb

···

M. Edward (Ed) Borasky <znmeb@cesmail.net> wrote:

One of the Summer of Code projects extends Matrix to do most of the
common matrix operations. Try

http://rubyforge.org/projects/matrix

--
Une Bévue

-------- Original-Nachricht --------

Datum: Wed, 21 Nov 2007 00:47:45 +0900
Von: "Cameron McBride" <cameron.mcbride@gmail.com>
An: ruby-talk@ruby-lang.org
Betreff: Re: [Matrix] eigenvalues, eigenvectors in Ruby ???

> One of the Summer of Code projects extends Matrix to do most of the
> common matrix operations. Try
>
> http://rubyforge.org/projects/matrix

Dear Ed and Cameron,

That's kind of neat, I missed it. Thanks.

Me too. Very nice.

Do you know right off how its accuracy compares to GSL/LAPACK routines?

Am I wrong in thinking that you can use that library without being
forced to calculate in Floats, i.e. can one use Rationals, Sqrts,
continued fractions, whatever as matrix entries ?

Then, its accuracy should only be limited by the space available on
the machine.

Best regards,

Axel

···

On 11/20/07, M. Edward (Ed) Borasky <znmeb@cesmail.net> wrote:

--
Psssst! Schon vom neuen GMX MultiMessenger gehört?
Der kann`s mit allen: http://www.gmx.net/de/go/multimessenger

-------- Original-Nachricht --------

Datum: Wed, 21 Nov 2007 18:00:04 +0900
Von: unbewusst.sein@weltanschauung.com.invalid
An: ruby-talk@ruby-lang.org
Betreff: Re: [Matrix] eigenvalues, eigenvectors in Ruby ???

>
> One of the Summer of Code projects extends Matrix to do most of the
> common matrix operations. Try
>
> http://rubyforge.org/projects/matrix

I've downloadd it, how do I install it ?
I've found this only file (except docs) :

/opt/local/lib/ruby/1.8/matrix.rb

--
Une Bévue

You should just require it : require "matrix".
Since there is already a matrix.rb in the standard library,
make sure you require the right one - an alternative would
be to rename the matrix.rb file with the extended functionality and keep the old.

Best regards,

Axel

···

M. Edward (Ed) Borasky <znmeb@cesmail.net> wrote:

--
Der GMX SmartSurfer hilft bis zu 70% Ihrer Onlinekosten zu sparen!
Ideal für Modem und ISDN: GMX Handytarife 2024 | jetzt wechseln & sparen

I don't know, however i found a discreapency between GSL and
extendedMatrix for eigenvectors :

require 'extendmatrix.rb'

a = Matrix[ [ 0, 1, 1, 0 ],
            [ 1, 0, 1, 0 ],
            [ 1, 1, 0, 1 ],
            [ 0, 0, 1, 0 ] ]
            
p a.det
# => 2
p a.trace
# => 0
p a.eigenvaluesJacobi
# Vector[-1.0, 2.17008648662603, -1.48119430409202, 0.311107817465982]
# GSL : a.eigval = [ 2.170e+00 3.111e-01 -1.000e+00 -1.481e+00 ]
p a.cJacobiV
=begin
Matrix[[0.707106781186547, 0.522720725656538, -0.302028136647909,
-0.368160355880552],
      [-0.707106781186547, 0.522720725656538, -0.302028136647909,
-0.368160355880552],
      [0.0, 0.611628457346727, 0.749390492326316, 0.253622791118195],
      [0.0, 0.281845198827065, -0.505936655478633, 0.815224744804294]]
GSL : a.eigvec =
    [ 5.227e-01 3.682e-01 -7.071e-01 3.020e-01
       5.227e-01 3.682e-01 7.071e-01 3.020e-01
       6.116e-01 -2.536e-01 -9.158e-16 -7.494e-01
       2.818e-01 -8.152e-01 4.852e-16 5.059e-01 ]
=end

the eigenvalues are "the same" however eigenvectors doesn't have even
the same sign to within a multiplicative constant ???

what did i miss this morning ?
a cofee cup ??

···

Cameron McBride <cameron.mcbride@gmail.com> wrote:

Do you know right off how its accuracy compares to GSL/LAPACK routines?

--
Une Bévue

yes i had this prob with gsl, my matrices (coming from chemistry then
having only integer numbers...) print out with -0.000e+00 for 0 and
1.000e+00 for 1 ...)

I'll try it asap ( http://rubyforge.org/projects/matrix ) because the
readme says :

The project consists of some enhancements to the Ruby "Matrix" module
and includes: LU and QR (Householder, Givens, Gram Schmidt, Hessenberg)
decompositions, bidiagonalization, eigenvalue and eigenvector
-----------------------------------^^^^^^^^^^^----^^^^^^^^^^^
calculations.

i need also permutation matrices, whose i didn't understood in gsl...

anyway, thanks a lot !!!

···

Axel Etzold <AEtzold@gmx.de> wrote:

Am I wrong in thinking that you can use that library without being
forced to calculate in Floats, i.e. can one use Rationals, Sqrts,
continued fractions, whatever as matrix entries ?

--
Une Bévue

Axel Etzold wrote:

-------- Original-Nachricht --------

Datum: Wed, 21 Nov 2007 00:47:45 +0900
Von: "Cameron McBride" <cameron.mcbride@gmail.com>
An: ruby-talk@ruby-lang.org
Betreff: Re: [Matrix] eigenvalues, eigenvectors in Ruby ???

One of the Summer of Code projects extends Matrix to do most of the
common matrix operations. Try

http://rubyforge.org/projects/matrix

Dear Ed and Cameron,

That's kind of neat, I missed it. Thanks.

Me too. Very nice.

Do you know right off how its accuracy compares to GSL/LAPACK routines?

Am I wrong in thinking that you can use that library without being forced to calculate in Floats, i.e. can one use Rationals, Sqrts,
continued fractions, whatever as matrix entries ?

Then, its accuracy should only be limited by the space available on
the machine.

Best regards,

Axel

Yes, one of the design goals was to support Rational arithmetic. But unless the matrix is poorly-conditioned, you shouldn't have to do so, and for real symmetric eigensolutions, the only problem with using floating arithmetic would be if you have some pathologically close eigenvalues you need to separate.

I don't know about continued fractions ... I don't think that's in there. If you want to do that, you'll need something like Maxima, Axiom or Yacas. Or a special-purpose exact linear algebra library.

Anybody up to do a Ruby binding to Yacas? There's an R binding to it, so it can't be too difficult. There are also Java hooks, so this may be yet another JRuby killer app. :slight_smile:

···

On 11/20/07, M. Edward (Ed) Borasky <znmeb@cesmail.net> wrote:

-------- Original-Nachricht --------

Datum: Thu, 22 Nov 2007 17:05:09 +0900
Von: unbewusst.sein@weltanschauung.com.invalid
An: ruby-talk@ruby-lang.org
Betreff: eigenvector discrepency was (Re: [Matrix] eigenvalues, eigenvectors in Ruby ???)

Eigenvectors form an orthogonal basis of a vector space. As a basis
is just a maximal set of linear independent vectors, there
are infinitely many of them ... the only condition is that they
span the eigenspace corresponding to the eigenvalue.
So, if the eigenvalue equations matrix*vector=eigenvalue*vector
hold, I'd not be concerned.

> Do you know right off how its accuracy compares to GSL/LAPACK routines?

That statement refers to inaccuracies in numerical
calculations introduced due to the fact that Float or Double precision
numbers cannot exactly represent certain numbers (this
actually holds true for any number that is not a 'very' finite
sum of powers of 2 and (1/2) - think of
1/10, sqrt(2),sqrt(3), pi.
It has nothing to do with differences in eigenvectors, even though
an entry in them might probably be 1/sqrt(2), rather than 0.7172,
as a numerical software might claim.
Now, as we know thanks to Ed Borasky, in Ruby, you have two choices:

a.) use bindings to GSL or R or Lapack or ... , and thus ultimately to do calculations in a fast language like C or Fortran, which
uses Double precision, and gives a fast, yet not quite exact result.
That's what you would do almost exclusively. People who teach numerics
at universities will tell you you can't do otherwise (i.e., that's
their translation of the English "Matlab,
Octave etc. can't do otherwise unless you rewrite almost everything
- once for being able to use Rationals, and then again, to be able
to use continued fractions - because you learned about them only
two months later ....")

b.) use a pure Ruby, object-oriented implementation of numerical
algorithms as in the extended matrix.rb suggested by Ed Borasky.
That will give you exact calculations using Rationals right away,
and exact results of expressions like sqrt(2)+sqrt(3) via a
(yet to be written addition method for a class of continued fractions,
see Arithmetic with Continued Fractions, as the expressions
for sqrt(2) and sqrt(3) lead to periodic continued fractions, i.e.,
representations involving finite information).
The calculations to be done are far more involved than adding two Floats,
and, additionally, Ruby is slower than C or Fortran, but it can be done,
using Ruby, in very finite coding time.

In most situations, sticking with a.) is ok., but if you really
wanted b.), no numerical software will do it for you, unless you rewrite
a substantial part of the code ... in Ruby, you just define
another class and some methods to it.

Best regards,

Axel

···

Cameron McBride <cameron.mcbride@gmail.com> wrote:

I don't know, however i found a discreapency between GSL and
extendedMatrix for eigenvectors :

require 'extendmatrix.rb'

a = Matrix[ [ 0, 1, 1, 0 ],
            [ 1, 0, 1, 0 ],
            [ 1, 1, 0, 1 ],
            [ 0, 0, 1, 0 ] ]
            
p a.det
# => 2
p a.trace
# => 0
p a.eigenvaluesJacobi
# Vector[-1.0, 2.17008648662603, -1.48119430409202, 0.311107817465982]
# GSL : a.eigval = [ 2.170e+00 3.111e-01 -1.000e+00 -1.481e+00 ]
p a.cJacobiV
=begin
Matrix[[0.707106781186547, 0.522720725656538, -0.302028136647909,
-0.368160355880552],
      [-0.707106781186547, 0.522720725656538, -0.302028136647909,
-0.368160355880552],
      [0.0, 0.611628457346727, 0.749390492326316, 0.253622791118195],
      [0.0, 0.281845198827065, -0.505936655478633, 0.815224744804294]]
GSL : a.eigvec =
    [ 5.227e-01 3.682e-01 -7.071e-01 3.020e-01
       5.227e-01 3.682e-01 7.071e-01 3.020e-01
       6.116e-01 -2.536e-01 -9.158e-16 -7.494e-01
       2.818e-01 -8.152e-01 4.852e-16 5.059e-01 ]
=end

the eigenvalues are "the same" however eigenvectors doesn't have even
the same sign to within a multiplicative constant ???

what did i miss this morning ?
a cofee cup ??
--
Une Bévue

--
Ist Ihr Browser Vista-kompatibel? Jetzt die neuesten
Browser-Versionen downloaden: GMX Browser - verwenden Sie immer einen aktuellen Browser. Kostenloser Download.

Une Bévue wrote:

Do you know right off how its accuracy compares to GSL/LAPACK routines?

I don't know, however i found a discreapency between GSL and
extendedMatrix for eigenvectors :

require 'extendmatrix.rb'

a = Matrix[ [ 0, 1, 1, 0 ], [ 1, 0, 1, 0 ],
            [ 1, 1, 0, 1 ],
            [ 0, 0, 1, 0 ] ]
            p a.det
# => 2
p a.trace
# => 0
p a.eigenvaluesJacobi
# Vector[-1.0, 2.17008648662603, -1.48119430409202, 0.311107817465982]
# GSL : a.eigval = [ 2.170e+00 3.111e-01 -1.000e+00 -1.481e+00 ]
p a.cJacobiV
=begin
Matrix[[0.707106781186547, 0.522720725656538, -0.302028136647909,
-0.368160355880552], [-0.707106781186547, 0.522720725656538, -0.302028136647909,
-0.368160355880552], [0.0, 0.611628457346727, 0.749390492326316, 0.253622791118195], [0.0, 0.281845198827065, -0.505936655478633, 0.815224744804294]]
GSL : a.eigvec = [ 5.227e-01 3.682e-01 -7.071e-01 3.020e-01 5.227e-01 3.682e-01 7.071e-01 3.020e-01 6.116e-01 -2.536e-01 -9.158e-16 -7.494e-01 2.818e-01 -8.152e-01 4.852e-16 5.059e-01 ]
=end

the eigenvalues are "the same" however eigenvectors doesn't have even
the same sign to within a multiplicative constant ???

what did i miss this morning ?
a cofee cup ??

An eigenvector represents a direction in N-dimensional space. Two different packages will not necessarily report eigenvectors with the same sign as a result. As someone else posted, eigenvalues and eigenvectors are the solutions of the equations

Matrix*eigenvector = eigenvalue*eigenvector,

so the way to test an eigensolution is to evaluate the above equations to see if they are satisfied within, say, 10 - 15 decimal places.

···

Cameron McBride <cameron.mcbride@gmail.com> wrote:

To be clear, the printing accuracy isn't the machine accuracy. The
-0.000e+00 is just the specific
format that the GSL::Matrix object uses for printing.

Also, the default matrix in GSL is a double (float) based one. You
can create integer matricies. Look at the manual.

Cameron

···

On Nov 20, 2007 2:35 PM, Une Bévue <unbewusst.sein@weltanschauung.com.invalid> wrote:

> Am I wrong in thinking that you can use that library without being
> forced to calculate in Floats, i.e. can one use Rationals, Sqrts,
> continued fractions, whatever as matrix entries ?

yes i had this prob with gsl, my matrices (coming from chemistry then
having only integer numbers...) print out with -0.000e+00 for 0 and
1.000e+00 for 1 ...)

-------- Original-Nachricht --------

Datum: Wed, 21 Nov 2007 04:35:00 +0900
Von: unbewusst.sein@weltanschauung.com.invalid
An: ruby-talk@ruby-lang.org
Betreff: Re: [Matrix] eigenvalues, eigenvectors in Ruby ???

>
> Am I wrong in thinking that you can use that library without being
> forced to calculate in Floats, i.e. can one use Rationals, Sqrts,
> continued fractions, whatever as matrix entries ?

yes i had this prob with gsl, my matrices (coming from chemistry then
having only integer numbers...) print out with -0.000e+00 for 0 and
1.000e+00 for 1 ...)

That's because GSL internally calculates with double precision numbers,
which cannot represent every number correctly, as not every number
is a sum of positive and negative powers of 2. But you can always
round to whole numbers ...

I'll try it asap ( http://rubyforge.org/projects/matrix ) because the
readme says :

The project consists of some enhancements to the Ruby "Matrix" module
and includes: LU and QR (Householder, Givens, Gram Schmidt, Hessenberg)
decompositions, bidiagonalization, eigenvalue and eigenvector
-----------------------------------^^^^^^^^^^^----^^^^^^^^^^^
calculations.

i need also permutation matrices, whose i didn't understood in gsl...

Have a look at the example on the Wikipedia article.

A permutation matrix has exactly one 1 entry in every row, at the
place that the permutation vector indicates (note that Ruby starts
counting at 0, so the permutation vector given in the Wikipedia article
should actually be (0 3 1 4 2)), the remaining entries are 0.
The reason to associate a vector and a matrix like this is that
due to the way multiplication of matrices is defined, multiplying

permutation_matrix*matrix

gives you the column vectors of 'matrix' shuffled as the permutation
vector indicates, i.e.
the matrix (inner arrays=rows)

[[1,1,1,1,1],[2,2,2,2,2],[3,3,3,3,3],[4,4,4,4,4],[5,5,5,5,5]]

would be mapped to

[[1,1,1,1,1],[4,4,4,4,4],[2,2,2,2,2],[5,5,5,5,5],[3,3,3,3,3]]

Best regards,

Axel

···

Axel Etzold <AEtzold@gmx.de> wrote:

--
Ist Ihr Browser Vista-kompatibel? Jetzt die neuesten
Browser-Versionen downloaden: GMX Browser - verwenden Sie immer einen aktuellen Browser. Kostenloser Download.

-------- Original-Nachricht --------

Datum: Wed, 21 Nov 2007 09:25:09 +0900
Von: "M. Edward (Ed) Borasky" <znmeb@cesmail.net>
An: ruby-talk@ruby-lang.org
Betreff: Re: [Matrix] eigenvalues, eigenvectors in Ruby ???

Axel Etzold wrote:
> -------- Original-Nachricht --------
>> Datum: Wed, 21 Nov 2007 00:47:45 +0900
>> Von: "Cameron McBride" <cameron.mcbride@gmail.com>
>> An: ruby-talk@ruby-lang.org
>> Betreff: Re: [Matrix] eigenvalues, eigenvectors in Ruby ???
>
>>> One of the Summer of Code projects extends Matrix to do most of the
>>> common matrix operations. Try
>>>
>>> http://rubyforge.org/projects/matrix
>
> Dear Ed and Cameron,
>
>> That's kind of neat, I missed it. Thanks.
>>
>
> Me too. Very nice.
>
>> Do you know right off how its accuracy compares to GSL/LAPACK routines?
>
> Am I wrong in thinking that you can use that library without being
> forced to calculate in Floats, i.e. can one use Rationals, Sqrts,
> continued fractions, whatever as matrix entries ?
>
> Then, its accuracy should only be limited by the space available on
> the machine.
>
> Best regards,
>
> Axel

Yes, one of the design goals was to support Rational arithmetic. But
unless the matrix is poorly-conditioned, you shouldn't have to do so,
and for real symmetric eigensolutions, the only problem with using
floating arithmetic would be if you have some pathologically close
eigenvalues you need to separate.

I don't know about continued fractions ... I don't think that's in
there. If you want to do that, you'll need something like Maxima, Axiom
or Yacas. Or a special-purpose exact linear algebra library.

Anybody up to do a Ruby binding to Yacas? There's an R binding to it, so
it can't be too difficult. There are also Java hooks, so this may be yet
another JRuby killer app. :slight_smile:

Dear Ed,

it's very good that one doesn't have to do every calculation in
Float or Double .. of course, Rationals and Symbolic computations can
become very complex, but if one needs to do it,
it's nice to know that Ruby has an option that can help.

Best regards,

Axel

···

>> On 11/20/07, M. Edward (Ed) Borasky <znmeb@cesmail.net> wrote:

--
Ist Ihr Browser Vista-kompatibel? Jetzt die neuesten
Browser-Versionen downloaden: GMX Browser - verwenden Sie immer einen aktuellen Browser. Kostenloser Download.