ruby-mpfi 0.0.9 → 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,9 +5,9 @@
5
5
 
6
6
  VALUE r_mpfi_matrix, r_mpfi_square_matrix, r_mpfi_col_vector, r_mpfi_row_vector, r_mpfi_vector_module;
7
7
 
8
- void r_mpfi_matrix_free(void *ptr);
8
+ void r_mpfi_matrix_free (void *ptr);
9
9
 
10
10
  void r_mpfi_matrix_suitable_matrix_init (VALUE *other, MPFIMatrix **ptr_other, int row, int column);
11
- VALUE r_mpfi_matrix_robj(MPFIMatrix *x);
11
+ VALUE r_mpfi_matrix_robj (MPFIMatrix *x);
12
12
 
13
13
  #endif
@@ -0,0 +1,21 @@
1
+ require "mpfr"
2
+ require "mpfi.so"
3
+
4
+ class MPFI < Numeric
5
+ def subdivision(num)
6
+ each_subdivision(num).to_a
7
+ end
8
+
9
+ def each_subdivision_by_size(size, &block)
10
+ if block_given?
11
+ num = (self.diam_abs / size).ceil
12
+ each_subdivision(num, &block)
13
+ else
14
+ to_enum(:each_subdivision_by_size, size)
15
+ end
16
+ end
17
+
18
+ def subdivision_by_size(size)
19
+ each_subdivision_by_size(size).to_a
20
+ end
21
+ end
@@ -1,8 +1,9 @@
1
+ require "mpfi"
1
2
  require "mpfr/matrix"
2
3
  require "mpfi/matrix.so"
3
4
  # If we do not load "mpfr/matrix" before load of "mpfi/matrix.so", segmentation fault error raises.
4
5
 
5
- class MPFI
6
+ class MPFI < Numeric
6
7
  class Matrix
7
8
  def inspect
8
9
  tmp = str_ary_for_inspect2
@@ -37,6 +38,70 @@ class MPFI
37
38
  str_ary_for_inspect.join(delimiter)
38
39
  end
39
40
 
41
+ def each_subdivision(nums)
42
+ if block_given?
43
+ row_size = self.row_size
44
+ col_size = self.column_size
45
+ if (row_size != nums.size) || !(nums.all? { |col_nums| col_nums.size == col_size })
46
+ raise ArgumentError, "Invalid numbers to specify split"
47
+ end
48
+ dim = row_size * col_size
49
+ num_current = 0
50
+ elements = []
51
+ row_size.times do |r|
52
+ col_size.times do |c|
53
+ elements << self[r, c].subdivision(nums[r][c])
54
+ end
55
+ end
56
+ indices = Array.new(dim, 0)
57
+ args = Array.new(row_size) { Array.new(col_size) }
58
+ while num_current >= 0
59
+ while num_current < dim
60
+ row_num, col_num = num_current.divmod(col_size)
61
+ if args[row_num][col_num] = elements[num_current][indices[num_current]]
62
+ indices[num_current] += 1
63
+ else
64
+ indices[num_current] = 0
65
+ num_current -= 1
66
+ break
67
+ end
68
+ num_current += 1
69
+ end
70
+ if num_current == dim
71
+ yield(self.class.new(args))
72
+ num_current -= 1
73
+ end
74
+ end
75
+ else
76
+ to_enum(:each_subdivision, nums)
77
+ end
78
+ end
79
+
80
+ def each_subdivision_by_size(size, &block)
81
+ if block_given?
82
+ row_size = self.row_size
83
+ col_size = self.column_size
84
+ row_num, col_num = num.divmod(col_size)
85
+ nums = Array.new(row_size) { Array.new(col_size) }
86
+ row_size.times do |r|
87
+ col_size.times do |c|
88
+ nums[r][c] = (self[r, c].diam_abs / size).ceil
89
+ end
90
+ end
91
+ each_subdivision(nums, &block)
92
+ else
93
+ to_enum(:each_subdivision_by_size, size)
94
+ end
95
+ end
96
+
97
+ def subdivision(nums)
98
+ each_subdivision(nums).to_a
99
+ end
100
+
101
+ def subdivision_by_size(size)
102
+ each_subdivision_by_size(size).to_a
103
+ end
104
+
40
105
  def self.create(a)
