|
1 | 1 | module MatrixFilters |
| 2 | + CELL_SIZE = 7 |
| 3 | + T_COLORS = { |
| 4 | + 1 => '#c00', |
| 5 | + 2 => '#e66a00', |
| 6 | + 3 => '#c0a000', |
| 7 | + 4 => '#008800', |
| 8 | + 5 => '#0070c0', |
| 9 | + 6 => '#00c' |
| 10 | + }.freeze |
| 11 | + |
2 | 12 | def latex_matrix(input, transpose = false) |
| 13 | + rows = matrix_rows(input, transpose) |
| 14 | + return input if rows.empty? |
| 15 | + |
| 16 | + latex_from_rows(rows) |
| 17 | + end |
| 18 | + |
| 19 | + def matrix_preview(input, transpose = false, force_svg = false, column_limit = 20) |
| 20 | + rows = matrix_rows(input, transpose) |
| 21 | + return input if rows.empty? |
| 22 | + |
| 23 | + force_svg = force_svg || rows.first.length > column_limit.to_i |
| 24 | + force_svg ? minimal_matrix_svg(rows) : "$#{latex_from_rows(rows)}$" |
| 25 | + end |
| 26 | + |
| 27 | + def strip_times(input) |
| 28 | + input.gsub(/\*/, '') |
| 29 | + end |
| 30 | + |
| 31 | + def normalize_grouped_superscripts(input) |
| 32 | + input.gsub(/\^\(([^()]*)\)/, '^{\1}') |
| 33 | + end |
| 34 | + |
| 35 | + def matrix_rows(input, transpose = false) |
3 | 36 | columns = parse_brace_list(input.to_s.strip) |
4 | | - return input if columns.empty? |
| 37 | + return [] if columns.empty? |
5 | 38 |
|
6 | 39 | widths = columns.map(&:length).uniq |
7 | 40 | raise ArgumentError, "matrix columns must all have the same length" unless widths.length == 1 |
8 | 41 |
|
9 | | - rows = transpose ? columns.transpose : columns |
| 42 | + transpose ? columns.transpose : columns |
| 43 | + end |
| 44 | + |
| 45 | + def latex_from_rows(rows) |
10 | 46 | rows = rows.map do |row| |
11 | 47 | row.map { |entry| normalize_grouped_superscripts(entry) }.join(' & ') |
12 | 48 | end |
13 | 49 | "\\begin{pmatrix}#{rows.join(' \\\\ ')}\\end{pmatrix}".gsub(/\*/, '') |
14 | 50 | end |
15 | 51 |
|
16 | | - def strip_times(input) |
17 | | - input.gsub(/\*/, '') |
| 52 | + def minimal_matrix_svg(rows) |
| 53 | + width = rows.first.length * CELL_SIZE |
| 54 | + height = rows.length * CELL_SIZE |
| 55 | + black = +'' |
| 56 | + t_paths = Hash.new { |paths, exponent| paths[exponent] = +'' } |
| 57 | + |
| 58 | + rows.each_with_index do |row, y_index| |
| 59 | + row.each_with_index do |entry, x_index| |
| 60 | + next if entry == '0' |
| 61 | + |
| 62 | + x = x_index * CELL_SIZE |
| 63 | + y = y_index * CELL_SIZE |
| 64 | + path = "M#{x} #{y}h#{CELL_SIZE}v#{CELL_SIZE}h-#{CELL_SIZE}z" |
| 65 | + exponent = t_exponent(entry) |
| 66 | + exponent ? t_paths[exponent] << path : black << path |
| 67 | + end |
| 68 | + end |
| 69 | + |
| 70 | + svg = %(<svg class="minimal-matrix" width="#{width}" height="#{height}" viewBox="0 0 #{width} #{height}" role="img" aria-label="#{rows.length} by #{rows.first.length} matrix preview" shape-rendering="crispEdges">) |
| 71 | + svg << %(<path fill="#000" d="#{black}"/>) unless black.empty? |
| 72 | + T_COLORS.each do |exponent, color| |
| 73 | + path = t_paths[exponent] |
| 74 | + svg << %(<path fill="#{color}" d="#{path}"/>) unless path.empty? |
| 75 | + end |
| 76 | + svg << %(</svg>) |
18 | 77 | end |
19 | 78 |
|
20 | | - def normalize_grouped_superscripts(input) |
21 | | - input.gsub(/\^\(([^()]*)\)/, '^{\1}') |
| 79 | + def t_exponent(entry) |
| 80 | + exponents = entry.scan(/T(?:\^\(?(-?\d+)\)?)?/).map do |match| |
| 81 | + match[0].nil? || match[0].empty? ? 1 : match[0].to_i |
| 82 | + end |
| 83 | + return nil if exponents.empty? |
| 84 | + |
| 85 | + [[exponents.max, 1].max, 6].min |
22 | 86 | end |
23 | 87 |
|
24 | 88 | def parse_brace_list(source) |
|
0 commit comments