# Vectors & Surds in Ruby

I was wondering if anyone knows how to use the Vectors class in ruby? I
have looked at the documentation and it is not very clear on how to use
it. I am also looking for a way to express surds in Ruby. I am doing
this for a research project and I need to inform you that this will be
used within my folio. If you disagree with this please do specify
against this.

Hi,

I think you’ll have to be more specific about both questions.

What exactly do you not understand regarding the Vector class? The
documentation looks pretty good to me:

What do you mean by “expressing surds”? Are you talking about an exact
represantation of n-th roots as opposed to floating point
approximations? I’m sure there’s a library for that. Or simply write

Jan E. wrote in post #1087890:

Hi,

I think you’ll have to be more specific about both questions.

What exactly do you not understand regarding the Vector class? The
documentation looks pretty good to me:
Class: Vector (Ruby 1.9.3)

What do you mean by “expressing surds”? Are you talking about an exact
represantation of n-th roots as opposed to floating point
approximations? I’m sure there’s a library for that. Or simply write

Thanks, for the reply. With the Vector class, I am unsure how to set the
vector a value, when I try it in irb i am given the error “NameError:
uninitialized constant Vector”. Also with the expressing surds, I want
to take a float value and then express in in it’s exact value eg 1.73 as
√3.

Jan E. wrote in post #1087901:

Unless there are other specifications that you didn’t tell us, this is
nonsensical.

Thanks for that, I wasn’t aware that the “require matrix” was necessary.
As for the surds, I am finding the square root of a set of values, and
want the answer as an exact value, rather than a float. My code is
below:

begin
puts “Do you want the length of Vector A or Vector B?”

len_vect_A = Math.sqrt(vect_Ax ** 2 + vect_Ay ** 2)
puts “The length of vector A is #{len_vect_A.round(3)}”

else
len_vect_B = Math.sqrt(vect_Bx ** 2 + vect_By ** 2)
puts “The length of vector B is #{len_vect_B.round(3)}”
end

Nuggety N. wrote in post #1087895:

Thanks, for the reply. With the Vector class, I am unsure how to set the
vector a value, when I try it in irb i am given the error “NameError:
uninitialized constant Vector”.

The Vector class is part of the standard library, so you need to include
the files:

require ‘matrix’

Nuggety N. wrote in post #1087895:

Also with the expressing surds, I want
to take a float value and then express in in it’s exact value eg 1.73 as
√3.

This obviously makes no sense. An inexact value is an inexact value,
there’s no way to get back the original number, because you simply don’t
know which one it is. For example, all surds above the maximum floating
point number map to this same number.

Unless there are other specifications that you didn’t tell us, this is
nonsensical.

Nuggety N. wrote in post #1087904:

As for the surds, I am finding the square root of a set of values, and
want the answer as an exact value, rather than a float.

Then you have to use symbolic calculations with custom square root
objects. Something like the Rational class for doing exact calculations
with rational numbers. You cannot use floating point methods like
Math.sqrt().

See if you find a library, I’m pretty sure this has already been done
before. If you do a lot of advanced math, it might also be a good idea
to outsource the calculations to a computer algebra system like Axiom
and use Ruby only for getting the user input and displaying the result:

On 12/05/2012 05:15 PM, Admin T. wrote:

To the OP:

If you want to do a lot of math with vectors and surds (symbolic
computation), I think for now you are better off using Python rather
than Ruby. I am sorry to say this.

Regards,

Bill

For numerics there are several packages with fast Vector/Matrix
implementation with lots of features

1. NArray : narray | RubyGems.org | your community gem host
2. bindings to the GNU scientific library: gsl | RubyGems.org | your community gem host

hth
ralf

To the OP:

If you want to do a lot of math with vectors and surds (symbolic
computation), I think for now you are better off using Python rather
than Ruby. I am sorry to say this.

Regards,

Bill

There’s also NMatrix: GitHub - SciRuby/nmatrix: Dense and sparse linear algebra library for Ruby via SciRuby. It’s being
actively developed.

2012年12月5日水曜日 Ralf M. [email protected]:

Bill

Carlos A. wrote in post #1087959:

There’s also NMatrix: GitHub - SciRuby/nmatrix: Dense and sparse linear algebra library for Ruby via SciRuby. It’s being
actively developed.

2012年12月5日水曜日 Ralf M. [email protected]:

Bill

Thank you for all who point to the vector/matrix capabilities in Ruby.
But for now, even SciRuby says that “Ruby has for some time had no
equivalent to the beautifully constructed NumPy, SciPy, and matplotlib
libraries for Python.”

For vector, NMatrix is in second alpha stage. Depending on the OP’s
need, probably NArray will be enough.

But what is Ruby capability in terms of symbolic computation?

Regards,

Bill