mirror of
https://github.com/aziis98/ro-vis.git
synced 2026-10-18 11:52:20 +00:00
enhancements to the latex cards
This commit is contained in:
@@ -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
|
||||
@@ -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
|
||||
|
||||
@@ -2,8 +2,6 @@ import 'katex/dist/katex.min.css'
|
||||
import katex from 'katex'
|
||||
|
||||
export const KaTeX = ({ source }) => {
|
||||
console.log(source)
|
||||
|
||||
return (
|
||||
<div
|
||||
class="katex"
|
||||
|
||||
@@ -1,14 +1,30 @@
|
||||
import { KaTeX } from './KaTeX.jsx'
|
||||
|
||||
const StepBlock = ({ type, content }) => {
|
||||
if (!['text', 'code', 'math'].includes(type)) {
|
||||
throw new Error(`Invalid step block type: ${type}`)
|
||||
}
|
||||
|
||||
return type === 'text' ? (
|
||||
<p>{content}</p>
|
||||
) : type === 'code' ? (
|
||||
<pre>
|
||||
<code>{content}</code>
|
||||
</pre>
|
||||
) : type === 'math' ? (
|
||||
<KaTeX source={content} />
|
||||
) : null
|
||||
}
|
||||
|
||||
export const Steps = ({ steps }) => {
|
||||
return (
|
||||
<div class="steps">
|
||||
{steps.map((step, i) => (
|
||||
{steps.map(step => (
|
||||
<div class="step">
|
||||
<div class="label">{step.label}</div>
|
||||
<div class="state">
|
||||
{Object.entries(step.state).map(([key, value]) => (
|
||||
<KaTeX source={`${key} = ${value}`} />
|
||||
<div class="title">{step.title}</div>
|
||||
<div class="content">
|
||||
{step.blocks.map((block, i) => (
|
||||
<StepBlock key={i} {...block} />
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -17,7 +17,10 @@ export async function evalRuby(code) {
|
||||
await preloadRuby()
|
||||
|
||||
window.rubyOutputs = []
|
||||
|
||||
console.time('Ruby eval')
|
||||
const result = vmInstance.eval(code)
|
||||
console.timeEnd('Ruby eval')
|
||||
|
||||
return {
|
||||
result,
|
||||
|
||||
+2
-2
@@ -57,7 +57,7 @@ h6 {
|
||||
border-radius: 0.75rem;
|
||||
|
||||
background: #fff;
|
||||
border: 2px solid #333;
|
||||
border: 2px solid #888;
|
||||
|
||||
width: 10rem;
|
||||
min-height: calc(10rem * 3 / 4);
|
||||
@@ -95,7 +95,7 @@ h6 {
|
||||
|
||||
min-width: 20rem;
|
||||
|
||||
& > .label {
|
||||
& > .title {
|
||||
font-weight: 500;
|
||||
font-size: 15px;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user