nmatrix 0.0.3 → 0.0.4
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/.gitignore +3 -0
- data/CONTRIBUTING.md +66 -0
- data/Gemfile +1 -1
- data/History.txt +68 -10
- data/LICENSE.txt +2 -2
- data/Manifest.txt +2 -0
- data/README.rdoc +90 -69
- data/Rakefile +18 -9
- data/ext/nmatrix/data/complex.h +7 -7
- data/ext/nmatrix/data/data.cpp +2 -7
- data/ext/nmatrix/data/data.h +7 -4
- data/ext/nmatrix/data/rational.h +2 -2
- data/ext/nmatrix/data/ruby_object.h +3 -10
- data/ext/nmatrix/extconf.rb +79 -54
- data/ext/nmatrix/new_extconf.rb +11 -12
- data/ext/nmatrix/nmatrix.cpp +94 -125
- data/ext/nmatrix/nmatrix.h +38 -17
- data/ext/nmatrix/ruby_constants.cpp +2 -15
- data/ext/nmatrix/ruby_constants.h +2 -14
- data/ext/nmatrix/storage/common.cpp +2 -2
- data/ext/nmatrix/storage/common.h +2 -2
- data/ext/nmatrix/storage/dense.cpp +206 -31
- data/ext/nmatrix/storage/dense.h +5 -2
- data/ext/nmatrix/storage/list.cpp +52 -4
- data/ext/nmatrix/storage/list.h +3 -2
- data/ext/nmatrix/storage/storage.cpp +6 -6
- data/ext/nmatrix/storage/storage.h +2 -2
- data/ext/nmatrix/storage/yale.cpp +202 -49
- data/ext/nmatrix/storage/yale.h +5 -4
- data/ext/nmatrix/ttable_helper.rb +108 -108
- data/ext/nmatrix/types.h +2 -15
- data/ext/nmatrix/util/io.cpp +2 -2
- data/ext/nmatrix/util/io.h +2 -2
- data/ext/nmatrix/util/lapack.h +2 -2
- data/ext/nmatrix/util/math.cpp +14 -14
- data/ext/nmatrix/util/math.h +2 -2
- data/ext/nmatrix/util/sl_list.cpp +2 -2
- data/ext/nmatrix/util/sl_list.h +2 -2
- data/ext/nmatrix/util/util.h +2 -2
- data/lib/nmatrix.rb +13 -35
- data/lib/nmatrix/blas.rb +182 -56
- data/lib/nmatrix/io/market.rb +38 -14
- data/lib/nmatrix/io/mat5_reader.rb +393 -278
- data/lib/nmatrix/io/mat_reader.rb +121 -107
- data/lib/nmatrix/lapack.rb +59 -14
- data/lib/nmatrix/monkeys.rb +32 -30
- data/lib/nmatrix/nmatrix.rb +204 -100
- data/lib/nmatrix/nvector.rb +166 -57
- data/lib/nmatrix/shortcuts.rb +364 -231
- data/lib/nmatrix/version.rb +8 -4
- data/nmatrix.gemspec +5 -3
- data/scripts/mac-brew-gcc.sh +1 -1
- data/spec/blas_spec.rb +80 -2
- data/spec/math_spec.rb +78 -32
- data/spec/nmatrix_list_spec.rb +55 -55
- data/spec/nmatrix_spec.rb +60 -117
- data/spec/nmatrix_yale_resize_test_associations.yaml +2802 -0
- data/spec/nmatrix_yale_spec.rb +214 -198
- data/spec/nvector_spec.rb +58 -2
- data/spec/shortcuts_spec.rb +156 -32
- data/spec/slice_spec.rb +229 -178
- data/spec/spec_helper.rb +2 -2
- metadata +71 -21
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
#--
|
2
2
|
# = NMatrix
|
3
3
|
#
|
4
4
|
# A linear algebra library for scientific computation in Ruby.
|
@@ -25,63 +25,65 @@ require "packable"
|
|
25
25
|
#
|
26
26
|
# Base class for .mat file reading (Matlab files).
|
27
27
|
#
|
28
|
+
#++
|
28
29
|
|
29
30
|
require 'packable'
|
30
31
|
|
31
32
|
module NMatrix::IO::Matlab
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
}
|
33
|
+
#
|
34
|
+
# Class for parsing a .mat file stream.
|
35
|
+
#
|
36
|
+
# The full format of .mat files is available here:
|
37
|
+
# * http://www.mathworks.com/help/pdf_doc/matlab/matfile_format.pdf
|
38
|
+
#
|
39
|
+
class MatReader
|
40
|
+
MDTYPE_UNPACK_ARGS = {
|
41
|
+
:miINT8 => [Integer, {:signed => true, :bytes => 1}],
|
42
|
+
:miUINT8 => [Integer, {:signed => false, :bytes => 1}],
|
43
|
+
:miINT16 => [Integer, {:signed => true, :bytes => 2}],
|
44
|
+
:miUINT16 => [Integer, {:signed => false, :bytes => 2}],
|
45
|
+
:miINT32 => [Integer, {:signed => true, :bytes => 4}],
|
46
|
+
:miUINT32 => [Integer, {:signed => false, :bytes => 4}],
|
47
|
+
:miSINGLE => [Float, {:precision => :single, :bytes => 4, :endian => :native}],
|
48
|
+
:miDOUBLE => [Float, {:precision => :double, :bytes => 4, :endian => :native}],
|
49
|
+
:miINT64 => [Integer, {:signed => true, :bytes => 8}],
|
50
|
+
:miUINT64 => [Integer, {:signed => false, :bytes => 8}]
|
51
|
+
}
|
50
52
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
53
|
+
DTYPE_PACK_ARGS = {
|
54
|
+
:byte => [Integer, {:signed => false, :bytes => 1}],
|
55
|
+
:int8 => [Integer, {:signed => true, :bytes => 1}],
|
56
|
+
:int16 => [Integer, {:signed => true, :bytes => 2}],
|
57
|
+
:int32 => [Integer, {:signed => true, :bytes => 4}],
|
58
|
+
:int64 => [Integer, {:signed => true, :bytes => 8}],
|
59
|
+
:float32 => [Float, {:precision => :single, :bytes => 4, :endian => :native}],
|
60
|
+
:float64 => [Float, {:precision => :double, :bytes => 8, :endian => :native}],
|
61
|
+
:complex64 => [Float, {:precision => :single, :bytes => 4, :endian => :native}], #2x
|
62
|
+
:complex128 => [Float, {:precision => :double, :bytes => 8, :endian => :native}]
|
63
|
+
}
|
62
64
|
|
63
65
|
ITYPE_PACK_ARGS = {
|
64
66
|
:uint8 => [Integer, {:signed => false, :bytes => 1}],
|
65
|
-
|
66
|
-
|
67
|
-
|
67
|
+
:uint16 => [Integer, {:signed => false, :bytes => 2}],
|
68
|
+
:uint32 => [Integer, {:signed => false, :bytes => 4}],
|
69
|
+
:uint64 => [Integer, {:signed => false, :bytes => 8}],
|
68
70
|
}
|
69
71
|
|
70
|
-
|
72
|
+
NO_REPACK = [:miINT8, :miUINT8, :miINT16, :miINT32, :miSINGLE, :miDOUBLE, :miINT64]
|
71
73
|
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
74
|
+
# Convert from MATLAB dtype to NMatrix dtype.
|
75
|
+
MDTYPE_TO_DTYPE = {
|
76
|
+
:miUINT8 => :byte,
|
77
|
+
:miINT8 => :int8,
|
78
|
+
:miINT16 => :int16,
|
79
|
+
:miUINT16 => :int16,
|
80
|
+
:miINT32 => :int32,
|
81
|
+
:miUINT32 => :int32,
|
82
|
+
:miINT64 => :int64,
|
83
|
+
:miUINT64 => :int64,
|
84
|
+
:miSINGLE => :float32,
|
85
|
+
:miDOUBLE => :float64
|
86
|
+
}
|
85
87
|
|
86
88
|
MDTYPE_TO_ITYPE = {
|
87
89
|
:miUINT8 => :uint8,
|
@@ -94,69 +96,81 @@ module NMatrix::IO::Matlab
|
|
94
96
|
:miUINT64 => :uint64
|
95
97
|
}
|
96
98
|
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
99
|
+
# Before release v7.1 (release 14) matlab (TM) used the system
|
100
|
+
# default character encoding scheme padded out to 16-bits. Release 14
|
101
|
+
# and later use Unicode. When saving character data, R14 checks if it
|
102
|
+
# can be encoded in 7-bit ascii, and saves in that format if so.
|
103
|
+
MDTYPES = [
|
104
|
+
nil,
|
105
|
+
:miINT8,
|
106
|
+
:miUINT8,
|
107
|
+
:miINT16,
|
108
|
+
:miUINT16,
|
109
|
+
:miINT32,
|
110
|
+
:miUINT32,
|
111
|
+
:miSINGLE,
|
112
|
+
nil,
|
113
|
+
:miDOUBLE,
|
114
|
+
nil,
|
115
|
+
nil,
|
116
|
+
:miINT64,
|
117
|
+
:miUINT64,
|
118
|
+
:miMATRIX,
|
119
|
+
:miCOMPRESSED,
|
120
|
+
:miUTF8,
|
121
|
+
:miUTF16,
|
122
|
+
:miUTF32
|
123
|
+
]
|
124
|
+
|
125
|
+
MCLASSES = [
|
126
|
+
nil,
|
127
|
+
:mxCELL,
|
128
|
+
:mxSTRUCT,
|
129
|
+
:mxOBJECT,
|
130
|
+
:mxCHAR,
|
131
|
+
:mxSPARSE,
|
132
|
+
:mxDOUBLE,
|
133
|
+
:mxSINGLE,
|
134
|
+
:mxINT8,
|
135
|
+
:mxUINT8,
|
136
|
+
:mxINT16,
|
137
|
+
:mxUINT16,
|
138
|
+
:mxINT32,
|
139
|
+
:mxUINT32,
|
140
|
+
:mxINT64,
|
141
|
+
:mxUINT64,
|
142
|
+
:mxFUNCTION,
|
143
|
+
:mxOPAQUE,
|
144
|
+
:mxOBJECT_CLASS_FROM_MATRIX_H
|
145
|
+
]
|
146
|
+
|
147
|
+
attr_reader :byte_order
|
144
148
|
|
145
|
-
|
149
|
+
#
|
150
|
+
# call-seq:
|
151
|
+
# new(stream, options = {}) -> MatReader
|
152
|
+
#
|
153
|
+
# * *Raises* :
|
154
|
+
# - +ArgumentError+ -> First argument must be IO.
|
155
|
+
#
|
156
|
+
def initialize(stream, options = {})
|
157
|
+
raise ArgumentError, 'First arg must be IO.' unless stream.is_a?(::IO)
|
146
158
|
|
147
|
-
|
148
|
-
|
159
|
+
@stream = stream
|
160
|
+
@byte_order = options[:byte_order] || guess_byte_order
|
161
|
+
end
|
149
162
|
|
150
|
-
|
151
|
-
|
152
|
-
|
163
|
+
#
|
164
|
+
# call-seq:
|
165
|
+
# guess_byte_order -> Symbol
|
166
|
+
#
|
167
|
+
def guess_byte_order
|
168
|
+
# Assume native, since we don't know what type of file we have.
|
169
|
+
:native
|
170
|
+
end
|
153
171
|
|
154
|
-
|
155
|
-
# Assume native, since we don't know what type of file we have.
|
156
|
-
:native
|
157
|
-
end
|
172
|
+
protected
|
158
173
|
|
159
|
-
|
160
|
-
|
161
|
-
end
|
174
|
+
attr_reader :stream
|
175
|
+
end
|
162
176
|
end
|
data/lib/nmatrix/lapack.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
#--
|
1
2
|
# = NMatrix
|
2
3
|
#
|
3
4
|
# A linear algebra library for scientific computation in Ruby.
|
@@ -8,8 +9,8 @@
|
|
8
9
|
#
|
9
10
|
# == Copyright Information
|
10
11
|
#
|
11
|
-
# SciRuby is Copyright (c) 2010 -
|
12
|
-
# NMatrix is Copyright (c)
|
12
|
+
# SciRuby is Copyright (c) 2010 - 2013, Ruby Science Foundation
|
13
|
+
# NMatrix is Copyright (c) 2013, Ruby Science Foundation
|
13
14
|
#
|
14
15
|
# Please see LICENSE.txt for additional copyright notices.
|
15
16
|
#
|
@@ -29,31 +30,60 @@
|
|
29
30
|
#
|
30
31
|
# Note: most of these functions are borrowed from ATLAS, which is available under a BSD-
|
31
32
|
# style license.
|
33
|
+
#++
|
32
34
|
|
33
35
|
class NMatrix
|
34
36
|
module LAPACK
|
35
37
|
class << self
|
36
|
-
|
37
|
-
#
|
38
|
+
#
|
39
|
+
# call-seq:
|
40
|
+
# clapack_gesv(order, n, nrhs, a, lda, ipiv, b, ldb) -> NMatrix
|
41
|
+
#
|
42
|
+
# Computes the solution to a system of linear equations
|
38
43
|
# A * X = B,
|
39
44
|
# where A is an N-by-N matrix and X and B are N-by-NRHS matrices.
|
40
|
-
#
|
41
|
-
#
|
42
|
-
#
|
45
|
+
#
|
46
|
+
# The LU factorization used to factor A is dependent on the +order+
|
47
|
+
# parameter, as detailed in the leading comments of clapack_getrf.
|
48
|
+
#
|
49
|
+
# The factored form of A is then used solve the system of equations
|
50
|
+
# A * X = B.
|
51
|
+
#
|
43
52
|
# A is overwritten with the appropriate LU factorization, and B, which
|
44
53
|
# contains B on input, is overwritten with the solution X on output.
|
45
54
|
#
|
46
55
|
# From ATLAS 3.8.0.
|
47
56
|
#
|
48
|
-
# Note: Because this function is implemented in Ruby, the ATLAS lib
|
49
|
-
# never called! For float32, float64, complex64, and
|
50
|
-
# versions of getrf and getrs *will* be called.
|
57
|
+
# Note: Because this function is implemented in Ruby, the ATLAS lib
|
58
|
+
# version is never called! For float32, float64, complex64, and
|
59
|
+
# complex128, the ATLAS lib versions of getrf and getrs *will* be called.
|
60
|
+
#
|
61
|
+
# * *Arguments* :
|
62
|
+
# - +order+ ->
|
63
|
+
# - +n+ ->
|
64
|
+
# - +nrhs+ ->
|
65
|
+
# - +a+ ->
|
66
|
+
# - +lda+ ->
|
67
|
+
# - +ipiv+ ->
|
68
|
+
# - +b+ ->
|
69
|
+
# - +ldb+ ->
|
70
|
+
# * *Returns* :
|
71
|
+
# -
|
72
|
+
# * *Raises* :
|
73
|
+
# - ++ ->
|
74
|
+
#
|
51
75
|
def clapack_gesv(order, n, nrhs, a, lda, ipiv, b, ldb)
|
52
76
|
clapack_getrf(order, n, n, a, lda, ipiv)
|
53
77
|
clapack_getrs(order, :no_transpose, n, nrhs, a, lda, ipiv, b, ldb)
|
54
78
|
end
|
55
79
|
|
56
|
-
#
|
80
|
+
#
|
81
|
+
# call-seq:
|
82
|
+
# clapack_posv(order, uplo, n ,nrhs, a, lda, b, ldb) -> ...
|
83
|
+
#
|
84
|
+
# TODO Complete this description.
|
85
|
+
#
|
86
|
+
# Computes the solution to a real system of linear equations
|
57
87
|
# A * X = B,
|
58
88
|
# where A is an N-by-N symmetric positive definite matrix and X and B
|
59
89
|
# are N-by-NRHS matrices.
|
@@ -67,9 +97,24 @@ class NMatrix
|
|
67
97
|
#
|
68
98
|
# From ATLAS 3.8.0.
|
69
99
|
#
|
70
|
-
# Note: Because this function is implemented in Ruby, the ATLAS lib
|
71
|
-
# never called! For float32, float64, complex64, and
|
72
|
-
# versions of potrf and potrs *will* be called.
|
100
|
+
# Note: Because this function is implemented in Ruby, the ATLAS lib
|
101
|
+
# version is never called! For float32, float64, complex64, and
|
102
|
+
# complex128, the ATLAS lib versions of potrf and potrs *will* be called.
|
103
|
+
#
|
104
|
+
# * *Arguments* :
|
105
|
+
# - +order+ ->
|
106
|
+
# - +uplo+ ->
|
107
|
+
# - +n+ ->
|
108
|
+
# - +nrhs+ ->
|
109
|
+
# - +a+ ->
|
110
|
+
# - +lda+ ->
|
111
|
+
# - +b+ ->
|
112
|
+
# - +ldb+ ->
|
113
|
+
# * *Returns* :
|
114
|
+
# -
|
115
|
+
# * *Raises* :
|
116
|
+
# - ++ ->
|
117
|
+
#
|
73
118
|
def clapack_posv(order, uplo, n, nrhs, a, lda, b, ldb)
|
74
119
|
clapack_potrf(order, uplo, n, a, lda)
|
75
120
|
clapack_potrs(order, uplo, n, nrhs, a, lda, b, ldb)
|
data/lib/nmatrix/monkeys.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
#--
|
1
2
|
# = NMatrix
|
2
3
|
#
|
3
4
|
# A linear algebra library for scientific computation in Ruby.
|
@@ -8,8 +9,8 @@
|
|
8
9
|
#
|
9
10
|
# == Copyright Information
|
10
11
|
#
|
11
|
-
# SciRuby is Copyright (c) 2010 -
|
12
|
-
# NMatrix is Copyright (c)
|
12
|
+
# SciRuby is Copyright (c) 2010 - 2013, Ruby Science Foundation
|
13
|
+
# NMatrix is Copyright (c) 2013, Ruby Science Foundation
|
13
14
|
#
|
14
15
|
# Please see LICENSE.txt for additional copyright notices.
|
15
16
|
#
|
@@ -23,52 +24,53 @@
|
|
23
24
|
# == monkeys.rb
|
24
25
|
#
|
25
26
|
# Ruby core extensions for NMatrix.
|
27
|
+
#++
|
26
28
|
|
27
29
|
#######################
|
28
30
|
# Classes and Modules #
|
29
31
|
#######################
|
30
32
|
|
31
33
|
class Array
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
34
|
+
# Convert a Ruby Array to an NMatrix.
|
35
|
+
#
|
36
|
+
# You must provide a shape for the matrix as the first argument.
|
37
|
+
#
|
38
|
+
# == Arguments:
|
39
|
+
# <tt>shape</tt> :: Array describing matrix dimensions (or Fixnum for square) -- REQUIRED!
|
40
|
+
# <tt>dtype</tt> :: Override data type (e.g., to store a Float as :float32 instead of :float64) -- optional.
|
41
|
+
# <tt>stype</tt> :: Optional storage type (defaults to :dense)
|
42
|
+
def to_nm(shape, dtype = nil, stype = :dense)
|
43
|
+
dtype ||=
|
44
|
+
case self[0]
|
45
|
+
when Fixnum then :int64
|
46
|
+
when Float then :float64
|
47
|
+
when Rational then :rational128
|
48
|
+
when Complex then :complex128
|
49
|
+
end
|
50
|
+
|
51
|
+
matrix = NMatrix.new(:dense, shape, self, dtype)
|
52
|
+
|
53
|
+
if stype != :dense then matrix.cast(stype, dtype) else matrix end
|
54
|
+
end
|
53
55
|
|
54
56
|
unless method_defined?(:max)
|
55
|
-
def max
|
57
|
+
def max #:nodoc:
|
56
58
|
self.inject(self.first) { |m, n| if n > m then n else m end }
|
57
59
|
end
|
58
60
|
end
|
59
61
|
|
60
62
|
unless method_defined?(:min)
|
61
|
-
def min
|
63
|
+
def min #:nodoc:
|
62
64
|
self.inject(self.first) { |m, n| if n < m then n else m end }
|
63
65
|
end
|
64
66
|
end
|
65
67
|
end
|
66
68
|
|
67
|
-
class Object
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
69
|
+
class Object #:nodoc:
|
70
|
+
def returning(value)
|
71
|
+
yield(value)
|
72
|
+
value
|
73
|
+
end
|
72
74
|
end
|
73
75
|
|
74
76
|
class String #:nodoc:
|