Module: StairCar::UMatrixMatrixMath
- Included in:
- UMatrix
- Defined in:
- lib/stair_car/umatrix/matrix_math.rb
Defined Under Namespace
Classes: MatrixMathError
Instance Method Summary collapse
- #*(val) ⇒ Object
- #**(val) ⇒ Object
- #+(val) ⇒ Object
- #-(val) ⇒ Object
- #/(val) ⇒ Object
-
#call_data_method(method, dimension, *args) ⇒ Object
Calls aggraration method on the data.
-
#dim_convert(dimension) ⇒ Object
ujmp requires you pass in a special code for all dimensions.
-
#find_params(object, method_name, &search) ⇒ Object
In jruby, we need to pass the params to java_methods to distinguish them when multiple java methods (with different params) have the same name.
- #max(dimension = nil) ⇒ Object
- #mean(dimension = nil) ⇒ Object
- #min(dimension = nil) ⇒ Object
- #perform(method, val) ⇒ Object
- #std(dimension = nil, sample = false) ⇒ Object
- #sum(dimension = nil) ⇒ Object
- #variance(dimension = nil, sample = false) ⇒ Object
Instance Method Details
#*(val) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/stair_car/umatrix/matrix_math.rb', line 82 def *(val) n = self.rows m = self.cols p = val.rows q = val.cols if m != p raise MatrixMathError, "matricies can not be multiplied" end # Build a like matrix to receive c = @data.like(n, q) @data.z_mult(val.data, c) return UMatrix.new(c) end |
#**(val) ⇒ Object
76 77 |
# File 'lib/stair_car/umatrix/matrix_math.rb', line 76 def **(val) end |
#+(val) ⇒ Object
68 69 70 |
# File 'lib/stair_car/umatrix/matrix_math.rb', line 68 def +(val) perform(:plus, val) end |
#-(val) ⇒ Object
72 73 74 |
# File 'lib/stair_car/umatrix/matrix_math.rb', line 72 def -(val) perform(:minus, val) end |
#/(val) ⇒ Object
79 80 |
# File 'lib/stair_car/umatrix/matrix_math.rb', line 79 def /(val) end |
#call_data_method(method, dimension, *args) ⇒ Object
Calls aggraration method on the data
111 112 113 114 115 116 117 118 119 |
# File 'lib/stair_car/umatrix/matrix_math.rb', line 111 def call_data_method(method, dimension, *args) result = UMatrix.new(@data.send(method, Java::OrgUjmpCoreCalculation::Calculation.NEW, dim_convert(dimension), *args)) if dimension return result else # Just return the first element return result[0,0] end end |
#dim_convert(dimension) ⇒ Object
ujmp requires you pass in a special code for all dimensions
102 103 104 105 106 107 108 |
# File 'lib/stair_car/umatrix/matrix_math.rb', line 102 def dim_convert(dimension) if dimension == nil dimension = Java::org.ujmp.core.Matrix::ALL end return dimension end |
#find_params(object, method_name, &search) ⇒ Object
In jruby, we need to pass the params to java_methods to distinguish them when multiple java methods (with different params) have the same name.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/stair_car/umatrix/matrix_math.rb', line 8 def find_params(object, method_name, &search) while object object.declared_instance_methods.each do |method| if method.name == method_name if yield(method.parameter_types) # Method checks to see if the parameter types are correct, if they # are then we return them. return method.parameter_types end end end # If we didn't find it here, move up the chain object = object.superclass end return nil end |
#max(dimension = nil) ⇒ Object
125 126 127 |
# File 'lib/stair_car/umatrix/matrix_math.rb', line 125 def max(dimension=nil) return call_data_method(:max, dimension) end |
#mean(dimension = nil) ⇒ Object
133 134 135 |
# File 'lib/stair_car/umatrix/matrix_math.rb', line 133 def mean(dimension=nil) return call_data_method(:mean, dimension, false) end |
#min(dimension = nil) ⇒ Object
129 130 131 |
# File 'lib/stair_car/umatrix/matrix_math.rb', line 129 def min(dimension=nil) return call_data_method(:min, dimension) end |
#perform(method, val) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/stair_car/umatrix/matrix_math.rb', line 27 def perform(method, val) method = method.to_s result = @data.clone if val.is_a?(UMatrix) @param_types ||= {} @param_types[method] ||= find_params(result.java_class, method) do |params| # params.first.name == 'org.ujmp.core.Matrix' params.size == 3 && params[0].name == 'org.ujmp.core.calculation.Calculation$Ret' && params[1].name == 'boolean' && params[2].name == 'org.ujmp.core.Matrix' end if rows == val.rows && cols == val.cols # Another matrix of the same size # result = result.java_send(method, @param_types[method], val.data) result.java_send(method, @param_types[method], Java::OrgUjmpCoreCalculation::Calculation.ORIG, false.to_java, val.data) elsif rows == val.rows && val.cols == 1 # Vector on column (vertical) UMatrix.new(result).each_column do |row| # row.data.send(method, Java::OrgUjmpCoreCalculation::Calculation.ORIG, false.to_java, val.data) # row.data.java_send(method, @param_types[method], val.data) row.data.java_send(method, @param_types[method], Java::OrgUjmpCoreCalculation::Calculation.ORIG, false.to_java, val.data) end elsif cols == val.cols && val.rows == 1 # Vector on rows (horizontal) UMatrix.new(result).each_row do |col| col.data.java_send(method, @param_types[method], Java::OrgUjmpCoreCalculation::Calculation.ORIG, false.to_java, val.data) # col.data.send(method, Java::OrgUjmpCoreCalculation::Calculation.ORIG, false.to_java, val.data) end else # Incorrect size raise MatrixMathError, "matrix dimensions incorrect" end else # Passed in a number result = result.send(method, Java::OrgUjmpCoreCalculation::Calculation.ORIG, false.to_java, val.to_java(:double)) end return UMatrix.new(result) end |
#std(dimension = nil, sample = false) ⇒ Object
157 158 159 160 161 162 163 164 165 |
# File 'lib/stair_car/umatrix/matrix_math.rb', line 157 def std(dimension=nil, sample=false) if dimension value = aggrate_on_dimension(:variance, dimension, sample) else value = Math.sqrt(self.variance(dimension, sample)) end return value end |
#sum(dimension = nil) ⇒ Object
121 122 123 |
# File 'lib/stair_car/umatrix/matrix_math.rb', line 121 def sum(dimension=nil) return call_data_method(:sum, dimension, false) end |
#variance(dimension = nil, sample = false) ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/stair_car/umatrix/matrix_math.rb', line 137 def variance(dimension=nil, sample=false) if dimension value = aggrate_on_dimension(:variance, dimension, sample) else mean = self.mean total = 0 self.each_non_zero do |v| total += (mean - v) ** 2 end if sample value = total / (self.size - 1) else value = total / self.size end end return value end |