Version: dev

This is the documentation for the development version. For the latest release, see v0.14.0.

expression_names

Stable function · Display

Tier: stable · Kind: function · Topic: Display

JuliaTypeScriptPythonRustGo
expression_namesexpressionNamesexpression_namesexpression_namesExpressionNames

Julia

expression_names(expr)
expression_names(expr) -> Vector{String}

The variable names an expression or an equation uses, in order of first use: its string leaves under lhs, rhs and args. These are the names to give print_math to show an expression on its own, with its names declared.

expr is a decoded ESM expression or equation (the value print_math takes), or a typed ASTExpr or Equation .

julia> expression_names(Dict("lhs" => "y", "rhs" => Dict("op" => "*", "args" => ["k", "y"])))
2-element Vector{String}:
 "y"
 "k"

Source

Example (display)

# print_math takes an expression or equation in its JSON form, as in the
# decoded document, and the names it may use: a run of letters is one name
# only when it is declared.
decoded = JSON3.read(to_json(doc), Dict{String, Any})
decay_eq = decoded["models"]["Decay"]["equations"][1]
names = expression_names(decay_eq)
@assert names == ["N", "lambda"]
println(print_math(decay_eq, names, :markdown))
@assert print_math(decay_eq, names, :markdown) == raw"\frac{\mathrm{d} N}{\mathrm{d} t} = \left(-\lambda\right) N"
@assert print_math(decay_eq, names, :typst) == "(dif N)/(dif t) = (-lambda) N"

# `show` of a typed expression or equation is its Typst math.
@assert string(model.equations[1]) == "(dif N)/(dif t) = (-lambda) N"
@assert string(model.equations[2].rhs) == "ln(2)/lambda"

From docs/api/examples/julia/documents.jl

TypeScript

function expressionNames(expr: unknown): string[]

The variable names an expression or an equation uses, in order of first use: its string leaves under lhs, rhs and args. These are the names to give printMath to show an expression on its own, with its names declared.

Source

Example (display)

const model = loadPath(fixture('decay.esm')).models!.Decay
const [decay, halfLife] = model.equations

// printMath takes an expression or equation and the names it may use: a run
// of letters is one name only when it is declared.
const names = expressionNames(decay)
assert.deepEqual(names, ['N', 'lambda'])
assert.equal(printMath(decay, names, 'markdown'), '\\frac{\\mathrm{d} N}{\\mathrm{d} t} = \\left(-\\lambda\\right) N')
assert.equal(printMath(decay, names, 'typst'), '(dif N)/(dif t) = (-lambda) N')

// A single expression prints the same way.
assert.equal(printMath(halfLife.rhs, expressionNames(halfLife.rhs), 'typst'), 'ln(2)/lambda')

From docs/api/examples/typescript/display.ts

Python

expression_names(expr: Any) -> list[str]

The variable names an expression or an equation uses, in order of first use: its string leaves under lhs, rhs and args. These are the names to give print_math to show an expression on its own, with its names declared.

expr is an expression or equation in its JSON form (a number, a str, a list or a dict).

Source

Example (display)

# print_math takes an expression or equation in its JSON form, as in the
# decoded document, and the names it may use: a run of letters is one name
# only when it is declared.
decoded = json.loads(earthsci_ast.to_json(doc))
decay_eq, half_life_eq = decoded["models"]["Decay"]["equations"]
names = earthsci_ast.expression_names(decay_eq)
assert names == ["N", "lambda"]
print(earthsci_ast.print_math(decay_eq, names, "markdown"))
print(earthsci_ast.print_math(decay_eq, names, "typst"))
assert earthsci_ast.print_math(decay_eq, names, "markdown") == (
    r"\frac{\mathrm{d} N}{\mathrm{d} t} = \left(-\lambda\right) N"
)
assert earthsci_ast.print_math(decay_eq, names, "typst") == "(dif N)/(dif t) = (-lambda) N"

# LaTeX spells a multi-letter name more readably than Typst does.
half_life_names = earthsci_ast.expression_names(half_life_eq)
assert earthsci_ast.print_math(half_life_eq, half_life_names, "markdown") == (
    r"\mathit{half\_life} = \frac{\ln(2)}{\lambda}"
)

From docs/api/examples/python/documents.py

Rust

pub fn expression_names(expr: &Value) -> Vec<String>

The variable names an expression or an equation uses, in order of first use: its string leaves under lhs, rhs and args. These are the names to give print_math to show an expression on its own, with its names declared.

Source · Rust docs

Example (display)

let doc = load_path(fixture("decay.esm"))?;
let model = &doc.models.as_ref().unwrap()["Decay"];

// print_math takes the JSON form of an expression or equation, and the
// names it may use: a run of letters is one name only when declared.
let decay = serde_json::to_value(&model.equations[0])?;
let names = expression_names(&decay);
assert_eq!(names, ["N", "lambda"]);

let latex = print_math(&decay, &names, DocumentFormat::Markdown);
let typst = print_math(&decay, &names, DocumentFormat::Typst);
println!("{latex}\n{typst}");
assert_eq!(
    latex,
    r"\frac{\mathrm{d} N}{\mathrm{d} t} = \left(-\lambda\right) N"
);
assert_eq!(typst, "(dif N)/(dif t) = (-lambda) N");

// An expression prints the same way, and `Display` is its Typst math.
let rhs = &model.equations[1].rhs;
assert_eq!(rhs.to_string(), "ln(2)/lambda");

From docs/api/examples/rust/display.rs

Go

func ExpressionNames(expr any) []string

ExpressionNames returns the variable names an expression or an equation uses, in order of first use: its string leaves under lhs, rhs and args. These are the names to give PrintMath to show an expression on its own, with its names declared. expr is a decoded expression or equation, or one of this package’s typed values (ExprNode, Equation, …).

Source · Go docs

Example (display)

doc, err := esm.LoadPath(fixture("decay.esm"))
if err != nil {
	log.Fatal(err)
}
model := doc.Models["Decay"]
decay := model.Equations[0]
names := esm.ExpressionNames(decay)
fmt.Println(names)
fmt.Println(esm.PrintMath(decay, names, esm.DocumentMarkdown))
fmt.Println(esm.PrintMath(decay, names, esm.DocumentTypst))

// A single expression prints the same way.
rhs := model.Equations[1].RHS
fmt.Println(esm.PrintMath(rhs, esm.ExpressionNames(rhs), esm.DocumentTypst))

From docs/api/examples/go/display_test.go