equation
Stable type · Document types
Tier: stable · Kind: type · Topic: Document types
| Julia | TypeScript | Python | Rust | Go |
|---|---|---|---|---|
Equation | not exported | Equation | Equation | Equation |
Julia
struct Equation
Equation(lhs::ASTExpr, rhs::ASTExpr; _comment)
Equation(lhs::ASTExpr, rhs::ASTExpr; _comment=nothing)
Mathematical equation with left-hand side and right-hand side expressions.
Used for differential equations and algebraic constraints.
Optional _comment provides a human-readable description.
TypeScript
TypeScript does not export this type.
Python
class Equation
Equation(lhs: Expr, rhs: Expr, _comment: str | None = None)
Mathematical equation with left and right hand sides.
Example (edit)
# Edits return a new model in `modified_object`; the input is left unchanged.
edited = earthsci_ast.add_variable(
model, "N0", ModelVariable(type="parameter", units="mol", default=100.0)
).modified_object
edited = earthsci_ast.add_variable(
edited, "fraction", ModelVariable(type="unknown", units="1")
).modified_object
step = earthsci_ast.add_equation(
edited, Equation(lhs="fraction", rhs=ExprNode(op="/", args=["N", "N0"]))
)
assert step.success, step.errors
doc.models["Decay"] = step.modified_object
assert "fraction" not in model.variables # the original model is untouched
assert earthsci_ast.validate(doc).is_valid
assert earthsci_ast.observed_unknowns(doc.models["Decay"]) == ["fraction", "half_life"]
text = earthsci_ast.to_json(doc)
assert json.loads(text)["models"]["Decay"]["equations"][-1] == {
"lhs": "fraction",
"rhs": {"op": "/", "args": ["N", "N0"]},
}
with tempfile.TemporaryDirectory() as tmp:
out = Path(tmp) / "decay_edited.esm"
earthsci_ast.write_path(doc, out)
reread = earthsci_ast.load_path(out)
assert list(reread.models["Decay"].variables) == ["N", "lambda", "half_life", "N0", "fraction"]
Rust
pub struct Equation {
pub lhs: Expr,
pub rhs: Expr,
pub comment: Option<String>,
}
Differential equation
Go
type Equation struct {
LHS Expression `json:"lhs"`
RHS Expression `json:"rhs"`
// Comment is the schema-sanctioned `_comment` annotation (esm-schema.json
// `$defs/Equation`): free prose the author attached to THIS equation. The
// core carries no meaning for it, but it is authored content, so dropping
// it on parse → emit silently strips every equation annotation from a
// document edited through this binding. `omitempty`: the key is optional
// and absent from the overwhelming majority of equations, so emitting a
// null (or an empty string) for the unannotated case would add noise to
// every document — and `"_comment": null` fails the schema's `string` type.
//
// Rebuilds that construct a NEW Equation from an old one (substitution,
// namespacing, flattening) must carry it across explicitly; a fresh
// Equation{LHS, RHS} literal would drop it.
Comment *string `json:"_comment,omitempty"`
}
func (e *Equation) UnmarshalJSON(data []byte) error
Equation represents a mathematical equation with LHS and RHS
Methods
func (e *Equation) UnmarshalJSON(data []byte) error
Custom JSON unmarshaling for Equation