Using the GSL library


#1

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


#2

On Thu, May 7, 2009 at 22:59, Jason L.
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. 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


#3

]

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’


#4

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


#5

On Fri, May 8, 2009 at 09:39, Jason L.
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