mdarray 0.4.0-java → 0.4.2-java

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/README.md CHANGED
@@ -19,3 +19,4 @@ second dimension has a length of 3.
19
19
  [[ 1., 0., 0.],
20
20
  [ 0., 1., 2.]]
21
21
 
22
+ MDArray wiki can be found at: https://github.com/rbotafogo/mdarray/wiki
data/Rakefile CHANGED
@@ -17,7 +17,7 @@ end
17
17
 
18
18
  desc 'Install the gem in the standard location'
19
19
  task :install_gem => [:make_gem] do
20
- sh "gem install #{$gem_name}-#{$version}.gem"
20
+ sh "gem install #{$gem_name}-#{$version}-java.gem"
21
21
  end
22
22
 
23
23
  desc 'Make documentation'
data/lib/mdarray.rb CHANGED
@@ -194,42 +194,13 @@ class MDArray
194
194
 
195
195
  #---------------------------------------------------------------------------------------
196
196
  # Checks to see if this array is compatible with another array. Two arrays are
197
- # compatible if they have the same shape
197
+ # compatible if they have the same shape and have operatable types.
198
198
  #---------------------------------------------------------------------------------------
199
199
 
200
200
  def compatible(array)
201
201
  (get_shape == array.get_shape)? true : false
202
202
  end
203
203
 
204
- #---------------------------------------------------------------------------------------
205
- # Returns the upcasted type between the type of this array and another array
206
- # @param other_val [MDArray] the other array
207
- # @param force_cast [Type] will return force_cast. This is used when one wants to
208
- # force the resulting type on a new array.
209
- # @return upcasted type between the two arrays or forced type
210
- #---------------------------------------------------------------------------------------
211
-
212
- def get_type(other_val, force_cast = nil)
213
-
214
- if (force_cast != nil)
215
- type = force_cast
216
- elsif (other_val.is_a? Numeric)
217
- # if type is integer, then make it the smaller possible integer and then let upcast
218
- # do its work
219
- if (other_val.integer?)
220
- type = "short"
221
- else
222
- type = "double"
223
- end
224
- type = MDArray.upcast(@type, type)
225
- else
226
- type = MDArray.upcast(@type, other_val.type)
227
- end
228
-
229
- return type
230
-
231
- end
232
-
233
204
  #---------------------------------------------------------------------------------------
234
205
  # Prints a list of all available functions know to MDArray. Should be reimplemented.
235
206
  # For debuging only for now.
@@ -406,9 +377,8 @@ require_relative 'mdarray/function_creation'
406
377
  require_relative 'mdarray/ruby_functions'
407
378
  require_relative 'mdarray/operators'
408
379
  require_relative 'mdarray/ruby_operators'
409
- # require_relative 'mdarray/fast_non_numerical'
410
380
  require_relative 'mdarray/access'
411
- require_relative 'mdarray/slices'
381
+ require_relative 'mdarray/views'
412
382
  require_relative 'mdarray/printing'
413
383
  require_relative 'mdarray/counter'
414
- # require_relative 'mdarray/statistics'
384
+ require_relative 'mdarray/ruby_stats'
@@ -188,30 +188,6 @@ class MDArray
188
188
 
189
189
  end
190
190
 
191
- #------------------------------------------------------------------------------------
192
- #
193
- #------------------------------------------------------------------------------------
194
-
195
- def each_along_axes(axes)
196
-
197
- counter = Counter.new(self)
198
-
199
- sizes = Array.new
200
- (0..rank - 1).each do |axis|
201
- if (axes.include?(axis))
202
- sizes[axis] = 1
203
- else
204
- sizes[axis] = shape[axis]
205
- end
206
- end
207
-
208
- counter.each_along_axes(axes) do |ct|
209
- # yield ct, sizes if block_given?
210
- yield section(ct, sizes, true) if block_given?
211
- end
212
-
213
- end
214
-
215
191
  #------------------------------------------------------------------------------------
216
192
  #
217
193
  #------------------------------------------------------------------------------------
@@ -101,62 +101,6 @@ end
101
101
  ##########################################################################################
102
102
 
103
103
  class BooleanMDArray < NonNumericalMDArray
