use crate::codegen;
use oxieml::EmlTree;
#[derive(Debug, Clone)]
pub struct Distilled {
pub latex: String,
pub pretty: String,
pub rust: String,
pub numpy: String,
pub sympy: String,
pub complexity: usize,
}
#[must_use]
pub fn canonical_latex(tree: &EmlTree) -> String {
tree.lower().simplify().to_latex()
}
#[must_use]
pub fn canonical_pretty(tree: &EmlTree) -> String {
format!("{}", tree.lower().simplify())
}
#[must_use]
pub fn distill(tree: &EmlTree) -> Distilled {
Distilled {
latex: canonical_latex(tree),
pretty: canonical_pretty(tree),
rust: codegen::rust_code(tree),
numpy: codegen::numpy_code(tree),
sympy: codegen::sympy_code(tree),
complexity: tree.size(),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn exp_distills_to_clean_forms() {
let tree = EmlTree::eml(&EmlTree::var(0), &EmlTree::one());
let d = distill(&tree);
assert!(
d.latex.contains("e^") || d.latex.contains("exp"),
"latex: {}",
d.latex
);
assert!(
!d.latex.contains("\\ln\\left(1"),
"ln(1) not simplified: {}",
d.latex
);
assert!(d.rust.contains("fn f("));
assert!(d.numpy.contains("np.exp"));
assert!(d.sympy.contains("exp("));
assert_eq!(d.complexity, tree.size());
}
#[test]
fn canonical_latex_matches_solution_latex() {
let tree = oxieml::Canonical::exp(&EmlTree::var(0));
assert_eq!(canonical_latex(&tree), tree.lower().simplify().to_latex());
}
}