jblas-ruby 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,325 @@
1
+ # Java based matrix classes for ruby
2
+ #
3
+ # This file provides a module JBLAS which contains efficient matrix
4
+ # implementations based on java and BLAS/LAPACK implementations.
5
+ #
6
+ # For double precision computations, DoubleMatrix and DoubleVector are
7
+ # provided, for single precision computations, FloatMatrix and
8
+ # FloatVector.
9
+ #
10
+ # Basically, the classes provided are the actual java classes with
11
+ # syntactic sugar added (for example for arithmetic operations). This
12
+ # is integrates very nicely, however, not that the full list of
13
+ # methods is not available in rdoc.
14
+
15
+ # Copyright (c) 2009-2010, Mikio L. Braun and contributors
16
+ # All rights reserved.
17
+ #
18
+ # Redistribution and use in source and binary forms, with or without
19
+ # modification, are permitted provided that the following conditions are
20
+ # met:
21
+ #
22
+ # * Redistributions of source code must retain the above copyright
23
+ # notice, this list of conditions and the following disclaimer.
24
+ #
25
+ # * Redistributions in binary form must reproduce the above
26
+ # copyright notice, this list of conditions and the following
27
+ # disclaimer in the documentation and/or other materials provided
28
+ # with the distribution.
29
+ #
30
+ # * Neither the name of the Technische Universität Berlin nor the
31
+ # names of its contributors may be used to endorse or promote
32
+ # products derived from this software without specific prior
33
+ # written permission.
34
+ #
35
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36
+ # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37
+ # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38
+ # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39
+ # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40
+ # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41
+ # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42
+ # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43
+ # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44
+ # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46
+
47
+ require 'jblas/java'
48
+ require 'jblas/matrix_mixin'
49
+ require 'jblas/extensions'
50
+ require 'jblas/functions'
51
+ require 'jblas/arith'
52
+ require 'jblas/complex'
53
+ require 'jblas/errors'
54
+ require 'jblas/mixin_complex_matrix'
55
+
56
+ # The jblas module provides matrix classes and functions to
57
+ # comfortably work with the matrices.
58
+ #
59
+ # = Overview
60
+ #
61
+ # JBLAS defines the following six classes:
62
+ #
63
+ # * DoubleMatrix - double precision real matrix
64
+ # * FloatMatrix - single precision real matrix
65
+ # * ComplexDoubleMatrix - double precision complex matrix
66
+ # * ComplexFloatMatrix - single precision complex matrix
67
+ # * ComplexDouble - double precision complex number
68
+ # * ComplexFloat - single precision complex number
69
+ #
70
+ # These classes have the usual arithmetic operations defined as well as coercion
71
+ # to make them work as seamlessly as possible with the norm built-in Ruby
72
+ # numerical types.
73
+ #
74
+ # Technically, jblas-ruby is organized in a number of mixins which are included
75
+ # in the Java objects to add syntactic sugar and make the objects play well
76
+ # with Ruby. Links to these mixins are included in each section.
77
+ #
78
+ # = Creation
79
+ #
80
+ # <em>See also JBLAS::MatrixClassMixin, mat</em>
81
+ #
82
+ # You create a matrix or vector explicitly by using the
83
+ # DoubleMatrix[], or FloatMatrix[] constructor. For example:
84
+ #
85
+ # DoubleMatrix[[1,2,3],[4,5,6]]
86
+ # => 1.0 2.0 3.0
87
+ # 4.0 5.0 6.0
88
+ #
89
+ # DoubleMatrix[1,2,3]
90
+ # => 1.0
91
+ # 2.0
92
+ # 3.0
93
+ #
94
+ # Since typing DoubleMatrix all the time is a bit cumbersome, jblas also
95
+ # provides the mat function which is a short-hand for DoubleMatrix[...]:
96
+ #
97
+ # mat[1,2,3]
98
+ # => 1.0
99
+ # 2.0
100
+ # 3.0
101
+ #
102
+ # Apart from these constructors, there are few more functions which
103
+ # generate matrices or vectors:
104
+ #
105
+ # * zeros(n), and zeros(n,m): vector or matrix of zeros.
106
+ # * ones(...) - a vector or matrix of ones.
107
+ # * rand(...) - a vector or matrix whose elements are drawn uniformly in [0,1]
108
+ # * randn(...) - elemens are drawn from normal Gaussian distribution
109
+ # * diag(x) - return diagonal of a matrix or matrix with given diagonal
110
+ # * eye(n) - identity matrix
111
+ # * x.hcat(y) - returns the horizontal concatenation of x and y
112
+ # * x.vcat(y) - returns the vertical concatenation of x and y
113
+ #
114
+ # hcat and vcat also exist as methods which take an arbitrary number of arguments
115
+ # and returns the horizontal or vertical concatenation of its arguments.
116
+ #
117
+ # = Accessing elements
118
+ #
119
+ # <em>See also JBLAS::MatrixAccessMixin.</em>
120
+ #
121
+ # To access individual elements, use the [] or []= methods, or +get+, and +put+
122
+ # respectively.
123
+ #
124
+ # Rows and columns can be accessed with get_row, put_row and get_column, put_column.
125
+ #
126
+ # You can also often use ranges or enumerables with [] and []=, for example
127
+ #
128
+ # x[0..2, [1,2,3]]
129
+ #
130
+ # = Arithmetics
131
+ #
132
+ # <em>See also JBLAS::MatrixAccessMixin.</em>
133
+ #
134
+ # Arithmetic is defined using the usual operators, that is
135
+ #
136
+ # * (matrix-)multiplication: a * b
137
+ # * addition: a + b
138
+ # * subtraction: a - b
139
+ #
140
+ # Multiplication is the usual (linear algebra) multiplication.
141
+ #
142
+ # There exist also non-operator versions (which are the original Java functions)
143
+ # These also give you more control over the generation
144
+ # of temporary objects. The suffix "!" or "i" indicates that the computation
145
+ # is performed in-place on the left operand.
146
+ #
147
+ # * (matrix-)multiplication: a.mmul(b), a.mmul!(b)
148
+ # * elementwise multiplication: a.mul(b), a.mul!(b)
149
+ # * addition: a.add(b), a.add!(b)
150
+ # * subtraction: a.sub(b), a.sub!(b)
151
+ # * elementwise division: a.div(b), a.div!(b)
152
+ #
153
+ # Some special functions exist for adding the same column vector to
154
+ # all columns of a matrix, or row vector to all rows:
155
+ #
156
+ # * m.add_column_vector(x) adds a column vector
157
+ # * m.add_row_vector(x) adds a row vector
158
+ #
159
+ # = Matrix and Vectors as Enumerables
160
+ #
161
+ # <em>See also JBLAS::MatrixEnumMixin.</em>
162
+ #
163
+ # Both the matrices and vectors implement the Enumerable mixin. Matrices behave
164
+ # as if they are a linear array of their elements (going down rows first). If
165
+ # you want to iterate over rows or columns, use the rows_to_a or columns_to_a methods,
166
+ # as well as each_row and each_column.
167
+ #
168
+ # = Functions
169
+ #
170
+ # JBLAS defines a large number of mathematical functions. You can
171
+ # either call these as a method on a matrix, vector, or even number,
172
+ # or in the usual notation as a function.
173
+ #
174
+ # * acos: arcus cosine
175
+ # * asin: arcus sine
176
+ # * atan: arcus tangens
177
+ # * cos: cosine
178
+ # * cosh: hyperbolic cosine
179
+ # * exp: exponential
180
+ # * log10: logarithm to base 10
181
+ # * log: natural logarithm
182
+ # * sin: sine
183
+ # * sinh: hyperbolic sine
184
+ # * sqrt: square root
185
+ # * tan: tangens
186
+ # * tanh: hyperbolic tangens
187
+ #
188
+ # By adding the suffix "i" or "!" to the method functions, you again perform the
189
+ # computation in-place. For example
190
+ #
191
+ # exp(x)
192
+ #
193
+ # returns a copy of +x+, but
194
+ #
195
+ # exp(x)
196
+ # exp!(x)
197
+ #
198
+ # do not.
199
+ #
200
+ #
201
+ # = Geometry
202
+ #
203
+ # Some functions to deal with geometric properties:
204
+ #
205
+ # * norm(x, type=2) computes the norm for type=1, 2, or :inf
206
+ # * x.dot(y) computes the scalar product
207
+ #
208
+ #
209
+ # = Linear Equations
210
+ #
211
+ # In order to solve the linear equation <tt>a * x = b</tt>, with +b+
212
+ # either being a matrix or a vector, call solve:
213
+ #
214
+ # x = a.solve(b)
215
+ #
216
+ # or
217
+ #
218
+ # solve(a, b)
219
+ #
220
+ #
221
+ # = Eigenproblems
222
+ #
223
+ # Compute the eigenvalue of a square matrix +a+ with
224
+ #
225
+ # e = eig(a)
226
+ #
227
+ # Compute the eigenvectors as well with
228
+ #
229
+ # u, d = eigv(a)
230
+ #
231
+ # eigv returns two matrices, the matrix +u+ whose columns are the
232
+ # eigenvectors, and the matrix +d+, whose diagonal contains the
233
+ # eigenvalues.
234
+ #
235
+ # = Singular value decomposition
236
+ #
237
+ # Compute the singular value decomposition of an (arbitrarily shaped)
238
+ # matrix +a+ with
239
+ #
240
+ # u, s, v = svd(a)
241
+ #
242
+ # The columns of +u+ and +v+ will contain the singular vectors, and +s+ is
243
+ # a vector containing the singular values.
244
+ #
245
+ # You can also compute a sparse SVD with svd(a, true) (meaning that +u+
246
+ # and +v+ are not square but have the minimal rectangular size necessary).
247
+ #
248
+ # Finally, svdv(a) computes only the singular values of +a+.
249
+
250
+ module JBLAS
251
+ ######################################################################
252
+ #
253
+ # Matrices
254
+ #
255
+ ######################################################################
256
+
257
+ # Matrix for stroing float values.
258
+ #
259
+ # This matrix is essentially org.jblas.DoubleMatrix with all the syntactic
260
+ # sugar defined in the MatrixClassMixin and MatrixMixin mix-ins. See those
261
+ # modules for further information, and the JBLAS module for an overview.
262
+ class DoubleMatrix
263
+ class <<self
264
+ include MatrixClassMixin
265
+ end
266
+
267
+ include MatrixMixin
268
+ end
269
+
270
+ # Matrix for storing float values.
271
+ #
272
+ # This matrix is essentially org.jblas.FloatMatrix with all the syntactic
273
+ # sugar defined in the MatrixClassMixin and MatrixMixin mix-ins. See those
274
+ # modules for further information, and the JBLAS module for an overview.
275
+ class FloatMatrix
276
+ class <<self
277
+ include MatrixClassMixin
278
+ end
279
+ include MatrixMixin
280
+ end
281
+
282
+ # Matrix for storing complex double values.
283
+ #
284
+ # This matrix is essentially org.jblas.ComplexDoubleMatrix with all the syntactic
285
+ # sugar defined in the MatrixClassMixin and MatrixMixin mix-ins. See those
286
+ # modules for further information, and the JBLAS module for an overview.
287
+ class ComplexDoubleMatrix
288
+ class <<self
289
+ include MatrixClassMixin
290
+ end
291
+ include MatrixMixin
292
+ include ComplexMatrixMixin
293
+ end
294
+
295
+ # Matrix for storing complex float values.
296
+ #
297
+ # This matrix is essentially org.jblas.ComplexFloatMatrix with all the syntactic
298
+ # sugar defined in the MatrixClassMixin and MatrixMixin mix-ins. See those
299
+ # modules for further information, and the JBLAS module for an overview.
300
+ class ComplexFloatMatrix
301
+ class <<self
302
+ include MatrixClassMixin
303
+ end
304
+ include MatrixMixin
305
+ include ComplexMatrixMixin
306
+ end
307
+
308
+ # Double precision complex number.
309
+ #
310
+ # This class is essentially org.jblas.ComplexDouble with the syntactic sugar
311
+ # defined in the module ComplexMixin.
312
+ class ComplexDouble
313
+ include ComplexMixin
314
+ end
315
+
316
+ # Single precision complex number.
317
+ #
318
+ # This class is essentially org.jblas.ComplexFloat with the syntactic sugar
319
+ # defined in the module ComplexMixin.
320
+ class ComplexFloat
321
+ include ComplexMixin
322
+ end
323
+
324
+ I = ComplexDouble::I
325
+ end
@@ -0,0 +1,69 @@
1
+ # jblas-ruby - fast linear algebra for JRuby
2
+ # (c) 2009-2010 by Mikio L. Braun and contributors
3
+ #
4
+ # Version 1.1, August 20, 2010
5
+ # (jblas-ruby follows jblas (http://jblas.org) version numbers, although
6
+ # jblas-ruby is somewhat more unstable)
7
+ #
8
+ # Homepage: http://mikiobraun.github.com/jblas-ruby/
9
+ #
10
+ # This is only the rdoc starting page. For more information, have a look at
11
+ # the JBLAS module.
12
+ #
13
+ # jblas-ruby is under a BSD style license. See the file COPYING.
14
+ #
15
+ # = Relationship to jblas
16
+ #
17
+ # jblas-ruby started as being a somewhat thin layer around the jblas library.
18
+ # In the beginning, I only added syntatic sugar for arithmetic operations.
19
+ # From that, jblas-ruby has evolved somewhat towards a more M*TLAB like environment.
20
+ # For example, many methods like DoubleMatrix#diag are also available as
21
+ # functions such that you can say the more natural diag(x) instead of x.diag.
22
+ #
23
+ # I also tried to cover most of the Java methods in the documentation, such that
24
+ # you don't need to be proficient in jblas to be able to work with jblas.
25
+ # Occasionally, things may just not work the way you expect them due to some
26
+ # strange typing error. I apologize for all the cases where this happens.
27
+ #
28
+ # Finally, if you're already familiar to jblas, here is a short overview of
29
+ # how Java names translate to JRuby ones:
30
+ #
31
+ # * methods are translated from CamelCase to underscore_style. So x.getRows()
32
+ # becomes x.get_rows().
33
+ # * argument parenthesis are optional, so x.getRows() becomes x.get_rows.
34
+ # * Bean-like methods can be accessed as attributes. Another way to call x.getLength is
35
+ # x.length. Likewise, x.setLength(i) becomes x.length = i.
36
+ # * Packages are converted from the dot.interleaving.style to CamelCase and prefixed
37
+ # with Java::. So org.jblas.DoubleMatrix becomes Java::OrgJblas::DoubleMatrix.
38
+
39
+ # Copyright (c) 2009-2010, Mikio L. Braun and contributors
40
+ # All rights reserved.
41
+ #
42
+ # Redistribution and use in source and binary forms, with or without
43
+ # modification, are permitted provided that the following conditions are
44
+ # met:
45
+ #
46
+ # * Redistributions of source code must retain the above copyright
47
+ # notice, this list of conditions and the following disclaimer.
48
+ #
49
+ # * Redistributions in binary form must reproduce the above
50
+ # copyright notice, this list of conditions and the following
51
+ # disclaimer in the documentation and/or other materials provided
52
+ # with the distribution.
53
+ #
54
+ # * Neither the name of the Technische Universität Berlin nor the
55
+ # names of its contributors may be used to endorse or promote
56
+ # products derived from this software without specific prior
57
+ # written permission.
58
+ #
59
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
60
+ # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
61
+ # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
62
+ # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
63
+ # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
64
+ # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
65
+ # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
66
+ # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
67
+ # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
68
+ # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
69
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,62 @@
1
+ # Defines class JBLAS::ReversedArithmetic which is necessary for expressions
2
+ # like "3 - matrix".
3
+
4
+ # Copyright (c) 2009-2010, Mikio L. Braun and contributors
5
+ # All rights reserved.
6
+ #
7
+ # Redistribution and use in source and binary forms, with or without
8
+ # modification, are permitted provided that the following conditions are
9
+ # met:
10
+ #
11
+ # * Redistributions of source code must retain the above copyright
12
+ # notice, this list of conditions and the following disclaimer.
13
+ #
14
+ # * Redistributions in binary form must reproduce the above
15
+ # copyright notice, this list of conditions and the following
16
+ # disclaimer in the documentation and/or other materials provided
17
+ # with the distribution.
18
+ #
19
+ # * Neither the name of the Technische Universität Berlin nor the
20
+ # names of its contributors may be used to endorse or promote
21
+ # products derived from this software without specific prior
22
+ # written permission.
23
+ #
24
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25
+ # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26
+ # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27
+ # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28
+ # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29
+ # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30
+ # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31
+ # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32
+ # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33
+ # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35
+
36
+ module JBLAS
37
+ # When arguments to arithmetic operators are promoted via coerce, they
38
+ # change their order. This class is a wrapper for the promoted self
39
+ # which has <tt>-</tt> and <tt>/</tt> overloaded to call +rsub+ and
40
+ # +rdiv+ instead of +div+ and +sub+
41
+ class ReversedArithmetic
42
+ def initialize(o)
43
+ @value = o;
44
+ end
45
+
46
+ def +(o)
47
+ @value.add o
48
+ end
49
+
50
+ def *(o)
51
+ @value.mul o
52
+ end
53
+
54
+ def -(o)
55
+ @value.rsub o
56
+ end
57
+
58
+ def /(o)
59
+ @value.rdiv o
60
+ end
61
+ end
62
+ end