# Setting precision in Ruby-GSL calculations?

**URL:** <https://www.ruby-forum.com/t/setting-precision-in-ruby-gsl-calculations/100621>\
**Category:** Ruby\
**Created:** [July 30, 2006, 11:37pm UTC](https://www.ruby-forum.com/t/setting-precision-in-ruby-gsl-calculations/100621 "2006-07-30T23:37:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 30, 2006, 11:37pm UTC](https://www.ruby-forum.com/t/setting-precision-in-ruby-gsl-calculations/100621/1 "2006-07-30T23:37:56Z")

</div>

Dear all,

I’d like to use ruby-gsl for some singular value decompositions, i.e.  
a matrix m should be decomposed into a product of three matrices,

m=u_s_v.transposed,

now,as the documentation says that this algorithm is not implemented  
if the dimensions of m (m1 x m2) are such that m1\<m2, I’ve implemented  
my own version, re-reading some long-forgotten linear algebra book.  
In the process, I need to find the eigenvectors and the eigenvalues of  
m\*m.transposed,  
but these are printed out and apparently also calculated only to three  
decimal digits  
by the command

```
 eigval, eigvec = Eigen::symmv(m*m.transposed).

```

This causes the matrix u, which is constructed from the values eigval,  
to have quite strange values for its determinant (I get values of  
1.06,  
0.96 for u_u.transpose.det, but u is unitary by definition, i.e.,  
u_u.transpose.det=1).

Then, of course, m isn’t remotely equal to u_s_v.transposed …

Is there a way of setting the precision of the eigenvalue and  
eigenvector  
calculations  
any higher ?

Thank you very much,

Best regards,

Axel

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 30, 2006, 11:51pm UTC](https://www.ruby-forum.com/t/setting-precision-in-ruby-gsl-calculations/100621/2 "2006-07-30T23:51:02Z")

</div>

removed\_email\_address@domain.invalid wrote:

> In the process, I need to find the eigenvectors and the eigenvalues of  
> u\*u.transpose.det=1).  
> Best regards,
> 
> Axel

If m = u_s_v.transposed, what does m.transposed equal? Isn’t it  
m.transposed = v_s.transposed_u.transposed?

In other words, can you transpose m … m.transposed has dimensions m2 x  
m1 … and take the SVD of m.transposed, and then recover the SVD of m  
from that?

I’ve forgotten most of my computational linear algebra too. 🙂

By the way, how large are m1 and m2?

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 31, 2006, 1:09am UTC](https://www.ruby-forum.com/t/setting-precision-in-ruby-gsl-calculations/100621/3 "2006-07-31T01:09:08Z")

</div>

Hi,

> I’d like to use ruby-gsl for some singular value decompositions, i.e.  
> Which library do you use? There are two extensions,  
> ruby-gsl and rb-gsl(Ruby/GSL).

```
ruby-gsl http://ruby-gsl.sourceforge.net/
rb-gsl (Ruby/GSL) http://rubyforge.org/projects/rb-gsl/

```

> but these are printed out and apparently also calculated only to three  
> decimal digits  
> If you use Ruby/GSL, this is just because  
> matrices are displayed with the printf format  
> %4.3e, not because of precision. You can get  
> more significant figures by displaying elements  
> as m[0][1].

> by the command
> 
> ```
> eigval, eigvec = Eigen::symmv(m*m.transposed).
> 
> ```
> 
> This causes the matrix u, which is constructed from the values  
> eigval,  
> to have quite strange values for its determinant (I get values of  
> 1.06,  
> 0.96 for u_u.transpose.det, but u is unitary by definition, i.e.,  
> u_u.transpose.det=1).  
> Is it unitary? Isn’t it an orthogonal matrix?

> Then, of course, m isn’t remotely equal to u_s_v.transposed …

The following is rb-gsl(Ruby/GSL) outputs.

irb(main):001:0\> require(“gsl”)  
=\> true  
irb(main):002:0\> m = GSL::Matrix[[10, 5, -10], [2, -11, 10]].trans  
=\> GSL::Matrix  
[ 1.000e+01 2.000e+00  
5.000e+00 -1.100e+01  
-1.000e+01 1.000e+01 ]  
irb(main):003:0\> u, v, s = m.SV\_decomp  
=\> [GSL::Linalg::SV::UMatrix  
[ -2.981e-01 8.944e-01  
-5.963e-01 -4.472e-01  
7.454e-01 5.356e-17 ], GSL::Linalg::SV::VMatrix  
[ -7.071e-01 7.071e-01  
7.071e-01 7.071e-01 ], GSL::Linalg::SV::SingularValues  
[1.897e+01 9.487e+00]]  
irb(main):004:0\> u.trans_u (U is orthogonal)  
=\> GSL::Matrix  
[ 1.000e+00 1.408e-16  
1.408e-16 1.000e+00 ]  
irb(main):005:0\> v_v.trans (V is also orthogonal)  
=\> GSL::Matrix  
[ 1.000e+00 -1.015e-17  
-1.015e-17 1.000e+00 ]  
irb(main):006:0\> u\*Matrix.diagonal(s)\*v.trans  
=\> GSL::Matrix (Reconstruct m)  
[ 1.000e+01 2.000e+00  
5.000e+00 -1.100e+01  
-1.000e+01 1.000e+01 ]

Is this what you expect?

Yoshiki

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 31, 2006, 1:19am UTC](https://www.ruby-forum.com/t/setting-precision-in-ruby-gsl-calculations/100621/4 "2006-07-31T01:19:07Z")

</div>

removed\_email\_address@domain.invalid wrote:

> [snip]

> Is this what you expect?
> 
> Yoshiki  
> R and Ruby/GSL agree … that’s a good sign they’re both right. 🙂
