mdarray 0.5.4-java → 0.5.5-java
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/LICENSE.txt +35 -16
- data/README.md +180 -234
- data/lib/colt/matrix/algebra.rb +466 -0
- data/lib/colt/matrix/colt_matrix.rb +161 -220
- data/lib/colt/matrix/creation.rb +374 -0
- data/lib/colt/matrix/{matrix_hierarchy.rb → hierarchy.rb} +78 -99
- data/lib/colt/matrix/property.rb +363 -0
- data/lib/mdarray/views.rb +46 -1
- data/test/colt/matrix/test_complete.rb +29 -0
- data/test/colt/{test_matrix.rb → matrix/test_creation.rb} +26 -3
- data/test/colt/{test_double_matrix2d.rb → matrix/test_matrix1d_floatingalgebra.rb} +86 -84
- data/test/colt/matrix/test_matrix2d_fixpointalgebra.rb +531 -0
- data/test/colt/matrix/test_matrix2d_floatingalgebra.rb +693 -0
- data/test/colt/matrix/test_operations.rb +120 -0
- data/test/colt/matrix/test_properties.rb +649 -0
- data/test/colt/test_complete.rb +1 -1
- data/test/complete.rb +12 -7
- data/test/mdarray/test_views.rb +55 -0
- data/vendor/{incanter.jar → mdarray.jar} +0 -0
- data/version.rb +1 -1
- metadata +22 -16
- data/lib/colt/matrix/matrix2D_floating_algebra.rb +0 -325
- data/test/colt/test_float_matrix2d.rb +0 -171
- data/vendor/commons-compiler.jar +0 -0
- data/vendor/janino.jar +0 -0
@@ -0,0 +1,466 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
##########################################################################################
|
4
|
+
# Copyright © 2013 Rodrigo Botafogo. All Rights Reserved. Permission to use, copy, modify,
|
5
|
+
# and distribute this software and its documentation for educational, research, and
|
6
|
+
# not-for-profit purposes, without fee and without a signed licensing agreement, is hereby
|
7
|
+
# granted, provided that the above copyright notice, this paragraph and the following two
|
8
|
+
# paragraphs appear in all copies, modifications, and distributions. Contact Rodrigo
|
9
|
+
# Botafogo - rodrigo.a.botafogo@gmail.com for commercial licensing opportunities.
|
10
|
+
#
|
11
|
+
# IN NO EVENT SHALL RODRIGO BOTAFOGO BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
|
12
|
+
# INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF
|
13
|
+
# THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF RODRIGO BOTAFOGO HAS BEEN ADVISED OF THE
|
14
|
+
# POSSIBILITY OF SUCH DAMAGE.
|
15
|
+
#
|
16
|
+
# RODRIGO BOTAFOGO SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
17
|
+
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
|
18
|
+
# SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS".
|
19
|
+
# RODRIGO BOTAFOGO HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
|
20
|
+
# OR MODIFICATIONS.
|
21
|
+
##########################################################################################
|
22
|
+
|
23
|
+
# require 'jruby/core_ext'
|
24
|
+
|
25
|
+
require 'java'
|
26
|
+
|
27
|
+
##########################################################################################
|
28
|
+
#
|
29
|
+
##########################################################################################
|
30
|
+
|
31
|
+
class Colt
|
32
|
+
|
33
|
+
##########################################################################################
|
34
|
+
#
|
35
|
+
##########################################################################################
|
36
|
+
|
37
|
+
module Matrix1DAlgebra
|
38
|
+
|
39
|
+
#------------------------------------------------------------------------------------
|
40
|
+
# Returns the dot product of two vectors x and y, which is Sum(x[i]*y[i]).
|
41
|
+
#------------------------------------------------------------------------------------
|
42
|
+
|
43
|
+
def mult(other_val, from = 0, length = other_val.size)
|
44
|
+
|
45
|
+
p "mult"
|
46
|
+
@mdarray.print
|
47
|
+
other_val.mdarray.print
|
48
|
+
printf("\n\n")
|
49
|
+
|
50
|
+
if (other_val.is_a? Numeric)
|
51
|
+
MDMatrix.from_mdarray(@mdarray * other_val)
|
52
|
+
elsif (other_val.is_a? MDMatrix)
|
53
|
+
if (other_val.rank == 2)
|
54
|
+
MDMatrix
|
55
|
+
.from_colt_matrix(other_val.transpose.colt_matrix.zMult(@colt_matrix, nil))
|
56
|
+
else
|
57
|
+
p "other_val.rank = #{other_val.rank}"
|
58
|
+
@colt_matrix.zDotProduct(other_val.colt_matrix, from, length)
|
59
|
+
end
|
60
|
+
else
|
61
|
+
raise "Cannot multiply matrix by given value"
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
|
66
|
+
alias :* :mult
|
67
|
+
|
68
|
+
end # Matrix1DAlgebra
|
69
|
+
|
70
|
+
##########################################################################################
|
71
|
+
#
|
72
|
+
##########################################################################################
|
73
|
+
|
74
|
+
module Matrix2DAlgebra
|
75
|
+
|
76
|
+
#------------------------------------------------------------------------------------
|
77
|
+
# Linear algebraic matrix-matrix multiplication; C = alpha * A x B + beta*C.
|
78
|
+
# C[i,j] = alpha*Sum(A[i,k] * B[k,j]) + beta*C[i,j], k=0..n-1.
|
79
|
+
# Matrix shapes: A(m x n), B(n x p), C(m x p).
|
80
|
+
# Note: Matrix shape conformance is checked after potential transpositions.
|
81
|
+
#------------------------------------------------------------------------------------
|
82
|
+
|
83
|
+
def mult(other_val, alpha = 1, beta = 0, transA = false, transB = false, result = nil)
|
84
|
+
|
85
|
+
if (other_val.is_a? Numeric)
|
86
|
+
MDMatrix.from_mdarray(@mdarray * other_val)
|
87
|
+
elsif (other_val.is_a? MDMatrix)
|
88
|
+
if (other_val.rank > 2)
|
89
|
+
raise "Rank should be 1 or 2"
|
90
|
+
elsif (other_val.rank == 2)
|
91
|
+
result = MDMatrix
|
92
|
+
.build(@mdarray.type, [@mdarray.shape[0],
|
93
|
+
other_val.mdarray.shape[1]]) if result == nil
|
94
|
+
@colt_matrix.zMult(other_val.colt_matrix, result.colt_matrix, alpha, beta,
|
95
|
+
transA, transB)
|
96
|
+
else # multiplying by a vector
|
97
|
+
result = MDMatrix.build(@mdarray.type, [@mdarray.shape[1]]) if result == nil
|
98
|
+
@colt_matrix.zMult(other_val.colt_matrix, result.colt_matrix, alpha, beta, transA)
|
99
|
+
end
|
100
|
+
|
101
|
+
# MDMatrix.from_colt_matrix(result)
|
102
|
+
result
|
103
|
+
else
|
104
|
+
raise "Cannot multiply matrix by given value"
|
105
|
+
end
|
106
|
+
|
107
|
+
end
|
108
|
+
|
109
|
+
alias :* :mult
|
110
|
+
|
111
|
+
#------------------------------------------------------------------------------------
|
112
|
+
# Constructs and returns a new view which is the transposition of the given matrix A.
|
113
|
+
#------------------------------------------------------------------------------------
|
114
|
+
|
115
|
+
def transpose
|
116
|
+
MDMatrix.from_mdarray(@mdarray.transpose(0, 1))
|
117
|
+
end
|
118
|
+
|
119
|
+
end # Matrix2DAlgebra
|
120
|
+
|
121
|
+
########################################################################################
|
122
|
+
#
|
123
|
+
########################################################################################
|
124
|
+
|
125
|
+
module MatrixFloatingAlgebra
|
126
|
+
|
127
|
+
#------------------------------------------------------------------------------------
|
128
|
+
# Computes the Kronecker product of two real matrices.
|
129
|
+
#------------------------------------------------------------------------------------
|
130
|
+
|
131
|
+
def kron(matrix)
|
132
|
+
result = @colt_algebra.kron(@colt_matrix, matrix.colt_matrix)
|
133
|
+
MDMatrix.from_colt_matrix(result)
|
134
|
+
end
|
135
|
+
|
136
|
+
#------------------------------------------------------------------------------------
|
137
|
+
# Returns the one-norm of vector x, which is Sum(abs(x[i])).
|
138
|
+
#------------------------------------------------------------------------------------
|
139
|
+
|
140
|
+
def norm1
|
141
|
+
@colt_algebra.norm1(@colt_matrix)
|
142
|
+
end
|
143
|
+
|
144
|
+
#------------------------------------------------------------------------------------
|
145
|
+
# Returns the two-norm of matrix A, which is the maximum singular value; obtained
|
146
|
+
# from SVD.
|
147
|
+
#------------------------------------------------------------------------------------
|
148
|
+
|
149
|
+
def norm2
|
150
|
+
@colt_algebra.norm2(@colt_matrix)
|
151
|
+
end
|
152
|
+
|
153
|
+
#------------------------------------------------------------------------------------
|
154
|
+
# Returns the Frobenius norm of matrix A, which is Sqrt(Sum(A[i,j]^2))
|
155
|
+
#------------------------------------------------------------------------------------
|
156
|
+
|
157
|
+
def normF
|
158
|
+
@colt_algebra.normF(@colt_matrix)
|
159
|
+
end
|
160
|
+
|
161
|
+
#------------------------------------------------------------------------------------
|
162
|
+
# Returns the infinity norm of matrix A, which is the maximum absolute row sum.
|
163
|
+
#------------------------------------------------------------------------------------
|
164
|
+
|
165
|
+
def norm_infinity
|
166
|
+
@colt_algebra.normInfinity(@colt_matrix)
|
167
|
+
end
|
168
|
+
|
169
|
+
end # MatrixFloatingAlgebra
|
170
|
+
|
171
|
+
|
172
|
+
########################################################################################
|
173
|
+
#
|
174
|
+
########################################################################################
|
175
|
+
|
176
|
+
module Matrix2DFloatingAlgebra
|
177
|
+
include_package "cern.colt.matrix.tdouble.algo"
|
178
|
+
include_package "cern.colt.matrix.tfloat.algo"
|
179
|
+
|
180
|
+
#------------------------------------------------------------------------------------
|
181
|
+
# Solves the upper triangular system U*x=b;
|
182
|
+
#------------------------------------------------------------------------------------
|
183
|
+
|
184
|
+
def backward_solve(matrix1D)
|
185
|
+
result = @colt_algebra.backwardSolve(@colt_matrix, matrix1D.colt_matrix)
|
186
|
+
MDMatrix.from_colt_matrix(result)
|
187
|
+
end
|
188
|
+
|
189
|
+
#------------------------------------------------------------------------------------
|
190
|
+
# Constructs and returns the cholesky-decomposition of the given matrix. For a
|
191
|
+
# symmetric, positive definite matrix A, the Cholesky decomposition is a lower
|
192
|
+
# triangular matrix L so that A = L*L'; If the matrix is not symmetric positive
|
193
|
+
# definite, the IllegalArgumentException is thrown.
|
194
|
+
#------------------------------------------------------------------------------------
|
195
|
+
|
196
|
+
def chol
|
197
|
+
result = @colt_algebra.chol(@colt_matrix).getL()
|
198
|
+
MDMatrix.from_colt_matrix(result)
|
199
|
+
end
|
200
|
+
|
201
|
+
#------------------------------------------------------------------------------------
|
202
|
+
# Returns the condition of matrix A, which is the ratio of largest to smallest
|
203
|
+
# singular value.
|
204
|
+
#------------------------------------------------------------------------------------
|
205
|
+
|
206
|
+
def cond
|
207
|
+
@colt_algebra.cond(@colt_matrix)
|
208
|
+
end
|
209
|
+
|
210
|
+
#------------------------------------------------------------------------------------
|
211
|
+
# Returns the determinant of matrix A.
|
212
|
+
#------------------------------------------------------------------------------------
|
213
|
+
|
214
|
+
def det
|
215
|
+
@colt_algebra.det(@colt_matrix)
|
216
|
+
end
|
217
|
+
|
218
|
+
#------------------------------------------------------------------------------------
|
219
|
+
#
|
220
|
+
#------------------------------------------------------------------------------------
|
221
|
+
|
222
|
+
def eig
|
223
|
+
eig = @colt_algebra.eig(@colt_matrix)
|
224
|
+
[MDMatrix.from_colt_matrix(eig.getD),
|
225
|
+
MDMatrix.from_colt_matrix(eig.getImagEigenvalues),
|
226
|
+
MDMatrix.from_colt_matrix(eig.getRealEigenvalues),
|
227
|
+
MDMatrix.from_colt_matrix(eig.getV)]
|
228
|
+
end
|
229
|
+
|
230
|
+
#------------------------------------------------------------------------------------
|
231
|
+
# Solves the lower triangular system L*x=b;
|
232
|
+
#------------------------------------------------------------------------------------
|
233
|
+
|
234
|
+
def forward_solve(matrix1D)
|
235
|
+
result = @colt_algebra.forwardSolve(@colt_matrix, matrix1D.colt_matrix)
|
236
|
+
MDMatrix.from_colt_matrix(result)
|
237
|
+
end
|
238
|
+
|
239
|
+
#------------------------------------------------------------------------------------
|
240
|
+
# Returns the inverse or pseudo-inverse of matrix A.
|
241
|
+
#------------------------------------------------------------------------------------
|
242
|
+
|
243
|
+
def inverse
|
244
|
+
result = @colt_algebra.inverse(@colt_matrix)
|
245
|
+
MDMatrix.from_colt_matrix(result)
|
246
|
+
end
|
247
|
+
|
248
|
+
#------------------------------------------------------------------------------------
|
249
|
+
# Constructs and returns the LU-decomposition of the given matrix.
|
250
|
+
#------------------------------------------------------------------------------------
|
251
|
+
|
252
|
+
def lu
|
253
|
+
result = @colt_algebra.lu(@colt_matrix)
|
254
|
+
[result.isNonsingular(), result.det(), result.getPivot.to_a(),
|
255
|
+
MDMatrix.from_colt_matrix(result.getL()),
|
256
|
+
MDMatrix.from_colt_matrix(result.getU())]
|
257
|
+
end
|
258
|
+
|
259
|
+
#------------------------------------------------------------------------------------
|
260
|
+
# Returns the effective numerical rank of matrix A, obtained from Singular Value
|
261
|
+
# Decomposition.
|
262
|
+
#------------------------------------------------------------------------------------
|
263
|
+
|
264
|
+
def numerical_rank
|
265
|
+
@colt_algebra.rank(@colt_matrix)
|
266
|
+
end
|
267
|
+
|
268
|
+
#------------------------------------------------------------------------------------
|
269
|
+
# Linear algebraic matrix power; B = A^k <==> B = A*A*...
|
270
|
+
#------------------------------------------------------------------------------------
|
271
|
+
|
272
|
+
def power(val)
|
273
|
+
result = @colt_algebra.pow(@colt_matrix, val)
|
274
|
+
MDMatrix.from_colt_matrix(result)
|
275
|
+
end
|
276
|
+
|
277
|
+
alias :** :power
|
278
|
+
|
279
|
+
#------------------------------------------------------------------------------------
|
280
|
+
# Solves A*X = B
|
281
|
+
#------------------------------------------------------------------------------------
|
282
|
+
|
283
|
+
def solve(matrix)
|
284
|
+
result = @colt_algebra.solve(@colt_matrix, matrix.colt_matrix)
|
285
|
+
MDMatris.from_colt_matrix(resul)
|
286
|
+
end
|
287
|
+
|
288
|
+
#------------------------------------------------------------------------------------
|
289
|
+
# Solves X*A = B, which is also A'*X' = B'.
|
290
|
+
#------------------------------------------------------------------------------------
|
291
|
+
|
292
|
+
def solve_transpose(matrix)
|
293
|
+
result = @colt_algebra.solveTranspose(@colt_matrix, matrix.colt_matrix)
|
294
|
+
MDMatris.from_colt_matrix(resul)
|
295
|
+
end
|
296
|
+
|
297
|
+
#------------------------------------------------------------------------------------
|
298
|
+
# Constructs and returns the SingularValue-decomposition of the given matrix.
|
299
|
+
#------------------------------------------------------------------------------------
|
300
|
+
|
301
|
+
def svd
|
302
|
+
result = @colt_algebra.svd(@colt_matrix)
|
303
|
+
[result.getInfo().val, result.cond(), result.norm2(), result.rank(),
|
304
|
+
result.getSingularValues().to_a(),
|
305
|
+
MDMatrix.from_colt_matrix(result.getS()),
|
306
|
+
MDMatrix.from_colt_matrix(result.getU()),
|
307
|
+
MDMatrix.from_colt_matrix(result.getV())]
|
308
|
+
end
|
309
|
+
|
310
|
+
#------------------------------------------------------------------------------------
|
311
|
+
# Returns the sum of the diagonal elements of matrix A; Sum(A[i,i]).
|
312
|
+
#------------------------------------------------------------------------------------
|
313
|
+
|
314
|
+
def trace
|
315
|
+
@colt_algebra.trace(@colt_matrix)
|
316
|
+
end
|
317
|
+
|
318
|
+
#------------------------------------------------------------------------------------
|
319
|
+
# Modifies the matrix to be a lower trapezoidal matrix.
|
320
|
+
#------------------------------------------------------------------------------------
|
321
|
+
|
322
|
+
def trapezoidal_lower
|
323
|
+
result = @colt_algebra.trapezoidalLower(@colt_matrix)
|
324
|
+
MDMatrix.from_colt_matrix(result)
|
325
|
+
end
|
326
|
+
|
327
|
+
#------------------------------------------------------------------------------------
|
328
|
+
# Returns the two-norm (aka euclidean norm) of vector X.vectorize();
|
329
|
+
#------------------------------------------------------------------------------------
|
330
|
+
|
331
|
+
def vector_norm2
|
332
|
+
@colt_algebra.vectorNorm2(@colt_matrix)
|
333
|
+
end
|
334
|
+
|
335
|
+
end # Matrix2dFloatingAlgebra
|
336
|
+
|
337
|
+
##########################################################################################
|
338
|
+
#
|
339
|
+
##########################################################################################
|
340
|
+
|
341
|
+
module Matrix2DDoubleAlgebra
|
342
|
+
include_package "cern.colt.matrix.tdouble.algo"
|
343
|
+
|
344
|
+
#------------------------------------------------------------------------------------
|
345
|
+
# Constructs and returns the QR-decomposition of the given matrix.
|
346
|
+
#------------------------------------------------------------------------------------
|
347
|
+
|
348
|
+
def qr(economy_size = true)
|
349
|
+
result = @colt_algebra.qr(@colt_matrix)
|
350
|
+
[result.hasFullRank(), MDMatrix.from_colt_matrix(result.getQ(economy_size)),
|
351
|
+
MDMatrix.from_colt_matrix(result.getR(economy_size))]
|
352
|
+
end
|
353
|
+
|
354
|
+
end
|
355
|
+
|
356
|
+
##########################################################################################
|
357
|
+
#
|
358
|
+
##########################################################################################
|
359
|
+
|
360
|
+
module Matrix2DFloatAlgebra
|
361
|
+
include_package "cern.colt.matrix.tfloat.algo"
|
362
|
+
|
363
|
+
#------------------------------------------------------------------------------------
|
364
|
+
# Constructs and returns the QR-decomposition of the given matrix.
|
365
|
+
#------------------------------------------------------------------------------------
|
366
|
+
|
367
|
+
def qr
|
368
|
+
result = @colt_algebra.qr(@colt_matrix)
|
369
|
+
[result.hasFullRank(),
|
370
|
+
MDMatrix.from_colt_matrix(result.getH()),
|
371
|
+
MDMatrix.from_colt_matrix(result.getQ()),
|
372
|
+
MDMatrix.from_colt_matrix(result.getR())]
|
373
|
+
end
|
374
|
+
|
375
|
+
end
|
376
|
+
|
377
|
+
end # Colt
|
378
|
+
|
379
|
+
|
380
|
+
##########################################################################################
|
381
|
+
#
|
382
|
+
##########################################################################################
|
383
|
+
|
384
|
+
class DoubleMDMatrix1D
|
385
|
+
|
386
|
+
include Colt::MatrixFloatingAlgebra
|
387
|
+
include Colt::Matrix1DAlgebra
|
388
|
+
|
389
|
+
end
|
390
|
+
|
391
|
+
##########################################################################################
|
392
|
+
#
|
393
|
+
##########################################################################################
|
394
|
+
|
395
|
+
class DoubleMDMatrix2D
|
396
|
+
|
397
|
+
include Colt::MatrixFloatingAlgebra
|
398
|
+
include Colt::Matrix2DAlgebra
|
399
|
+
include Colt::Matrix2DFloatingAlgebra
|
400
|
+
include Colt::Matrix2DDoubleAlgebra
|
401
|
+
|
402
|
+
end
|
403
|
+
|
404
|
+
##########################################################################################
|
405
|
+
#
|
406
|
+
##########################################################################################
|
407
|
+
|
408
|
+
class FloatMDMatrix1D
|
409
|
+
|
410
|
+
include Colt::MatrixFloatingAlgebra
|
411
|
+
include Colt::Matrix1DAlgebra
|
412
|
+
|
413
|
+
end
|
414
|
+
|
415
|
+
##########################################################################################
|
416
|
+
#
|
417
|
+
##########################################################################################
|
418
|
+
|
419
|
+
class FloatMDMatrix2D
|
420
|
+
|
421
|
+
include Colt::MatrixFloatingAlgebra
|
422
|
+
include Colt::Matrix2DAlgebra
|
423
|
+
include Colt::Matrix2DFloatingAlgebra
|
424
|
+
include Colt::Matrix2DFloatAlgebra
|
425
|
+
|
426
|
+
end
|
427
|
+
|
428
|
+
##########################################################################################
|
429
|
+
#
|
430
|
+
##########################################################################################
|
431
|
+
|
432
|
+
class LongMDMatrix1D
|
433
|
+
|
434
|
+
include Colt::Matrix1DAlgebra
|
435
|
+
|
436
|
+
end
|
437
|
+
|
438
|
+
##########################################################################################
|
439
|
+
#
|
440
|
+
##########################################################################################
|
441
|
+
|
442
|
+
class LongMDMatrix2D
|
443
|
+
|
444
|
+
include Colt::Matrix2DAlgebra
|
445
|
+
|
446
|
+
end
|
447
|
+
|
448
|
+
##########################################################################################
|
449
|
+
#
|
450
|
+
##########################################################################################
|
451
|
+
|
452
|
+
class IntMDMatrix1D
|
453
|
+
|
454
|
+
include Colt::Matrix1DAlgebra
|
455
|
+
|
456
|
+
end
|
457
|
+
|
458
|
+
##########################################################################################
|
459
|
+
#
|
460
|
+
##########################################################################################
|
461
|
+
|
462
|
+
class IntMDMatrix2D
|
463
|
+
|
464
|
+
include Colt::Matrix2DAlgebra
|
465
|
+
|
466
|
+
end
|