Class: NuLin::LLS_SVD

Inherits:
LLS
  • Object
show all
Defined in:
lib/nulin/lls.rb

Instance Attribute Summary collapse

Attributes inherited from LLS

#solution

Instance Method Summary collapse

Methods inherited from LLS

#valid_matrix?

Constructor Details

#initialize(a, b, options) ⇒ LLS_SVD

Returns a new instance of LLS_SVD.



98
99
100
101
102
# File 'lib/nulin/lls.rb', line 98

def initialize(a, b, options)
  super
  @rcond = options.fetch(:rcond, -1.0)
  compute
end

Instance Attribute Details

#rankObject (readonly)

The effective rank of the matrix.



108
109
110
# File 'lib/nulin/lls.rb', line 108

def rank
  @rank
end

#singular_valuesObject (readonly)

Singular values of the matrix as NArray. The values are ordered decreasingly.



106
107
108
# File 'lib/nulin/lls.rb', line 106

def singular_values
  @singular_values
end

Instance Method Details

#computeObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/nulin/lls.rb', line 110

def compute
  m, n = @a.shape
  r, nrhs = @b.shape
  k = [m, n].min
  lda = [1, m].max
  ldb = [1, m, n].max
  b = NVector.new(@typecode, ldb, nrhs)
  b[0...m, 0...nrhs] = @b
  s = NArray.new(NuLin.to_real_typecode(@typecode), k)
  lwork = 3*k + [2*k, m, n, nrhs].max + 1
  work = NVector.new(@typecode, lwork)
  if @a.complex?
    rwork = [NArray.new(@typecode, 5*k)]
  else
    rwork = []
  end
  @rank, = NuLin::Native.call(@typecode, "gelss", m, n, nrhs, @a, lda, b, ldb, s,
                              @rcond, 0, work, lwork, *rwork, 0)
  
  @singular_values = s
  @solution = b[0...n, true]
  @solution.flatten! if @b_is_rank1
end