pnmatrix 1.2.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (111) hide show
  1. checksums.yaml +7 -0
  2. data/ext/nmatrix/binary_format.txt +53 -0
  3. data/ext/nmatrix/data/complex.h +388 -0
  4. data/ext/nmatrix/data/data.cpp +274 -0
  5. data/ext/nmatrix/data/data.h +651 -0
  6. data/ext/nmatrix/data/meta.h +64 -0
  7. data/ext/nmatrix/data/ruby_object.h +386 -0
  8. data/ext/nmatrix/extconf.rb +70 -0
  9. data/ext/nmatrix/math/asum.h +99 -0
  10. data/ext/nmatrix/math/cblas_enums.h +36 -0
  11. data/ext/nmatrix/math/cblas_templates_core.h +507 -0
  12. data/ext/nmatrix/math/gemm.h +241 -0
  13. data/ext/nmatrix/math/gemv.h +178 -0
  14. data/ext/nmatrix/math/getrf.h +255 -0
  15. data/ext/nmatrix/math/getrs.h +121 -0
  16. data/ext/nmatrix/math/imax.h +82 -0
  17. data/ext/nmatrix/math/laswp.h +165 -0
  18. data/ext/nmatrix/math/long_dtype.h +62 -0
  19. data/ext/nmatrix/math/magnitude.h +54 -0
  20. data/ext/nmatrix/math/math.h +751 -0
  21. data/ext/nmatrix/math/nrm2.h +165 -0
  22. data/ext/nmatrix/math/rot.h +117 -0
  23. data/ext/nmatrix/math/rotg.h +106 -0
  24. data/ext/nmatrix/math/scal.h +71 -0
  25. data/ext/nmatrix/math/trsm.h +336 -0
  26. data/ext/nmatrix/math/util.h +162 -0
  27. data/ext/nmatrix/math.cpp +1368 -0
  28. data/ext/nmatrix/nm_memory.h +60 -0
  29. data/ext/nmatrix/nmatrix.cpp +285 -0
  30. data/ext/nmatrix/nmatrix.h +476 -0
  31. data/ext/nmatrix/ruby_constants.cpp +151 -0
  32. data/ext/nmatrix/ruby_constants.h +106 -0
  33. data/ext/nmatrix/ruby_nmatrix.c +3130 -0
  34. data/ext/nmatrix/storage/common.cpp +77 -0
  35. data/ext/nmatrix/storage/common.h +183 -0
  36. data/ext/nmatrix/storage/dense/dense.cpp +1096 -0
  37. data/ext/nmatrix/storage/dense/dense.h +129 -0
  38. data/ext/nmatrix/storage/list/list.cpp +1628 -0
  39. data/ext/nmatrix/storage/list/list.h +138 -0
  40. data/ext/nmatrix/storage/storage.cpp +730 -0
  41. data/ext/nmatrix/storage/storage.h +99 -0
  42. data/ext/nmatrix/storage/yale/class.h +1139 -0
  43. data/ext/nmatrix/storage/yale/iterators/base.h +143 -0
  44. data/ext/nmatrix/storage/yale/iterators/iterator.h +131 -0
  45. data/ext/nmatrix/storage/yale/iterators/row.h +450 -0
  46. data/ext/nmatrix/storage/yale/iterators/row_stored.h +140 -0
  47. data/ext/nmatrix/storage/yale/iterators/row_stored_nd.h +169 -0
  48. data/ext/nmatrix/storage/yale/iterators/stored_diagonal.h +124 -0
  49. data/ext/nmatrix/storage/yale/math/transpose.h +110 -0
  50. data/ext/nmatrix/storage/yale/yale.cpp +2074 -0
  51. data/ext/nmatrix/storage/yale/yale.h +203 -0
  52. data/ext/nmatrix/types.h +55 -0
  53. data/ext/nmatrix/util/io.cpp +279 -0
  54. data/ext/nmatrix/util/io.h +115 -0
  55. data/ext/nmatrix/util/sl_list.cpp +627 -0
  56. data/ext/nmatrix/util/sl_list.h +144 -0
  57. data/ext/nmatrix/util/util.h +78 -0
  58. data/lib/nmatrix/blas.rb +378 -0
  59. data/lib/nmatrix/cruby/math.rb +744 -0
  60. data/lib/nmatrix/enumerate.rb +253 -0
  61. data/lib/nmatrix/homogeneous.rb +241 -0
  62. data/lib/nmatrix/io/fortran_format.rb +138 -0
  63. data/lib/nmatrix/io/harwell_boeing.rb +221 -0
  64. data/lib/nmatrix/io/market.rb +263 -0
  65. data/lib/nmatrix/io/point_cloud.rb +189 -0
  66. data/lib/nmatrix/jruby/decomposition.rb +24 -0
  67. data/lib/nmatrix/jruby/enumerable.rb +13 -0
  68. data/lib/nmatrix/jruby/error.rb +4 -0
  69. data/lib/nmatrix/jruby/math.rb +501 -0
  70. data/lib/nmatrix/jruby/nmatrix_java.rb +840 -0
  71. data/lib/nmatrix/jruby/operators.rb +283 -0
  72. data/lib/nmatrix/jruby/slice.rb +264 -0
  73. data/lib/nmatrix/lapack_core.rb +181 -0
  74. data/lib/nmatrix/lapack_plugin.rb +44 -0
  75. data/lib/nmatrix/math.rb +953 -0
  76. data/lib/nmatrix/mkmf.rb +100 -0
  77. data/lib/nmatrix/monkeys.rb +137 -0
  78. data/lib/nmatrix/nmatrix.rb +1172 -0
  79. data/lib/nmatrix/rspec.rb +75 -0
  80. data/lib/nmatrix/shortcuts.rb +1163 -0
  81. data/lib/nmatrix/version.rb +39 -0
  82. data/lib/nmatrix/yale_functions.rb +118 -0
  83. data/lib/nmatrix.rb +28 -0
  84. data/spec/00_nmatrix_spec.rb +892 -0
  85. data/spec/01_enum_spec.rb +196 -0
  86. data/spec/02_slice_spec.rb +407 -0
  87. data/spec/03_nmatrix_monkeys_spec.rb +80 -0
  88. data/spec/2x2_dense_double.mat +0 -0
  89. data/spec/4x4_sparse.mat +0 -0
  90. data/spec/4x5_dense.mat +0 -0
  91. data/spec/blas_spec.rb +215 -0
  92. data/spec/elementwise_spec.rb +311 -0
  93. data/spec/homogeneous_spec.rb +100 -0
  94. data/spec/io/fortran_format_spec.rb +88 -0
  95. data/spec/io/harwell_boeing_spec.rb +98 -0
  96. data/spec/io/test.rua +9 -0
  97. data/spec/io_spec.rb +159 -0
  98. data/spec/lapack_core_spec.rb +482 -0
  99. data/spec/leakcheck.rb +16 -0
  100. data/spec/math_spec.rb +1363 -0
  101. data/spec/nmatrix_yale_resize_test_associations.yaml +2802 -0
  102. data/spec/nmatrix_yale_spec.rb +286 -0
  103. data/spec/rspec_monkeys.rb +56 -0
  104. data/spec/rspec_spec.rb +35 -0
  105. data/spec/shortcuts_spec.rb +474 -0
  106. data/spec/slice_set_spec.rb +162 -0
  107. data/spec/spec_helper.rb +172 -0
  108. data/spec/stat_spec.rb +214 -0
  109. data/spec/test.pcd +20 -0
  110. data/spec/utm5940.mtx +83844 -0
  111. metadata +295 -0
