Class: RMatrix::PrintTable

Inherits:
Object
  • Object
show all
Defined in:
lib/rmatrix/printing/print_table.rb

Direct Known Subclasses

MatrixTable

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePrintTable

Returns a new instance of PrintTable.



4
5
6
7
8
9
# File 'lib/rmatrix/printing/print_table.rb', line 4

def initialize
  self.row_count = self.column_count = 0
  self.cells = {}
  self.column_justifications = {}
  self.separators = Hash.new(', ')
end

Instance Attribute Details

#cellsObject

Returns the value of attribute cells.



3
4
5
# File 'lib/rmatrix/printing/print_table.rb', line 3

def cells
  @cells
end

#column_countObject

Returns the value of attribute column_count.



3
4
5
# File 'lib/rmatrix/printing/print_table.rb', line 3

def column_count
  @column_count
end

#column_justificationsObject

Returns the value of attribute column_justifications.



3
4
5
# File 'lib/rmatrix/printing/print_table.rb', line 3

def column_justifications
  @column_justifications
end

#row_countObject

Returns the value of attribute row_count.



3
4
5
# File 'lib/rmatrix/printing/print_table.rb', line 3

def row_count
  @row_count
end

#separatorsObject

Returns the value of attribute separators.



3
4
5
# File 'lib/rmatrix/printing/print_table.rb', line 3

def separators
  @separators
end

Instance Method Details

#[](column, row) ⇒ Object



86
87
88
# File 'lib/rmatrix/printing/print_table.rb', line 86

def [](column, row)
  self.cells[[column, row]]
end

#[]=(column, row, value) ⇒ Object



90
91
92
93
94
# File 'lib/rmatrix/printing/print_table.rb', line 90

def []=(column, row, value)
  build_column!(column)
  build_row!(row)
  self.cells[[column, row]] = value
end

#build_column!(idx) ⇒ Object



96
97
98
# File 'lib/rmatrix/printing/print_table.rb', line 96

def build_column!(idx)
  self.column_count = [self.column_count || 0, idx.succ].max
end

#build_row!(idx) ⇒ Object



100
101
102
# File 'lib/rmatrix/printing/print_table.rb', line 100

def build_row!(idx)
  self.row_count = [self.row_count || 0 , idx.succ].max
end

#cell_repr(cell) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/rmatrix/printing/print_table.rb', line 74

def cell_repr(cell)
  case cell
  when nil then ''
  when Numeric then numeric_to_truncated_string(cell)
  else "#{cell}"
  end
end

#column_justification(i) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/rmatrix/printing/print_table.rb', line 47

def column_justification(i)
  case self.column_justifications[i]
  when nil then :right
  when :right then :right
  when :left then :left
  else raise "Unexpected justification for column #{self.column_justifications[i] }"
  end
end

#column_width(column) ⇒ Object



68
69
70
71
72
# File 'lib/rmatrix/printing/print_table.rb', line 68

def column_width(column)
  self.row_count.times.reduce(0) do |agg, row|
    [agg, self.cell_repr(self.cells[[column, row]]).length].max
  end
end

#column_widthsObject



62
63
64
65
66
# File 'lib/rmatrix/printing/print_table.rb', line 62

def column_widths
  column_count.times.map do |i|
    column_width(i)
  end
end

#numeric_to_truncated_string(numeric) ⇒ Object



82
83
84
# File 'lib/rmatrix/printing/print_table.rb', line 82

def numeric_to_truncated_string(numeric)
  numeric.to_s
end

#set_column_separator(column, separator) ⇒ Object



56
57
58
59
60
# File 'lib/rmatrix/printing/print_table.rb', line 56

def set_column_separator(column, separator)
  self.row_count.times do |row|
    self.separators[[column, row]] =  separator
  end
end

#to_sObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rmatrix/printing/print_table.rb', line 11

def to_s
  widths = self.column_widths
  self.row_count.times.map do |row|
    self.column_count.times.flat_map do |column|
      cell_text = cell_repr(self.cells[[column, row]])
      justification = column_justification(column)
      width = widths[column]
      contents = case justification
      when :left  then cell_text.ljust(width)
      when :right then cell_text.rjust(width)
      end
      [contents,self.separators[[column, row]]]
    end[0...-1].join
  end.join("\n")
end

#to_texObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rmatrix/printing/print_table.rb', line 27

def to_tex
  tex_map = self.row_count.times.map do |row|
    self.column_count.times.map do |column|
      cell_repr(self.cells[[column, row]])
    end
  end
  <<-TEX
\\[
  \\text{Mat}_{\\varphi\\text{ to }M} = \\kbordermatrix{
& c_1 & c_2 & c_3 & c_4 & c_5 \\\\
r_1 & 1 & 1 & 1 & 1 & 1 \\\\
r_2 & 0 & 1 & 0 & 0 & 1 \\\\
r_3 & 0 & 0 & 1 & 0 & 1 \\\\
r_4 & 0 & 0 & 0 & 1 & 1 \\\\
r_5 & 0 & 0 & 0 & 0 & 1
  }
\\]
TEX
end