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.
- data/lib/jblas.rb +325 -0
- data/lib/jblas/0readme.rb +69 -0
- data/lib/jblas/arith.rb +62 -0
- data/lib/jblas/complex.rb +92 -0
- data/lib/jblas/errors.rb +52 -0
- data/lib/jblas/extensions.rb +114 -0
- data/lib/jblas/functions.rb +404 -0
- data/lib/jblas/java.rb +75 -0
- data/lib/jblas/jblas-1.1.1.jar +0 -0
- data/lib/jblas/matrix_mixin.rb +64 -0
- data/lib/jblas/mixin_access.rb +155 -0
- data/lib/jblas/mixin_arith.rb +168 -0
- data/lib/jblas/mixin_class.rb +80 -0
- data/lib/jblas/mixin_complex_matrix.rb +64 -0
- data/lib/jblas/mixin_convert.rb +104 -0
- data/lib/jblas/mixin_enum.rb +83 -0
- data/lib/jblas/mixin_general.rb +202 -0
- data/lib/jblas/proxies.rb +55 -0
- metadata +81 -0
@@ -0,0 +1,80 @@
|
|
1
|
+
# Mixin for class methods of the matrix classes. Defines JBLAS::MatrixClassMixin.
|
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 the Matrix classes. These basically add the [] construction
|
37
|
+
# method (such that you can say DoubleMatrix[1,2,3]
|
38
|
+
module MatrixClassMixin
|
39
|
+
# Create a new matrix. For example, you can say DoubleMatrix[1,2,3].
|
40
|
+
#
|
41
|
+
# See also from_array.
|
42
|
+
def [](*data)
|
43
|
+
from_array data
|
44
|
+
end
|
45
|
+
|
46
|
+
# Create a new matrix. There are two ways to use this function
|
47
|
+
#
|
48
|
+
# <b>pass an array</b>::
|
49
|
+
# Constructs a column vector. For example: DoubleMatrix.from_array 1, 2, 3
|
50
|
+
# <b>pass an array of arrays</b>::
|
51
|
+
# Constructs a matrix, inner arrays are rows. For
|
52
|
+
# example: DoubleMatrix.from_array [[1,2,3],[4,5,6]]
|
53
|
+
#
|
54
|
+
# See also [], JBLAS#mat
|
55
|
+
def from_array(data)
|
56
|
+
n = data.length
|
57
|
+
if data.reject{|l| Numeric === l}.size == 0
|
58
|
+
a = self.new(n, 1)
|
59
|
+
(0...data.length).each do |i|
|
60
|
+
a[i, 0] = data[i]
|
61
|
+
end
|
62
|
+
return a
|
63
|
+
else
|
64
|
+
begin
|
65
|
+
lengths = data.collect{|v| v.length}
|
66
|
+
rescue
|
67
|
+
raise "All columns must be arrays"
|
68
|
+
end
|
69
|
+
raise "All columns must have equal length!" if lengths.min < lengths.max
|
70
|
+
a = self.new(n, lengths.max)
|
71
|
+
for i in 0...n
|
72
|
+
for j in 0...lengths.max
|
73
|
+
a[i,j] = data[i][j]
|
74
|
+
end
|
75
|
+
end
|
76
|
+
return a
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
# Some extra methods added to complex matrices. Defines JBLAS::ComplexMatrixMixin.
|
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 defining some extra methods for complex matrices.
|
37
|
+
#
|
38
|
+
# Collected in MatrixMixin.
|
39
|
+
module ComplexMatrixMixin
|
40
|
+
# Compute the hermitian transpose (transpose and complex conjugate). See hermitian.
|
41
|
+
def h; hermitian; end
|
42
|
+
|
43
|
+
# Compute the hermitian transpose (transpoe and complex conjugate).
|
44
|
+
def hermitian; JAVA_METHOD; end if false
|
45
|
+
|
46
|
+
# Compute complex conjugate.
|
47
|
+
def conj; JAVA_METHOD; end if false
|
48
|
+
|
49
|
+
# Compute complex conjugate (in-place).
|
50
|
+
def conj!; JAVA_METHOD; end if false
|
51
|
+
|
52
|
+
# Compute complex conjugate scalar product (self.h * other).
|
53
|
+
def dotu(other); JAVA_METHOD; end if false
|
54
|
+
|
55
|
+
# Compute transposed scalar product (self.t * other).
|
56
|
+
def dotc; JAVA_METHOD; end if false
|
57
|
+
|
58
|
+
# Get only the real part.
|
59
|
+
def real; JAVA_METHOD; end if false
|
60
|
+
|
61
|
+
# Get only the imaginary part.
|
62
|
+
def imag; JAVA_METHOD; end if false
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,104 @@
|
|
1
|
+
# Methods added to the Java classes are encoded as mixins in this file.
|
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 conversion options.
|
37
|
+
#
|
38
|
+
# Collected in MatrixMixin.
|
39
|
+
module MatrixConvertMixin
|
40
|
+
# Convert this matrix to a string.
|
41
|
+
#
|
42
|
+
# This methods takes a few extra arguments to control how the result looks
|
43
|
+
# like.
|
44
|
+
#
|
45
|
+
# +fmt+ is a format as used by sprintf, +coljoin+ is the string used to
|
46
|
+
# join column, +rowjoin+ is what is used to join rows. For example,
|
47
|
+
#
|
48
|
+
# x.to_s('%.1f', ' ', "\n")
|
49
|
+
#
|
50
|
+
# Returns a matrix where columns are separated by spaces, rows by newlines
|
51
|
+
# and each element is shown with one digit after the comma.
|
52
|
+
def to_s(fmt=nil, coljoin=', ', rowjoin='; ')
|
53
|
+
if fmt
|
54
|
+
x = rows_to_a
|
55
|
+
'[' + x.map do |r|
|
56
|
+
if Enumerable === r
|
57
|
+
r.map {|e| sprintf(fmt, e)}.join(coljoin)
|
58
|
+
else
|
59
|
+
sprintf(fmt, r)
|
60
|
+
end
|
61
|
+
end.join(rowjoin) + ']'
|
62
|
+
else
|
63
|
+
toString
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
# Return an a representation of this object.
|
68
|
+
def inspect
|
69
|
+
s = "<#{self.class} of size #{rows} #{columns}: #{to_s}>"
|
70
|
+
end
|
71
|
+
|
72
|
+
def rows_to_a
|
73
|
+
(0...rows).map do |i|
|
74
|
+
row(i).to_a
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def columns_to_a
|
79
|
+
(0...columns).map do |j|
|
80
|
+
column(j).to_a
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
# Convert the matrix to an array.
|
85
|
+
#
|
86
|
+
# If the matrix
|
87
|
+
def to_ary
|
88
|
+
to_a
|
89
|
+
end
|
90
|
+
|
91
|
+
# Return the matrix as an Enumerable (just returns self).
|
92
|
+
def to_a #:nodoc:
|
93
|
+
data.to_a
|
94
|
+
end
|
95
|
+
|
96
|
+
def to_mat #:nodoc:
|
97
|
+
self
|
98
|
+
end
|
99
|
+
|
100
|
+
def to_matrix
|
101
|
+
self
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
# Mixin for making matrices behave like enumerables. Defines JBLAS::MatrixEnumMixin.
|
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 collecting enumerable operations.
|
37
|
+
#
|
38
|
+
# Collected in MatrixMixin.
|
39
|
+
module MatrixEnumMixin
|
40
|
+
include Enumerable
|
41
|
+
|
42
|
+
# Iterate over rows.
|
43
|
+
def each_row
|
44
|
+
(0...rows).each do |i|
|
45
|
+
yield row(i)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
# Iterate over columns.
|
50
|
+
def each_column
|
51
|
+
(0...columns).each do |j|
|
52
|
+
yield column(j)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
# Each iterates over each element, going down rows first.
|
57
|
+
def each
|
58
|
+
(0...length).each do |i|
|
59
|
+
yield get(i)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
# Map each element.
|
64
|
+
#
|
65
|
+
# Returns a new matrix of the same type. This means that the block
|
66
|
+
# must return something which can again be stored in the matrix.
|
67
|
+
def map(&block)
|
68
|
+
return dup.map!(&block)
|
69
|
+
end
|
70
|
+
|
71
|
+
# Map each element and store the result in the matrix.
|
72
|
+
#
|
73
|
+
# Note that the result must be again something
|
74
|
+
# which can be stored in the matrix. Otherwise you should do an
|
75
|
+
# to_a first.
|
76
|
+
def map!(&block)
|
77
|
+
(0...length).each do |i|
|
78
|
+
put(i, block.call(get(i)))
|
79
|
+
end
|
80
|
+
self
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,202 @@
|
|
1
|
+
# General matrix operations. Defines the JBLAS::MatrixGeneralMixin.
|
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 'jblas/proxies'
|
36
|
+
|
37
|
+
module JBLAS
|
38
|
+
# Mixin for general operations not fitting in any other category.
|
39
|
+
#
|
40
|
+
# Collected in MatrixMixin.
|
41
|
+
module MatrixGeneralMixin
|
42
|
+
# Transpose the matrix. You obtain a transposed view of the matrix.
|
43
|
+
# Some operations are not possible on such views, for example
|
44
|
+
# most in-place operations. For such, do a +compact+ first.
|
45
|
+
def t; transpose; end
|
46
|
+
|
47
|
+
if false
|
48
|
+
# Transpose the matrix. See t.
|
49
|
+
def transpose
|
50
|
+
JAVA_METHOD
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
# Get the size of the matrix as <tt>[rows, columns]</tt>
|
55
|
+
def dims
|
56
|
+
[rows, columns]
|
57
|
+
end
|
58
|
+
|
59
|
+
# Get the total number of elements. Synonymous to length.
|
60
|
+
def size
|
61
|
+
length
|
62
|
+
end
|
63
|
+
|
64
|
+
|
65
|
+
# Check whether the column index +i+ is valid.
|
66
|
+
def check_column_index(i)
|
67
|
+
unless 0 <= i and i < columns
|
68
|
+
raise IndexError, "column index out of bounds"
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
# Check whether the row index +i+ is valid.
|
73
|
+
def check_row_index(i)
|
74
|
+
unless 0 <= i and i < rows
|
75
|
+
raise IndexError, "column index out of bounds"
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
# Returns true if the matrix is square and symmetric.
|
80
|
+
def symmetric?
|
81
|
+
square? and self.sub(self.t).normmax < 1e-6
|
82
|
+
end
|
83
|
+
|
84
|
+
# Compute the inverse of self.
|
85
|
+
def inv
|
86
|
+
unless square?
|
87
|
+
raise ArgumentError, 'Inverses can only be computed from square ' +
|
88
|
+
'matrices. Use solve instead!'
|
89
|
+
end
|
90
|
+
self.solve(self.class.eye(rows))
|
91
|
+
end
|
92
|
+
|
93
|
+
# Solve the linear equation self * x = b.
|
94
|
+
def solve(b)
|
95
|
+
if symmetric?
|
96
|
+
Solve.solve_symmetric(self, b)
|
97
|
+
else
|
98
|
+
Solve.solve(self, b)
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
# Return a new matrix which consists of the _self_ and _y_ side by
|
103
|
+
# side. In general the hcat method should be used sparingly as it
|
104
|
+
# creates a new matrix and copies everything on each use. You
|
105
|
+
# should always ask yourself if an array of vectors or matrices
|
106
|
+
# doesn't serve you better. That said, you _can_ do funny things
|
107
|
+
# with +inject+. For example,
|
108
|
+
#
|
109
|
+
# a = mat[1,2,3]
|
110
|
+
# [a, 2*a, 3*a].inject {|s,x| s = s.hcat(x)}
|
111
|
+
# => 1.0 2.0 3.0
|
112
|
+
# 2.0 4.0 6.0
|
113
|
+
# 3.0 6.0 9.0
|
114
|
+
def hcat(y)
|
115
|
+
unless self.dims[0] == y.dims[0]
|
116
|
+
raise ArgumentError, "Matrices must have same number of rows"
|
117
|
+
end
|
118
|
+
DoubleMatrix.concat_horizontally(self, y)
|
119
|
+
end
|
120
|
+
|
121
|
+
# Return a new matrix which consists of the _self_ on top of _y_.
|
122
|
+
# In general the hcat methods should be used sparingly. You
|
123
|
+
# should always ask yourself if an array of vectors or matrices
|
124
|
+
# doesn't serve you better. See also hcat.
|
125
|
+
def vcat(y)
|
126
|
+
unless self.dims[1] == y.dims[1]
|
127
|
+
raise ArgumentError, "Matrices must have same number of columns"
|
128
|
+
end
|
129
|
+
DoubleMatrix.concat_vertically(self, y)
|
130
|
+
end
|
131
|
+
|
132
|
+
# Returns a proxy of the matrix for which '*' is defined as elementwise.
|
133
|
+
#
|
134
|
+
# That is:
|
135
|
+
# * a * b => matrix multiplication
|
136
|
+
# * a.e * b => element wise multiplication
|
137
|
+
#
|
138
|
+
# For extra coolness, try writing it as "a .e* b", such that it looks
|
139
|
+
# more like the ".e" belongs to the operator, not the object. (Not sure
|
140
|
+
# whether this is really worth it, though ;) )
|
141
|
+
def e
|
142
|
+
JBLAS::MatrixElementWiseProxy.new(self)
|
143
|
+
end
|
144
|
+
|
145
|
+
# Returns a proxy for the matrix for which '*' is defined as the scalar
|
146
|
+
# product. See also +e+
|
147
|
+
#
|
148
|
+
# * a * b => matrix multiplication (a.mmul(b))
|
149
|
+
# * a .d* b => scalar product (a.dot(b))
|
150
|
+
def d
|
151
|
+
MatrixDotProxy.new(self)
|
152
|
+
end
|
153
|
+
|
154
|
+
if false
|
155
|
+
# Returns true if self is a vector
|
156
|
+
def vector?; JAVA_METHOD; end
|
157
|
+
|
158
|
+
# Returns true if self is a row vector.
|
159
|
+
def row_vector?; JAVA_METHOD; end
|
160
|
+
|
161
|
+
# Returns true if self is a column vector.
|
162
|
+
def column_vector?; JAVA_METHOD; end
|
163
|
+
|
164
|
+
# Returns true if self is a scalar.
|
165
|
+
def scalar?; JAVA_METHOD; end
|
166
|
+
|
167
|
+
# Returns the first entry of self.
|
168
|
+
def scalar; JAVA_METHOD; end
|
169
|
+
end
|
170
|
+
|
171
|
+
# Return a vector as row vector.
|
172
|
+
def as_row
|
173
|
+
unless vector?
|
174
|
+
self
|
175
|
+
else
|
176
|
+
row_vector? ? self : self.t
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
# Return a column vector.
|
181
|
+
def as_column
|
182
|
+
unless vector?
|
183
|
+
self
|
184
|
+
else
|
185
|
+
column_vector? ? self : self.t
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
# Save as ascii (tab-separated list, every row is a line)
|
190
|
+
def save_ascii(fn)
|
191
|
+
o = open(fn, 'w')
|
192
|
+
rows.times do |i|
|
193
|
+
columns.times do |j|
|
194
|
+
o.print get(i,j)
|
195
|
+
o.print "\t" if j < columns - 1
|
196
|
+
end
|
197
|
+
o.puts
|
198
|
+
end
|
199
|
+
o.close
|
200
|
+
end
|
201
|
+
end
|
202
|
+
end
|