@@ -0,0 +1,4 @@
1
+ DataTypeError = Class.new(StandardError)
2
+ StorageTypeError = Class.new(StandardError)
3
+ ShapeError = Class.new(StandardError)
4
+ NotInvertibleError = Class.new(StandardError)
@@ -0,0 +1,501 @@
1
+ #--
2
+ # = NMatrix
3
+ #
4
+ # A linear algebra library for scientific computation in Ruby.
5
+ # NMatrix is part of SciRuby.
6
+ #
7
+ # NMatrix was originally inspired by and derived from NArray, by
8
+ # Masahiro Tanaka: http://narray.rubyforge.org
9
+ #
10
+ # == Copyright Information
11
+ #
12
+ # SciRuby is Copyright (c) 2010 - 2014, Ruby Science Foundation
13
+ # NMatrix is Copyright (c) 2012 - 2014, John Woods and the Ruby Science Foundation
14
+ #
15
+ # Please see LICENSE.txt for additional copyright notices.
16
+ #
17
+ # == Contributing
18
+ #
19
+ # By contributing source code to SciRuby, you agree to be bound by
20
+ # our Contributor Agreement:
21
+ #
22
+ # * https://github.com/SciRuby/sciruby/wiki/Contributor-Agreement
23
+ #
24
+ # == math.rb
25
+ #
26
+ # Math functionality for NMatrix, along with any NMatrix instance
27
+ # methods that correspond to ATLAS/BLAS/LAPACK functions (e.g.,
28
+ # laswp).
29
+ #++
30
+
31
+ class NMatrix
32
+
33
+ #
34
+ # call-seq:
35
+ # getrf! -> Array
36
+ #
37
+ # LU factorization of a general M-by-N matrix +A+ using partial pivoting with
38
+ # row interchanges. The LU factorization is A = PLU, where P is a row permutation
39
+ # matrix, L is a lower triangular matrix with unit diagonals, and U is an upper
40
+ # triangular matrix (note that this convention is different from the
41
+ # clapack_getrf behavior, but matches the standard LAPACK getrf).
42
+ # +A+ is overwritten with the elements of L and U (the unit
43
+ # diagonal elements of L are not saved). P is not returned directly and must be
44
+ # constructed from the pivot array ipiv. The row indices in ipiv are indexed
45
+ # starting from 1.
46
+ # Only works for dense matrices.
47
+ #
48
+ # * *Returns* :
49
+ # - The IPIV vector. The L and U matrices are stored in A.
50
+ # * *Raises* :
51
+ # - +StorageTypeError+ -> ATLAS functions only work on dense matrices.
52
+ #
53
+ def getrf!
54
+ ipiv = LUDecomposition.new(self.twoDMat).getPivot.to_a
55
+ return ipiv
56
+ end
57
+
58
+ #
59
+ # call-seq:
60
+ # geqrf! -> shape.min x 1 NMatrix
61
+ #
62
+ # QR factorization of a general M-by-N matrix +A+.
63
+ #
64
+ # The QR factorization is A = QR, where Q is orthogonal and R is Upper Triangular
65
+ # +A+ is overwritten with the elements of R and Q with Q being represented by the
66
+ # elements below A's diagonal and an array of scalar factors in the output NMatrix.
67
+ #
68
+ # The matrix Q is represented as a product of elementary reflectors
69
+ # Q = H(1) H(2) . . . H(k), where k = min(m,n).
70
+ #
71
+ # Each H(i) has the form
72
+ #
73
+ # H(i) = I - tau * v * v'
74
+ #
75
+ # http://www.netlib.org/lapack/explore-html/d3/d69/dgeqrf_8f.html
76
+ #
77
+ # Only works for dense matrices.
78
+ #
79
+ # * *Returns* :
80
+ # - Vector TAU. Q and R are stored in A. Q is represented by TAU and A
81
+ # * *Raises* :
82
+ # - +StorageTypeError+ -> LAPACK functions only work on dense matrices.
83
+ #
84
+ def geqrf!
85
+ # The real implementation is in lib/nmatrix/lapacke.rb
86
+ raise(NotImplementedError, "geqrf! requires the nmatrix-lapacke gem")
87
+ end
88
+
89
+ #
90
+ # call-seq:
91
+ # ormqr(tau) -> NMatrix
92
+ # ormqr(tau, side, transpose, c) -> NMatrix
93
+ #
94
+ # Returns the product Q * c or c * Q after a call to geqrf! used in QR factorization.
95
+ # +c+ is overwritten with the elements of the result NMatrix if supplied. Q is the orthogonal matrix
96
+ # represented by tau and the calling NMatrix
97
+ #
98
+ # Only works on float types, use unmqr for complex types.
99
+ #
100
+ # == Arguments
101
+ #
102
+ # * +tau+ - vector containing scalar factors of elementary reflectors
103
+ # * +side+ - direction of multiplication [:left, :right]
104
+ # * +transpose+ - apply Q with or without transpose [false, :transpose]
105
+ # * +c+ - NMatrix multplication argument that is overwritten, no argument assumes c = identity
106
+ #
107
+ # * *Returns* :
108
+ #
109
+ # - Q * c or c * Q Where Q may be transposed before multiplication.
110
+ #
111
+ #
112
+ # * *Raises* :
113
+ # - +StorageTypeError+ -> LAPACK functions only work on dense matrices.
114
+ # - +TypeError+ -> Works only on floating point matrices, use unmqr for complex types
115
+ # - +TypeError+ -> c must have the same dtype as the calling NMatrix
116
+ #
117
+ def ormqr(tau, side=:left, transpose=false, c=nil)
118
+ # The real implementation is in lib/nmatrix/lapacke.rb
119
+ raise(NotImplementedError, "ormqr requires the nmatrix-lapacke gem")
120
+
121
+ end
122
+
123
+ #
124
+ # call-seq:
125
+ # unmqr(tau) -> NMatrix
126
+ # unmqr(tau, side, transpose, c) -> NMatrix
127
+ #
128
+ # Returns the product Q * c or c * Q after a call to geqrf! used in QR factorization.
129
+ # +c+ is overwritten with the elements of the result NMatrix if it is supplied. Q is the orthogonal matrix
130
+ # represented by tau and the calling NMatrix
131
+ #
132
+ # Only works on complex types, use ormqr for float types.
133
+ #
134
+ # == Arguments
135
+ #
136
+ # * +tau+ - vector containing scalar factors of elementary reflectors
137
+ # * +side+ - direction of multiplication [:left, :right]
138
+ # * +transpose+ - apply Q as Q or its complex conjugate [false, :complex_conjugate]
139
+ # * +c+ - NMatrix multplication argument that is overwritten, no argument assumes c = identity
140
+ #
141
+ # * *Returns* :
142
+ #
143
+ # - Q * c or c * Q Where Q may be transformed to its complex conjugate before multiplication.
144
+ #
145
+ #
146
+ # * *Raises* :
147
+ # - +StorageTypeError+ -> LAPACK functions only work on dense matrices.
148
+ # - +TypeError+ -> Works only on floating point matrices, use unmqr for complex types
149
+ # - +TypeError+ -> c must have the same dtype as the calling NMatrix
150
+ #
151
+ def unmqr(tau, side=:left, transpose=false, c=nil)
152
+ # The real implementation is in lib/nmatrix/lapacke.rb
153
+ raise(NotImplementedError, "unmqr requires the nmatrix-lapacke gem")
154
+ end
155
+
156
+ #
157
+ # call-seq:
158
+ # potrf!(upper_or_lower) -> NMatrix
159
+ #
160
+ # Cholesky factorization of a symmetric positive-definite matrix -- or, if complex,
161
+ # a Hermitian positive-definite matrix +A+.
162
+ # The result will be written in either the upper or lower triangular portion of the
163
+ # matrix, depending on whether the argument is +:upper+ or +:lower+.
164
+ # Also the function only reads in the upper or lower part of the matrix,
165
+ # so it doesn't actually have to be symmetric/Hermitian.
166
+ # However, if the matrix (i.e. the symmetric matrix implied by the lower/upper
167
+ # half) is not positive-definite, the function will return nonsense.
168
+ #
169
+ # This functions requires either the nmatrix-atlas or nmatrix-lapacke gem
170
+ # installed.
171
+ #
172
+ # * *Returns* :
173
+ # the triangular portion specified by the parameter
174
+ # * *Raises* :
175
+ # - +StorageTypeError+ -> ATLAS functions only work on dense matrices.
176
+ # - +ShapeError+ -> Must be square.
177
+ # - +NotImplementedError+ -> If called without nmatrix-atlas or nmatrix-lapacke gem
178
+ #
179
+ def potrf!(which)
180
+ # The real implementation is in the plugin files.
181
+ cholesky = CholeskyDecomposition.new(self.twoDMat)
182
+ if which == :upper
183
+ u = create_dummy_nmatrix
184
+ twoDMat = cholesky.getLT
185
+ u.s = ArrayRealVector.new(ArrayGenerator.getArrayDouble(twoDMat.getData, @shape[0], @shape[1]))
186
+ return u
187
+ else
188
+ l = create_dummy_nmatrix
189
+ twoDMat = cholesky.getL
190
+ l.s = ArrayRealVector.new(ArrayGenerator.getArrayDouble(twoDMat.getData, @shape[0], @shape[1]))
191
+ return l
192
+ end
193
+ end
194
+
195
+ def potrf_upper!
196
+ potrf! :upper
197
+ end
198
+
199
+ def potrf_lower!
200
+ potrf! :lower
201
+ end
202
+
203
+
204
+ #
205
+ # call-seq:
206
+ # factorize_cholesky -> [upper NMatrix, lower NMatrix]
207
+ #
208
+ # Calculates the Cholesky factorization of a matrix and returns the
209
+ # upper and lower matrices such that A=LU and L=U*, where * is
210
+ # either the transpose or conjugate transpose.
211
+ #
212
+ # Unlike potrf!, this makes method requires that the original is matrix is
213
+ # symmetric or Hermitian. However, it is still your responsibility to make
214
+ # sure it is positive-definite.
215
+ def factorize_cholesky
216
+ # raise "Matrix must be symmetric/Hermitian for Cholesky factorization" unless self.hermitian?
217
+ cholesky = CholeskyDecomposition.new(self.twoDMat)
218
+ l = create_dummy_nmatrix
219
+ twoDMat = cholesky.getL
220
+ l.s = ArrayRealVector.new(ArrayGenerator.getArrayDouble(twoDMat.getData, @shape[0], @shape[1]))
221
+ u = create_dummy_nmatrix
222
+ twoDMat = cholesky.getLT
223
+ u.s = ArrayRealVector.new(ArrayGenerator.getArrayDouble(twoDMat.getData, @shape[0], @shape[1]))
224
+ return [u,l]
225
+ end
226
+
227
+ #
228
+ # call-seq:
229
+ # factorize_lu -> ...
230
+ #
231
+ # LU factorization of a matrix. Optionally return the permutation matrix.
232
+ # Note that computing the permutation matrix will introduce a slight memory
233
+ # and time overhead.
234
+ #
235
+ # == Arguments
236
+ #
237
+ # +with_permutation_matrix+ - If set to *true* will return the permutation
238
+ # matrix alongwith the LU factorization as a second return value.
239
+ #
240
+ def factorize_lu with_permutation_matrix=nil
241
+ raise(NotImplementedError, "only implemented for dense storage") unless self.stype == :dense
242
+ raise(NotImplementedError, "matrix is not 2-dimensional") unless self.dimensions == 2
243
+ t = self.clone
244
+ pivot = create_dummy_nmatrix
245
+ twoDMat = LUDecomposition.new(self.twoDMat).getP
246
+ pivot.s = ArrayRealVector.new(ArrayGenerator.getArrayDouble(twoDMat.getData, @shape[0], @shape[1]))
247
+ return [t,pivot]
248
+ end
249
+
250
+ #
251
+ # call-seq:
252
+ # factorize_qr -> [Q,R]
253
+ #
254
+ # QR factorization of a matrix without column pivoting.
255
+ # Q is orthogonal and R is upper triangular if input is square or upper trapezoidal if
256
+ # input is rectangular.
257
+ #
258
+ # Only works for dense matrices.
259
+ #
260
+ # * *Returns* :
261
+ # - Array containing Q and R matrices
262
+ #
263
+ # * *Raises* :
264
+ # - +StorageTypeError+ -> only implemented for desnse storage.
265
+ # - +ShapeError+ -> Input must be a 2-dimensional matrix to have a QR decomposition.
266
+ #
267
+ def factorize_qr
268
+
269
+ raise(NotImplementedError, "only implemented for dense storage") unless self.stype == :dense
270
+ raise(ShapeError, "Input must be a 2-dimensional matrix to have a QR decomposition") unless self.dim == 2
271
+ qrdecomp = QRDecomposition.new(self.twoDMat)
272
+
273
+ qmat = create_dummy_nmatrix
274
+ qtwoDMat = qrdecomp.getQ
275
+ qmat.s = ArrayRealVector.new(ArrayGenerator.getArrayDouble(qtwoDMat.getData, @shape[0], @shape[1]))
276
+
277
+ rmat = create_dummy_nmatrix
278
+ rtwoDMat = qrdecomp.getR
279
+ rmat.s = ArrayRealVector.new(ArrayGenerator.getArrayDouble(rtwoDMat.getData, @shape[0], @shape[1]))
280
+ return [qmat,rmat]
281
+
282
+ end
283
+
284
+ # Solve the matrix equation AX = B, where A is +self+, B is the first
285
+ # argument, and X is returned. A must be a nxn square matrix, while B must be
286
+ # nxm. Only works with dense matrices and non-integer, non-object data types.
287
+ #
288
+ # == Arguments
289
+ #
290
+ # * +b+ - the right hand side
291
+ #
292
+ # == Options
293
+ #
294
+ # * +form+ - Signifies the form of the matrix A in the linear system AX=B.
295
+ # If not set then it defaults to +:general+, which uses an LU solver.
296
+ # Other possible values are +:lower_tri+, +:upper_tri+ and +:pos_def+ (alternatively,
297
+ # non-abbreviated symbols +:lower_triangular+, +:upper_triangular+,
298
+ # and +:positive_definite+ can be used.
299
+ # If +:lower_tri+ or +:upper_tri+ is set, then a specialized linear solver for linear
300
+ # systems AX=B with a lower or upper triangular matrix A is used. If +:pos_def+ is chosen,
301
+ # then the linear system is solved via the Cholesky factorization.
302
+ # Note that when +:lower_tri+ or +:upper_tri+ is used, then the algorithm just assumes that
303
+ # all entries in the lower/upper triangle of the matrix are zeros without checking (which
304
+ # can be useful in certain applications).
305
+ #
306
+ #
307
+ # == Usage
308
+ #
309
+ # a = NMatrix.new [2,2], [3,1,1,2], dtype: dtype
310
+ # b = NMatrix.new [2,1], [9,8], dtype: dtype
311
+ # a.solve(b)
312
+ #
313
+ # # solve an upper triangular linear system more efficiently:
314
+ # require 'benchmark'
315
+ # require 'nmatrix/lapacke'
316
+ # rand_mat = NMatrix.random([10000, 10000], dtype: :float64)
317
+ # a = rand_mat.triu
318
+ # b = NMatrix.random([10000, 10], dtype: :float64)
319
+ # Benchmark.bm(10) do |bm|
320
+ # bm.report('general') { a.solve(b) }
321
+ # bm.report('upper_tri') { a.solve(b, form: :upper_tri) }
322
+ # end
323
+ # # user system total real
324
+ # # general 73.170000 0.670000 73.840000 ( 73.810086)
325
+ # # upper_tri 0.180000 0.000000 0.180000 ( 0.182491)
326
+ #
327
+ def solve(b, opts = {})
328
+ raise(ShapeError, "Must be called on square matrix") unless self.dim == 2 && self.shape[0] == self.shape[1]
329
+ raise(ShapeError, "number of rows of b must equal number of cols of self") if
330
+ self.shape[1] != b.shape[0]
331
+ raise(ArgumentError, "only works with dense matrices") if self.stype != :dense
332
+ raise(ArgumentError, "only works for non-integer, non-object dtypes") if
333
+ integer_dtype? or object_dtype? or b.integer_dtype? or b.object_dtype?
334
+
335
+ opts = { form: :general }.merge(opts)
336
+ x = b.clone
337
+ n = self.shape[0]
338
+ nrhs = b.shape[1]
339
+
340
+ nmatrix = create_dummy_nmatrix
341
+ case opts[:form]
342
+ when :general, :upper_tri, :upper_triangular, :lower_tri, :lower_triangular
343
+ #LU solver
344
+ solver = LUDecomposition.new(self.twoDMat).getSolver
345
+ nmatrix.s = solver.solve(b.s)
346
+ return nmatrix
347
+ when :pos_def, :positive_definite
348
+ solver = CholeskyDecomposition.new(self.twoDMat).getSolver
349
+ nmatrix.s = solver.solve(b.s)
350
+ return nmatrix
351
+ else
352
+ raise(ArgumentError, "#{opts[:form]} is not a valid form option")
353
+ end
354
+
355
+ end
356
+
357
+ #
358
+ # call-seq:
359
+ # det -> determinant
360
+ #
361
+ # Calculate the determinant by way of LU decomposition. This is accomplished
362
+ # using clapack_getrf, and then by taking the product of the diagonal elements. There is a
363
+ # risk of underflow/overflow.
364
+ #
365
+ # There are probably also more efficient ways to calculate the determinant.
366
+ # This method requires making a copy of the matrix, since clapack_getrf
367
+ # modifies its input.
368
+ #
369
+ # For smaller matrices, you may be able to use +#det_exact+.
370
+ #
371
+ # This function is guaranteed to return the same type of data in the matrix
372
+ # upon which it is called.
373
+ #
374
+ # Integer matrices are converted to floating point matrices for the purposes of
375
+ # performing the calculation, as xGETRF can't work on integer matrices.
376
+ #
377
+ # * *Returns* :
378
+ # - The determinant of the matrix. It's the same type as the matrix's dtype.
379
+ # * *Raises* :
380
+ # - +ShapeError+ -> Must be used on square matrices.
381
+ #
382
+ def det
383
+ raise(ShapeError, "determinant can be calculated only for square matrices") unless self.dim == 2 && self.shape[0] == self.shape[1]
384
+ self.det_exact2
385
+ end
386
+
387
+ #
388
+ # call-seq:
389
+ # complex_conjugate -> NMatrix
390
+ # complex_conjugate(new_stype) -> NMatrix
391
+ #
392
+ # Get the complex conjugate of this matrix. See also complex_conjugate! for
393
+ # an in-place operation (provided the dtype is already +:complex64+ or
394
+ # +:complex128+).
395
+ #
396
+ # Doesn't work on list matrices, but you can optionally pass in the stype you
397
+ # want to cast to if you're dealing with a list matrix.
398
+ #
399
+ # * *Arguments* :
400
+ # - +new_stype+ -> stype for the new matrix.
401
+ # * *Returns* :
402
+ # - If the original NMatrix isn't complex, the result is a +:complex128+ NMatrix. Otherwise, it's the original dtype.
403
+ #
404
+ def complex_conjugate(new_stype = self.stype)
405
+ self.cast(new_stype, NMatrix::upcast(dtype, :complex64)).complex_conjugate!
406
+ end
407
+
408
+ #
409
+ # call-seq:
410
+ # conjugate_transpose -> NMatrix
411
+ #
412
+ # Calculate the conjugate transpose of a matrix. If your dtype is already
413
+ # complex, this should only require one copy (for the transpose).
414
+ #
415
+ # * *Returns* :
416
+ # - The conjugate transpose of the matrix as a copy.
417
+ #
418
+ def conjugate_transpose
419
+ self.transpose.complex_conjugate!
420
+ end
421
+
422
+ #
423
+ # call-seq:
424
+ # absolute_sum -> Numeric
425
+ #
426
+ # == Arguments
427
+ # - +incx+ -> the skip size (defaults to 1, no skip)
428
+ # - +n+ -> the number of elements to include
429
+ #
430
+ # Return the sum of the contents of the vector. This is the BLAS asum routine.
431
+ def asum incx=1, n=nil
432
+ if self.shape == [1]
433
+ return self[0].abs unless self.complex_dtype?
434
+ return self[0].real.abs + self[0].imag.abs
435
+ end
436
+ return method_missing(:asum, incx, n) unless vector?
437
+ NMatrix::BLAS::asum(self, incx, self.size / incx)
438
+ end
439
+ alias :absolute_sum :asum
440
+
441
+ #
442
+ # call-seq:
443
+ # norm2 -> Numeric
444
+ #
445
+ # == Arguments
446
+ # - +incx+ -> the skip size (defaults to 1, no skip)
447
+ # - +n+ -> the number of elements to include
448
+ #
449
+ # Return the 2-norm of the vector. This is the BLAS nrm2 routine.
450
+ def nrm2 incx=1, n=nil
451
+ self.twoDMat.getFrobeniusNorm()
452
+ end
453
+ alias :norm2 :nrm2
454
+
455
+ #
456
+ # call-seq:
457
+ # scale! -> NMatrix
458
+ #
459
+ # == Arguments
460
+ # - +alpha+ -> Scalar value used in the operation.
461
+ # - +inc+ -> Increment used in the scaling function. Should generally be 1.
462
+ # - +n+ -> Number of elements of +vector+.
463
+ #
464
+ # This is a destructive method, modifying the source NMatrix. See also #scale.
465
+ # Return the scaling result of the matrix. BLAS scal will be invoked if provided.
466
+
467
+ def scale!(alpha, incx=1, n=nil)
468
+ #FIXME
469
+ # raise(DataTypeError, "Incompatible data type for the scaling factor") unless
470
+ # NMatrix::upcast(self.dtype, NMatrix::min_dtype(alpha)) == self.dtype
471
+ raise(DataTypeError, "Incompatible data type for the scaling factor") if
472
+ self.dtype == :int8
473
+ @s.mapMultiplyToSelf(alpha)
474
+ return self
475
+ end
476
+
477
+ #
478
+ # call-seq:
479
+ # scale -> NMatrix
480
+ #
481
+ # == Arguments
482
+ # - +alpha+ -> Scalar value used in the operation.
483
+ # - +inc+ -> Increment used in the scaling function. Should generally be 1.
484
+ # - +n+ -> Number of elements of +vector+.
485
+ #
486
+ # Return the scaling result of the matrix. BLAS scal will be invoked if provided.
487
+
488
+ def scale(alpha, incx=1, n=nil)
489
+ # FIXME
490
+ # raise(DataTypeError, "Incompatible data type for the scaling factor") unless
491
+ # NMatrix::upcast(self.dtype, NMatrix::min_dtype(alpha)) == self.dtype
492
+ raise(DataTypeError, "Incompatible data type for the scaling factor") if
493
+ self.dtype == :byte || self.dtype == :int8 || self.dtype == :int16 ||
494
+ self.dtype == :int32 || self.dtype == :int64
495
+ nmatrix = NMatrix.new :copy
496
+ nmatrix.shape = @shape.clone
497
+ nmatrix.s = ArrayRealVector.new(@s.toArray.clone).mapMultiplyToSelf(alpha)
498
+ return nmatrix
499
+ end
500
+
501
+ end