jblas-ruby 1.1.1

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.
@@ -0,0 +1,75 @@
1
+ # Load the java libraries and import the classes into JBLAS.
2
+
3
+ # Copyright (c) 2009-2010, Mikio L. Braun and contributors
4
+ # All rights reserved.
5
+ #
6
+ # Redistribution and use in source and binary forms, with or without
7
+ # modification, are permitted provided that the following conditions are
8
+ # met:
9
+ #
10
+ # * Redistributions of source code must retain the above copyright
11
+ # notice, this list of conditions and the following disclaimer.
12
+ #
13
+ # * Redistributions in binary form must reproduce the above
14
+ # copyright notice, this list of conditions and the following
15
+ # disclaimer in the documentation and/or other materials provided
16
+ # with the distribution.
17
+ #
18
+ # * Neither the name of the Technische Universität Berlin nor the
19
+ # names of its contributors may be used to endorse or promote
20
+ # products derived from this software without specific prior
21
+ # written permission.
22
+ #
23
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24
+ # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25
+ # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26
+ # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27
+ # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28
+ # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29
+ # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30
+ # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31
+ # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32
+ # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
+
35
+ require 'java'
36
+ begin
37
+ require 'jblas/jblas-1.1.1.jar'
38
+ rescue LoadError => e
39
+ begin
40
+ org.jblas.DoubleMatrix
41
+ rescue NameError => e
42
+ raise LoadError, 'Cannot load jblas-1.1.jar, and it also does not seem to be in the class path!'
43
+ end
44
+ end
45
+
46
+ module JBLAS
47
+ ######################################################################
48
+ #
49
+ # Java Classes Import
50
+ #
51
+ ######################################################################
52
+
53
+ # In connection with rish (http://mikiobraun.github.com/rish/), we
54
+ # need this guard for possible reloading of this file.
55
+ unless JBLAS < Java
56
+ include Java
57
+
58
+ java_import org.jblas.DoubleMatrix
59
+ java_import org.jblas.FloatMatrix
60
+ java_import org.jblas.ComplexDoubleMatrix
61
+ java_import org.jblas.ComplexFloatMatrix
62
+ java_import org.jblas.ComplexDouble
63
+ java_import org.jblas.ComplexFloat
64
+ java_import org.jblas.SimpleBlas
65
+ java_import org.jblas.DoubleFunction
66
+ java_import org.jblas.FloatFunction
67
+
68
+ java_import org.jblas.Solve
69
+ java_import org.jblas.Eigen
70
+ java_import org.jblas.Geometry
71
+ java_import org.jblas.Decompose
72
+ java_import org.jblas.MatrixFunctions
73
+ java_import org.jblas.Singular
74
+ end
75
+ end
Binary file
@@ -0,0 +1,64 @@
1
+ # Methods added to the Java classes are encoded as mixins in this file. Defines
2
+ # JBLAS::MatrixMixin.
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
+ require 'jblas/mixin_general'
37
+ require 'jblas/mixin_convert'
38
+ require 'jblas/mixin_access'
39
+ require 'jblas/mixin_arith'
40
+ require 'jblas/mixin_class'
41
+ require 'jblas/mixin_enum'
42
+
43
+ module JBLAS
44
+ # Extensions to matrix classes (mostly arithmetics). This mixin is
45
+ # then used to automatically enrich the DoubleMatrix and FloatMatrix
46
+ # classes.
47
+ #
48
+ # See JBLAS for an overview. You can also browse the different functions
49
+ # by section:
50
+ #
51
+ # * MatrixGeneralMixin - general methods
52
+ # * MatrixConvertMixin - converting matrices to other representations
53
+ # * MatrixArithMixin - arithmetic operations
54
+ # * MatrixEnumMixin - matrices as an Enumerable
55
+ # * MatrixAccessMixin - accessing elements
56
+ # Java-side jblas.
57
+ module MatrixMixin
58
+ include MatrixGeneralMixin
59
+ include MatrixConvertMixin
60
+ include MatrixArithMixin
61
+ include MatrixEnumMixin
62
+ include MatrixAccessMixin
63
+ end # module MatrixMixin
64
+ end
@@ -0,0 +1,155 @@
1
+ # Mixins and extensions for acessing elements.
2
+
3
+ # Copyright (c) 2009-2010, Mikio L. Braun and contributors
4
+ # All rights reserved.
5
+ #
6
+ # Redistribution and use in source and binary forms, with or without
7
+ # modification, are permitted provided that the following conditions are
8
+ # met:
9
+ #
10
+ # * Redistributions of source code must retain the above copyright
11
+ # notice, this list of conditions and the following disclaimer.
12
+ #
13
+ # * Redistributions in binary form must reproduce the above
14
+ # copyright notice, this list of conditions and the following
15
+ # disclaimer in the documentation and/or other materials provided
16
+ # with the distribution.
17
+ #
18
+ # * Neither the name of the Technische Universität Berlin nor the
19
+ # names of its contributors may be used to endorse or promote
20
+ # products derived from this software without specific prior
21
+ # written permission.
22
+ #
23
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24
+ # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25
+ # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26
+ # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27
+ # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28
+ # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29
+ # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30
+ # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31
+ # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32
+ # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
+
35
+ class Numeric # :nodoc:
36
+ def to_indices
37
+ self
38
+ end
39
+ end
40
+
41
+ module Enumerable # :nodoc:
42
+ def to_indices
43
+ to_a.to_java :int
44
+ end
45
+ end
46
+
47
+ module Java::OrgJblasRanges::Range
48
+ def to_indices
49
+ self
50
+ end
51
+ end
52
+
53
+ module JBLAS
54
+ # Mixin for all kinds of element access.
55
+ #
56
+ # This mixin is collected into MatrixMixin.
57
+ #
58
+ # You have the following options of accessing elements:
59
+ #
60
+ # * <b>Linear access</b>: accessing all elements linearly, going down rows
61
+ # first (x[i], x.get(i), x.put(i))
62
+ # * <b>Two-dimensional access</b>: Accessing element in row i,
63
+ # column j (x[i,j], x.get(i,j), x.put(i,j)
64
+ # * <b>Accessing rows and columns</b>: get_row(i), get_column(i), put_row(i), put_column(i), and
65
+ # also get_rows(i), get_columns(i).
66
+ # * <b>Accessing row and column ranges</b>: get_row_range, get_column_range, get_range
67
+ #
68
+ # As indices, you can use one of the following:
69
+ #
70
+ # * <b>Numbers</b>: x[10], x[3,4], x.getRow(3), etc.
71
+ # * <b>Indices as int[] arrays</b>: Unfortunately, these are bit hard
72
+ # to construct in JRuby ([1,2,3].to_java :int), therefore the emthod
73
+ # to_indices is added to Numeric, Enumerable, Array, and the matrices.
74
+ # Then: `i = [1,2,3]; x[i.to_indices]`.
75
+ # * <b>Matrices</b>: In that case, x.find_indices is called to find the
76
+ # non-zero elements and use those as indices.
77
+ # * <b>Ranges</b>: See JBLAS::Ranges
78
+ #
79
+ # For some access functions (in particular getting rows and columns), you
80
+ # can also specify where to copy the result to get better performance through
81
+ # suppressing object creations.
82
+ #
83
+ module MatrixAccessMixin
84
+ # Get the entry at _i_, _j_. If _j_ is omitted, linear
85
+ # addressing is used (that is, _i_ just enumerates all entries
86
+ # going down rows first.)
87
+ #
88
+ # As indices you can use numbers, int[] arrays, matrices (non-zero elements
89
+ # are taken as indices then), and ranges.
90
+ def [](i, j=nil)
91
+ if j
92
+ get(i.to_indices, j.to_indices)
93
+ else
94
+ get(i.to_indices)
95
+ end
96
+ end
97
+
98
+ # Set the entry at _i_, _j_ to _v_. If _j_ is omitted, linear
99
+ # addressing is used (that is, _i_ just enumerates all entries
100
+ # going down rows first.)
101
+ #
102
+ # As indices you can use numbers, int[] arrays, matrices (non-zero elements
103
+ # are taken as indices then), and ranges.
104
+ def []=(i, j, v=nil)
105
+ if v
106
+ put(i.to_indices, j.to_indices, v)
107
+ else
108
+ put(i.to_indices, j)
109
+ end
110
+ end
111
+
112
+ # Get row of a matrix. Unlike the row(i) method, this method
113
+ # returns a copy of the given row. If result is given, the
114
+ # row is copied in that matrix.
115
+ def get_row(i, result=nil); JAVA_METHOD; end if false
116
+
117
+ # Get a number of rows.
118
+ #
119
+ # As indices you can use numbers, int[] arrays, matrices (non-zero elements
120
+ # are taken as indices then), and ranges.
121
+ def get_rows(i); JAVA_METHOD; end if false
122
+
123
+ # Get a column of a matrix. Unlike column(i) method, this
124
+ # method returns a copy of the given column.
125
+ def get_column(i); JAVA_METHOD; end if false
126
+
127
+ # Get a number of rows.
128
+ #
129
+ # As indices you can use numbers, int[] arrays, matrices (non-zero elements
130
+ # are taken as indices then), and ranges.
131
+ def get_columns(i); JAVA_METHOD; end if false
132
+
133
+ # Get a copy of rows i1 .. i2 - 1 from column j.
134
+ def get_row_range(i1, i2, j); JAVA_METHOD; end if false
135
+
136
+ # Get a copy of columns j1 .. j2 - 1 from row i.
137
+ def get_column_range(i, j1, j2); JAVA_METHOD; end if false
138
+
139
+ # Get a copy of the submatrix with rows i1 .. i2 - 1 and
140
+ # columns j1 .. j2 - 1.
141
+ def get_range(i1, i2, j1, j2); JAVA_METHOD; end if false
142
+
143
+ # Return an array usable as an index.
144
+ def to_indices
145
+ self
146
+ end
147
+ end
148
+
149
+ module_function
150
+
151
+ # Convenience function for converting arbitrary objects into indices.
152
+ def idx(i)
153
+ i.to_indices
154
+ end
155
+ end
@@ -0,0 +1,168 @@
1
+ # Syntactic sugar for arithmetic operations. See JBLAS::MatrixArithMixin.
2
+
3
+ # Copyright (c) 2009-2010, Mikio L. Braun and contributors
4
+ # All rights reserved.
5
+ #
6
+ # Redistribution and use in source and binary forms, with or without
7
+ # modification, are permitted provided that the following conditions are
8
+ # met:
9
+ #
10
+ # * Redistributions of source code must retain the above copyright
11
+ # notice, this list of conditions and the following disclaimer.
12
+ #
13
+ # * Redistributions in binary form must reproduce the above
14
+ # copyright notice, this list of conditions and the following
15
+ # disclaimer in the documentation and/or other materials provided
16
+ # with the distribution.
17
+ #
18
+ # * Neither the name of the Technische Universität Berlin nor the
19
+ # names of its contributors may be used to endorse or promote
20
+ # products derived from this software without specific prior
21
+ # written permission.
22
+ #
23
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24
+ # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25
+ # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26
+ # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27
+ # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28
+ # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29
+ # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30
+ # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31
+ # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32
+ # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
+
35
+ module JBLAS
36
+ # Mixin for syntactic sugar for arithmetic operations.
37
+ #
38
+ # Collected in MatrixMixin.
39
+ #
40
+ # Roughly defines the following types of operators:
41
+ # * arithmetic (+,-,*,/,...)
42
+ # * logical operations (|,&,...)
43
+ # * logical tests (==,>,<,...)
44
+ # * arithmetic operations with column and row vectors (add_column_vector, ...)
45
+ #
46
+ module MatrixArithMixin
47
+ # Add _o_ to this matrix. Works with matrices and scalars.
48
+ def +(o); add(o); end
49
+ # Subtract _o_ from this matrix. Works with matrices and scalars.
50
+ def -(o); sub(o); end
51
+ # Multiply this matrix with _o_. If _o_ is a matrix, this
52
+ # matrix-matrix multiplication. If you want element-wise
53
+ # multiplication, you must use +emul+
54
+ def *(o); mmul(o); end
55
+ # Divide this matrix by _o_.
56
+ def /(o); div(o); end
57
+ # Negating a matrix
58
+ def -@; neg; end
59
+
60
+ # Element-wise test on less-than with _o_.
61
+ def <(o); lt(o); end
62
+ # Element-wise test on less-than-or-equal with _o_.
63
+ def <=(o); le(o); end
64
+ # Element-wise test on greater-than with _o_.
65
+ def >(o); gt(o); end
66
+ # Element-wise test on greater-than-or-equal with _o_.
67
+ def >=(o); ge(o); end
68
+ # Element-wise test on equality with _o_.
69
+ def ===(o); eq(o); end
70
+
71
+ # Add matrices in-place.
72
+ def add!(s); addi(s); end
73
+ # Subtract matrices in-place.
74
+ def sub!(s); subi(s); end
75
+ # Multiply matrices element-wise in-place.
76
+ def mul!(s); muli(s); end
77
+ # Matrix multiply matrices in-place.
78
+ def mmul!(s); mmuli(s); end
79
+ # Divide matrices element-wise in-place.
80
+ def div!(s); divi(s); end
81
+ # Element-wise test for equality in-place.
82
+ def eq!(s); eqi(s); end
83
+ # Element-wise test for inequality in-place.
84
+ def ne!(s); nei(s); end
85
+ # Element-wise test for less-than in-place.
86
+ def lt!(s); lti(s); end
87
+ # Element-wise test for less-than-or-equal in-place.
88
+ def le!(s); lei(s); end
89
+ # Element-wise test for greater-than in-place.
90
+ def gt!(s); gti(s); end
91
+ # Element-wise test for greater-than-or-equal in-place.
92
+ def ge!(s); gei(s); end
93
+ # Element-wise logical "and" in-place.
94
+ def and!(s); andi(s); end
95
+ # Element-wise logical "or" in-place.
96
+ def or!(s); ori(s); end
97
+ # Element-wise logical exclusive-or in-place.
98
+ def xor!(s); xori(s); end
99
+
100
+ # Add column vector to matrix in-place.
101
+ def add_column_vector!(s); addi_column_vector(s); end
102
+ # Add row vector to matrix in-place.
103
+ def add_row_vector!(s); addi_row_vector(s); end
104
+ # Subtract column vector from matrix in-place.
105
+ def sub_column_vector!(s); subi_column_vector(s); end
106
+ # Subtract column vector from matrix in-place.
107
+ def sub_row_vector!(s); subi_row_vector(s); end
108
+ # Multiply row vector element-wise with matrix in-place.
109
+ def mul_column_vector!(s); muli_column_vector(s); end
110
+ # Divide matrix element-wise by column vector in-place.
111
+ def mul_row_vector!(s); muli_row_vector(s); end
112
+ # Multiply row vector element-wise with matrix in-place.
113
+ def div_column_vector!(s); divi_column_vector(s); end
114
+ # Divide matrix element-wise by row vector in-place.
115
+ def div_row_vector!(s); divi_row_vector(s); end
116
+
117
+ # Test on equality.
118
+ def ==(o)
119
+ #puts "== called with self = #{self.inspect}, o = #{o.inspect}"
120
+ equals(o)
121
+ end
122
+
123
+ # Element-wise logical and.
124
+ def &(o); self.and(o); end
125
+ # Element-wise logical or.
126
+ def |(o); self.or(o); end
127
+ # Element-wise logical exclusive-or.
128
+ def ^(o); self.xor(o); end
129
+
130
+ # Compute self to the power of o.
131
+ def **(o); MatrixFunctions.pow(self, o); end
132
+
133
+ def coerce(o) # :nodoc:
134
+ case o
135
+ when Numeric
136
+ return ReversedArithmetic.new(self), o
137
+ else
138
+ return self, o
139
+ end
140
+ #unless self.class === o
141
+ # [ReversedArithmetic.new(self), o]
142
+ #end
143
+ end
144
+
145
+ # Documentation for Java methods
146
+ if false
147
+ # Add matrices (see also +).
148
+ def add(o); JAVA_METHOD; end
149
+ # Add matrices in-place (see also add!).
150
+ def addi(o, result=self); JAVA_METHOD; end
151
+
152
+ # Subtract matrices (see also -).
153
+ def sub(o); JAVA_METHOD; end
154
+ # Subtract matrices in-place (see also sub!).
155
+ def subi(o, result=self); JAVA_METHOD; end
156
+
157
+ # Multiply matrices element-wise. (see also *)
158
+ def mul(o); JAVA_METHOD; end
159
+ # Multiply matrices element-wise in-place. (see also mul!)
160
+ def muli(o, result=self); JAVA_METHOD; end
161
+
162
+ # Divide matrices element-wise (see also /)
163
+ def div(o); JAVA_METHOD; end
164
+ # Divide matric element-wise in-place (see also div!)
165
+ def divi(o, result=self); JAVA_METHOD; end
166
+ end
167
+ end
168
+ end