matlab-ruby 1.0.1 → 2.0.0

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.
@@ -1,7 +1,20 @@
1
+ == 2.0.0 / 2008-01-03
2
+
3
+ * 1 bug fix
4
+ * Added NilClass to_ruby method.
5
+ * 3 changes
6
+ * Removed dynamic variable name setters/getters in favor of put_variable, get_variable methods.
7
+ * 1 dimensional MATLAB matrices are now converted into a normal Ruby Arrays.
8
+ * 1 dimensional MATLAB struct matrices are now converted into normal Ruby Hashes.
9
+ * 3 enhancements
10
+ * MATLAB functions may now be executed directly from the engine instead of having to use eval_string.
11
+ * Ruby Arrays are now converted into 1 dimensional MATLAB matrices.
12
+ * Ruby Hashes are now converted into 1 dimensional MATLAB struct matrices.
13
+
1
14
  == 1.0.1 / 2007-04-27
2
15
 
3
16
  * 1 bug fix
4
- * Need to check for nil value during MATLAB to Ruby StructMatrix conversion otherwise it segfaults
17
+ * Need to check for nil value during MATLAB to Ruby StructMatrix conversion otherwise it segfaults.
5
18
 
6
19
  == 1.0.0 / 2007-04-06
7
20
 
data/README.txt CHANGED
@@ -17,16 +17,16 @@ A Ruby interface to the MATLAB interpreted language.
17
17
  require 'matlab'
18
18
 
19
19
  engine = Matlab::Engine.new
20
- engine.x = 123.456
21
- engine.y = 789.101112
22
- engine.eval_string "z = x * y"
23
- engine.z
20
+ engine.put_variable "x", 123.456
21
+ engine.put_variable "y", 789.101112
22
+ engine.eval "z = x * y"
23
+ engine.get_variable "z"
24
24
 
25
25
  matrix = Matlab::Matrix.new(20, 400)
26
26
  20.times { |m| 400.times { |n| matrix[m, n] = rand } }
27
- engine.m = matrix
27
+ engine.put_variable "m", matrix
28
28
 
29
- engine.eval_string "save '/tmp/20_x_400_matrix"
29
+ engine.save "/tmp/20_x_400_matrix"
30
30
  engine.close
31
31
 
32
32
  == REQUIREMENTS:
@@ -27,6 +27,10 @@ class NilClass
27
27
  def to_matlab
28
28
  Matlab::Driver::Native::API.mxGetNaN
29
29
  end
30
+
31
+ def to_ruby
32
+ nil
33
+ end
30
34
  end
31
35
 
32
36
  class Float
@@ -44,18 +48,44 @@ class Numeric
44
48
  end
45
49
 
46
50
  class Array
51
+ # Converts the array into a 1 Dimensional MATLAB numeric or cell matrix
52
+ def to_matlab
53
+ if all? { |value| value.kind_of?(Numeric) || value.nil? }
54
+ matrix = Matlab::Matrix.new(size, 1)
55
+
56
+ each_with_index do |value, index|
57
+ matrix[index, 0] = value
58
+ end
59
+ matrix.to_matlab
60
+ else
61
+ to_cell_matrix.to_matlab
62
+ end
63
+ end
64
+
47
65
  # Flattens and converts the array to a 1 Dimensional Matlab::CellMatrix
48
66
  def to_cell_matrix
49
- values = flatten
50
- cell_matrix = Matlab::CellMatrix.new(values.size, 1)
67
+ cell_matrix = Matlab::CellMatrix.new(size, 1)
51
68
 
52
- values.each_with_index do |value, index|
69
+ each_with_index do |value, index|
53
70
  cell_matrix[index, 0] = value
54
71
  end
55
72
  cell_matrix
56
73
  end
57
74
  end
58
75
 
76
+ class Hash
77
+ # Converts the hash into a 1 Dimensional MATLAB struct matrix
78
+ def to_matlab
79
+ struct_matrix = Matlab::StructMatrix.new(1, 1, *keys)
80
+
81
+ each do |(key, value)|
82
+ struct_matrix[0,0][key] = value
83
+ end
84
+
85
+ struct_matrix.to_matlab
86
+ end
87
+ end
88
+
59
89
  module Matlab
