Version: v0.14.0

contains

Stable function · Expressions

Tier: stable · Kind: function · Topic: Expressions

JuliaTypeScriptPythonRustGo
not exportedcontainscontainscontainsContains

Julia

Julia does not export this function.

TypeScript

function contains(expr: Expr, varName: string): boolean

Check if an expression contains a specific variable

Parameters

  • expr — Expression to search
  • varName — Variable name to look for

Returns

True if the variable appears in the expression

Source

Example (expressions)

// Expressions are plain JSON trees; build one directly ...
const built: Expr = { op: '*', args: ['k', { op: '-', args: ['T_wall', 'T_air'] }] }

// ... or read one from LaTeX ('markdown') or Typst math, under the names it
// uses: the inverse of `printMath`.
const names = ['k', 'T_wall', 'T_air']
const latex = 'k \\left(T_{\\mathrm{wall}} - T_{\\mathrm{air}}\\right)'
const parsed = parseMath(latex, names, 'markdown') as Expr
assert.deepEqual(parsed, built)
assert.equal(printMath(parsed, names, 'markdown'), latex)
assert.deepEqual([...freeVariables(parsed)].sort(), ['T_air', 'T_wall', 'k'])
assert.equal(contains(parsed, 'T_wall'), true)

// Substitution is single-pass and returns a new tree.
const bound = substitute(parsed, { T_wall: 300, k: 0.05 })
assert.deepEqual(bound, { op: '*', args: [0.05, { op: '-', args: [300, 'T_air'] }] })
assert.deepEqual([...freeVariables(bound)], ['T_air'])
assert.equal(simplify(substitute(parsed, { T_wall: 300, T_air: 300 })), 0)

// Text whose top is `=` reads as an equation.
const eq = parseMath('(dif T_(upright("air")))/(dif t) = k (T_(upright("wall")) - T_(upright("air")))', names, 'typst') as {
  lhs: Expr
  rhs: Expr
}
assert.deepEqual(eq.rhs, built)

// Text that is not math raises a diagnostic error.
assert.throws(() => parseMath('k \\left(T_{\\mathrm{wall}}', names, 'markdown'), EsmDiagnosticError)

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

Python

contains(expr: Expr, var_name: str) -> bool

Check if an expression contains a specific variable.

Parameters

  • expr (Expr) — Expression to search in
  • var_name (str) — Variable name to look for

Returns

  • bool — True if variable is found, False otherwise

Source

Rust

pub fn contains(expr: &Expr, var_name: &str) -> bool

Check if an expression contains a specific variable

Arguments

  • expr - The expression to search
  • var_name - The variable name to look for

Returns

  • true if the variable is found, false otherwise

Source · Rust docs

Example (evaluate)

let expr = read("k A - x", &["k", "A", "x"]);
let bindings = HashMap::from([
    ("k".to_string(), 2.0),
    ("A".to_string(), 3.0),
    ("x".to_string(), 1.0),
]);
assert_eq!(evaluate(&expr, &bindings), Ok(5.0));

// A missing binding is reported by name, not guessed.
let partial = HashMap::from([("k".to_string(), 2.0)]);
let mut unbound = evaluate(&expr, &partial).unwrap_err();
unbound.sort();
assert_eq!(unbound, ["A", "x"]);

assert!(contains(&expr, "A") && !contains(&expr, "B"));
// simplify folds numbers: with k = 2 and A = 3, k A is 6.
let numbers = HashMap::from([
    ("k".to_string(), Expr::Number(2.0)),
    ("A".to_string(), Expr::Number(3.0)),
]);
assert_eq!(simplify(&substitute(&expr, &numbers)).to_string(), "6 - x");

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

Go

func Contains(expr Expression, varName string) bool

Contains checks if a specific variable appears anywhere in an expression.

Source · Go docs