Quantcast
Channel: Active questions tagged ruby - Stack Overflow
Viewing all articles
Browse latest Browse all 4607

Original variable changes outside the recursive function's scope even though I duplicated it

$
0
0

I come from a python and C++ background. The original variable is real_array_slice and real_slice.Input array: [2.799999952316284, -0.40000003576278687, -0.40000003576278687, -0.3999999761581421, -0.3999999761581421, -0.3999999761581421, -0.4000000059604645, -0.40000003576278687][2.799999952316284, -0.40000003576278687, -0.40000003576278687, -0.3999999761581421, -0.3999999761581421, -0.3999999761581421, -0.4000000059604645, -0.40000003576278687]

Function is called and prints the correct input array:

print(real_array_slice)real_array,imag_array = fft(real_array_slice,imag_array_slice,sign)

Inside that function the printed array changes due to recursive calls, but this should be impossible because the variable is being duplicated:

def fft(real_slice = [],imag_slice = [],sign)  console.clear  print real_slice  evens_real, odds_real = fft_recursion(real_slice.dup,sign)  evens_imag, odds_imag = fft_recursion(imag_slice.dup,sign)

For further investigation, the recursive function:

def fft_recursion(slice,sign)    return slice if slice.size <= 1    evens_odds = slice.partition.with_index{|_,i| i.even?}    if sign == -1 #FFT    evens, odds = evens_odds.map{|even_odd| fft(even_odd, sign)*2}    else #IFFT    evens, odds = evens_odds.map{|even_odd| fft(even_odd,sign)}    endend

I tried keeping the recursive function and the main function together, but the same issue occurred.The array should stay the same outside the recursive function calls.


Viewing all articles
Browse latest Browse all 4607

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>