Module: StairCar::PMatrixMatrixMath
- Included in:
- PMatrix
- Defined in:
- lib/stair_car/pmatrix/matrix_math.rb
Defined Under Namespace
Classes: MatrixMathError
Instance Method Summary collapse
- #*(val) ⇒ Object
- #**(val) ⇒ Object
- #+(val) ⇒ Object
- #-(val) ⇒ Object
- #/(val) ⇒ Object
- #max(dimension = nil) ⇒ Object
- #mean(dimension = nil) ⇒ Object
- #min(dimension = nil) ⇒ Object
- #perform(val, &block) ⇒ Object
- #std(dimension = nil, sample = false) ⇒ Object
- #sum(dimension = nil) ⇒ Object
- #variance(dimension = nil, sample = false) ⇒ Object
Instance Method Details
#*(val) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/stair_car/pmatrix/matrix_math.rb', line 55 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 PMatrix.new(c) end |
#**(val) ⇒ Object
47 48 49 |
# File 'lib/stair_car/pmatrix/matrix_math.rb', line 47 def **(val) perform(val) { |a,b| a * b } end |
#+(val) ⇒ Object
38 39 40 41 |
# File 'lib/stair_car/pmatrix/matrix_math.rb', line 38 def +(val) perform(val) { |a,b| a + b } # perform(val, Java::cern.jet.math.tdouble.DoubleFunctions.plus) end |
#-(val) ⇒ Object
43 44 45 |
# File 'lib/stair_car/pmatrix/matrix_math.rb', line 43 def -(val) perform(val) { |a,b| a - b } end |
#/(val) ⇒ Object
51 52 53 |
# File 'lib/stair_car/pmatrix/matrix_math.rb', line 51 def /(val) perform(val) { |a,b| a / b } end |
#max(dimension = nil) ⇒ Object
86 87 88 89 90 91 92 93 94 |
# File 'lib/stair_car/pmatrix/matrix_math.rb', line 86 def max(dimension=nil) if dimension value = aggrate_on_dimension(:max, dimension) else value, row, col = @data.max_location end return value end |
#mean(dimension = nil) ⇒ Object
106 107 108 109 110 111 112 113 114 |
# File 'lib/stair_car/pmatrix/matrix_math.rb', line 106 def mean(dimension=nil) if dimension value = aggrate_on_dimension(:mean, dimension) else value = self.sum / self.size end return value end |
#min(dimension = nil) ⇒ Object
96 97 98 99 100 101 102 103 104 |
# File 'lib/stair_car/pmatrix/matrix_math.rb', line 96 def min(dimension=nil) if dimension value = aggrate_on_dimension(:min, dimension) else value, row, col = @data.min_location end return value end |
#perform(val, &block) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/stair_car/pmatrix/matrix_math.rb', line 6 def perform(val, &block) result = @data.copy if val.is_a?(PMatrix) if rows == val.rows && cols == val.cols # Another matrix of the same size result = result.assign(val.data, &block) elsif rows == val.rows && val.cols == 1 # Vector on column (vertical) PMatrix.new(result).each_column do |row| row.data.assign(val.data, &block) end elsif cols == val.cols && val.rows == 1 # Vector on rows (horizontal) PMatrix.new(result).each_row do |col| col.data.assign(val.data, &block) end else # Incorrect size raise MatrixMathError, "matrix dimensions incorrect" end else # Passed in a number perform_on_val = Proc.new do |cell_value| yield(cell_value, val) end result.assign(perform_on_val) end return PMatrix.new(result) end |
#std(dimension = nil, sample = false) ⇒ Object
136 137 138 139 140 141 142 143 144 |
# File 'lib/stair_car/pmatrix/matrix_math.rb', line 136 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
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/stair_car/pmatrix/matrix_math.rb', line 74 def sum(dimension=nil) if dimension == 0 # Sum rows return PMatrix.ones(1, rows) * self elsif dimension == 1 # Sum cols return self * PMatrix.ones(cols, 1) else return @data.z_sum end end |
#variance(dimension = nil, sample = false) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/stair_car/pmatrix/matrix_math.rb', line 116 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 |