60
90
  class Matrix
61
91
  # Converts the matrix into a MATLAB numeric matrix
@@ -75,7 +105,7 @@ module Matlab
75
105
  matrix
76
106
  end
77
107
 
78
- # Creates a Matlab::Matrix from a MATLAB numeric matrix
108
+ # Creates a Matlab::Matrix or Ruby Array from a MATLAB numeric matrix
79
109
  def self.from_matlab(matrix)
80
110
  m = Matlab::Driver::Native::API.mxGetM(matrix)
81
111
  n = Matlab::Driver::Native::API.mxGetN(matrix)
@@ -91,7 +121,11 @@ module Matlab
91
121
  end
92
122
  end
93
123
 
94
- matlab_matrix
124
+ if m == 1 || n == 1
125
+ matlab_matrix.cells.flatten
126
+ else
127
+ matlab_matrix
128
+ end
95
129
  end
96
130
  end
97
131
 
@@ -112,7 +146,7 @@ module Matlab
112
146
  matrix
113
147
  end
114
148
 
115
- # Creates a Matlab::CellMatrix from a MATLAB cell matrix
149
+ # Creates a Matlab::CellMatrix or Ruby Array from a MATLAB cell matrix
116
150
  def self.from_matlab(matrix)
117
151
  m = Matlab::Driver::Native::API.mxGetM(matrix)
118
152
  n = Matlab::Driver::Native::API.mxGetN(matrix)
@@ -128,7 +162,11 @@ module Matlab
128
162
  end
129
163
  end
130
164
 
131
- cell_matrix
165
+ if m == 1 || n == 1
166
+ cell_matrix.cells.collect { |cell| cell.first }
167
+ else
168
+ cell_matrix
169
+ end
132
170
  end
133
171
  end
134
172
 
@@ -152,7 +190,7 @@ module Matlab
152
190
  matrix
153
191
  end
154
192
 
155
- # Creates a Matlab::StructMatrix from a MATLAB struct matrix
193
+ # Creates a Matlab::StructMatrix or Ruby Hash from a MATLAB struct matrix
156
194
  def self.from_matlab(matrix)
157
195
  m = Matlab::Driver::Native::API.mxGetM(matrix)
158
196
  n = Matlab::Driver::Native::API.mxGetN(matrix)
@@ -171,7 +209,11 @@ module Matlab
171
209
  end
172
210
  end
173
211
 
174
- struct_matrix
212
+ if m == 1 && n == 1
213
+ struct_matrix.cells.flatten.first
214
+ else
215
+ struct_matrix
216
+ end
175
217
  end
176
218
  end
177
219
  end
@@ -7,10 +7,10 @@ module Matlab
7
7
  #
8
8
  # engine = Matlab::Engine.new
9
9
  #
10
- # engine.x = 123.456
11
- # engine.y = 789.101112
12
- # engine.eval_string "z = x * y"
13
- # p engine.z
10
+ # engine.put_variable "x", 123.456
11
+ # engine.put_variable "y", 789.101112
12
+ # engine.eval "z = x * y"
13
+ # p engine.get_variable "z"
14
14
  #
15
15
  # engine.close
16
16
  #
@@ -35,15 +35,30 @@ module Matlab
35
35
  @driver.eval_string(@handle, string)
36
36
  end
37
37
 
38
- # Puts or Gets a value to MATLAB via a given name
38
+ # Put a value to MATLAB via a given name
39
+ def put_variable(name, value)
40
+ @driver.put_variable(@handle, name, value)
41
+ end
42
+
43
+ # Get a value from MATLAB via a given name
44
+ def get_variable(name)
45
+ @driver.get_variable(@handle, name)
46
+ end
47
+
48
+ # Call a MATLAB function passing in the arguments
39
49
  def method_missing(method_id, *args)
40
50
  method_name = method_id.id2name
41
51
 
42
- if method_name[(-1)..-1] == "="
43
- @driver.put_variable(@handle, method_name.chop, args.first)
44
- else
45
- @driver.get_variable(@handle, method_name)
52
+ variable_names = []
53
+ args.each_with_index do |arg, index|
54
+ variable_names << variable_name = "mr#{index}_#{method_name}"
55
+ put_variable(variable_name, arg)
46
56
  end