104
-
105
- #------------------------------------------------------------------------------------
106
- #
107
- #------------------------------------------------------------------------------------
108
-
109
- def self.make_boolean_op(name, func)
110
- define_method(name) do |other_val|
111
- boolean_op(other_val, func)
112
- end
113
- end
114
-
115
- #---------------------------------------------------------------------------------------
116
- #
117
- #---------------------------------------------------------------------------------------
118
-
119
- def boolean_op(other_val, method)
120
- result = MDArray.build("boolean", shape)
121
- exec_boolean_op(result, other_val, method)
122
- end
123
-
124
- #======================================================================================
125
- # Logical
126
- #======================================================================================
127
-
128
- def self.and(val1, val2)
129
- val1 and val2
130
- end
131
-
132
- #------------------------------------------------------------------------------------
133
- #
134
- #------------------------------------------------------------------------------------
135
-
136
- def self.or(val1, val2)
137
- val1 or val2
138
- end
139
-
140
- #------------------------------------------------------------------------------------
141
- #
142
- #------------------------------------------------------------------------------------
143
-
144
- def self.not(val1)
145
- !val1
146
- end
147
-
148
- #------------------------------------------------------------------------------------
149
- #
150
- #------------------------------------------------------------------------------------
151
-
152
- # make_binary_methods("and", BooleanMDArray.method(:and))
153
- # make_binary_methods("or", BooleanMDArray.method(:or))
154
-
155
- make_boolean_op("and", BooleanMDArray.method(:and))
156
- make_boolean_op("or", BooleanMDArray.method(:or))
157
-
158
- alias :| :or
159
- alias :& :and
160
104
 
161
105
  end
162
106
 
@@ -123,6 +123,43 @@ class BinaryOperator < Operator
123
123
 
124
124
  private
125
125
 
126
+ #---------------------------------------------------------------------------------------
127
+ # Checks type compatibility for this operands. If types are compatible then if needed
128
+ # returns the upcasted type.
129
+ # @returns proper type for the resulting array for the given operands
130
+ #---------------------------------------------------------------------------------------
131
+
132
+ def get_type
133
+
134
+ # if operation done in a numeric type then result is the upcast of this type and
135
+ # the other_val's type
136
+ if (@op1.is_a? NumericalMDArray)
137
+ if (@op2.is_a? Numeric)
138
+ # if type is integer, then make it the smaller possible integer and then let upcast
139
+ # do its work
140
+ if (@op2.integer?)
141
+ type = "short"
142
+ else
143
+ type = "double"
144
+ end
145
+ elsif (@op2.is_a? NumericalMDArray)
146
+ type = @op2.type
147
+ else
148
+ raise "Cannot operate numerical type (#{@op1.type}) with non-numerical type (#{@op2.class})"
149
+ end
150
+ type = MDArray.upcast(@op1.type, type)
151
+
152
+ # It is a non-numerical type, so both types need to be the same
153
+ elsif ((@op2.is_a? MDArray) && (@op1.type == @op2.type))
154
+ type = @op1.type
155
+ else
156
+ raise "Cannot operate numerical type (#{@op1.type}) with non-numerical type (#{@op2.class})"
157
+ end
158
+
159
+ return type
160
+
161
+ end
162
+
126
163
  #---------------------------------------------------------------------------------------
127
164
  #
128
165
  #---------------------------------------------------------------------------------------
@@ -131,9 +168,9 @@ class BinaryOperator < Operator
131
168
 
132
169
  @op1 = args.shift
133
170
  @op2 = args.shift
171
+
134
172
  requested_type = args.shift
135
- @type = (@force_type)? @force_type : (requested_type)? requested_type :
136
- @op1.get_type(@op2)
173
+ @type = (@force_type)? @force_type : (requested_type)? requested_type : get_type
137
174
  @coerced = @op1.coerced
138
175
  func = MDArray.select_function(@name, MDArray.functions, @type)
139
176
  if (func.is_a? Proc)
@@ -152,27 +189,35 @@ class BinaryOperator < Operator
152
189
  def get_args(*args)
153
190
 
154
191
  parse_args(*args)
