Module: NMatrix::FactorizeLUMethods

Defined in:
lib/nmatrix/math.rb

Overview

Methods for generating permutation matrix from LU factorization results.

Class Method Summary collapse

Class Method Details

.permutation_array_for(pivot_array) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/nmatrix/math.rb', line 51

def permutation_array_for(pivot_array)
  perm_arry = Array.new(pivot_array.size) { |i| i }
  perm_arry.each_index do |i|
    #the pivot indices returned by LAPACK getrf are indexed starting
    #from 1, so we need to subtract 1 here
    perm_arry[i], perm_arry[pivot_array[i]-1] = perm_arry[pivot_array[i]-1], perm_arry[i]
  end

  perm_arry
end

.permutation_matrix_from(pivot_array) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/nmatrix/math.rb', line 42

def permutation_matrix_from(pivot_array)
  perm_arry = permutation_array_for(pivot_array)
  n         = NMatrix.zeros(perm_arry.size, dtype: :byte)

  perm_arry.each_with_index { |e, i| n[e,i] = 1 }

  n
end