57
+
58
+ eval_string("#{method_name}(#{variable_names.join(", ")})")
59
+ result = get_variable("ans")
60
+ eval_string("clear #{variable_names.join(" ")}")
61
+ result
47
62
  end
48
63
 
49
64
  # Closes this engine
@@ -2,9 +2,9 @@ module Matlab
2
2
 
3
3
  module Version
4
4
 
5
- MAJOR = 1
5
+ MAJOR = 2
6
6
  MINOR = 0
7
- TINY = 1
7
+ TINY = 0
8
8
 
9
9
  STRING = [ MAJOR, MINOR, TINY ].join( "." )
10
10
  #:beta-tag:
@@ -33,15 +33,29 @@ class ConversionsTest < Test::Unit::TestCase
33
33
  end
34
34
 
35
35
  def test_array_to_cell_matrix
36
- cell_matrix = Matlab::CellMatrix.new(9, 1)
37
- 9.times { |i| cell_matrix[i, 0] = (i > 4 ? i : i.to_s) }
36
+ cell_matrix = Matlab::CellMatrix.new(7, 1)
38
37
  cell_matrix[0, 0] = true
39
- cell_matrix[4, 0] = nil
40
- cell_matrix[8, 0] = false
38
+ cell_matrix[1, 0] = "1"
39
+ cell_matrix[2, 0] = "2"
40
+ cell_matrix[3, 0] = ["3", nil, 5]
41
+ cell_matrix[4, 0] = 6
42
+ cell_matrix[5, 0] = 7
43
+ cell_matrix[6, 0] = false
41
44
 
42
45
  assert_equal cell_matrix, [true, "1", "2", ["3", nil, 5], 6, 7, false].to_cell_matrix
43
46
  end
44
47
 
48
+ def test_array_to_matlab_to_ruby
49
+ assert_equal [1.0, nil, 3.0], [1, nil, 3.0].to_matlab.to_ruby
50
+ assert_equal [1.0, "2", false, ["foo", "bar", "baz"]], [1, "2", false, ["foo", "bar", "baz"]].to_matlab.to_ruby
51
+ end
52
+
53
+ def test_hash_to_matlab_to_ruby
54
+ assert_equal({"foo" => "bar"}, {"foo" => "bar"}.to_matlab.to_ruby)
55
+ assert_equal({"foo" => [1.0,2.0,3.0]}, {"foo" => [1,2,3]}.to_matlab.to_ruby)
56
+ assert_equal({"foo" => { "bar" => [1.0,2.0,3.0, [4.0,5.0,6.0]] }}, {"foo" => { "bar" => [1,2,3, [4,5,6]] }}.to_matlab.to_ruby)
57
+ end
58
+
45
59
  def test_matlab_matrix_to_matlab_to_ruby
46
60
  matrix = Matlab::Matrix.new(3, 3)
47
61
  3.times { |m| 3.times { |n| matrix[m, n] = rand } }
@@ -65,13 +65,23 @@ class EngineTest < Test::Unit::TestCase
65
65
  @engine.close
66
66
  end
67
67
 
68
- def test_method_missing_get
68
+ def test_get_variable
69
69
  @driver.expects(:get_variable).with(@handle, "x")
70
- @engine.x
70
+ @engine.get_variable("x")
71
71
  end
72
72
 
73
- def test_method_missing_put
73
+ def test_put_variable
74
74
  @driver.expects(:put_variable).with(@handle, "y", 123.456)
75
- @engine.y = 123.456
75
+ @engine.put_variable("y", 123.456)
76
+ end
77
+
78
+ def test_method_missing
79
+ @engine.expects(:put_variable).with("mr0_rand", 2)
80
+ @engine.expects(:put_variable).with("mr1_rand", 4)
81
+ @engine.expects(:eval_string).with("rand(mr0_rand, mr1_rand)")
82
+ @engine.expects(:get_variable).with("ans").returns("The Answer")
83
+ @engine.expects(:eval_string).with("clear mr0_rand mr1_rand")
84
+
85
+ assert_equal "The Answer", @engine.rand(2, 4)
76
86
  end
77
87
  end
metadata CHANGED
@@ -1,35 +1,38 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.9.2
3
- specification_version: 1
4
2
  name: matlab-ruby
