Page 1 of 1

complex numbers in ruby

Posted: Thu Jun 02, 2016 9:38 pm
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?

Re: complex numbers in ruby

Posted: Thu Jun 02, 2016 10:07 pm
by tulamide
All methods and behaviour of complex numbers explained in detail here:

http://ruby-doc.org/core-1.9.3/Complex.html

Re: complex numbers in ruby

Posted: Thu Jun 02, 2016 10:28 pm
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}

Re: complex numbers in ruby

Posted: Fri Jun 03, 2016 8:32 pm
by tester
Thanks Tronic!