155
- if (@op2.is_a? Numeric)
156
- op2_iterator = Const.new(@op2)
157
- elsif (@op2.is_a? NumericalMDArray)
158
- if (@op1.compatible(@op2))
192
+
193
+ if (@op1.is_a? NumericalMDArray)
194
+ if (@op2.is_a? Numeric)
195
+ op2_iterator = Const.new(@op2)
196
+ elsif (@op2.is_a? NumericalMDArray)
197
+ if (!@op1.compatible(@op2))
198
+ raise "Invalid operation - arrays are incompatible"
199
+ end
159
200
  op2_iterator = @op2.get_iterator_fast
160
- else
201
+ else # Operation with another user defined type
202
+ false
203
+ # *TODO: make it more general using coerce if other_val type is not recognized
204
+ # if (arg is not recognized)
205
+ # self_equiv, arg_equiv = arg.coerce(self)
206
+ # self_equiv * arg_equiv
207
+ # end
208
+ end
209
+
210
+ else # NonNumericalMDArray
211
+ if (!@op1.compatible(@op2))
161
212
  raise "Invalid operation - arrays are incompatible"
162
213
  end
163
- else
164
- false
165
- # *TODO: make it more general using coerce if other_val type is not recognized
166
- # if (arg is not recognized)
167
- # self_equiv, arg_equiv = arg.coerce(self)
168
- # self_equiv * arg_equiv
169
- # end
214
+ op2_iterator = @op2.get_iterator_fast
170
215
  end
171
-
216
+
172
217
  yield @op1.get_iterator_fast, op2_iterator, @op1.shape, *@other_args
173
-
218
+
174
219
  end
175
-
220
+
176
221
  end # BinaryOperator
177
222
 
178
223
  ##########################################################################################
@@ -0,0 +1,43 @@
1
+
2
+ ##########################################################################################
3
+ #
4
+ ##########################################################################################
5
+
6
+ module BooleanFunctions
7
+ extend FunctionCreation
8
+ extend RubyFunctions
9
+
10
+ @and = Proc.new { |val1, val2| val1 and val2 }
11
+ @or = Proc.new { |val1, val2| val1 or val2 }
12
+ @not = Proc.new { |val1| !val1}
13
+
14
+ @binary_methods = [:and, :or]
15
+
16
+ @unary_methods = [:not]
17
+
18
+ @binary_methods.each do |method|
19
+ make_binary_operators(method.to_s,
20
+ ruby_binary_function("#{method.to_s}_ruby",
21
+ instance_variable_get("@#{method.to_s}")))
22
+ end
23
+
24
+ @unary_methods.each do |method|
25
+ make_unary_operators(method.to_s,
26
+ ruby_unary_function("#{method.to_s}_ruby",
27
+ instance_variable_get("@#{method.to_s}")))
28
+ end
29
+
30
+ alias :| :or
31
+ alias :& :and
32
+
33
+ end # BooleanFunctions
34
+
35
+ ##########################################################################################
36
+ #
37
+ ##########################################################################################
38
+
39
+ class BooleanMDArray
40
+
41
+ include BooleanFunctions
42
+
43
+ end # BooleanMDArray
@@ -75,4 +75,5 @@ end # UserFunction
75
75
  require_relative 'ruby_generic_functions'
76
76
  require_relative 'ruby_numeric_functions'
77
77
  require_relative 'ruby_math'
78
+ require_relative 'ruby_boolean_functions'
78
79
  require_relative 'ruby_stats'
@@ -43,6 +43,7 @@ class RubyBinaryOperator < BinaryOperator
43
43
  get_args(*args) do |op1_iterator, op2_iterator, shape, *other_args|
44
44
  result = MDArray.build(@type, shape)
45
45
  res_iterator = result.get_iterator_fast
46
+
46
47
  if (@coerced)
47
48
  while (res_iterator.has_next?)
