Class: Diagonal_Matrix

Inherits:
Matrix
  • Object
show all
Defined in:
lib/matrix_gem/diagonal_matrix.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Matrix

#**, #==, #[], #each, #inverse, #inversed, #minor, #to_str, #transpose, #transposed

Methods included from Properties

#col, #col_length, #diagonal_values, #is_diagonal, #is_square, #orthogonal?, #row, #row_length, #set_col, #set_diagonal, #set_row, #to_a, #to_f, #zero?

Constructor Details

#initialize(rows, cols = nil, *nums) ⇒ Diagonal_Matrix

Creates a matrix by given rows, columns, and nums where the diagonal elements are composed of nums. With given only rows create identity matrix.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/matrix_gem/diagonal_matrix.rb', line 7

def initialize(rows, cols = nil, *nums)
  if cols == nil
    @matrix = identity rows
  elsif nums.length != [rows, cols].min
    raise MatrixArgumentError,
    "Wrong number of arguments (#{2 + nums.length} for #{2 + [rows, cols].min})"
  else
    @matrix = []
    rows.times do |row|
      @matrix[row] = []
      cols.times do |col|
        if row == col
          @matrix[row][col] = nums[row]
        else
          @matrix[row][col] = 0
        end
      end
    end
  end
end

Class Method Details

.diagonal(*nums) ⇒ Object

Creates a matrix where the diagonal elements are composed of nums.



30
31
32
33
# File 'lib/matrix_gem/diagonal_matrix.rb', line 30

def diagonal(*nums)
  size = nums.length
  Diagonal_Matrix.new size, size, *(nums)
end

.identity(n) ⇒ Object



40
41
42
# File 'lib/matrix_gem/diagonal_matrix.rb', line 40

def identity(n)
  Diagonal_Matrix.new n
end

.zero(n) ⇒ Object

Creates a zero matrix with dimension equal to n.



36
37
38
# File 'lib/matrix_gem/diagonal_matrix.rb', line 36

def zero(n)
  Diagonal_Matrix.diagonal(*(Array.new(n, 0)))
end

Instance Method Details

#*(matrix) ⇒ Object

Matrix multiplication. Returns new instance of Diagonal_Matrix. Raises error if product can’t be instance of Diagonal_Matrix.

Raises:



75
76
77
78
79
80
# File 'lib/matrix_gem/diagonal_matrix.rb', line 75

def *(matrix)
  product = super(matrix)
  raise MatrixInvalidValue, "Product of multiplication is not diagonal matrix." if product.diagonal?

  Diagonal_Matrix.new(product.m, product.n, *(product.diagonal_values))
end

#+(matrix) ⇒ Object

Sum values on main diagonal of two matrices. Raises error if ‘matrix’ is not a Matrix or if matrices dimensions mismatch.



58
59
60
61
62
# File 'lib/matrix_gem/diagonal_matrix.rb', line 58

def +(matrix)
  sum_validation(self, matrix)
  values = self.diagonal_values.zip(matrix.diagonal_values).map{ |i| i.inject(:+) }
  Diagonal_Matrix.new self.m, self.n, *(values)
end

#-(matrix) ⇒ Object

Returns the difference of values on main diagonal of two matrices in new matrix. Raises error if ‘matrix’ is not a Matrix or if matrices dimensions mismatch.



66
67
68
69
70
71
# File 'lib/matrix_gem/diagonal_matrix.rb', line 66

def -(matrix)
  sum_validation(self, matrix)
  values = self.diagonal_values.zip(matrix.diagonal_values).map{ |i| i.inject(:-) }

  Diagonal_Matrix.new self.m, self.n, *(values)
end

#/(matrix) ⇒ Object

Matrix division (multiplication by the inverse). Raises error if difference can’t be instance of Diagonal_Matrix. Raises error if matrix is not invertible.

Raises:



85
86
87
88
89
90
# File 'lib/matrix_gem/diagonal_matrix.rb', line 85

def /(matrix)
  diff = super(matrix)
  raise MatrixInvalidValue, "Difference of matrices in not a diagonal matrix." if diff.diagonal?

  Diagonal_Matrix.new(diff.m, diff.n, *(diff.diagonal_values))
end

#[]=(i, j = nil, value) ⇒ Object

Set element on main diagonal



46
47
48
49
50
51
52
53
54
# File 'lib/matrix_gem/diagonal_matrix.rb', line 46

def []=(i, j = nil, value)
  if j != nil && i != j
    raise MatrixIndexOutOfRange,
    "You can set only elements on main diagonal in a diagonal matrix."
  elsif @matrix.size <= i
    raise MatrixIndexOutOfRange
  end
    @matrix[i][i] = value
end

#detObject

Returns the determinant of the matrix.



93
94
95
96
# File 'lib/matrix_gem/diagonal_matrix.rb', line 93

def det
  is_square_validation self
  self.diagonal_values.reduce(:*)
end