Module: NumRuby::Linalg

Defined in:
lib/numruby/lapack.rb

Class Method Summary collapse

Class Method Details

.cholesky(matrix) ⇒ Object



130
131
132
# File 'lib/numruby/lapack.rb', line 130

def self.cholesky(matrix)

end

.cholesky_solve(matrix) ⇒ Object



134
135
136
# File 'lib/numruby/lapack.rb', line 134

def self.cholesky_solve(matrix)

end

.det(matrix) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/numruby/lapack.rb', line 32

def self.det(matrix)
  if not matrix.is_a?(NMatrix)
    raise("Invalid matrix. Not of type NMatrix.")
  end
  if matrix.dim != 2
    raise("Invalid shape of matrix. Should be 2.")
  end
  if matrix.shape[0] != matrix.shape[1]
    raise("Invalid shape. Expected square matrix.")
  end

  return matrix.det
end

.diagsvd(matrix) ⇒ Object



122
123
124
# File 'lib/numruby/lapack.rb', line 122

def self.diagsvd(matrix)

end

.dot(lha, rha) ⇒ Object



20
21
22
# File 'lib/numruby/lapack.rb', line 20

def self.dot(lha, rha)
  lha.dot(rha)
end

.eigObject



58
59
60
# File 'lib/numruby/lapack.rb', line 58

def self.eig

end

.eighObject



62
63
64
# File 'lib/numruby/lapack.rb', line 62

def self.eigh

end

.eigvalshObject



66
67
68
# File 'lib/numruby/lapack.rb', line 66

def self.eigvalsh

end

.inv(matrix) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/numruby/lapack.rb', line 2

def self.inv(matrix)
  if not matrix.is_a?(NMatrix)
    raise("Invalid matrix. Not of type NMatrix.")
  end
  if matrix.dim != 2
    raise("Invalid shape of matrix. Should be 2.")
  end
  if matrix.shape[0] != matrix.shape[1]
    raise("Invalid shape. Expected square matrix.")
  end
  m, n = matrix.shape

  lu, ipiv = NumRuby::Lapack.getrf(matrix)
  inv_a = NumRuby::Lapack.getri(lu, ipiv)

  return inv_a
end

.kronecker_prodObject



54
55
56
# File 'lib/numruby/lapack.rb', line 54

def self.kronecker_prod

end

.least_squareObject



46
47
48
# File 'lib/numruby/lapack.rb', line 46

def self.least_square

end

.lu(matrix, permute_l: False) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/numruby/lapack.rb', line 73

def self.lu(matrix, permute_l: False)
  if not matrix.is_a?(NMatrix)
    raise("Invalid matrix. Not of type NMatrix.")
  end
  if matrix.dim != 2
    raise("Invalid shape of matrix. Should be 2.")
  end

  lu, ipiv = NumRuby::Linalg.getrf(matrix)

  # TODO: calulate p, l, u
end

.lu_factor(matrix) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/numruby/lapack.rb', line 86

def self.lu_factor(matrix)
  if not matrix.is_a?(NMatrix)
    raise("Invalid matrix. Not of type NMatrix.")
  end
  if matrix.dim != 2
    raise("Invalid shape of matrix. Should be 2.")
  end
  if matrix.shape[0] != matrix.shape[1]
    raise("Invalid shape. Expected square matrix.")
  end

  lu, ipiv = NumRuby::Linalg.getrf(matrix)

  return [lu, ipiv]
end

.lu_solve(lu, ipiv, b, trans: 0) ⇒ Object



102
103
104
105
106
107
108
109
# File 'lib/numruby/lapack.rb', line 102

def self.lu_solve(lu, ipiv, b, trans: 0)
  if lu.shape[0] != b.shape[0]
    raise("Incompatibel dimensions.")
  end

  x = NumRuby::Lapack.getrs(lu, ipiv, b, trans)
  return x
end

.normObject



24
25
26
# File 'lib/numruby/lapack.rb', line 24

def self.norm

end

.orth(matrix) ⇒ Object



126
127
128
# File 'lib/numruby/lapack.rb', line 126

def self.orth(matrix)

end

.pinvObject



50
51
52
# File 'lib/numruby/lapack.rb', line 50

def self.pinv

end

.qr(matrix, mode: "full", pivoting: false) ⇒ Object

Computes QR decomposition of a matrix.

Calculates the decomposition A = Q*R where Q is unitary/orthogonal and R is upper triangular.

Args:

  • matrix, type: NMatrix Matrix to be decomposed
  • mode, type: String Determines what information is to be returned: either both Q and R ('full', default), only R ('r') or both Q and R but computed in economy-size ('economic', see Notes). The final option 'raw' (added in Scipy 0.11) makes the function return two matrices (Q, TAU) in the internal format used by LAPACK.
  • pivoting, type: Boolean Whether or not factorization should include pivoting for rank-revealing qr decomposition. If pivoting, compute the decomposition AP = QR as above, but where P is chosen such that the diagonal of R is non-increasing.


156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/numruby/lapack.rb', line 156

def self.qr(matrix, mode: "full", pivoting: false)
  if not ['full', 'r', 'economic', 'raw'].include?(mode.downcase)
    raise("Invalid mode. Should be one of ['full', 'r', 'economic', 'raw']")
  end
  if not matrix.is_a?(NMatrix)
    raise("Invalid matrix. Not of type NMatrix")
  end
  if matrix.dim != 2
    raise("Invalid shape of matrix. Should be 2.")
  end
  m, n = matrix.shape

  if pivoting == true
    qr, tau, jpvt = NumRuby::Lapack.geqp3(matrix)
    jpvt -= 1
  else
    qr, tau = NumRuby::Lapack.geqrf(matrix)
  end

  # calculate R here for both pivot true & false

  if ['economic', 'raw'].include?(mode.downcase) or m < n
    r = NumRuby.triu(matrix)
  else
    r = NumRuby.triu(matrix[0...n, 0...n])
  end

  if pivoting == true
    rj = r, jpvt
  else
    rj = r
  end

  if mode == 'r'
    return rj
  elsif mode == 'raw'
    return [qr, tau]
  end

  if m < n
    q = NumRuby::Lapack.orgqr(qr[0...m, 0...m], tau)
  elsif mode == 'economic'
    q = NumRuby::Lapack.orgqr(qr, tau)
  else
    # TODO: Implement slice view and set slice
    q = NumRuby::Lapack.orgqr(qr, tau)
  end

  return q, rj
end

.solve(a, b, sym_pos: False, lower: False, assume_a: "gen", transposed: False) ⇒ Object



28
29
30
# File 'lib/numruby/lapack.rb', line 28

def self.solve(a, b, sym_pos: False, lower: False, assume_a: "gen", transposed: False)
  # TODO: implement this and remove NMatrix.solve
end

.svd(matrix) ⇒ Object

Computes the SVD decomposition of matrix. Args:

  • input matrix, type: NMatrix


114
115
116
# File 'lib/numruby/lapack.rb', line 114

def self.svd(matrix)

end

.svdvals(matrix) ⇒ Object



118
119
120
# File 'lib/numruby/lapack.rb', line 118

def self.svdvals(matrix)

end