Forum: Ruby using the GSL library

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
A74096209c78118ff69d50a51129d697?d=identicon&s=25 Jason Lillywhite (jlillywh)
on 2009-05-08 05:59
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
A4a4095ff08bd0fced3c3fddbeac743a?d=identicon&s=25 Cameron McBride (Guest)
on 2009-05-08 07:12
(Received via mailing list)
On Thu, May 7, 2009 at 22:59, Jason Lillywhite
<jason.lillywhite@gmail.com> 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
A74096209c78118ff69d50a51129d697?d=identicon&s=25 Jason Lillywhite (jlillywh)
on 2009-05-08 16:39
]
>
> 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'
A4a4095ff08bd0fced3c3fddbeac743a?d=identicon&s=25 Cameron McBride (Guest)
on 2009-05-08 22:26
(Received via mailing list)
On Fri, May 8, 2009 at 09:39, Jason Lillywhite
<jason.lillywhite@gmail.com> 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
A74096209c78118ff69d50a51129d697?d=identicon&s=25 Jason Lillywhite (jlillywh)
on 2009-05-08 22:46
OK. that makes sense. I will be aware of the multiple external library
issue. Thanks!
This topic is locked and can not be replied to.