Class: Carbuncle::Matrix

Inherits:
Object show all
Includes:
Enumerable
Defined in:
gems/carbuncle-math/mrblib/matrix.rb

Overview

A matrix is a table of numbers, in a 4x4 grid. Useful for doing some mathematical operations.

Defined Under Namespace

Classes: Column, Line, Row

Constant Summary

Constants included from Enumerable

Enumerable::NONE

Instance Method Summary collapse

Methods included from Enumerable

__update_hash, #all?, #any?, #chain, #collect, #count, #cycle, #detect, #drop, #drop_while, #each_cons, #each_slice, #each_with_index, #each_with_object, #entries, #filter_map, #find_all, #find_index, #first, #flat_map, #grep, #group_by, #hash, #include?, #inject, #lazy, #max, #max_by, #min, #min_by, #minmax, #minmax_by, #none?, #one?, #partition, #reject, #reverse_each, #sort, #sort_by, #sum, #take, #take_while, #tally, #to_h, #uniq, #zip

Constructor Details

#initializeself

Creates a new matrix, the values are the values of an Identity matrix.



# File 'gems/carbuncle-math/mrblib/matrix.rb', line 63

Instance Method Details

#columnsObject



89
90
91
92
93
94
95
96
# File 'gems/carbuncle-math/mrblib/matrix.rb', line 89

def columns
  @columns ||= [
    Carbuncle::Matrix::Column.new(self, 0),
    Carbuncle::Matrix::Column.new(self, 1),
    Carbuncle::Matrix::Column.new(self, 2),
    Carbuncle::Matrix::Column.new(self, 3)
  ]
end

#each(&block) ⇒ Object



76
77
78
# File 'gems/carbuncle-math/mrblib/matrix.rb', line 76

def each(&block)
  rows.map(&:to_a).flatten.each(&block)
end

#initialize_copy(matrix) ⇒ self

Creates a new matrix, with the same values as the one given.

Returns:

  • (self)


76
77
78
# File 'gems/carbuncle-math/mrblib/matrix.rb', line 76

def each(&block)
  rows.map(&:to_a).flatten.each(&block)
end

#inspectObject



102
103
104
105
106
107
108
109
110
111
# File 'gems/carbuncle-math/mrblib/matrix.rb', line 102

def inspect
  result = <<-HEREDOC
    Matrix:
      #{rows[0].to_a.join(', ')},
      #{rows[1].to_a.join(', ')},
      #{rows[2].to_a.join(', ')},
      #{rows[3].to_a.join(', ')},
  HEREDOC
  result.strip_heredoc
end

#rowsObject



80
81
82
83
84
85
86
87
# File 'gems/carbuncle-math/mrblib/matrix.rb', line 80

def rows
  @rows ||= [
    Carbuncle::Matrix::Row.new(self, 0),
    Carbuncle::Matrix::Row.new(self, 1),
    Carbuncle::Matrix::Row.new(self, 2),
    Carbuncle::Matrix::Row.new(self, 3)
  ]
end

#sizeObject



113
114
115
# File 'gems/carbuncle-math/mrblib/matrix.rb', line 113

def size
  16
end

#to_sObject



98
99
100
# File 'gems/carbuncle-math/mrblib/matrix.rb', line 98

def to_s
  inspect
end