Module: Properties
Instance Method Summary collapse
-
#col(index) ⇒ Object
(also: #column)
Returns array with column on index as values.
-
#col_length ⇒ Object
(also: #n, #col_size)
Returns the number of columns.
-
#diagonal_values ⇒ Object
Return array with values on main diagonal.
-
#is_diagonal ⇒ Object
(also: #diagonal?)
Returns true if there is values not equal to 0 only on main diagonal.
-
#is_square ⇒ Object
(also: #square?)
Returns true if this is a matrix with equal rows and columns.
-
#orthogonal? ⇒ Boolean
(also: #is_orthogonal)
Returns true if this is square matrix and its transpose is equal to its inverse.
-
#row(index) ⇒ Object
Retuns array with row on index as values.
-
#row_length ⇒ Object
(also: #m, #row_size)
Returns the number of rows.
-
#set_col(index, elements) ⇒ Object
Set values of matrix column.
-
#set_diagonal(*nums) ⇒ Object
(also: #set_diagonal_values)
Set values on main diagonal.
-
#set_row(index, elements) ⇒ Object
Set values of matrix row.
-
#to_a ⇒ Object
Returns an array of arrays that describe the rows of the matrix.
-
#to_f ⇒ Object
Make all values floating point.
-
#zero? ⇒ Boolean
(also: #is_zero)
Returns true if this is a matrix with only zero elements.
Instance Method Details
#col(index) ⇒ Object Also known as: column
Returns array with column on index as values. Also aliased as column()
72 73 74 75 76 |
# File 'lib/matrix_gem/properties_module.rb', line 72 def col(index) column = [] (0..self.m-1).each{ |x| column << self[x, index] } column end |
#col_length ⇒ Object Also known as: n, col_size
Returns the number of columns. Also aliased as n(), col_size(), column_count()
44 45 46 |
# File 'lib/matrix_gem/properties_module.rb', line 44 def col_length self[0].length end |
#diagonal_values ⇒ Object
Return array with values on main diagonal.
19 20 21 22 23 24 25 26 27 |
# File 'lib/matrix_gem/properties_module.rb', line 19 def diagonal_values size = ([self.m, self.n].min) -1 values = [] (0..size).each do |i| values << self[i][i] end values end |
#is_diagonal ⇒ Object Also known as: diagonal?
Returns true if there is values not equal to 0 only on main diagonal. Also aliased as diagonal?
98 99 100 101 102 103 104 105 |
# File 'lib/matrix_gem/properties_module.rb', line 98 def is_diagonal (0..self.m-1).each do |i| (0..self.m-1).each do |j| return false if ((self[i,j] != 0 && i != j)) end end true end |
#is_square ⇒ Object Also known as: square?
Returns true if this is a matrix with equal rows and columns. Also aliased as square?
118 119 120 121 |
# File 'lib/matrix_gem/properties_module.rb', line 118 def is_square return false if self.m != self.n true end |
#orthogonal? ⇒ Boolean Also known as: is_orthogonal
Returns true if this is square matrix and its transpose is equal to its inverse. Also aliased as is_zero
126 127 128 129 |
# File 'lib/matrix_gem/properties_module.rb', line 126 def orthogonal? return true if ((self.is_square) && (self.transposed == self.inversed)) false end |
#row(index) ⇒ Object
Retuns array with row on index as values.
66 67 68 |
# File 'lib/matrix_gem/properties_module.rb', line 66 def row(index) self[index] end |
#row_length ⇒ Object Also known as: m, row_size
Returns the number of rows. Also aliased as m(), row_size(), row_count()
50 51 52 53 54 |
# File 'lib/matrix_gem/properties_module.rb', line 50 def row_length i = 0 self.each{ |row| i+=1 } i/self[0].length end |
#set_col(index, elements) ⇒ Object
Set values of matrix column. Elements shoud be an array of values. Raise error if length of elements is not equal to matrix column length.
90 91 92 93 94 |
# File 'lib/matrix_gem/properties_module.rb', line 90 def set_col(index, elements) raise MatrixArgumentError, 'Different length of elements and column length' if elements.length != self.col_length (0..self.m-1).each{ |x| self[x, index] = elements[x] } self end |
#set_diagonal(*nums) ⇒ Object Also known as: set_diagonal_values
Set values on main diagonal. Raise error if nums are less than matrix main diagonal length Also aliased as set_diagonal_values
32 33 34 35 36 37 38 39 |
# File 'lib/matrix_gem/properties_module.rb', line 32 def set_diagonal(*nums) size = ([self.m, self.n].min) -1 raise MatrixArgumentError, 'Wrong number of arguments.' if nums.length < size + 1 (0..size).each do |i| self[i,i] = nums[i] end self end |
#set_row(index, elements) ⇒ Object
Set values of matrix row. Elements shoud be an array of values Raise error if length of elements is not equal to matrix row length.
81 82 83 84 85 86 |
# File 'lib/matrix_gem/properties_module.rb', line 81 def set_row(index, elements) raise MatrixArgumentError, 'Different length of elements and row length' if elements.length != self.row_length self[index] = elements self end |
#to_a ⇒ Object
Returns an array of arrays that describe the rows of the matrix.
6 7 8 |
# File 'lib/matrix_gem/properties_module.rb', line 6 def to_a self end |
#to_f ⇒ Object
Make all values floating point.
11 12 13 14 15 16 |
# File 'lib/matrix_gem/properties_module.rb', line 11 def to_f (0..self.n - 1).each{ |i| (0..self.m - 1).each do |j| self[i,j] = self[i,j].to_f end } self end |
#zero? ⇒ Boolean Also known as: is_zero
Returns true if this is a matrix with only zero elements. Also aliased as is_zero
110 111 112 113 |
# File 'lib/matrix_gem/properties_module.rb', line 110 def zero? self.map{ |x| return false if x != 0 } true end |