Class: DeepMiner::VectorMatrix
- Inherits:
-
Object
- Object
- DeepMiner::VectorMatrix
- Defined in:
- lib/deep_miner/vector_matrix.rb
Instance Attribute Summary collapse
-
#m ⇒ Object
readonly
Returns the value of attribute m.
-
#matrix ⇒ Object
readonly
Returns the value of attribute matrix.
-
#n ⇒ Object
readonly
Returns the value of attribute n.
-
#random ⇒ Object
readonly
Returns the value of attribute random.
Class Method Summary collapse
-
.identity(n, objectify = false) ⇒ Object
Matrix Specific Operations.
-
.objectify(new_matrix) ⇒ Object
General Operations.
Instance Method Summary collapse
-
#add(o_matrix, objectify, not_objectified = true) ⇒ Object
Add.
- #add_value(i, j, x) ⇒ Object
- #apply_new_matrix(x) ⇒ Object
- #apply_value(x) ⇒ Object
-
#diagonal? ⇒ Boolean
Matrix Qualities.
- #empty? ⇒ Boolean
- #get_column(index, objectify = false) ⇒ Object
- #get_diagonal(objectify = false) ⇒ Object
- #get_row(index, objectify = false) ⇒ Object
- #get_value(i, j) ⇒ Object
-
#initialize(n, m, random, matrix = nil) ⇒ VectorMatrix
constructor
A new instance of VectorMatrix.
-
#multiply(o_matrix, objectify, not_objectified = true) ⇒ Object
Multiply.
-
#scale(scale_val, objectify = false) ⇒ Object
Scalar Product.
- #see_i_vector_size ⇒ Object
- #see_j_vector_size ⇒ Object
- #set_value(i, j, x) ⇒ Object
- #square? ⇒ Boolean
-
#subtract(o_matrix, objectify, not_objectified = true) ⇒ Object
Subtract.
- #to_array ⇒ Object
- #trace ⇒ Object
- #transpose(objectify = false) ⇒ Object
Constructor Details
#initialize(n, m, random, matrix = nil) ⇒ VectorMatrix
Returns a new instance of VectorMatrix.
5 6 7 8 9 10 11 12 13 14 |
# File 'lib/deep_miner/vector_matrix.rb', line 5 def initialize(n, m, random, matrix = nil) if matrix.nil? @n = n @m = m @random = random initialize_h else simple_init(matrix) end end |
Instance Attribute Details
#m ⇒ Object (readonly)
Returns the value of attribute m.
3 4 5 |
# File 'lib/deep_miner/vector_matrix.rb', line 3 def m @m end |
#matrix ⇒ Object (readonly)
Returns the value of attribute matrix.
3 4 5 |
# File 'lib/deep_miner/vector_matrix.rb', line 3 def matrix @matrix end |
#n ⇒ Object (readonly)
Returns the value of attribute n.
3 4 5 |
# File 'lib/deep_miner/vector_matrix.rb', line 3 def n @n end |
#random ⇒ Object (readonly)
Returns the value of attribute random.
3 4 5 |
# File 'lib/deep_miner/vector_matrix.rb', line 3 def random @random end |
Class Method Details
.identity(n, objectify = false) ⇒ Object
Matrix Specific Operations
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/deep_miner/vector_matrix.rb', line 94 def self.identity(n, objectify = false) fail ArgumentError, 'N must have a size greater than 0' if n <= 0 new_matrix = Array.new(n) { Array.new(n) } 0.upto(n - 1) do |i| 0.upto(n - 1) do |j| if i == j new_matrix[i][j] = 1 else new_matrix[i][j] = 0 end end end return VectorMatrix.new(n, n, nil, new_matrix) if objectify new_matrix end |
.objectify(new_matrix) ⇒ Object
General Operations
18 19 20 21 |
# File 'lib/deep_miner/vector_matrix.rb', line 18 def self.objectify(new_matrix) fail ArgumentError, 'Input must be Array' unless new_matrix.is_a? Array VectorMatrix.new(nil, nil, nil, new_matrix) end |
Instance Method Details
#add(o_matrix, objectify, not_objectified = true) ⇒ Object
Add
144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/deep_miner/vector_matrix.rb', line 144 def add(o_matrix, objectify, not_objectified = true) fail ArgumentError, 'Input matrix must be of type Array' if not_objectified and !o_matrix.is_a? Array o_matrix = VectorMatrix.objectify(o_matrix) if not_objectified added = Array.new(@n) { Array.new(@m) } 0.upto(@n - 1) do |i| 0.upto(@m - 1) do |j| added[i][j] = @matrix[i][j] + o_matrix.get_value(i, j) end end return VectorMatrix.new(nil, nil, nil, added) if objectify added end |
#add_value(i, j, x) ⇒ Object
45 46 47 48 49 |
# File 'lib/deep_miner/vector_matrix.rb', line 45 def add_value(i, j, x) fail ArgumentError, 'Values cannot be nil' if i.nil? || j.nil? || x.nil? fail ArgumentError, 'Indices must be in matrix bounds' if i < 0 || j < 0 || i > @n || j > @m @matrix[i][j] += x end |
#apply_new_matrix(x) ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/deep_miner/vector_matrix.rb', line 27 def apply_new_matrix(x) fail ArgumentError, 'Input must be Array' unless x.is_a? Array @matrix = x @n = @matrix.size @m = @matrix[0].size @random = nil end |
#apply_value(x) ⇒ Object
23 24 25 |
# File 'lib/deep_miner/vector_matrix.rb', line 23 def apply_value(x) @matrix = Array.new(@n) { Array.new(@m, x) } end |
#diagonal? ⇒ Boolean
Matrix Qualities
183 184 185 186 187 188 189 190 |
# File 'lib/deep_miner/vector_matrix.rb', line 183 def diagonal? 0.upto(@n - 1) do |i| 0.upto(@m - 1) do |j| return false if (@matrix[i][j].nil? || @matrix[i][j] != 0.0) && i != j end end true end |
#empty? ⇒ Boolean
192 193 194 195 196 197 198 199 200 |
# File 'lib/deep_miner/vector_matrix.rb', line 192 def empty? return true if @matrix.size == 0 0.upto(@n - 1) do |i| 0.upto(@m - 1) do |j| return false unless @matrix[i][j].nil? end end true end |
#get_column(index, objectify = false) ⇒ Object
62 63 64 65 66 67 68 69 70 |
# File 'lib/deep_miner/vector_matrix.rb', line 62 def get_column(index, objectify = false) fail ArgumentError, 'Index must be in matrix bounds' if index < 0 || index > @m - 1 column = [] 0.upto(@n - 1) do |i| column << @matrix[i][index] end return VectorMatrix.new(nil, nil, nil, [] << column) if objectify column end |
#get_diagonal(objectify = false) ⇒ Object
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/deep_miner/vector_matrix.rb', line 72 def get_diagonal(objectify = false) diagonal = [] 0.upto(@n - 1) do |i| 0.upto(@m - 1) do |j| diagonal << @matrix[i][j] if i == j end end return VectorMatrix.new(nil, nil, nil, [] << diagonal) if objectify diagonal end |
#get_row(index, objectify = false) ⇒ Object
56 57 58 59 60 |
# File 'lib/deep_miner/vector_matrix.rb', line 56 def get_row(index, objectify = false) fail ArgumentError, 'Index must be in matrix bounds' if index < 0 || index > @n - 1 return VectorMatrix.new(nil, nil, nil, [] << @matrix[index]) if objectify @matrix[index] end |
#get_value(i, j) ⇒ Object
51 52 53 54 |
# File 'lib/deep_miner/vector_matrix.rb', line 51 def get_value(i, j) fail ArgumentError, 'Indices must be in matrix bounds' if i < 0 || j < 0 || i > @n || j > @m @matrix[i][j] end |
#multiply(o_matrix, objectify, not_objectified = true) ⇒ Object
Multiply
172 173 174 175 176 177 178 179 |
# File 'lib/deep_miner/vector_matrix.rb', line 172 def multiply(o_matrix, objectify, not_objectified = true) fail ArgumentError, 'Input matrix must be of type Array' if not_objectified and !o_matrix.is_a? Array o_matrix = VectorMatrix.objectify(o_matrix) if not_objectified fail ArgumentError, 'Illegal matrix multiplication dimensions' if @m != o_matrix.see_j_vector_size multiplied = multiply_h(o_matrix) return VectorMatrix.new(nil, nil, nil, multiplied) if objectify multiplied end |
#scale(scale_val, objectify = false) ⇒ Object
Scalar Product
131 132 133 134 135 136 137 138 139 140 |
# File 'lib/deep_miner/vector_matrix.rb', line 131 def scale(scale_val, objectify = false) scaled = Array.new(@n) { Array.new(@m) } 0.upto(@n - 1) do |i| 0.upto(@m - 1) do |j| scaled[i][j] = scale_val * @matrix[i][j] end end return VectorMatrix.new(nil, nil, nil, scaled) if objectify scaled end |
#see_i_vector_size ⇒ Object
83 84 85 |
# File 'lib/deep_miner/vector_matrix.rb', line 83 def see_i_vector_size @matrix.size end |
#see_j_vector_size ⇒ Object
87 88 89 90 |
# File 'lib/deep_miner/vector_matrix.rb', line 87 def see_j_vector_size return 0 if self.see_i_vector_size == 0 @matrix[0].size end |
#set_value(i, j, x) ⇒ Object
39 40 41 42 43 |
# File 'lib/deep_miner/vector_matrix.rb', line 39 def set_value(i, j, x) fail ArgumentError, 'Values cannot be nil' if i.nil? || j.nil? || x.nil? fail ArgumentError, 'Indices must be in matrix bounds' if i < 0 || j < 0 || i > @n || j > @m @matrix[i][j] = x end |
#square? ⇒ Boolean
202 203 204 |
# File 'lib/deep_miner/vector_matrix.rb', line 202 def square? @n == @m end |
#subtract(o_matrix, objectify, not_objectified = true) ⇒ Object
Subtract
158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/deep_miner/vector_matrix.rb', line 158 def subtract(o_matrix, objectify, not_objectified = true) fail ArgumentError, 'Input matrix must be of type Array' if not_objectified and !o_matrix.is_a? Array o_matrix = VectorMatrix.objectify(o_matrix) if not_objectified subtracted = Array.new(@n) { Array.new(@m) } 0.upto(@n - 1) do |i| 0.upto(@m - 1) do |j| subtracted[i][j] = @matrix[i][j] - o_matrix.get_value(i, j) end end return VectorMatrix.new(nil, nil, nil, subtracted) if objectify subtracted end |
#to_array ⇒ Object
35 36 37 |
# File 'lib/deep_miner/vector_matrix.rb', line 35 def to_array @matrix end |
#trace ⇒ Object
110 111 112 113 114 115 116 117 |
# File 'lib/deep_miner/vector_matrix.rb', line 110 def trace diagonal = get_diagonal(false) sum = 0.0 0.upto(diagonal.size - 1) do |i| sum += diagonal[i] end sum end |
#transpose(objectify = false) ⇒ Object
119 120 121 122 123 124 125 126 |
# File 'lib/deep_miner/vector_matrix.rb', line 119 def transpose(objectify = false) transposed = [] 0.upto(@m - 1) do |i| transposed << get_column(i) end return VectorMatrix.new(nil, nil, nil, transposed) if objectify transposed end |