Class: Matrix
- Inherits:
- 
      Object
      
        - Object
- Matrix
 
- Includes:
- Enumerable, MatrixErr, Properties
- Defined in:
- lib/matrix_gem.rb
Instance Method Summary collapse
- 
  
    
      #*(m)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Matrix multiplication. 
- 
  
    
      #+(matrix)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Return the sum of two matrices in new matrix. 
- 
  
    
      #-(matrix)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Return the difference of two matrices in new matrix. 
- 
  
    
      #==(matrix)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Returns true if and only if the two matrices contain equal elements. 
- 
  
    
      #[](i, j = nil)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Returns element (i,j) of the matrix. 
- 
  
    
      #[]=(i, j = nil, val)  ⇒ Object 
    
    
      (also: #set_element)
    
  
  
  
  
  
  
  
  
  
    Set element (i,j) of the matrix. 
- 
  
    
      #det  ⇒ Object 
    
    
      (also: #determinant)
    
  
  
  
  
  
  
  
  
  
    Returns the determinant of the matrix. 
- 
  
    
      #each  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Each method. 
- 
  
    
      #initialize(rows, cols, *nums)  ⇒ Matrix 
    
    
  
  
  
    constructor
  
  
  
  
  
  
  
    ———Initialize the matrix—— 1. 
- 
  
    
      #inverse  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Returns the inverse of the matrix. 
- 
  
    
      #inversed  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Chanege matrix to its inversed. 
- 
  
    
      #multiply_row(matrix, index, number)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Multiplication of matrix row with number. 
- 
  
    
      #transpose  ⇒ Object 
    
    
      (also: #t)
    
  
  
  
  
  
  
  
  
  
    Transpose the matrix. 
- 
  
    
      #transposed  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Return a new matrix which is the transposition of the given one. 
Methods included from Properties
#col, #col_length, #row, #row_change, #row_length, #to_a
Constructor Details
#initialize(rows, cols, *nums) ⇒ Matrix
———Initialize the matrix——
1. Matrix with values
   Matrix.new(rows, cols, numbers) // numbers = rows*cols
2. Matrix only with dimention(rows and cols) make Identity matrix
   Matrix.new(rows, cols)
| 15 16 17 18 19 20 21 22 23 24 25 26 27 | # File 'lib/matrix_gem.rb', line 15 def initialize(rows, cols, *nums) if rows < 1 || cols < 1 raise MatrixArgumentError, "Rows and Columns should be positive numbers!" elsif nums.length == 0 @matrix = identity cols elsif rows * cols == nums.length @matrix = matrix_with_values nums, cols else raise MatrixArgumentError, "Wrong number of arguments (#{2 + nums.length} for #{2 + rows * cols})" end @matrix end | 
Instance Method Details
#*(m) ⇒ Object
Matrix multiplication.
| 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | # File 'lib/matrix_gem.rb', line 99 def *(m) case(m) when Numeric new_matrix_values = [] self.each { |x| new_matrix_values << x * m } Matrix.new self.m, self.n, *(new_matrix_values) when Matrix multiply_validation self, m rows = Array.new(self.m) { |i| Array.new(m.n) { |j| (0 ... m.n ).inject(0) do |vij, k| vij + self[i, k] * m[k, j] end } } end end | 
#+(matrix) ⇒ Object
Return the sum of two matrices in new matrix
| 30 31 32 33 34 35 | # File 'lib/matrix_gem.rb', line 30 def +(matrix) sum_validation(matrix, self) values = self.zip(matrix).map{|i| i.inject(:+)} Matrix.new self.m, self.n, *(values) end | 
#-(matrix) ⇒ Object
Return the difference of two matrices in new matrix
| 38 39 40 41 42 43 | # File 'lib/matrix_gem.rb', line 38 def -(matrix) sum_validation(matrix, self) values = self.zip(matrix).map{|i| i.inject(:-)} Matrix.new self.m, self.n, *(values) end | 
#==(matrix) ⇒ Object
Returns true if and only if the two matrices contain equal elements.
| 94 95 96 | # File 'lib/matrix_gem.rb', line 94 def ==(matrix) matrix.to_a == self.to_a end | 
#[](i, j = nil) ⇒ Object
Returns element (i,j) of the matrix. That is: row i, column j. If only i is given return row. That is: row with index i.
| 47 48 49 50 51 52 53 | # File 'lib/matrix_gem.rb', line 47 def [](i, j = nil) if j == nil @matrix[i] else @matrix[i][j] end end | 
#[]=(i, j = nil, val) ⇒ Object Also known as: set_element
Set element (i,j) of the matrix. That is: row i, column j. Set row i of the matrix if j is not given. That is: column j. Also aliased as set_element
| 58 59 60 61 62 63 64 65 | # File 'lib/matrix_gem.rb', line 58 def []=(i, j = nil, val) if j == nil raise ErrDimensionMismatch if val.length != self.m @matrix[i] = val else @matrix[i][j] = val end end | 
#det ⇒ Object Also known as: determinant
Returns the determinant of the matrix. Also alised as determinant()
| 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | # File 'lib/matrix_gem.rb', line 119 def det is_square_validation self _this = copy(self) c = 1 new_matrix = nil size = _this.n (0..size - 2).each do |i| (i + 1..size -1).each do |j| if _this[i][i] == 0 (i+1..size-1).each do |k| if _this[k,i] != 0 swap_rows(_this, k, i) c *= -1 end end end if _this[i,i] == 0 p _this return 0 end new_matrix = cauchy_method(_this, i, j, -_this[j,i]/_this[i,i].to_f) end end det = 1 (0..size-1).each do |i| det *= new_matrix[i][i] end det *= c det.round end | 
#each ⇒ Object
Each method.
| 85 86 87 88 89 90 91 | # File 'lib/matrix_gem.rb', line 85 def each @matrix.each do |sub_arr| sub_arr.each do |value| yield value end end end | 
#inverse ⇒ Object
Returns the inverse of the matrix.
| 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | # File 'lib/matrix_gem.rb', line 163 def inverse is_square_validation self raise ErrZeroDeterminant if self.det == 0 _this = copy(self) c = 1 e = Matrix.new _this.m, _this.n size = _this.m (0..size-2).each do |i| (i+1..size-1).each do |j| if _this[i, i] == 0 (i..size-2).each do |k| if _this[k, i] != 0 swap_rows(_this, k, i) swap_rows(e, k, i) c *= -1 end end end return 0 if _this[i, i] == 0 cauchy_method(e, i, j, -_this[j, i]/_this[i, i].to_f) cauchy_method(_this, i, j, -_this[j, i]/_this[i, i].to_f) end end (0..size-2).each do |i| (i+1..size-1).each do |j| cauchy_method(e, size-i-1, size-j-1, -_this[size-j-1, size-i-1]/_this[size-i-1, size-i-1]) cauchy_method(_this, size-i-1, size-j-1, -_this[size-j-1, size-i-1]/_this[size-i-1, size-i-1]) end end (0..size-1).each do |i| e.row_change i, multiply_row(e, i, 1/_this[i,i]) _this.row_change i, multiply_row(_this, i, 1/_this[i,i]) end e end | 
#inversed ⇒ Object
Chanege matrix to its inversed.
| 208 209 210 211 212 | # File 'lib/matrix_gem.rb', line 208 def inversed elements = [] self.inverse.each{ |x| elements << x} @matrix = elements.each_slice(@matrix[0].length).to_a end | 
#multiply_row(matrix, index, number) ⇒ Object
Multiplication of matrix row with number.
| 158 159 160 | # File 'lib/matrix_gem.rb', line 158 def multiply_row(matrix, index, number) matrix = matrix.row(index).map{ |n| n*number } end | 
#transpose ⇒ Object Also known as: t
Transpose the matrix. Also aliased as t().
| 77 78 79 80 81 | # File 'lib/matrix_gem.rb', line 77 def transpose elements = [] @matrix.to_a.transpose.map{ |x| x.map{ |y| elements << y } } @matrix = elements.each_slice(@matrix[0].length).to_a end |