complex numbers in ruby

For general discussion related FlowStone
Post Reply
tester
Posts: 1786
Joined: Wed Jan 18, 2012 10:52 pm
Location: Poland, internet

complex numbers in ruby

Post by tester »

input = array

In ruby:

1.
How to convert array into array of complex numbers? (is this handled as one "object"?) I mean - from what I see, ruby is handling complex numbers as single objects, but I don't know to what degree.

2.
How to multiply (and other operations) array of complex numbers through a number and through another array of complex numbers? (items with the same indexes, so resulting array has the same size)) I mean - is it working the same wah as with real numbers?

3.
How to split the number / array of complex numbers into real part on out1 and img part on out2?
Need to take a break? I have something right for you.
Feel free to donate. Thank you for your contribution.
tulamide
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: complex numbers in ruby

Post by tulamide »

All methods and behaviour of complex numbers explained in detail here:

http://ruby-doc.org/core-1.9.3/Complex.html
"There lies the dog buried" (German saying translated literally)
Tronic
Posts: 539
Joined: Wed Dec 21, 2011 12:59 pm

Re: complex numbers in ruby

Post by Tronic »

tester wrote:input = array

In ruby:

1.
How to convert array into array of complex numbers? (is this handled as one "object"?) I mean - from what I see, ruby is handling complex numbers as single objects, but I don't know to what degree.

real = [0.5,2.5,3.8]
imag = [10.5,20.5,30.8]
cplx = Array.new real.size
cplx.each_index {|idx| cplx[idx] = Complex(real[idx],imag[idx]) }
tester wrote:input = array
2.
How to multiply (and other operations) array of complex numbers through a number and through another array of complex numbers? (items with the same indexes, so resulting array has the same size)) I mean - is it working the same wah as with real numbers?

... normal ruby iterator
tester wrote:input = array
3.
How to split the number / array of complex numbers into real part on out1 and img part on out2?

real = cplx.collect{|i| i.real}
imag = cplx.collect{|i| i.imag}
tester
Posts: 1786
Joined: Wed Jan 18, 2012 10:52 pm
Location: Poland, internet

Re: complex numbers in ruby

Post by tester »

Thanks Tronic!
Need to take a break? I have something right for you.
Feel free to donate. Thank you for your contribution.
Post Reply