diff --git a/src/algorithms/simplesso-primale-algebrico/example.rb b/experiments/example.rb similarity index 100% rename from src/algorithms/simplesso-primale-algebrico/example.rb rename to experiments/example.rb diff --git a/experiments/foo.rb b/experiments/foo.rb new file mode 100644 index 0000000..d63acfe --- /dev/null +++ b/experiments/foo.rb @@ -0,0 +1,35 @@ +class CardDSL + def initialize + @blocks = [] + end + + def text(content) + @blocks << { type: "text", content: content } + end + + def code(content) + @blocks << { type: "code", content: content } + end + + def math(content) + @blocks << { type: "math", content: content } + end + + def blocks + @blocks + end +end + +def print_card(&block) + card = CardDSL.new + card.instance_eval(&block) + + card.blocks.each { |block| p block } + # JS.global[:rubyOutputs].push(card) +end + +print_card do + text "Hello, world!" + code "puts 'Hello, world!'" + math "\\begin{bmatrix} 1 & 2 \\\\ 3 & 4 \\end{bmatrix}" +end diff --git a/src/algorithms/simplesso-primale-algebrico/algorithm.rb b/src/algorithms/simplesso-primale-algebrico/algorithm.rb index 1561774..4ffc412 100644 --- a/src/algorithms/simplesso-primale-algebrico/algorithm.rb +++ b/src/algorithms/simplesso-primale-algebrico/algorithm.rb @@ -35,11 +35,35 @@ Vector.class_eval do end end -def print_step(label, state) - puts "Ruby: #{label} #{state}" +class CardDSL + def initialize + @blocks = [] + end + + def text(content) + @blocks << { type: "text", content: content } + end + + def code(content) + @blocks << { type: "code", content: content } + end + + def math(content) + @blocks << { type: "math", content: content } + end + + def blocks + @blocks + end +end + +def print_card(title, &block) + card = CardDSL.new + card.instance_eval(&block) + JS.global[:rubyOutputs].push({ - label: label, - state: state, + title: title, + blocks: card.blocks, }) end @@ -51,41 +75,63 @@ def run(config) # set of initial indices basis = [1, 2] # [2, 3], ruby is 0-indexed - print_step "Initial", { - "c": c.to_latex, - "A": mat.to_latex, - "b": b.to_latex, - "\\mathcal B": "\\{ #{basis.map { |i| i + 1 }.join(", ")} \\}", - } + print_card "Initial" do + math <<-LATEX + \\begin{aligned} + c &= #{c.to_latex} \\\\ + A &= #{mat.to_latex} \\\\ + b &= #{b.to_latex} \\\\ + \\mathcal B &= \\{ #{basis.map { |i| i + 1 }.join(", ")} \\} + \\end{aligned} + LATEX + end 10.times do mat_basis = mat.submatrix(basis, (0...mat.column_count)) mat_basis_inv = mat_basis.inv - print_step "Step", { - "\\mathcal B": "\\{ #{basis.map { |i| i + 1 }.join(", ")} \\}", - "A_\\mathcal B": mat_basis.to_latex, - "A_\\mathcal B^{-1}": mat_basis_inv.to_latex, - } + print_card "Step" do + math <<-LATEX + \\begin{aligned} + \\mathcal B &= \\{ #{basis.map { |i| i + 1 }.join(", ")} \\} \\\\ + A_\\mathcal B &= #{mat_basis.to_latex} \\\\ + A_\\mathcal B^{-1} &= #{mat_basis_inv.to_latex} + \\end{aligned} + LATEX + end b_basis = Vector.elements(basis.map { |i| b[i] }) x_bar = mat_basis_inv * b_basis - print_step "Step", { - "b_\\mathcal B": b_basis.to_latex, - "\\bar x": "#{mat_basis_inv.to_latex} #{b_basis.to_latex} = #{x_bar.to_latex}", - } + print_card "Step" do + math <<-LATEX + \\begin{aligned} + b_\\mathcal B &= #{b_basis.to_latex} \\\\ + \\bar x &= #{mat_basis_inv.to_latex} #{b_basis.to_latex} = #{x_bar.to_latex} + \\end{aligned} + LATEX + end y_bar_basis = mat_basis_inv * c - print_step "Step", { - "\\bar y_\\mathcal B": "#{mat_basis_inv.to_latex} #{c.to_latex} = #{y_bar_basis.to_latex}", - } + print_card "Step" do + math <<-LATEX + \\begin{aligned} + \\bar y_\\mathcal B &= #{mat_basis_inv.to_latex} #{c.to_latex} = #{y_bar_basis.to_latex} + \\end{aligned} + LATEX + end if y_bar_basis.all? { |y| y >= 0 } - print_step "Step", { - "\\bar y_\\mathcal B": "#{y_bar_basis.to_latex} \\geq 0", - } + print_card "Step" do + text "Optimal solution found." + math <<-LATEX + \\begin{aligned} + \\bar y_\\mathcal B = #{y_bar_basis.to_latex} \\geq 0 + \\end{aligned} + LATEX + end + break end @@ -94,33 +140,28 @@ def run(config) h = basis[(0...basis.size).to_a.find { |i| y_bar_basis[i] < 0 }] - # print_step "Step", { - # "\\mathcal B": "\\{ #{basis.map { |i| i + 1 }.join(", ")} \\}", - # "\\bar y_\\mathcal B": y_bar_basis.to_latex, - # "h": "\\min \\{ i \\in \\mathcal B : \\bar y_i < 0 \\} = #{h + 1}", - # } - - print_step "Step", [ - "Lorem ipsum dolor sit amet, consectetur adipiscing elit.", - %Q( - $$ - \\begin{align*} + print_card "Step" do + math <<-LATEX + \\begin{aligned} \\mathcal B &= \\{ #{basis.map { |i| i + 1 }.join(", ")} \\} \\\\ \\bar y_\\mathcal B &= #{y_bar_basis.to_latex} \\\\ h &= \\min \\{ i \\in \\mathcal B : \\bar y_i < 0 \\} = #{h + 1} - \\end{align*} - $$ - ), - ] + \\end{aligned} + LATEX + end u = Vector.basis(size: basis.size, index: h) xi = -mat_basis_inv * u - print_step "Step", { - "u_{\\mathcal B(h)}": "(e_h)_\\mathcal B = #{u.to_latex}", - "\\xi": "#{mat_basis_inv.to_latex} #{u.to_latex} = #{xi.to_latex}", - } + print_card "Step" do + math <<-LATEX + \\begin{aligned} + u_{\\mathcal B(h)} &= (e_h)_\\mathcal B = #{u.to_latex} \\\\ + \\xi &= A_\\mathcal B^{-1} u_{\\mathcal B(h)} = #{mat_basis_inv.to_latex} #{u.to_latex} = #{xi.to_latex} + \\end{aligned} + LATEX + end basis_op = (0...mat.row_count).to_a - basis @@ -128,9 +169,15 @@ def run(config) d = mat_basis_op * xi - print_step "Step", { - "d": "#{mat_basis_op.to_latex} #{xi.to_latex} = #{d.to_latex} \\leq 0", - } + print_card "Step" do + math <<-LATEX + \\begin{aligned} + N &= \\{ #{basis.map { |i| i + 1 }.join(", ")} \\} \\setminus \\{ #{h + 1} \\} \\\\ + A_N &= #{mat_basis_op.to_latex} \\\\ + d &= A_N \\xi = #{mat_basis_op.to_latex} #{xi.to_latex} = #{d.to_latex} + \\end{aligned} + LATEX + end if d.all? { |d| d <= 0 } break diff --git a/src/components/KaTeX.jsx b/src/components/KaTeX.jsx index e011167..20f49e8 100644 --- a/src/components/KaTeX.jsx +++ b/src/components/KaTeX.jsx @@ -2,8 +2,6 @@ import 'katex/dist/katex.min.css' import katex from 'katex' export const KaTeX = ({ source }) => { - console.log(source) - return (
{content}
+ ) : type === 'code' ? ( +
+ {content}
+
+ ) : type === 'math' ? (
+