nmatrix 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. data/.gitignore +3 -0
  2. data/CONTRIBUTING.md +66 -0
  3. data/Gemfile +1 -1
  4. data/History.txt +68 -10
  5. data/LICENSE.txt +2 -2
  6. data/Manifest.txt +2 -0
  7. data/README.rdoc +90 -69
  8. data/Rakefile +18 -9
  9. data/ext/nmatrix/data/complex.h +7 -7
  10. data/ext/nmatrix/data/data.cpp +2 -7
  11. data/ext/nmatrix/data/data.h +7 -4
  12. data/ext/nmatrix/data/rational.h +2 -2
  13. data/ext/nmatrix/data/ruby_object.h +3 -10
  14. data/ext/nmatrix/extconf.rb +79 -54
  15. data/ext/nmatrix/new_extconf.rb +11 -12
  16. data/ext/nmatrix/nmatrix.cpp +94 -125
  17. data/ext/nmatrix/nmatrix.h +38 -17
  18. data/ext/nmatrix/ruby_constants.cpp +2 -15
  19. data/ext/nmatrix/ruby_constants.h +2 -14
  20. data/ext/nmatrix/storage/common.cpp +2 -2
  21. data/ext/nmatrix/storage/common.h +2 -2
  22. data/ext/nmatrix/storage/dense.cpp +206 -31
  23. data/ext/nmatrix/storage/dense.h +5 -2
  24. data/ext/nmatrix/storage/list.cpp +52 -4
  25. data/ext/nmatrix/storage/list.h +3 -2
  26. data/ext/nmatrix/storage/storage.cpp +6 -6
  27. data/ext/nmatrix/storage/storage.h +2 -2
  28. data/ext/nmatrix/storage/yale.cpp +202 -49
  29. data/ext/nmatrix/storage/yale.h +5 -4
  30. data/ext/nmatrix/ttable_helper.rb +108 -108
  31. data/ext/nmatrix/types.h +2 -15
  32. data/ext/nmatrix/util/io.cpp +2 -2
  33. data/ext/nmatrix/util/io.h +2 -2
  34. data/ext/nmatrix/util/lapack.h +2 -2
  35. data/ext/nmatrix/util/math.cpp +14 -14
  36. data/ext/nmatrix/util/math.h +2 -2
  37. data/ext/nmatrix/util/sl_list.cpp +2 -2
  38. data/ext/nmatrix/util/sl_list.h +2 -2
  39. data/ext/nmatrix/util/util.h +2 -2
  40. data/lib/nmatrix.rb +13 -35
  41. data/lib/nmatrix/blas.rb +182 -56
  42. data/lib/nmatrix/io/market.rb +38 -14
  43. data/lib/nmatrix/io/mat5_reader.rb +393 -278
  44. data/lib/nmatrix/io/mat_reader.rb +121 -107
  45. data/lib/nmatrix/lapack.rb +59 -14
  46. data/lib/nmatrix/monkeys.rb +32 -30
  47. data/lib/nmatrix/nmatrix.rb +204 -100
  48. data/lib/nmatrix/nvector.rb +166 -57
  49. data/lib/nmatrix/shortcuts.rb +364 -231
  50. data/lib/nmatrix/version.rb +8 -4
  51. data/nmatrix.gemspec +5 -3
  52. data/scripts/mac-brew-gcc.sh +1 -1
  53. data/spec/blas_spec.rb +80 -2
  54. data/spec/math_spec.rb +78 -32
  55. data/spec/nmatrix_list_spec.rb +55 -55
  56. data/spec/nmatrix_spec.rb +60 -117
  57. data/spec/nmatrix_yale_resize_test_associations.yaml +2802 -0
  58. data/spec/nmatrix_yale_spec.rb +214 -198
  59. data/spec/nvector_spec.rb +58 -2
  60. data/spec/shortcuts_spec.rb +156 -32
  61. data/spec/slice_spec.rb +229 -178
  62. data/spec/spec_helper.rb +2 -2
  63. metadata +71 -21
