Version: dev

remove_variable

Stable function · Editing

Tier: stable · Kind: function · Topic: Editing

JuliaTypeScriptPythonRustGo
remove_variableremoveVariableremove_variable, remove_variable_from_modelremove_variableRemoveVariable

Note: spelling differs from the §2 rule: python=remove_variable_from_model (expected remove_variable); multiple spellings exported by: python

Julia

remove_variable(model::Model, name::String)
remove_variable(model::Model, name::String) -> Model

Remove a variable from a model.

Creates a new model without the specified variable. Warns about dependencies but does not automatically update equations that reference the variable. Throws EditError if the variable does not exist.

Source

Example (rename_variable)

renamed = rename_variable(model, "lambda", "k")
@assert parameters(renamed) == ["k"]
@assert to_ascii(renamed.equations[1]) == "D(N)/Dt = (-k) * N"

# Drop the half_life equation, then the variable it defined.
trimmed = remove_variable(remove_equation(model, 2), "half_life")
@assert unknowns(trimmed) == ["N"]
@assert length(trimmed.equations) == 1

# Editing a variable that does not exist raises an EditError.
try
    rename_variable(model, "nope", "k")
    error("expected an EditError")
catch e
    @assert e isa EditError
end

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

TypeScript

function removeVariable(model: Model, name: string): Model

Remove a variable from a model, with reference checking

Parameters

  • model — Model to remove variable from
  • name — Variable name to remove

Returns

New model with variable removed

Throws

  • VariableInUseError if variable is still referenced
  • EntityNotFoundError if variable doesn’t exist

Source

Python

Exported as remove_variable and remove_variable_from_model. Both names refer to the same operation.

remove_variable

remove_variable(model: Model, var_name: str, validate_after_edit: bool = True) -> EditResult

Remove a variable from a model.

Source

remove_variable_from_model

remove_variable_from_model(model: Model, var_name: str, validate_after_edit: bool = True) -> EditResult

Deprecated alias for remove_variable . Remove a variable from a model.

Source

Example (rename_variable)

renamed = earthsci_ast.rename_variable(doc.models["Decay"], "lambda", "k").modified_object
assert "k" in renamed.variables and "lambda" not in renamed.variables
assert earthsci_ast.to_ascii(renamed.equations[1]) == "half_life = log(2) / k"

trimmed = earthsci_ast.remove_variable(renamed, "N0")
assert "N0" not in trimmed.modified_object.variables

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

Rust

pub fn remove_variable(model: &Model, var_name: &str) -> EditResult<Model>

Remove a variable from a model

Arguments

  • model - The model to modify
  • var_name - Name of the variable to remove

Returns

  • EditResult<Model> - New model without the variable

Source · Rust docs

Example (remove_variable)

let model = &reread.models.as_ref().unwrap()["Decay"];
let trimmed = remove_variable(model, "half_life")?;
assert!(!trimmed.variables.contains_key("half_life"));
// Removing a name that is not there is an error, not a no-op.
assert!(remove_variable(&trimmed, "half_life").is_err());

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

Go

func RemoveVariable(model Model, name string) (Model, error)

RemoveVariable returns a copy of model without the variable name, after checking that nothing still references it.

Returns *EntityNotFoundError when the variable does not exist, and *VariableInUseError when it is still read from any expression site (see modelExpressionSites) or written by any event affect. A site reported from more than one position — an equation matching in both lhs and rhs — is collapsed to a single reference, preserving discovery order.

Source · Go docs