[Matrix] eigenvalues, eigenvectors in Ruby ???

U

Une Bévue

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

for symetrical matrices ?
 
A

Alex Shulgin

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.)
 
A

Axel Etzold

-------- Original-Nachricht --------
Datum: Tue, 20 Nov 2007 19:50:14 +0900
Von: (e-mail address removed)
An: (e-mail address removed)
Betreff: [Matrix] eigenvalues, eigenvectors in Ruby ???
is there algo allready implemented to find eigenvalues, eigenvectors in
Ruby ???

for symetrical matrices ?

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
 
M

M. Edward (Ed) Borasky

Axel said:
-------- Original-Nachricht --------
Datum: Tue, 20 Nov 2007 19:50:14 +0900
Von: (e-mail address removed)
An: (e-mail address removed)
Betreff: [Matrix] eigenvalues, eigenvectors in Ruby ???
is there algo allready implemented to find eigenvalues, eigenvectors in
Ruby ???

for symetrical matrices ?

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
 
C

Cameron McBride

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

for symetrical matrices ?

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
 
A

Axel Etzold

-------- Original-Nachricht --------
Datum: Wed, 21 Nov 2007 00:47:45 +0900
Von: "Cameron McBride" <[email protected]>
An: (e-mail address removed)
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
 
U

Une Bévue

Axel Etzold said:
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 ...)

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 !!!
 
U

Une Bévue

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

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

Cameron McBride

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 ...)

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
 
A

Axel Etzold

-------- Original-Nachricht --------
Datum: Wed, 21 Nov 2007 04:35:00 +0900
Von: (e-mail address removed)
An: (e-mail address removed)
Betreff: Re: [Matrix] eigenvalues, eigenvectors in Ruby ???
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.
http://en.wikipedia.org/wiki/Permutation_matrix#Examples

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
 
M

M. Edward (Ed) Borasky

Axel said:
-------- Original-Nachricht --------
Datum: Wed, 21 Nov 2007 00:47:45 +0900
Von: "Cameron McBride" <[email protected]>
An: (e-mail address removed)
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. :)
 
U

Une Bévue

Axel Etzold said:
Have a look at the example on the Wikipedia article.
http://en.wikipedia.org/wiki/Permutation_matrix#Examples

YES, that's the one i've followed...
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]]

i see, now why permutation isn't a matrix like that :

0 0 1 0
0 0 0 1
1 0 0 0
0 1 0 0

but rather a vector indicating the permutations to be done...

thanks a lot !
 
A

Axel Etzold

-------- Original-Nachricht --------
Datum: Wed, 21 Nov 2007 18:00:04 +0900
Von: (e-mail address removed)
An: (e-mail address removed)
Betreff: Re: [Matrix] eigenvalues, eigenvectors in Ruby ???
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

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
 
A

Axel Etzold

-------- Original-Nachricht --------
Datum: Wed, 21 Nov 2007 09:25:09 +0900
Von: "M. Edward (Ed) Borasky" <[email protected]>
An: (e-mail address removed)
Betreff: Re: [Matrix] eigenvalues, eigenvectors in Ruby ???
Axel said:
-------- Original-Nachricht --------
Datum: Wed, 21 Nov 2007 00:47:45 +0900
Von: "Cameron McBride" <[email protected]>
An: (e-mail address removed)
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. :)

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
 
E

Edwin Van leeuwen

M. Edward (Ed) Borasky said:
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. :)

You can talk to maxima quite easily, because maxima can start as a
server/daemon, allowing you to just send commands to it via a socket
(and read the results).

Edwin
 
U

Une Bévue

Cameron McBride said:
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 ??
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top