48
49
  res_iterator.set_next(@do_func.call(op2_iterator.get_next,
@@ -10,7 +10,11 @@ module RubyStats
10
10
  # Statistics
11
11
  #======================================================================================
12
12
 
13
- =begin
13
+
14
+ #---------------------------------------------------------------------------------------
15
+ # Sums all values
16
+ #---------------------------------------------------------------------------------------
17
+
14
18
  func = ["ruby_sum", "RubyFunction", Proc.new { |sum, val| sum + val }, "*", "*", "void"]
15
19
  make_unary_op("sum", "reduce", func, nil, Proc.new { 0 })
16
20
 
@@ -19,29 +23,25 @@ module RubyStats
19
23
  #---------------------------------------------------------------------------------------
20
24
 
21
25
  pre_calc = Proc.new { |arr1, *args| arr1[0] }
22
-
23
- calc = Proc.new { |min, val| min = (min < val)? min : val }
24
-
25
- make_unary_op("min", :reduce, calc, nil, pre_calc)
26
+ calc = Proc.new { |min, val| min = ((min < val)? min : val) }
27
+ func = ["ruby_min", "RubyFunction", calc, "*", "*", "void"]
28
+ make_unary_op("min", :reduce, func, nil, pre_calc)
26
29
 
27
30
  #---------------------------------------------------------------------------------------
28
31
  # calculates the maximum
29
32
  #---------------------------------------------------------------------------------------
30
33
 
31
34
  pre_calc = Proc.new { |arr1, *args| arr1[0] }
32
-
33
35
  calc = Proc.new { |max, val| max = (max > val)? max : val }
34
-
35
- make_unary_op("max", :reduce, calc, nil, pre_calc)
36
+ func = ["ruby_max", "RubyFunction", calc, "*", "*", "void"]
37
+ make_unary_op("max", :reduce, func, nil, pre_calc)
36
38
 
37
39
  #---------------------------------------------------------------------------------------
38
40
  # calculates the mean
39
41
  #---------------------------------------------------------------------------------------
40
42
 
41
43
  pre_calc = Proc.new { 0 }
42
-
43
44
  calc = Proc.new { |start, val| start += val }
44
-
45
45
  post_calc = Proc.new do |result, *args|
46
46
  arr = args.shift
47
47
  if (arr.size == 0)
@@ -49,21 +49,19 @@ module RubyStats
49
49
  end
50
50
  result / arr.size
51
51
  end
52
-
53
- make_unary_op("mean", :reduce, calc, nil, pre_calc, post_calc)
52
+ func = ["ruby_mean", "RubyFunction", calc, "*", "*", "void"]
53
+ make_unary_op("mean", :reduce, func, nil, pre_calc, post_calc)
54
54
 
55
55
  #---------------------------------------------------------------------------------------
56
56
  # calculates the weighted mean
57
57
  #---------------------------------------------------------------------------------------
58
58
 
59
59
  pre_calc = Proc.new { [0, 0] }
60
-
61
60
  calc = Proc.new do |result, val, weight|
62
61
  result[0] += (val * weight)
63
62
  result[1] += weight
64
63
  result
65
64
  end
66
-
67
65
  post_calc = Proc.new do |result, *args|
68
66
  arr = args.shift
69
67
  if (arr.size == 0)
@@ -71,9 +69,24 @@ module RubyStats
71
69
  end
72
70
  [result[0] / result[1], arr.size]
73
71
  end
72
+ func = ["ruby_weighted_mean", "RubyFunction", calc, "*", "*", "void"]
73
+ make_binary_op("weighted_mean", :reduce, func, nil, pre_calc, post_calc)
74
+
75
+ end # RubyStats
76
+
77
+ ##########################################################################################
78
+ #
79
+ ##########################################################################################
80
+
81
+ class DoubleMDArray
82
+
83
+ include RubyStats
84
+
85
+ end
74
86
 
75
- make_binary_op("weighted_mean", :reduce, calc, nil, pre_calc, post_calc)
76
87
 
88
+
89
+ =begin
77
90
  #---------------------------------------------------------------------------------------
78
91
  # Expectation value
79
92
  #---------------------------------------------------------------------------------------
@@ -98,8 +111,10 @@ module RubyStats
98
111
  end
99
112
  result / arr.size
100
113
  end
101
- =end
102
- =begin
114
+
115
+ #---------------------------------------------------------------------------------------
116
+ #
117
+ #---------------------------------------------------------------------------------------
103
118
 
104
119
  def mean
105
120
  expectation_value(Proc.identity, Proc.everywhere)[0]
@@ -145,5 +160,3 @@ module RubyStats
145
160
  # calculates the mean
146
161
 
147
162
  =end
148
-
149
- end # RubyStats