Project

EarthSciASTDiff

Analytical sparse Jacobians for EarthSciAST (.esm) models by symbolic differentiation of the expression AST.

Contents

This is the documentation for the older version v0.7.0. For the latest release, see v0.8.0.

cse_templates

Stable function · Serialize as a document block

Tier: stable · Kind: function · Topic: Serialize as a document block

JuliaPythonRust
cse_templatescse_templatescse_templates

Julia

cse_templates(exprs::Vector{EarthSciAST.ASTExpr}; min_nodes, prefix)
cse_templates(exprs; min_nodes = 12, prefix = "jt")
    -> (templates::OrderedDict{String,ASTExpr}, rewritten::Vector{ASTExpr})

Extract repeated subtrees of exprs by the rule of esm-jacobian-spec §4.1. Every structurally distinct subtree is visited once, largest first (ties in first-encounter post-order over the inputs). Its count is its number of occurrences that remain once every larger extracted subtree is replaced by a reference and kept once as a template body. A subtree that spans at least min_nodes AST nodes and still counts at least 2 is extracted, named "$(prefix)$k" in visiting order, and every occurrence (in the inputs and in larger template bodies) is replaced by an apply_expression_template reference. The result is a reference DAG, acyclic by strict containment, and a body only references templates named after it. Θ(distinct subtrees · log) after one walk of the inputs (a subtree shared in memory is walked once).

Source

Python

cse_templates(exprs: list[Expr], min_nodes: int = 12, prefix: str = 'jt')

Extract repeated subtrees by the rule of esm-jacobian-spec §4.1 (the reference’s cse.jl). Every distinct subtree is visited once, largest first (ties in first-encounter order); its count is its number of occurrences left once every larger extracted subtree is a reference whose body is kept once; one of at least min_nodes nodes that still counts at least 2 is extracted and named <prefix><k> in visiting order. Returns (templates: {name: body}, rewritten exprs).

Source

Rust

pub fn cse_templates(exprs: &[Expr], min_nodes: usize, prefix: &str) -> (Vec<(String, Expr)>, Vec<Expr>)

Extract repeated subtrees of exprs by the rule of esm-jacobian-spec §4.1: every distinct subtree is visited once, largest first (ties in first-encounter order); its count is its number of occurrences left once every larger extracted subtree is a reference whose body is kept once; one of at least min_nodes nodes that still counts at least 2 is extracted and named <prefix><k> in visiting order. Byte-identical to the reference.

Source · Rust docs