5
3
  version: !ruby/object:Gem::Version
6
- version: 1.0.1
7
- date: 2007-04-27 00:00:00 -06:00
8
- summary: A Ruby interface to the Matlab interpreted language.
9
- require_paths:
10
- - lib
11
- - ext
12
- email:
13
- - jonathan.younger@lipomics.com
14
- homepage: http://matlab-ruby.rubyforge.org/
15
- rubyforge_project: matlab-ruby
16
- description: "== USAGE: require 'matlab' engine = Matlab::Engine.new engine.x = 123.456 engine.y = 789.101112 engine.eval_string \"z = x * y\" engine.z matrix = Matlab::Matrix.new(20, 400) 20.times { |m| 400.times { |n| matrix[m, n] = rand } } engine.m = matrix engine.eval_string \"save '/tmp/20_x_400_matrix\" engine.close == REQUIREMENTS: * MATLAB * GCC or some other compiler to build the included extension * SWIG (If you want to recompile the SWIG wrapper) * Mocha (For testing only)"
17
- autorequire:
18
- default_executable:
19
- bindir: bin
20
- has_rdoc: true
21
- required_ruby_version: !ruby/object:Gem::Version::Requirement
22
- requirements:
23
- - - ">"
24
- - !ruby/object:Gem::Version
25
- version: 0.0.0
26
- version:
4
+ version: 2.0.0
27
5
  platform: ruby
28
- signing_key:
29
- cert_chain:
30
- post_install_message:
31
6
  authors:
32
7
  - Jonathan Younger
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-01-07 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hoe
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.4.0
23
+ version:
24
+ description: "== USAGE: require 'matlab' engine = Matlab::Engine.new engine.put_variable \"x\", 123.456 engine.put_variable \"y\", 789.101112 engine.eval \"z = x * y\" engine.get_variable \"z\" matrix = Matlab::Matrix.new(20, 400) 20.times { |m| 400.times { |n| matrix[m, n] = rand } } engine.put_variable \"m\", matrix engine.save \"/tmp/20_x_400_matrix\" engine.close == REQUIREMENTS: * MATLAB * GCC or some other compiler to build the included extension * SWIG (If you want to recompile the SWIG wrapper) * Mocha (For testing only)"
25
+ email:
26
+ - jonathan.younger@lipomics.com
27
+ executables: []
28
+
29
+ extensions:
30
+ - ext/matlab_api/extconf.rb
31
+ extra_rdoc_files:
32
+ - History.txt
33
+ - LICENSE.txt
34
+ - Manifest.txt
35
+ - README.txt
33
36
  files:
34
37
  - History.txt
35
38
  - LICENSE.txt
@@ -50,27 +53,35 @@ files:
50
53
  - test/driver/native/test_conversions.rb
51
54
  - test/driver/native/test_driver.rb
52
55
  - test/test_engine.rb
56
+ has_rdoc: true
57
+ homepage: http://matlab-ruby.rubyforge.org/
58
+ post_install_message:
59
+ rdoc_options:
60
+ - --main
61
+ - README.txt
62
+ require_paths:
63
+ - lib
64
+ - ext
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: "0"
70
+ version:
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: "0"
76
+ version:
77
+ requirements: []
78
+
79
+ rubyforge_project: matlab-ruby
80
+ rubygems_version: 1.0.1
81
+ signing_key:
82
+ specification_version: 2
83
+ summary: A Ruby interface to the Matlab interpreted language.
53
84
  test_files:
54
85
  - test/driver/native/test_conversions.rb
55
86
  - test/driver/native/test_driver.rb
56
87
  - test/test_engine.rb
57
- rdoc_options: []
58
-
59
- extra_rdoc_files: []
60
-
61
- executables: []
62
-
63
- extensions:
64
- - ext/matlab_api/extconf.rb
65
- requirements: []
66
-
67
- dependencies:
68
- - !ruby/object:Gem::Dependency
69
- name: hoe
70
- version_requirement:
71
- version_requirements: !ruby/object:Gem::Version::Requirement
72
- requirements:
73
- - - ">="
74
- - !ruby/object:Gem::Version
75
- version: 1.2.0
76
- version: