lib/tensor_stream/evaluator/operation_helpers/array_ops_helper.rb
# general case transposition with flat arrays
general case transposition with flat arrays
general case transposition with flat arrays
def transpose_with_perm(arr, new_arr, shape, new_shape, perm)
      arr_size = shape.reduce(:*)
      divisors = shape.dup.drop(1).reverse.inject([1]) do |a, s|
        a << s * a.last
      end.reverse
      multipliers = new_shape.dup.drop(1).reverse.inject([1]) do |a, s|
        a << s * a.last
      end.reverse
      arr_size.times do |p|
        ptr = p
        index = []
        divisors.each_with_object(index) do |div, a|
          a << (ptr / div.to_f).floor
          ptr = ptr % div
        end
        remaped = perm.map { |x| index[x] }
        ptr2 = 0
        multipliers.each_with_index do |m, idx|
          ptr2 += remaped[idx] * m
        end
        new_arr[ptr2] = arr[p]
      end
      [new_arr, new_shape]
    end
def transpose_with_perm ( arr , new_arr , shape , new_shape , perm ) arr_size = shape . reduce ( :* ) divisors = shape . dup . drop ( 1 ) . reverse . inject ( [ 1 ] ) do | a , s | a << s * a . last end . reverse multipliers = new_shape . dup . drop ( 1 ) . reverse . inject ( [ 1 ] ) do | a , s | a << s * a . last end . reverse arr_size . times do | p | ptr = p index = [ ] divisors . each_with_object ( index ) do | div , a | a << ( ptr / div . to_f ) . floor ptr = ptr % div end remaped = perm . map { | x | index [ x ] } ptr2 = 0 multipliers . each_with_index do | m , idx | ptr2 += remaped [ idx ] * m end new_arr [ ptr2 ] = arr [ p ] end [ new_arr , new_shape ] end
general case transposition with flat arrays
general case transposition with flat arrays
# remap based on perm
arr
<not_specific>
new_arr
<not_specific>
shape
<not_specific>
new_shape
<not_specific>
perm
<not_specific>