@@ -1,4 +1,4 @@
1
- require "packable"
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
- # Class for parsing a .mat file stream.
33
- #
34
- # The full format of .mat files is available here:
35
- # * http://www.mathworks.com/help/pdf_doc/matlab/matfile_format.pdf
36
- #
37
- class MatReader
38
- MDTYPE_UNPACK_ARGS = {
39
- :miINT8 => [Integer, {:signed => true, :bytes => 1}],
40
- :miUINT8 => [Integer, {:signed => false, :bytes => 1}],
41
- :miINT16 => [Integer, {:signed => true, :bytes => 2}],
42
- :miUINT16 => [Integer, {:signed => false, :bytes => 2}],
43
- :miINT32 => [Integer, {:signed => true, :bytes => 4}],
44
- :miUINT32 => [Integer, {:signed => false, :bytes => 4}],
45
- :miSINGLE => [Float, {:precision => :single, :bytes => 4, :endian => :native}],
46
- :miDOUBLE => [Float, {:precision => :double, :bytes => 4, :endian => :native}],
47
- :miINT64 => [Integer, {:signed => true, :bytes => 8}],
48
- :miUINT64 => [Integer, {:signed => false, :bytes => 8}]
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
- DTYPE_PACK_ARGS = {
52
- :byte => [Integer, {:signed => false, :bytes => 1}],
53
- :int8 => [Integer, {:signed => true, :bytes => 1}],
54
- :int16 => [Integer, {:signed => true, :bytes => 2}],
55
- :int32 => [Integer, {:signed => true, :bytes => 4}],
56
- :int64 => [Integer, {:signed => true, :bytes => 8}],
57
- :float32 => [Float, {:precision => :single, :bytes => 4, :endian => :native}],
58
- :float64 => [Float, {:precision => :double, :bytes => 8, :endian => :native}],
59
- :complex64 => [Float, {:precision => :single, :bytes => 4, :endian => :native}], #2x
60
- :complex128 => [Float, {:precision => :double, :bytes => 8, :endian => :native}]
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
- :uint16 => [Integer, {:signed => false, :bytes => 2}],
66
- :uint32 => [Integer, {:signed => false, :bytes => 4}],
67
- :uint64 => [Integer, {:signed => false, :bytes => 8}],
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
- NO_REPACK = [:miINT8, :miUINT8, :miINT16, :miINT32, :miSINGLE, :miDOUBLE, :miINT64]
72
+ NO_REPACK = [:miINT8, :miUINT8, :miINT16, :miINT32, :miSINGLE, :miDOUBLE, :miINT64]
71
73
 
72
- # Convert from MATLAB dtype to NMatrix dtype.
73
- MDTYPE_TO_DTYPE = {
74
- :miUINT8 => :byte,
75
- :miINT8 => :int8,
76
- :miINT16 => :int16,
77
- :miUINT16 => :int16,
78
- :miINT32 => :int32,
79
- :miUINT32 => :int32,
80
- :miINT64 => :int64,
81
- :miUINT64 => :int64,
82
- :miSINGLE => :float32,
83
- :miDOUBLE => :float64
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
- # Before release v7.1 (release 14) matlab (TM) used the system
98
- # default character encoding scheme padded out to 16-bits. Release 14
99
- # and later use Unicode. When saving character data, R14 checks if it
100
- # can be encoded in 7-bit ascii, and saves in that format if so.
101
- MDTYPES = [
102
- nil,
103
- :miINT8,
104
- :miUINT8,
105
- :miINT16,
106
- :miUINT16,
107
- :miINT32,
108
- :miUINT32,
109
- :miSINGLE,
110
- nil,
111
- :miDOUBLE,
112
- nil,
113
- nil,
114
- :miINT64,
115
- :miUINT64,
116
- :miMATRIX,
117
- :miCOMPRESSED,
118
- :miUTF8,
119
- :miUTF16,
120
- :miUTF32
121
- ]
122
-
123
- MCLASSES = [
124
- nil,
125
- :mxCELL,
126
- :mxSTRUCT,
127
- :mxOBJECT,
128
- :mxCHAR,
129
- :mxSPARSE,
130
- :mxDOUBLE,
131
- :mxSINGLE,
132
- :mxINT8,
133
- :mxUINT8,
134
- :mxINT16,
135
- :mxUINT16,
136
- :mxINT32,
137
- :mxUINT32,
138
- :mxINT64,
139
- :mxUINT64,
140
- :mxFUNCTION,
141
- :mxOPAQUE,
142
- :mxOBJECT_CLASS_FROM_MATRIX_H
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
- attr_reader :byte_order
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
- def initialize(stream, options = {})
148
- raise ArgumentError, 'First arg must be IO.' unless stream.is_a?(::IO)
159
+ @stream = stream
160
+ @byte_order = options[:byte_order] || guess_byte_order
161
+ end
149
162
 
150
- @stream = stream
151
- @byte_order = options[:byte_order] || guess_byte_order
152
- end
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
- def guess_byte_order
155
- # Assume native, since we don't know what type of file we have.
156
- :native
157
- end
172
+ protected
158
173
 
159
- protected
160
- attr_reader :stream
161
- end
174
+ attr_reader :stream
175
+ end
162
176
  end
@@ -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 - 2012, Ruby Science Foundation
12
- # NMatrix is Copyright (c) 2012, Ruby Science Foundation
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
- # clapack_gesv computes the solution to a system of linear equations
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
- # The LU factorization used to factor A is dependent on the Order parameter,
41
- # as detailed in the leading comments of clapack_getrf.
42
- # The factored form of A is then used solve the system of equations A * X = B.
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 version is
49
- # never called! For float32, float64, complex64, and complex128, the ATLAS lib
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
- # clapack_posv computes the solution to a real system of linear equations
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 version is
71
- # never called! For float32, float64, complex64, and complex128, the ATLAS lib
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)
@@ -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 - 2012, Ruby Science Foundation
12
- # NMatrix is Copyright (c) 2012, Ruby Science Foundation
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
- # Convert a Ruby Array to an NMatrix.
33
- #
34
- # You must provide a shape for the matrix as the first argument.
35
- #
36
- # == Arguments:
37
- # <tt>shape</tt> :: Array describing matrix dimensions (or Fixnum for square) -- REQUIRED!
38
- # <tt>dtype</tt> :: Override data type (e.g., to store a Float as :float32 instead of :float64) -- optional.
39
- # <tt>stype</tt> :: Optional storage type (defaults to :dense)
40
- def to_nm(shape, dtype = nil, stype = :dense)
41
- dtype ||=
42
- case self[0]
43
- when Fixnum then :int64
44
- when Float then :float64
45
- when Rational then :rational128
46
- when Complex then :complex128
47
- end
48
-
49
- matrix = NMatrix.new(:dense, shape, self, dtype)
50
-
51
- if stype != :dense then matrix.cast(stype, dtype) else matrix end
52
- end
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
- def returning(value)
69
- yield(value)
70
- value
71
- end
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: