# Using the GSL library

**URL:** <https://www.ruby-forum.com/t/using-the-gsl-library/167474>\
**Category:** Ruby\
**Created:** [May 8, 2009, 5:59am UTC](https://www.ruby-forum.com/t/using-the-gsl-library/167474 "2009-05-08T05:59:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![leveille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@leveille](https://www.ruby-forum.com/u/leveille)\
**Post date:** [May 8, 2009, 5:59am UTC](https://www.ruby-forum.com/t/using-the-gsl-library/167474/1 "2009-05-08T05:59:09Z")

</div>

I’m trying to get started with the gsl-ruby library - thought I’d start  
with calculating mean of an array. I’m following the example on  
[http://rb-gsl.rubyforge.org/stats.html](http://rb-gsl.rubyforge.org/stats.html). But I run into this:

require(“gsl”)  
=\> true  
irb(main):002:0\> v = Vector[1…7]  
NameError: uninitialized constant Vector from (irb):2

Isn’t Vector in the core?.. so I did this

irb(main):003:0\> require ‘matrix’  
=\> true  
irb(main):004:0\> v = Vector[1…7]  
=\> Vector[1…7]

irb(main):005:0\> v.mean  
NoMethodError: undefined method `mean’ for Vector[1…7]:Vector  
from (irb):5

It appears my GSL is not installed or something…? I would really  
appreciate some guidance. Thank you.

I have installed libgsl-ruby1.8, libgsl0ldbl, libgsl-ruby, and gsl-bin

---

<div class="post-metadata">

**Author:** ![leveille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@leveille](https://www.ruby-forum.com/u/leveille)\
**Post date:** [May 8, 2009, 7:12am UTC](https://www.ruby-forum.com/t/using-the-gsl-library/167474/2 "2009-05-08T07:12:27Z")

</div>

On Thu, May 7, 2009 at 22:59, Jason L.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m trying to get started with the gsl-ruby library - thought I’d start  
> with calculating mean of an array. I’m following the example on  
> [http://rb-gsl.rubyforge.org/stats.html](http://rb-gsl.rubyforge.org/stats.html). But I run into this:
> 
> require(“gsl”)  
> =\> true  
> irb(main):002:0\> v = Vector[1…7]  
> NameError: uninitialized constant Vector from (irb):2

I forget exactly when it happened, but the GSL namespace isn’t loaded  
by default in the newer versions of the library. This is a good  
thing.

Your example will work if you type:  
v = GSL::Vector[1…7]

if you really want the short versions, just type  
include GSL

> Isn’t Vector in the core?.. so I did this
> 
> irb(main):003:0\> require ‘matrix’  
> =\> true  
> irb(main):004:0\> v = Vector[1…7]  
> =\> Vector[1…7]

be careful here. What you just did was load the std library ‘matrix’  
library, which also defined a Vector constructor - but no method  
called ‘mean’ This is very different than GSL.

Cameron

---

<div class="post-metadata">

**Author:** ![leveille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@leveille](https://www.ruby-forum.com/u/leveille)\
**Post date:** [May 8, 2009, 4:39pm UTC](https://www.ruby-forum.com/t/using-the-gsl-library/167474/3 "2009-05-08T16:39:54Z")

</div>

]

> be careful here. What you just did was load the std library ‘matrix’  
> library, which also defined a Vector constructor - but no method  
> called ‘mean’ This is very different than GSL.

Thank you.

I see that GSL methods must take only GSL::Vectors as arguments rather  
than ‘matrix’ Vectors:

irb(main):032:0\> a = [1,5,7,2]  
=\> [1, 5, 7, 2]  
irb(main):033:0\> v1 = GSL::Vector.alloc(a)  
=\> GSL::Vector  
[1.000e+00 5.000e+00 7.000e+00 2.000e+00]  
irb(main):034:0\> v2 = Vector.elements(a)  
=\> Vector[1, 5, 7, 2]  
irb(main):035:0\> GSL::Stats::mean(v1)  
=\> 3.75  
irb(main):036:0\> GSL::Stats::mean(v2)  
TypeError: wrong argument type Vector  
from (irb):36:in `mean’  
from (irb):36

It seems a little odd that we have two kinds of vectors but they are  
both a ‘vector’

---

<div class="post-metadata">

**Author:** ![leveille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@leveille](https://www.ruby-forum.com/u/leveille)\
**Post date:** [May 8, 2009, 10:46pm UTC](https://www.ruby-forum.com/t/using-the-gsl-library/167474/4 "2009-05-08T22:46:36Z")

</div>

OK. that makes sense. I will be aware of the multiple external library  
issue. Thanks!

---

<div class="post-metadata">

**Author:** ![leveille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@leveille](https://www.ruby-forum.com/u/leveille)\
**Post date:** [May 8, 2009, 10:26pm UTC](https://www.ruby-forum.com/t/using-the-gsl-library/167474/5 "2009-05-08T22:26:07Z")

</div>

On Fri, May 8, 2009 at 09:39, Jason L.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > be careful here. What you just did was load the std library ‘matrix’  
> > library, which also defined a Vector constructor - but no method  
> > called ‘mean’ This is very different than GSL.

> It seems a little odd that we have two kinds of vectors but they are  
> both a ‘vector’

not really. “vector” in this case is just a name corresponding to a  
variable (objects of a class in two independent and non-compatible  
libraries). Both libraries provide an object meant to represent a  
mathematical “vector”, so their names make sense in both cases. Both  
are “add ons” to the language itself, which is why I cautioned you on  
their use. Be careful when one loads external libraries, there is no  
guarantee that different libraries will co-exist peacefully.

The latter point is why I mentioned GSL::Vector being “a good thing”  
than “include GSL; Vector”. GSL is now wrapped in a “namespace”  
(module in rubyspeak) so it doesn’t cause confusion with the default  
namespace.

It’s a subtle point when getting started, but a critical piece to keep  
in mind.

Cameron
