Forum: Ruby Vectors & Surds in Ruby

Posted by Nuggety Nanna (lewis_r)
on 2012-12-05 11:07
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.
Posted by Jan E. (jacques1)
on 2012-12-05 11:45
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:
http://ruby-doc.org/stdlib-1.9.3/libdoc/matrix/rdo...

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 
your own class: Root[2, 2]
Posted by Nuggety Nanna (lewis_r)
on 2012-12-05 12:00
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:
> http://ruby-doc.org/stdlib-1.9.3/libdoc/matrix/rdo...
>
> 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
> your own class: Root[2, 2]

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.
Posted by Jan E. (jacques1)
on 2012-12-05 12:13
Nuggety Nanna 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 Nanna 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.
Posted by Nuggety Nanna (lewis_r)
on 2012-12-05 12:17
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?"
answer = gets.chomp.downcase
end until answer == "a" || answer == "b"

  if answer == "a"
    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
Posted by Jan E. (jacques1)
on 2012-12-05 12:51
Nuggety Nanna 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:

http://en.wikipedia.org/wiki/Axiom_(computer_algebra_system)
Posted by Admin Tensor (tensor)
on 2012-12-05 17:15
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
Posted by Ralf Mueller (Guest)
on 2012-12-05 17:41
(Received via mailing list)
On 12/05/2012 05:15 PM, Admin Tensor 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 : http://rubygems.org/gems/narray
2. bindings to the GNU scientific library: http://rubygems.org/gems/gsl

hth
ralf
Posted by Carlos Agarie (Guest)
on 2012-12-05 18:46
(Received via mailing list)
There's also NMatrix: http://github.com/sciruby/nmatrix. It's being
actively developed.

2012年12月5日水曜日 Ralf Mueller ralf.mueller@zmaw.de:

>> Bill
>
--
Posted by Admin Tensor (tensor)
on 2012-12-05 19:07
Carlos Agarie wrote in post #1087959:
> There's also NMatrix: http://github.com/sciruby/nmatrix. It's being
> actively developed.
>
> 2012年12月5日水曜日 Ralf Mueller ralf.mueller@zmaw.de:
>
>>> 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
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.