Class: EpiMath::Matrix
- Inherits:
-
Object
- Object
- EpiMath::Matrix
- Defined in:
- lib/epimath100/matrix.class.rb
Overview
TODO : improve the documentation
Instance Attribute Summary collapse
-
#columns ⇒ Object
readonly
Returns the value of attribute columns.
-
#lines ⇒ Object
readonly
Returns the value of attribute lines.
-
#v ⇒ Object
readonly
Returns the value of attribute v.
Class Method Summary collapse
-
.mult_array(t1, t2) ⇒ Object
Parameters: t1,t2:: Multiply each elements of t1 and t2 2b2 and sum all == Returns: Float.
-
.to_inttab(arg, num = Float) ⇒ Object
Convert each element of an Array in an Integer => must be implented in Array Class.
Instance Method Summary collapse
-
#*(matrix) ⇒ Object
Parameters: matrix:: This argument is a Matrix or an Integer.
-
#+(matrix) ⇒ Object
Parameters: matrix:: This argument is a Matrix or an Integer.
- #del_line(x = -1) ⇒ Object
-
#get_column(y) ⇒ Object
Parameters: y:: Integer.
-
#get_deter ⇒ Object
Returns:: Numerical value which is the determinant of the matrix.
-
#get_line(x) ⇒ Object
Parameters: y:: Integer.
-
#get_val(x, y) ⇒ Object
Parameters: x,y:: Integers.
-
#have_the_same_dimensions(matrix) ⇒ Object
Params: matrix:: matrix is a Matrix to compare.
-
#initialize(tab = [[]]) ⇒ Matrix
constructor
Parameters: tab:: tab is a double Array like [ [1,2], [3,4], [1,4] ].
- #new_column(tab = []) ⇒ Object
- #new_line(tab = []) ⇒ Object
-
#set_val(val, x, y) ⇒ Object
Parameters: x,y:: Integers.
- #to_ary ⇒ Object
- #to_s ⇒ Object
- #to_vector ⇒ Object
Constructor Details
#initialize(tab = [[]]) ⇒ Matrix
Parameters:
- tab
-
tab is a double Array like [ [1,2], [3,4], [1,4] ]. This array should be a valid Matrix tab or an other Matrix
Returns:
nothing
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 |
# File 'lib/epimath100/matrix.class.rb', line 37 def initialize tab=[[]] @v = tab #Must call to_inttab @columns = 0 @lines = 0 if tab.is_a?Array and tab.size > 0 and tab[0].is_a?Array @columns = tab[0].size # check if the line have the good size tab.size.times do |i| @lines += 1 if (tab[i].size != @columns) Error.call "Matrix::new : '#{tab[i]}' is not a valid line" end end elsif tab.is_a?Matrix @v = tab.v @columns = tab.columns @lines = tab.lines else Error.call "Matrix::new : '#{tab}' is not a valid matrix" end return end |
Instance Attribute Details
#columns ⇒ Object (readonly)
Returns the value of attribute columns.
9 10 11 |
# File 'lib/epimath100/matrix.class.rb', line 9 def columns @columns end |
#lines ⇒ Object (readonly)
Returns the value of attribute lines.
9 10 11 |
# File 'lib/epimath100/matrix.class.rb', line 9 def lines @lines end |
#v ⇒ Object (readonly)
Returns the value of attribute v.
9 10 11 |
# File 'lib/epimath100/matrix.class.rb', line 9 def v @v end |
Class Method Details
.mult_array(t1, t2) ⇒ Object
Parameters:
- t1,t2
-
Multiply each elements of t1 and t2 2b2 and sum all
Returns:
Float
208 209 210 211 212 213 214 215 216 217 |
# File 'lib/epimath100/matrix.class.rb', line 208 def self.mult_array(t1, t2) Error.call "Can't multiply this. One of the arguments is not an array.", Error::ERR_HIGH if (!t1.is_a?Array or !t2.is_a?Array) Error.call "Can't multiply this. Arrays do not have the same size.", Error::ERR_HIGH if t1.size != t2.size result = 0.0 t1.size.times do |i| result = (result + t1[i].to_f * t2[i].to_f).to_f end return result end |
.to_inttab(arg, num = Float) ⇒ Object
Convert each element of an Array in an Integer => must be implented in Array Class
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/epimath100/matrix.class.rb', line 12 def self.to_inttab arg, num=Float Error.call "Matrix::to_inttab : '#{arg}' is not a valid Array", Error::ERR_HIGH if !arg.is_a?Array arg.size.times do |i| # if it's an Array in an Array, the if arg[i].is_a?Array arg[i] = self.to_inttab argv[i] else if num == Integer arg[i] = arg[i].to_i else arg[i] = arg[i].to_f end end end end |
Instance Method Details
#*(matrix) ⇒ Object
Parameters:
- matrix
-
This argument is a Matrix or an Integer. If it’s a Matrix, it will do matrix product. Else, if it’s a integer, it will multiply each coeficient of the current Matrix.
Returns:
Matrix
Matrix_Product:
- little explanation
-
If matrix is a Matrix, we will multiply 2by2 each coeficient of the column X of the current Matrix and the line X of matrix. Then, we do the sum of them and we put it in a new Matrix at the position X. The is just a sum up, view the details on wiki bitch.
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/epimath100/matrix.class.rb', line 232 def *(matrix) #produit matriciel #convert vector -> matrix if matrix.is_a?Vector Error.call "Matrix::* : Transformation implicite de Vector en Matrix", Error::ERR_LOW matrix = matrix.to_matrix end if matrix.is_a?Matrix Error.call "Matrix::* : Invalid multiplication at line #{matrix.lines} and column #{@columns}", Error::ERR_HIGH if @columns != matrix.lines result = [] @lines.times do |i| result << [] end #colonne de resultat = colonne de matrix X #ligne de resutlat = ligne de self Y @lines.times do |y| matrix.columns.times do |x| result[y][x] = Matrix.mult_array(get_line(y), matrix.get_column(x)) end end return Matrix.new result #produit d'un entier et d'une matrix elsif matrix.is_a?Numeric result = @v @lines.times do |x| @columns.times do |y| result[x][y] = result[x][y].to_f * matrix end end return Matrix.new result #message d'erreur else Error.call "Matrix::* : Impossible de calculer cela (#{matrix} n'est pas une matrix)", Error::ERR_HIGH end end |
#+(matrix) ⇒ Object
Parameters:
- matrix
-
This argument is a Matrix or an Integer. If it’s a Matrix, it must have the same dimensions than the current Matrix. Else, if it’s a integer, it will be added to each coeficients of the current Matrix.
Returns:
Matrix
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
# File 'lib/epimath100/matrix.class.rb', line 278 def +(matrix) result = @v if have_the_same_dimensions matrix @lines.times do |x| @columns.times do |y| result[x][y] += matrix.v[x][y] end end elsif matrix.is_a?Numeric @lines.times do |x| @columns.times do |y| result[x][y] += matrix end end else Error.call "Matrix::+ : Impossible de calculer cela", Error::ERR_HIGH end Matrix.new result end |
#del_line(x = -1) ⇒ Object
109 110 111 112 113 114 115 116 |
# File 'lib/epimath100/matrix.class.rb', line 109 def del_line x=-1 if x.is_a?Integer and @v[x] != nil @lines -= 1 @v.delete_at x else Error.call "Matrix::del_line : Line '#{x}' doesn't exist" end end |
#get_column(y) ⇒ Object
Parameters:
- y
-
Integer. It’s the n° column which is extracted
Returns:
Array
178 179 180 181 182 183 184 185 186 |
# File 'lib/epimath100/matrix.class.rb', line 178 def get_column y Error.call "Matrix::get_column : Column #{y} doesn't exist" if !y.is_a?Integer or y < 0 or y >= @columns result = [] @lines.times do |i| result << @v[i][y] end return result end |
#get_deter ⇒ Object
Returns::
Numerical value which is the determinant of the matrix. It only work on 2x2
300 301 302 303 304 305 306 |
# File 'lib/epimath100/matrix.class.rb', line 300 def get_deter Error.call "Matrix::get_deter : This error comes from get_deter which works only with 2x2 matrix" if @columns != 2 or @lines != 2 det = get_val(0, 0).to_i * get_val(1, 1).to_i det -= get_val(0, 1).to_i * get_val(1, 0).to_i return det end |
#get_line(x) ⇒ Object
Parameters:
- y
-
Integer. It’s the n° line which is extracted
Returns:
Array
167 168 169 170 171 |
# File 'lib/epimath100/matrix.class.rb', line 167 def get_line x Error.call "Matrix::get_line : Line #{x} doesn't exist" if !x.is_a?Integer or x < 0 or x >= @lines return @v[x] end |
#get_val(x, y) ⇒ Object
Parameters:
- x,y
-
Integers. They are the coordonates of the value which will extract from the matrix
Returns:
a value fo the matrix
123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/epimath100/matrix.class.rb', line 123 def get_val x, y if !x.is_a?Integer Error.call "Matrix::get_val : '#{x}' is not a correct line" return nil elsif !y.is_a?Integer Error.call "Matrix::get_val : '#{y}' is not a correct column" return nil elsif x < 0 or y < 0 or x >= @lines or y >= @columns Error.call "Matrix::get_val : The specified positions are invalids (#{x},#{y})" return nil else return @v[x][y] end end |
#have_the_same_dimensions(matrix) ⇒ Object
Params:
- matrix
-
matrix is a Matrix to compare.
Returns:
True or False.
Usage::
- The function check if the current matrix and matrix
-
have the same dimensions (linse and columns)
195 196 197 198 199 200 201 |
# File 'lib/epimath100/matrix.class.rb', line 195 def have_the_same_dimensions matrix if (matrix.is_a?Matrix and matrix.columns == @columns and matrix.lines == @lines) true else false end end |
#new_column(tab = []) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/epimath100/matrix.class.rb', line 94 def new_column tab=[] Error.call "Matrix::new_column : Size of the new column (#{tab} => #{tab.size}) is not valid" if !tab.is_a?Array or tab.size != @lines @columns += 1 if tab.is_a? Array and tab.size == @lines @lines.times do |i| @v[i] << tab[i] end else @lines.times do |i| @v[i] << 0 end end end |
#new_line(tab = []) ⇒ Object
87 88 89 90 91 92 |
# File 'lib/epimath100/matrix.class.rb', line 87 def new_line tab=[] Error.call "Matrix::new_line : Size of the new line (#{tab} => #{tab.size}) is not valid" if !tab.is_a?Array or tab.size != @column @lines += 1 @v << tab end |
#set_val(val, x, y) ⇒ Object
Parameters:
- x,y
-
Integers. They are the coordonates of the value which will write in the matrix
Returns:
a value fo the matrix
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/epimath100/matrix.class.rb', line 143 def set_val val, x, y if !x.is_a?Integer Error.call "Matrix::set_val : '#{x}' is not a correct line" return nil elsif !y.is_a?Integer Error.call "Matrix::set_val : '#{y}' is not a correct column" return nil elsif !val.is_a?Numeric Error.call "Matrix::set_val : '#{val}' is not a correct value" return nil elsif x < 0 or y < 0 or x >= @lines or y >= @columns Error.call "Matrix::set_val : The specified positions are invalids (#{x} >= #{@lines},#{y} >= #{@columns})\n#{self.to_s}" return nil else @v[x][y] = val return @v[x][y] end end |
#to_ary ⇒ Object
79 80 81 |
# File 'lib/epimath100/matrix.class.rb', line 79 def to_ary @v end |
#to_s ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/epimath100/matrix.class.rb', line 63 def to_s out = "" @v.each do |line| out << "[" # display all elements of this line line.each do |element| out << element.to_f.round(3).to_s << " " end out << "\b]\n" # TODO : FIX THAT broggi_t end out end |