41
106
  case a
42
107
  when MPFI::Vector
@@ -64,7 +129,6 @@ class MPFI
64
129
  else
65
130
  self.new(a)
66
131
  end
67
-
68
132
  end
69
133
 
70
134
  # ary is two-dimensional Array.
@@ -106,7 +170,6 @@ class MPFI
106
170
  end
107
171
  ret
108
172
  end
109
-
110
173
  end
111
174
 
112
175
  module Vector
@@ -134,28 +197,54 @@ class MPFI
134
197
  str_ary_for_inspect.join(delimiter)
135
198
  end
136
199
 
137
- def subdivision_by_size(size)
138
- division = []
139
- each { |el| division << el.subdivision((el.diam_abs / size).ceil) }
140
- ary_size = division.inject(1) { |mul, a| mul * a.size }
141
- ary = Array.new(ary_size) { Array.new }
142
- num = ary_size
143
- division.each do |div|
144
- j = 0
145
- repeat = ary_size / num
146
- num = num / div.size
147
- repeat.times do
148
- div.size.times do |i|
149
- num.times do
150
- ary[j] << div[i]
151
- j += 1
200
+ def each_subdivision(*nums)
201
+ if block_given?
202
+ dim = self.size
203
+ unless dim == nums.size
204
+ raise ArgumentError, "Invalid numbers to specify split"
205
+ end
206
+ num_current = 0
207
+ elements = nums.each_with_index.map { |num, i| self[i].subdivision(num) }
208
+ indices = Array.new(dim, 0)
209
+ args = []
210
+ while num_current >= 0
211
+ while num_current < dim
212
+ if args[num_current] = elements[num_current][indices[num_current]]
213
+ indices[num_current] += 1
214
+ else
215
+ indices[num_current] = 0
216
+ num_current -= 1
217
+ break
152
218
  end
219
+ num_current += 1
220
+ end
221
+ if num_current == dim
222
+ yield(self.class.new(args))
223
+ num_current -= 1
153
224
  end
154
225
  end
226
+ else
227
+ to_enum(:each_subdivision, *nums)
155
228
  end
156
- ary.map! { |a| self.class.new(a) }
157
229
  end
158
-
230
+
231
+ def each_subdivision_by_size(size, &block)
232
+ if block_given?
233
+ nums = self.size.times.map { |i| (self[i].diam_abs / size).ceil }
234
+ each_subdivision(*nums, &block)
235
+ else
236
+ to_enum(:each_subdivision_by_size, size)
237
+ end
238
+ end
239
+
240
+ def subdivision(*nums)
241
+ each_subdivision(*nums).to_a
242
+ end
243
+
244
+ def subdivision_by_size(size)
245
+ each_subdivision_by_size(size).to_a
246
+ end
247
+
159
248
  def self.inner_product(a, b)
160
249
  a.inner_product(b)
161
250
  end
@@ -163,7 +252,6 @@ class MPFI
163
252
  def self.distance(a, b)
164
253
  a.distance_from(b)
165
254
  end
166
-
167
255
  end
168
256
 
169
257
  class ColumnVector
@@ -185,6 +273,4 @@ class MPFI
185
273
  end
186
274
  end
187
275
  end
188
-
189
276
  end
190
-
@@ -1,3 +1,3 @@
1
- class MPFI
2
- VERSION = '0.0.9'
1
+ class MPFI < Numeric
2
+ VERSION = '0.0.10'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-mpfi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.0.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Takayuki YAMAGUCHI
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-05 00:00:00.000000000 Z
11
+ date: 2013-11-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -99,6 +99,7 @@ files:
99
99
  - ext/mpfi_matrix/mpfi/func_mpfi_matrix.h
100
100
  - ext/mpfi_matrix/mpfi/ruby_mpfi_matrix.c
101
101
  - ext/mpfi_matrix/mpfi/ruby_mpfi_matrix.h
102
+ - lib/mpfi.rb
102
103
  - lib/mpfi/matrix.rb
103
104
  - lib/mpfi/version.rb
104
105
  - ruby-mpfi.gemspec